How to get IP Address of machine?

Is there any API call to get IP Address of a machine? I know there is function which returns name of the machine, gethostname(). But I need the IP.

Thanks & Regards,
Ahsan

See gethostbyname()

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.

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!

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...

#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.

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

Damn, the inet_ntoa won't work. I want to at least be able to get the IP after a call to accept on the socket.... I'll go searching, unless you all are bored and can help me out. If I find anything, I'll post it up...too bad no one around here as a good TCP for Unix book.

BTW, just out of curiosity, you're in Rockville and using HP-UX machines, any chance you're on an FAA contract?

Why won't inet_ntoa work? It seems to work whenever I try it. Post your code, maybe I or someone else will see something. And there are dozens of servers with source code available for you to find on the Internet. Most are able to log client ip addresses.

The best network programming book is UNIX Network Programming by Rich Stevens.

I will ignore your quest for additional clues to my identity. :cool:

it's so hard to read your source code.hehe.
What should I do for my first step of learning C network programming?
maybe it's not easy to answer.

I didn't think that inet_ntoa would work because I didn't have an in_addr structure to pass it, I only had a struct sockaddr_in, and I didn't see an in_addr structure residing within that. So I didn't want to sit and guess what part (if any) of that structure to pass it, or try and find something to extract that information. However the man page for what I ended up using (inet_ntop) does say that is equivalent to inet_ntoa, although it takes entirely different arguments.

Anyway, I found code (in the Steven's book) to obtain my desired results. I knew we had a copy laying around here somewhere, I just couldn't find it...but you're right it is the God of network books.

So (drumroll) for those curious, this is what I ended up doing. Keep in mind that I needed to go from an integer socket descriptor to an IP, so this code may not be exactly what you need.

Just a quick note, the saddr and slen variables work fine with the type I gave them for HP-UX (and Redhat - while it gives compile warnings it works)). However, some OSes want saddr to be of type struct sockaddr *, and some want slen to be of type socklen_t. And while that may work, sockaddr doesn't have the internet sin_addr field required...so some typecasting may need to be done to shut those compilers up.

Also, this isn't the exact code I used, but it does the job and presents the basics without all the garbage I put around it for my particular needs.


int getIP(int sfd, char *buf, size_t bufSize, int getMine)
{
   int  rVal;
   struct sockaddr_in saddr;
   int slen;

   /* get saddr depending on what caller wants */

   if (getMine)
   {
      rVal = getsockname(sfd, &saddr, &slen);
   }
   else
   {
      rVal = getpeername(sfd, &saddr, &slen);
   }

   /* insure saddr was filled in */

   if (rVal < 0)
   {
      /* it wasn't */

      rVal = 0;
   }
   else
   {
      /* it was - convert address in sin_addr to dotted IP */

      rVal = (inet_ntop (AF_INET, &saddr.sin_addr, buf, bufSize) == NULL) ? 0 : 1;
   }

   return rVal;
}

Also, if you wanted to know port information, you could get it with:

port = ntohs(saddr.sin_port);

Provided, of course, that saddr was filled in.

getMine allows you to get information regarding the local socket connection, otherwise (by passing it 0) you get information regarding the connection at the other end (the peer).

And that's it...hope this helps those curious.