/* Eli Fulkerson, June 2005. http://www.elifulkerson.com for updates. */ /* License: Do whatever you want with this without restriction. Its too trivial to bother fighting over. That being said, I'd love to hear feedback if you find it useful, and appreciate being given credit if you feel like it. */ #include #include #pragma comment(lib, "wsock32.lib") int main(int argc, char *argv[]) { /* very primitive argument checking. We only have one argument, which is our prefix */ if (argc < 1 || !argv[1]) { printf("Usage: waitforip [prefix]\n"); printf(" where [prefix] is the ip address or partial ip address that you\n"); printf(" are waiting to recieve. For instance: 'waitforip 192.168' will\n"); printf(" pause and wait until the machine joins a network starting in 192.168.\n"); printf("\n"); printf(" This uses a string match. '192.16' would match both '192.168.a.b' and \n"); printf(" '192.16.a.b'. The program knows nothing of subnets and octets.\n"); printf("\n"); printf("(waitforip.exe by Eli Fulkerson. http://www.elifulkerson.com for updates.)\n"); return 1; } /* prefixlen gives us the length for strncmp(), later */ int prefixlen = 0; prefixlen = strlen(argv[1]); /* ip address listing stuff */ WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD( 1, 1 ); char name[255]; PHOSTENT hostinfo; char *ip; /* formatting stuff */ bool has_gotten_ip = false; bool has_given_slept_notice = false; bool has_given_big_z = false; /* while we haven't gotten an IP that we like... */ while ( !has_gotten_ip ) { /* get the ip data from the machine's adapters... */ if ( WSAStartup( wVersionRequested, &wsaData ) == 0 ) { if( gethostname ( name, sizeof(name)) == 0) { if((hostinfo = gethostbyname(name)) != NULL) { /* loop through all the ip addresses. If we get a match against prefix, return 0 */ int count = 0; while(hostinfo->h_addr_list[count]) { ip = inet_ntoa (*(struct in_addr *)hostinfo->h_addr_list[count]); if ( strncmp(argv[1], ip, prefixlen) == 0) { has_gotten_ip = true; } count++; } } } } /* Pretty formatting stuff. Give the "waiting message" once. Give the "big Z" on the next cycle. Give "little z" on all cycles after that */ if (!has_given_slept_notice) { printf("Waiting for matching ip address... "); has_given_slept_notice = true; } else { if (!has_given_big_z) { printf("Z"); has_given_big_z = true; } else { printf("z"); } /* sleep for one second, then go back and try again */ Sleep(1000); } } /* our ip matches, lets roll */ printf("\nAddress found, continuing...\n"); return 0; }