diff options
Diffstat (limited to 'usbmuxd/device.c')
| -rw-r--r-- | usbmuxd/device.c | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/usbmuxd/device.c b/usbmuxd/device.c index cddf718..3a5883c 100644 --- a/usbmuxd/device.c +++ b/usbmuxd/device.c | |||
| @@ -142,7 +142,7 @@ static int send_packet(struct mux_device *dev, enum mux_protocol proto, void *he | |||
| 142 | unsigned char *buffer; | 142 | unsigned char *buffer; |
| 143 | int hdrlen; | 143 | int hdrlen; |
| 144 | int res; | 144 | int res; |
| 145 | 145 | ||
| 146 | switch(proto) { | 146 | switch(proto) { |
| 147 | case MUX_PROTO_VERSION: | 147 | case MUX_PROTO_VERSION: |
| 148 | hdrlen = sizeof(struct version_header); | 148 | hdrlen = sizeof(struct version_header); |
| @@ -155,14 +155,14 @@ static int send_packet(struct mux_device *dev, enum mux_protocol proto, void *he | |||
| 155 | return -1; | 155 | return -1; |
| 156 | } | 156 | } |
| 157 | usbmuxd_log(LL_SPEW, "send_packet(%d, 0x%x, %p, %p, %d)", dev->id, proto, header, data, length); | 157 | usbmuxd_log(LL_SPEW, "send_packet(%d, 0x%x, %p, %p, %d)", dev->id, proto, header, data, length); |
| 158 | 158 | ||
| 159 | int total = sizeof(struct mux_header) + hdrlen + length; | 159 | int total = sizeof(struct mux_header) + hdrlen + length; |
| 160 | 160 | ||
| 161 | if(total > USB_MTU) { | 161 | if(total > USB_MTU) { |
| 162 | usbmuxd_log(LL_ERROR, "Tried to send packet larger than USB MTU (hdr %d data %d total %d) to device %d", hdrlen, length, total, dev->id); | 162 | usbmuxd_log(LL_ERROR, "Tried to send packet larger than USB MTU (hdr %d data %d total %d) to device %d", hdrlen, length, total, dev->id); |
| 163 | return -1; | 163 | return -1; |
| 164 | } | 164 | } |
| 165 | 165 | ||
| 166 | buffer = malloc(total); | 166 | buffer = malloc(total); |
| 167 | struct mux_header *mhdr = (struct mux_header *)buffer; | 167 | struct mux_header *mhdr = (struct mux_header *)buffer; |
| 168 | mhdr->protocol = htonl(proto); | 168 | mhdr->protocol = htonl(proto); |
| @@ -170,7 +170,7 @@ static int send_packet(struct mux_device *dev, enum mux_protocol proto, void *he | |||
| 170 | memcpy(buffer + sizeof(struct mux_header), header, hdrlen); | 170 | memcpy(buffer + sizeof(struct mux_header), header, hdrlen); |
| 171 | if(data && length) | 171 | if(data && length) |
| 172 | memcpy(buffer + sizeof(struct mux_header) + hdrlen, data, length); | 172 | memcpy(buffer + sizeof(struct mux_header) + hdrlen, data, length); |
| 173 | 173 | ||
| 174 | if((res = usb_send(dev->usbdev, buffer, total)) < 0) { | 174 | if((res = usb_send(dev->usbdev, buffer, total)) < 0) { |
| 175 | usbmuxd_log(LL_ERROR, "usb_send failed while sending packet (len %d) to device %d: %d", total, dev->id, res); | 175 | usbmuxd_log(LL_ERROR, "usb_send failed while sending packet (len %d) to device %d: %d", total, dev->id, res); |
| 176 | free(buffer); | 176 | free(buffer); |
| @@ -183,7 +183,7 @@ static uint16_t find_sport(struct mux_device *dev) | |||
| 183 | { | 183 | { |
| 184 | if(collection_count(&dev->connections) >= 65535) | 184 | if(collection_count(&dev->connections) >= 65535) |
| 185 | return 0; //insanity | 185 | return 0; //insanity |
| 186 | 186 | ||
| 187 | while(1) { | 187 | while(1) { |
| 188 | int ok = 1; | 188 | int ok = 1; |
| 189 | FOREACH(struct mux_connection *conn, &dev->connections) { | 189 | FOREACH(struct mux_connection *conn, &dev->connections) { |
| @@ -207,7 +207,7 @@ static int send_anon_rst(struct mux_device *dev, uint16_t sport, uint16_t dport, | |||
| 207 | th.th_ack = htonl(ack); | 207 | th.th_ack = htonl(ack); |
| 208 | th.th_flags = TH_RST; | 208 | th.th_flags = TH_RST; |
| 209 | th.th_off = sizeof(th) / 4; | 209 | th.th_off = sizeof(th) / 4; |
| 210 | 210 | ||
| 211 | usbmuxd_log(LL_DEBUG, "[OUT] dev=%d sport=%d dport=%d flags=0x%x", dev->id, sport, dport, th.th_flags); | 211 | usbmuxd_log(LL_DEBUG, "[OUT] dev=%d sport=%d dport=%d flags=0x%x", dev->id, sport, dport, th.th_flags); |
| 212 | 212 | ||
| 213 | int res = send_packet(dev, MUX_PROTO_TCP, &th, NULL, 0); | 213 | int res = send_packet(dev, MUX_PROTO_TCP, &th, NULL, 0); |
| @@ -225,7 +225,7 @@ static int send_tcp(struct mux_connection *conn, uint8_t flags, const unsigned c | |||
| 225 | th.th_flags = flags; | 225 | th.th_flags = flags; |
| 226 | th.th_off = sizeof(th) / 4; | 226 | th.th_off = sizeof(th) / 4; |
| 227 | th.th_win = htons(conn->tx_win >> 8); | 227 | th.th_win = htons(conn->tx_win >> 8); |
| 228 | 228 | ||
| 229 | usbmuxd_log(LL_DEBUG, "[OUT] dev=%d sport=%d dport=%d seq=%d ack=%d flags=0x%x window=%d[%d] len=%d", | 229 | usbmuxd_log(LL_DEBUG, "[OUT] dev=%d sport=%d dport=%d seq=%d ack=%d flags=0x%x window=%d[%d] len=%d", |
| 230 | conn->dev->id, conn->sport, conn->dport, conn->tx_seq, conn->tx_ack, flags, conn->tx_win, conn->tx_win >> 8, length); | 230 | conn->dev->id, conn->sport, conn->dport, conn->tx_seq, conn->tx_ack, flags, conn->tx_win, conn->tx_win >> 8, length); |
| 231 | 231 | ||
| @@ -278,7 +278,7 @@ int device_start_connect(int device_id, uint16_t dport, struct mux_client *clien | |||
| 278 | usbmuxd_log(LL_WARNING, "Attempted to connect to nonexistent device %d", device_id); | 278 | usbmuxd_log(LL_WARNING, "Attempted to connect to nonexistent device %d", device_id); |
| 279 | return -RESULT_BADDEV; | 279 | return -RESULT_BADDEV; |
| 280 | } | 280 | } |
| 281 | 281 | ||
| 282 | uint16_t sport = find_sport(dev); | 282 | uint16_t sport = find_sport(dev); |
| 283 | if(!sport) { | 283 | if(!sport) { |
| 284 | usbmuxd_log(LL_WARNING, "Unable to allocate port for device %d", device_id); | 284 | usbmuxd_log(LL_WARNING, "Unable to allocate port for device %d", device_id); |
| @@ -288,7 +288,7 @@ int device_start_connect(int device_id, uint16_t dport, struct mux_client *clien | |||
| 288 | struct mux_connection *conn; | 288 | struct mux_connection *conn; |
| 289 | conn = malloc(sizeof(struct mux_connection)); | 289 | conn = malloc(sizeof(struct mux_connection)); |
| 290 | memset(conn, 0, sizeof(struct mux_connection)); | 290 | memset(conn, 0, sizeof(struct mux_connection)); |
| 291 | 291 | ||
| 292 | conn->dev = dev; | 292 | conn->dev = dev; |
| 293 | conn->client = client; | 293 | conn->client = client; |
| 294 | conn->state = CONN_CONNECTING; | 294 | conn->state = CONN_CONNECTING; |
| @@ -301,15 +301,15 @@ int device_start_connect(int device_id, uint16_t dport, struct mux_client *clien | |||
| 301 | conn->rx_recvd = 0; | 301 | conn->rx_recvd = 0; |
| 302 | conn->flags = 0; | 302 | conn->flags = 0; |
| 303 | conn->max_payload = USB_MTU - sizeof(struct mux_header) - sizeof(struct tcphdr); | 303 | conn->max_payload = USB_MTU - sizeof(struct mux_header) - sizeof(struct tcphdr); |
| 304 | 304 | ||
| 305 | conn->ob_buf = malloc(CONN_OUTBUF_SIZE); | 305 | conn->ob_buf = malloc(CONN_OUTBUF_SIZE); |
| 306 | conn->ob_capacity = CONN_OUTBUF_SIZE; | 306 | conn->ob_capacity = CONN_OUTBUF_SIZE; |
| 307 | conn->ib_buf = malloc(CONN_INBUF_SIZE); | 307 | conn->ib_buf = malloc(CONN_INBUF_SIZE); |
| 308 | conn->ib_capacity = CONN_INBUF_SIZE; | 308 | conn->ib_capacity = CONN_INBUF_SIZE; |
| 309 | conn->ib_size = 0; | 309 | conn->ib_size = 0; |
| 310 | 310 | ||
| 311 | int res; | 311 | int res; |
| 312 | 312 | ||
| 313 | res = send_tcp(conn, TH_SYN, NULL, 0); | 313 | res = send_tcp(conn, TH_SYN, NULL, 0); |
| 314 | if(res < 0) { | 314 | if(res < 0) { |
| 315 | usbmuxd_log(LL_ERROR, "Error sending TCP SYN to device %d (%d->%d)", dev->id, sport, dport); | 315 | usbmuxd_log(LL_ERROR, "Error sending TCP SYN to device %d (%d->%d)", dev->id, sport, dport); |
| @@ -323,12 +323,12 @@ int device_start_connect(int device_id, uint16_t dport, struct mux_client *clien | |||
| 323 | static void update_connection(struct mux_connection *conn) | 323 | static void update_connection(struct mux_connection *conn) |
| 324 | { | 324 | { |
| 325 | conn->sendable = conn->rx_win - (conn->tx_seq - conn->rx_ack); | 325 | conn->sendable = conn->rx_win - (conn->tx_seq - conn->rx_ack); |
| 326 | 326 | ||
| 327 | if(conn->sendable > conn->ob_capacity) | 327 | if(conn->sendable > conn->ob_capacity) |
| 328 | conn->sendable = conn->ob_capacity; | 328 | conn->sendable = conn->ob_capacity; |
| 329 | if(conn->sendable > conn->max_payload) | 329 | if(conn->sendable > conn->max_payload) |
| 330 | conn->sendable = conn->max_payload; | 330 | conn->sendable = conn->max_payload; |
| 331 | 331 | ||
| 332 | if(conn->sendable > 0) | 332 | if(conn->sendable > 0) |
| 333 | conn->events |= POLLIN; | 333 | conn->events |= POLLIN; |
| 334 | else | 334 | else |
| @@ -368,7 +368,7 @@ void device_client_process(int device_id, struct mux_client *client, short event | |||
| 368 | return; | 368 | return; |
| 369 | } | 369 | } |
| 370 | usbmuxd_log(LL_SPEW, "device_client_process (%d)", events); | 370 | usbmuxd_log(LL_SPEW, "device_client_process (%d)", events); |
| 371 | 371 | ||
| 372 | int res; | 372 | int res; |
| 373 | int size; | 373 | int size; |
| 374 | if(events & POLLOUT) { | 374 | if(events & POLLOUT) { |
| @@ -463,7 +463,7 @@ static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned | |||
| 463 | { | 463 | { |
| 464 | usbmuxd_log(LL_DEBUG, "[IN] dev=%d sport=%d dport=%d seq=%d ack=%d flags=0x%x window=%d[%d] len=%d", | 464 | usbmuxd_log(LL_DEBUG, "[IN] dev=%d sport=%d dport=%d seq=%d ack=%d flags=0x%x window=%d[%d] len=%d", |
| 465 | dev->id, ntohs(th->th_sport), ntohs(th->th_dport), ntohl(th->th_seq), ntohl(th->th_ack), th->th_flags, ntohs(th->th_win) << 8, ntohs(th->th_win), payload_length); | 465 | dev->id, ntohs(th->th_sport), ntohs(th->th_dport), ntohl(th->th_seq), ntohl(th->th_ack), th->th_flags, ntohs(th->th_win) << 8, ntohs(th->th_win), payload_length); |
| 466 | 466 | ||
| 467 | uint16_t sport = ntohs(th->th_dport); | 467 | uint16_t sport = ntohs(th->th_dport); |
| 468 | uint16_t dport = ntohs(th->th_sport); | 468 | uint16_t dport = ntohs(th->th_sport); |
| 469 | struct mux_connection *conn = NULL; | 469 | struct mux_connection *conn = NULL; |
| @@ -473,7 +473,7 @@ static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned | |||
| 473 | break; | 473 | break; |
| 474 | } | 474 | } |
| 475 | } ENDFOREACH | 475 | } ENDFOREACH |
| 476 | 476 | ||
| 477 | if(!conn) { | 477 | if(!conn) { |
| 478 | usbmuxd_log(LL_WARNING, "No connection for device %d incoming packet %d->%d", dev->id, dport, sport); | 478 | usbmuxd_log(LL_WARNING, "No connection for device %d incoming packet %d->%d", dev->id, dport, sport); |
| 479 | if(!(th->th_flags & TH_RST)) { | 479 | if(!(th->th_flags & TH_RST)) { |
| @@ -482,11 +482,11 @@ static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned | |||
| 482 | } | 482 | } |
| 483 | return; | 483 | return; |
| 484 | } | 484 | } |
| 485 | 485 | ||
| 486 | conn->rx_seq = ntohl(th->th_seq); | 486 | conn->rx_seq = ntohl(th->th_seq); |
| 487 | conn->rx_ack = ntohl(th->th_ack); | 487 | conn->rx_ack = ntohl(th->th_ack); |
| 488 | conn->rx_win = ntohs(th->th_win) << 8; | 488 | conn->rx_win = ntohs(th->th_win) << 8; |
| 489 | 489 | ||
| 490 | if(th->th_flags & TH_RST) { | 490 | if(th->th_flags & TH_RST) { |
| 491 | char *buf = malloc(payload_length+1); | 491 | char *buf = malloc(payload_length+1); |
| 492 | memcpy(buf, payload, payload_length); | 492 | memcpy(buf, payload, payload_length); |
| @@ -496,7 +496,7 @@ static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned | |||
| 496 | usbmuxd_log(LL_DEBUG, "RST reason: %s", buf); | 496 | usbmuxd_log(LL_DEBUG, "RST reason: %s", buf); |
| 497 | free(buf); | 497 | free(buf); |
| 498 | } | 498 | } |
| 499 | 499 | ||
| 500 | if(conn->state == CONN_CONNECTING) { | 500 | if(conn->state == CONN_CONNECTING) { |
| 501 | if(th->th_flags != (TH_SYN|TH_ACK)) { | 501 | if(th->th_flags != (TH_SYN|TH_ACK)) { |
| 502 | if(th->th_flags & TH_RST) | 502 | if(th->th_flags & TH_RST) |
| @@ -544,10 +544,10 @@ void device_data_input(struct usb_device *usbdev, unsigned char *buffer, int len | |||
| 544 | usbmuxd_log(LL_WARNING, "Cannot find device entry for RX input from USB device %p on location 0x%x", usbdev, usb_get_location(usbdev)); | 544 | usbmuxd_log(LL_WARNING, "Cannot find device entry for RX input from USB device %p on location 0x%x", usbdev, usb_get_location(usbdev)); |
| 545 | return; | 545 | return; |
| 546 | } | 546 | } |
| 547 | 547 | ||
| 548 | if(!length) | 548 | if(!length) |
| 549 | return; | 549 | return; |
| 550 | 550 | ||
| 551 | usbmuxd_log(LL_SPEW, "Mux data input for device %p: %p len %d", dev, buffer, length); | 551 | usbmuxd_log(LL_SPEW, "Mux data input for device %p: %p len %d", dev, buffer, length); |
| 552 | 552 | ||
| 553 | // handle broken up transfers | 553 | // handle broken up transfers |
| @@ -573,18 +573,18 @@ void device_data_input(struct usb_device *usbdev, unsigned char *buffer, int len | |||
| 573 | return; | 573 | return; |
| 574 | } | 574 | } |
| 575 | } | 575 | } |
| 576 | 576 | ||
| 577 | struct mux_header *mhdr = (struct mux_header *)buffer; | 577 | struct mux_header *mhdr = (struct mux_header *)buffer; |
| 578 | 578 | ||
| 579 | if(ntohl(mhdr->length) != length) { | 579 | if(ntohl(mhdr->length) != length) { |
| 580 | usbmuxd_log(LL_ERROR, "Incoming packet size mismatch (dev %d, expected %d, got %d)", dev->id, ntohl(mhdr->length), length); | 580 | usbmuxd_log(LL_ERROR, "Incoming packet size mismatch (dev %d, expected %d, got %d)", dev->id, ntohl(mhdr->length), length); |
| 581 | return; | 581 | return; |
| 582 | } | 582 | } |
| 583 | 583 | ||
| 584 | struct tcphdr *th; | 584 | struct tcphdr *th; |
| 585 | unsigned char *payload; | 585 | unsigned char *payload; |
| 586 | int payload_length; | 586 | int payload_length; |
| 587 | 587 | ||
| 588 | switch(ntohl(mhdr->protocol)) { | 588 | switch(ntohl(mhdr->protocol)) { |
| 589 | case MUX_PROTO_VERSION: | 589 | case MUX_PROTO_VERSION: |
| 590 | device_version_input(dev, (struct version_header *)(mhdr+1)); | 590 | device_version_input(dev, (struct version_header *)(mhdr+1)); |
| @@ -599,7 +599,7 @@ void device_data_input(struct usb_device *usbdev, unsigned char *buffer, int len | |||
| 599 | usbmuxd_log(LL_ERROR, "Incoming packet for device %d has unknown protocol 0x%x)", dev->id, ntohl(mhdr->protocol)); | 599 | usbmuxd_log(LL_ERROR, "Incoming packet for device %d has unknown protocol 0x%x)", dev->id, ntohl(mhdr->protocol)); |
| 600 | break; | 600 | break; |
| 601 | } | 601 | } |
| 602 | 602 | ||
| 603 | } | 603 | } |
| 604 | 604 | ||
| 605 | int device_add(struct usb_device *usbdev) | 605 | int device_add(struct usb_device *usbdev) |
