diff options
| -rw-r--r-- | iproxy.c | 13 | ||||
| -rw-r--r-- | libusbmuxd.c | 34 | ||||
| -rw-r--r-- | libusbmuxd.h | 42 | ||||
| -rw-r--r-- | usbmuxd.h | 3 |
4 files changed, 59 insertions, 33 deletions
| @@ -31,7 +31,6 @@ | |||
| 31 | #include <errno.h> | 31 | #include <errno.h> |
| 32 | #include <arpa/inet.h> | 32 | #include <arpa/inet.h> |
| 33 | #include <pthread.h> | 33 | #include <pthread.h> |
| 34 | #include <usbmuxd.h> | ||
| 35 | #include "sock_stuff.h" | 34 | #include "sock_stuff.h" |
| 36 | #include "libusbmuxd.h" | 35 | #include "libusbmuxd.h" |
| 37 | 36 | ||
| @@ -142,7 +141,7 @@ void *run_ctos_loop(void *arg) | |||
| 142 | void *acceptor_thread(void *arg) | 141 | void *acceptor_thread(void *arg) |
| 143 | { | 142 | { |
| 144 | struct client_data *cdata; | 143 | struct client_data *cdata; |
| 145 | usbmuxd_device_t *dev_list = NULL; | 144 | usbmuxd_scan_result *dev_list = NULL; |
| 146 | pthread_t ctos; | 145 | pthread_t ctos; |
| 147 | 146 | ||
| 148 | if (!arg) { | 147 | if (!arg) { |
| @@ -152,21 +151,21 @@ void *acceptor_thread(void *arg) | |||
| 152 | 151 | ||
| 153 | cdata = (struct client_data*)arg; | 152 | cdata = (struct client_data*)arg; |
| 154 | 153 | ||
| 155 | if (usbmuxd_scan(&dev_list) != 0) { | 154 | if (usbmuxd_scan(&dev_list) < 0) { |
| 156 | printf("Connecting to usbmuxd failed, terminating.\n"); | 155 | printf("Connecting to usbmuxd failed, terminating.\n"); |
| 157 | free(dev_list); | 156 | free(dev_list); |
| 158 | return NULL; | 157 | return NULL; |
| 159 | } | 158 | } |
| 160 | 159 | ||
| 161 | if (!dev_list || dev_list[0].device_id == 0) { | 160 | if (dev_list == NULL || dev_list[0].handle == 0) { |
| 162 | printf("No connected device found, terminating.\n"); | 161 | printf("No connected device found, terminating.\n"); |
| 163 | free(dev_list); | 162 | free(dev_list); |
| 164 | return NULL; | 163 | return NULL; |
| 165 | } | 164 | } |
| 166 | 165 | ||
| 167 | fprintf(stdout, "Requesting connecion to device %d port %d\n", dev_list[0].device_id, device_port); | 166 | fprintf(stdout, "Requesting connecion to device handle == %d, port %d\n", dev_list[0].handle, device_port); |
| 168 | 167 | ||
| 169 | cdata->sfd = usbmuxd_connect(dev_list[0].device_id, device_port); | 168 | cdata->sfd = usbmuxd_connect(dev_list[0].handle, device_port); |
| 170 | free(dev_list); | 169 | free(dev_list); |
| 171 | if (cdata->sfd < 0) { | 170 | if (cdata->sfd < 0) { |
| 172 | fprintf(stderr, "Error connecting to device!\n"); | 171 | fprintf(stderr, "Error connecting to device!\n"); |
| @@ -191,7 +190,7 @@ int main(int argc, char **argv) | |||
| 191 | int mysock = -1; | 190 | int mysock = -1; |
| 192 | 191 | ||
| 193 | if (argc != 3) { | 192 | if (argc != 3) { |
| 194 | printf("usage: %s LOCAL_PORT DEVICE_PORT\n", argv[0]); | 193 | printf("usage: %s LOCAL_TCP_PORT DEVICE_TCP_PORT\n", argv[0]); |
| 195 | return 0; | 194 | return 0; |
| 196 | } | 195 | } |
| 197 | 196 | ||
diff --git a/libusbmuxd.c b/libusbmuxd.c index 4e4ec10..ed331aa 100644 --- a/libusbmuxd.c +++ b/libusbmuxd.c | |||
| @@ -7,7 +7,11 @@ | |||
| 7 | #include <arpa/inet.h> | 7 | #include <arpa/inet.h> |
| 8 | #include <unistd.h> | 8 | #include <unistd.h> |
| 9 | 9 | ||
| 10 | // usbmuxd public interface | ||
| 11 | #include <libusbmuxd.h> | ||
| 12 | // usbmuxd protocol | ||
| 10 | #include <usbmuxd.h> | 13 | #include <usbmuxd.h> |
| 14 | // socket utility functions | ||
| 11 | #include "sock_stuff.h" | 15 | #include "sock_stuff.h" |
| 12 | 16 | ||
| 13 | static int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result) | 17 | static int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result) |
| @@ -40,7 +44,7 @@ static int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result) | |||
| 40 | return -1; | 44 | return -1; |
| 41 | } | 45 | } |
| 42 | 46 | ||
| 43 | int usbmuxd_scan(usbmuxd_device_t **devices) | 47 | int usbmuxd_scan(usbmuxd_scan_result **available_devices) |
| 44 | { | 48 | { |
| 45 | struct usbmuxd_scan_request s_req; | 49 | struct usbmuxd_scan_request s_req; |
| 46 | int sfd; | 50 | int sfd; |
| @@ -48,7 +52,7 @@ int usbmuxd_scan(usbmuxd_device_t **devices) | |||
| 48 | uint32_t res; | 52 | uint32_t res; |
| 49 | uint32_t pktlen; | 53 | uint32_t pktlen; |
| 50 | int recv_len; | 54 | int recv_len; |
| 51 | usbmuxd_device_t *newlist = NULL; | 55 | usbmuxd_scan_result *newlist = NULL; |
| 52 | struct usbmuxd_device_info_record dev_info_pkt; | 56 | struct usbmuxd_device_info_record dev_info_pkt; |
| 53 | int dev_cnt = 0; | 57 | int dev_cnt = 0; |
| 54 | 58 | ||
| @@ -81,7 +85,7 @@ int usbmuxd_scan(usbmuxd_device_t **devices) | |||
| 81 | return -1; | 85 | return -1; |
| 82 | } | 86 | } |
| 83 | 87 | ||
| 84 | *devices = NULL; | 88 | *available_devices = NULL; |
| 85 | // receive device list | 89 | // receive device list |
| 86 | while (1) { | 90 | while (1) { |
| 87 | if (recv_buf_timeout(sfd, &pktlen, 4, MSG_PEEK, 500) == 4) { | 91 | if (recv_buf_timeout(sfd, &pktlen, 4, MSG_PEEK, 500) == 4) { |
| @@ -99,10 +103,14 @@ int usbmuxd_scan(usbmuxd_device_t **devices) | |||
| 99 | fprintf(stderr, "%s: received less data than specified in header!\n", __func__); | 103 | fprintf(stderr, "%s: received less data than specified in header!\n", __func__); |
| 100 | } else { | 104 | } else { |
| 101 | //fprintf(stderr, "%s: got device record with id %d, UUID=%s\n", __func__, dev_info_pkt.device_info.device_id, dev_info_pkt.device_info.serial_number); | 105 | //fprintf(stderr, "%s: got device record with id %d, UUID=%s\n", __func__, dev_info_pkt.device_info.device_id, dev_info_pkt.device_info.serial_number); |
| 102 | newlist = (usbmuxd_device_t*)realloc(*devices, sizeof(usbmuxd_device_t) * (dev_cnt+1)); | 106 | newlist = (usbmuxd_scan_result *)realloc(*available_devices, sizeof(usbmuxd_scan_result) * (dev_cnt+1)); |
| 103 | if (newlist) { | 107 | if (newlist) { |
| 104 | memcpy(newlist+dev_cnt, &dev_info_pkt.device, sizeof(usbmuxd_device_t)); | 108 | newlist[dev_cnt].handle = (int)dev_info_pkt.device.device_id; |
| 105 | *devices = newlist; | 109 | newlist[dev_cnt].product_id = dev_info_pkt.device.product_id; |
| 110 | memset(newlist[dev_cnt].serial_number, '\0', sizeof(newlist[dev_cnt].serial_number)); | ||
| 111 | memcpy(newlist[dev_cnt].serial_number, dev_info_pkt.device.serial_number, | ||
| 112 | sizeof(dev_info_pkt.device.serial_number)); | ||
| 113 | *available_devices = newlist; | ||
| 106 | dev_cnt++; | 114 | dev_cnt++; |
| 107 | } else { | 115 | } else { |
| 108 | fprintf(stderr, "%s: ERROR: out of memory when trying to realloc!\n", __func__); | 116 | fprintf(stderr, "%s: ERROR: out of memory when trying to realloc!\n", __func__); |
| @@ -117,14 +125,14 @@ int usbmuxd_scan(usbmuxd_device_t **devices) | |||
| 117 | } | 125 | } |
| 118 | 126 | ||
| 119 | // terminating zero record | 127 | // terminating zero record |
| 120 | newlist = (usbmuxd_device_t*)realloc(*devices, sizeof(usbmuxd_device_t) * (dev_cnt+1)); | 128 | newlist = (usbmuxd_scan_result *)realloc(*available_devices, sizeof(usbmuxd_scan_result) * (dev_cnt+1)); |
| 121 | memset(newlist+dev_cnt, 0, sizeof(usbmuxd_device_t)); | 129 | memset(newlist+dev_cnt, 0, sizeof(usbmuxd_scan_result)); |
| 122 | *devices = newlist; | 130 | *available_devices = newlist; |
| 123 | 131 | ||
| 124 | return 0; | 132 | return dev_cnt; |
| 125 | } | 133 | } |
| 126 | 134 | ||
| 127 | int usbmuxd_connect(uint32_t device_id, uint16_t port) | 135 | int usbmuxd_connect(const int handle, const unsigned short tcp_port) |
| 128 | { | 136 | { |
| 129 | int sfd; | 137 | int sfd; |
| 130 | struct usbmuxd_connect_request c_req; | 138 | struct usbmuxd_connect_request c_req; |
| @@ -141,8 +149,8 @@ int usbmuxd_connect(uint32_t device_id, uint16_t port) | |||
| 141 | c_req.header.reserved = 0; | 149 | c_req.header.reserved = 0; |
| 142 | c_req.header.type = USBMUXD_CONNECT; | 150 | c_req.header.type = USBMUXD_CONNECT; |
| 143 | c_req.header.tag = 3; | 151 | c_req.header.tag = 3; |
| 144 | c_req.device_id = device_id; | 152 | c_req.device_id = (uint32_t)handle; |
| 145 | c_req.tcp_dport = htons(port); | 153 | c_req.tcp_dport = htons(tcp_port); |
| 146 | c_req.reserved = 0; | 154 | c_req.reserved = 0; |
| 147 | 155 | ||
| 148 | if (send_buf(sfd, &c_req, sizeof(c_req)) < 0) { | 156 | if (send_buf(sfd, &c_req, sizeof(c_req)) < 0) { |
diff --git a/libusbmuxd.h b/libusbmuxd.h index 82f9a47..da99f1f 100644 --- a/libusbmuxd.h +++ b/libusbmuxd.h | |||
| @@ -1,27 +1,45 @@ | |||
| 1 | #ifndef __LIBUSBMUXD_H | 1 | #ifndef __LIBUSBMUXD_H |
| 2 | #define __LIBUSBMUXD_H | 2 | #define __LIBUSBMUXD_H |
| 3 | 3 | ||
| 4 | #include <usbmuxd.h> | 4 | /** |
| 5 | * Array entry returned by 'usbmuxd_scan()' scanning. | ||
| 6 | * | ||
| 7 | * If more than one device is available, 'product_id' and | ||
| 8 | * 'serial_number' and be analysed to help make a selection. | ||
| 9 | * The relevant 'handle' should be passed to 'usbmuxd_connect()', to | ||
| 10 | * start a proxy connection. The value 'handle' should be considered | ||
| 11 | * opaque and no presumption made about the meaning of its value. | ||
| 12 | */ | ||
| 13 | typedef struct { | ||
| 14 | int handle; | ||
| 15 | int product_id; | ||
| 16 | char serial_number[41]; | ||
| 17 | } usbmuxd_scan_result; | ||
| 5 | 18 | ||
| 6 | /** | 19 | /** |
| 7 | * Contacts usbmuxd via it's unix domain socket and performs a scan for | 20 | * Contacts usbmuxd and performs a scan for connected devices. |
| 8 | * connected devices. | ||
| 9 | * | 21 | * |
| 10 | * @param devices Pointer to an array of usbmuxd_device_t. | 22 | * @param available_devices pointer to array of usbmuxd_scan_result. |
| 11 | * Assumed initially NULL, will be allocated by this function. | 23 | * Array of available devices. The required 'handle' |
| 24 | * should be passed to 'usbmuxd_connect()'. The returned array | ||
| 25 | * is zero-terminated for convenience; the final (unused) | ||
| 26 | * entry containing handle == 0. The returned array pointer | ||
| 27 | * should be freed by passing to 'free()' after use. | ||
| 12 | * | 28 | * |
| 13 | * @return number of devices found, negative on error | 29 | * @return number of available devices, zero on no devices, or negative on error |
| 14 | */ | 30 | */ |
| 15 | int usbmuxd_scan(usbmuxd_device_t **devices); | 31 | int usbmuxd_scan(usbmuxd_scan_result **available_devices); |
| 16 | 32 | ||
| 17 | /** | 33 | /** |
| 18 | * Performs the connect procedure via usbmuxd. | 34 | * Request proxy connect to |
| 35 | * | ||
| 36 | * @param handle returned by 'usbmuxd_scan()' | ||
| 19 | * | 37 | * |
| 20 | * @param device_id USB device number of the device to connect to | 38 | * @param tcp_port TCP port number on device, in range 0-65535. |
| 21 | * @param port Port number to connect to | 39 | * common values are 62078 for lockdown, and 22 for SSH. |
| 22 | * | 40 | * |
| 23 | * @return socket of the connection, negative on error | 41 | * @return file descriptor socket of the connection, or -1 on error |
| 24 | */ | 42 | */ |
| 25 | int usbmuxd_connect(uint32_t device_id, uint16_t port); | 43 | int usbmuxd_connect(const int handle, const unsigned short tcp_port); |
| 26 | 44 | ||
| 27 | #endif /* __LIBUSBMUXD_H */ | 45 | #endif /* __LIBUSBMUXD_H */ |
| @@ -1,3 +1,5 @@ | |||
| 1 | /* Protocol defintion for usbmuxd proxy protocol */ | ||
| 2 | |||
| 1 | #ifndef __USBMUXD_H | 3 | #ifndef __USBMUXD_H |
| 2 | #define __USBMUXD_H | 4 | #define __USBMUXD_H |
| 3 | 5 | ||
| @@ -29,7 +31,6 @@ struct usbmuxd_device { | |||
| 29 | uint16_t product_id; | 31 | uint16_t product_id; |
| 30 | char serial_number[40]; | 32 | char serial_number[40]; |
| 31 | } __attribute__((__packed__)); | 33 | } __attribute__((__packed__)); |
| 32 | typedef struct usbmuxd_device usbmuxd_device_t; | ||
| 33 | 34 | ||
| 34 | struct usbmuxd_device_info_record { | 35 | struct usbmuxd_device_info_record { |
| 35 | struct usbmuxd_header header; | 36 | struct usbmuxd_header header; |
