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);
}
This should work with any c or c++ compiler. But I only tested on HP-UX.
Quote:
Originally posted by DreamWarrior
Also, to extend this a bit, is it possible to obtain the information starting from a file descriptor that is a socket. I.E. if the only piece of information I have about the connection is the file descriptor, can I get the sockaddr_in structure from that to pass to gethostbyname to then convert into an IP?
Thanks!
|
Yow!! We have 4 hour response time.
You cannot do that portably and it usually requires root power. Look at the source code for lsof. It does stuff like that.
But all sockets structures store ip address not domain names