Quote:
Originally posted by DreamWarrior
Hey, could you all be a bit more specific on this one. I read the man page for gethostbyname and it seems to be returning a generic internal address format. That needs to be converted into a dot notation IP address, and I don't know how to do it.
|
The best solution to stuff like this is to browse some source code. There is a ton of source code on the internet. And there are several programs that format ip addresses. But I'm trapped at the office waiting for a tech, and I don't have anything better to do, so...
Code:
#ifdef __STDC__
#define PROTOTYPICAL
#endif
#ifdef __cplusplus
#define PROTOTYPICAL
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#ifdef PROTOTYPICAL
int main(int argc, char *argv[])
#else
main(argc,argv)
char *argv[];
#endif
{
char *mess;
struct hostent *hp;
int dumpall;
dumpall=0;
while(*++argv) {
if(!strcmp(*argv,(char *)"-d")) {
dumpall=!dumpall;
continue;
}
/*
* Call gethostbyname for current argument
*/
if(! (hp = gethostbyname(*argv))) {
switch(h_errno){
case HOST_NOT_FOUND:
mess=(char *)"Not Found";
break;
case TRY_AGAIN:
mess=(char *)"Time Out";
break;
case NO_RECOVERY:
mess=(char *)"No Recovery";
break;
case NO_ADDRESS:
mess=(char *)"No Address";
break;
default:
mess=(char *)"unknown error";
break;
}
printf("%s %s\n", *argv, mess);
} else {
printf("%s %s %s \n", *argv, hp->h_name,
inet_ntoa(*(struct in_addr *)(hp->h_addr)));
if(dumpall) {
printf(" addresses:\n");
while(*(hp->h_addr_list)){
printf(" %s \n",
inet_ntoa(*(struct in_addr *)*(hp->h_addr_list)++));
}
printf(" aliases:\n");
while(*(hp->h_aliases)){
printf(" %s \n",*(hp->h_aliases)++);
}
}
}
}
exit(0);
}