Link to home
Start Free TrialLog in
Avatar of lwinkenb
lwinkenb

asked on

get ip address of ppp0

Is there any way (using C), to get the ip address of the ppp0 interface?
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

I would parse the results coming back from popen("ifconfig");
Avatar of sunnycoder

    int gethostname(char *hostname, size_t size);

 It returns the name of the computer that your program is running on.

   
    struct hostent *gethostbyname(const char *name);

it returns a pointer to a struct hostent, the layout of which is as follows:

    struct hostent {
        char    *h_name;
        char    **h_aliases;
        int     h_addrtype;
        int     h_length;
        char    **h_addr_list;
    };
    #define h_addr h_addr_list[0]

 

And here are the descriptions of the fields in the struct hostent:


h_name -- Official name of the host.

h_aliases -- A NULL-terminated array of alternate names for the host.

h_addrtype -- The type of address being returned; usually AF_INET.

h_length -- The length of the address in bytes.

h_addr_list -- A zero-terminated array of network addresses for the host. Host addresses are in Network Byte Order.

h_addr -- The first address in h_addr_list.

All descriptions copied from http://www.ecst.csuchico.edu/~beej/guide/net/html/syscalls.html

The hostent structure [usually] contains IP addresses, not ethernet MAC addresses.
ASKER CERTIFIED SOLUTION
Avatar of scn
scn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lwinkenb
lwinkenb

ASKER

scn's example worked perfectly.

Sunny, I had tried doing that way already, but it was only returning 10.0.0.1, and not the external address as well (I realize h_addr_list is an array that can contain more than one IP, but it only had 10.0.0.1 in my case).

Brett, I was parsing the output of ifconfig before, but the program had to make assumptions on the location of the ifconfig.  I wanted it to be more flexible for running on different machines.

Thanks for the input everyone.
There's a nice way of doing it from a bash script I found recently:

INET_IP=`ifconfig ppp0 | grep inet | cut -d : -f 2 | cut -d \  -f 1`