How to find the IP address of current machine in C

Discussion in 'C Programming' started by venutaurus539@gmail.com, May 29, 2009.

  1. Guest

    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.
    , May 29, 2009
    #1
    1. Advertising

  2. Jim Guest

    On 2009-05-29, <> wrote:
    > 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
    --
    http://www.ursaMinorBeta.co.uk http://twitter.com/GreyAreaUK
    Please help save Bletchley Park - sign the petition for
    Government funding at: (open to UK residents and ex.pats)
    http://petitions.number10.gov.uk/BletchleyPark/ Thank you.
    Jim, May 29, 2009
    #2
    1. Advertising

  3. Guest

    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
    On May 29, 3:19 pm, Jim <> wrote:
    > On 2009-05-29, <> wrote:
    >
    > > 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
    > --http://www.ursaMinorBeta.co.uk http://twitter.com/GreyAreaUK
    > Please help save Bletchley Park - sign the petition for
    > Government funding at:     (open to UK residents and ex.pats)http://petitions.number10.gov.uk/BletchleyPark/   Thank you.
    , May 29, 2009
    #3
  4. On 29 May 2009 at 9:48, wrote:
    > 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;
    }
    Antoninus Twink, May 29, 2009
    #4
  5. dfighter Guest

    wrote:
    > 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)

    --

    dfighter
    dfighter, May 29, 2009
    #5
  6. Chris Dollin Guest

    Richard wrote:

    > dfighter <> writes:
    >
    >> wrote:
    >>> 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)

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

    --
    "I see a great hand reaching out of the stars." Elric, /Babylon 5/

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN
    Chris Dollin, May 29, 2009
    #6
  7. In article <gvopbf$sg6$>,
    Chris Dollin <> wrote:
    ....
    (rgrdev asked)
    >> 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".


    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...
    Kenny McCormack, May 29, 2009
    #7
  8. Flash Gordon Guest

    Antoninus Twink wrote:
    > On 29 May 2009 at 9:48, wrote:
    >> 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


    <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.
    --
    Flash Gordon
    Flash Gordon, May 29, 2009
    #8
  9. Richard Bos Guest

    Han from China <> wrote:

    > 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
    Richard Bos, May 29, 2009
    #9
  10. In article <>,
    Gordon Burditt <> 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 McCormack, May 29, 2009
    #10
  11. Richard Bos Guest

    Richard <> wrote:

    > (Kenny McCormack) writes:
    >
    > > 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 was fishing.


    Nice try, but no dice.

    Richard
    Richard Bos, May 30, 2009
    #11
  12. Guest 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.

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


    --
    -----BEGIN GEEK CODE BLOCK-----
    GCS/CM/CC/E/IT/LS/M d-(--) C++++$ UBL++++$ P++++ L+++++$ E--- W+++ w--
    PS+++ PE-- Y++ PGP+++ R++ tv-- b++>+++ D+ G>+++ e++>+++++ h* r++ z+++
    ------END GEEK CODE BLOCK------
    Guest, May 30, 2009
    #12
  13. Gordon Burditt wrote:
    > 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
    Joachim Schmitz, May 30, 2009
    #13
  14. On 30 May 2009 at 18:13, Joachim Schmitz wrote:
    > Gordon Burditt wrote:
    >> 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.


    What part of the word "most" is causing you trouble?
    Antoninus Twink, May 30, 2009
    #14
  15. Antoninus Twink wrote:
    > On 30 May 2009 at 18:13, Joachim Schmitz wrote:
    >> Gordon Burditt wrote:
    >>> 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.

    >
    > What part of the word "most" is causing you trouble?


    What part of "and one of them is" is causing you trouble?
    Joachim Schmitz, May 30, 2009
    #15
  16. Han from China <> writes:

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

    --
    Chris.
    Chris McDonald, May 31, 2009
    #16
  17. Mark McIntyre wrote:
    > Joachim Schmitz wrote:
    >> Antoninus Twink wrote:
    >>> On 30 May 2009 at 18:13, Joachim Schmitz wrote:
    >>>> Gordon Burditt wrote:
    >>>>> 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.
    >>>
    >>> What part of the word "most" is causing you trouble?

    >>
    >> What part of "and one of them is" is causing you trouble?

    >
    > 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
    Joachim Schmitz, May 31, 2009
    #17
  18. Rafael Guest

    Gordon Burditt escreveu:

    >> I don't even want to extract the
    >> IP address from ifconfig because it is time consuming.

    >
    > 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
    Rafael, Jun 3, 2009
    #18
  19. tenga

    Joined:
    Oct 19, 2011
    Messages:
    1
    Kenny you are f...en idiot
    tenga, Oct 19, 2011
    #19
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. UJ
    Replies:
    4
    Views:
    711
    John Rivers
    Sep 2, 2005
  2. Daniel Czajko
    Replies:
    3
    Views:
    13,453
    kesahey
    Oct 21, 2009
  3. Wybo Dekker
    Replies:
    1
    Views:
    352
    Yukihiro Matsumoto
    Nov 15, 2005
  4. vdvorkin
    Replies:
    0
    Views:
    399
    vdvorkin
    Feb 10, 2011
  5. vdvorkin
    Replies:
    3
    Views:
    808
    vdvorkin
    Feb 14, 2011
Loading...

Share This Page