[gnet-dev] ifconfig-like interface descriptions?
- From: Christian Taedcke devolo de
- To: gnet-dev lists gnetlibrary org
- Subject: [gnet-dev] ifconfig-like interface descriptions?
- Date: Thu, 2 Sep 2004 09:35:13 +0200
Hello
i'm porting my streamingserver from normal unix sockets to gnet to use it
also in win32. gnet is a great library, but i miss one functionality: I
need the subnetmasks of the network interfaces from
gnet_inetaddr_list_interfaces().
My old code (only linux) is at the bottom of this mail.
Thanks
/**
* @Info saves all network interfaces
* @Result
* @Comment
*/
void CharpHandler::GetInterfaces() {
struct Interface interface;
int sock = 0;
struct ifreq ifreq;
struct sockaddr_in *saptr = NULL;
struct if_nameindex *iflist = NULL, *listsave = NULL;
//need a socket for ioctl()
if( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("socket");
return ;
}
//returns pointer to dynamically allocated list of structs
iflist = listsave = if_nameindex();
//walk through the array returned and query for each
//interface's address
for(iflist; *(char *)iflist != 0; iflist++){
//copy interfacename
strncpy(interface.name,iflist->if_name, IF_NAMESIZE);
strncpy(ifreq.ifr_name,iflist->if_name, IF_NAMESIZE);
//get the address for this interface
if(ioctl(sock, SIOCGIFADDR, &ifreq) != 0){
perror("ioctl 1");
return;
}
saptr = (struct sockaddr_in *)&ifreq.ifr_addr;
//set ipaddress
interface.ipaddress = saptr->sin_addr.s_addr;
//get netmask
if(ioctl(sock, SIOCGIFNETMASK, &ifreq) != 0){
perror("ioctl 2");
return;
}
saptr = (struct sockaddr_in *)&ifreq.ifr_addr;
//set netmask
interface.netmask = saptr->sin_addr.s_addr;
//save the interface
interfacelist.push_back(interface);
}
//free the dynamic memory kernel allocated for us
if_freenameindex(listsave);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]