How to find the IP address of current machine in C

V

venutaurus539

Hi All,
Can some one please let me know how to find the ip address of
a machine within a C program. I've tried gethostbyname() but it uses /
etc/hosts file which is not I want. I don't even want to extract the
IP address from ifconfig because it is time consuming. So, can any one
let me know how can I do this without effecting portability across
linux flavors (No Windows please).

Awaiting your reply,

Thank you,
Venu Madhav.
 
J

Jim

Hi All,
Can some one please let me know how to find the ip address of
a machine within a C program. I've tried gethostbyname() but it uses /
etc/hosts file which is not I want. I don't even want to extract the
IP address from ifconfig because it is time consuming. So, can any one
let me know how can I do this without effecting portability across
linux flavors (No Windows please).

Keep in mind that a machine can have several IPs, including (but not limited
to) one for each network card.

Jim
 
V

venutaurus539

Thanks for the reply,
Yeah, I know that. Is there any way to get that list
of IPs of a machine.

Thank you,
Venu Madhav
 
A

Antoninus Twink

Can some one please let me know how to find the ip address of a
machine within a C program. I've tried gethostbyname() but it uses /
etc/hosts file which is not I want.

The program below will list all your up network interfaces and their IP
addresses. Note that if you're behind a router/firewall/etc. then the IP
address will probably be internal to your LAN, as with ifconfig. To get
the "external-facing" IP address, I don't think there's any better way
than using HTTP to download a page from a site like whatismyip.org and
then scraping the IP it reports.


#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>

int main(void)
{
int fd;
struct if_nameindex *curif, *ifs;
struct ifreq req;

if((fd = socket(PF_INET, SOCK_DGRAM, 0)) != -1) {
ifs = if_nameindex();
if(ifs) {
for(curif = ifs; curif && curif->if_name; curif++) {
strncpy(req.ifr_name, curif->if_name, IFNAMSIZ);
req.ifr_name[IFNAMSIZ] = 0;
if (ioctl(fd, SIOCGIFADDR, &req) < 0)
perror("ioctl");
else
printf("%s: [%s]\n", curif->if_name,
inet_ntoa(((struct sockaddr_in*) &req.ifr_addr)->sin_addr));
}
if_freenameindex(ifs);
if(close(fd)!=0)
perror("close");
} else
perror("if_nameindex");
} else
perror("socket");
return 0;
}
 
D

dfighter

Hi All,
Can some one please let me know how to find the ip address of
a machine within a C program. I've tried gethostbyname() but it uses /
etc/hosts file which is not I want. I don't even want to extract the
IP address from ifconfig because it is time consuming. So, can any one
let me know how can I do this without effecting portability across
linux flavors (No Windows please).

Awaiting your reply,

Thank you,
Venu Madhav.
"The C programming language" cannot handle networking by default.
I suggest you post in comp.unix.programmer as they are the most
competent regarding unix programming issues (that includes network
programming)
 
C

Chris Dollin

Richard said:
What on earth makes you think it's Unix? Linux maybe?

"portability across linux flavors" and "/etc/hosts" are what we in the
comprehension business call "clues".
 
K

Kenny McCormack

(rgrdev asked)
"portability across linux flavors" and "/etc/hosts" are what we in the
comprehension business call "clues".

One of the common bits of the Usenet religion (*) is that Linux is not Unix.
Note that this statement may be technically true (for certain sets of
definitions and beliefs), but that's not the point. The point is that
for all practical purposes, Linux is "a Unix" and to maintain otherwise
is, well, to use the term from my previous post, "pushing an agenda".

I must admit that I was surprised to see that sort of post coming from
rgrdev, who is normally a quite rational poster (aka, "troll", in the
jargon of CLC).

(*) I'm using the word "religion" here as a general umbrella for man's
irrational beliefs. Everybody has some...
 
F

Flash Gordon

Antoninus said:
The program below will list all your up network interfaces and their IP
addresses. Note that if you're behind a router/firewall/etc. then the IP

<snip>

