Debian Codegraph @

src/network.c

raw symbols SCIP
1 #include "network.h"
2 #include "socket-shim.h"
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <memory.h>
7
8 #ifdef __EMSCRIPTEN__
9 /* WASM has no network interfaces — fill with zeros */
10 int _librist_network_get_macaddr(uint8_t mac[]) {
11 memset(mac, 0, 6);
12 return 0;
13 }
14 #else /* !__EMSCRIPTEN__ */
15
16 #ifndef _WIN32
17 #include <ifaddrs.h>
18 #ifdef __linux__
19 #include <netpacket/packet.h>
20 #elif defined(__GNU__)
21 #include <sys/ioctl.h>
22 #include <net/if.h>
23 #else
24 #include <net/if_dl.h>
25 #endif
26 #else
27 #include <winsock2.h>
28 #include <iphlpapi.h>
29 #endif
30
31 int _librist_network_get_macaddr(uint8_t mac[]) {
32 char mac_null[6] = {0};
33 #ifndef _WIN32
34 #if defined(__GNU__)
35 int sock = socket(PF_INET, SOCK_DGRAM, 0);
36 if (sock < 0)
37 return -1;
38 #endif
39 struct ifaddrs *ifaddr = NULL;
40 struct ifaddrs *ifa = NULL;
41 if (getifaddrs(&ifaddr) == -1) {
42 #if defined(__GNU__)
43 close(sock);
44 #endif
45 return -1;
46 } else {
47 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
48 #ifdef __linux__
49
50 if ((ifa->ifa_addr) && (ifa->ifa_addr->sa_family == AF_PACKET)) {
51 struct sockaddr_ll *s = (struct sockaddr_ll *)ifa->ifa_addr;
52 if (memcmp(mac_null, s->sll_addr, 6)) {
53 memcpy(mac, s->sll_addr, 6);
54 break;
55 }
56 }
57 #elif defined(__GNU__)
58 struct ifreq ifr;
59 strcpy(ifr.ifr_name, ifa->ifa_name);
60 if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
61 continue;
62 }
63 if (ifr.ifr_hwaddr.sa_family != 1)
64 continue;
65 memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
66 break;
67 #else
68 if ((ifa->ifa_addr) && (ifa->ifa_addr->sa_family == AF_LINK)) {
69 struct sockaddr_dl *s = (struct sockaddr_dl *)ifa->ifa_addr;
70 if (memcmp(mac_null, s->sdl_data + s->sdl_nlen, 6)) {
71 memcpy(mac, s->sdl_data + s->sdl_nlen, 6);
72 break;
73 }
74 }
75 #endif
76 }
77 freeifaddrs(ifaddr);
78 }
79 #if defined(__GNU__)
80 close(sock);
81 #endif
82 #else
83 /* GetAdaptersInfo is not part of the UWP (WINAPI_FAMILY_APP) API surface
84 * and llvm-mingw's UWP iphlpapi import library does not export it, causing
85 * link failures of librist-dependent UWP apps. GetAdaptersAddresses is
86 * supported on both classic Win32 desktop and on UWP since Windows 8, so
87 * use it unconditionally. */
88 ULONG buf_size = 15 * 1024; /* MSDN-recommended initial size */
89 PIP_ADAPTER_ADDRESSES adapters = NULL;
90 ULONG ret = ERROR_BUFFER_OVERFLOW;
91 for (int attempt = 0; attempt < 3 && ret == ERROR_BUFFER_OVERFLOW; attempt++) {
92 free(adapters);
93 adapters = (PIP_ADAPTER_ADDRESSES)malloc(buf_size);
94 if (!adapters)
95 return -1;
96 ret = GetAdaptersAddresses(AF_UNSPEC,
97 GAA_FLAG_SKIP_UNICAST | GAA_FLAG_SKIP_ANYCAST |
98 GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER,
99 NULL, adapters, &buf_size);
100 }
101 if (ret != ERROR_SUCCESS) {
102 free(adapters);
103 return -1;
104 }
105 for (PIP_ADAPTER_ADDRESSES adapter = adapters; adapter != NULL;
106 adapter = adapter->Next) {
107 if (adapter->PhysicalAddressLength >= 6 &&
108 memcmp(mac_null, adapter->PhysicalAddress, 6) != 0) {
109 memcpy(mac, adapter->PhysicalAddress, 6);
110 break;
111 }
112 }
113 free(adapters);
114 #endif
115 return 0;
116 }
117 #endif /* !__EMSCRIPTEN__ */