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); }