I'll be concise: I am writing some user-space code that needs to
obtain the IPv4 protocol address and hardware address from an interface
(arbitrary). I am using the ioctl() method with SIOCGIFHWADDR and
SIOCGIFADDR, operating on struct ifreq (contains struct sockaddr). The
hardware address part works like I expect. But the IP address doesn't;
it's stored in sa_data, offset two bytes. I don't understand why. Here is
a code pseudo-fragment:
Obtaining hardware address (works as I expected):
int s = socket( AF_INET, SOCK_DGRAM, 0 );
struct ifreq req;
strcpy( req.ifr_name, "some-device" );
ioctl( s, SIOCGIFHWADDR, &req );
close( s );
memcpy( "place-to-store-hwaddr", req.ifr_hwaddr.sa_data, 6 );
/* the "6" is for an ethernet address */
Obtaining the protocol address (it is offset?):
int s = socket( AF_INET, SOCK_DGRAM, 0 );
struct ifreq req;
strcpy( req.ifr_name, "same-device" );
ioctl( s, SIOCGIFADDR, &req );
close( s );
32-bit-holder = *((u_int32_t*)(req.ifr_addr.sa_data+2));
Note the "+2". I found this by trial and error. If anyone can explain
this to me (or if I'm doing something wrong) I would be very greatful.
thanks,
-bp
-- B. James Phillippe . bryan@terran.org UNIX Software Engineer . http://www.terran.org/~bryan Member since 1.1.59 . finger:bryan@earth.terran.org MOTM: Waiting for the DSL to go in :)
- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/