Is there a way to detect a USB Ethernet Adapter even if no IP has been given, Once it connect to an iPhone/iPad

I want to detect if the adapter is connected to the iPhone even if no IP has been given to the iPhone. I can detect that the interface is connected when the iPhone has been given an IP address, but how can I detect the adapter when not?

Answered by DTS Engineer in 867975022
even if no IP has been given to the iPhone.

Given to the iPhone by what? A DHCP server?

If that’s what you’re concerned about then I think you’re missing a key point. DHCP is primarily relevant to IPv4 [1]. Assuming the link is up, the interface will have an IPv6 link-local address [1].

I recommend that you read through the posts referenced by Extra-ordinary Networking. They explain a lot of the backstory to this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Yeah, there’s DHCPv6, but it’s not the primary mechanism for IPv6 address assignment.

[2] It’s likely also get an IPv4 link-local address, per RFC 3927, but that’s not nearly as straightforward.

even if no IP has been given to the iPhone.

Given to the iPhone by what? A DHCP server?

If that’s what you’re concerned about then I think you’re missing a key point. DHCP is primarily relevant to IPv4 [1]. Assuming the link is up, the interface will have an IPv6 link-local address [1].

I recommend that you read through the posts referenced by Extra-ordinary Networking. They explain a lot of the backstory to this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Yeah, there’s DHCPv6, but it’s not the primary mechanism for IPv6 address assignment.

[2] It’s likely also get an IPv4 link-local address, per RFC 3927, but that’s not nearly as straightforward.

It’s better to reply as a reply, rather than in the comments; see Quinn’s Top Ten DevForums Tips for this and other titbits.

I notice that when a wired network adapter is connected, an additional network interface named enX appears

Yep.

there are other network interfaces also named enX affect my judgment.

Right. This boils down to how you distinguish between different interfaces types. I have info about that in Extra-ordinary Networking > Network Interface APIs > Network Interface Type.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you for your reply. I tried blow method to get interface type.

extern uint32_t functionalTypeForInterfaceNamed(const char * name) {
    int fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd < 0) { return IFRTYPE_FUNCTIONAL_UNKNOWN; }

    struct ifreq ifr = {};
    strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));

    bool success = ioctl(fd, SIOCGIFFUNCTIONALTYPE, &ifr) >= 0;

    int junk = close(fd);
    assert(junk == 0);

    if (!success) {
        return IFRTYPE_FUNCTIONAL_UNKNOWN;
    }

    return ifr.ifr_ifru.ifru_functional_type;
}

There are three default enX interfaces, the en0 is IFRTYPE_FUNCTIONAL_WIFI_INFRA, the en1,en2 are IFRTYPE_FUNCTIONAL_WIRED, but I haven't inserted any ethernet adapter on my iPad yet. When I insert an ethernet adapter, an additional network interface which is also IFRTYPE_FUNCTIONAL_WIRED appear. It definitely affect my judgement. How to distinguish between those default enX which are IFRTYPE_FUNCTIONAL_WIRED type and the real ethernet adapter which insert on my iPad?

How to distinguish between those … and the real ethernet adapter which insert on my iPad?

Unfortunately I can’t see a good way to do that on iOS [1].

I think you could make a reasonable case that those additional interfaces should not be returning IFRTYPE_FUNCTIONAL_WIRED, but instead something more indicative of their purpose (like IFRTYPE_FUNCTIONAL_INTCOPROC). If you’d like to see that change, I encourage you to file a bug along those lines. And if you do file a bug, please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] On macOS you have a lot more options, including both System Configuration framework and I/O Kit. Neither of those are API on iOS or its child platforms.

I've noticed that when I plug a USB Ethernet adapter into my iPad, an “Ethernet” option appears in the Settings menu. How does the system detect that the adapter has been connected? Hope to receive your reply again.

How does the system detect that the adapter has been connected?

Settings is a built-in component of iOS and thus not limited to using just APIs.

Earlier I wrote:

On macOS you have a lot more options, including both System Configuration framework and I/O Kit.

If you were trying to solve this problem on macOS, it’d be easy to do with those APIs. However, those APIs are not available on iOS [1]. Moreover, I’m skeptical we’ll ever make them available on iOS [2], which is why my suggestion above wasn’t to file an ER for those APIs but rather a bug report against the functional type values.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] The frameworks might be available but the specific APIs you need not.

[2] Because of the privacy impact.

Is there a way to detect a USB Ethernet Adapter even if no IP has been given, Once it connect to an iPhone/iPad
 
 
Q