SIOCGICONG doesn't work excatly the same on *BSD. You should use getifaddrs() whenever possible, it is said to be cleaner and easier to use. getifaddrs is available on Darwin, *BSD and recent Linux.
/ David
Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum wrote:
How portable is SIOCGIFCONF? Solaris 9 seems to consider it obsolete, and provides SIOCGLIFCONF as a replacement. And for W*ndows something else completely is probably needed.
Otherwise it seems ok, but it should of course return IPV6 addresses as well. Which makes the proposed API a bit awkward, since each interface may have two addresses, one V4 and one V6.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2004-09-11 05:22: Subject: gethostip
Parsing ifconfig output is boring, so something like this could perhaps be added? I'm not adding it myself, since network/socket/ioctl-stuff isn't my best area.
static void f_gethostip(INT32 args) { int fd, i, j, up; struct ifconf ifc; struct sockaddr_in addr; char buffer[ INTERFACES * sizeof( struct ifreq ) ];
pop_n_elems(args);
fd = fd_socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); if( fd < 0 ) Pike_error("gethostip: Failed to open socket.\n");
ifc.ifc_len = sizeof( buffer ); ifc.ifc_ifcu.ifcu_buf = (caddr_t)buffer; if( ioctl( fd, SIOCGIFCONF, &ifc ) < 0 ) Pike_error("gethostip: Query failed.\n");
up = 0; for( i=0; i<ifc.ifc_len; ) { struct ifreq *ifr = (struct ifreq *)( (caddr_t)ifc.ifc_req+i ); struct ifreq ifr2; i += sizeof(struct ifreq);
strcpy( ifr2.ifr_name, ifr->ifr_name ); if( ioctl( fd, SIOCGIFFLAGS, &ifr2 )<0 ) Pike_error("gethostip: Query failed.\n");
if( (ifr2.ifr_flags & IFF_LOOPBACK) || !(ifr2.ifr_flags & IFF_UP) || (ifr->ifr_addr.sa_family != AF_INET ) ) continue;
push_text( ifr->ifr_name ); memcpy( &addr, &ifr->ifr_addr, sizeof(ifr->ifr_addr) ); push_text( inet_ntoa( addr.sin_addr ) );
up++; } fd_close(fd); f_aggregate_mapping(up*2); }
/ Martin Nilsson (DivX Networks)