Created
March 26, 2025 16:39
-
-
Save herrernst/e5b3db455487eb2cc5b459acc7ad4464 to your computer and use it in GitHub Desktop.
Check if a macOS network interface is a "direct link" interface (what is used for iOS device debugging over USB)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <net/if.h> | |
#include <sys/ioctl.h> | |
#include <unistd.h> | |
#include <sys/errno.h> | |
// adopted from https://developer.apple.com/documentation/technotes/tn3158-resolving-xcode-15-device-connection-issues#Update-software-using-packet-filtering-rules | |
/** | |
Tests whether an interface is a direct-link interface. | |
- Parameter name: The BSD interface name, for example, `en0`. | |
- Returns 1 if it is a direct-link interface, 0 if it’s not, and -1 if an error occurred. | |
In the last case, see `errno` for the error code. | |
*/ | |
int isDirectLinkInterface(const char * name) { | |
int fd = socket(PF_INET, SOCK_DGRAM, 0); | |
if (fd < 0) { | |
return -1; | |
} | |
struct ifreq ifr = { 0 }; | |
strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); | |
// also displayed as DIRECTLINK under eflags with `ifconfig -v` | |
int success = ioctl(fd, SIOCGIFDIRECTLINK, &ifr) >= 0; | |
if (!success) { | |
int error = errno; | |
(void) close(fd); | |
errno = error; | |
return -1; | |
} | |
(void) close(fd); | |
return ifr.ifr_ifru.ifru_is_directlink != 0; | |
} | |
int main(int argc, const char * argv[]) { | |
if (argc <= 1) { | |
printf("Usage: %s ifname\n", argv[0]); | |
return EXIT_FAILURE; | |
} | |
int isDirectLinkInterfaceVal = isDirectLinkInterface(argv[1]); | |
if (isDirectLinkInterfaceVal >= 0) { | |
printf("isDirectLinkInterface: %d\n", isDirectLinkInterfaceVal); | |
return EXIT_SUCCESS; | |
} else { | |
printf("isDirectLinkInterface error, errno: %d\n", errno); | |
return EXIT_FAILURE; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment