summaryrefslogtreecommitdiffstats
path: root/src/iphone.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/iphone.c')
-rw-r--r--src/iphone.c281
1 files changed, 281 insertions, 0 deletions
diff --git a/src/iphone.c b/src/iphone.c
index 586b3bc..3c13859 100644
--- a/src/iphone.c
+++ b/src/iphone.c
@@ -23,6 +23,7 @@
23#include <stdlib.h> 23#include <stdlib.h>
24#include <string.h> 24#include <string.h>
25#include <errno.h> 25#include <errno.h>
26#include <arpa/inet.h>
26 27
27#include <usbmuxd.h> 28#include <usbmuxd.h>
28#include "iphone.h" 29#include "iphone.h"
@@ -350,6 +351,286 @@ iphone_error_t iphone_device_recv(iphone_connection_t connection, char *data, ui
350 return IPHONE_E_UNKNOWN_ERROR; 351 return IPHONE_E_UNKNOWN_ERROR;
351} 352}
352 353
354/**
355 * Sends a plist over the given connection.
356 * Internally used generic plist send function.
357 *
358 * @param connection The connection to use for sending.
359 * Can be NULL if ssl_session is non-NULL.
360 * @param plist plist to send
361 * @param binary 1 = send binary plist, 0 = send xml plist
362 * @param ssl_session If set to NULL, the communication will be unencrypted.
363 * For encrypted communication, pass a valid and properly initialized
364 * gnutls_session_t. connection is ignored when ssl_session is non-NULL.
365 *
366 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when one or more
367 * parameters are invalid, IPHONE_E_PLIST_ERROR when dict is not a valid
368 * plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs.
369 */
370static iphone_error_t internal_plist_send(iphone_connection_t connection, plist_t plist, int binary, gnutls_session_t ssl_session)
371{
372 iphone_error_t res = IPHONE_E_UNKNOWN_ERROR;
373 char *content = NULL;
374 uint32_t length = 0;
375 uint32_t nlen = 0;
376 int bytes = 0;
377
378 if ((!connection && !ssl_session) || !plist) {
379 return IPHONE_E_INVALID_ARG;
380 }
381
382 if (binary) {
383 plist_to_bin(plist, &content, &length);
384 } else {
385 plist_to_xml(plist, &content, &length);
386 }
387
388 if (!content || length == 0) {
389 return IPHONE_E_PLIST_ERROR;
390 }
391
392 nlen = htonl(length);
393 log_debug_msg("%s: sending %d bytes\n", __func__, length);
394 if (ssl_session) {
395 bytes = gnutls_record_send(ssl_session, (const char*)&nlen, sizeof(nlen));
396 } else {
397 iphone_device_send(connection, (const char*)&nlen, sizeof(nlen), (uint32_t*)&bytes);
398 }
399 if (bytes == sizeof(nlen)) {
400 if (ssl_session) {
401 bytes = gnutls_record_send(ssl_session, content, length);
402 } else {
403 iphone_device_send(connection, content, length, (uint32_t*)&bytes);
404 }
405 if (bytes > 0) {
406 log_debug_msg("%s: received %d bytes\n", __func__, bytes);
407 log_debug_buffer(content, bytes);
408 if ((uint32_t)bytes == length) {
409 res = IPHONE_E_SUCCESS;
410 } else {
411 log_debug_msg("%s: ERROR: Could not send all data (%d of %d)!\n", __func__, bytes, length);
412 }
413 }
414 }
415 if (bytes <= 0) {
416 log_debug_msg("%s: ERROR: sending to device failed.\n", __func__);
417 }
418
419 free(content);
420
421 return res;
422}
423
424/**
425 * Sends an XML plist over the given connection.
426 *
427 * @param connection The connection to send data over
428 * @param plist plist to send
429 *
430 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection
431 * or plist is NULL, IPHONE_E_PLIST_ERROR when dict is not a valid plist,
432 * or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs.
433 */
434iphone_error_t iphone_device_send_xml_plist(iphone_connection_t connection, plist_t plist)
435{
436 return internal_plist_send(connection, plist, 0, NULL);
437}
438
439/**
440 * Sends a binary plist over the given connection.
441 *
442 * @param connection The connection to send data over
443 * @param plist plist to send
444 *
445 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection
446 * or plist is NULL, IPHONE_E_PLIST_ERROR when dict is not a valid plist,
447 * or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs.
448 */
449iphone_error_t iphone_device_send_binary_plist(iphone_connection_t connection, plist_t plist)
450{
451 return internal_plist_send(connection, plist, 1, NULL);
452}
453
454/**
455 * Sends an encrypted XML plist.
456 *
457 * @param ssl_session Valid and properly initialized gnutls_session_t.
458 * @param plist plist to send
459 *
460 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when ssl_session
461 * or plist is NULL, IPHONE_E_PLIST_ERROR when dict is not a valid plist,
462 * or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs.
463 */
464iphone_error_t iphone_device_send_encrypted_xml_plist(gnutls_session_t ssl_session, plist_t plist)
465{
466 return internal_plist_send(NULL, plist, 0, ssl_session);
467}
468
469/**
470 * Sends an encrypted binary plist.
471 *
472 * @param ssl_session Valid and properly initialized gnutls_session_t.
473 * @param plist plist to send
474 *
475 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when ssl_session
476 * or plist is NULL, IPHONE_E_PLIST_ERROR when dict is not a valid plist,
477 * or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs.
478 */
479iphone_error_t iphone_device_send_encrypted_binary_plist(gnutls_session_t ssl_session, plist_t plist)
480{
481 return internal_plist_send(NULL, plist, 1, ssl_session);
482}
483
484/**
485 * Receives a plist over the given connection.
486 * Internally used generic plist send function.
487 *
488 * @param connection The connection to receive data on
489 * @param plist pointer to a plist_t that will point to the received plist
490 * upon successful return
491 * @param timeout Maximum time in milliseconds to wait for data.
492 * @param ssl_session If set to NULL, the communication will be unencrypted.
493 * For encrypted communication, pass a valid and properly initialized
494 * gnutls_session_t.
495 *
496 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection
497 * or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be
498 * converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified
499 * error occurs.
500 */
501static iphone_error_t internal_plist_recv_timeout(iphone_connection_t connection, plist_t *plist, unsigned int timeout, gnutls_session_t ssl_session)
502{
503 iphone_error_t res = IPHONE_E_UNKNOWN_ERROR;
504 uint32_t pktlen = 0;
505 uint32_t bytes = 0;
506
507 if ((!connection && !ssl_session) || !plist) {
508 return IPHONE_E_INVALID_ARG;
509 }
510
511 iphone_device_recv_timeout(connection, (char*)&pktlen, sizeof(pktlen), &bytes, timeout);
512 log_debug_msg("%s: initial read=%i\n", __func__, bytes);
513 if (bytes < 4) {
514 log_debug_msg("%s: initial read failed!\n", __func__);
515 return IPHONE_E_NOT_ENOUGH_DATA;
516 } else {
517 if ((char)pktlen == 0) { /* prevent huge buffers */
518 uint32_t curlen = 0;
519 char *content = NULL;
520 pktlen = ntohl(pktlen);
521 log_debug_msg("%s: %d bytes following\n", __func__, pktlen);
522 content = (char*)malloc(pktlen);
523
524 while (curlen < pktlen) {
525 iphone_device_recv(connection, content+curlen, pktlen-curlen, &bytes);
526 if (bytes <= 0) {
527 res = IPHONE_E_UNKNOWN_ERROR;
528 break;
529 }
530 log_debug_msg("%s: received %d bytes\n", __func__, bytes);
531 curlen += bytes;
532 }
533 log_debug_buffer(content, pktlen);
534 if (!memcmp(content, "bplist00", 8)) {
535 plist_from_bin(content, pktlen, plist);
536 } else {
537 plist_from_xml(content, pktlen, plist);
538 }
539 if (*plist) {
540 res = IPHONE_E_SUCCESS;
541 } else {
542 res = IPHONE_E_PLIST_ERROR;
543 }
544 free(content);
545 content = NULL;
546 } else {
547 res = IPHONE_E_UNKNOWN_ERROR;
548 }
549 }
550 return res;
551}
552
553/**
554 * Receives a plist over the given connection with specified timeout.
555 * Binary or XML plists are automatically handled.
556 *
557 * @param connection The connection to receive data on
558 * @param plist pointer to a plist_t that will point to the received plist
559 * upon successful return
560 * @param timeout Maximum time in milliseconds to wait for data.
561 *
562 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection
563 * or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be
564 * converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified
565 * error occurs.
566 */
567iphone_error_t iphone_device_receive_plist_with_timeout(iphone_connection_t connection, plist_t *plist, unsigned int timeout)
568{
569 return internal_plist_recv_timeout(connection, plist, timeout, NULL);
570}
571
572/**
573 * Receives a plist over the given connection.
574 * Binary or XML plists are automatically handled.
575 *
576 * This function is like iphone_device_receive_plist_with_timeout
577 * using a timeout of 10 seconds.
578 * @see iphone_device_receive_plist_with_timeout
579 *
580 * @param connection The connection to receive data on
581 * @param plist pointer to a plist_t that will point to the received plist
582 * upon successful return
583 *
584 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection
585 * or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be
586 * converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified
587 * error occurs.
588 */
589iphone_error_t iphone_device_receive_plist(iphone_connection_t connection, plist_t *plist)
590{
591 return internal_plist_recv_timeout(connection, plist, 10000, NULL);
592}
593
594/**
595 * Receives an encrypted plist with specified timeout.
596 * Binary or XML plists are automatically handled.
597 *
598 * @param ssl_session Valid and properly initialized gnutls_session_t.
599 * @param plist pointer to a plist_t that will point to the received plist
600 * upon successful return
601 * @param timeout Maximum time in milliseconds to wait for data.
602 *
603 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when ssl_session
604 * or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be
605 * converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified
606 * error occurs.
607 */
608iphone_error_t iphone_device_receive_encrypted_plist_with_timeout(gnutls_session_t ssl_session, plist_t *plist, unsigned int timeout)
609{
610 return internal_plist_recv_timeout(NULL, plist, timeout, ssl_session);
611}
612
613/**
614 * Receives an encrypted plist.
615 * Binary or XML plists are automatically handled.
616 * This function is like iphone_device_receive_encrypted_plist_with_timeout
617 * with a timeout value of 10 seconds.
618 *
619 * @param ssl_session Valid and properly initialized gnutls_session_t.
620 * @param connection The connection to receive data on
621 * @param plist pointer to a plist_t that will point to the received plist
622 * upon successful return
623 *
624 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when ssl_session
625 * or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be
626 * converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified
627 * error occurs.
628 */
629iphone_error_t iphone_device_receive_encrypted_plist(gnutls_session_t ssl_session, plist_t *plist)
630{
631 return internal_plist_recv_timeout(NULL, plist, 10000, ssl_session);
632}
633
353iphone_error_t iphone_device_get_handle(iphone_device_t device, uint32_t *handle) 634iphone_error_t iphone_device_get_handle(iphone_device_t device, uint32_t *handle)
354{ 635{
355 if (!device) 636 if (!device)