While back porting code to SLES8 U2, figured out that quite a few IOCTL flags have not been implemented. All that was working on Linux 2.6.x kernel and quite a few distros based on 2.4.20 > kernel but not on SLES8 U2. Most of my code was about finding number of interfaces using SIOCGIFCOUNT and then looping those many times to find more information about interfaces using flags SIOCGIFCONF and SIOCGIFFLAG.

Found that ioctl(s, SIOCGIFCOUNT, (char*)&numIfs) sets the errno to EINVAL.

That makes it harder to get more information about interfaces as for calling
ioctl(s, SIOCGIFCONF, &ifConf), ifConf.ifc_buf should be allocated adequate memory, which typically depends on number of interfaces e.g.

ifConf.ifc_len = numIfs*sizeof(struct ifreq);
ifConf.ifc_buf = (char *)malloc(ifConf.ifc_len);

The only work around I could think of was to assume a large number of interfaces and loop until the interface name is null. e.g.
for (ptrIfReq = ifConf.ifc_req; ptrIfReq->ifr_name[0] != '\0'; ptrIfReq++) {

inAddr = (struct sockaddr_in *)&ptrIfReq->ifr_addr;
if ( inAddr->sin_family == AF_INET ) {
if ( ioctl(s, SIOCGIFFLAGS, (char *)ptrIfReq) < 0 ) {
continue;
}
if (((ptrIfReq->ifr_flags & IFF_LOOPBACK ) != IFF_LOOPBACK) &&
((ptrIfReq->ifr_flags & IFF_UP ) == IFF_UP )) {
sprintf(outbuf, "%s %s", outbuf,
inet_ntoa(inAddr->sin_addr));
}
}
printf ("ifIndex is %d\n", ifIndex);
}

I am not sure how safe this logic is but it does work reasonably well and serves the purpose.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed

This blog copyright 2009 by harshit Kalley