summaryrefslogtreecommitdiffstats
path: root/src/libusbmuxd.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2026-11-18 00:32:05 +0100
committerGravatar Nikias Bassen2026-11-18 00:32:05 +0100
commitcf4b806dff1003e56b8219367bbb29cb01c3f619 (patch)
tree2d742615573966989ad2a5e4e4fca9c93204695f /src/libusbmuxd.c
parent68779a6f0571a3486e329d2e55949b0eb0720af8 (diff)
downloadlibusbmuxd-cf4b806dff1003e56b8219367bbb29cb01c3f619.tar.gz
libusbmuxd-cf4b806dff1003e56b8219367bbb29cb01c3f619.tar.bz2
Convert network device address in conn_data to native sockaddr layout
usbmuxd reports the network address of a device as a BSD sockaddr ([sa_len][sa_family]...), even when it runs on a different OS than the client (e.g. macOS usbmuxd reached over TCP from Linux). libusbmuxd passed it through unchanged, so consumers casting conn_data to struct sockaddr (like libimobiledevice) got the wrong family on Linux and Windows. Convert the address when parsing it, so conn_data holds a native sockaddr for the platform the library runs on. On macOS nothing changes, on the other BSDs only the family value is adjusted. iproxy and inetcat now use conn_data directly and require this version of the library. The format is documented in usbmuxd.h.
Diffstat (limited to 'src/libusbmuxd.c')
-rw-r--r--src/libusbmuxd.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/libusbmuxd.c b/src/libusbmuxd.c
index d558b87..b391723 100644
--- a/src/libusbmuxd.c
+++ b/src/libusbmuxd.c
@@ -245,6 +245,43 @@ static void sanitize_udid(usbmuxd_device_info_t *devinfo)
}
}
+/**
+ * usbmuxd (Apple's, also when reached remotely) reports network addresses as
+ * BSD sockaddr: [sa_len][sa_family][...]. Convert it in place to the native
+ * layout so that conn_data can be used as a struct sockaddr on every platform.
+ */
+static void convert_network_addr(uint8_t *conn_data)
+{
+#ifdef __APPLE__
+ /* same layout and family values as on the wire */
+ (void)conn_data;
+#else
+ int family;
+
+ switch (conn_data[1]) {
+ case 0x02: /* AF_INET (macOS) */
+ family = AF_INET;
+ break;
+#ifdef AF_INET6
+ case 0x1E: /* AF_INET6 (macOS) */
+ family = AF_INET6;
+ break;
+#endif
+ default:
+ memset(conn_data, 0, 2);
+ return;
+ }
+#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
+ /* BSD layout, only the family value differs */
+ conn_data[1] = (uint8_t)family;
+#else
+ /* 16-bit sa_family; everything after the 2-byte header has the same layout */
+ uint16_t fam16 = (uint16_t)family;
+ memcpy(conn_data, &fam16, sizeof(fam16));
+#endif
+#endif
+}
+
static usbmuxd_device_info_t *device_info_from_plist(plist_t props)
{
usbmuxd_device_info_t* devinfo = NULL;
@@ -318,6 +355,10 @@ static usbmuxd_device_info_t *device_info_from_plist(plist_t props)
devinfo = NULL;
}
+ if (devinfo && devinfo->conn_type == CONNECTION_TYPE_NETWORK) {
+ convert_network_addr(devinfo->conn_data);
+ }
+
return devinfo;
}