diff options
| -rw-r--r-- | Makefile | 14 | ||||
| -rw-r--r-- | iphone.c | 16 | ||||
| -rw-r--r-- | iproxy.c | 160 | ||||
| -rw-r--r-- | libusbmuxd.c | 170 | ||||
| -rw-r--r-- | libusbmuxd.h | 27 | ||||
| -rw-r--r-- | main.c | 18 | ||||
| -rw-r--r-- | testclient.c | 146 | ||||
| -rw-r--r-- | usbmuxd.h | 5 |
8 files changed, 253 insertions, 303 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | TARGETS=usbmuxd iproxy testclient | 1 | TARGETS=usbmuxd iproxy |
| 2 | CFLAGS=-Wall -g -DDEBUG | 2 | CFLAGS=-I. -Wall -g -DDEBUG |
| 3 | LIBS=-lpthread -lusb -lrt | 3 | LIBS=-lpthread -lusb -lrt |
| 4 | LDFLAGS= | 4 | LDFLAGS= |
| 5 | INSTALL_PREFIX=/usr | 5 | INSTALL_PREFIX=/usr |
| @@ -7,9 +7,10 @@ INSTALL_PREFIX=/usr | |||
| 7 | all: $(TARGETS) | 7 | all: $(TARGETS) |
| 8 | 8 | ||
| 9 | main.o: main.c usbmuxd.h sock_stuff.h iphone.h | 9 | main.o: main.c usbmuxd.h sock_stuff.h iphone.h |
| 10 | iphone.o: iproxy.c iphone.h usbmuxd.h sock_stuff.h | 10 | iphone.o: iphone.c iphone.h usbmuxd.h sock_stuff.h |
| 11 | sock_stuff.o: sock_stuff.c sock_stuff.h | 11 | sock_stuff.o: sock_stuff.c sock_stuff.h |
| 12 | testclient.o: testclient.c sock_stuff.h | 12 | libusbmuxd.o: libusbmuxd.c libusbmuxd.h usbmuxd.h |
| 13 | iproxy.o: iproxy.c sock_stuff.h | ||
| 13 | 14 | ||
| 14 | %.o: %.c | 15 | %.o: %.c |
| 15 | $(CC) -o $@ $(CFLAGS) -c $< | 16 | $(CC) -o $@ $(CFLAGS) -c $< |
| @@ -17,10 +18,7 @@ testclient.o: testclient.c sock_stuff.h | |||
| 17 | usbmuxd: main.o sock_stuff.o iphone.o | 18 | usbmuxd: main.o sock_stuff.o iphone.o |
| 18 | $(CC) -o $@ $(LDFLAGS) $^ $(LIBS) | 19 | $(CC) -o $@ $(LDFLAGS) $^ $(LIBS) |
| 19 | 20 | ||
| 20 | testclient: testclient.o sock_stuff.o | 21 | iproxy: iproxy.o libusbmuxd.o sock_stuff.o |
| 21 | $(CC) -o $@ $(LDFLAGS) $^ $(LIBS) | ||
| 22 | |||
| 23 | iproxy: iproxy.o sock_stuff.o | ||
| 24 | $(CC) -o $@ $(LDFLAGS) $^ $(LIBS) | 22 | $(CC) -o $@ $(LDFLAGS) $^ $(LIBS) |
| 25 | 23 | ||
| 26 | clean: | 24 | clean: |
| @@ -114,7 +114,7 @@ static int clients = 0; | |||
| 114 | 114 | ||
| 115 | /** | 115 | /** |
| 116 | */ | 116 | */ |
| 117 | int toto_debug = 0; | 117 | int toto_debug = 1; |
| 118 | void log_debug_msg(const char *format, ...) | 118 | void log_debug_msg(const char *format, ...) |
| 119 | { | 119 | { |
| 120 | #ifndef STRIP_DEBUG_CODE | 120 | #ifndef STRIP_DEBUG_CODE |
| @@ -124,7 +124,7 @@ void log_debug_msg(const char *format, ...) | |||
| 124 | va_start(args, format); | 124 | va_start(args, format); |
| 125 | 125 | ||
| 126 | if (toto_debug) | 126 | if (toto_debug) |
| 127 | fprintf(stderr, format, args); | 127 | vfprintf(stderr, format, args); |
| 128 | 128 | ||
| 129 | va_end(args); | 129 | va_end(args); |
| 130 | 130 | ||
| @@ -215,6 +215,12 @@ static iphone_error_t iphone_config_usb_device(iphone_device_t phone) | |||
| 215 | int bytes; | 215 | int bytes; |
| 216 | char buf[512]; | 216 | char buf[512]; |
| 217 | 217 | ||
| 218 | log_debug_msg("checking configuration...\n"); | ||
| 219 | if (phone->__device->config->bConfigurationValue != 3) { | ||
| 220 | log_debug_msg("WARNING: usb device configuration is not 3 as expected!\n"); | ||
| 221 | } | ||
| 222 | |||
| 223 | #if 0 | ||
| 218 | log_debug_msg("setting configuration...\n"); | 224 | log_debug_msg("setting configuration...\n"); |
| 219 | ret = usb_set_configuration(phone->device, 3); | 225 | ret = usb_set_configuration(phone->device, 3); |
| 220 | if (ret != 0) { | 226 | if (ret != 0) { |
| @@ -242,6 +248,7 @@ static iphone_error_t iphone_config_usb_device(iphone_device_t phone) | |||
| 242 | } else { | 248 | } else { |
| 243 | log_debug_msg("done.\n"); | 249 | log_debug_msg("done.\n"); |
| 244 | } | 250 | } |
| 251 | #endif | ||
| 245 | 252 | ||
| 246 | log_debug_msg("claiming interface... "); | 253 | log_debug_msg("claiming interface... "); |
| 247 | ret = usb_claim_interface(phone->device, 1); | 254 | ret = usb_claim_interface(phone->device, 1); |
| @@ -307,8 +314,9 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t | |||
| 307 | if (dev->devnum == dev_n) { | 314 | if (dev->devnum == dev_n) { |
| 308 | phone->__device = dev; | 315 | phone->__device = dev; |
| 309 | phone->device = usb_open(phone->__device); | 316 | phone->device = usb_open(phone->__device); |
| 310 | iphone_config_usb_device(phone); | 317 | if (iphone_config_usb_device(phone) == IPHONE_E_SUCCESS) { |
| 311 | goto found; | 318 | goto found; |
| 319 | } | ||
| 312 | } | 320 | } |
| 313 | 321 | ||
| 314 | iphone_free_device(phone); | 322 | iphone_free_device(phone); |
| @@ -31,8 +31,9 @@ | |||
| 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" | 34 | #include <usbmuxd.h> |
| 35 | #include "sock_stuff.h" | 35 | #include "sock_stuff.h" |
| 36 | #include "libusbmuxd.h" | ||
| 36 | 37 | ||
| 37 | static uint16_t listen_port = 0; | 38 | static uint16_t listen_port = 0; |
| 38 | static uint16_t device_port = 0; | 39 | static uint16_t device_port = 0; |
| @@ -46,43 +47,6 @@ struct client_data { | |||
| 46 | volatile int stop_stoc; | 47 | volatile int stop_stoc; |
| 47 | }; | 48 | }; |
| 48 | 49 | ||
| 49 | int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result) | ||
| 50 | { | ||
| 51 | struct usbmuxd_result res; | ||
| 52 | int recv_len; | ||
| 53 | int i; | ||
| 54 | uint32_t rrr[5]; | ||
| 55 | |||
| 56 | if (!result) { | ||
| 57 | return -EINVAL; | ||
| 58 | } | ||
| 59 | |||
| 60 | if ((recv_len = recv_buf(sfd, &res, sizeof(res))) <= 0) { | ||
| 61 | perror("recv"); | ||
| 62 | return -errno; | ||
| 63 | } else { | ||
| 64 | memcpy(&rrr, &res, recv_len); | ||
| 65 | for (i = 0; i < recv_len/4; i++) { | ||
| 66 | fprintf(stderr, "%08x ", rrr[i]); | ||
| 67 | } | ||
| 68 | fprintf(stderr, "\n"); | ||
| 69 | if ((recv_len == sizeof(res)) | ||
| 70 | && (res.header.length == recv_len) | ||
| 71 | && (res.header.reserved == 0) | ||
| 72 | && (res.header.type == USBMUXD_RESULT) | ||
| 73 | ) { | ||
| 74 | *result = res.result; | ||
| 75 | if (res.header.tag == tag) { | ||
| 76 | return 1; | ||
| 77 | } else { | ||
| 78 | return 0; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | return -1; | ||
| 84 | } | ||
| 85 | |||
| 86 | void *run_stoc_loop(void *arg) | 50 | void *run_stoc_loop(void *arg) |
| 87 | { | 51 | { |
| 88 | struct client_data *cdata = (struct client_data*)arg; | 52 | struct client_data *cdata = (struct client_data*)arg; |
| @@ -178,13 +142,7 @@ void *run_ctos_loop(void *arg) | |||
| 178 | void *acceptor_thread(void *arg) | 142 | void *acceptor_thread(void *arg) |
| 179 | { | 143 | { |
| 180 | struct client_data *cdata; | 144 | struct client_data *cdata; |
| 181 | int recv_len = 0; | 145 | usbmuxd_device_t *dev_list = NULL; |
| 182 | int scan_done; | ||
| 183 | int connected; | ||
| 184 | uint32_t pktlen; | ||
| 185 | unsigned char *buf; | ||
| 186 | struct usbmuxd_scan_request scan; | ||
| 187 | struct am_device_info device_info; | ||
| 188 | pthread_t ctos; | 146 | pthread_t ctos; |
| 189 | 147 | ||
| 190 | if (!arg) { | 148 | if (!arg) { |
| @@ -194,102 +152,36 @@ void *acceptor_thread(void *arg) | |||
| 194 | 152 | ||
| 195 | cdata = (struct client_data*)arg; | 153 | cdata = (struct client_data*)arg; |
| 196 | 154 | ||
| 197 | cdata->sfd = connect_unix_socket(USBMUXD_SOCKET_FILE); | 155 | if (usbmuxd_scan(&dev_list) != 0) { |
| 198 | if (cdata->sfd < 0) { | 156 | printf("Connecting to usbmuxd failed, terminating.\n"); |
| 199 | printf("error opening socket, terminating.\n"); | 157 | free(dev_list); |
| 200 | return NULL; | 158 | return NULL; |
| 201 | } | 159 | } |
| 202 | 160 | ||
| 203 | // send scan | 161 | if (!dev_list || dev_list[0].device_id == 0) { |
| 204 | scan.header.length = sizeof(struct usbmuxd_scan_request); | 162 | printf("No connected device found, terminating.\n"); |
| 205 | scan.header.reserved = 0; | 163 | free(dev_list); |
| 206 | scan.header.type = USBMUXD_SCAN; | 164 | return NULL; |
| 207 | scan.header.tag = 2; | 165 | } |
| 208 | |||
| 209 | scan_done = 0; | ||
| 210 | connected = 0; | ||
| 211 | |||
| 212 | fprintf(stdout, "sending scan packet\n"); | ||
| 213 | if (send(cdata->sfd, &scan, scan.header.length, 0) == scan.header.length) { | ||
| 214 | uint32_t res = -1; | ||
| 215 | // get response | ||
| 216 | if (usbmuxd_get_result(cdata->sfd, scan.header.tag, &res) && (res==0)) { | ||
| 217 | fprintf(stdout, "Got response to scan request!\n"); | ||
| 218 | scan_done = 1; | ||
| 219 | } else { | ||
| 220 | fprintf(stderr, "Did not get response to scan request (with result=0)...\n"); | ||
| 221 | close(cdata->sfd); | ||
| 222 | cdata->sfd = -1; | ||
| 223 | return NULL; | ||
| 224 | } | ||
| 225 | |||
| 226 | device_info.device_id = 0; | ||
| 227 | |||
| 228 | if (scan_done) { | ||
| 229 | // get all devices | ||
| 230 | while (1) { | ||
| 231 | if (recv_buf_timeout(cdata->sfd, &pktlen, 4, MSG_PEEK, 1000) == 4) { | ||
| 232 | buf = (unsigned char*)malloc(pktlen); | ||
| 233 | if (!buf) { | ||
| 234 | exit(-ENOMEM); | ||
| 235 | } | ||
| 236 | recv_len = recv_buf(cdata->sfd, buf, pktlen); | ||
| 237 | if (recv_len < pktlen) { | ||
| 238 | fprintf(stdout, "received less data than specified in header!\n"); | ||
| 239 | } | ||
| 240 | fprintf(stdout, "Received device data\n"); | ||
| 241 | //log_debug_buffer(stdout, (char*)buf, pktlen); | ||
| 242 | memcpy(&device_info, buf + sizeof(struct usbmuxd_header), sizeof(device_info)); | ||
| 243 | free(buf); | ||
| 244 | } else { | ||
| 245 | // we _should_ have all of them now. | ||
| 246 | // or perhaps an error occured. | ||
| 247 | break; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | if (device_info.device_id > 0) { | ||
| 253 | struct usbmuxd_connect_request c_req; | ||
| 254 | |||
| 255 | fprintf(stdout, "Requesting connecion to device %d port %d\n", device_info.device_id, device_port); | ||
| 256 | 166 | ||
| 257 | // try to connect to last device found | 167 | fprintf(stdout, "Requesting connecion to device %d port %d\n", dev_list[0].device_id, device_port); |
| 258 | c_req.header.length = sizeof(c_req); | ||
| 259 | c_req.header.reserved = 0; | ||
| 260 | c_req.header.type = USBMUXD_CONNECT; | ||
| 261 | c_req.header.tag = 3; | ||
| 262 | c_req.device_id = device_info.device_id; | ||
| 263 | c_req.tcp_dport = htons(device_port); | ||
| 264 | c_req.reserved = 0; | ||
| 265 | 168 | ||
| 266 | if (send_buf(cdata->sfd, &c_req, sizeof(c_req)) < 0) { | 169 | cdata->sfd = usbmuxd_connect(dev_list[0].device_id, device_port); |
| 267 | perror("send"); | 170 | free(dev_list); |
| 268 | } else { | 171 | if (cdata->sfd < 0) { |
| 269 | // read ACK | 172 | fprintf(stderr, "Error connecting to device!\n"); |
| 270 | res = -1; | 173 | } else { |
| 271 | fprintf(stdout, "Reading connect result...\n"); | 174 | cdata->stop_ctos = 0; |
| 272 | if (usbmuxd_get_result(cdata->sfd, c_req.header.tag, &res)) { | 175 | pthread_create(&ctos, NULL, run_ctos_loop, cdata); |
| 273 | if (res == 0) { | 176 | pthread_join(ctos, NULL); |
| 274 | fprintf(stdout, "Connect success!\n"); | 177 | } |
| 275 | connected = 1; | ||
| 276 | } else { | ||
| 277 | fprintf(stderr, "Connect failed, Error code=%d\n", res); | ||
| 278 | } | ||
| 279 | } | ||
| 280 | } | ||
| 281 | } | ||
| 282 | 178 | ||
| 283 | if (connected) { | 179 | if (cdata->fd > 0) { |
| 284 | cdata->stop_ctos = 0; | 180 | close(cdata->fd); |
| 285 | pthread_create(&ctos, NULL, run_ctos_loop, cdata); | 181 | } |
| 286 | pthread_join(ctos, NULL); | 182 | if (cdata->sfd > 0) { |
| 287 | } else { | 183 | close(cdata->sfd); |
| 288 | fprintf(stderr, "Error connecting to device!\n"); | ||
| 289 | } | ||
| 290 | } | 184 | } |
| 291 | close(cdata->fd); | ||
| 292 | close(cdata->sfd); | ||
| 293 | 185 | ||
| 294 | return NULL; | 186 | return NULL; |
| 295 | } | 187 | } |
diff --git a/libusbmuxd.c b/libusbmuxd.c new file mode 100644 index 0000000..4e4ec10 --- /dev/null +++ b/libusbmuxd.c | |||
| @@ -0,0 +1,170 @@ | |||
| 1 | #include <stdint.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <errno.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <string.h> | ||
| 6 | #include <sys/socket.h> | ||
| 7 | #include <arpa/inet.h> | ||
| 8 | #include <unistd.h> | ||
| 9 | |||
| 10 | #include <usbmuxd.h> | ||
| 11 | #include "sock_stuff.h" | ||
| 12 | |||
| 13 | static int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result) | ||
| 14 | { | ||
| 15 | struct usbmuxd_result res; | ||
| 16 | int recv_len; | ||
| 17 | |||
| 18 | if (!result) { | ||
| 19 | return -EINVAL; | ||
| 20 | } | ||
| 21 | |||
| 22 | if ((recv_len = recv_buf(sfd, &res, sizeof(res))) <= 0) { | ||
| 23 | perror("recv"); | ||
| 24 | return -errno; | ||
| 25 | } else { | ||
| 26 | if ((recv_len == sizeof(res)) | ||
| 27 | && (res.header.length == recv_len) | ||
| 28 | && (res.header.reserved == 0) | ||
| 29 | && (res.header.type == USBMUXD_RESULT) | ||
| 30 | ) { | ||
| 31 | *result = res.result; | ||
| 32 | if (res.header.tag == tag) { | ||
| 33 | return 1; | ||
| 34 | } else { | ||
| 35 | return 0; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | return -1; | ||
| 41 | } | ||
| 42 | |||
| 43 | int usbmuxd_scan(usbmuxd_device_t **devices) | ||
| 44 | { | ||
| 45 | struct usbmuxd_scan_request s_req; | ||
| 46 | int sfd; | ||
| 47 | int scan_success = 0; | ||
| 48 | uint32_t res; | ||
| 49 | uint32_t pktlen; | ||
| 50 | int recv_len; | ||
| 51 | usbmuxd_device_t *newlist = NULL; | ||
| 52 | struct usbmuxd_device_info_record dev_info_pkt; | ||
| 53 | int dev_cnt = 0; | ||
| 54 | |||
| 55 | sfd = connect_unix_socket(USBMUXD_SOCKET_FILE); | ||
| 56 | if (sfd < 0) { | ||
| 57 | fprintf(stderr, "%s: error opening socket!\n", __func__); | ||
| 58 | return sfd; | ||
| 59 | } | ||
| 60 | |||
| 61 | s_req.header.length = sizeof(struct usbmuxd_scan_request); | ||
| 62 | s_req.header.reserved = 0; | ||
| 63 | s_req.header.type = USBMUXD_SCAN; | ||
| 64 | s_req.header.tag = 2; | ||
| 65 | |||
| 66 | // send scan request packet | ||
| 67 | if (send_buf(sfd, &s_req, s_req.header.length) == s_req.header.length) { | ||
| 68 | res = -1; | ||
| 69 | // get response | ||
| 70 | if (usbmuxd_get_result(sfd, s_req.header.tag, &res) && (res == 0)) { | ||
| 71 | scan_success = 1; | ||
| 72 | } else { | ||
| 73 | fprintf(stderr, "%s: Did not get response to scan request (with result=0)...\n", __func__); | ||
| 74 | close(sfd); | ||
| 75 | return res; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | if (!scan_success) { | ||
| 80 | fprintf(stderr, "%s: Could not send scan request!\n", __func__); | ||
| 81 | return -1; | ||
| 82 | } | ||
| 83 | |||
| 84 | *devices = NULL; | ||
| 85 | // receive device list | ||
| 86 | while (1) { | ||
| 87 | if (recv_buf_timeout(sfd, &pktlen, 4, MSG_PEEK, 500) == 4) { | ||
| 88 | if (pktlen != sizeof(dev_info_pkt)) { | ||
| 89 | // invalid packet size received! | ||
| 90 | fprintf(stderr, "%s: Invalid packet size (%d) received when expecting a device info record.\n", __func__, pktlen); | ||
| 91 | break; | ||
| 92 | } | ||
| 93 | |||
| 94 | recv_len = recv_buf(sfd, &dev_info_pkt, pktlen); | ||
| 95 | if (recv_len <= 0) { | ||
| 96 | fprintf(stderr, "%s: Error when receiving device info record\n", __func__); | ||
| 97 | break; | ||
| 98 | } else if (recv_len < pktlen) { | ||
| 99 | fprintf(stderr, "%s: received less data than specified in header!\n", __func__); | ||
| 100 | } 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); | ||
| 102 | newlist = (usbmuxd_device_t*)realloc(*devices, sizeof(usbmuxd_device_t) * (dev_cnt+1)); | ||
| 103 | if (newlist) { | ||
| 104 | memcpy(newlist+dev_cnt, &dev_info_pkt.device, sizeof(usbmuxd_device_t)); | ||
| 105 | *devices = newlist; | ||
| 106 | dev_cnt++; | ||
| 107 | } else { | ||
| 108 | fprintf(stderr, "%s: ERROR: out of memory when trying to realloc!\n", __func__); | ||
| 109 | break; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } else { | ||
| 113 | // we _should_ have all of them now. | ||
| 114 | // or perhaps an error occured. | ||
| 115 | break; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | // terminating zero record | ||
| 120 | newlist = (usbmuxd_device_t*)realloc(*devices, sizeof(usbmuxd_device_t) * (dev_cnt+1)); | ||
| 121 | memset(newlist+dev_cnt, 0, sizeof(usbmuxd_device_t)); | ||
| 122 | *devices = newlist; | ||
| 123 | |||
| 124 | return 0; | ||
| 125 | } | ||
| 126 | |||
| 127 | int usbmuxd_connect(uint32_t device_id, uint16_t port) | ||
| 128 | { | ||
| 129 | int sfd; | ||
| 130 | struct usbmuxd_connect_request c_req; | ||
| 131 | int connected = 0; | ||
| 132 | uint32_t res = -1; | ||
| 133 | |||
| 134 | sfd = connect_unix_socket(USBMUXD_SOCKET_FILE); | ||
| 135 | if (sfd < 0) { | ||
| 136 | fprintf(stderr, "%s: Error: Connection to usbmuxd failed: %s\n", __func__, strerror(errno)); | ||
| 137 | return sfd; | ||
| 138 | } | ||
| 139 | |||
| 140 | c_req.header.length = sizeof(c_req); | ||
| 141 | c_req.header.reserved = 0; | ||
| 142 | c_req.header.type = USBMUXD_CONNECT; | ||
| 143 | c_req.header.tag = 3; | ||
| 144 | c_req.device_id = device_id; | ||
| 145 | c_req.tcp_dport = htons(port); | ||
| 146 | c_req.reserved = 0; | ||
| 147 | |||
| 148 | if (send_buf(sfd, &c_req, sizeof(c_req)) < 0) { | ||
| 149 | perror("send"); | ||
| 150 | } else { | ||
| 151 | // read ACK | ||
| 152 | fprintf(stderr, "%s: Reading connect result...\n", __func__); | ||
| 153 | if (usbmuxd_get_result(sfd, c_req.header.tag, &res)) { | ||
| 154 | if (res == 0) { | ||
| 155 | fprintf(stderr, "%s: Connect success!\n", __func__); | ||
| 156 | connected = 1; | ||
| 157 | } else { | ||
| 158 | fprintf(stderr, "%s: Connect failed, Error code=%d\n", __func__, res); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | if (connected) { | ||
| 164 | return sfd; | ||
| 165 | } | ||
| 166 | |||
| 167 | close(sfd); | ||
| 168 | |||
| 169 | return -1; | ||
| 170 | } | ||
diff --git a/libusbmuxd.h b/libusbmuxd.h new file mode 100644 index 0000000..82f9a47 --- /dev/null +++ b/libusbmuxd.h | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | #ifndef __LIBUSBMUXD_H | ||
| 2 | #define __LIBUSBMUXD_H | ||
| 3 | |||
| 4 | #include <usbmuxd.h> | ||
| 5 | |||
| 6 | /** | ||
| 7 | * Contacts usbmuxd via it's unix domain socket and performs a scan for | ||
| 8 | * connected devices. | ||
| 9 | * | ||
| 10 | * @param devices Pointer to an array of usbmuxd_device_t. | ||
| 11 | * Assumed initially NULL, will be allocated by this function. | ||
| 12 | * | ||
| 13 | * @return number of devices found, negative on error | ||
| 14 | */ | ||
| 15 | int usbmuxd_scan(usbmuxd_device_t **devices); | ||
| 16 | |||
| 17 | /** | ||
| 18 | * Performs the connect procedure via usbmuxd. | ||
| 19 | * | ||
| 20 | * @param device_id USB device number of the device to connect to | ||
| 21 | * @param port Port number to connect to | ||
| 22 | * | ||
| 23 | * @return socket of the connection, negative on error | ||
| 24 | */ | ||
| 25 | int usbmuxd_connect(uint32_t device_id, uint16_t port); | ||
| 26 | |||
| 27 | #endif /* __LIBUSBMUXD_H */ | ||
| @@ -92,10 +92,10 @@ static void print_buffer(FILE *fp, const char *data, const int length) | |||
| 92 | if (verbose >= 4) fprintf(fp, "%04x: ", i); | 92 | if (verbose >= 4) fprintf(fp, "%04x: ", i); |
| 93 | for (j=0;j<16;j++) { | 93 | for (j=0;j<16;j++) { |
| 94 | if (i+j >= length) { | 94 | if (i+j >= length) { |
| 95 | printf(" "); | 95 | if (verbose >= 4) fprintf(fp, " "); |
| 96 | continue; | 96 | continue; |
| 97 | } | 97 | } |
| 98 | if (verbose >= 4) printf("%02hhx ", *(data+i+j)); | 98 | if (verbose >= 4) fprintf(fp, "%02hhx ", *(data+i+j)); |
| 99 | } | 99 | } |
| 100 | if (verbose >= 4) fprintf(fp, " | "); | 100 | if (verbose >= 4) fprintf(fp, " | "); |
| 101 | for(j=0;j<16;j++) { | 101 | for(j=0;j<16;j++) { |
| @@ -103,7 +103,7 @@ static void print_buffer(FILE *fp, const char *data, const int length) | |||
| 103 | break; | 103 | break; |
| 104 | c = *(data+i+j); | 104 | c = *(data+i+j); |
| 105 | if ((c < 32) || (c > 127)) { | 105 | if ((c < 32) || (c > 127)) { |
| 106 | printf("."); | 106 | if (verbose >= 4) fprintf(fp, "."); |
| 107 | continue; | 107 | continue; |
| 108 | } | 108 | } |
| 109 | if (verbose >= 4) fprintf(fp, "%c", c); | 109 | if (verbose >= 4) fprintf(fp, "%c", c); |
| @@ -491,14 +491,14 @@ static void *usbmuxd_client_init_thread(void *arg) | |||
| 491 | if (verbose >= 2) fprintf(stderr, "%s: started (fd=%d)\n", __func__, cdata->socket); | 491 | if (verbose >= 2) fprintf(stderr, "%s: started (fd=%d)\n", __func__, cdata->socket); |
| 492 | 492 | ||
| 493 | if ((recv_len = usbmuxd_get_request(cdata->socket, (void**)&s_req, 0)) <= 0) { | 493 | if ((recv_len = usbmuxd_get_request(cdata->socket, (void**)&s_req, 0)) <= 0) { |
| 494 | if (verbose >= 2) fprintf(stderr, "%s: No Hello packet received, error %s\n", __func__, strerror(errno)); | 494 | if (verbose >= 2) fprintf(stderr, "%s: No scan packet received, error %s\n", __func__, strerror(errno)); |
| 495 | goto leave; | 495 | goto leave; |
| 496 | } | 496 | } |
| 497 | 497 | ||
| 498 | if ((recv_len == sizeof(struct usbmuxd_scan_request)) && (s_req->header.length == sizeof(struct usbmuxd_scan_request)) | 498 | if ((recv_len == sizeof(struct usbmuxd_scan_request)) && (s_req->header.length == sizeof(struct usbmuxd_scan_request)) |
| 499 | && (s_req->header.reserved == 0) && (s_req->header.type == USBMUXD_SCAN)) { | 499 | && (s_req->header.reserved == 0) && (s_req->header.type == USBMUXD_SCAN)) { |
| 500 | // send success response | 500 | // send success response |
| 501 | if (verbose >= 3) fprintf(stderr, "%s: Got Hello packet!\n", __func__); | 501 | if (verbose >= 3) fprintf(stderr, "%s: Got scan packet!\n", __func__); |
| 502 | usbmuxd_send_result(cdata->socket, s_req->header.tag, 0); | 502 | usbmuxd_send_result(cdata->socket, s_req->header.tag, 0); |
| 503 | } else if ((recv_len == sizeof(struct usbmuxd_connect_request)) && (s_req->header.type == USBMUXD_CONNECT)) { | 503 | } else if ((recv_len == sizeof(struct usbmuxd_connect_request)) && (s_req->header.type == USBMUXD_CONNECT)) { |
| 504 | c_req = (struct usbmuxd_connect_request*)s_req; | 504 | c_req = (struct usbmuxd_connect_request*)s_req; |
| @@ -506,7 +506,7 @@ static void *usbmuxd_client_init_thread(void *arg) | |||
| 506 | goto connect; | 506 | goto connect; |
| 507 | } else { | 507 | } else { |
| 508 | // send error response and exit | 508 | // send error response and exit |
| 509 | if (verbose >= 2) fprintf(stderr, "%s: Invalid Hello packet received.\n", __func__); | 509 | if (verbose >= 2) fprintf(stderr, "%s: Invalid scan packet received.\n", __func__); |
| 510 | // TODO is this required?! | 510 | // TODO is this required?! |
| 511 | usbmuxd_send_result(cdata->socket, s_req->header.tag, EINVAL); | 511 | usbmuxd_send_result(cdata->socket, s_req->header.tag, EINVAL); |
| 512 | goto leave; | 512 | goto leave; |
| @@ -537,14 +537,14 @@ static void *usbmuxd_client_init_thread(void *arg) | |||
| 537 | memset(&dev_info_rec, 0, sizeof(dev_info_rec)); | 537 | memset(&dev_info_rec, 0, sizeof(dev_info_rec)); |
| 538 | dev_info_rec.header.length = sizeof(dev_info_rec); | 538 | dev_info_rec.header.length = sizeof(dev_info_rec); |
| 539 | dev_info_rec.header.type = USBMUXD_DEVICE_INFO; | 539 | dev_info_rec.header.type = USBMUXD_DEVICE_INFO; |
| 540 | dev_info_rec.device_info.device_id = dev->devnum; | 540 | dev_info_rec.device.device_id = dev->devnum; |
| 541 | dev_info_rec.device_info.product_id = dev->descriptor.idProduct; | 541 | dev_info_rec.device.product_id = dev->descriptor.idProduct; |
| 542 | if (dev->descriptor.iSerialNumber) { | 542 | if (dev->descriptor.iSerialNumber) { |
| 543 | usb_dev_handle *udev; | 543 | usb_dev_handle *udev; |
| 544 | //pthread_mutex_lock(&usbmux_mutex); | 544 | //pthread_mutex_lock(&usbmux_mutex); |
| 545 | udev = usb_open(dev); | 545 | udev = usb_open(dev); |
| 546 | if (udev) { | 546 | if (udev) { |
| 547 | usb_get_string_simple(udev, dev->descriptor.iSerialNumber, dev_info_rec.device_info.serial_number, sizeof(dev_info_rec.device_info.serial_number)+1); | 547 | usb_get_string_simple(udev, dev->descriptor.iSerialNumber, dev_info_rec.device.serial_number, sizeof(dev_info_rec.device.serial_number)+1); |
| 548 | usb_close(udev); | 548 | usb_close(udev); |
| 549 | } | 549 | } |
| 550 | //pthread_mutex_unlock(&usbmux_mutex); | 550 | //pthread_mutex_unlock(&usbmux_mutex); |
diff --git a/testclient.c b/testclient.c deleted file mode 100644 index dc2dd28..0000000 --- a/testclient.c +++ /dev/null | |||
| @@ -1,146 +0,0 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <fcntl.h> | ||
| 4 | #include <stddef.h> | ||
| 5 | #include <sys/socket.h> | ||
| 6 | #include <sys/un.h> | ||
| 7 | #include <unistd.h> | ||
| 8 | #include <errno.h> | ||
| 9 | #include <arpa/inet.h> | ||
| 10 | #include "usbmuxd.h" | ||
| 11 | #include "sock_stuff.h" | ||
| 12 | |||
| 13 | int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result) | ||
| 14 | { | ||
| 15 | struct usbmuxd_result res; | ||
| 16 | int recv_len; | ||
| 17 | |||
| 18 | if (!result) { | ||
| 19 | return -EINVAL; | ||
| 20 | } | ||
| 21 | |||
| 22 | if ((recv_len = recv_buf(sfd, &res, sizeof(res))) <= 0) { | ||
| 23 | perror("recv"); | ||
| 24 | return -errno; | ||
| 25 | } else { | ||
| 26 | if ((recv_len == sizeof(res)) | ||
| 27 | && (res.header.length == recv_len) | ||
| 28 | && (res.header.reserved == 0) | ||
| 29 | && (res.header.type == USBMUXD_RESULT) | ||
| 30 | ) { | ||
| 31 | *result = res.result; | ||
| 32 | if (res.header.tag == tag) { | ||
| 33 | return 1; | ||
| 34 | } else { | ||
| 35 | return 0; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | return -1; | ||
| 41 | } | ||
| 42 | |||
| 43 | int main(int argc, char **argv) | ||
| 44 | { | ||
| 45 | int sfd; | ||
| 46 | int recv_len = 0; | ||
| 47 | int scan_done; | ||
| 48 | int connected; | ||
| 49 | uint32_t pktlen; | ||
| 50 | unsigned char *buf; | ||
| 51 | struct usbmuxd_scan_request scan; | ||
| 52 | struct am_device_info device_info; | ||
| 53 | |||
| 54 | sfd = connect_unix_socket(USBMUXD_SOCKET_FILE); | ||
| 55 | if (sfd < 0) { | ||
| 56 | printf("error opening socket, terminating.\n"); | ||
| 57 | return -1; | ||
| 58 | } | ||
| 59 | |||
| 60 | // send scan | ||
| 61 | scan.header.length = sizeof(struct usbmuxd_scan_request); | ||
| 62 | scan.header.reserved = 0; | ||
| 63 | scan.header.type = USBMUXD_SCAN; | ||
| 64 | scan.header.tag = 2; | ||
| 65 | |||
| 66 | scan_done = 0; | ||
| 67 | connected = 0; | ||
| 68 | |||
| 69 | fprintf(stdout, "sending scan packet\n"); | ||
| 70 | if (send(sfd, &scan, scan.header.length, 0) == scan.header.length) { | ||
| 71 | uint32_t res = -1; | ||
| 72 | // get response | ||
| 73 | if (usbmuxd_get_result(sfd, scan.header.tag, &res) && (res==0)) { | ||
| 74 | fprintf(stdout, "Got response to scan request!\n"); | ||
| 75 | scan_done = 1; | ||
| 76 | } else { | ||
| 77 | fprintf(stderr, "Did not get response to scan request (with result=0)...\n"); | ||
| 78 | close(sfd); | ||
| 79 | return -1; | ||
| 80 | } | ||
| 81 | |||
| 82 | device_info.device_id = 0; | ||
| 83 | |||
| 84 | if (scan_done) { | ||
| 85 | // get all devices | ||
| 86 | while (1) { | ||
| 87 | if (recv_buf_timeout(sfd, &pktlen, 4, MSG_PEEK, 1000) == 4) { | ||
| 88 | buf = (unsigned char*)malloc(pktlen); | ||
| 89 | if (!buf) { | ||
| 90 | exit(-ENOMEM); | ||
| 91 | } | ||
| 92 | recv_len = recv_buf(sfd, buf, pktlen); | ||
| 93 | if (recv_len < pktlen) { | ||
| 94 | fprintf(stdout, "received less data than specified in header!\n"); | ||
| 95 | } | ||
| 96 | fprintf(stdout, "got device data:\n"); | ||
| 97 | //log_debug_buffer(stdout, (char*)buf, pktlen); | ||
| 98 | memcpy(&device_info, buf + sizeof(struct usbmuxd_header), sizeof(device_info)); | ||
| 99 | free(buf); | ||
| 100 | } else { | ||
| 101 | // we _should_ have all of them now. | ||
| 102 | // or perhaps an error occured. | ||
| 103 | break; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | if (device_info.device_id > 0) { | ||
| 109 | struct usbmuxd_connect_request c_req; | ||
| 110 | |||
| 111 | // try to connect to last device found | ||
| 112 | c_req.header.length = sizeof(c_req); | ||
| 113 | c_req.header.reserved = 0; | ||
| 114 | c_req.header.type = USBMUXD_CONNECT; | ||
| 115 | c_req.header.tag = 3; | ||
| 116 | c_req.device_id = device_info.device_id; | ||
| 117 | c_req.tcp_dport = htons(22); | ||
| 118 | c_req.reserved = 0; | ||
| 119 | |||
| 120 | if (send_buf(sfd, &c_req, sizeof(c_req)) < 0) { | ||
| 121 | perror("send"); | ||
| 122 | } else { | ||
| 123 | // read ACK | ||
| 124 | res = -1; | ||
| 125 | if (usbmuxd_get_result(sfd, c_req.header.tag, &res)) { | ||
| 126 | if (res == 0) { | ||
| 127 | fprintf(stdout, "Connect success!\n"); | ||
| 128 | connected = 1; | ||
| 129 | } else { | ||
| 130 | fprintf(stderr, "Connect failed, Error code=%d\n", res); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | if (connected) { | ||
| 137 | |||
| 138 | |||
| 139 | // do communication now. | ||
| 140 | sleep(10); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | close(sfd); | ||
| 144 | |||
| 145 | return 0; | ||
| 146 | } | ||
| @@ -24,15 +24,16 @@ struct usbmuxd_connect_request { | |||
| 24 | uint16_t reserved; // set to zero | 24 | uint16_t reserved; // set to zero |
| 25 | } __attribute__((__packed__)); | 25 | } __attribute__((__packed__)); |
| 26 | 26 | ||
| 27 | struct am_device_info { | 27 | struct usbmuxd_device { |
| 28 | uint32_t device_id; | 28 | uint32_t device_id; |
| 29 | uint16_t product_id; | 29 | uint16_t product_id; |
| 30 | char serial_number[40]; | 30 | char serial_number[40]; |
| 31 | } __attribute__((__packed__)); | 31 | } __attribute__((__packed__)); |
| 32 | typedef struct usbmuxd_device usbmuxd_device_t; | ||
| 32 | 33 | ||
| 33 | struct usbmuxd_device_info_record { | 34 | struct usbmuxd_device_info_record { |
| 34 | struct usbmuxd_header header; | 35 | struct usbmuxd_header header; |
| 35 | struct am_device_info device_info; | 36 | struct usbmuxd_device device; |
| 36 | char padding[222]; | 37 | char padding[222]; |
| 37 | } __attribute__((__packed__)); | 38 | } __attribute__((__packed__)); |
| 38 | 39 | ||
