Re: SIOCGIFADDR bug?

really kuznet@ms2.inr.ac.ru (inr-linux-kernel@ms2.inr.ac.ru)
21 Jun 1996 22:27:11 +0400


Zachary Roger Amsden (amsden+@andrew.cmu.EDU) wrote:
: Excerpts from internet.computing.linux-kernel: 18-Jun-96 Re: SIOCGIFADDR
: bug? by kuznet@ms2.inr.ac.ru@ms2
: >
: > Did you specify disired address family?
: > You program will get hardware ethernet address.
: > If you want internet address, write ifr.ifr_addr.sa_family = AF_INET.
: >
: >
: I meant internet address. I should get the internet address because I
: opened the connection to the net kernel with AF_INET.

Alas, it is not case. :-)

: I found out a
: very strange thing about this bug?. If I call SIOCGIFADDR twice, I get
: the correct address. I'll have to double check the address I was
: getting the first time and see if it matches the ethernet (not internet)
: address. I could be wrong, but I heard that you don't even need to call
: SIOCGIFADDR twice, just call it after any other IOCTL call and it will
: work. Strange, huh?

Looks strange like almost any obscure bug, but the explanation is always simple
as tomatoes :-)

When you call SIOCGIFADDR with ifr.ifr_addr.sa_family == 0 (AF_UNSPEC),
it fills ifr with hardware ethernet address and set ifr.ifr_addr.sa_family
to ARPHRD_ETHER (=1). Any other SIOCGIF*ADDR fills it with correct value
(AF_INET). Apparently you forgot to clear ifr before calling SIOCGIFADDR
second time (another error 8)), so that you occasionally get correct answer.

Note that double SIOCGIFADDR "works" only because Linux does not
support (yet!) multiple address families per device. Be ready that
your powerful "technique" of getting addresses will stop to work
at any moment. :-)

Alexey Kuznetsov.