Get IP Address and Sunet Mask Of Each Network Interface



#include <stdio.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>

#define	MAX_NETWORK_ADAPTORS						3
#define	MAX_ADAPTOR_NAME_LENGTH						20

BYTE found_network_adaptors_count;
char NetworkAdaptorName[MAX_NETWORK_ADAPTORS][MAX_ADAPTOR_NAME_LENGTH];
char NetworkAdaptorIpAddress[MAX_NETWORK_ADAPTORS][INET_ADDRSTRLEN];
char NetworkAdaptorSubnetMask[MAX_NETWORK_ADAPTORS][INET_ADDRSTRLEN];


//***************************************************
//***************************************************
//********** READ NETWORK ADAPTOR SETTINGS **********
//***************************************************
//***************************************************
void network_read_network_adaptor_settings (void)
{
	struct ifaddrs * ifAddrStruct = NULL;
	struct ifaddrs * ifa = NULL;
	void * tmpAddrPtr = NULL;

	found_network_adaptors_count = 0;

	//Get each network interface on this machine
	getifaddrs(&ifAddrStruct);

	for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next)
	{
		//----- GET NEXT NETWORK INTERFACE ------
		if (ifa->ifa_addr->sa_family == AF_INET)
		{
			//----- IPV4 ADDRESS ------

			//GET IP ADDRESS
			tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
			char IpAddressBuffer[INET_ADDRSTRLEN];
			inet_ntop(AF_INET, tmpAddrPtr, IpAddressBuffer, INET_ADDRSTRLEN);
			printf("%s IPV4 Address %s\n", ifa->ifa_name, IpAddressBuffer);


			//GET SUBNET MASK
			tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
			char SnAddressBuffer[INET_ADDRSTRLEN];
			inet_ntop(AF_INET, tmpAddrPtr, SnAddressBuffer, INET_ADDRSTRLEN);
			printf("%s Subnet Mask %s\n", ifa->ifa_name, SnAddressBuffer);

			if (strncmp((const char*)&IpAddressBuffer[0], "127.0.0.1", 10) != 0)
			{
				//----- NOT THE LOOPBACK ADDRESS - ADD TO OUR SETTINGS BUFFER -----
				if (found_network_adaptors_count < MAX_NETWORK_ADAPTORS)
				{
					strncpy(&NetworkAdaptorName[found_network_adaptors_count][0], ifa->ifa_name, MAX_ADAPTOR_NAME_LENGTH);
					strncpy(&NetworkAdaptorIpAddress[found_network_adaptors_count][0], IpAddressBuffer, INET_ADDRSTRLEN);
					strncpy(&NetworkAdaptorSubnetMask[found_network_adaptors_count][0], SnAddressBuffer, INET_ADDRSTRLEN);

					found_network_adaptors_count++;
				}
			}

		}
		else if (ifa->ifa_addr->sa_family == AF_INET6)
		{
			//----- IPV6 ADDRESS -----

			//GET IP ADDRESS
			tmpAddrPtr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
			char addressBuffer[INET6_ADDRSTRLEN];
			inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);

			printf("%s IPV6 Address %s\n", ifa->ifa_name, addressBuffer);
		}

	} //for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next)

	//Reclaim the storage used by getifaddrs
	if (ifAddrStruct != NULL)
		freeifaddrs(ifAddrStruct);

	return;
}

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published.