LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-17-2006, 12:51 AM   #1
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 435

Rep: Reputation: 30
Get IP Address System call


Hello,


Is there a way to get my IP address using a system call of some sort? I've looked at the man page for gethostbyaddr, but there isn't an example how to use it in practice. Thanks for any help.
 
Old 03-17-2006, 05:05 AM   #2
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
This is one method, there are others but less portable:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/ioctl.h> 
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>

#include <arpa/inet.h>
#include <net/if.h>

#include <unistd.h>


#define inaddrr(x) (*(struct in_addr *) &ifr->x[sizeof sa.sin_port])
#define IFRSIZE   ((int)(size * sizeof (struct ifreq)))

typedef char ip_address[15+1];

static int
get_addr(char * ifname,ip_address theip) {

  struct ifreq		*ifr;
  struct ifreq		ifrr;
  struct sockaddr_in	sa;
  struct sockaddr	ifaddr;
  int			sockfd;

  if((sockfd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))==-1)
  {
	printf ("Socket for DGRAM cannot be created\n");
	return -1;
  }

  ifr = &ifrr;

  ifrr.ifr_addr.sa_family = AF_INET;

  strncpy(ifrr.ifr_name, ifname, sizeof(ifrr.ifr_name));

  if (ioctl(sockfd, SIOCGIFADDR, ifr) < 0) {
    printf("No %s interface.\n", ifname);
    return -1;
  }

  ifaddr = ifrr.ifr_addr;
     strncpy(theip,inet_ntoa(inaddrr(ifr_addr.sa_data)),sizeof(ip_address));

  return 0;

}

int main(void){
ip_address	theip;


	if (get_addr("eth0",theip)==0)
	      printf("The ip for eth0 is:%s\n",theip);
        else  
              printf("Couldn't get ip adress for eth0\n");
	return 0;
}

Last edited by nx5000; 03-17-2006 at 05:08 AM.
 
Old 03-17-2006, 05:10 AM   #3
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
By the way, I hope its for a UNIX system?
Otherwise for Winblows I dunno. transform socket to Socket maybe
 
Old 03-17-2006, 08:51 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Quote:
Originally Posted by nx5000
By the way, I hope its for a UNIX system?
Otherwise for Winblows I dunno. transform socket to Socket maybe
Your program neither works as is for Unix.

It is Linux specific, as it assumes the ethernet interface name is "eth0".
It can easily be ported to non-Linux Unix systems though.
 
Old 03-17-2006, 08:51 AM   #5
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
with,
the teletype line that you are currently working and
the id from which you are executing

parse the /var/adm/wtmpx file
you can retrieve the ip address
 
Old 03-17-2006, 08:58 AM   #6
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
nx,

you missed out /usr/include/sys/sockio.h to include
SIOCGIFADDR
 
Old 03-17-2006, 09:25 AM   #7
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
jlliagre you're right its maybe not portable to unix as I use ioctl... its been some time I have no access to Unix stations and all these differents implementations.

mmm eth0 is for the example... remove it if you prefer

Maybe you could post a more-portable solution?

Works perfectly for me without any more includes.
 
Old 03-17-2006, 04:30 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Quote:
Originally Posted by nx5000
jlliagre you're right its maybe not portable to unix as I use ioctl... its been some time I have no access to Unix stations and all these differents implementations.
In fact not, your program is quite portable, and works for me with Solaris.

Quote:
mmm eth0 is for the example... remove it if you prefer
But if I remove it, the program stops working ...

That's the point here, the OP question was about how retrieving one's own IP address.

The problem is that there is no standard way to figure out what is your own network interface, not to mention which one to choose when there are many of them.
Quote:
Maybe you could post a more-portable solution?
Sure, here's one.
Unfortunately it fails in many cases, if the naming is incorrect (like setting the hostname as a localhost alias) or is dynamic and not updated.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>
#include <netdb.h>
#include <sys/types.h>
#include <errno.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
  struct utsname utsname;
  struct hostent *h;

  uname(&utsname);

  if(!(h=gethostbyname(utsname.nodename)))
  {
    fprintf(stderr,"%s\n", hstrerror(h_errno));
    exit(1);
  }
  printf("%s\n", inet_ntoa(*((struct in_addr *)h->h_addr)));
  return 0;
}
Quote:
Works perfectly for me without any more includes.
I too had to add "#include <sys/sockio.h>" for having it compilable.
BSD and Solaris need this, Linux has SIOCGIFADDR defined in more than one place.
 
Old 03-18-2006, 02:32 AM   #9
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
printf("%s\n", inet_ntoa(*((struct in_addr *)h->h_addr)));
i believe this is more readable,
Code:
printf("%s\n", inet_ntoa(*((struct in_addr *)h->h_addr_list[0])));
or this,

Code:
printf("%s\n", inet_ntoa(**((struct in_addr **)h->h_addr_list)));

Last edited by kshkid; 03-18-2006 at 02:35 AM.
 
Old 03-18-2006, 02:42 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
May be ...
 
Old 04-08-2010, 04:29 AM   #11
Rodrex Lee
LQ Newbie
 
Registered: May 2008
Posts: 6

Rep: Reputation: 0
Cool Network IP Address of own machine inside C code

One more solution which I found much better than the above one, to know the network IP address of own machine inside C code is as follows:--

FILE *fd;
char ip_addr[20];
system("cmd=`/sbin/ifconfig`;y=${cmd#*inet addr:};y=${y%% *};echo $y >ipaddr");
fd = fopen("ipaddr", "r+");
fscanf(fd, "%s", &ip_addr);
fclose(fd);
fprintf(stderr,"IP Address:%s", ip_addr);

This works fine for me.
Kindly put your suggestions for above solution.

Thanks & Regards,
Rodrex.


PS: one more solution after such a long time
 
Old 04-09-2010, 05:32 PM   #12
rndm_luser
LQ Newbie
 
Registered: Jan 2009
Posts: 8

Rep: Reputation: 1
Check out getaddrinfo.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System call for getting MAC address Madhusudhan Linux - Networking 5 02-09-2006 09:06 PM
Setting IP Address to an Interface using System Call Kirthi Linux - Networking 1 01-30-2005 11:20 AM
Is it possible to use system() and get the return value from the system call newguy21 Programming 1 08-11-2004 01:37 PM
any sys call to get ip address of NIC jmdey Linux - General 2 05-30-2002 03:09 PM


All times are GMT -5. The time now is 02:52 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration