Skip to content

Commit 96100f8

Browse files
committed
ffi/netinfo: new module to retrieve network information
Avoid using unsupported ioctls on macOS, plus some additional tweak to support more configurations: - uses `getifaddrs()` results to retrieve the MAC address (from `AF_LINK` on macOS, `AF_PACKET` on Linux), instead of the Linux specific `SIOCGIFHWADDR` ioctl - for checking the interface is wireless on Linux: do a filesystem check for `/sys/class/net/$IFACE/wireless/`, instead of using the `SIOCGIWNAME` ioctl, which is not always supported (it depends on the driver: no dice on my Kindle PW2, or with the `mac80211_hwsim` module to simulate wireless interfaces) - on macOS: checking if the interface is wireless is still unsupported, as is retrieving the SSID (which is not possible anyway on more recent macOS versions without location permission)
1 parent e30319c commit 96100f8

11 files changed

Lines changed: 338 additions & 16 deletions

ffi-cdecl/posix_cdecl.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@
4343
#include <net/if.h>
4444
#if defined(__APPLE__)
4545
# include <net/if_dl.h>
46+
# include <net/if_types.h>
4647
#endif
4748
#include <net/route.h>
4849
#include <netdb.h>
4950
#include <netinet/ip.h>
5051
#include <netinet/ip_icmp.h>
52+
#if defined(__linux__)
53+
# include <netpacket/packet.h>
54+
#endif
5155
#include <poll.h>
5256
#include <pthread.h>
5357
#if defined(__linux__)
@@ -285,11 +289,18 @@ cdecl_type(__fsfilcnt_t);
285289

286290
cdecl_struct(statvfs);
287291

292+
#if defined(__APPLE__)
293+
cdecl_const(IFT_ETHER);
294+
#endif
295+
288296
cdecl_const(AF_INET);
289297
cdecl_const(AF_INET6);
290298
#if defined(__APPLE__)
291299
cdecl_const(AF_LINK);
292300
#endif
301+
#if defined(__linux__)
302+
cdecl_const(AF_PACKET);
303+
#endif
293304
cdecl_const(AF_UNIX);
294305

295306
cdecl_const(NI_MAXHOST);
@@ -339,6 +350,9 @@ cdecl_struct(sockaddr);
339350
#if defined(__APPLE__)
340351
cdecl_struct(sockaddr_dl);
341352
#endif
353+
#if defined(__linux__)
354+
cdecl_struct(sockaddr_ll);
355+
#endif
342356
cdecl_struct(sockaddr_in);
343357
cdecl_struct(sockaddr_in6);
344358
cdecl_struct(sockaddr_un);
@@ -357,9 +371,7 @@ cdecl_struct(ifreq);
357371

358372
#if defined(__linux__) && !defined(__ANDROID__)
359373

360-
cdecl_const(SIOCGIFHWADDR);
361374
cdecl_const(SIOCGIWESSID);
362-
cdecl_const(SIOCGIWNAME);
363375

364376
cdecl_const(IW_ENCODE_INDEX);
365377
cdecl_const(IW_ESSID_MAX_SIZE);

