Пример #1
0
void main()
{
	static long address;
	static int retval;
	
	sock_init();
	// Wait for the interface to come up
	while (ifpending(IF_DEFAULT) == IF_COMING_UP) {
		tcp_tick(NULL);
	}

	pop3_init(storemsg);

	printf("Resolving name...\n");
	address = resolve(POP_HOST);
	printf("Calling pop3_getmail()...\n");
	pop3_getmail(POP_USER, POP_PASS, address);

	printf("Entering pop3_tick()...\n");
	while((retval = pop3_tick()) == POP_PENDING)
		continue;

	if(retval == POP_SUCCESS)
		printf("POP was successful!\n");
	if(retval == POP_TIME)
		printf("POP timed out!\n");
	if(retval == POP_ERROR)
		printf("POP returned a general error!\n");
		
	printf("All done!\n");
}
Пример #2
0
void main()
{
	sock_init();
	// Wait for the interface to come up
	while (ifpending(IF_DEFAULT) == IF_COMING_UP) {
		tcp_tick(NULL);
	}

   #if _USER
   	smtp_setserver(SMTP_SERVER);
   #endif

#ifdef USE_SMTP_AUTH
	smtp_setauth ("myusername", "mypassword");
#endif

	smtp_sendmail(TO, FROM, SUBJECT, BODY);

	while(smtp_mailtick()==SMTP_PENDING)
		continue;

	if(smtp_status()==SMTP_SUCCESS)
		printf("Message sent\n");
	else
		printf("Error sending message\n");
}
Пример #3
0
int main()
{
	int line_num;	// This is our data handler opaque parameter.

	sock_init();
	// Wait for the interface to come up
	while (ifpending(IF_DEFAULT) == IF_COMING_UP) {
		tcp_tick(NULL);
	}

   #if _USER
   	smtp_setserver(SMTP_SERVER);
   #endif

#ifdef USE_SMTP_AUTH
	smtp_setauth ("myusername", "mypassword");
#endif

	smtp_sendmail(TO, FROM, SUBJECT, NULL);	// No fixed message
	smtp_data_handler(mail_generator, &line_num, 0);	// Set message generator function

	line_num = 1;	// Initialize for data handler benefit.

	while(smtp_mailtick()==SMTP_PENDING)
		continue;

	if(smtp_status()==SMTP_SUCCESS)
		printf("Message sent\n");
	else
		printf("Error sending message\n");
	return 0;
}
Пример #4
0
/**
 * Does network and HTTP setup
 */
