libimobiledevice / libusbmuxd

A client library to multiplex connections from and to iOS devices
https://libimobiledevice.org
GNU Lesser General Public License v2.1
574 stars 270 forks source link

Inconsistency between libusbmuxd and libimobiledevice in network structure parsing #134

Open tihmstar opened 7 months ago

tihmstar commented 7 months ago

In libusbmuxd tools the parsing code found here: https://github.com/libimobiledevice/libusbmuxd/blob/master/tools/iproxy.c#L132-L145 and here: https://github.com/libimobiledevice/libusbmuxd/blob/master/tools/inetcat.c#L219-L231 Looks like this:

        if (dev->conn_data[1] == 0x02) { // AF_INET
            saddr->sa_family = AF_INET;
            memcpy(&saddr->sa_data[0], (uint8_t*)dev->conn_data+2, 14);
        }
        else if (dev->conn_data[1] == 0x1E) { //AF_INET6 (bsd)
#ifdef AF_INET6
            saddr->sa_family = AF_INET6;
            /* copy the address and the host dependent scope id */
            memcpy(&saddr->sa_data[0], (uint8_t*)dev->conn_data+2, 26);
#else
            fprintf(stderr, "ERROR: Got an IPv6 address but this system doesn't support IPv6\n");
            CDATA_FREE(cdata);
            return NULL;
#endif
        }

While at first i thought it's odd that you hardcode 0x1E for AF_INET6 (bsd), i realized it may be due to compatibility reasons on original Apple usbmuxd on windows (as on linux either would work just fine).

But in libimobiledevice the same code is parsed differently, which is problematic! Looking here: https://github.com/libimobiledevice/libimobiledevice/blob/master/src/idevice.c#L333-L345 we find:

            switch (saddr->sa_family) {
                case AF_INET:
                    addrlen = sizeof(struct sockaddr_in);
                    break;
#ifdef AF_INET6
                case AF_INET6:
                    addrlen = sizeof(struct sockaddr_in6);
                    break;
#endif
                default:
                    debug_info("Unsupported address family 0x%02x\n", saddr->sa_family);
                    continue;
            }

Note: even sockaddr is different between macOS and Linux.

struct sockaddr{ //macOS
   uint8_t sa_len;
   uint8_t sa_family;
   char sa_data[14];
};
struct sockaddr{ //linux
   uint16_t sa_family;
   char sa_data[14];
};

.... After looking through more code it looks like libimobiledevice style is more sane (libimobiledevice-glue also assumes this structure). So i recomment to change the libusmuxd code to match libimobiledevice and libimobiledevice-glue.

If 0x1E) { //AF_INET6 (bsd) is needed for compatibility reasons, i recommend to add a small compatibility layer inside libusbmuxd which converts that format into the system-standard format