/** * Add a gateway to the routing table. * * The format of 'config_string' is: * IP-address [, subnet] , mask]] * * If 'config_string' is NULL, simply add 'ip' with zero * mask and subnet. */ BOOL _arp_add_gateway (const char *config_string, DWORD ip) { struct gate_entry *gw; DWORD subnet = 0UL; DWORD mask = 0UL; int i; SIO_TRACE (("_arp_add_gateway")); if (config_string) { const char* subnetp, *maskp; /* Get gateways IP address from string */ ip = aton (config_string); if (ip == 0) return (FALSE); /* invalid */ /* Check if optional subnet was supplied */ subnetp = strchr (config_string, ','); if (subnetp) { /* NB: atoi (used in aton) stops conversion when the first non-number * is hit, so there is no need to manually null-terminate the string. * i.e. aton ("123.123.123.123,blabla") = 123.123.123.123 */ subnet = aton (++subnetp); /* Check if optional mask was supplied */ maskp = strchr (subnetp, ','); if (maskp) mask = aton (++maskp); else /* No mask was supplied, we derive it from the supplied subnet */ { switch (subnet >> 30) { case 0: case 1: mask = 0xFF000000UL; break; case 2: mask = 0xFFFFFE00UL; /* minimal class B */ break; case 3: default: mask = 0xFFFFFF00UL; break; } } } } else /* (config_string == NULL) */ { if (ip == 0UL)
main() { char ch; long ipaddr; char ipaddr_string[16]; printf ("Wi-Fi DHCP or Static sample\n"); do { // Prompt for configuration method printf("Select method (s=static, d=dhcp): "); ch=getchar(); // Give feedback on the choice printf("%c\n",ch); if(ch=='s' || ch=='S') { // Bring the interface up statically configured printf ("configuring interface for static IP\n"); ifconfig(IF_WIFI0, IFS_IPADDR,aton(_PRIMARY_STATIC_IP), IFS_NETMASK, aton(_PRIMARY_NETMASK), IFS_ROUTER_SET, aton(MY_GATEWAY), IFS_END); break; } else if(ch=='d' || ch=='D') { // Bring the interface up with DCHP printf ("configuring interface for DHCP\n"); ifconfig(IF_WIFI0, IFS_DHCP, 1, IFS_END); break; } else { printf("Invalid selection\n\n"); } } while(1); // Initialize the TCP/IP stack sock_init_or_exit(1); // Print the IP Address to stdio ifconfig(IF_WIFI0,IFG_IPADDR,&ipaddr,IFS_END); printf("IP Address: %s -- responding to pings\n",inet_ntoa(ipaddr_string,ipaddr)); // Give the stack time... Ready to be PINGed while(1) { tcp_tick(NULL); } }
/** * Convert a byte characters from a given file and returns a byte. * @param in file * @return -1 if EOF */ static int getByte(FILE *in) { int ch1, ch2; if (feof(in)) { printf("eof"); return -1; } ch1 = fgetc(in); if (feof(in)) { printf("eof"); return -1; } ch2 = fgetc(in); return 16 * aton(ch1) + aton(ch2); }
/* * uart_getnum * * Read a number in from the specified channel. Accepts base * decorators (like 0x) to distinguish hexadecimal numbers from * decimal ones. See aton for the list. */ uint32_t uart_getnum(int chan) { char buf[48]; uart_gets(chan, buf, 48); return aton(buf); }
int main(int argc, char *argv[]) { int t[MAX_STRING] = { E, B, G, D, A, E }; /* tunning */ int s = 6; /* strings */ int f = 12; /* frets */ int c = 0; /* capo */ int m = 0; /* mode (scale) */ int n = C; /* tone */ int opt; while ((opt = getopt(argc, argv, "t:s:f:c:m:n:h")) != -1) { switch (opt) { case 't': if (*optarg == '?') return tune_table(); s = tunestr(t, optarg); break; case 's': if ((s = atoi(optarg)) > MAX_STRING) { fprintf(stderr, "max strings %d\n", MAX_STRING); return -1; } break; case 'f': f = atoi(optarg); break; case 'c': c = atoi(optarg); break; case 'm': if (*optarg == '?') return scale_table(); if ((m = atoi(optarg)) > sizeof(scales)) { fprintf(stderr, "invalid scale\n"); return -1; } break; case 'n': if ((n = aton(optarg)) < 0) { fprintf(stderr, "invalid tone %s\n", optarg); return -1; } break; case 'h': default: fprintf(stderr, "Usage: %s [-s strings] [-f frets] " "[-c capo] [-t tunning]\n", argv[0]); return -1; } } if (f < c) { fprintf(stderr, "nothing to print\n"); return -1; } printf("Scale: %s\t Tone: %s\n\n", scales[m].name, notes[n]); fb_print(s, t, f, c, scales[m].bit, n); return 0; }
int main(int argc,char* argv[]) { char name[10]; memset(name,'0',10); int a [100]; memset (a,0,100); strcpy(name,argv[1]); a[0]=aton(argv[1]); // a[1]=atoi(argv[2]) printf("%s\n",name); return 0; }
/* * resolve() * convert domain name -> address resolution. * returns 0 if name is unresolvable right now */ longword resolve_fn( char *name, sockfunct_t fn ) // S. Lawson { longword ipaddr; // S. Lawson #define DNSCACHESIZE 4 // cache up to 4 names #define DNSCACHELENGTH 32 // up to 32 characters #define DNSCACHETIMEOUT 120 // for up to 2 minutes static char DNScacheName[DNSCACHESIZE][DNSCACHELENGTH]; static longword DNScacheIP[DNSCACHESIZE]; static longword DNScacheTimeout[DNSCACHESIZE]={0,0,0,0}; static char DNScacheNext=0; int DNScacheScan; if( !name ) return 0L; rip( name ); // S. Lawson - trim for cache scan if ( isaddr( name )) return( aton( name )); // S. Lawson for (DNScacheScan=0 ; DNScacheScan<DNSCACHESIZE ; DNScacheScan++) { if (DNScacheTimeout[DNScacheScan]==0L) continue; if (chk_timeout(DNScacheTimeout[DNScacheScan])) { DNScacheTimeout[DNScacheScan]=0L; continue; } if(!strcmpi(DNScacheName[DNScacheScan],name)) return DNScacheIP[DNScacheScan]; } #ifdef NOTUSED // S. Lawson if( do_ns_lookup(name, DTYPEA, typea_unpacker, &ipaddr) ) return (intel(ipaddr)); else return (0L); #else // S. Lawson if( do_ns_lookup(name, DTYPEA, typea_unpacker, &ipaddr, fn) ) { strncpy(DNScacheName[DNScacheNext], name, DNSCACHELENGTH); DNScacheName[DNScacheNext][DNSCACHELENGTH-1]='\0'; DNScacheIP[DNScacheNext]=intel(ipaddr); DNScacheTimeout[DNScacheNext]=set_timeout(DNSCACHETIMEOUT); if (++DNScacheNext>=DNSCACHESIZE) DNScacheNext=0; return (intel(ipaddr)); } return (0L); #endif // S. Lawson }
int main() { auto snmp_parms _p; auto snmp_parms * p; auto word tt; auto word trapindices[2]; auto word monindex; // Set the community passwords snmp_set_dflt_communities("public", "private", "trap"); // Set p to be a pointer to _p, for calling convenience. p = &_p; // Set parameter structure to default initial state (required). snmp_init_parms(p); // Create the MIB tree. The following functions all operate on the parameter structure. // "p" is passed to all functions, and also set to the return value. This is the recommended // way of doing the MIB tree setup, since if any step fails it will return NULL. Passing the // NULL on to subsequent functions is harmless, and avoids the need to do error checking // after each call. Only at the end of sequence sould "p" be tested for NULL. // Set the "root" of the MIB tree for following calls. Note that the entire MIB // tree can be rooted at a different point simply by changing this one call. p = snmp_append_parse_stem(p, "3.1.1"); // Set to SNMP_ENTERPRISE.oemExperiments.demos // Read/write access - the following is set by default so no need to call. p = snmp_set_access(p, SNMP_PUBLIC_MASK|SNMP_PRIVATE_MASK, SNMP_PRIVATE_MASK); p = snmp_add_int(p, "1.1.0", &rw_int); monindex = snmp_last_index(p); // Save index for later monitor call p = snmp_set_callback(p, scale); p = snmp_add_long(p, "1.2.0", &rw_long); // This variable has a callback function, scale(). p = snmp_set_callback(p, NULL); p = snmp_add_foct(p, "1.3.0", rw_fixed, 20); p = snmp_add_str(p, "1.4.0", rw_str, 20); p = snmp_add_oct(p, "1.5.0", rw_oct, 22); p = snmp_add_objectID(p, "1.6.0", &rw_oid); p = snmp_add_ipaddr(p, "1.7.0", &trapdest_ip); p = snmp_add_timeticks(p, "1.8.0", &rw_tt); // Read-only access for following additions p = snmp_set_access(p, SNMP_PUBLIC_MASK|SNMP_PRIVATE_MASK, 0); p = snmp_add_int(p, "2.1.0", &r_int); trapindices[0] = snmp_last_index(p); p = snmp_add_long(p, "2.2.0", &r_long); p = snmp_add_foct(p, "2.3.0", r_fixed, 20); p = snmp_add_str(p, "2.4.0", r_str, 20); p = snmp_add_oct(p, "2.5.0", r_oct, 22); trapindices[1] = snmp_last_index(p); p = snmp_add_objectID(p, "2.6.0", &r_oid); // Initialize the variables. rw_int = 1001; rw_long = 1000002; memcpy(rw_fixed, "rw_fixed abcdefghijk", 20); strcpy(rw_str, "rw_str"); memcpy(rw_oct, "\x06\x00rw_oct", 8); memcpy(&rw_oid, &_p, sizeof(snmp_oid)); trapdest_ip = aton(MANAGER_IP); rw_tt = snmp_timeticks(); // Set base epoch r_int = 2001; r_long = 2000002; memcpy(r_fixed, "r_fixed abcdefghijkl", 20); strcpy(r_str, "r_str"); memcpy(r_oct, "\x05\x00r_oct", 7); memcpy(&r_oid, &_p, sizeof(snmp_oid)); // Finally, we check that the MIB tree was constructed without error. // If there was any error, p will be set to NULL. if (!p) { printf("There was an error constructing the MIB.\n"); exit(1); } // Monitor the rw_int variable (whose MIB tree index was saved in monindex). // trapindices was set up with the indices for r_int and r_oct. snmp_monitor(monindex, 0, 3000, 1, 16, 6, &trapdest_ip, SNMP_TRAPDEST, 30, 2, trapindices); // See what we've got. snmp_print_tree(); printf("MIB tree: used %ld out of %ld bytes\n", snmp_used(), (long)SNMP_MIB_SIZE); // Now start up the network sock_init(); // Print interfaces ip_print_ifs(); // Print routers router_printall(); tt = _SET_SHORT_TIMEOUT(5000); for (;;) { if (_CHK_SHORT_TIMEOUT(tt)) { #ifdef SEND_TRAPS snmp_trap(trapdest_ip, SNMP_TRAPDEST, 20, 2, trapindices); #endif tt = _SET_SHORT_TIMEOUT(5000); } tcp_tick(NULL); } }
settings():verbose(10),myport(11011),masterport(11011),myip(get_myip()),masterip(aton("127.0.0.1")),i_am_master(1){}
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); } } }
int main(int argc, char *argv[]) { FILE *infile,*otfile; unsigned char fnami[14],fnamo[14],ch,ch1,ch2,a1,a2,a3,a4; //limits us to short filenames int count=0,u,t; //count keeps track of number of bytes written, //u is value of the current line's address field //t is byte count of current line int i; //loop counter //puts("what filename to convert?"); //scanf("%s",fnami); strcpy(fnami,argv[1]); //get the filename prefix strcat(fnami,".hex"); // and hope the extension is '.hex' //puts("Convert to what filename?"); //scanf("%s",fnamo); strcpy(fnamo,argv[1]); //get the filename prefix strcat(fnamo,".mem"); // and hope the extension is '.hex' if((infile=fopen(fnami,"rb"))==NULL) { printf("fopen input file failed.\n"); exit(0); } otfile=fopen(fnamo,"wb"); //write identifier comment into .mem file fprintf(otfile,"// .mem file generated by hex2mem v2 wmh 05/31/04 \n"); while(1) { //strip away everything up to and including the ':' while(fgetc(infile)!=':'); //get t= byte count field from an input line if((t=16*aton(fgetc(infile))+aton(fgetc(infile)))==0) //quit program when byte count is 0 { fclose(infile); fclose(otfile); exit(0); } //construct address field for new line of mem file fputc(0x40,otfile); //write '@' delim to mem file for(i=0; i<4; i++) fputc( fgetc(infile), otfile); //copy addressfield from hex file to mem file fputc(0x20, otfile); //write <space> to otfile //throw away the '00' delimiter field fgetc(infile); fgetc(infile); //copy each data byte from the current line to the output (and skip the checksum) while(t>0) { fputc(fgetc(infile),otfile); //copy 1st character of byte fputc(fgetc(infile),otfile); // 2nd t--; count++; } //send newline character fputc(0x0A, otfile); }//go up for next line of input }
int main (int argc, char **argv) { eth_address eth = { 1,2,3,4,5,6 }; int a_flag = 0; int h_flag = 0; int i_flag = 0; int f_flag = 0; int n_flag = 0; int m_flag = 0; int d_flag = 0; int ch; while ((ch = getopt(argc, argv, "adn?h:i:f:m:t:r:")) != EOF) switch (ch) { case 'a': a_flag = 1; break; case 'd': d_flag = 1; break; case 'n': n_flag = 1; break; case 'h': h_flag = 1; tftp_server = aton (optarg); break; case 'i': i_flag = 1; my_ip_addr = aton (optarg); break; case 'f': f_flag = 1; tftp_set_boot_fname (optarg, strlen(optarg)); break; case 'm': m_flag = 1; sin_mask = aton (optarg); break; case 't': tftp_timeout = atoi (optarg); break; case 'r': tftp_retry = atoi (optarg); break; case '?': default: usage (argv[0]); } if (n_flag) /* Demonstrate running with no config file */ { _watt_no_config = 1; dbg_mode_all = 1; dbg_print_stat = 1; debug_on = 3; if (!m_flag) sin_mask = aton ("255.255.0.0"); if (!i_flag) my_ip_addr = aton ("192.168.0.1"); if (!h_flag) tftp_server = aton ("192.168.0.2"); if (!f_flag) tftp_set_boot_fname ("test.fil", 8); if (a_flag) _arp_add_cache (tftp_server, (const eth_address*)ð, FALSE); } else if (m_flag || i_flag || h_flag || f_flag || a_flag) { puts ("This option requires the `-n' flag"); return (-1); } if (d_flag) dbug_init(); if (n_flag) dbug_open(); /* Must set our hook first */ tftp_writer = write_func; tftp_terminator = close_func; sock_init(); sleep (1); /* drain network buffers */ tcp_tick (NULL); return (0); }
/** * @return non-zero if error */ static void replace(FILE* in, struct ConvertInfo *info, Memory *memory) { int i, j; char name[DEF_NAMELEN]; int len = 0; // read name while (len < DEF_NAMELEN - 1) { char ch = fgetc(in); if (!strchr(NAME_CHARS, ch)) { break; } name[len] = ch; len++; } name[len] = 0; for (i = 0; i < info->defsize; i++) { if (!strcmp(name, info->defs[i].name)) { int tmp; char value[DEF_VALUELEN]; memset(value, 0, DEF_VALUELEN); // replace! switch (memory->type) { case TYPE_STRING: strncpy(&memory->buffer[memory->address], info->defs[i].value, memory->length); strncpy(value, &memory->buffer[memory->address], memory->length); if (info->verbose) { printf("[%s]->[%s], ", name, value); } break; case TYPE_BYTE: tmp = 0; for (j = 0; j < 2; j++) { if (aton(info->defs[i].value[j])) { tmp = tmp * 16 + aton(info->defs[i].value[j]); } } memory->buffer[memory->address] = tmp; if (info->verbose) { printf("[%s]->[%02x], ", name, tmp); } break; case TYPE_WORD: tmp = 0; for (j = 0; j < 2; j++) { tmp = tmp * 16 + aton(info->defs[i].value[j]); } memory->buffer[memory->address + 1] = tmp; tmp = 0; for (j = 2; j < 4; j++) { tmp = tmp * 16 + aton(info->defs[i].value[j]); } memory->buffer[memory->address] = tmp; if (info->verbose) { printf("[%s]->[%02x%02x], ", name, memory->buffer[memory->address + 1], memory->buffer[memory->address]); } break; } break; } } }