summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/usbmuxd.h3
-rw-r--r--src/libusbmuxd.c41
-rw-r--r--tools/inetcat.c25
-rw-r--r--tools/iproxy.c21
4 files changed, 58 insertions, 32 deletions
diff --git a/include/usbmuxd.h b/include/usbmuxd.h
index 859fccd..8403c69 100644
--- a/include/usbmuxd.h
+++ b/include/usbmuxd.h
@@ -56,6 +56,9 @@ enum usbmux_connection_type {
* The relevant 'handle' should be passed to 'usbmuxd_connect()', to
* start a proxy connection. The value 'handle' should be considered
* opaque and no presumption made about the meaning of its value.
+ * The conn_data member, for CONNECTION_TYPE_NETWORK devices, holds the
+ * device address as a native struct sockaddr (sockaddr_in or sockaddr_in6)
+ * of the platform the library runs on.
*/
typedef struct {
uint32_t handle;
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;
}
diff --git a/tools/inetcat.c b/tools/inetcat.c
index 220a8a7..906b5c9 100644
--- a/tools/inetcat.c
+++ b/tools/inetcat.c
@@ -224,25 +224,16 @@ int main(int argc, char **argv)
int devfd = -1;
if (dev->conn_type == CONNECTION_TYPE_NETWORK) {
- unsigned char saddr_[32];
- memset(saddr_, '\0', sizeof(saddr_));
- struct sockaddr* saddr = (struct sockaddr*)&saddr_[0];
- 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)
+ struct sockaddr_storage saddr_storage;
+ struct sockaddr* saddr = (struct sockaddr*)&saddr_storage;
+ /* conn_data holds the address in the native sockaddr layout */
+ memcpy(saddr, dev->conn_data, sizeof(saddr_storage));
+ if (saddr->sa_family != AF_INET
#ifdef AF_INET6
- saddr->sa_family = AF_INET6;
- 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");
- free(dev_list);
- return 1;
+ && saddr->sa_family != AF_INET6
#endif
- }
- else {
- fprintf(stderr, "Unsupported address family 0x%02x\n", dev->conn_data[1]);
+ ) {
+ fprintf(stderr, "Unsupported address family 0x%02x\n", saddr->sa_family);
free(dev_list);
return 1;
}
diff --git a/tools/iproxy.c b/tools/iproxy.c
index d4c7d06..d411103 100644
--- a/tools/iproxy.c
+++ b/tools/iproxy.c
@@ -131,23 +131,14 @@ static void *acceptor_thread(void *arg)
struct sockaddr_storage saddr_storage;
struct sockaddr* saddr = (struct sockaddr*)&saddr_storage;
- 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)
+ /* conn_data holds the address in the native sockaddr layout */
+ memcpy(saddr, dev->conn_data, sizeof(saddr_storage));
+ if (saddr->sa_family != AF_INET
#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;
+ && saddr->sa_family != AF_INET6
#endif
- }
- else {
- fprintf(stderr, "Unsupported address family 0x%02x\n", dev->conn_data[1]);
+ ) {
+ fprintf(stderr, "Unsupported address family 0x%02x\n", saddr->sa_family);
CDATA_FREE(cdata);
return NULL;
}