implement wireless checking for bsd/osx

This commit is contained in:
walker0643 2018-07-05 19:31:34 -04:00
parent c9bd5c2aff
commit a8d0c1620d
1 changed files with 24 additions and 7 deletions

View File

@ -8,6 +8,7 @@
#include <linux/wireless.h> #include <linux/wireless.h>
#else #else
#include <net/if.h> #include <net/if.h>
#include <net/if_media.h>
#endif #endif
#include <string> #include <string>
@ -16,18 +17,34 @@
namespace Debauchee namespace Debauchee
{ {
static bool is_wireless(const char * ifname) static bool is_wireless(const char * ifname, const SocketResource& sock)
{ {
#ifdef __linux__ #ifdef __linux__
struct iwreq req;
::memset(&req, 0, sizeof(struct iwreq));
::strncpy(req.ifr_name, ifname, IFNAMSIZ - 1);
return ioctl(sock, SIOCGIWMODE, &req) >= 0;
#else
struct ifmediareq req;
::memset(&req, 0, sizeof(struct ifmediareq));
::strncpy(req.ifm_name, ifname, IFNAMSIZ - 1);
if (ioctl(sock, SIOCGIFMEDIA, &req) >= 0 &&
(req.ifm_status & IFM_AVALID)
) {
return IFM_TYPE(req.ifm_active) == IFM_IEEE80211;
}
return false;
#endif
}
static bool is_wireless(const char * ifname)
{
if (ifname) { if (ifname) {
SocketResource fd(AF_INET, SOCK_STREAM, 0); SocketResource sock(AF_INET, SOCK_DGRAM, 0);
if (fd.is_valid()) { if (sock.is_valid()) {
struct iwreq req { 0 }; return is_wireless(ifname, sock);
::strncpy(req.ifr_name, ifname, IFNAMSIZ - 1);
return ioctl(fd, SIOCGIWMODE, req) >= 0;
} }
} }
#endif
return false; return false;
} }