ffi-cdecl/posix_h_android_arm.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ struct statvfs {
137137
static const unsigned AF_INET = 2;
138138
// cdecl_const_AF_INET6
139139
static const unsigned AF_INET6 = 10;
140+
// cdecl_const_AF_PACKET
141+
static const unsigned AF_PACKET = 17;
140142
// cdecl_const_AF_UNIX
141143
static const unsigned AF_UNIX = 1;
142144
// cdecl_const_NI_MAXHOST
@@ -178,6 +180,16 @@ struct sockaddr {
178180
sa_family_t sa_family;
179181
char sa_data[14];
180182
};
183+
// cdecl_struct_sockaddr_ll
184+
struct sockaddr_ll {
185+
unsigned short sll_family;
186+
uint16_t sll_protocol;
187+
int sll_ifindex;
188+
unsigned short sll_hatype;
189+
unsigned char sll_pkttype;
190+
unsigned char sll_halen;
191+
unsigned char sll_addr[8];
192+
};
181193
// cdecl_struct_sockaddr_in
182194
struct sockaddr_in {
183195
sa_family_t sin_family;

ffi-cdecl/posix_h_android_arm64.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ struct statvfs {
138138
static const unsigned AF_INET = 2;
139139
// cdecl_const_AF_INET6
140140
static const unsigned AF_INET6 = 10;
141+
// cdecl_const_AF_PACKET
142+
static const unsigned AF_PACKET = 17;
141143
// cdecl_const_AF_UNIX
142144
static const unsigned AF_UNIX = 1;
143145
// cdecl_const_NI_MAXHOST
@@ -179,6 +181,16 @@ struct sockaddr {
179181
sa_family_t sa_family;
180182
char sa_data[14];
181183
};
184+
// cdecl_struct_sockaddr_ll
185+
struct sockaddr_ll {
186+
unsigned short sll_family;
187+
uint16_t sll_protocol;
188+
int sll_ifindex;
189+
unsigned short sll_hatype;
190+
unsigned char sll_pkttype;
191+
unsigned char sll_halen;
192+
unsigned char sll_addr[8];
193+
};
182194
// cdecl_struct_sockaddr_in
183195
struct sockaddr_in {
184196
sa_family_t sin_family;

ffi-cdecl/posix_h_android_x64.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ struct statvfs {
138138
static const unsigned AF_INET = 2;
139139
// cdecl_const_AF_INET6
140140
static const unsigned AF_INET6 = 10;
141+
// cdecl_const_AF_PACKET
142+
static const unsigned AF_PACKET = 17;
141143
// cdecl_const_AF_UNIX
142144
static const unsigned AF_UNIX = 1;
143145
// cdecl_const_NI_MAXHOST
@@ -179,6 +181,16 @@ struct sockaddr {
179181
sa_family_t sa_family;
180182
char sa_data[14];
181183
};
184+
// cdecl_struct_sockaddr_ll
185+
struct sockaddr_ll {
186+
unsigned short sll_family;
187+
uint16_t sll_protocol;
188+
int sll_ifindex;
189+
unsigned short sll_hatype;
190+
unsigned char sll_pkttype;
191+
unsigned char sll_halen;
192+
unsigned char sll_addr[8];
193+
};
182194
// cdecl_struct_sockaddr_in
183195
struct sockaddr_in {
184196
sa_family_t sin_family;

ffi-cdecl/posix_h_android_x86.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ struct statvfs {
137137
static const unsigned AF_INET = 2;
138138
// cdecl_const_AF_INET6
139139
static const unsigned AF_INET6 = 10;
140+
// cdecl_const_AF_PACKET
141+
static const unsigned AF_PACKET = 17;
140142
// cdecl_const_AF_UNIX
141143
static const unsigned AF_UNIX = 1;
142144
// cdecl_const_NI_MAXHOST
@@ -178,6 +180,16 @@ struct sockaddr {
178180
sa_family_t sa_family;
179181
char sa_data[14];
180182
};
183+
// cdecl_struct_sockaddr_ll
184+
struct sockaddr_ll {
185+
unsigned short sll_family;
186+
uint16_t sll_protocol;
187+
int sll_ifindex;
188+
unsigned short sll_hatype;
189+
unsigned char sll_pkttype;
190+
unsigned char sll_halen;
191+
unsigned char sll_addr[8];
192+
};
181193
// cdecl_struct_sockaddr_in
182194
struct sockaddr_in {
183195
sa_family_t sin_family;

ffi-cdecl/posix_h_linux_arm.lua

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ struct statvfs {
143143
static const unsigned AF_INET = 2;
144144
// cdecl_const_AF_INET6
145145
static const unsigned AF_INET6 = 10;
146+
// cdecl_const_AF_PACKET
147+
static const unsigned AF_PACKET = 17;
146148
// cdecl_const_AF_UNIX
147149
static const unsigned AF_UNIX = 1;
148150
// cdecl_const_NI_MAXHOST
@@ -186,6 +188,16 @@ struct sockaddr {
186188
sa_family_t sa_family;
187189
char sa_data[14];
188190
};
191+
// cdecl_struct_sockaddr_ll
192+
struct sockaddr_ll {
193+
unsigned short sll_family;
194+
unsigned short sll_protocol;
195+
int sll_ifindex;
196+
unsigned short sll_hatype;
197+
unsigned char sll_pkttype;
198+
unsigned char sll_halen;
199+
unsigned char sll_addr[8];
200+
};
189201
// cdecl_struct_sockaddr_in
190202
struct sockaddr_in {
191203
sa_family_t sin_family;
@@ -254,12 +266,8 @@ struct ifreq {
254266
caddr_t ifru_data;
255267
} ifr_ifru;
256268
};
257-
// cdecl_const_SIOCGIFHWADDR
258-
static const unsigned SIOCGIFHWADDR = 35111;
259269
// cdecl_const_SIOCGIWESSID
260270
static const unsigned SIOCGIWESSID = 35611;
261-
// cdecl_const_SIOCGIWNAME
262-
static const unsigned SIOCGIWNAME = 35585;
263271
// cdecl_const_IW_ENCODE_INDEX
264272
static const unsigned IW_ENCODE_INDEX = 255;
265273
// cdecl_const_IW_ESSID_MAX_SIZE

ffi-cdecl/posix_h_linux_arm64.lua

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ struct statvfs {
142142
static const unsigned AF_INET = 2;
143143
// cdecl_const_AF_INET6
144144
static const unsigned AF_INET6 = 10;
145+
// cdecl_const_AF_PACKET
146+
static const unsigned AF_PACKET = 17;
145147
// cdecl_const_AF_UNIX
146148
static const unsigned AF_UNIX = 1;
147149
// cdecl_const_NI_MAXHOST
@@ -185,6 +187,16 @@ struct sockaddr {
185187
sa_family_t sa_family;
186188
char sa_data[14];
187189
};
190+
// cdecl_struct_sockaddr_ll
191+
struct sockaddr_ll {
192+
unsigned short sll_family;
193+
unsigned short sll_protocol;
194+
int sll_ifindex;
195+
unsigned short sll_hatype;
196+
unsigned char sll_pkttype;
197+
unsigned char sll_halen;
198+
unsigned char sll_addr[8];
199+
};
188200
// cdecl_struct_sockaddr_in
189201
struct sockaddr_in {
190202
sa_family_t sin_family;
@@ -253,12 +265,8 @@ struct ifreq {
253265
caddr_t ifru_data;
254266
} ifr_ifru;
255267
};
256-
// cdecl_const_SIOCGIFHWADDR
257-
static const unsigned SIOCGIFHWADDR = 35111;
258268
// cdecl_const_SIOCGIWESSID
259269
static const unsigned SIOCGIWESSID = 35611;
260-
// cdecl_const_SIOCGIWNAME
261-
static const unsigned SIOCGIWNAME = 35585;
262270
// cdecl_const_IW_ENCODE_INDEX
263271
static const unsigned IW_ENCODE_INDEX = 255;
264272
// cdecl_const_IW_ESSID_MAX_SIZE

ffi-cdecl/posix_h_linux_x64.lua

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ struct statvfs {
143143
static const unsigned AF_INET = 2;
144144
// cdecl_const_AF_INET6
145145
static const unsigned AF_INET6 = 10;
146+
// cdecl_const_AF_PACKET
147+
static const unsigned AF_PACKET = 17;
146148
// cdecl_const_AF_UNIX
147149
static const unsigned AF_UNIX = 1;
148150
// cdecl_const_NI_MAXHOST
@@ -186,6 +188,16 @@ struct sockaddr {
186188
sa_family_t sa_family;
187189
char sa_data[14];
188190
};
191+
// cdecl_struct_sockaddr_ll
192+
struct sockaddr_ll {
193+
unsigned short sll_family;
194+
unsigned short sll_protocol;
195+
int sll_ifindex;
196+
unsigned short sll_hatype;
197+
unsigned char sll_pkttype;
198+
unsigned char sll_halen;
199+
unsigned char sll_addr[8];
200+
};
189201
// cdecl_struct_sockaddr_in
190202
struct sockaddr_in {
191203
sa_family_t sin_family;
@@ -254,12 +266,8 @@ struct ifreq {
254266
caddr_t ifru_data;
255267
} ifr_ifru;
256268
};
257-
// cdecl_const_SIOCGIFHWADDR
258-
static const unsigned SIOCGIFHWADDR = 35111;
259269
// cdecl_const_SIOCGIWESSID
260270
static const unsigned SIOCGIWESSID = 35611;
261-
// cdecl_const_SIOCGIWNAME
262-
static const unsigned SIOCGIWNAME = 35585;
263271
// cdecl_const_IW_ENCODE_INDEX
264272
static const unsigned IW_ENCODE_INDEX = 255;
265273
// cdecl_const_IW_ESSID_MAX_SIZE

ffi-cdecl/posix_h_macos.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ struct statvfs {
139139
unsigned long f_flag;
140140
unsigned long f_namemax;
141141
};
142+
// cdecl_const_IFT_ETHER
143+
static const unsigned IFT_ETHER = 6;
142144
// cdecl_const_AF_INET
143145
static const unsigned AF_INET = 2;
144146
// cdecl_const_AF_INET6

0 commit comments

Comments
 (0)