void setupHttp() {
	// IP Buffer
	char buffer[16];
	// User ID
	int userid;

	// Initialize the board
   	brdInit();

	// Initialize the socket
   	sock_init();

	// Wait for IP address to be obtained
	while (ifpending(IF_DEFAULT) == IF_COMING_UP) {
		tcp_tick(NULL);
	}

	// Output the IP Address
	printf("My IP address is %s\n", inet_ntoa(buffer, gethostid()));

	//Set up the HTTP Server
   	http_init();
   	tcp_reserveport(80);

	// Set redirect
	http_set_path("/", "/index.zhtml");

	// Set up authentication
	sspec_addrule("/", "Admin", admin, admin, SERVER_ANY, SERVER_AUTH_BASIC, NULL);

   // Add our users
   // Ario
   userid = sauth_adduser("ario", "fish", SERVER_ANY);
   sauth_setusermask(userid, admin, NULL);
   // Chan
   userid = sauth_adduser("chan", "bar", SERVER_ANY);
   sauth_setusermask(userid, admin, NULL);
   // Jeff
   userid = sauth_adduser("jeff", "bar7", SERVER_ANY);
   sauth_setusermask(userid, admin, NULL);
   // Shea
   userid = sauth_adduser("shea", "bar2", SERVER_ANY);
   sauth_setusermask(userid, admin, NULL);
   // Toby
   userid = sauth_adduser("toby", "bar3", SERVER_ANY);
   sauth_setusermask(userid, admin, NULL);

   //Done
}
Пример #5
0
void main()
{
	int status;
	char buffer[2048];
	longword ip;
	int i, dst, health;
	struct tm	t;
	unsigned long	longsec;


	// open connection to NIST server

	sock_init();
	// Wait for the interface to come up
	while (ifpending(IF_DEFAULT) == IF_COMING_UP) {
		tcp_tick(NULL);
	}

	ip=resolve(NIST_SERVER_IP);
	tcp_open(&s, 0, ip, NIST_PORT, NULL);
	sock_wait_established(&s, NIST_TIMEOUT, NULL, &status);
	sock_mode(&s, TCP_MODE_ASCII);


	// receive and process data -- the server will close the connection,
	// which will cause execution to continue at sock_err below.

	while (tcp_tick(&s)) {
		sock_wait_input(&s, NIST_TIMEOUT, NULL, &status);
		sock_gets(&s, buffer, 48);
	}

sock_err:
	if (status == -1) {
		printf("\nConnection timed out, exiting.\n");
		exit(1);
	}
	if (status != 1) {
		printf("\nUnknown sock_err (%d), exiting.\n", status);
		exit(1);
	}

	sock_close(&s);

	// Dynamic C doesn't have a sscanf function, so we do
	// it this way instead...
	t.tm_year = 100 + 10*(buffer[6]-'0') + (buffer[7]-'0');
	t.tm_mon  = 10*(buffer[9]-'0') + (buffer[10]-'0');
	t.tm_mday = 10*(buffer[12]-'0') + (buffer[13]-'0');
	t.tm_hour = 10*(buffer[15]-'0') + (buffer[16]-'0');
	t.tm_min  = 10*(buffer[18]-'0') + (buffer[19]-'0');
	t.tm_sec  = 10*(buffer[21]-'0') + (buffer[22]-'0');
	dst       = 10*(buffer[24]-'0') + (buffer[25]-'0');
	health    = buffer[27]-'0';

	// convert from tm_struct to seconds since Jan 1, 1980
	// (much easier to adjust for DST and timezone this way)

	longsec = mktime(&t);
	longsec += 3600ul * TIMEZONE;		// adjust for timezone
   dst = (dst >= 1 && dst <= 50 );
	if (dst)
		longsec += 3600ul;	// DST is in effect


	// convert back to tm struct for display to stdio
	mktm(&t, longsec);
	printf("Current time:  %02d:%02d:%02d  %02d/%02d/%04d\n",
				t.tm_hour, t.tm_min, t.tm_sec,
				t.tm_mon, t.tm_mday, 1900 + t.tm_year);
	if (dst)
		printf("Daylight Saving Time is in effect.\n");

	switch (health) {
		case 0:
			printf("Server is healthy.\n");
			break;
		case 1:
			printf("Server may be off by up to 5 seconds.\n");
			break;
		case 2:
			printf("Server is off by more than 5 seconds; not setting RTC.\n");
			break;
		default:
			printf("Server failure has occured; try another server (not setting RTC).\n");
			break;
	}

	// finally, set the RTC if the results seems good
	if (health < 2)
		write_rtc(longsec);

}
Пример #6
0
void main(void)
{
	// index is used to loop through the interfaces
	int index;

	// Initialize the TCP/IP stack
  	sock_init();

   // Initialize the state machine structure
   for (index = 0; index <= VIRTUAL_ETH; index++) {
     	socks[index].state = LSTN_STATE;
      socks[index].iface = 0;
      memset(socks[index].buff, 0, sizeof(socks[index].buff));
      socks[index].bytes = 0;
   }

	// Perform network configuration on the main (physical) Ethernet ineterface
   printf("Bringing up Main Interface %2d:\n", socks[0].iface);
   ifconfig(IF_ETH0,
   			IFS_IPADDR, 	aton(LOCAL_IP),
   			IFS_NETMASK,	aton(LOCAL_NETMASK),
           	IFS_ROUTER_SET,aton(LOCAL_GATEWAY),
            IFS_UP,
          	IFS_END);
   // Wait for the interface to come up
   while (ifpending(IF_ETH0) == IF_COMING_UP) {
		tcp_tick(NULL);
	}
   printf("Main Interface %2d: is up!!\n", socks[0].iface);

	// Configure each of the virtual Ethernet interfaces
   for (index = 1; index <= VIRTUAL_ETH; index++) {
		// virtual_eth() creates a new virtual Ethernet interface and returns
		// the new interface number
   	socks[index].iface = virtual_eth(IF_ETH0, aton(LOCAL_IP) + index,
   	                                 aton(LOCAL_NETMASK), NULL);
      if (socks[index].iface != -1) {
      	printf("Created Virtual Interface %2d:\n", socks[index].iface);
      }
      else {
      	exit(0);
      }
		// Wait for the virtual Ethernet interface to come up
      while (ifpending(socks[index].iface) == IF_COMING_UP) {
			tcp_tick(NULL);
		}
      printf("Virtual Interface %2d: is up!!\n", socks[index].iface);
   }

	// Print out information on the interfaces
	ip_print_ifs();

	// Begin the main program loop
   while (1) {
   	// Iterate over the Ethernet interfaces
   	for (index = 0; index <= VIRTUAL_ETH; index++) {
      	switch (socks[index].state) {
      		// Listen on the socket
      		case LSTN_STATE:
      			// Note that the iface number is passed to tcp_extlisten()
               if (tcp_extlisten(&socks[index].s, socks[index].iface,
                                 LOCAL_PORT, 0, 0, NULL, 0, 0, 0)) {
                  socks[index].state = ESTB_STATE;
                  socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer
                  printf("Interface %2d: listening on port: %5d\n",
                         socks[index].iface, LOCAL_PORT);
               }
               else {
               	// tcp_extlisten() failed--let the user know
                  printf("Interface %2d: tcp_extlisten failed\n",
                         socks[index].iface);
               	socks[index].state = CLSE_STATE;
                  socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer
               }
               break;
           	// Check if a connection has been established
            case ESTB_STATE:
              	if (sock_established(&socks[index].s) ||
              	    sock_bytesready(&socks[index].s) >= 0) {
               	socks[index].state = RECV_STATE;
                  socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer
                  printf("Interface %2d: socket established.\n",
                         socks[index].iface);
               }
               break;
				// Check if data has been received.  If so, read it out.
            case RECV_STATE:
            	// Read any incoming data
            	socks[index].bytes = sock_fastread(&socks[index].s,
            	                                   socks[index].buff,
            	                                   sizeof(socks[index].buff));
               if (socks[index].bytes == -1) {
               	// sock_fastread() returned an error--means that the socket is
               	// likely closed
               	printf("Interface %2d: sock_fastread failed\n",
               	       socks[index].iface);
               	socks[index].state = CLSE_STATE;
                  socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer
               }
               // Check if we received any data
               if (socks[index].bytes > 0) {
               	printf("Interface %2d: revd: %2d bytes\n", socks[index].iface,
               	       socks[index].bytes);
						socks[index].state = SEND_STATE;			// send the data back
                  socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer
            	}
            	break;
            // Echo back any received data
         	case SEND_STATE:
            	socks[index].bytes = sock_fastwrite(&socks[index].s,
            	                                    socks[index].buff,
            	                                    socks[index].bytes);
               if (socks[index].bytes == -1) {
               	// sock_fastwrite() returned an error--means that the socket
               	// is likely closed
               	printf("Interface %2d: sock_fastwrite failed\n",
               	       socks[index].iface);
               	socks[index].state = CLSE_STATE;
                  socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer
               }
               // Check how much data was written.  Note that in this program,
               // if not all the data was written, the remaining data will be
               // dropped.  A more realistic program would try sending the rest
               // of the data later, or using sock_awrite() until the data can
               // be sent.
               if (socks[index].bytes > 0) {
               	printf("Interface %2d: sent: %2d bytes\n",
               	       socks[index].iface, socks[index].bytes);
						socks[index].state = RECV_STATE;
                  socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer
            	}
            	break;
				// Close the socket
        		case CLSE_STATE:
               sock_close(&socks[index].s);
               socks[index].state = CLWT_STATE;
               socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer
               break;
				// Wait for the socket to completely close
            case CLWT_STATE:
					if (!sock_alive(&socks[index].s)) {
                  printf("Interface %2d: socket closed.\n",
                         socks[index].iface);
                  socks[index].state = LSTN_STATE;
                  socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer
               }
               break;
				// Abort the socket--used only if a socket has timed out in one of
				// the closing states
            case ABRT_STATE:
               sock_abort(&socks[index].s);     // abort the socket
               socks[index].state = LSTN_STATE; // try to listen again
               socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer
               break;
         }

         // Drive the TCP/IP stack
    		tcp_tick(NULL);

			// Check the timeout on this socket, and close or abort the socket
			// if necessary
         timed_out(index);
    	}
 	}
}
Пример #7
0
/****************************************************************************
	main

	Print out a menu, wait for keypresses, while calling tcp_tick.

****************************************************************************/
void main(void)
{
	int val0, val1,i, level;
   mac_addr mac;
   char c;
   word waitms, pingit;
   longword pingid;
   wifi_status status;
   int len;
   int printmenu;
   unsigned long int end;

	// Initialize the scan_complete variable
	scan_complete = 0;

	sock_init();

	waitms = _SET_SHORT_TIMEOUT(300);
	pingit = 0;
   printmenu = 1;
   for (;;) {
   	if (printmenu) {
      	printmenu = 0;
	      printf("\nMenu:\n");
	      printf("   Press s to scan available access points\n");
	      printf("   Press a to scan access points and associate\n");
	      printf("   Press m to print WIFI MAC status\n");
	      printf("\n");
      }
		tcp_tick(NULL);
		if (kbhit()) {
      	switch (getchar()) {
         	case 'm':
            case 'M':
	            ifconfig (IF_WIFI0, IFG_WIFI_STATUS, &status, IFS_END);
	            print_status(&status);
	            printmenu = 1;
					break;

            case 's':
            case 'S':
	            // Bring the interface down before starting a scan
	            ifdown(IF_WIFI0);
	            while (ifpending(IF_WIFI0) != IF_DOWN)
	               tcp_tick(NULL);
	            // Set the callback before requesting scan
	            ifconfig(IF_WIFI0, IFS_WIFI_SCAN, scan_callback, IFS_END);
	            printf("Starting scan...\n");
					break;

            case 'a':
            case 'A':
	            // Bring the interface down before starting a scan
	            ifdown(IF_WIFI0);
	            while (ifpending(IF_WIFI0) != IF_DOWN)
	               tcp_tick(NULL);
	            ifconfig(IF_WIFI0, IFS_WIFI_SCAN, scan_assoc_callback, IFS_END);
	            printf("Starting scan...\n");
					break;
			}
		}
		// Check to see if a scan has completed.  If so, then bring the
		// interface back up.
		if (scan_complete) {
			ifup(IF_WIFI0);
			scan_complete = 0;
         printmenu = 1;
		}
   }
}
Пример #8
0
SSPEC_RESOURCETABLE_END


