C program : how to list interfaces with a valid IP ?

Hi all,

I think that the title is explicit enough :slight_smile: I would like in a C program to list the interfaces with a valid IP. Those that can permit to send something over a network.

Thanks :wink:

Having an IP address does not mean the routing is good enough to be able to send or receive anything over the interface.

I have been able to do so with getifaddrs() on Linux. Not sure other Unix flavours. Go search for manpage for that function.

Hi

You can use the "system" function from C, and you can call ifconfig like commands (i'm not sure that such command exist on UN*X, but it is possible). In that way you can list the interfaces. If you pipe the command with grep, you can also filter out which interfaces has a valid IP address.

Hope it helps

Stevens has a good example that is pretty compliant and portable.
Buy, beg or borrow Unix Network Programming if you don't already have a copy.

perhaps you need to write the survices by yourself,the interfaces should be designed thorouly .well,that's all what I know.

Who is 'Stevens' ? I'll look for this book thanks for the tip

Nice function ! That works but I have a problem with the assignment "ifcur = iflist" : error: incompatible types in assignment ?! I don't understand why.

	
...
struct ifaddrs*		iflist 
,		        ifcur ;
	
if (getifaddrs (&iflist) < 0)
	printf ("* Erreur getifaddrs ! \n") ;

ifcur = iflist ;
	
freeifaddrs(iflist);
...

Your declaration of struct ifaddrs* might be throwing off the compiler when it comes to a struct ifaddrs object and a pointer to that type. The argument to getifaddrs is a pointer to pointer to struct ifaddrs so use &ifcur instead of &iflist and since freeifaddrs takes a unilevel pointer use ifcur.

...
struct ifaddrs iflist, *ifcur
ifcur = &iflist;

if (getifaddrs (&ifcur) < 0)
	printf ("* Erreur getifaddrs ! \n") ;
	
freeifaddrs(ifcur);
...