summaryrefslogtreecommitdiffstats
path: root/usbmux.c
diff options
context:
space:
mode:
Diffstat (limited to 'usbmux.c')
-rw-r--r--usbmux.c325
1 files changed, 202 insertions, 123 deletions
diff --git a/usbmux.c b/usbmux.c
index 0a175ed..106c5ff 100644
--- a/usbmux.c
+++ b/usbmux.c
@@ -50,7 +50,7 @@ static const uint32_t WINDOW_MAX = 5 * 1024;
50static const uint32_t WINDOW_INCREMENT = 512; 50static const uint32_t WINDOW_INCREMENT = 512;
51 51
52typedef struct { 52typedef struct {
53 char* buffer; 53 char *buffer;
54 int leftover; 54 int leftover;
55 int capacity; 55 int capacity;
56} receivebuf_t; 56} receivebuf_t;
@@ -145,20 +145,20 @@ static void print_buffer(const char *data, const int length)
145 int j; 145 int j;
146 unsigned char c; 146 unsigned char c;
147 147
148 for(i=0; i<length; i+=16) { 148 for (i = 0; i < length; i += 16) {
149 printf("%04x: ", i); 149 printf("%04x: ", i);
150 for (j=0;j<16;j++) { 150 for (j = 0; j < 16; j++) {
151 if (i+j >= length) { 151 if (i + j >= length) {
152 printf(" "); 152 printf(" ");
153 continue; 153 continue;
154 } 154 }
155 printf("%02hhx ", *(data+i+j)); 155 printf("%02hhx ", *(data + i + j));
156 } 156 }
157 printf(" | "); 157 printf(" | ");
158 for(j=0;j<16;j++) { 158 for (j = 0; j < 16; j++) {
159 if (i+j >= length) 159 if (i + j >= length)
160 break; 160 break;
161 c = *(data+i+j); 161 c = *(data + i + j);
162 if ((c < 32) || (c > 127)) { 162 if ((c < 32) || (c > 127)) {
163 printf("."); 163 printf(".");
164 continue; 164 continue;
@@ -171,7 +171,7 @@ static void print_buffer(const char *data, const int length)
171} 171}
172#endif 172#endif
173 173
174void hton_header(usbmux_tcp_header *hdr) 174void hton_header(usbmux_tcp_header * hdr)
175{ 175{
176 if (hdr) { 176 if (hdr) {
177 hdr->length = htonl(hdr->length); 177 hdr->length = htonl(hdr->length);
@@ -181,7 +181,7 @@ void hton_header(usbmux_tcp_header *hdr)
181 } 181 }
182} 182}
183 183
184void ntoh_header(usbmux_tcp_header *hdr) 184void ntoh_header(usbmux_tcp_header * hdr)
185{ 185{
186 if (hdr) { 186 if (hdr) {
187 hdr->length = ntohl(hdr->length); 187 hdr->length = ntohl(hdr->length);
@@ -197,7 +197,8 @@ void ntoh_header(usbmux_tcp_header *hdr)
197 */ 197 */
198usbmux_version_header *version_header() 198usbmux_version_header *version_header()
199{ 199{
200 usbmux_version_header *version = (usbmux_version_header *) malloc(sizeof(usbmux_version_header)); 200 usbmux_version_header *version =
201 (usbmux_version_header *) malloc(sizeof(usbmux_version_header));
201 version->type = 0; 202 version->type = 0;
202 version->length = htonl(20); 203 version->length = htonl(20);
203 version->major = htonl(1); 204 version->major = htonl(1);
@@ -222,25 +223,34 @@ static int usbmux_config_usb_device(usbmux_device_t device)
222#if 0 223#if 0
223 log_debug_msg("checking configuration...\n"); 224 log_debug_msg("checking configuration...\n");
224 if (device->__device->config->bConfigurationValue != 3) { 225 if (device->__device->config->bConfigurationValue != 3) {
225 log_debug_msg("WARNING: usb device configuration is not 3 as expected!\n"); 226 log_debug_msg
227 ("WARNING: usb device configuration is not 3 as expected!\n");
226 } 228 }
227 229
228 log_debug_msg("setting configuration...\n"); 230 log_debug_msg("setting configuration...\n");
229 ret = usb_set_configuration(device->device, 3); 231 ret = usb_set_configuration(device->device, 3);
230 if (ret != 0) { 232 if (ret != 0) {
231 log_debug_msg("Hm, usb_set_configuration returned %d: %s\n", ret, strerror(-ret)); 233 log_debug_msg("Hm, usb_set_configuration returned %d: %s\n", ret,
234 strerror(-ret));
232#if LIBUSB_HAS_GET_DRIVER_NP 235#if LIBUSB_HAS_GET_DRIVER_NP
233 log_debug_msg("trying to fix:\n"); 236 log_debug_msg("trying to fix:\n");
234 log_debug_msg("-> detaching kernel driver... "); 237 log_debug_msg("-> detaching kernel driver... ");
235 ret = usb_detach_kernel_driver_np(device->device, device->__device->config->interface->altsetting->bInterfaceNumber); 238 ret =
239 usb_detach_kernel_driver_np(device->device,
240 device->__device->config->
241 interface->altsetting->
242 bInterfaceNumber);
236 if (ret != 0) { 243 if (ret != 0) {
237 log_debug_msg("usb_detach_kernel_driver_np returned %d: %s\n", ret, strerror(-ret)); 244 log_debug_msg("usb_detach_kernel_driver_np returned %d: %s\n",
245 ret, strerror(-ret));
238 } else { 246 } else {
239 log_debug_msg("done.\n"); 247 log_debug_msg("done.\n");
240 log_debug_msg("setting configuration again... "); 248 log_debug_msg("setting configuration again... ");
241 ret = usb_set_configuration(device->device, 3); 249 ret = usb_set_configuration(device->device, 3);
242 if (ret != 0) { 250 if (ret != 0) {
243 log_debug_msg("Error: usb_set_configuration returned %d: %s\n", ret, strerror(-ret)); 251 log_debug_msg
252 ("Error: usb_set_configuration returned %d: %s\n", ret,
253 strerror(-ret));
244 log_debug_msg("--> trying to continue anyway...\n"); 254 log_debug_msg("--> trying to continue anyway...\n");
245 } else { 255 } else {
246 log_debug_msg("done.\n"); 256 log_debug_msg("done.\n");
@@ -257,7 +267,8 @@ static int usbmux_config_usb_device(usbmux_device_t device)
257 log_debug_msg("claiming interface... "); 267 log_debug_msg("claiming interface... ");
258 ret = usb_claim_interface(device->usbdev, 1); 268 ret = usb_claim_interface(device->usbdev, 1);
259 if (ret != 0) { 269 if (ret != 0) {
260 log_debug_msg("Error: usb_claim_interface returned %d: %s\n", ret, strerror(-ret)); 270 log_debug_msg("Error: usb_claim_interface returned %d: %s\n", ret,
271 strerror(-ret));
261 return -ENODEV; 272 return -ENODEV;
262 } else { 273 } else {
263 log_debug_msg("done.\n"); 274 log_debug_msg("done.\n");
@@ -266,7 +277,7 @@ static int usbmux_config_usb_device(usbmux_device_t device)
266 do { 277 do {
267 bytes = usb_bulk_read(device->usbdev, BULKIN, buf, 512, 800); 278 bytes = usb_bulk_read(device->usbdev, BULKIN, buf, 512, 800);
268 } while (bytes > 0); 279 } while (bytes > 0);
269 280
270 return 0; 281 return 0;
271} 282}
272 283
@@ -283,7 +294,8 @@ static int usbmux_config_usb_device(usbmux_device_t device)
283 * descriptor on return. 294 * descriptor on return.
284 * @return 0 if ok, otherwise a negative errno value. 295 * @return 0 if ok, otherwise a negative errno value.
285 */ 296 */
286int usbmux_get_specific_device(int bus_n, int dev_n, usbmux_device_t * device) 297int usbmux_get_specific_device(int bus_n, int dev_n,
298 usbmux_device_t * device)
287{ 299{
288 struct usb_bus *bus; 300 struct usb_bus *bus;
289 struct usb_device *dev; 301 struct usb_device *dev;
@@ -294,7 +306,8 @@ int usbmux_get_specific_device(int bus_n, int dev_n, usbmux_device_t * device)
294 if (!device || (device && *device)) 306 if (!device || (device && *device))
295 return -EINVAL; 307 return -EINVAL;
296 308
297 usbmux_device_t newdevice = (usbmux_device_t) malloc(sizeof(struct usbmux_device_int)); 309 usbmux_device_t newdevice =
310 (usbmux_device_t) malloc(sizeof(struct usbmux_device_int));
298 311
299 // Initialize the struct 312 // Initialize the struct
300 newdevice->usbdev = NULL; 313 newdevice->usbdev = NULL;
@@ -314,40 +327,49 @@ int usbmux_get_specific_device(int bus_n, int dev_n, usbmux_device_t * device)
314 // Set the device configuration 327 // Set the device configuration
315 for (bus = usb_get_busses(); bus; bus = bus->next) 328 for (bus = usb_get_busses(); bus; bus = bus->next)
316 //if (bus->location == bus_n) 329 //if (bus->location == bus_n)
317 for (dev = bus->devices; dev != NULL; dev = dev->next) 330 for (dev = bus->devices; dev != NULL; dev = dev->next)
318 if (dev->devnum == dev_n) { 331 if (dev->devnum == dev_n) {
319 newdevice->__device = dev; 332 newdevice->__device = dev;
320 newdevice->usbdev = usb_open(newdevice->__device); 333 newdevice->usbdev = usb_open(newdevice->__device);
321 if (usbmux_config_usb_device(newdevice) == 0) { 334 if (usbmux_config_usb_device(newdevice) == 0) {
322 goto found; 335 goto found;
323 }
324 } 336 }
337 }
325 338
326 usbmux_free_device(newdevice); 339 usbmux_free_device(newdevice);
327 340
328 log_debug_msg("usbmux_get_specific_device: device not found\n"); 341 log_debug_msg("usbmux_get_specific_device: device not found\n");
329 return -ENODEV; 342 return -ENODEV;
330 343
331found: 344 found:
332 // Send the version command to the device 345 // Send the version command to the device
333 version = version_header(); 346 version = version_header();
334 bytes = usb_bulk_write(newdevice->usbdev, BULKOUT, (char *) version, sizeof(*version), 800); 347 bytes =
348 usb_bulk_write(newdevice->usbdev, BULKOUT, (char *) version,
349 sizeof(*version), 800);
335 if (bytes < 20) { 350 if (bytes < 20) {
336 log_debug_msg("%s: libusb did NOT send enough!\n", __func__); 351 log_debug_msg("%s: libusb did NOT send enough!\n", __func__);
337 if (bytes < 0) { 352 if (bytes < 0) {
338 log_debug_msg("%s: libusb gave me the error %d: %s (%s)\n", __func__, bytes, usb_strerror(), strerror(-bytes)); 353 log_debug_msg("%s: libusb gave me the error %d: %s (%s)\n",
354 __func__, bytes, usb_strerror(),
355 strerror(-bytes));
339 } 356 }
340 } 357 }
341 // Read the device's response 358 // Read the device's response
342 bytes = usb_bulk_read(newdevice->usbdev, BULKIN, (char *) version, sizeof(*version), 800); 359 bytes =
360 usb_bulk_read(newdevice->usbdev, BULKIN, (char *) version,
361 sizeof(*version), 800);
343 362
344 // Check for bad response 363 // Check for bad response
345 if (bytes < 20) { 364 if (bytes < 20) {
346 free(version); 365 free(version);
347 usbmux_free_device(newdevice); 366 usbmux_free_device(newdevice);
348 log_debug_msg("%s: Invalid version message -- header too short.\n", __func__); 367 log_debug_msg("%s: Invalid version message -- header too short.\n",
368 __func__);
349 if (bytes < 0) { 369 if (bytes < 0) {
350 log_debug_msg("%s: libusb error message %d: %s (%s)\n", __func__, bytes, usb_strerror(), strerror(-bytes)); 370 log_debug_msg("%s: libusb error message %d: %s (%s)\n",
371 __func__, bytes, usb_strerror(),
372 strerror(-bytes));
351 return bytes; 373 return bytes;
352 } 374 }
353 return -EBADMSG; 375 return -EBADMSG;
@@ -363,7 +385,8 @@ found:
363 // Bad header 385 // Bad header
364 usbmux_free_device(newdevice); 386 usbmux_free_device(newdevice);
365 free(version); 387 free(version);
366 log_debug_msg("%s: Received a bad header/invalid version number.", __func__); 388 log_debug_msg("%s: Received a bad header/invalid version number.",
389 __func__);
367 return -EBADMSG; 390 return -EBADMSG;
368 } 391 }
369 392
@@ -371,7 +394,7 @@ found:
371 log_debug_msg("%s: Unknown error.\n", __func__); 394 log_debug_msg("%s: Unknown error.\n", __func__);
372 usbmux_free_device(newdevice); 395 usbmux_free_device(newdevice);
373 free(version); 396 free(version);
374 return -EBADMSG; // if it got to this point it's gotta be bad 397 return -EBADMSG; // if it got to this point it's gotta be bad
375} 398}
376 399
377/** Cleans up an usbmux_device_t structure, then frees the structure itself. 400/** Cleans up an usbmux_device_t structure, then frees the structure itself.
@@ -435,25 +458,31 @@ int send_to_device(usbmux_device_t device, char *data, int datalen)
435 int bytes = 0; 458 int bytes = 0;
436 459
437#ifdef DEBUG 460#ifdef DEBUG
438 #ifdef DEBUG_MORE 461#ifdef DEBUG_MORE
439 printf("===============================\n%s: trying to send\n", __func__); 462 printf("===============================\n%s: trying to send\n",
463 __func__);
440 print_buffer(data, datalen); 464 print_buffer(data, datalen);
441 printf("===============================\n"); 465 printf("===============================\n");
442 #endif 466#endif
443#endif 467#endif
444 do { 468 do {
445 if (retrycount > 3) { 469 if (retrycount > 3) {
446 log_debug_msg("EPIC FAIL! aborting on retry count overload.\n"); 470 log_debug_msg
471 ("EPIC FAIL! aborting on retry count overload.\n");
447 return -ECOMM; 472 return -ECOMM;
448 } 473 }
449 474
450 bytes = usb_bulk_write(device->usbdev, BULKOUT, data, datalen, timeout); 475 bytes =
476 usb_bulk_write(device->usbdev, BULKOUT, data, datalen,
477 timeout);
451 if (bytes == -ETIMEDOUT) { 478 if (bytes == -ETIMEDOUT) {
452 // timed out waiting for write. 479 // timed out waiting for write.
453 log_debug_msg("usb_bulk_write timeout error.\n"); 480 log_debug_msg("usb_bulk_write timeout error.\n");
454 return bytes; 481 return bytes;
455 } else if (bytes < 0) { 482 } else if (bytes < 0) {
456 log_debug_msg("usb_bulk_write failed with error. err:%d (%s)(%s)\n", bytes, usb_strerror(), strerror(-bytes)); 483 log_debug_msg
484 ("usb_bulk_write failed with error. err:%d (%s)(%s)\n",
485 bytes, usb_strerror(), strerror(-bytes));
457 return bytes; 486 return bytes;
458 } else if (bytes == 0) { 487 } else if (bytes == 0) {
459 log_debug_msg("usb_bulk_write sent nothing. retrying.\n"); 488 log_debug_msg("usb_bulk_write sent nothing. retrying.\n");
@@ -461,22 +490,24 @@ int send_to_device(usbmux_device_t device, char *data, int datalen)
461 retrycount++; 490 retrycount++;
462 continue; 491 continue;
463 } else if (bytes < datalen) { 492 } else if (bytes < datalen) {
464 log_debug_msg("usb_bulk_write failed to send full dataload. %d of %d\n", bytes, datalen); 493 log_debug_msg
494 ("usb_bulk_write failed to send full dataload. %d of %d\n",
495 bytes, datalen);
465 timeout = timeout * 4; 496 timeout = timeout * 4;
466 retrycount++; 497 retrycount++;
467 data += bytes; 498 data += bytes;
468 datalen -= bytes; 499 datalen -= bytes;
469 continue; 500 continue;
470 } 501 }
471 } while(0); // fall out 502 } while (0); // fall out
472 503
473#ifdef DEBUG 504#ifdef DEBUG
474 if (bytes > 0) { 505 if (bytes > 0) {
475 if (toto_debug > 0) { 506 if (toto_debug > 0) {
476 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); 507 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
477 printf("%s: sent to device\n", __func__); 508 printf("%s: sent to device\n", __func__);
478 print_buffer(data, bytes); 509 print_buffer(data, bytes);
479 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); 510 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
480 } 511 }
481 } 512 }
482#endif 513#endif
@@ -493,13 +524,16 @@ int send_to_device(usbmux_device_t device, char *data, int datalen)
493 * 524 *
494 * @return How many bytes were read in, or -1 on error. 525 * @return How many bytes were read in, or -1 on error.
495 */ 526 */
496int recv_from_device_timeout(usbmux_device_t device, char *data, int datalen, int timeoutmillis) 527int recv_from_device_timeout(usbmux_device_t device, char *data,
528 int datalen, int timeoutmillis)
497{ 529{
498 if (!device) 530 if (!device)
499 return -EINVAL; 531 return -EINVAL;
500 //log_debug_msg("%s: attempting to receive %i bytes\n", __func__, datalen); 532 //log_debug_msg("%s: attempting to receive %i bytes\n", __func__, datalen);
501 533
502 int bytes = usb_bulk_read(device->usbdev, BULKIN, data, datalen, timeoutmillis); 534 int bytes =
535 usb_bulk_read(device->usbdev, BULKIN, data, datalen,
536 timeoutmillis);
503 // There are some things which are errors, others which are no problem. 537 // There are some things which are errors, others which are no problem.
504 // It's not documented in libUSB, but it seems that the error values 538 // It's not documented in libUSB, but it seems that the error values
505 // returned are just negated ERRNO values. 539 // returned are just negated ERRNO values.
@@ -509,12 +543,14 @@ int recv_from_device_timeout(usbmux_device_t device, char *data, int datalen, in
509 // picked up any data. no problem. 543 // picked up any data. no problem.
510 return 0; 544 return 0;
511 } else { 545 } else {
512 fprintf(stderr, "%s: libusb gave me the error %d: %s (%s)\n", __func__, bytes, usb_strerror(), strerror(-bytes)); 546 fprintf(stderr, "%s: libusb gave me the error %d: %s (%s)\n",
513 log_debug_msg("%s: libusb gave me the error %d: %s (%s)\n", __func__, bytes, usb_strerror(), strerror(-bytes)); 547 __func__, bytes, usb_strerror(), strerror(-bytes));
548 log_debug_msg("%s: libusb gave me the error %d: %s (%s)\n",
549 __func__, bytes, usb_strerror(),
550 strerror(-bytes));
514 } 551 }
515 return bytes; 552 return bytes;
516 } 553 }
517
518#ifdef DEBUG 554#ifdef DEBUG
519 if (bytes > 0) { 555 if (bytes > 0) {
520 if (toto_debug > 0) { 556 if (toto_debug > 0) {
@@ -538,7 +574,8 @@ int recv_from_device_timeout(usbmux_device_t device, char *data, int datalen, in
538 */ 574 */
539usbmux_tcp_header *new_mux_packet(uint16_t s_port, uint16_t d_port) 575usbmux_tcp_header *new_mux_packet(uint16_t s_port, uint16_t d_port)
540{ 576{
541 usbmux_tcp_header *conn = (usbmux_tcp_header *) malloc(sizeof(usbmux_tcp_header)); 577 usbmux_tcp_header *conn =
578 (usbmux_tcp_header *) malloc(sizeof(usbmux_tcp_header));
542 conn->type = htonl(6); 579 conn->type = htonl(6);
543 conn->length = HEADERLEN; 580 conn->length = HEADERLEN;
544 conn->sport = htons(s_port); 581 conn->sport = htons(s_port);
@@ -566,7 +603,9 @@ static void delete_connection(usbmux_client_t connection)
566 603
567 // update the global list of connections 604 // update the global list of connections
568 if (clients > 1) { 605 if (clients > 1) {
569 newlist = (usbmux_client_t *) malloc(sizeof(usbmux_client_t) * (clients - 1)); 606 newlist =
607 (usbmux_client_t *) malloc(sizeof(usbmux_client_t) *
608 (clients - 1));
570 int i = 0, j = 0; 609 int i = 0, j = 0;
571 for (i = 0; i < clients; i++) { 610 for (i = 0; i < clients; i++) {
572 if (connlist[i] == connection) 611 if (connlist[i] == connection)
@@ -607,7 +646,9 @@ static void add_connection(usbmux_client_t connection)
607{ 646{
608 pthread_mutex_lock(&usbmuxmutex); 647 pthread_mutex_lock(&usbmuxmutex);
609 usbmux_client_t *newlist = 648 usbmux_client_t *newlist =
610 (usbmux_client_t *) realloc(connlist, sizeof(usbmux_client_t) * (clients + 1)); 649 (usbmux_client_t *) realloc(connlist,
650 sizeof(usbmux_client_t) * (clients +
651 1));
611 newlist[clients] = connection; 652 newlist[clients] = connection;
612 connlist = newlist; 653 connlist = newlist;
613 clients++; 654 clients++;
@@ -661,7 +702,8 @@ static uint16_t get_free_port()
661 * @param client A mux TCP header for the connection which is used for tracking and data transfer. 702 * @param client A mux TCP header for the connection which is used for tracking and data transfer.
662 * @return 0 on success, a negative errno value otherwise. 703 * @return 0 on success, a negative errno value otherwise.
663 */ 704 */
664int usbmux_new_client(usbmux_device_t device, uint16_t src_port, uint16_t dst_port, usbmux_client_t * client) 705int usbmux_new_client(usbmux_device_t device, uint16_t src_port,
706 uint16_t dst_port, usbmux_client_t * client)
665{ 707{
666 if (!device || !dst_port) 708 if (!device || !dst_port)
667 return -EINVAL; 709 return -EINVAL;
@@ -670,11 +712,11 @@ int usbmux_new_client(usbmux_device_t device, uint16_t src_port, uint16_t dst_po
670 712
671 if (!src_port) { 713 if (!src_port) {
672 // this is a special case, if we get 0, this is not good, so 714 // this is a special case, if we get 0, this is not good, so
673 return -EISCONN; // TODO: error code suitable? 715 return -EISCONN; // TODO: error code suitable?
674 } 716 }
675
676 // Initialize connection stuff 717 // Initialize connection stuff
677 usbmux_client_t new_connection = (usbmux_client_t) malloc(sizeof(struct usbmux_client_int)); 718 usbmux_client_t new_connection =
719 (usbmux_client_t) malloc(sizeof(struct usbmux_client_int));
678 new_connection->header = new_mux_packet(src_port, dst_port); 720 new_connection->header = new_mux_packet(src_port, dst_port);
679 721
680 // send TCP syn 722 // send TCP syn
@@ -682,7 +724,8 @@ int usbmux_new_client(usbmux_device_t device, uint16_t src_port, uint16_t dst_po
682 int err = 0; 724 int err = 0;
683 new_connection->header->tcp_flags = TCP_SYN; 725 new_connection->header->tcp_flags = TCP_SYN;
684 new_connection->header->length = new_connection->header->length; 726 new_connection->header->length = new_connection->header->length;
685 new_connection->header->length16 = new_connection->header->length16; 727 new_connection->header->length16 =
728 new_connection->header->length16;
686 new_connection->header->scnt = 0; 729 new_connection->header->scnt = 0;
687 new_connection->header->ocnt = 0; 730 new_connection->header->ocnt = 0;
688 new_connection->device = device; 731 new_connection->device = device;
@@ -697,8 +740,12 @@ int usbmux_new_client(usbmux_device_t device, uint16_t src_port, uint16_t dst_po
697 new_connection->error = 0; 740 new_connection->error = 0;
698 new_connection->cleanup = 0; 741 new_connection->cleanup = 0;
699 hton_header(new_connection->header); 742 hton_header(new_connection->header);
700 log_debug_msg("%s: send_to_device (%d --> %d)\n", __func__, ntohs(new_connection->header->sport), ntohs(new_connection->header->dport)); 743 log_debug_msg("%s: send_to_device (%d --> %d)\n", __func__,
701 err = send_to_device(device, (char *) new_connection->header, sizeof(usbmux_tcp_header)); 744 ntohs(new_connection->header->sport),
745 ntohs(new_connection->header->dport));
746 err =
747 send_to_device(device, (char *) new_connection->header,
748 sizeof(usbmux_tcp_header));
702 if (err >= 0) { 749 if (err >= 0) {
703 *client = new_connection; 750 *client = new_connection;
704 return 0; 751 return 0;
@@ -732,7 +779,9 @@ int usbmux_free_client(usbmux_client_t client)
732 client->header->length16 = 0x1C; 779 client->header->length16 = 0x1C;
733 hton_header(client->header); 780 hton_header(client->header);
734 781
735 err = send_to_device(client->device, (char*)client->header, sizeof(usbmux_tcp_header)); 782 err =
783 send_to_device(client->device, (char *) client->header,
784 sizeof(usbmux_tcp_header));
736 if (err < 0) { 785 if (err < 0) {
737 log_debug_msg("%s: error sending TCP_FIN\n", __func__); 786 log_debug_msg("%s: error sending TCP_FIN\n", __func__);
738 result = err; 787 result = err;
@@ -762,7 +811,8 @@ int usbmux_free_client(usbmux_client_t client)
762 * 811 *
763 * @return 0 on success or a negative errno value on error. 812 * @return 0 on success or a negative errno value on error.
764 */ 813 */
765int usbmux_send(usbmux_client_t client, const char *data, uint32_t datalen, uint32_t * sent_bytes) 814int usbmux_send(usbmux_client_t client, const char *data, uint32_t datalen,
815 uint32_t * sent_bytes)
766{ 816{
767 if (!client->device || !client || !sent_bytes) 817 if (!client->device || !client || !sent_bytes)
768 return -EINVAL; 818 return -EINVAL;
@@ -781,7 +831,8 @@ int usbmux_send(usbmux_client_t client, const char *data, uint32_t datalen, uint
781 clock_gettime(CLOCK_REALTIME, &ts); 831 clock_gettime(CLOCK_REALTIME, &ts);
782 //ts.tv_sec += 1; 832 //ts.tv_sec += 1;
783 ts.tv_nsec += 750 * 1000; 833 ts.tv_nsec += 750 * 1000;
784 if (pthread_cond_timedwait(&client->wait, &client->mutex, &ts) == ETIMEDOUT) { 834 if (pthread_cond_timedwait(&client->wait, &client->mutex, &ts) ==
835 ETIMEDOUT) {
785 // timed out. optimistically grow the window and try to make progress 836 // timed out. optimistically grow the window and try to make progress
786 client->wr_window += WINDOW_INCREMENT; 837 client->wr_window += WINDOW_INCREMENT;
787 } 838 }
@@ -791,7 +842,7 @@ int usbmux_send(usbmux_client_t client, const char *data, uint32_t datalen, uint
791 842
792 // client->scnt and client->ocnt should already be in host notation... 843 // client->scnt and client->ocnt should already be in host notation...
793 // we don't need to change them juuuust yet. 844 // we don't need to change them juuuust yet.
794 char *buffer = (char *) malloc(blocksize + 2); // allow 2 bytes of safety padding 845 char *buffer = (char *) malloc(blocksize + 2); // allow 2 bytes of safety padding
795 // Set the length 846 // Set the length
796 client->header->length = blocksize; 847 client->header->length = blocksize;
797 client->header->length16 = blocksize; 848 client->header->length16 = blocksize;
@@ -802,7 +853,9 @@ int usbmux_send(usbmux_client_t client, const char *data, uint32_t datalen, uint
802 memcpy(buffer, client->header, sizeof(usbmux_tcp_header)); 853 memcpy(buffer, client->header, sizeof(usbmux_tcp_header));
803 memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen); 854 memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen);
804 855
805 log_debug_msg("%s: send_to_device(%d --> %d)\n", __func__, ntohs(client->header->sport), ntohs(client->header->dport)); 856 log_debug_msg("%s: send_to_device(%d --> %d)\n", __func__,
857 ntohs(client->header->sport),
858 ntohs(client->header->dport));
806 sendresult = send_to_device(client->device, buffer, blocksize); 859 sendresult = send_to_device(client->device, buffer, blocksize);
807 // Now that we've sent it off, we can clean up after our sloppy selves. 860 // Now that we've sent it off, we can clean up after our sloppy selves.
808 if (buffer) 861 if (buffer)
@@ -812,7 +865,7 @@ int usbmux_send(usbmux_client_t client, const char *data, uint32_t datalen, uint
812 ntoh_header(client->header); 865 ntoh_header(client->header);
813 866
814 // update counts ONLY if the send succeeded. 867 // update counts ONLY if the send succeeded.
815 if ((uint32_t)sendresult == blocksize) { 868 if ((uint32_t) sendresult == blocksize) {
816 // Re-calculate scnt 869 // Re-calculate scnt
817 client->header->scnt += datalen; 870 client->header->scnt += datalen;
818 client->wr_window -= blocksize; 871 client->wr_window -= blocksize;
@@ -826,12 +879,14 @@ int usbmux_send(usbmux_client_t client, const char *data, uint32_t datalen, uint
826 return -ETIMEDOUT; 879 return -ETIMEDOUT;
827 } else if (sendresult < 0) { 880 } else if (sendresult < 0) {
828 return sendresult; 881 return sendresult;
829 } else if ((uint32_t)sendresult == blocksize) { 882 } else if ((uint32_t) sendresult == blocksize) {
830 // actual number of data bytes sent. 883 // actual number of data bytes sent.
831 *sent_bytes = sendresult - HEADERLEN; 884 *sent_bytes = sendresult - HEADERLEN;
832 return 0; 885 return 0;
833 } else { 886 } else {
834 fprintf(stderr, "usbsend managed to dump a packet that is not full size. %d of %d\n", sendresult, blocksize); 887 fprintf(stderr,
888 "usbsend managed to dump a packet that is not full size. %d of %d\n",
889 sendresult, blocksize);
835 return -EBADMSG; 890 return -EBADMSG;
836 } 891 }
837} 892}
@@ -844,14 +899,15 @@ int usbmux_send(usbmux_client_t client, const char *data, uint32_t datalen, uint
844 * 899 *
845 * @return number of bytes consumed (header + data) 900 * @return number of bytes consumed (header + data)
846 */ 901 */
847uint32_t append_receive_buffer(usbmux_client_t client, char* packet) 902uint32_t append_receive_buffer(usbmux_client_t client, char *packet)
848{ 903{
849 if (client == NULL || packet == NULL) return 0; 904 if (client == NULL || packet == NULL)
905 return 0;
850 906
851 usbmux_tcp_header *header = (usbmux_tcp_header *) packet; 907 usbmux_tcp_header *header = (usbmux_tcp_header *) packet;
852 char* data = &packet[HEADERLEN]; 908 char *data = &packet[HEADERLEN];
853 uint32_t packetlen = ntohl(header->length); 909 uint32_t packetlen = ntohl(header->length);
854 uint32_t datalen = packetlen-HEADERLEN; 910 uint32_t datalen = packetlen - HEADERLEN;
855 911
856 int dobroadcast = 0; 912 int dobroadcast = 0;
857 913
@@ -860,7 +916,7 @@ uint32_t append_receive_buffer(usbmux_client_t client, char* packet)
860 // we need to handle a few corner case tasks and book-keeping which 916 // we need to handle a few corner case tasks and book-keeping which
861 // falls on our responsibility because we are the ones reading in 917 // falls on our responsibility because we are the ones reading in
862 // feedback. 918 // feedback.
863 if (client->header->scnt == 0 && client->header->ocnt == 0 ) { 919 if (client->header->scnt == 0 && client->header->ocnt == 0) {
864 log_debug_msg("client is still waiting for handshake.\n"); 920 log_debug_msg("client is still waiting for handshake.\n");
865 if (header->tcp_flags == (TCP_SYN | TCP_ACK)) { 921 if (header->tcp_flags == (TCP_SYN | TCP_ACK)) {
866 log_debug_msg("yes, got syn+ack ; replying with ack.\n"); 922 log_debug_msg("yes, got syn+ack ; replying with ack.\n");
@@ -872,9 +928,14 @@ uint32_t append_receive_buffer(usbmux_client_t client, char* packet)
872 hton_header(client->header); 928 hton_header(client->header);
873 // push it to USB 929 // push it to USB
874 // TODO: need to check for error in the send here.... :( 930 // TODO: need to check for error in the send here.... :(
875 log_debug_msg("%s: send_to_device (%d --> %d)\n", __func__, ntohs(client->header->sport), ntohs(client->header->dport)); 931 log_debug_msg("%s: send_to_device (%d --> %d)\n", __func__,
876 if (send_to_device(client->device, (char *)client->header, sizeof(usbmux_tcp_header)) <= 0) { 932 ntohs(client->header->sport),
877 log_debug_msg("%s: error when pushing to usb...\n", __func__); 933 ntohs(client->header->dport));
934 if (send_to_device
935 (client->device, (char *) client->header,
936 sizeof(usbmux_tcp_header)) <= 0) {
937 log_debug_msg("%s: error when pushing to usb...\n",
938 __func__);
878 } 939 }
879 // need to revert some of the fields back to host notation. 940 // need to revert some of the fields back to host notation.
880 ntoh_header(client->header); 941 ntoh_header(client->header);
@@ -885,7 +946,6 @@ uint32_t append_receive_buffer(usbmux_client_t client, char* packet)
885 log_debug_msg("WOAH! client failed to get proper syn+ack.\n"); 946 log_debug_msg("WOAH! client failed to get proper syn+ack.\n");
886 } 947 }
887 } 948 }
888
889 // update TCP counters and windows. 949 // update TCP counters and windows.
890 // 950 //
891 // save the window that we're getting from the USB device. 951 // save the window that we're getting from the USB device.
@@ -899,30 +959,33 @@ uint32_t append_receive_buffer(usbmux_client_t client, char* packet)
899 char e_msg[128]; 959 char e_msg[128];
900 e_msg[0] = 0; 960 e_msg[0] = 0;
901 if (datalen > 1) { 961 if (datalen > 1) {
902 memcpy(e_msg, data+1, datalen-1); 962 memcpy(e_msg, data + 1, datalen - 1);
903 e_msg[datalen-1] = 0; 963 e_msg[datalen - 1] = 0;
904 } 964 }
905 // fetch the message 965 // fetch the message
906 switch(data[0]) { 966 switch (data[0]) {
907 case 0: 967 case 0:
908 // this is not an error, it's just a status message. 968 // this is not an error, it's just a status message.
909 log_debug_msg("received status message: %s\n", e_msg); 969 log_debug_msg("received status message: %s\n", e_msg);
910 datalen = 0; 970 datalen = 0;
911 break; 971 break;
912 case 1: 972 case 1:
913 log_debug_msg("received error message: %s\n", e_msg); 973 log_debug_msg("received error message: %s\n", e_msg);
914 datalen = 0; 974 datalen = 0;
915 break; 975 break;
916 default: 976 default:
917 log_debug_msg("received unknown message (type 0x%02x): %s\n", data[0], e_msg); 977 log_debug_msg
918 //datalen = 0; // <-- we let this commented out for testing 978 ("received unknown message (type 0x%02x): %s\n",
919 break; 979 data[0], e_msg);
980 //datalen = 0; // <-- we let this commented out for testing
981 break;
920 } 982 }
921 } else { 983 } else {
922 log_debug_msg("peer sent connection reset. setting error: %d\n", client->error); 984 log_debug_msg
985 ("peer sent connection reset. setting error: %d\n",
986 client->error);
923 } 987 }
924 } 988 }
925
926 // the packet's ocnt tells us how much of our data the device has received. 989 // the packet's ocnt tells us how much of our data the device has received.
927 if (header->tcp_flags & TCP_ACK) { 990 if (header->tcp_flags & TCP_ACK) {
928 // this is a hacky magic number condition. it seems that once 991 // this is a hacky magic number condition. it seems that once
@@ -930,7 +993,7 @@ uint32_t append_receive_buffer(usbmux_client_t client, char* packet)
930 // number, we quickly fall into connection reset problems. 993 // number, we quickly fall into connection reset problems.
931 // Once we see the reported window size start falling off, 994 // Once we see the reported window size start falling off,
932 // ut off and wait for solid acks to come back. 995 // ut off and wait for solid acks to come back.
933 if (ntohs(header->window) < 256) 996 if (ntohs(header->window) < 256)
934 client->wr_window = 0; 997 client->wr_window = 0;
935 998
936 // check what just got acked. 999 // check what just got acked.
@@ -938,21 +1001,24 @@ uint32_t append_receive_buffer(usbmux_client_t client, char* packet)
938 // we got some kind of ack, but it hasn't caught up 1001 // we got some kind of ack, but it hasn't caught up
939 // with the pending that have been sent. 1002 // with the pending that have been sent.
940 pthread_cond_broadcast(&client->wr_wait); 1003 pthread_cond_broadcast(&client->wr_wait);
941 } else if (ntohl(header->ocnt) > /*client->wr_pending_scnt*/ client->header->scnt) { 1004 } else if (ntohl(header->ocnt) >
942 fprintf(stderr, "WTF?! acks overtook pending outstanding. %u,%u\n", ntohl(header->ocnt), client->wr_pending_scnt); 1005 /*client->wr_pending_scnt */ client->header->scnt) {
1006 fprintf(stderr,
1007 "WTF?! acks overtook pending outstanding. %u,%u\n",
1008 ntohl(header->ocnt), client->wr_pending_scnt);
943 } else { 1009 } else {
944 // reset the window 1010 // reset the window
945 client->wr_window = WINDOW_MAX; 1011 client->wr_window = WINDOW_MAX;
946 pthread_cond_broadcast(&client->wr_wait); 1012 pthread_cond_broadcast(&client->wr_wait);
947 } 1013 }
948 } 1014 }
949
950 // the packet's scnt will be our new ocnt. 1015 // the packet's scnt will be our new ocnt.
951 client->header->ocnt = ntohl(header->scnt); 1016 client->header->ocnt = ntohl(header->scnt);
952 1017
953 // ensure there is enough space, either by first malloc or realloc 1018 // ensure there is enough space, either by first malloc or realloc
954 if (datalen > 0) { 1019 if (datalen > 0) {
955 log_debug_msg("%s: putting %d bytes into client's recv_buffer\n", __func__, datalen); 1020 log_debug_msg("%s: putting %d bytes into client's recv_buffer\n",
1021 __func__, datalen);
956 if (client->r_len == 0) 1022 if (client->r_len == 0)
957 dobroadcast = 1; 1023 dobroadcast = 1;
958 1024
@@ -960,7 +1026,8 @@ uint32_t append_receive_buffer(usbmux_client_t client, char* packet)
960 client->recv_buffer = malloc(datalen); 1026 client->recv_buffer = malloc(datalen);
961 client->r_len = 0; 1027 client->r_len = 0;
962 } else { 1028 } else {
963 client->recv_buffer = realloc(client->recv_buffer, client->r_len + datalen); 1029 client->recv_buffer =
1030 realloc(client->recv_buffer, client->r_len + datalen);
964 } 1031 }
965 1032
966 memcpy(&client->recv_buffer[client->r_len], data, datalen); 1033 memcpy(&client->recv_buffer[client->r_len], data, datalen);
@@ -982,7 +1049,7 @@ uint32_t append_receive_buffer(usbmux_client_t client, char* packet)
982 * because we're only called from one location, pullbulk, where the lock 1049 * because we're only called from one location, pullbulk, where the lock
983 * is already held. 1050 * is already held.
984 */ 1051 */
985usbmux_client_t find_client(usbmux_tcp_header* recv_header) 1052usbmux_client_t find_client(usbmux_tcp_header * recv_header)
986{ 1053{
987 // remember, as we're looking for the client, the receive header is 1054 // remember, as we're looking for the client, the receive header is
988 // coming from the USB into our client. This means that when we check 1055 // coming from the USB into our client. This means that when we check
@@ -1017,17 +1084,19 @@ int usbmux_pullbulk(usbmux_device_t device)
1017 return -EINVAL; 1084 return -EINVAL;
1018 1085
1019 int res = 0; 1086 int res = 0;
1020 static const int DEFAULT_CAPACITY = 128*1024; 1087 static const int DEFAULT_CAPACITY = 128 * 1024;
1021 if (device->usbReceive.buffer == NULL) { 1088 if (device->usbReceive.buffer == NULL) {
1022 device->usbReceive.capacity = DEFAULT_CAPACITY; 1089 device->usbReceive.capacity = DEFAULT_CAPACITY;
1023 device->usbReceive.buffer = malloc(device->usbReceive.capacity); 1090 device->usbReceive.buffer = malloc(device->usbReceive.capacity);
1024 device->usbReceive.leftover = 0; 1091 device->usbReceive.leftover = 0;
1025 } 1092 }
1026
1027 // start the cursor off just ahead of the leftover. 1093 // start the cursor off just ahead of the leftover.
1028 char* cursor = &device->usbReceive.buffer[device->usbReceive.leftover]; 1094 char *cursor = &device->usbReceive.buffer[device->usbReceive.leftover];
1029 // pull in content, note that the amount we can pull is capacity minus leftover 1095 // pull in content, note that the amount we can pull is capacity minus leftover
1030 int readlen = recv_from_device_timeout(device, cursor, device->usbReceive.capacity - device->usbReceive.leftover, 3000); 1096 int readlen =
1097 recv_from_device_timeout(device, cursor,
1098 device->usbReceive.capacity -
1099 device->usbReceive.leftover, 3000);
1031 if (readlen < 0) { 1100 if (readlen < 0) {
1032 res = readlen; 1101 res = readlen;
1033 //fprintf(stderr, "recv_from_device_timeout gave us an error.\n"); 1102 //fprintf(stderr, "recv_from_device_timeout gave us an error.\n");
@@ -1036,7 +1105,6 @@ int usbmux_pullbulk(usbmux_device_t device)
1036 if (readlen > 0) { 1105 if (readlen > 0) {
1037 //fprintf(stdout, "recv_from_device_timeout pulled an extra %d bytes\n", readlen); 1106 //fprintf(stdout, "recv_from_device_timeout pulled an extra %d bytes\n", readlen);
1038 } 1107 }
1039
1040 // the amount of content we have to work with is the remainder plus 1108 // the amount of content we have to work with is the remainder plus
1041 // what we managed to read 1109 // what we managed to read
1042 device->usbReceive.leftover += readlen; 1110 device->usbReceive.leftover += readlen;
@@ -1050,23 +1118,29 @@ int usbmux_pullbulk(usbmux_device_t device)
1050 break; 1118 break;
1051 usbmux_tcp_header *header = (usbmux_tcp_header *) cursor; 1119 usbmux_tcp_header *header = (usbmux_tcp_header *) cursor;
1052 1120
1053 log_debug_msg("%s: recv_from_device_timeout (%d --> %d)\n", __func__, ntohs(header->sport), ntohs(header->dport)); 1121 log_debug_msg("%s: recv_from_device_timeout (%d --> %d)\n",
1122 __func__, ntohs(header->sport),
1123 ntohs(header->dport));
1054 1124
1055 // now that we have a header, check if there is sufficient data 1125 // now that we have a header, check if there is sufficient data
1056 // to construct a full packet, including its data 1126 // to construct a full packet, including its data
1057 uint32_t packetlen = ntohl(header->length); 1127 uint32_t packetlen = ntohl(header->length);
1058 if ((uint32_t)device->usbReceive.leftover < packetlen) { 1128 if ((uint32_t) device->usbReceive.leftover < packetlen) {
1059 fprintf(stderr, "%s: not enough data to construct a full packet\n", __func__); 1129 fprintf(stderr,
1130 "%s: not enough data to construct a full packet\n",
1131 __func__);
1060 break; 1132 break;
1061 } 1133 }
1062
1063 // ok... find the client this packet will get stuffed to. 1134 // ok... find the client this packet will get stuffed to.
1064 usbmux_client_t client = find_client(header); 1135 usbmux_client_t client = find_client(header);
1065 if (client == NULL) { 1136 if (client == NULL) {
1066 log_debug_msg("WARNING: client for packet cannot be found. dropping packet.\n"); 1137 log_debug_msg
1138 ("WARNING: client for packet cannot be found. dropping packet.\n");
1067 } else { 1139 } else {
1068 // stuff the data 1140 // stuff the data
1069 log_debug_msg("%s: found client, calling append_receive_buffer\n", __func__); 1141 log_debug_msg
1142 ("%s: found client, calling append_receive_buffer\n",
1143 __func__);
1070 append_receive_buffer(client, cursor); 1144 append_receive_buffer(client, cursor);
1071 1145
1072 // perhaps this is too general, == -ECONNRESET 1146 // perhaps this is too general, == -ECONNRESET
@@ -1075,7 +1149,9 @@ int usbmux_pullbulk(usbmux_device_t device)
1075 pthread_mutex_lock(&client->mutex); 1149 pthread_mutex_lock(&client->mutex);
1076 if (client->cleanup) { 1150 if (client->cleanup) {
1077 pthread_mutex_unlock(&client->mutex); 1151 pthread_mutex_unlock(&client->mutex);
1078 log_debug_msg("freeing up connection (%d->%d)\n", ntohs(client->header->sport), ntohs(client->header->dport)); 1152 log_debug_msg("freeing up connection (%d->%d)\n",
1153 ntohs(client->header->sport),
1154 ntohs(client->header->dport));
1079 delete_connection(client); 1155 delete_connection(client);
1080 } else { 1156 } else {
1081 pthread_mutex_unlock(&client->mutex); 1157 pthread_mutex_unlock(&client->mutex);
@@ -1096,9 +1172,10 @@ int usbmux_pullbulk(usbmux_device_t device)
1096 // 1172 //
1097 // if there are no leftovers, we just leave the datastructure as is, 1173 // if there are no leftovers, we just leave the datastructure as is,
1098 // and re-use the block next time. 1174 // and re-use the block next time.
1099 if (device->usbReceive.leftover > 0 && cursor != device->usbReceive.buffer) { 1175 if (device->usbReceive.leftover > 0
1176 && cursor != device->usbReceive.buffer) {
1100 log_debug_msg("%s: we got a leftover, so handle it\n", __func__); 1177 log_debug_msg("%s: we got a leftover, so handle it\n", __func__);
1101 char* newbuff = malloc(DEFAULT_CAPACITY); 1178 char *newbuff = malloc(DEFAULT_CAPACITY);
1102 memcpy(newbuff, cursor, device->usbReceive.leftover); 1179 memcpy(newbuff, cursor, device->usbReceive.leftover);
1103 free(device->usbReceive.buffer); 1180 free(device->usbReceive.buffer);
1104 device->usbReceive.buffer = newbuff; 1181 device->usbReceive.buffer = newbuff;
@@ -1133,7 +1210,9 @@ int usbmux_get_error(usbmux_client_t client)
1133 * 1210 *
1134 * @return 0 on success or a negative errno value on failure. 1211 * @return 0 on success or a negative errno value on failure.
1135 */ 1212 */
1136int usbmux_recv_timeout(usbmux_client_t client, char *data, uint32_t datalen, uint32_t * recv_bytes, int timeout) 1213int usbmux_recv_timeout(usbmux_client_t client, char *data,
1214 uint32_t datalen, uint32_t * recv_bytes,
1215 int timeout)
1137{ 1216{
1138 1217
1139 if (!client || !data || datalen == 0 || !recv_bytes) 1218 if (!client || !data || datalen == 0 || !recv_bytes)
@@ -1147,15 +1226,15 @@ int usbmux_recv_timeout(usbmux_client_t client, char *data, uint32_t datalen, ui
1147 if (timeout > 0 && (client->recv_buffer == NULL || client->r_len == 0)) { 1226 if (timeout > 0 && (client->recv_buffer == NULL || client->r_len == 0)) {
1148 struct timespec ts; 1227 struct timespec ts;
1149 clock_gettime(CLOCK_REALTIME, &ts); 1228 clock_gettime(CLOCK_REALTIME, &ts);
1150 ts.tv_sec += timeout/1000; 1229 ts.tv_sec += timeout / 1000;
1151 ts.tv_nsec += (timeout-((int)(timeout/1000))*1000)*1000; 1230 ts.tv_nsec += (timeout - ((int) (timeout / 1000)) * 1000) * 1000;
1152 pthread_cond_timedwait(&client->wait, &client->mutex, &ts); 1231 pthread_cond_timedwait(&client->wait, &client->mutex, &ts);
1153 } 1232 }
1154 1233
1155 *recv_bytes = 0; 1234 *recv_bytes = 0;
1156 if (client->recv_buffer != NULL && client->r_len > 0) { 1235 if (client->recv_buffer != NULL && client->r_len > 0) {
1157 uint32_t foolen = datalen; 1236 uint32_t foolen = datalen;
1158 if ((int)foolen > client->r_len) 1237 if ((int) foolen > client->r_len)
1159 foolen = client->r_len; 1238 foolen = client->r_len;
1160 memcpy(data, client->recv_buffer, foolen); 1239 memcpy(data, client->recv_buffer, foolen);
1161 *recv_bytes = foolen; 1240 *recv_bytes = foolen;
@@ -1163,7 +1242,7 @@ int usbmux_recv_timeout(usbmux_client_t client, char *data, uint32_t datalen, ui
1163 // preserve any left-over unread amounts. 1242 // preserve any left-over unread amounts.
1164 int remainder = client->r_len - foolen; 1243 int remainder = client->r_len - foolen;
1165 if (remainder > 0) { 1244 if (remainder > 0) {
1166 char* newbuf = malloc(remainder); 1245 char *newbuf = malloc(remainder);
1167 memcpy(newbuf, client->recv_buffer + foolen, remainder); 1246 memcpy(newbuf, client->recv_buffer + foolen, remainder);
1168 client->r_len = remainder; 1247 client->r_len = remainder;
1169 free(client->recv_buffer); 1248 free(client->recv_buffer);