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. |
 |
12-07-2001, 04:51 PM
|
#1
|
|
Newbie
Registered: Feb 2001
Posts: 5
|
getting your own IP Address
[ Log in to get rid of this advertisement]
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
|
|
|
|
12-09-2001, 04:27 PM
|
#2
|
|
Guru
Registered: Dec 2001
Location: The Netherlands
Distribution: Gentoo + LFS
Posts: 1,316
|
The simplest way would be to run the command "hostname -i"
|
|
|
|
12-10-2001, 08:14 AM
|
#3
|
|
Newbie
Registered: Feb 2001
Posts: 5
|
I'm looking for a function call that I can call from a program.
cs
|
|
|
|
12-10-2001, 09:31 AM
|
#4
|
|
Guru
Registered: Dec 2001
Location: The Netherlands
Distribution: Gentoo + LFS
Posts: 1,316
|
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.
|
|
|
|
12-10-2001, 01:25 PM
|
#5
|
|
Newbie
Registered: Sep 2001
Posts: 21
|
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.
|
|
|
|
12-10-2001, 02:13 PM
|
#6
|
|
Newbie
Registered: Feb 2001
Posts: 5
|
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);
}
|
|
|
|
12-10-2001, 04:55 PM
|
#7
|
|
Newbie
Registered: Sep 2001
Posts: 21
|
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.
|
|
|
|
03-01-2003, 02:49 PM
|
#8
|
|
Member
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 472
|
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.
|
|
|
|
09-25-2004, 02:14 AM
|
#9
|
|
Newbie
Registered: Sep 2004
Posts: 1
|
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;
}
|
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:54 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
LQ Podcast
LQ Radio
|
|