Game Development Reference
In-Depth Information
2.
The getifaddrs() function creates a linked list of structures that describe network
interfaces of the local system:
if ( getifaddrs( &MyAddrs ) != 0 ) { return false; }
3.
Iterate through the linked list:
for ( ifa = MyAddrs; ifa != NULL; ifa = ifa->ifa_next )
{
if ( ( ifa->ifa_addr == NULL ) ||
!( ifa->ifa_flags & IFF_UP ) ) { continue; }
4.
Treat IPv4 and IPv6 addressed differently:
switch ( ifa->ifa_addr->sa_family )
{
case AF_INET:
{ in_addr = &( ( struct sockaddr_in* )
ifa->ifa_addr )->sin_addr; break; }
case AF_INET6:
{ in_addr = &( ( struct sockaddr_in6* )
ifa->ifa_addr )->sin6_addr; break; }
default:
continue;
}
5.
Convert the network address structure into a C-string and save it in the Adapters
vector:
if ( inet_ntop( ifa->ifa_addr->sa_family,
in_addr, buf, sizeof( buf ) ) )
{
sAdapterInfo Info;
strcpy( Info.FName, ifa->ifa_name );
strcpy( Info.FIP, buf );
sprintf( Info.FID, "%d", Idx );
Adapters.push_back( Info );
Idx++;
}
}
6.
Release the linked list:
freeifaddrs( MyAddrs );
 
Search WWH ::




Custom Search