diff options
| author | 2021-06-07 03:41:55 +0200 | |
|---|---|---|
| committer | 2021-06-07 03:41:55 +0200 | |
| commit | 0dcfb68954f7ee9957f528f0eea1f9dba9c9cb68 (patch) | |
| tree | 2bc6e47debce816265ebdbb86a6f38feaf66dcfb /src | |
| parent | 046b26150e004a8ac740e699c6c3e11be29e8f11 (diff) | |
| download | libimobiledevice-glue-0dcfb68954f7ee9957f528f0eea1f9dba9c9cb68.tar.gz libimobiledevice-glue-0dcfb68954f7ee9957f528f0eea1f9dba9c9cb68.tar.bz2  | |
socket: Add get_primary_mac_address()
Diffstat (limited to 'src')
| -rw-r--r-- | src/socket.c | 62 | 
1 files changed, 62 insertions, 0 deletions
diff --git a/src/socket.c b/src/socket.c index 067ef41..2ffab4a 100644 --- a/src/socket.c +++ b/src/socket.c @@ -55,6 +55,12 @@ static int wsa_init = 0;  #ifdef AF_INET6  #include <net/if.h>  #include <ifaddrs.h> +#ifdef __APPLE__ +#include <net/if_dl.h> +#endif +#ifdef __linux__ +#include <netpacket/packet.h> +#endif  #endif  #endif  #include "common.h" @@ -417,6 +423,7 @@ static void freeifaddrs(struct ifaddrs *ifa)  	free(ifa->ifa_addr);  	free(ifa->ifa_netmask);  	free(ifa->ifa_dstaddr); +	free(ifa->ifa_data);  	freeifaddrs(ifa->ifa_next);  	free(ifa);  } @@ -501,6 +508,7 @@ static int getifaddrs(struct ifaddrs** ifap)  				ifa = ifanew;  				ifa->ifa_next = NULL;  			} +			ifa->ifa_data = NULL;  			/* name */  			ifa->ifa_name = strdup(adapter->AdapterName); @@ -522,6 +530,12 @@ static int getifaddrs(struct ifaddrs** ifap)  			ifa->ifa_netmask = (struct sockaddr*)malloc(sizeof(struct sockaddr_storage));  			memset(ifa->ifa_netmask, 0, sizeof(struct sockaddr_storage)); +			/* store mac address */ +			if (adapter->PhysicalAddressLength == 6) { +				ifa->ifa_data = malloc(6); +				memcpy(ifa->ifa_data, adapter->PhysicalAddress, 6); +			} +  /* pre-Vista must hunt for matching prefix in linked list, otherwise use   * OnLinkPrefixLength from IP_ADAPTER_UNICAST_ADDRESS structure.   * FirstPrefix requires Windows XP SP1, from SP1 to pre-Vista provides a @@ -685,6 +699,54 @@ static int getifaddrs(struct ifaddrs** ifap)  #endif  #endif +LIBIMOBILEDEVICE_GLUE_API int get_primary_mac_address(unsigned char mac_addr_buf[6]) +{ +	int result = -1; +	struct ifaddrs *ifaddr = NULL, *ifa = NULL; +	if (getifaddrs(&ifaddr) != -1) { +		for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { +			if (ifa->ifa_addr == NULL) { +				continue; +			} +			if ((ifa->ifa_flags & IFF_UP) == 0) { +				continue; +			} +			if (ifa->ifa_flags & IFF_LOOPBACK) { +				continue; +			} +#if defined(__APPLE__) +			if (ifa->ifa_addr->sa_family != AF_LINK) { +				continue; +			} +			if (!strcmp(ifa->ifa_name, "en0")) { +				memcpy(mac_addr_buf, (unsigned char *)LLADDR((struct sockaddr_dl *)(ifa)->ifa_addr), 6); +				result = 0; +				break; +			} +#elif defined (__linux__) +			if (ifa->ifa_addr->sa_family != AF_PACKET) { +				continue; +			} +			if (strcmp(ifa->ifa_name, "lo") != 0) { +				memcpy(mac_addr_buf, ((struct sockaddr_ll*)ifa->ifa_addr)->sll_addr, 6); +				result = 0; +				break; +			} +#elif defined (WIN32) +			if (ifa->ifa_data) { +				memcpy(mac_addr_buf, ifa->ifa_data, 6); +				result = 0; +				break; +			} +#else +#error get_primary_mac_address is not supported on this platform. +#endif +		} +		freeifaddrs(ifaddr); +	} +	return result; +} +  static int32_t _sockaddr_in6_scope_id(struct sockaddr_in6* addr)  {  	int32_t res = -1;  | 
