[ale] Finding your IP address

Todd Graham Lewis tlewis at mindspring.net
Sun Aug 17 07:13:30 EDT 1997


On Sun, 17 Aug 1997, Zephaniah E. Hull wrote:

> I'm looking for a good way to find the machine's IP address from
> inside a C program, The only thing that comes to mind is that (I
> think) you can get your hostname and then do a DNS lookup on that,
> But I don't have a clue how,

Check out the ifconfig source code.  I think you just open a generic
network socket and then do some ioctl's on it.  An strace of "netstat
-in" suggests the following:

socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 3
ioctl(3, SIOCGIFCONF, 0xbffff674)       = 0
ioctl(3, SIOCGIFFLAGS, 0xbffff63c)      = 0
ioctl(3, SIOCGIFADDR, 0xbffff63c)       = 0
ioctl(3, SIOCGIFHWADDR, 0xbffff63c)     = 0
ioctl(3, SIOCGIFMETRIC, 0xbffff63c)     = 0
ioctl(3, SIOCGIFMTU, 0xbffff63c)        = 0
ioctl(3, SIOCGIFDSTADDR, 0xbffff63c)    = 0
ioctl(3, SIOCGIFBRDADDR, 0xbffff63c)    = 0
ioctl(3, SIOCGIFNETMASK, 0xbffff63c)    = 0

The "SIOCGIFADDR" and "SIOCGIFNETMASK" calls seem to be promising.

Good luck.

-- 
Todd Graham Lewis       Manager of Web Engineering    MindSpring Enterprises
(800) 719-4664, x2804             Linux!               tlewis at mindspring.net






More information about the Ale mailing list