Rick Macdonald wrote:
> 
> On Mon, 21 Jun 1999, Ramesh Natarajan wrote:
> 
> > This message was sent from Geocrawler.com by "Ramesh Natarajan" <[EMAIL 
> > PROTECTED]>
> >
> > Hi, Once dialed in, how to determine the local (dynamically assigned)
> > IP id? I know pppd invokes ip-up with the local IP. But outside that,
> > is there a env variable, API or a file that holds returns this info?
> 
> I would use ip-up to write a file that contains the IP. However, I used to
> use the following sometimes. I don't use ppp anymore, but this might
> still work. awk can probably do it more elegantly...
> 
> /sbin/ifconfig | grep P-t-P | cut -d: -f2 | cut -d" " -f1

nice, now can you do that with a perl one-liner? :)

Well it's better than my c utility that does it. But maybe the c utility is a
few nanoseconds faster. :P

--Brock

---------------------  PGP key ID: FED76A3D <[EMAIL PROTECTED]> 4 / 5 / 1999

   __ _    Debian GNU           R. Brock Lynn <[EMAIL PROTECTED]>
  / /(_)_ __  _   ___  __   http://www.debian.org/ irc.openprojects.net
 / / | | '_ \| | | \ \/ /                  'Free Software'
/ /__| | | | | |_| |>  <   Remember that's "Free" as in Freedom, not "Free" as
\____/_|_| |_|\__,_/_/\_\   in price!   Debian's the Greatest!
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>

#include <unistd.h>
#include <stdio.h>

static struct in_addr device_address(const char *if_name)
{
        struct ifreq ifr;
        struct sockaddr_in *addr;
        int s;

        s = socket(PF_INET, SOCK_DGRAM, 0);
        if (s == -1)
        {
                perror("socket");
                exit(1);
        }

        strcpy(ifr.ifr_name, if_name);

        if (ioctl(s, SIOCGIFADDR, &ifr) < 0)
        {
                perror("ioctl(SIOCGIFADDR)");
                exit(1);
        }

        addr = (struct sockaddr_in *) &ifr.ifr_broadaddr;

        close(s);

        return addr->sin_addr;
}

int main(int argc, char **argv)
{
        struct in_addr addr;

        if (argc < 2)
        {
                fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
                exit(1);
        }

        addr = device_address(argv[1]);

        printf("IP Address: %s\n", inet_ntoa(addr));

        return 0;
}

Reply via email to