Reputation: 633
I have used getifaddrs() function to get the interface's ip address. From the man page, I know that:
The ifa_addr field references either the address of the interface or the link level address of the interface, if one exists, otherwise it is NULL. (The sa_family field of the ifa_addr field should be consulted to deter- mine the format of the ifa_addr address.)
My question is: how could ifa_addr be NULL? In my opinion, every interface has its address, isn't it?
I have googled a night, every page I found just says that "This field may contain a NULL pointer", no more explaination...
I really don't know how to search to get the detailed info, I have tried many key words, like "getifaddrs ifa_addr NULL"... but google just show me the getifaddrs() function's man page...
but, I do find only one page, which gives me a little more information. Just a little!
http://sourceforge.net/p/bonding/discussion/77913/thread/03f93486/
So, can anyone can show me some more detailed information on this topic?
Upvotes: 2
Views: 1446
Reputation: 229184
My question is: how could ifa_addr be NULL? In my opinion, every interface has its address, isn't it?
That assumption is wrong, an interface does not need to have an IP address (or other kind of address returned by getifaddrs() ) configured.
Upvotes: 0
Reputation: 81
As noted, the ifa_addr field may be NULL for an interface that doesn't have any address assigned to it. A simple way to see this in practice is to add a tunneling interface:
$ sudo ip tuntap add tun42 mode tun
This will create a tunneling interface without any address:
$ ifconfig -a
tun42: flags=4240<POINTOPOINT,NOARP,MULTICAST> mtu 1500
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 500 (UNSPEC)
$ ip link show
5: tun42: <POINTOPOINT,MULTICAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 500
link/none
Upvotes: 3