int main(int argc, char **argv) { char parts[4][4]; char *list[] = {"192.168.9.1", "0.0.0.0", "255.255.255.256", "255.255..255", "2d1.255.255.255", "", NULL}; int n; n = 0; while (list[n] != NULL) { if (split_ip(list[n], parts) == 0) { printf("ip: %s -> %s / %s / %s / %s\n", list[n], parts[0], parts[1], parts[2], parts[3]); } else { printf("ip: %s -> error\n", list[n]); } n++; } return 0; }
void *sendThread(void *p) { int i, j, n; // char _dest[MAXHOSTNAMELEN+1]; char _dest_ip[INET_ADDRSTRLEN+1]; struct sockaddr_in _destaddr; uchar o[4]; uint ip; uchar _seq; int rv; n = g_num_packets; /*num*/ ip = _startAddr.s_addr; for (i = 0; i < n; i++) { split_ip(ip,o); if (o[3] == 0) continue; if (!fBroadcastOk && (o[3] == 255)) continue; sprintf(_dest_ip,"%d.%d.%d.%d",o[0],o[1],o[2],o[3]); /* set _destaddr */ _destaddr.sin_family = AF_INET; _destaddr.sin_port = htons(g_port); if ( !inet_aton( _dest_ip, &_destaddr.sin_addr)) { printerr("inet_aton error %s\n",_dest_ip); continue; } for (j=0; j<g_repeat; j++) { /* send ping buffer */ _seq = 0; rv = send_probe(&_destaddr,_seq); g_npings++; if (fdebug) printerr("sendto[%d,%d] %s rv = %d\n", g_npings,_seq,_dest_ip,rv); if (rv < 0) { /*try to send again*/ rv = send_probe(&_destaddr,++_seq); if (rv < 0) { printerr("sendto[%d,%d] %s error %s\n", g_npings,_seq,_dest_ip,showlasterr()); continue; } } #ifdef NO_THREADS receiveThread(NULL); if (g_recv_status == 0 && !fBroadcastOk) { /* nothing returned, try again */ if (fping) { rv = send_poke1(&_destaddr); if (fdebug) printerr("sendto[%d,%d] %s poke rv = %d\n", g_npings,_seq,_dest_ip,rv); } rv = send_probe(&_destaddr,++_seq); if (fdebug) printerr("sendto[%d,%d] %s rv = %d\n", g_npings,_seq,_dest_ip,rv); if (rv >= 0) { receiveThread(NULL); } } #endif /* sleep an interval (g_delay usec) */ if (g_delay > 0) os_sleep(0,g_delay); } /*end-for g_repeat*/ ip++; /* increment to next IP */ } return(p); }
void show_ip(int saddr) { uchar ip[4]; split_ip(saddr,ip); printerr("%d.%d.%d.%d\n",ip[0],ip[1],ip[2],ip[3]); }
int init_leases_list() { DHCPLIST *temp; int i, j; u8b chaddr[16]; FILE *config; char line[80]; char textsubnet[16]; char textlease[5]; char textrouter[16]; char textmask[16]; char textlowrange[4], texthighrange[4]; char textserver[16]; u8b ip0, ip1, ip2, ip3; u8b lowrange, highrange; u8b textmac[17], textip[16]; u32b lease; /* Be nice variables and behave yourselves */ for( j = 0; j < 16; j++ ) { chaddr[j] = 0; textsubnet[j] = 0; textrouter[j] = 0; textmask[j] = 0; textip[j] = 0; textmac[j] = 0; } textlease[0] = 0; textlowrange[0] = 0; texthighrange[0] = 0; /* Now we can read our configuration file */ config = fopen( "dhcp.conf", "r" ); if( !config ) { perror("Reading config files"); exit( 0 ); } /* We _DO_ need a better parser */ list = (DHCPLIST *)malloc( sizeof( DHCPLIST )); temp = list; temp->back = NULL; while( (!feof( config )) && ( line )) { fscanf( config, "%s", line); if( !strcmp( line, "subnet" )) /* Read subnet parameters */ fscanf( config, "%s", textsubnet ); if( !strcmp( line, "lease" )) /* read lease parameters */ fscanf( config, "%s", textlease ); if( !strcmp( line, "router" )) fscanf( config, "%s", textrouter ); if( !strcmp( line, "mask" )) fscanf( config, "%s", textmask ); if( !strcmp( line, "range" )) fscanf( config, "%s %s", textlowrange, texthighrange ); if( !strcmp( line, "server" )) fscanf( config, "%s", textserver ); if( !strcmp( line, "host" )) { /* Host Specific Configuration */ fscanf( config, "%s %s", textmac, textip ); str2mac( textmac, temp->chaddr ); temp->type = STATIC; temp->data.ip = inet_addr( textip ); temp->next = (DHCPLIST *)malloc( sizeof( DHCPLIST )); temp->next->back = temp; temp = temp->next; temp->next =NULL; } } fclose( config ); lowrange = (u8b)atoi( textlowrange ); highrange = (u8b)atoi( texthighrange ); lease = (u32b)atoi( textlease ); /* Creating Static IP */ for( temp = list; temp->next; temp = temp->next ) { temp->available = FREE; temp->xid = 0; temp->data.router = inet_addr( textrouter ); temp->data.mask = inet_addr( textmask ); temp->data.lease = lease; temp->data.siaddr = inet_addr( textserver ); } /* Creating Dynamic IP */ for( i = lowrange; i < (highrange + 1); i++ ) { temp->available = FREE; temp->xid = 0; temp->type = DYNAMIC; maccpy( temp->chaddr, chaddr ); split_ip( textsubnet, &ip0, 0 ); split_ip( textsubnet, &ip1, 1 ); split_ip( textsubnet, &ip2, 2 ); temp->data.ip = i; temp->data.ip = temp->data.ip << 8; temp->data.ip += ip2; temp->data.ip = temp->data.ip << 8; temp->data.ip += ip1; temp->data.ip = temp->data.ip << 8; temp->data.ip += ip0; temp->data.router = inet_addr( textrouter ); temp->data.mask = inet_addr( textmask ); temp->data.lease = lease; temp->data.siaddr = inet_addr( textserver ); temp->next = (DHCPLIST *)malloc( sizeof( DHCPLIST )); temp->next->back = temp; temp = temp->next; } return 0; }