diff options
Diffstat (limited to 'src/iphone.c')
-rw-r--r-- | src/iphone.c | 281 |
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 @@ #include <stdlib.h> #include <string.h> #include <errno.h> +#include <arpa/inet.h> #include <usbmuxd.h> #include "iphone.h" @@ -350,6 +351,286 @@ iphone_error_t iphone_device_recv(iphone_connection_t connection, char *data, ui return IPHONE_E_UNKNOWN_ERROR; } +/** + * Sends a plist over the given connection. + * Internally used generic plist send function. + * + * @param connection The connection to use for sending. + * Can be NULL if ssl_session is non-NULL. + * @param plist plist to send + * @param binary 1 = send binary plist, 0 = send xml plist + * @param ssl_session If set to NULL, the communication will be unencrypted. + * For encrypted communication, pass a valid and properly initialized + * gnutls_session_t. connection is ignored when ssl_session is non-NULL. + * + * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when one or more + * parameters are invalid, IPHONE_E_PLIST_ERROR when dict is not a valid + * plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs. + */ +static iphone_error_t internal_plist_send(iphone_connection_t connection, plist_t plist, int binary, gnutls_session_t ssl_session) +{ + iphone_error_t res = IPHONE_E_UNKNOWN_ERROR; + char *content = NULL; + uint32_t length = 0; + uint32_t nlen = 0; + int bytes = 0; + + if ((!connection && !ssl_session) || !plist) { + return IPHONE_E_INVALID_ARG; + } + + if (binary) { + plist_to_bin(plist, &content, &length); + } else { + plist_to_xml(plist, &content, &length); + } + + if (!content || length == 0) { + return IPHONE_E_PLIST_ERROR; + } + + nlen = htonl(length); + log_debug_msg("%s: sending %d bytes\n", __func__, length); + if (ssl_session) { + bytes = gnutls_record_send(ssl_session, (const char*)&nlen, sizeof(nlen)); + } else { + iphone_device_send(connection, (const char*)&nlen, sizeof(nlen), (uint32_t*)&bytes); + } + if (bytes == sizeof(nlen)) { + if (ssl_session) { + bytes = gnutls_record_send(ssl_session, content, length); + } else { + iphone_device_send(connection, content, length, (uint32_t*)&bytes); + } + if (bytes > 0) { + log_debug_msg("%s: received %d bytes\n", __func__, bytes); + log_debug_buffer(content, bytes); + if ((uint32_t)bytes == length) { + res = IPHONE_E_SUCCESS; + } else { + log_debug_msg("%s: ERROR: Could not send all data (%d of %d)!\n", __func__, bytes, length); + } + } + } + if (bytes <= 0) { + log_debug_msg("%s: ERROR: sending to device failed.\n", __func__); + } + + free(content); + + return res; +} + +/** + * Sends an XML plist over the given connection. + * + * @param connection The connection to send data over + * @param plist plist to send + * + * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection + * or plist is NULL, IPHONE_E_PLIST_ERROR when dict is not a valid plist, + * or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs. + */ +iphone_error_t iphone_device_send_xml_plist(iphone_connection_t connection, plist_t plist) +{ + return internal_plist_send(connection, plist, 0, NULL); +} + +/** + * Sends a binary plist over the given connection. + * + * @param connection The connection to send data over + * @param plist plist to send + * + * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection + * or plist is NULL, IPHONE_E_PLIST_ERROR when dict is not a valid plist, + * or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs. + */ +iphone_error_t iphone_device_send_binary_plist(iphone_connection_t connection, plist_t plist) +{ + return internal_plist_send(connection, plist, 1, NULL); +} + +/** + * Sends an encrypted XML plist. + * + * @param ssl_session Valid and properly initialized gnutls_session_t. + * @param plist plist to send + * + * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when ssl_session + * or plist is NULL, IPHONE_E_PLIST_ERROR when dict is not a valid plist, + * or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs. + */ +iphone_error_t iphone_device_send_encrypted_xml_plist(gnutls_session_t ssl_session, plist_t plist) +{ + return internal_plist_send(NULL, plist, 0, ssl_session); +} + +/** + * Sends an encrypted binary plist. + * + * @param ssl_session Valid and properly initialized gnutls_session_t. + * @param plist plist to send + * + * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when ssl_session + * or plist is NULL, IPHONE_E_PLIST_ERROR when dict is not a valid plist, + * or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs. + */ +iphone_error_t iphone_device_send_encrypted_binary_plist(gnutls_session_t ssl_session, plist_t plist) +{ + return internal_plist_send(NULL, plist, 1, ssl_session); +} + +/** + * Receives a plist over the given connection. + * Internally used generic plist send function. + * + * @param connection The connection to receive data on + * @param plist pointer to a plist_t that will point to the received plist + * upon successful return + * @param timeout Maximum time in milliseconds to wait for data. + * @param ssl_session If set to NULL, the communication will be unencrypted. + * For encrypted communication, pass a valid and properly initialized + * gnutls_session_t. + * + * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection + * or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be + * converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified + * error occurs. + */ +static iphone_error_t internal_plist_recv_timeout(iphone_connection_t connection, plist_t *plist, unsigned int timeout, gnutls_session_t ssl_session) +{ + iphone_error_t res = IPHONE_E_UNKNOWN_ERROR; + uint32_t pktlen = 0; + uint32_t bytes = 0; + + if ((!connection && !ssl_session) || !plist) { + return IPHONE_E_INVALID_ARG; + } + + iphone_device_recv_timeout(connection, (char*)&pktlen, sizeof(pktlen), &bytes, timeout); + log_debug_msg("%s: initial read=%i\n", __func__, bytes); + if (bytes < 4) { + log_debug_msg("%s: initial read failed!\n", __func__); + return IPHONE_E_NOT_ENOUGH_DATA; + } else { + if ((char)pktlen == 0) { /* prevent huge buffers */ + uint32_t curlen = 0; + char *content = NULL; + pktlen = ntohl(pktlen); + log_debug_msg("%s: %d bytes following\n", __func__, pktlen); + content = (char*)malloc(pktlen); + + while (curlen < pktlen) { + iphone_device_recv(connection, content+curlen, pktlen-curlen, &bytes); + if (bytes <= 0) { + res = IPHONE_E_UNKNOWN_ERROR; + break; + } + log_debug_msg("%s: received %d bytes\n", __func__, bytes); + curlen += bytes; + } + log_debug_buffer(content, pktlen); + if (!memcmp(content, "bplist00", 8)) { + plist_from_bin(content, pktlen, plist); + } else { + plist_from_xml(content, pktlen, plist); + } + if (*plist) { + res = IPHONE_E_SUCCESS; + } else { + res = IPHONE_E_PLIST_ERROR; + } + free(content); + content = NULL; + } else { + res = IPHONE_E_UNKNOWN_ERROR; + } + } + return res; +} + +/** + * Receives a plist over the given connection with specified timeout. + * Binary or XML plists are automatically handled. + * + * @param connection The connection to receive data on + * @param plist pointer to a plist_t that will point to the received plist + * upon successful return + * @param timeout Maximum time in milliseconds to wait for data. + * + * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection + * or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be + * converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified + * error occurs. + */ +iphone_error_t iphone_device_receive_plist_with_timeout(iphone_connection_t connection, plist_t *plist, unsigned int timeout) +{ + return internal_plist_recv_timeout(connection, plist, timeout, NULL); +} + +/** + * Receives a plist over the given connection. + * Binary or XML plists are automatically handled. + * + * This function is like iphone_device_receive_plist_with_timeout + * using a timeout of 10 seconds. + * @see iphone_device_receive_plist_with_timeout + * + * @param connection The connection to receive data on + * @param plist pointer to a plist_t that will point to the received plist + * upon successful return + * + * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection + * or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be + * converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified + * error occurs. + */ +iphone_error_t iphone_device_receive_plist(iphone_connection_t connection, plist_t *plist) +{ + return internal_plist_recv_timeout(connection, plist, 10000, NULL); +} + +/** + * Receives an encrypted plist with specified timeout. + * Binary or XML plists are automatically handled. + * + * @param ssl_session Valid and properly initialized gnutls_session_t. + * @param plist pointer to a plist_t that will point to the received plist + * upon successful return + * @param timeout Maximum time in milliseconds to wait for data. + * + * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when ssl_session + * or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be + * converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified + * error occurs. + */ +iphone_error_t iphone_device_receive_encrypted_plist_with_timeout(gnutls_session_t ssl_session, plist_t *plist, unsigned int timeout) +{ + return internal_plist_recv_timeout(NULL, plist, timeout, ssl_session); +} + +/** + * Receives an encrypted plist. + * Binary or XML plists are automatically handled. + * This function is like iphone_device_receive_encrypted_plist_with_timeout + * with a timeout value of 10 seconds. + * + * @param ssl_session Valid and properly initialized gnutls_session_t. + * @param connection The connection to receive data on + * @param plist pointer to a plist_t that will point to the received plist + * upon successful return + * + * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when ssl_session + * or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be + * converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified + * error occurs. + */ +iphone_error_t iphone_device_receive_encrypted_plist(gnutls_session_t ssl_session, plist_t *plist) +{ + return internal_plist_recv_timeout(NULL, plist, 10000, ssl_session); +} + iphone_error_t iphone_device_get_handle(iphone_device_t device, uint32_t *handle) { if (!device) |