figuring out the local IP address of an interface

2006-10-24 Thread Girish Venkatachalam
Dear friends,

I know this question sounds basic but it is not. 

How to programmatically determine the IP address of an interface?

(Programmatically means using C of course :-)

getsockname(2) is supposed to work but it doesn't since it returns 0.0.0.0 for 
INADDR_ANY. getpeername(2) works, so am I supposed to send a packet, do a 
getpeername(2) at the other side and get back the result in the payload? 

Till now I have got away with a system(/sbin/ifconfig -a | grep hack.

Am I missing something? I surely am since the very notion of IP address of an 
interface is silly since it could be bridged,carped, trunked etc.

But say, I have obtained 192.168.1.2 thro' DHCP and this is what I want to 
figure out. That is the only IP that interface has. How to achieve that?

Thanks.

regards,
Girish

-- 
Great people are not defined by ability but by nobility



Re: figuring out the local IP address of an interface

2006-10-24 Thread Jason Stubbs
On Tuesday 24 October 2006 17:14, Girish Venkatachalam wrote:
 How to programmatically determine the IP address of an interface?
...
 Till now I have got away with a system(/sbin/ifconfig -a | grep hack.

A quick browse through ifconfig.c lead to getifaddrs(3) which seems to do 
exactly what you want.

--
Jason Stubbs



Re: figuring out the local IP address of an interface

2006-10-24 Thread Pawel S. Veselov

man -s3 getifaddrs ?

-- Pawel.

Girish Venkatachalam wrote:

Dear friends,

I know this question sounds basic but it is not. 


How to programmatically determine the IP address of an interface?

(Programmatically means using C of course :-)

getsockname(2) is supposed to work but it doesn't since it returns 0.0.0.0 for INADDR_ANY. getpeername(2) works, so am I supposed to send a packet, do a getpeername(2) at the other side and get back the result in the payload? 


Till now I have got away with a system(/sbin/ifconfig -a | grep hack.

Am I missing something? I surely am since the very notion of IP address of an 
interface is silly since it could be bridged,carped, trunked etc.

But say, I have obtained 192.168.1.2 thro' DHCP and this is what I want to 
figure out. That is the only IP that interface has. How to achieve that?

Thanks.

regards,
Girish




Re: figuring out the local IP address of an interface

2006-10-24 Thread Otto Moerbeek
On Tue, 24 Oct 2006, Girish Venkatachalam wrote:

 Dear friends,
 
 I know this question sounds basic but it is not. 
 
 How to programmatically determine the IP address of an interface?
 
 (Programmatically means using C of course :-)
 
 getsockname(2) is supposed to work but it doesn't since it returns 0.0.0.0 
 for INADDR_ANY. getpeername(2) works, so am I supposed to send a packet, do a 
 getpeername(2) at the other side and get back the result in the payload? 
 
 Till now I have got away with a system(/sbin/ifconfig -a | grep hack.
 
 Am I missing something? I surely am since the very notion of IP address of an 
 interface is silly since it could be bridged,carped, trunked etc.
 
 But say, I have obtained 192.168.1.2 thro' DHCP and this is what I want to 
 figure out. That is the only IP that interface has. How to achieve that?

You are looking for getifaddrs(3)

-Otto



Re: figuring out the local IP address of an interface

2006-10-24 Thread Mathieu Sauve-Frankel
networking(4)
getifaddrs(3) 

-- 
Mathieu Sauve-Frankel



Re: figuring out the local IP address of an interface

2006-10-24 Thread Nick Guenther

On 10/24/06, Girish Venkatachalam [EMAIL PROTECTED] wrote:

Dear friends,

I know this question sounds basic but it is not.

How to programmatically determine the IP address of an interface?

(Programmatically means using C of course :-)

getsockname(2) is supposed to work but it doesn't since it returns 0.0.0.0 for 
INADDR_ANY. getpeername(2) works, so am I supposed to send a packet, do a 
getpeername(2) at the other side and get back the result in the payload?

Till now I have got away with a system(/sbin/ifconfig -a | grep hack.

Am I missing something? I surely am since the very notion of IP address of an 
interface is silly since it could be bridged,carped, trunked etc.

But say, I have obtained 192.168.1.2 thro' DHCP and this is what I want to 
figure out. That is the only IP that interface has. How to achieve that?



I have never done it myself, but some quick documentation-digging
hints it should be possible. You can use ioctl-calls to access this
information.

Please see netintro(4) and good luck.

-Nick



Re: figuring out the local IP address of an interface

2006-10-24 Thread Philip Guenther

On 10/24/06, Girish Venkatachalam [EMAIL PROTECTED] wrote:

How to programmatically determine the IP address of an interface?


Your question is unclear.  Do you _really_ want to look up the list of
IP addresses bound to a given interface, specified by name and/or
index?  That what your question asks for, but that information is
usually only needed for some UDP servers and routing programs.  Or do
you just want to know the local IP of a connected socket?  Note that
the IP choosen may depend on the IP that you're sending/connecting to.



getsockname(2) is supposed to work but it doesn't since it returns 0.0.0.0 for
INADDR_ANY.


Was that before or after you used connect(), sendto(), sendmsg(), or accept()?



Till now I have got away with a system(/sbin/ifconfig -a | grep hack.


If you *really* want to see how the interfaces are configured (I doubt
it), you should take a look at getifaddrs()

Btw, I strongly suggest you pick up a copy of UNIX Network
Programming, volume 1 by Stevens.  You should ignore the XTI stuff in
the back, but the rest is Good Stuff.


Philip Guenther



Re: figuring out the local IP address of an interface

2006-10-24 Thread Girish Venkatachalam
On Tue, Oct 24, 2006 at 10:43:57AM +0200, Otto Moerbeek wrote:
 
 On Tue, 24 Oct 2006, Girish Venkatachalam wrote:
 
  Dear friends,
  
  I know this question sounds basic but it is not. 
  
  How to programmatically determine the IP address of an interface?
  
  (Programmatically means using C of course :-)
  
  getsockname(2) is supposed to work but it doesn't since it returns 0.0.0.0 
  for INADDR_ANY. getpeername(2) works, so am I supposed to send a packet, do 
  a getpeername(2) at the other side and get back the result in the payload? 
  
  Till now I have got away with a system(/sbin/ifconfig -a | grep hack.
  
  Am I missing something? I surely am since the very notion of IP address of 
  an interface is silly since it could be bridged,carped, trunked etc.
  
  But say, I have obtained 192.168.1.2 thro' DHCP and this is what I want to 
  figure out. That is the only IP that interface has. How to achieve that?
 
 You are looking for getifaddrs(3)

Thanks, but it just slipped my mind. I knew this but couldn't figure out what 
on earth the man page was trying to say.

Is there a way to portably make this work across linux,FreeBSD,NetBSD and 
OpenBSD?

Thanks to everyone who responded.

This mailing list rocks! :-)

As usual. :-)

regards,
Girish

-- 
Having nothing nothing can he lose



Re: figuring out the local IP address of an interface

2006-10-24 Thread Ste Jones

Is there a way to portably make this work across linux,FreeBSD,NetBSD and 
OpenBSD?


If I remember correctly you can possibly do it with libdnet
http://libdnet.sourceforge.net/

Cheers
Ste