diff options
author | Jonathan Beck | 2008-10-25 16:11:27 +0200 |
---|---|---|
committer | Jonathan Beck | 2008-10-25 16:52:20 +0200 |
commit | 0b2cfd2c7c6211ff5902e48720c34067f238ce90 (patch) | |
tree | 9c30578b4288795e7589c959caa08f3547ca0b88 /src | |
parent | bbd813da84c1ff8484bbd3eb7f575775271424da (diff) | |
download | libplist-0b2cfd2c7c6211ff5902e48720c34067f238ce90.tar.gz libplist-0b2cfd2c7c6211ff5902e48720c34067f238ce90.tar.bz2 |
Handle debugging through utilitary functions
Diffstat (limited to 'src')
-rw-r--r-- | src/AFC.c | 116 | ||||
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/initconf.c | 3 | ||||
-rw-r--r-- | src/iphone.c | 56 | ||||
-rw-r--r-- | src/lockdown.c | 151 | ||||
-rw-r--r-- | src/usbmux.c | 39 | ||||
-rw-r--r-- | src/userpref.c | 8 | ||||
-rw-r--r-- | src/utils.c | 80 | ||||
-rw-r--r-- | src/utils.h | 30 |
9 files changed, 239 insertions, 248 deletions
@@ -27,16 +27,13 @@ // This is the maximum size an AFC data packet can be const int MAXIMUM_PACKET_SIZE = (2 << 15) - 32; -extern int debug; - /** Locks an AFC client, done for thread safety stuff * * @param client The AFC client connection to lock */ static void afc_lock(iphone_afc_client_t client) { - if (debug) - fprintf(stderr, "Locked\n"); + log_debug_msg("Locked\n"); while (client->lock) { usleep(500); // they say it's obsolete, but whatever } @@ -49,8 +46,7 @@ static void afc_lock(iphone_afc_client_t client) */ static void afc_unlock(iphone_afc_client_t client) { // just to be pretty - if (debug) - fprintf(stderr, "Unlocked\n"); + log_debug_msg("Unlocked\n"); client->lock = 0; } @@ -156,16 +152,12 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); offset = client->afc_packet->this_length - sizeof(AFCPacket); - if (debug) - fprintf(stderr, "dispatch_AFC_packet: Offset: %i\n", offset); + log_debug_msg("dispatch_AFC_packet: Offset: %i\n", offset); if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) { - if (debug) { - fprintf(stderr, "dispatch_AFC_packet: Length did not resemble what it was supposed"); - fprintf(stderr, "to based on the packet.\n"); - fprintf(stderr, "length minus offset: %i\n", length - offset); - fprintf(stderr, "rest of packet: %i\n", - client->afc_packet->entire_length - client->afc_packet->this_length); - } + log_debug_msg("dispatch_AFC_packet: Length did not resemble what it was supposed"); + log_debug_msg("to based on the packet.\n"); + log_debug_msg("length minus offset: %i\n", length - offset); + log_debug_msg("rest of packet: %i\n", client->afc_packet->entire_length - client->afc_packet->this_length); free(buffer); return -1; } @@ -176,32 +168,25 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int return bytes; } - if (debug) { - fprintf(stderr, "dispatch_AFC_packet: sent the first now go with the second\n"); - fprintf(stderr, "Length: %i\n", length - offset); - fprintf(stderr, "Buffer: \n"); - fwrite(data + offset, 1, length - offset, stdout); - } + log_debug_msg("dispatch_AFC_packet: sent the first now go with the second\n"); + log_debug_msg("Length: %i\n", length - offset); + log_debug_msg("Buffer: \n"); + log_debug_msg(data + offset); iphone_mux_send(client->connection, data + offset, length - offset, &bytes); return bytes; } else { - if (debug) - fprintf(stderr, "dispatch_AFC_packet doin things the old way\n"); + log_debug_msg("dispatch_AFC_packet doin things the old way\n"); char *buffer = (char *) malloc(sizeof(char) * client->afc_packet->this_length); - if (debug) - fprintf(stderr, "dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length); + log_debug_msg("dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length); memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); - if (debug) - fprintf(stderr, "dispatch_AFC_packet packet data follows\n"); + log_debug_msg("dispatch_AFC_packet packet data follows\n"); if (length > 0) { memcpy(buffer + sizeof(AFCPacket), data, length); buffer[sizeof(AFCPacket) + length] = '\0'; } - if (debug) - fwrite(buffer, 1, client->afc_packet->this_length, stdout); - if (debug) - fprintf(stderr, "\n"); + log_debug_buffer(buffer, client->afc_packet->this_length); + log_debug_msg("\n"); iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes); if (buffer) { @@ -257,30 +242,23 @@ static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) free(buffer); if (r_packet->operation == AFC_ERROR && !(client->afc_packet->operation == AFC_DELETE && param1 == 7)) { - if (debug) - fprintf(stderr, - "Oops? Bad operation code received: 0x%X, operation=0x%X, param1=%d\n", - r_packet->operation, client->afc_packet->operation, param1); + log_debug_msg("Oops? Bad operation code received: 0x%X, operation=0x%X, param1=%d\n", + r_packet->operation, client->afc_packet->operation, param1); recv_len = r_packet->entire_length - r_packet->this_length; free(r_packet); - if (debug) - fprintf(stderr, "recv_len=%d\n", recv_len); + log_debug_msg("recv_len=%d\n", recv_len); if (param1 == 0) { - if (debug) - fprintf(stderr, "... false alarm, but still\n"); + log_debug_msg("... false alarm, but still\n"); *dump_here = NULL; return 0; } else { - if (debug) - fprintf(stderr, "Errno %i\n", param1); + log_debug_msg("Errno %i\n", param1); } *dump_here = NULL; return -1; } else { - if (debug) - fprintf(stderr, - "Operation code %x\nFull length %i and this length %i\n", - r_packet->operation, r_packet->entire_length, r_packet->this_length); + log_debug_msg("Operation code %x\nFull length %i and this length %i\n", + r_packet->operation, r_packet->entire_length, r_packet->this_length); } recv_len = r_packet->entire_length - r_packet->this_length; @@ -294,25 +272,19 @@ static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) final_buffer = (char *) malloc(sizeof(char) * recv_len); while (current_count < recv_len) { iphone_mux_recv(client->connection, buffer, recv_len - current_count, &bytes); - if (debug) - fprintf(stderr, "receive_AFC_data: still collecting packets\n"); + log_debug_msg("receive_AFC_data: still collecting packets\n"); if (bytes < 0) { - if (debug) - fprintf(stderr, "receive_AFC_data: mux_recv failed: %d\n", bytes); + log_debug_msg("receive_AFC_data: mux_recv failed: %d\n", bytes); break; } if (bytes > recv_len - current_count) { - if (debug) - fprintf(stderr, "receive_AFC_data: mux_recv delivered too much data\n"); + log_debug_msg("receive_AFC_data: mux_recv delivered too much data\n"); break; } if (bytes > 7 && strstr(buffer, "CFA6LPAA")) { - if (debug) - fprintf(stderr, - "receive_AFC_data: WARNING: there is AFC data in this packet at %ti\n", - strstr(buffer, "CFA6LPAA") - buffer); - if (debug) - fprintf(stderr, "receive_AFC_data: the total packet length is %i\n", bytes); + log_debug_msg("receive_AFC_data: WARNING: there is AFC data in this packet at %ti\n", + strstr(buffer, "CFA6LPAA") - buffer); + log_debug_msg("receive_AFC_data: the total packet length is %i\n", bytes); } memcpy(final_buffer + current_count, buffer, bytes); @@ -701,8 +673,7 @@ iphone_afc_open_file(iphone_afc_client_t client, const char *filename, free(data); if (bytes <= 0) { - if (debug) - fprintf(stderr, "afc_open_file: Didn't receive a response to the command\n"); + log_debug_msg("afc_open_file: Didn't receive a response to the command\n"); afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; } @@ -718,8 +689,7 @@ iphone_afc_open_file(iphone_afc_client_t client, const char *filename, *file = file_loc; return IPHONE_E_SUCCESS; } else { - if (debug) - fprintf(stderr, "afc_open_file: Didn't get any further data\n"); + log_debug_msg("afc_open_file: Didn't get any further data\n"); afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; } @@ -747,16 +717,14 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d if (!client || !client->afc_packet || !client->connection || !file) return IPHONE_E_INVALID_ARG; - if (debug) - fprintf(stderr, "afc_read_file called for length %i\n", length); + log_debug_msg("afc_read_file called for length %i\n", length); afc_lock(client); // Looping here to get around the maximum amount of data that // recieve_AFC_data can handle while (current_count < length) { - if (debug) - fprintf(stderr, "afc_read_file: current count is %i but length is %i\n", current_count, length); + log_debug_msg("afc_read_file: current count is %i but length is %i\n", current_count, length); // Send the read command AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); @@ -774,8 +742,7 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d } // Receive the data bytes_loc = receive_AFC_data(client, &input); - if (debug) - fprintf(stderr, "afc_read_file: bytes returned: %i\n", bytes_loc); + log_debug_msg("afc_read_file: bytes returned: %i\n", bytes_loc); if (bytes_loc < 0) { if (input) free(input); @@ -790,8 +757,7 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d // success } else { if (input) { - if (debug) - fprintf(stderr, "afc_read_file: %d\n", bytes_loc); + log_debug_msg("afc_read_file: %d\n", bytes_loc); memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc); free(input); input = NULL; @@ -799,8 +765,7 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d } } } - if (debug) - fprintf(stderr, "afc_read_file: returning current_count as %i\n", current_count); + log_debug_msg("afc_read_file: returning current_count as %i\n", current_count); afc_unlock(client); *bytes = current_count; @@ -831,8 +796,7 @@ iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file, afc_lock(client); - if (debug) - fprintf(stderr, "afc_write_file: Write length: %i\n", length); + log_debug_msg("afc_write_file: Write length: %i\n", length); // Divide the file into segments. for (i = 0; i < segments; i++) { @@ -893,8 +857,7 @@ iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file, bytes_loc = receive_AFC_data(client, &acknowledgement); afc_unlock(client); if (bytes_loc < 0) { - if (debug) - fprintf(stderr, "afc_write_file: uh oh?\n"); + log_debug_msg("afc_write_file: uh oh?\n"); } *bytes = current_count; return IPHONE_E_SUCCESS; @@ -916,8 +879,7 @@ iphone_error_t iphone_afc_close_file(iphone_afc_client_t client, iphone_afc_file afc_lock(client); - if (debug) - fprintf(stderr, "afc_close_file: File handle %i\n", file->filehandle); + log_debug_msg("afc_close_file: File handle %i\n", file->filehandle); // Send command memcpy(buffer, &file->filehandle, sizeof(uint32)); diff --git a/src/Makefile.am b/src/Makefile.am index 34f6c1c..6c2b275 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,7 +7,7 @@ bin_PROGRAMS = iphoneclient libiphone-initconf lckd-client iphoneclient_SOURCES = main.c iphoneclient_LDADD = libiphone.la -libiphone_initconf_SOURCES = initconf.c userpref.c lockdown.c plist.c usbmux.c iphone.c +libiphone_initconf_SOURCES = initconf.c userpref.c lockdown.c plist.c usbmux.c iphone.c utils.c libiphone_initconf_CFLAGS = $(libgthread2_CFLAGS) $(AM_CFLAGS) libiphone_initconf_LDFLAGS = $(libgthread2_LIBS) $(AM_LDFLAGS) @@ -17,4 +17,4 @@ lckd_client_LDFLAGS = -lreadline $(AM_LDFLAGS) lckd_client_LDADD = libiphone.la lib_LTLIBRARIES = libiphone.la -libiphone_la_SOURCES = usbmux.c iphone.c plist.c lockdown.c AFC.c userpref.c +libiphone_la_SOURCES = usbmux.c iphone.c plist.c lockdown.c AFC.c userpref.c utils.c diff --git a/src/initconf.c b/src/initconf.c index c8c6e84..8aca2a6 100644 --- a/src/initconf.c +++ b/src/initconf.c @@ -26,9 +26,10 @@ #include <gnutls/x509.h> #include <glib.h> +#include "libiphone/libiphone.h" #include "userpref.h" #include "lockdown.h" - +#include "utils.h" /** Generates a 2048 byte key, split into a function so that it can be run in a * thread. diff --git a/src/iphone.c b/src/iphone.c index 4980078..b7f6cc4 100644 --- a/src/iphone.c +++ b/src/iphone.c @@ -21,24 +21,13 @@ #include "usbmux.h" #include "iphone.h" +#include "utils.h" #include <arpa/inet.h> #include <usb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -int iphone_debug = 0; - -/** - * Sets the level of debugging. Currently the only acceptable values are 0 and - * 1. - * - * @param level Set to 0 for no debugging or 1 for debugging. - */ -void iphone_set_debug(int level) -{ - iphone_debug = level; -} /** * Given a USB bus and device number, returns a device handle to the iPhone on @@ -90,19 +79,19 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t } iphone_free_device(phone); - if (iphone_debug) - fprintf(stderr, "iphone_get_specific_device: iPhone not found\n"); + + log_debug_msg("iphone_get_specific_device: iPhone not found\n"); return IPHONE_E_NO_DEVICE; found: // Send the version command to the phone version = version_header(); bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800); - if (bytes < 20 && iphone_debug) { - fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n"); + if (bytes < 20) { + log_debug_msg("get_iPhone(): libusb did NOT send enough!\n"); if (bytes < 0) { - fprintf(stderr, "get_iPhone(): libusb gave me the error %d: %s (%s)\n", - bytes, usb_strerror(), strerror(-bytes)); + log_debug_msg("get_iPhone(): libusb gave me the error %d: %s (%s)\n", + bytes, usb_strerror(), strerror(-bytes)); } } // Read the phone's response @@ -112,11 +101,9 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t if (bytes < 20) { free(version); iphone_free_device(phone); - if (iphone_debug) - fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n"); - if (iphone_debug && bytes < 0) - fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n", - bytes, usb_strerror(), strerror(-bytes)); + log_debug_msg("get_iPhone(): Invalid version message -- header too short.\n"); + if (bytes < 0) + log_debug_msg("get_iPhone(): libusb error message %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes)); return IPHONE_E_NOT_ENOUGH_DATA; } // Check for correct version @@ -130,14 +117,12 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t // Bad header iphone_free_device(phone); free(version); - if (iphone_debug) - fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number."); + log_debug_msg("get_iPhone(): Received a bad header/invalid version number."); return IPHONE_E_BAD_HEADER; } // If it got to this point it's gotta be bad - if (iphone_debug) - fprintf(stderr, "get_iPhone(): Unknown error.\n"); + log_debug_msg("get_iPhone(): Unknown error.\n"); iphone_free_device(phone); free(version); return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad @@ -218,14 +203,13 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen) if (!phone) return -1; - if (iphone_debug) - fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data); + log_debug_msg("send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data); bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800); if (bytes < datalen) { - if (iphone_debug && bytes < 0) - fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), - strerror(-bytes)); + if (bytes < 0) + log_debug_msg("send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), + strerror(-bytes)); return -1; } else { return bytes; @@ -250,14 +234,12 @@ int recv_from_phone(iphone_device_t phone, char *data, int datalen) if (!phone) return -1; - if (iphone_debug) - fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen); + log_debug_msg("recv_from_phone(): attempting to receive %i bytes\n", datalen); bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500); if (bytes < 0) { - if (iphone_debug) - fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), - strerror(-bytes)); + log_debug_msg("recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), + strerror(-bytes)); return -1; } diff --git a/src/lockdown.c b/src/lockdown.c index ffabd88..65cbf90 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -30,8 +30,6 @@ #include <libtasn1.h> #include <gnutls/x509.h> -extern int debug; - const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = { {"PKCS1", 536872976, 0}, {0, 1073741836, 0}, @@ -176,13 +174,8 @@ iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uin length = htonl(length); memcpy(real_query, &length, sizeof(length)); memcpy(real_query + 4, raw_data, ntohl(length)); - if (debug) { - printf("lockdownd_send(): made the query, sending it along\n"); - FILE *packet = fopen("grpkt", "w"); - fwrite(real_query, 1, ntohl(length) + 4, packet); - fclose(packet); - packet = NULL; - } + log_debug_msg("lockdownd_send(): made the query, sending it along\n"); + dump_debug_buffer("grpkt", real_query, ntohl(length) + 4); if (!client->in_SSL) ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes); @@ -190,8 +183,7 @@ iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uin gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length)); ret = IPHONE_E_SUCCESS; } - if (debug) - printf("lockdownd_send(): sent it!\n"); + log_debug_msg("lockdownd_send(): sent it!\n"); free(real_query); *sent_bytes = bytes; return ret; @@ -215,8 +207,7 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) int bytes = 0, i = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - if (debug) - printf("lockdownd_hello() called\n"); + log_debug_msg("lockdownd_hello() called\n"); dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); key = add_key_str_dict_element(plist, dict, "Request", "QueryType", 1); char *XML_content; @@ -246,8 +237,7 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) for (i = 0; dictionary[i]; i += 2) { if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { - if (debug) - printf("lockdownd_hello(): success\n"); + log_debug_msg("lockdownd_hello(): success\n"); ret = IPHONE_E_SUCCESS; break; } @@ -479,11 +469,9 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch if (ret != IPHONE_E_SUCCESS) return ret; - if (debug) { - printf("lockdown_pair_device: iPhone's response to our pair request:\n"); - fwrite(XML_content, 1, bytes, stdout); - printf("\n\n"); - } + log_debug_msg("lockdown_pair_device: iPhone's response to our pair request:\n"); + log_debug_msg(XML_content); + log_debug_msg("\n\n"); plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); if (!plist) { @@ -519,13 +507,11 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch /* store public key in config if pairing succeeded */ if (success) { - if (debug) - printf("lockdownd_pair_device: pair success\n"); + log_debug_msg("lockdownd_pair_device: pair success\n"); store_device_public_key(uid, public_key_b64); ret = IPHONE_E_SUCCESS; } else { - if (debug) - printf("lockdownd_pair_device: pair failure\n"); + log_debug_msg("lockdownd_pair_device: pair failure\n"); ret = IPHONE_E_PAIRING_FAILED; } free(public_key_b64); @@ -678,15 +664,13 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c key = add_key_str_dict_element(plist, dict, "HostID", HostID, 1); if (!key) { - if (debug) - printf("Couldn't add a key.\n"); + log_debug_msg("Couldn't add a key.\n"); xmlFreeDoc(plist); return IPHONE_E_DICT_ERROR; } key = add_key_str_dict_element(plist, dict, "Request", "StartSession", 1); if (!key) { - if (debug) - printf("Couldn't add a key.\n"); + log_debug_msg("Couldn't add a key.\n"); xmlFreeDoc(plist); return IPHONE_E_DICT_ERROR; } @@ -719,8 +703,7 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c //gnutls_anon_client_credentials_t anoncred; gnutls_certificate_credentials_t xcred; - if (debug) - printf("We started the session OK, now trying GnuTLS\n"); + log_debug_msg("We started the session OK, now trying GnuTLS\n"); errno = 0; gnutls_global_init(); //gnutls_anon_allocate_client_credentials(&anoncred); @@ -743,32 +726,25 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c } gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. - if (debug) - printf("GnuTLS step 1...\n"); + log_debug_msg("GnuTLS step 1...\n"); gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control); - if (debug) - printf("GnuTLS step 2...\n"); + log_debug_msg("GnuTLS step 2...\n"); gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite); - if (debug) - printf("GnuTLS step 3...\n"); + log_debug_msg("GnuTLS step 3...\n"); gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead); - if (debug) - printf("GnuTLS step 4 -- now handshaking...\n"); + log_debug_msg("GnuTLS step 4 -- now handshaking...\n"); - if (errno && debug) - printf("WARN: errno says %s before handshake!\n", strerror(errno)); + if (errno) + log_debug_msg("WARN: errno says %s before handshake!\n", strerror(errno)); return_me = gnutls_handshake(*control->ssl_session); - if (debug) - printf("GnuTLS handshake done...\n"); + log_debug_msg("GnuTLS handshake done...\n"); free_dictionary(dictionary); if (return_me != GNUTLS_E_SUCCESS) { - if (debug) - printf("GnuTLS reported something wrong.\n"); + log_debug_msg("GnuTLS reported something wrong.\n"); gnutls_perror(return_me); - if (debug) - printf("oh.. errno says %s\n", strerror(errno)); + log_debug_msg("oh.. errno says %s\n", strerror(errno)); return IPHONE_E_SSL_ERROR; } else { control->in_SSL = 1; @@ -777,19 +753,17 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c } } - if (debug) { - printf("Apparently failed negotiating with lockdownd.\n"); - printf("Responding dictionary: \n"); - for (i = 0; dictionary[i]; i += 2) { - printf("\t%s: %s\n", dictionary[i], dictionary[i + 1]); - } + log_debug_msg("Apparently failed negotiating with lockdownd.\n"); + log_debug_msg("Responding dictionary: \n"); + for (i = 0; dictionary[i]; i += 2) { + log_debug_msg("\t%s: %s\n", dictionary[i], dictionary[i + 1]); } + free_dictionary(dictionary); return IPHONE_E_SSL_ERROR; } else { - if (debug) - printf("Didn't get enough bytes.\n"); + log_debug_msg("Didn't get enough bytes.\n"); return IPHONE_E_NOT_ENOUGH_DATA; } } @@ -807,21 +781,12 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size int bytes = 0; iphone_lckd_client_t control; control = (iphone_lckd_client_t) transport; - if (debug) - printf("lockdownd_secuwrite() called\n"); - if (debug) - printf("pre-send\nlength = %zi\n", length); + log_debug_msg("lockdownd_secuwrite() called\n"); + log_debug_msg("pre-send\nlength = %zi\n", length); iphone_mux_send(control->connection, buffer, length, &bytes); - if (debug) - printf("post-send\nsent %i bytes\n", bytes); - if (debug) { - FILE *my_ssl_packet = fopen("sslpacketwrite.out", "w+"); - fwrite(buffer, 1, length, my_ssl_packet); - fflush(my_ssl_packet); - printf("Wrote SSL packet to drive, too.\n"); - fclose(my_ssl_packet); - } + log_debug_msg("post-send\nsent %i bytes\n", bytes); + dump_debug_buffer("sslpacketwrite.out", buffer, length); return bytes; } @@ -839,8 +804,7 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ char *hackhackhack = NULL; iphone_lckd_client_t control; control = (iphone_lckd_client_t) transport; - if (debug) - printf("lockdownd_securead() called\nlength = %zi\n", length); + log_debug_msg("lockdownd_securead() called\nlength = %zi\n", length); // Buffering hack! Throw what we've got in our "buffer" into the stream first, then get more. if (control->gtls_buffer_hack_len > 0) { if (length > control->gtls_buffer_hack_len) { // If it's asking for more than we got @@ -849,8 +813,7 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ memcpy(buffer, control->gtls_buffer_hack, control->gtls_buffer_hack_len); // Fill their buffer partially free(control->gtls_buffer_hack); // free our memory, it's not chained anymore control->gtls_buffer_hack_len = 0; // we don't have a hack buffer anymore - if (debug) - printf("Did a partial fill to help quench thirst for data\n"); + log_debug_msg("Did a partial fill to help quench thirst for data\n"); } else if (length < control->gtls_buffer_hack_len) { // If it's asking for less... control->gtls_buffer_hack_len -= length; // subtract what they're asking for memcpy(buffer, control->gtls_buffer_hack, length); // fill their buffer @@ -859,37 +822,33 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ free(control->gtls_buffer_hack); // Free the old one control->gtls_buffer_hack = hackhackhack; // And make it the new one. hackhackhack = NULL; - if (debug) - printf("Quenched the thirst for data; new hack length is %i\n", control->gtls_buffer_hack_len); + log_debug_msg("Quenched the thirst for data; new hack length is %i\n", control->gtls_buffer_hack_len); return length; // hand it over. } else { // length == hack length memcpy(buffer, control->gtls_buffer_hack, length); // copy our buffer into theirs free(control->gtls_buffer_hack); // free our "obligation" control->gtls_buffer_hack_len = 0; // free our "obligation" - if (debug) - printf("Satiated the thirst for data; now we have to eventually receive again.\n"); + log_debug_msg("Satiated the thirst for data; now we have to eventually receive again.\n"); return length; // hand it over } } // End buffering hack! char *recv_buffer = (char *) malloc(sizeof(char) * (length * 1000)); // ensuring nothing stupid happens - if (debug) - printf("pre-read\nclient wants %zi bytes\n", length); + log_debug_msg("pre-read\nclient wants %zi bytes\n", length); iphone_mux_recv(control->connection, recv_buffer, (length * 1000), &bytes); - if (debug) - printf("post-read\nwe got %i bytes\n", bytes); - if (debug && bytes < 0) { - printf("lockdownd_securead(): uh oh\n"); - printf("I believe what we have here is a failure to communicate... libusb says %s but strerror says %s\n", - usb_strerror(), strerror(errno)); + log_debug_msg("post-read\nwe got %i bytes\n", bytes); + if (bytes < 0) { + log_debug_msg("lockdownd_securead(): uh oh\n"); + log_debug_msg + ("I believe what we have here is a failure to communicate... libusb says %s but strerror says %s\n", + usb_strerror(), strerror(errno)); return bytes + 28; // an errno } if (bytes >= length) { if (bytes > length) { - if (debug) - printf - ("lockdownd_securead: Client deliberately read less data than was there; resorting to GnuTLS buffering hack.\n"); + log_debug_msg + ("lockdownd_securead: Client deliberately read less data than was there; resorting to GnuTLS buffering hack.\n"); if (!control->gtls_buffer_hack_len) { // if there's no hack buffer yet //control->gtls_buffer_hack = strndup(recv_buffer+length, bytes-length); // strndup is NOT a good solution! control->gtls_buffer_hack_len += bytes - length; @@ -905,12 +864,10 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ memcpy(buffer + pos_start_fill, recv_buffer, length); free(recv_buffer); if (bytes == length) { - if (debug) - printf("Returning how much we received.\n"); + log_debug_msg("Returning how much we received.\n"); return bytes; } else { - if (debug) - printf("Returning what they want to hear.\nHack length: %i\n", control->gtls_buffer_hack_len); + log_debug_msg("Returning what they want to hear.\nHack length: %i\n", control->gtls_buffer_hack_len); return length; } } @@ -988,13 +945,11 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char dictionary = read_dict_element_strings(dict); for (i = 0; dictionary[i]; i += 2) { - if (debug) - printf("lockdownd_start_service() dictionary %s: %s\n", dictionary[i], dictionary[i + 1]); + log_debug_msg("lockdownd_start_service() dictionary %s: %s\n", dictionary[i], dictionary[i + 1]); if (!xmlStrcmp(dictionary[i], "Port")) { port_loc = atoi(dictionary[i + 1]); - if (debug) - printf("lockdownd_start_service() atoi'd port: %i\n", port); + log_debug_msg("lockdownd_start_service() atoi'd port: %i\n", port); } if (!xmlStrcmp(dictionary[i], "Result")) { @@ -1004,11 +959,9 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char } } - if (debug) { - printf("lockdownd_start_service(): DATA RECEIVED:\n\n"); - fwrite(XML_query, 1, bytes, stdout); - printf("end data received by lockdownd_start_service()\n"); - } + log_debug_msg("lockdownd_start_service(): DATA RECEIVED:\n\n"); + log_debug_msg(XML_query); + log_debug_msg("end data received by lockdownd_start_service()\n"); free(XML_query); xmlFreeDoc(plist); diff --git a/src/usbmux.c b/src/usbmux.c index cf1a2d9..2114758 100644 --- a/src/usbmux.c +++ b/src/usbmux.c @@ -27,8 +27,6 @@ #include "usbmux.h" -extern int debug; - static iphone_umux_client_t *connlist = NULL; static int clients = 0; @@ -151,8 +149,7 @@ iphone_error_t iphone_mux_new_client(iphone_device_t device, uint16_t src_port, } else { free(response); - if (debug) - printf("mux_connect: connection success\n"); + log_debug_msg("mux_connect: connection success\n"); new_connection->header->tcp_flags = 0x10; new_connection->header->scnt = 1; new_connection->header->ocnt = 1; @@ -189,12 +186,12 @@ iphone_error_t iphone_mux_free_client(iphone_umux_client_t client) int bytes = 0; bytes = usb_bulk_write(client->phone->device, BULKOUT, (char *) client->header, sizeof(usbmux_tcp_header), 800); - if (debug && bytes < 0) - printf("iphone_muxèfree_client(): when writing, libusb gave me the error: %s\n", usb_strerror()); + if (bytes < 0) + log_debug_msg("iphone_muxèfree_client(): when writing, libusb gave me the error: %s\n", usb_strerror()); bytes = usb_bulk_read(client->phone->device, BULKIN, (char *) client->header, sizeof(usbmux_tcp_header), 800); - if (debug && bytes < 0) - printf("get_iPhone(): when reading, libusb gave me the error: %s\n", usb_strerror()); + if (bytes < 0) + log_debug_msg("get_iPhone(): when reading, libusb gave me the error: %s\n", usb_strerror()); delete_connection(client); @@ -220,8 +217,7 @@ iphone_error_t iphone_mux_send(iphone_umux_client_t client, const char *data, ui // client->scnt and client->ocnt should already be in host notation... // we don't need to change them juuuust yet. *sent_bytes = 0; - if (debug) - printf("mux_send(): client wants to send %i bytes\n", datalen); + log_debug_msg("mux_send(): client wants to send %i bytes\n", datalen); char *buffer = (char *) malloc(sizeof(usbmux_tcp_header) + datalen + 2); // allow 2 bytes of safety padding // Set the length and pre-emptively htonl/htons it client->header->length = htonl(sizeof(usbmux_tcp_header) + datalen); @@ -235,21 +231,13 @@ iphone_error_t iphone_mux_send(iphone_umux_client_t client, const char *data, ui memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen); // We have a buffer full of data, we should now send it to the phone. - if (debug) - printf("actually sending %zi bytes of data at %p\n", sizeof(usbmux_tcp_header) + datalen, buffer); + log_debug_msg("actually sending %zi bytes of data at %p\n", sizeof(usbmux_tcp_header) + datalen, buffer); *sent_bytes = send_to_phone(client->phone, buffer, sizeof(usbmux_tcp_header) + datalen); - if (debug) - printf("mux_send: sent %i bytes!\n", *sent_bytes); + log_debug_msg("mux_send: sent %i bytes!\n", *sent_bytes); // Now that we've sent it off, we can clean up after our sloppy selves. - if (debug) { - FILE *packet = fopen("packet", "a+"); - fwrite(buffer, 1, *sent_bytes, packet); - fclose(packet); - printf("\n"); - } - + dump_debug_buffer("packet", buffer, *sent_bytes); if (buffer) free(buffer); // Re-calculate scnt and ocnt @@ -294,8 +282,7 @@ iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t * a.) Check incoming packet's ports. If proper, follow proper buffering and receiving operation. * b.) If not, find the client the ports belong to and fill that client's buffer, then return mux_recv with the same args to try again. */ - if (debug) - printf("mux_recv: datalen == %i\n", datalen); + log_debug_msg("mux_recv: datalen == %i\n", datalen); int bytes = 0, i = 0, complex = 0, offset = 0; *recv_bytes = 0; char *buffer = NULL; @@ -333,8 +320,7 @@ iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t bytes = recv_from_phone(client->phone, buffer, 131072); if (bytes < 28) { free(buffer); - if (debug) - printf("mux_recv: Did not even get the header.\n"); + log_debug_msg("mux_recv: Did not even get the header.\n"); return IPHONE_E_NOT_ENOUGH_DATA; } @@ -390,7 +376,6 @@ iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t } // If we get to this point, 'tis probably bad. - if (debug) - printf("mux_recv: Heisenbug: bytes and datalen not matching up\n"); + log_debug_msg("mux_recv: Heisenbug: bytes and datalen not matching up\n"); return IPHONE_E_UNKNOWN_ERROR; } diff --git a/src/userpref.c b/src/userpref.c index 57946f7..db54679 100644 --- a/src/userpref.c +++ b/src/userpref.c @@ -24,6 +24,7 @@ #include <stdio.h> #include <string.h> #include "userpref.h" +#include "utils.h" #include <string.h> #include <stdlib.h> @@ -35,7 +36,6 @@ #define LIBIPHONE_ROOT_CERTIF "RootCertificate.pem" #define LIBIPHONE_HOST_CERTIF "HostCertificate.pem" -extern int debug; /** Creates a freedesktop compatible configuration directory for libiphone. */ @@ -77,8 +77,7 @@ char *get_host_id() g_key_file_free(key_file); g_free(config_file); - if (debug) - printf("get_host_id(): Using %s as HostID\n", host_id); + log_debug_msg("get_host_id(): Using %s as HostID\n", host_id); return host_id; } @@ -246,8 +245,7 @@ int init_config_file(char *host_id, gnutls_datum_t * root_key, gnutls_datum_t * key_file = g_key_file_new(); /* Store in config file */ - if (debug) - printf("init_config_file(): setting hostID to %s\n", host_id); + log_debug_msg("init_config_file(): setting hostID to %s\n", host_id); g_key_file_set_value(key_file, "Global", "HostID", host_id); /* Write config file on disk */ diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..e54403b --- /dev/null +++ b/src/utils.c @@ -0,0 +1,80 @@ +/* + * utils.c + * contains utilitary methos for logging and debugging + * + * Copyright (c) 2008 Jonathan Beck All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include <stdarg.h> +#include <stdio.h> +#include "utils.h" + +int toto_debug = 0; + +/** + * Sets the level of debugging. Currently the only acceptable values are 0 and + * 1. + * + * @param level Set to 0 for no debugging or 1 for debugging. + */ +void iphone_set_debug(int level) +{ + toto_debug = level; +} + + + +void log_debug_msg(const char *format, ...) +{ +#ifndef STRIP_DEBUG + + va_list args; + /* run the real fprintf */ + va_start(args, format); + + if (toto_debug) + fprintf(stderr, format, args); + + va_end(args); + +#endif +} + +inline void log_debug_buffer(const char *data, const int length) +{ +#ifndef STRIP_DEBUG + + /* run the real fprintf */ + if (toto_debug) + fwrite(data, 1, length, stderr); + +#endif +} + +inline void dump_debug_buffer(const char *file, const char *data, const int length) +{ +#ifndef STRIP_DEBUG + + /* run the real fprintf */ + if (toto_debug) { + FILE *my_ssl_packet = fopen(file, "w+"); + fwrite(data, 1, length, my_ssl_packet); + fflush(my_ssl_packet); + fprintf(stderr, "Wrote SSL packet to drive, too.\n"); + fclose(my_ssl_packet); + } +#endif +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..489f610 --- /dev/null +++ b/src/utils.h @@ -0,0 +1,30 @@ +/* + * utils.h + * contains utilitary methos for logging and debugging + * + * Copyright (c) 2008 Jonathan Beck All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef UTILS_H +#define UTILS_H + +#include "libiphone/libiphone.h" + +inline void log_debug_msg(const char *format, ...); +inline void log_debug_buffer(const char *data, const int length); +inline void dump_debug_buffer(const char *file, const char *data, const int length); +#endif |