Hello everyone,
I have made this function to determine if the user has a router or not, is this a good method for retrieving IP, any feedback is appreciated.
Code:
BYTE r = RouterCheck();
if(r == 10 || r == 192) {
//Found Router IP, Retrieve IP via whatismyip.com or some other online website for retrieving IP.
}
Code:
BYTE RouterCheck() {
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
char ip[16], fip[4];
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 ) {
if( gethostname ( name, sizeof(name)) == 0) {
if((hostinfo = gethostbyname(name)) != NULL) {
strcpy(ip, inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list));
}
}
else {
WSACleanup();
return NULL;
}
WSACleanup();
}
else return NULL;
char *p;
p = strchr(ip, '.');
if(p == NULL) return NULL; //p-ip
strncpy(fip, ip, p-ip);
return (BYTE)atoi(fip);
}