void main()
{
	auto int if_status;
	auto unsigned long t;
	auto char buffer[100];

	MY_CTS_BIT_SETUP	// set up the CTS handshake input
	MY_RTS_BIT_SETUP	// set up the RTS handshake output

	sock_init();

	//configure PPP for dialing in to ISP and bring it up
	ifconfig(MY_PPP_INTERFACE,
				IFS_PPP_INIT,
				IFS_PPP_SPEED, DIALUP_SPEED,
				IFS_PPP_RTSPIN, MY_RTS_PORT, &MY_RTS_PORT_SHADOW, MY_RTS_BIT,
				IFS_PPP_CTSPIN, MY_CTS_PORT, MY_CTS_BIT,
				IFS_PPP_FLOWCONTROL, DIALUP_FLOWCONTROL,
				IFS_PPP_SENDEXPECT, DIALUP_SENDEXPECT,
				IFS_PPP_HANGUP, "ATH #ok",
				IFS_PPP_MODEMESCAPE, 1,
				IFS_PPP_ACCEPTIP, 1,
				IFS_PPP_ACCEPTDNS, 1,
				IFS_PPP_REMOTEAUTH, DIALUP_NAME, DIALUP_PASSWORD,
				IFS_UP,
				IFS_END);

	while (IF_COMING_UP == (if_status = ifpending(MY_PPP_INTERFACE)) ||
	       IF_COMING_DOWN == if_status)
	{
		tcp_tick(NULL);
	}
	if(ifstatus(MY_PPP_INTERFACE))
	{
		printf("PPP established\n");
	}
	else
	{
		printf("PPP failed\n");
	}
	printf("IP address is %s\n", inet_ntoa( buffer, gethostid()));

	http_init();

/*
 *  tcp_reserveport causes the web server to maintain pending requests
 * whenever there is not a listen socket available
 *
 */

   tcp_reserveport(80);

/*
 *  http_handler needs to be called to handle the active http servers.
 */

   while (1) {
      http_handler();
   }

	ifconfig(MY_PPP_INTERFACE, IFS_DOWN, IFS_END);

	//wait while PPP terminates
	tcp_tick(NULL);
	while (IF_COMING_DOWN == (if_status = ifpending(MY_PPP_INTERFACE)) ||
	       IF_COMING_UP == if_status)
	{
		tcp_tick(NULL);
	}

}
Пример #9
0
main()
{
	int state;
	word tmo;


	brdInit();				//initialize board for this demo

	// Bring up interface first time (also prints our address)
	sock_init_or_exit(1);

	// First initialization OK, turn on both LEDs
	if_led(LEDON);
	xcvr_led(LEDON);

	state = 0;
	tmo = _SET_SHORT_TIMEOUT(UPTIME);

	for(;;) {
		switch (state) {
		case 0:	// Up, timing out
			tcp_tick(NULL);
			if (_CHK_SHORT_TIMEOUT(tmo)) {
				printf("Bringing interface down...\n");
				state = 1;
				ifdown(IF_WIFI0);
			}
			break;
		case 1:	// bringing down
			tcp_tick(NULL);
			if (ifpending(IF_WIFI0) == IF_DOWN) {
				printf("Powering down...\n");
	         if_led(LEDOFF);
	         xcvr_led(LEDOFF);

            // Set flag for MAC to power down when tcp_tick function is called!
            pd_powerdown(IF_WIFI0);
	         tmo =_SET_SHORT_TIMEOUT(DOWNTIME);
	         state = 2;
	      }
	      break;
	   case 2:	// down, waiting
      	tcp_tick(NULL);
	   	if (_CHK_SHORT_TIMEOUT(tmo)) {
				printf("Powering up...\n");

            // Set flag for MAC to power-up when tcp_tick function is called!
	         pd_powerup(IF_WIFI0);
            xcvr_led(LEDON);
	         tmo =_SET_SHORT_TIMEOUT(500);	// settle for 1/2 sec
	         state = 3;
			}
			break;
		case 3: // let power stabilize
      	tcp_tick(NULL);
			if (_CHK_SHORT_TIMEOUT(tmo)) {
				printf("Bringing interface up...\n");
				ifup(IF_WIFI0);
	         state = 4;
			}
			break;
		case 4:	// waiting for up
			tcp_tick(NULL);
			if (ifpending(IF_WIFI0) != IF_COMING_UP) {
				if (ifpending(IF_WIFI0) == IF_DOWN) {
					printf("!!!!! Failed to come back up!!!!!\n");
					return -1;
				}
				printf("Up again!\n");
				if_led(LEDON);
	         tmo =_SET_SHORT_TIMEOUT(UPTIME);
				state = 0;
			}
			break;
		}
	}
}
Пример #10
0
int main()
{
	auto int if_status;

	printf("Multiple Interface Echo Test\n");
	usage();
	printf("\nPress any key to proceed.\n");
	while (!kbhit());
	getchar();
	printf("Initializing TCP/IP stack...\n");

	for (i = 0; i < IF_MAX; i++)
		ifconfig(i, IFG_IPADDR, oldip+i,
#ifdef USE_IF_CALLBACK
					IFS_IF_CALLBACK, updowncb,
#endif
					IFS_END);

	sock_init();

	// Wait for the interface to come up
		while (IF_COMING_UP == (if_status = ifpending(IF_DEFAULT)) ||
		       IF_COMING_DOWN == if_status)
	{
		tcp_tick(NULL);
	}

	printf("...done.\n");

	memset(state, 0, sizeof(state));
	socknum = -1;
	seq = 1;
	ping_ip = 0;
	act_ip = 0;
	asock = s + (NUM_SOCKS - 1);
	astate = state + (NUM_SOCKS - 1);
	*astate = STATE_DONT_USE;

	if(!udp_extopen(&usock, IF_ANY, ECHO_PORT, -1L, 0, echo_handler, 0, 0)) {
		printf("udp_extopen failed!\n");
		exit(0);
	}


	for (;;) {
		socknum++;
		if (socknum == NUM_SOCKS)
			socknum = 0;

		tcp_tick(NULL);

		if (state[socknum] > STATE_CLOSED &&
		    !sock_alive(s+socknum)) {
		   if (socknum == NUM_LISTEN_SOCKS) {
		   	printf("Active socket closed\n");
				state[socknum] = STATE_DONT_USE;
		   }
		   else {
				printf("Socket %d closed\n", socknum);
				state[socknum] = STATE_CLOSED;
			}
			sock_perror(s+socknum, NULL);
		}

		for (i = 0; i < IF_MAX; i++) {
			ifconfig(i, IFG_IPADDR, &ip, IFS_END);
			if (oldip[i] != ip) {
				printf("IPaddr on i/f %d changed from %08lx to %08lx\n",
					i, oldip[i], ip);
				oldip[i] = ip;
				ifconfig(i, IFS_ICMP_CONFIG_RESET, IFS_END);
			}
		}

		if (kbhit()) {
			kb = getchar();
			if (kb == '?')
				usage();
			else if (kb == 'i')
				ip_print_ifs();
			else if (kb == 'r')
				router_printall();
			else if (kb == 'c')
				arpcache_printall();
			else if (kb == 'p') {
				printf("Press an interface number [TCP/IP now blocked]\n");
				while (!kbhit());
				kb = getchar();
				if (isdigit(kb)) {
					ifconfig(kb - '0', IFG_ROUTER_DEFAULT, &ping_ip, IFS_END);
					if (ping_ip) {
						printf("Pinging router %08lX...\n", ping_ip);
						_send_ping(ping_ip, seq++, 1, IPTOS_DEFAULT, &ping_id);
					}
					else
						printf("No router for interface %d\n", kb - '0');
				}
				else
					printf("No interface selected.\n");
			}
			else if (kb == 'a') {
				if (act_ip && *astate != STATE_DONT_USE) {
					printf("Closing active connection to %s...\n", inet_ntoa(buf, act_ip));
					sock_close(asock);
					while (tcp_tick(asock));
				}
				*astate = STATE_DONT_USE;
				printf("Enter a host name or IP address [TCP/IP now blocked]\n");
				gets(buf);
				printf("Resolving...\n");
				act_ip = resolve(buf);
				if (act_ip) {
					printf("Enter a port number to connect to (0-65535)\n");
					gets(buf);
					aport = (word)atol(buf);
					printf("Opening to %s:%u...\n", inet_ntoa(buf, act_ip), aport);
					*astate = STATE_ACTOPEN;
				}
				else
					printf("Could not resolve %s to IP address.\n", buf);

			}
			else if (isdigit(kb)) {
				// Toggle interface status
				kb -= '0';
	  			if (!(1u<<kb & IF_SET)) {
            	printf("Not a valid interface\n");
					continue;
            }
				if (ifstatus(kb)) {
					printf("Bringing interface %d down...\n", kb);
					ifconfig(kb, IFS_DOWN, IFS_END);
				}
				else {
					printf("Bringing interface %d up...\n", kb);
					ifconfig(kb, IFS_UP, IFS_END);
				}
			}
		}

		if (ping_ip) {
			if (_chk_ping(ping_ip , &ping_id) != 0xffffffffL) {
				printf("Got ping response from %08lX\n", ping_ip);
				ping_ip = 0;
			}
		}

		switch (state[socknum]) {
			case STATE_CLOSED:
				if (!tcp_extlisten(s + socknum, IF_ANY, ECHO_PORT, 0, 0, t_handler, 0, 0, 0)) {
					printf("Listen failed - socket %d\n", socknum);
					state[socknum] = STATE_DONT_USE;
					break;
				}
				printf("Listening on socket %d...\n", socknum);
				state[socknum] = STATE_LISTEN;
				break;
			case STATE_ACTOPEN:
				if (!tcp_extopen(s + socknum, IF_ANY, 0, act_ip, aport, t_handler, 0, 0)) {
					printf("Active open failed\n");
					state[socknum] = STATE_DONT_USE;
					break;
				}
				state[socknum] = STATE_LISTEN;
				break;
			case STATE_LISTEN:
				if (sock_established(s + socknum)) {
					printf("Connection %d estab.\n", socknum);
					state[socknum] = STATE_ESTAB;
				}
				break;
			case STATE_ESTAB:
				if ((rb = sock_fastread(s + socknum, buf, sizeof(buf))) > 0) {
					sock_fastwrite(s + socknum, buf, rb);
					if (!strncmp(buf, "quit", 4)) {
						printf("Peer on socket %d requests close\n", socknum);
						sock_close(s + socknum);
						state[socknum] = STATE_CLOSING;
					}
				}
				else if (rb < 0) {
					printf("Connection %d closed by peer.\n", socknum);
					sock_close(s + socknum);
					state[socknum] = STATE_CLOSING;
				}
				break;
			default:
				break;
		}
	}
}
Пример #11
0
main()
{
	longword seq,ping_who,tmp_seq,time_out;
	char	buffer[100];
   auto wifi_region region_info;
   auto char index[10];
   auto int option;
   auto char tmpbuf[64];
   auto int updateRegion;
   auto int country;
   auto int configured;


	brdInit();				//initialize board for this demo

	seq=0;

	sock_init();			// Initial wifi interface

   // Make sure wifi IF is down to do ifconfig's functions
	printf("\nBringing interface down (disassociate)...\n");
   ifdown(IF_WIFI0);
   while (ifpending(IF_WIFI0) != IF_DOWN) {
     	printf(".");
     	tcp_tick(NULL);
   }
	printf("...Done.\n");

   configured = FALSE;
   updateRegion = FALSE;
   do
   {
   	country = get_stored_region(&region_info);
   	// Check if the region has been previously set.
 		if(country < 0 || updateRegion)
   	{
      	// Populate structure with region info, then display
   		ifconfig (IF_WIFI0, IFG_WIFI_REGION_INFO, &region_info, IFS_END);
   		printf("\nCurrent region setting:\n");
   		display_info(&region_info);

         // Select Region and write value to userblock
    		country = wifi_config_region(&region_info);
         updateRegion = FALSE;

   	}
   	else
   	{
      	// Set Region from previously saved value read from the userblock, this
      	// will set the runtime limits to be used by the wifi driver.

   		ifconfig (IF_WIFI0, IFS_WIFI_REGION, country,
         	IFG_WIFI_REGION_INFO, &region_info, IFS_END);

   		printf("\nRuntime region setting now being used by wifi driver:\n");
         display_info(&region_info);

      	// Region has already been set at runtime, check if it needs to
         // be changed due to country to country roaming.
    		printf("\nRegion already set, select option to continue");
         printf("\n1. Continue.");
         printf("\n2. Select new region.");
         printf("\nSelect > ");
			do
			{
				option = atoi(gets(tmpbuf));
	  		} while (!((option >= 1) && (option <= 2)));
         if(option == 2)
         	updateRegion = TRUE;
         else
         	configured = TRUE;
      }
	}while(!configured);

   // If you are not going to use the defaulted channel and/or power level,
   // then you can use the following functions to set channel and/or the
   // power level. This needs to be done after setting the region/country
   // to meet wifi driver requirements.

   //ifconfig (IF_WIFI0, IFS_WIFI_CHANNEL, 0, IFS_END); // Scan all channels
   //ifconfig (IF_WIFI0, IFS_WIFI_TX_POWER, 8, IFS_END); // Set to Pwr level 8


   // Startup the  wireless interface here...
	printf("Bringing interface back up (associate)...\n");
   ifup(IF_WIFI0);
   while (ifpending(IF_WIFI0) == IF_COMING_UP) {
      tcp_tick(NULL);
   }
	printf("...Done.\n");
	if (ifpending(IF_WIFI0) != IF_UP) {
		printf("Unfortunately, it failed to associate :-(\n");
		exit(1);
	}
   // End of regional setting section, from this point on do standard tcp/ip
   // protocol.


   /*
   // Here is where we gather the statistics...
	// Note that if you get a compile error here, it is because you are not running
	// this sample on a Wifi-equipped board.

	/* Print who we are... */
	printf( "My IP address is %s\n\n", inet_ntoa(buffer, gethostid()) );

	/*
	 *		Get the binary ip address for the target of our
	 *		pinging.
	 */

#ifdef PING_WHO
	/* Ping a specific IP addr: */
	ping_who=resolve(PING_WHO);
	if(ping_who==0) {
		printf("ERROR: unable to resolve %s\n",PING_WHO);
		exit(2);
	}
#else
	/* Examine our configuration, and ping the default router: */
	tmp_seq = ifconfig( IF_ANY, IFG_ROUTER_DEFAULT, & ping_who, IFS_END );
	if( tmp_seq != 0 ) {
		printf( "ERROR: ifconfig() failed --> %d\n", (int) tmp_seq );
		exit(2);
	}
	if(ping_who==0) {
		printf("ERROR: unable to resolve IFG_ROUTER_DEFAULT\n");
		exit(2);
	}
#endif

	for(;;) {
		/*
		 *		It is important to call tcp_tick here because
		 *		ping packets will not get processed otherwise.
		 *
		 */

		tcp_tick(NULL);

		/*
		 *		Send one ping every PING_DELAY ms.
		 */

		costate {
			waitfor(DelayMs(PING_DELAY));
			_ping(ping_who,seq++);
			pingoutled(LEDON);					// flash transmit LED
			waitfor(DelayMs(50));
			pingoutled(LEDOFF);
		}

		/*
		 *		Has a ping come in?  time_out!=0xfffffff->yes.
		 */

		costate {
			time_out=_chk_ping(ping_who,&tmp_seq);
			if(time_out!=0xffffffff) {

#ifdef VERBOSE
				printf("received ping:  %ld\n", tmp_seq);
#endif

				pinginled(LEDON);					// flash receive LED
				waitfor(DelayMs(50));
				pinginled(LEDOFF);
			}
		}
	}
}
Пример #12
0
int main()
{
	struct tftp_state ts;
	int status;
	word bflen;
	
   status = sock_init();
   if (status) {
   		printf("Could not init packet driver.\n");
   		exit(3);
   }
	// Wait for the interface to come up
	while (ifpending(IF_DEFAULT) == IF_COMING_UP) {
		tcp_tick(NULL);
	}
   

   /*########## DOWNLOAD ##########*/   

  	ts.state = 0;								// 0 = read
  	ts.buf_len = TFTP_DL_SIZE;				// max length to download
  	ts.buf_addr = xalloc(TFTP_DL_SIZE);	// allocate a buffer
  	ts.my_tid = 0;								// zero to use default TFTP UDP port number
  	ts.sock = &tsock;							// point to socket to use
  	ts.rem_ip = resolve(TFTP_SERVER);	// resolve server IP address
  	ts.mode = TFTP_MODE_OCTET;				// send/receive binary data
  	strcpy(ts.file, TFTP_DL_FILENAME);	// set file name on server
  	
  	printf("Downloading %s...\n", ts.file);
      	
  	// This uses the non-blocking TFTP functions, but in a blocking
  	// manner.  It would be easier to use tftp_exec(), but this
  	// doesn't return the server error message.
  	tftp_init(&ts);
	while ((status = tftp_tick(&ts)) > 0);	// Loop until complete
	if (!status)
		printf("Download completed\n");
	else if (status == -3)
		printf("ERROR: Download timed out.\n");
	else if (status == -5)
		printf("Download completed, but truncated\n");
	else {
		printf("Download failed: code %d\n", status);
		if (status == -1)
			printf("  Message from server: %s\n", ts.file);
	}


   /*########## UPLOAD ##########*/   

  	ts.state = 1;								// 0 = write
  	ts.buf_len = ts.buf_used;				// length to upload (use same buffer as downloaded)
  	ts.my_tid = 0;								// zero to use default TFTP UDP port number
  	ts.sock = &tsock;							// point to socket to use
  	ts.rem_ip = resolve(TFTP_SERVER);	// resolve server IP address
  	ts.mode = TFTP_MODE_OCTET;				// send/receive binary data
  	strcpy(ts.file, TFTP_UL_FILENAME);	// set file name on server
  	
  	printf("Uploading as %s...\n", ts.file);
      	
  	tftp_init(&ts);
	while ((status = tftp_tick(&ts)) > 0);	// Loop until complete
	if (!status)
		printf("Upload completed\n");
	else {
		printf("Upload failed: code %d\n", status);
		if (status == -1)
			printf("  Message from server: %s\n", ts.file);
		else if (status == -3)
			printf("ERROR: Download timed out.\n");
		else if (status == -2) {
			printf("  Server did not ack last packet...\n");
			printf("    Acked %u\n", ts.buf_used);
			if (ts.buf_len - ts.buf_used <= 512)
				printf("    Some bug-ridden servers don't ack the last packet sent :-(\n");
		}
	}

	printf("All done.\n");
	return 0;
}
Пример #13
0
main()
{
	longword seq,ping_who,tmp_seq,time_out;
	char	buffer[100];


	brdInit();				//initialize board for this demo
	seq=0;

	sock_init();			// Initialize wifi interface

   // Make sure wifi IF is down to do ifconfig's functions
	printf("\nBringing interface down (disassociate)...\n");
   ifdown(IF_WIFI0);
   while (ifpending(IF_WIFI0) != IF_DOWN) {
     	printf(".");
     	tcp_tick(NULL);
   }
	printf("...Done.\n");

   // Enable 802.11d country information capability
   // Note: Access Point must have 802.11d enabled with proper country selected
   ifconfig(IF_WIFI0, IFS_WIFI_MULTI_DOMAIN, 1, IFS_END);

   // Startup the  wireless interface here...
	printf("Bringing interface back up (associate)...\n");
   ifup(IF_WIFI0);
   while (ifpending(IF_WIFI0) == IF_COMING_UP) {
      tcp_tick(NULL);
   }
	printf("...Done.\n");
	if (ifpending(IF_WIFI0) != IF_UP) {
		printf("Unfortunately, it failed to associate :-(\n");
		exit(1);
	}
   // End of regional setting section, from this point on do standard tcp/ip
   // protocol.


   /*
   // Here is where we gather the statistics...
	// Note that if you get a compile error here, it is because you are not running
	// this sample on a Wifi-equipped board.

	/* Print who we are... */
	printf( "My IP address is %s\n\n", inet_ntoa(buffer, gethostid()) );

	/*
	 *		Get the binary ip address for the target of our
	 *		pinging.
	 */

#ifdef PING_WHO
	/* Ping a specific IP addr: */
	ping_who=resolve(PING_WHO);
	if(ping_who==0) {
		printf("ERROR: unable to resolve %s\n",PING_WHO);
		exit(2);
	}
#else
	/* Examine our configuration, and ping the default router: */
	tmp_seq = ifconfig( IF_ANY, IFG_ROUTER_DEFAULT, & ping_who, IFS_END );
	if( tmp_seq != 0 ) {
		printf( "ERROR: ifconfig() failed --> %d\n", (int) tmp_seq );
		exit(2);
	}
	if(ping_who==0) {
		printf("ERROR: unable to resolve IFG_ROUTER_DEFAULT\n");
		exit(2);
	}
#endif

	for(;;) {
		/*
		 *		It is important to call tcp_tick here because
		 *		ping packets will not get processed otherwise.
		 *
		 */

		tcp_tick(NULL);

		/*
		 *		Send one ping every PING_DELAY ms.
		 */

		costate {
			waitfor(DelayMs(PING_DELAY));
			pingoutled(LEDON);					// flash transmit LED
			waitfor(DelayMs(50));
			pingoutled(LEDOFF);
			_ping(ping_who,seq++);
		}

		/*
		 *		Has a ping come in?  time_out!=0xfffffff->yes.
		 */

		costate {
			time_out=_chk_ping(ping_who,&tmp_seq);
			if(time_out!=0xffffffff) {

#ifdef VERBOSE
				printf("received ping:  %ld\n", tmp_seq);
#endif

				pinginled(LEDON);					// flash receive LED
				waitfor(DelayMs(50));
				pinginled(LEDOFF);
#if RCM5600W_SERIES
				waitfor(DelayMs(250));
				pinginled(LEDON);					// flash receive LED again
				waitfor(DelayMs(50));
				pinginled(LEDOFF);
#endif
			}
		}
	}
}