On one of my Linux servers it does not. The OP should go to either
comp.unix.programmer or one of the Linux groups if s/he wants to know
how to get all of the addresses.
 
R

Richard Bos

Han from China said:
like Kiki Thompson and Dicky Heathfield.

Hattie, you are getting more childish by the minute. I expect that from
Kenny, not from you.

(IOW: please keep better track of your personae.)

Richard
 
K

Kenny McCormack

Standard C doesn't provide networking facilities.

Is the ip address you want that of the machine you're running on?
If not, the way to find it is via DNS lookup (e.g. gethostbyname()
).

Most machines that are actually connected to a network have more
than one IPv4 address, and one of them is "127.0.0.1". Most machines

You know, I just realized that the best answer to this question is...
(Are you ready for it? Drum roll, please...)

127.0.0.1

Simple, effective, portable (see implementation below, which should meet
the standards of this NG), and likely to be very efficient.

#include <stdio.h>

int main(void) { puts("(at least one of) Your IP address(es) is: 127.0.0.1"); }
 
G

Guest

This is the code I use for getting the IP address associated to a
certain network interface:

char* get_ipv4_addr(char *ifc) {
int sd=socket(AF_INET,SOCK_STREAM,0);
struct ifreq ifr;

strncpy (ifr.ifr_name,ifc,sizeof(ifr.ifr_name));
struct sockaddr_in *sin = (struct sockaddr_in*) &ifr.ifr_addr;

ioctl(sd,SIOCGIFNAME,&ifr);
ioctl(sd,SIOCGIFADDR,&ifr);

if (!sin->sin_addr.s_addr) return NULL;
else return inet_ntoa(sin->sin_addr);
}

p.s. I see more and more gratuitous and very poorly documented trolls
here. Just think about shutting down this newsgroup instead of
re-routing everybody makes a C-related question to other newsgroups, if
that is not a true ANSI C oriented question. This newsgroup gets very
useless in this case.
 
J

Joachim Schmitz

Gordon said:
Most machines that are actually connected to a network have more
than one IPv4 address, and one of them is "127.0.0.1".

Nope, this is not required at all.

Bye, Jojo
 
C

Chris McDonald

Han from China said:
Note that comp.unix.programmer posters are happy to deal with
ISO C as well. So let's shut down comp.lang.c and redirect
all posters to comp.unix.programmer, where they can get the
most knowledgeable experts, instead of the phony know-nothings
like Kiki Thompson and Dicky Heathfield.


Do you feel that, some time in the future, you'll ever be able to enter
a c.l.c. thread without your obligatory attack on individuals?
It will be a pleasure.
 
J

Joachim Schmitz

Mark said:
To be fair to the Troll, your comment didn't need the "nope" and would
have been uncontrovertial without it.

Well, I intereted Gordon's statement as '127.0.0.1 is required', which it is
not.

Bye, Jojo
 
R

Rafael

Gordon Burditt escreveu:
That may be the most portable way of doing it, for some reasonable
guess at what you are looking for.

So... to the OP: Read the ifconfig source and find your code there.

Regards
Rafael
 
Joined
Oct 19, 2011
Messages
1
Reaction score
0
In article <[email protected]>,
Gordon Burditt <[email protected]> wrote:
>> Can some one please let me know how to find the ip address of
>>a machine within a C program.

>
>Standard C doesn't provide networking facilities.
>
>Is the ip address you want that of the machine you're running on?
>If not, the way to find it is via DNS lookup (e.g. gethostbyname()
>).
>
>Most machines that are actually connected to a network have more
>than one IPv4 address, and one of them is "127.0.0.1". Most machines


You know, I just realized that the best answer to this question is...
(Are you ready for it? Drum roll, please...)

127.0.0.1

Simple, effective, portable (see implementation below, which should meet
the standards of this NG), and likely to be very efficient.

#include <stdio.h>

int main(void) { puts("(at least one of) Your IP address(es) is: 127.0.0.1"); }

Kenny you are f...en idiot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top