LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 12-07-2001, 04:51 PM   #1
charlies
LQ Newbie
 
Registered: Feb 2001
Posts: 5

Rep: Reputation: 0
getting your own IP Address


I am trying to get the IP address of my own machine
for display. The closest I have been able to get is the
local '127.0.0.1' number. I want the 123.45.67.8 number.
I suspect part of the problem is the ability to support
multiple interfaces, in my case I want the IP address of
the 'eth0' interface. So, is there a function I can call, or a
file I can parse to obtaine this info ?

thanks,

cs
 
Old 12-09-2001, 04:27 PM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
The simplest way would be to run the command "hostname -i"
 
Old 12-10-2001, 08:14 AM   #3
charlies
LQ Newbie
 
Registered: Feb 2001
Posts: 5

Original Poster
Rep: Reputation: 0
I'm looking for a function call that I can call from a program.

cs
 
Old 12-10-2001, 09:31 AM   #4
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
Use the gethostbyname function. If you don't know the hostname then use the gethostname function to obtain it first. Just remember it's when you get the address back from gethostbyname that it's in network byte order.
If you want that in a standard dotted format you can use the inet_ntoa function.
 
Old 12-10-2001, 01:25 PM   #5
pinoy
LQ Newbie
 
Registered: Sep 2001
Posts: 21

Rep: Reputation: 15
On which interface? There is an ioctl for it, I can't remember it on top of my head, but a quick grep through the kernel for SIOC* should point you to the right direction. The simplest way however is to use gethostbyname(localhost), this uses the resolver to get a list of ip addresses.
 
Old 12-10-2001, 02:13 PM   #6
charlies
LQ Newbie
 
Registered: Feb 2001
Posts: 5

Original Poster
Rep: Reputation: 0
Here's a part of a test pgm I've been using to get the IP address.
So far, I've only been able to get the 127.0.0.1 address. The
gethostname function returns the correct name ("cs_linux"),
but I still can't get the eth0 interface address. I'll see what info
I can find on the IOCTL approach.

cs



//--------------------------------------------------------------

int i;
int rc;
struct in_addr addr;
struct hostent *hostinfo = NULL;
char *ptr;
char buf[200];

rc = gethostname(buf, sizeof(buf));
if(rc == 0)
{
printf("Returned Host Name: %s\n",buf);
hostinfo = gethostbyname("localhost");
if(hostinfo == NULL)
{
printf("gethostbyname failed\n");
}
else
{
printf("gethostbyname ok !\n");
printf("name: %s, addr type: %d, addr len: %d\n",
hostinfo->h_name, hostinfo->h_addrtype, hostinfo->h_length );

for(i=0; ((i<5) && (hostinfo->h_aliases[i] != NULL)); i++)
{
ptr = hostinfo->h_aliases[i];
if(ptr)
{
printf("Alias %d: %s\n",i, ptr);
}
}

printf("\n\n");

ptr = hostinfo->h_addr_list[0];
while(*ptr)
{
addr.s_addr = *((uint32_t *) ptr);
printf("Address %s\n", inet_ntoa(addr));
ptr += hostinfo->h_length;
}
}
}
else
{
// error
printf("Error returned from gethostname = %d\n", rc);
}
 
Old 12-10-2001, 04:55 PM   #7
pinoy
LQ Newbie
 
Registered: Sep 2001
Posts: 21

Rep: Reputation: 15
Did a quick search through google and found this: http://daq.triumf.ca/online/software...Ch3.htm#E48E71

You can use SIOCGIFADDR. It returns you a struct ifreq; which has the name of the interface and the address of the interface. This however requires you have a socket bound to the correct interface.

You probably want to use SIOCGIFCONF which returns you a list of interfaces and its struct ifreqs.

I would provide source if I had access to a Linux box. Unfortunately my modem died at home and am still waiting for my DSL connection.

I also heard that getifaddrs() is easier to use, so you might also like to check that out.
 
Old 03-01-2003, 02:49 PM   #8
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Rep: Reputation: 30
I was looking for SIOCGIFADDR at LQ, and I found only this posting.

Although you deal with a different issue, I hope you could advise.
There is a problem. Whenever I try to start OpenOffice or StarOffice I get a splash banner for about 15 - 30 minutes, before the program actually starts (if it does at all). Shell returns:

SIOCGIFADDR got '00:00:00:00:00:00'

Please, if you know something about this, help.
 
Old 09-25-2004, 02:14 AM   #9
Tomik
LQ Newbie
 
Registered: Sep 2004
Posts: 1

Rep: Reputation: 0
my solution

#include <stdlib.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <sys/ioctl.h>
int main(int argn,char** argv)
{
int sock,n;
struct ifreq *ifr;
struct ifconf ifc;
char buf[1024],addres[16];
unsigned char* adr;
sock=socket(AF_INET,SOCK_STREAM,0);
ifc.ifc_buf=buf;
ifc.ifc_len=1024;
ioctl(sock, SIOCGIFCONF, &ifc);
n = ifc.ifc_len/sizeof(struct ifreq);
for (ifr = ifc.ifc_req; n > 0; n--, ifr++)
{
if (ifr->ifr_addr.sa_family == AF_INET)
{
adr=ifr->ifr_ifru.ifru_addr.sa_data+2;
snprintf(addres,16,"%i.%i.%i.%i",adr[0],adr[1],adr[2],adr[3]);
printf("%s = %s\n",ifr->ifr_name,addres);
}
}
close(sock);
return 0;
}
 
  


Reply



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
dhcp no ip address and netmask dont match route address pengy666 Linux - Wireless Networking 1 05-08-2005 09:33 AM
how to get ip address, broadcast address, mac address of a machine sumeshstar Programming 2 03-12-2005 04:33 AM
DHCP Server MAC Address found, IP address not assigned wmburke Linux - Wireless Networking 17 11-17-2004 10:33 AM
problem to print source address and destination address jooboo Programming 2 11-26-2003 03:24 PM
Should i use the eth0 ip address of my internet ip address when applying iptables ForumKid Linux - Security 2 01-03-2002 08:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:27 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration