summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2008-10-25 16:11:27 +0200
committerGravatar Jonathan Beck2008-10-25 16:52:20 +0200
commit0b2cfd2c7c6211ff5902e48720c34067f238ce90 (patch)
tree9c30578b4288795e7589c959caa08f3547ca0b88 /src
parentbbd813da84c1ff8484bbd3eb7f575775271424da (diff)
downloadlibimobiledevice-0b2cfd2c7c6211ff5902e48720c34067f238ce90.tar.gz
libimobiledevice-0b2cfd2c7c6211ff5902e48720c34067f238ce90.tar.bz2
Handle debugging through utilitary functions
Diffstat (limited to 'src')
-rw-r--r--src/AFC.c116
-rw-r--r--src/Makefile.am4
-rw-r--r--src/initconf.c3
-rw-r--r--src/iphone.c56
-rw-r--r--src/lockdown.c151
-rw-r--r--src/usbmux.c39
-rw-r--r--src/userpref.c8
-rw-r--r--src/utils.c80
-rw-r--r--src/utils.h30
9 files changed, 239 insertions, 248 deletions
diff --git a/src/AFC.c b/src/AFC.c
index 58c6bb2..4d6b269 100644
--- a/src/AFC.c
+++ b/src/AFC.c
@@ -27,16 +27,13 @@
27// This is the maximum size an AFC data packet can be 27// This is the maximum size an AFC data packet can be
28const int MAXIMUM_PACKET_SIZE = (2 << 15) - 32; 28const int MAXIMUM_PACKET_SIZE = (2 << 15) - 32;
29 29
30extern int debug;
31
32/** Locks an AFC client, done for thread safety stuff 30/** Locks an AFC client, done for thread safety stuff
33 * 31 *
34 * @param client The AFC client connection to lock 32 * @param client The AFC client connection to lock
35 */ 33 */
36static void afc_lock(iphone_afc_client_t client) 34static void afc_lock(iphone_afc_client_t client)
37{ 35{
38 if (debug) 36 log_debug_msg("Locked\n");
39 fprintf(stderr, "Locked\n");
40 while (client->lock) { 37 while (client->lock) {
41 usleep(500); // they say it's obsolete, but whatever 38 usleep(500); // they say it's obsolete, but whatever
42 } 39 }
@@ -49,8 +46,7 @@ static void afc_lock(iphone_afc_client_t client)
49 */ 46 */
50static void afc_unlock(iphone_afc_client_t client) 47static void afc_unlock(iphone_afc_client_t client)
51{ // just to be pretty 48{ // just to be pretty
52 if (debug) 49 log_debug_msg("Unlocked\n");
53 fprintf(stderr, "Unlocked\n");
54 client->lock = 0; 50 client->lock = 0;
55} 51}
56 52
@@ -156,16 +152,12 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int
156 memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); 152 memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket));
157 offset = client->afc_packet->this_length - sizeof(AFCPacket); 153 offset = client->afc_packet->this_length - sizeof(AFCPacket);
158 154
159 if (debug) 155 log_debug_msg("dispatch_AFC_packet: Offset: %i\n", offset);
160 fprintf(stderr, "dispatch_AFC_packet: Offset: %i\n", offset);
161 if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) { 156 if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) {
162 if (debug) { 157 log_debug_msg("dispatch_AFC_packet: Length did not resemble what it was supposed");
163 fprintf(stderr, "dispatch_AFC_packet: Length did not resemble what it was supposed"); 158 log_debug_msg("to based on the packet.\n");
164 fprintf(stderr, "to based on the packet.\n"); 159 log_debug_msg("length minus offset: %i\n", length - offset);
165 fprintf(stderr, "length minus offset: %i\n", length - offset); 160 log_debug_msg("rest of packet: %i\n", client->afc_packet->entire_length - client->afc_packet->this_length);
166 fprintf(stderr, "rest of packet: %i\n",
167 client->afc_packet->entire_length - client->afc_packet->this_length);
168 }
169 free(buffer); 161 free(buffer);
170 return -1; 162 return -1;
171 } 163 }
@@ -176,32 +168,25 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int
176 return bytes; 168 return bytes;
177 } 169 }
178 170
179 if (debug) { 171 log_debug_msg("dispatch_AFC_packet: sent the first now go with the second\n");
180 fprintf(stderr, "dispatch_AFC_packet: sent the first now go with the second\n"); 172 log_debug_msg("Length: %i\n", length - offset);
181 fprintf(stderr, "Length: %i\n", length - offset); 173 log_debug_msg("Buffer: \n");
182 fprintf(stderr, "Buffer: \n"); 174 log_debug_msg(data + offset);
183 fwrite(data + offset, 1, length - offset, stdout);
184 }
185 175
186 iphone_mux_send(client->connection, data + offset, length - offset, &bytes); 176 iphone_mux_send(client->connection, data + offset, length - offset, &bytes);
187 return bytes; 177 return bytes;
188 } else { 178 } else {
189 if (debug) 179 log_debug_msg("dispatch_AFC_packet doin things the old way\n");
190 fprintf(stderr, "dispatch_AFC_packet doin things the old way\n");
191 char *buffer = (char *) malloc(sizeof(char) * client->afc_packet->this_length); 180 char *buffer = (char *) malloc(sizeof(char) * client->afc_packet->this_length);
192 if (debug) 181 log_debug_msg("dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length);
193 fprintf(stderr, "dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length);
194 memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); 182 memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket));
195 if (debug) 183 log_debug_msg("dispatch_AFC_packet packet data follows\n");
196 fprintf(stderr, "dispatch_AFC_packet packet data follows\n");
197 if (length > 0) { 184 if (length > 0) {
198 memcpy(buffer + sizeof(AFCPacket), data, length); 185 memcpy(buffer + sizeof(AFCPacket), data, length);
199 buffer[sizeof(AFCPacket) + length] = '\0'; 186 buffer[sizeof(AFCPacket) + length] = '\0';
200 } 187 }
201 if (debug) 188 log_debug_buffer(buffer, client->afc_packet->this_length);
202 fwrite(buffer, 1, client->afc_packet->this_length, stdout); 189 log_debug_msg("\n");
203 if (debug)
204 fprintf(stderr, "\n");
205 iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes); 190 iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes);
206 191
207 if (buffer) { 192 if (buffer) {
@@ -257,30 +242,23 @@ static int receive_AFC_data(iphone_afc_client_t client, char **dump_here)
257 free(buffer); 242 free(buffer);
258 243
259 if (r_packet->operation == AFC_ERROR && !(client->afc_packet->operation == AFC_DELETE && param1 == 7)) { 244 if (r_packet->operation == AFC_ERROR && !(client->afc_packet->operation == AFC_DELETE && param1 == 7)) {
260 if (debug) 245 log_debug_msg("Oops? Bad operation code received: 0x%X, operation=0x%X, param1=%d\n",
261 fprintf(stderr, 246 r_packet->operation, client->afc_packet->operation, param1);
262 "Oops? Bad operation code received: 0x%X, operation=0x%X, param1=%d\n",
263 r_packet->operation, client->afc_packet->operation, param1);
264 recv_len = r_packet->entire_length - r_packet->this_length; 247 recv_len = r_packet->entire_length - r_packet->this_length;
265 free(r_packet); 248 free(r_packet);
266 if (debug) 249 log_debug_msg("recv_len=%d\n", recv_len);
267 fprintf(stderr, "recv_len=%d\n", recv_len);
268 if (param1 == 0) { 250 if (param1 == 0) {
269 if (debug) 251 log_debug_msg("... false alarm, but still\n");
270 fprintf(stderr, "... false alarm, but still\n");
271 *dump_here = NULL; 252 *dump_here = NULL;
272 return 0; 253 return 0;
273 } else { 254 } else {
274 if (debug) 255 log_debug_msg("Errno %i\n", param1);
275 fprintf(stderr, "Errno %i\n", param1);
276 } 256 }
277 *dump_here = NULL; 257 *dump_here = NULL;
278 return -1; 258 return -1;
279 } else { 259 } else {
280 if (debug) 260 log_debug_msg("Operation code %x\nFull length %i and this length %i\n",
281 fprintf(stderr, 261 r_packet->operation, r_packet->entire_length, r_packet->this_length);
282 "Operation code %x\nFull length %i and this length %i\n",
283 r_packet->operation, r_packet->entire_length, r_packet->this_length);
284 } 262 }
285 263
286 recv_len = r_packet->entire_length - r_packet->this_length; 264 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)
294 final_buffer = (char *) malloc(sizeof(char) * recv_len); 272 final_buffer = (char *) malloc(sizeof(char) * recv_len);
295 while (current_count < recv_len) { 273 while (current_count < recv_len) {
296 iphone_mux_recv(client->connection, buffer, recv_len - current_count, &bytes); 274 iphone_mux_recv(client->connection, buffer, recv_len - current_count, &bytes);
297 if (debug) 275 log_debug_msg("receive_AFC_data: still collecting packets\n");
298 fprintf(stderr, "receive_AFC_data: still collecting packets\n");
299 if (bytes < 0) { 276 if (bytes < 0) {
300 if (debug) 277 log_debug_msg("receive_AFC_data: mux_recv failed: %d\n", bytes);
301 fprintf(stderr, "receive_AFC_data: mux_recv failed: %d\n", bytes);
302 break; 278 break;
303 } 279 }
304 if (bytes > recv_len - current_count) { 280 if (bytes > recv_len - current_count) {
305 if (debug) 281 log_debug_msg("receive_AFC_data: mux_recv delivered too much data\n");
306 fprintf(stderr, "receive_AFC_data: mux_recv delivered too much data\n");
307 break; 282 break;
308 } 283 }
309 if (bytes > 7 && strstr(buffer, "CFA6LPAA")) { 284 if (bytes > 7 && strstr(buffer, "CFA6LPAA")) {
310 if (debug) 285 log_debug_msg("receive_AFC_data: WARNING: there is AFC data in this packet at %ti\n",
311 fprintf(stderr, 286 strstr(buffer, "CFA6LPAA") - buffer);
312 "receive_AFC_data: WARNING: there is AFC data in this packet at %ti\n", 287 log_debug_msg("receive_AFC_data: the total packet length is %i\n", bytes);
313 strstr(buffer, "CFA6LPAA") - buffer);
314 if (debug)
315 fprintf(stderr, "receive_AFC_data: the total packet length is %i\n", bytes);
316 } 288 }
317 289
318 memcpy(final_buffer + current_count, buffer, bytes); 290 memcpy(final_buffer + current_count, buffer, bytes);
@@ -701,8 +673,7 @@ iphone_afc_open_file(iphone_afc_client_t client, const char *filename,
701 free(data); 673 free(data);
702 674
703 if (bytes <= 0) { 675 if (bytes <= 0) {
704 if (debug) 676 log_debug_msg("afc_open_file: Didn't receive a response to the command\n");
705 fprintf(stderr, "afc_open_file: Didn't receive a response to the command\n");
706 afc_unlock(client); 677 afc_unlock(client);
707 return IPHONE_E_NOT_ENOUGH_DATA; 678 return IPHONE_E_NOT_ENOUGH_DATA;
708 } 679 }
@@ -718,8 +689,7 @@ iphone_afc_open_file(iphone_afc_client_t client, const char *filename,
718 *file = file_loc; 689 *file = file_loc;
719 return IPHONE_E_SUCCESS; 690 return IPHONE_E_SUCCESS;
720 } else { 691 } else {
721 if (debug) 692 log_debug_msg("afc_open_file: Didn't get any further data\n");
722 fprintf(stderr, "afc_open_file: Didn't get any further data\n");
723 afc_unlock(client); 693 afc_unlock(client);
724 return IPHONE_E_NOT_ENOUGH_DATA; 694 return IPHONE_E_NOT_ENOUGH_DATA;
725 } 695 }
@@ -747,16 +717,14 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d
747 717
748 if (!client || !client->afc_packet || !client->connection || !file) 718 if (!client || !client->afc_packet || !client->connection || !file)
749 return IPHONE_E_INVALID_ARG; 719 return IPHONE_E_INVALID_ARG;
750 if (debug) 720 log_debug_msg("afc_read_file called for length %i\n", length);
751 fprintf(stderr, "afc_read_file called for length %i\n", length);
752 721
753 afc_lock(client); 722 afc_lock(client);
754 723
755 // Looping here to get around the maximum amount of data that 724 // Looping here to get around the maximum amount of data that
756 // recieve_AFC_data can handle 725 // recieve_AFC_data can handle
757 while (current_count < length) { 726 while (current_count < length) {
758 if (debug) 727 log_debug_msg("afc_read_file: current count is %i but length is %i\n", current_count, length);
759 fprintf(stderr, "afc_read_file: current count is %i but length is %i\n", current_count, length);
760 728
761 // Send the read command 729 // Send the read command
762 AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); 730 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
774 } 742 }
775 // Receive the data 743 // Receive the data
776 bytes_loc = receive_AFC_data(client, &input); 744 bytes_loc = receive_AFC_data(client, &input);
777 if (debug) 745 log_debug_msg("afc_read_file: bytes returned: %i\n", bytes_loc);
778 fprintf(stderr, "afc_read_file: bytes returned: %i\n", bytes_loc);
779 if (bytes_loc < 0) { 746 if (bytes_loc < 0) {
780 if (input) 747 if (input)
781 free(input); 748 free(input);
@@ -790,8 +757,7 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d
790 // success 757 // success
791 } else { 758 } else {
792 if (input) { 759 if (input) {
793 if (debug) 760 log_debug_msg("afc_read_file: %d\n", bytes_loc);
794 fprintf(stderr, "afc_read_file: %d\n", bytes_loc);
795 memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc); 761 memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc);
796 free(input); 762 free(input);
797 input = NULL; 763 input = NULL;
@@ -799,8 +765,7 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d
799 } 765 }
800 } 766 }
801 } 767 }
802 if (debug) 768 log_debug_msg("afc_read_file: returning current_count as %i\n", current_count);
803 fprintf(stderr, "afc_read_file: returning current_count as %i\n", current_count);
804 769
805 afc_unlock(client); 770 afc_unlock(client);
806 *bytes = current_count; 771 *bytes = current_count;
@@ -831,8 +796,7 @@ iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file,
831 796
832 afc_lock(client); 797 afc_lock(client);
833 798
834 if (debug) 799 log_debug_msg("afc_write_file: Write length: %i\n", length);
835 fprintf(stderr, "afc_write_file: Write length: %i\n", length);
836 800
837 // Divide the file into segments. 801 // Divide the file into segments.
838 for (i = 0; i < segments; i++) { 802 for (i = 0; i < segments; i++) {
@@ -893,8 +857,7 @@ iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file,
893 bytes_loc = receive_AFC_data(client, &acknowledgement); 857 bytes_loc = receive_AFC_data(client, &acknowledgement);
894 afc_unlock(client); 858 afc_unlock(client);
895 if (bytes_loc < 0) { 859 if (bytes_loc < 0) {
896 if (debug) 860 log_debug_msg("afc_write_file: uh oh?\n");
897 fprintf(stderr, "afc_write_file: uh oh?\n");
898 } 861 }
899 *bytes = current_count; 862 *bytes = current_count;
900 return IPHONE_E_SUCCESS; 863 return IPHONE_E_SUCCESS;
@@ -916,8 +879,7 @@ iphone_error_t iphone_afc_close_file(iphone_afc_client_t client, iphone_afc_file
916 879
917 afc_lock(client); 880 afc_lock(client);
918 881
919 if (debug) 882 log_debug_msg("afc_close_file: File handle %i\n", file->filehandle);
920 fprintf(stderr, "afc_close_file: File handle %i\n", file->filehandle);
921 883
922 // Send command 884 // Send command
923 memcpy(buffer, &file->filehandle, sizeof(uint32)); 885 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
7iphoneclient_SOURCES = main.c 7iphoneclient_SOURCES = main.c
8iphoneclient_LDADD = libiphone.la 8iphoneclient_LDADD = libiphone.la
9 9
10libiphone_initconf_SOURCES = initconf.c userpref.c lockdown.c plist.c usbmux.c iphone.c 10libiphone_initconf_SOURCES = initconf.c userpref.c lockdown.c plist.c usbmux.c iphone.c utils.c
11libiphone_initconf_CFLAGS = $(libgthread2_CFLAGS) $(AM_CFLAGS) 11libiphone_initconf_CFLAGS = $(libgthread2_CFLAGS) $(AM_CFLAGS)
12libiphone_initconf_LDFLAGS = $(libgthread2_LIBS) $(AM_LDFLAGS) 12libiphone_initconf_LDFLAGS = $(libgthread2_LIBS) $(AM_LDFLAGS)
13 13
@@ -17,4 +17,4 @@ lckd_client_LDFLAGS = -lreadline $(AM_LDFLAGS)
17lckd_client_LDADD = libiphone.la 17lckd_client_LDADD = libiphone.la
18 18
19lib_LTLIBRARIES = libiphone.la 19lib_LTLIBRARIES = libiphone.la
20libiphone_la_SOURCES = usbmux.c iphone.c plist.c lockdown.c AFC.c userpref.c 20libiphone_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 @@
26#include <gnutls/x509.h> 26#include <gnutls/x509.h>
27#include <glib.h> 27#include <glib.h>
28 28
29#include "libiphone/libiphone.h"
29#include "userpref.h" 30#include "userpref.h"
30#include "lockdown.h" 31#include "lockdown.h"
31 32#include "utils.h"
32 33
33/** Generates a 2048 byte key, split into a function so that it can be run in a 34/** Generates a 2048 byte key, split into a function so that it can be run in a
34 * thread. 35 * 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 @@
21 21
22#include "usbmux.h" 22#include "usbmux.h"
23#include "iphone.h" 23#include "iphone.h"
24#include "utils.h"
24#include <arpa/inet.h> 25#include <arpa/inet.h>
25#include <usb.h> 26#include <usb.h>
26#include <stdio.h> 27#include <stdio.h>
27#include <stdlib.h> 28#include <stdlib.h>
28#include <string.h> 29#include <string.h>
29 30
30int iphone_debug = 0;
31
32/**
33 * Sets the level of debugging. Currently the only acceptable values are 0 and
34 * 1.
35 *
36 * @param level Set to 0 for no debugging or 1 for debugging.
37 */
38void iphone_set_debug(int level)
39{
40 iphone_debug = level;
41}
42 31
43/** 32/**
44 * Given a USB bus and device number, returns a device handle to the iPhone on 33 * 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
90 } 79 }
91 80
92 iphone_free_device(phone); 81 iphone_free_device(phone);
93 if (iphone_debug) 82
94 fprintf(stderr, "iphone_get_specific_device: iPhone not found\n"); 83 log_debug_msg("iphone_get_specific_device: iPhone not found\n");
95 return IPHONE_E_NO_DEVICE; 84 return IPHONE_E_NO_DEVICE;
96 85
97 found: 86 found:
98 // Send the version command to the phone 87 // Send the version command to the phone
99 version = version_header(); 88 version = version_header();
100 bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800); 89 bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800);
101 if (bytes < 20 && iphone_debug) { 90 if (bytes < 20) {
102 fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n"); 91 log_debug_msg("get_iPhone(): libusb did NOT send enough!\n");
103 if (bytes < 0) { 92 if (bytes < 0) {
104 fprintf(stderr, "get_iPhone(): libusb gave me the error %d: %s (%s)\n", 93 log_debug_msg("get_iPhone(): libusb gave me the error %d: %s (%s)\n",
105 bytes, usb_strerror(), strerror(-bytes)); 94 bytes, usb_strerror(), strerror(-bytes));
106 } 95 }
107 } 96 }
108 // Read the phone's response 97 // 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
112 if (bytes < 20) { 101 if (bytes < 20) {
113 free(version); 102 free(version);
114 iphone_free_device(phone); 103 iphone_free_device(phone);
115 if (iphone_debug) 104 log_debug_msg("get_iPhone(): Invalid version message -- header too short.\n");
116 fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n"); 105 if (bytes < 0)
117 if (iphone_debug && bytes < 0) 106 log_debug_msg("get_iPhone(): libusb error message %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes));
118 fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n",
119 bytes, usb_strerror(), strerror(-bytes));
120 return IPHONE_E_NOT_ENOUGH_DATA; 107 return IPHONE_E_NOT_ENOUGH_DATA;
121 } 108 }
122 // Check for correct version 109 // Check for correct version
@@ -130,14 +117,12 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t
130 // Bad header 117 // Bad header
131 iphone_free_device(phone); 118 iphone_free_device(phone);
132 free(version); 119 free(version);
133 if (iphone_debug) 120 log_debug_msg("get_iPhone(): Received a bad header/invalid version number.");
134 fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number.");
135 return IPHONE_E_BAD_HEADER; 121 return IPHONE_E_BAD_HEADER;
136 } 122 }
137 123
138 // If it got to this point it's gotta be bad 124 // If it got to this point it's gotta be bad
139 if (iphone_debug) 125 log_debug_msg("get_iPhone(): Unknown error.\n");
140 fprintf(stderr, "get_iPhone(): Unknown error.\n");
141 iphone_free_device(phone); 126 iphone_free_device(phone);
142 free(version); 127 free(version);
143 return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad 128 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)
218 203
219 if (!phone) 204 if (!phone)
220 return -1; 205 return -1;
221 if (iphone_debug) 206 log_debug_msg("send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data);
222 fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data);
223 207
224 bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800); 208 bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800);
225 if (bytes < datalen) { 209 if (bytes < datalen) {
226 if (iphone_debug && bytes < 0) 210 if (bytes < 0)
227 fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), 211 log_debug_msg("send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(),
228 strerror(-bytes)); 212 strerror(-bytes));
229 return -1; 213 return -1;
230 } else { 214 } else {
231 return bytes; 215 return bytes;
@@ -250,14 +234,12 @@ int recv_from_phone(iphone_device_t phone, char *data, int datalen)
250 234
251 if (!phone) 235 if (!phone)
252 return -1; 236 return -1;
253 if (iphone_debug) 237 log_debug_msg("recv_from_phone(): attempting to receive %i bytes\n", datalen);
254 fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen);
255 238
256 bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500); 239 bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500);
257 if (bytes < 0) { 240 if (bytes < 0) {
258 if (iphone_debug) 241 log_debug_msg("recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(),
259 fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), 242 strerror(-bytes));
260 strerror(-bytes));
261 return -1; 243 return -1;
262 } 244 }
263 245
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 @@
30#include <libtasn1.h> 30#include <libtasn1.h>
31#include <gnutls/x509.h> 31#include <gnutls/x509.h>
32 32
33extern int debug;
34
35const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = { 33const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = {
36 {"PKCS1", 536872976, 0}, 34 {"PKCS1", 536872976, 0},
37 {0, 1073741836, 0}, 35 {0, 1073741836, 0},
@@ -176,13 +174,8 @@ iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uin
176 length = htonl(length); 174 length = htonl(length);
177 memcpy(real_query, &length, sizeof(length)); 175 memcpy(real_query, &length, sizeof(length));
178 memcpy(real_query + 4, raw_data, ntohl(length)); 176 memcpy(real_query + 4, raw_data, ntohl(length));
179 if (debug) { 177 log_debug_msg("lockdownd_send(): made the query, sending it along\n");
180 printf("lockdownd_send(): made the query, sending it along\n"); 178 dump_debug_buffer("grpkt", real_query, ntohl(length) + 4);
181 FILE *packet = fopen("grpkt", "w");
182 fwrite(real_query, 1, ntohl(length) + 4, packet);
183 fclose(packet);
184 packet = NULL;
185 }
186 179
187 if (!client->in_SSL) 180 if (!client->in_SSL)
188 ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes); 181 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
190 gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length)); 183 gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length));
191 ret = IPHONE_E_SUCCESS; 184 ret = IPHONE_E_SUCCESS;
192 } 185 }
193 if (debug) 186 log_debug_msg("lockdownd_send(): sent it!\n");
194 printf("lockdownd_send(): sent it!\n");
195 free(real_query); 187 free(real_query);
196 *sent_bytes = bytes; 188 *sent_bytes = bytes;
197 return ret; 189 return ret;
@@ -215,8 +207,7 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control)
215 int bytes = 0, i = 0; 207 int bytes = 0, i = 0;
216 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 208 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
217 209
218 if (debug) 210 log_debug_msg("lockdownd_hello() called\n");
219 printf("lockdownd_hello() called\n");
220 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 211 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0);
221 key = add_key_str_dict_element(plist, dict, "Request", "QueryType", 1); 212 key = add_key_str_dict_element(plist, dict, "Request", "QueryType", 1);
222 char *XML_content; 213 char *XML_content;
@@ -246,8 +237,7 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control)
246 237
247 for (i = 0; dictionary[i]; i += 2) { 238 for (i = 0; dictionary[i]; i += 2) {
248 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 239 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) {
249 if (debug) 240 log_debug_msg("lockdownd_hello(): success\n");
250 printf("lockdownd_hello(): success\n");
251 ret = IPHONE_E_SUCCESS; 241 ret = IPHONE_E_SUCCESS;
252 break; 242 break;
253 } 243 }
@@ -479,11 +469,9 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch
479 if (ret != IPHONE_E_SUCCESS) 469 if (ret != IPHONE_E_SUCCESS)
480 return ret; 470 return ret;
481 471
482 if (debug) { 472 log_debug_msg("lockdown_pair_device: iPhone's response to our pair request:\n");
483 printf("lockdown_pair_device: iPhone's response to our pair request:\n"); 473 log_debug_msg(XML_content);
484 fwrite(XML_content, 1, bytes, stdout); 474 log_debug_msg("\n\n");
485 printf("\n\n");
486 }
487 475
488 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); 476 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0);
489 if (!plist) { 477 if (!plist) {
@@ -519,13 +507,11 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch
519 507
520 /* store public key in config if pairing succeeded */ 508 /* store public key in config if pairing succeeded */
521 if (success) { 509 if (success) {
522 if (debug) 510 log_debug_msg("lockdownd_pair_device: pair success\n");
523 printf("lockdownd_pair_device: pair success\n");
524 store_device_public_key(uid, public_key_b64); 511 store_device_public_key(uid, public_key_b64);
525 ret = IPHONE_E_SUCCESS; 512 ret = IPHONE_E_SUCCESS;
526 } else { 513 } else {
527 if (debug) 514 log_debug_msg("lockdownd_pair_device: pair failure\n");
528 printf("lockdownd_pair_device: pair failure\n");
529 ret = IPHONE_E_PAIRING_FAILED; 515 ret = IPHONE_E_PAIRING_FAILED;
530 } 516 }
531 free(public_key_b64); 517 free(public_key_b64);
@@ -678,15 +664,13 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c
678 664
679 key = add_key_str_dict_element(plist, dict, "HostID", HostID, 1); 665 key = add_key_str_dict_element(plist, dict, "HostID", HostID, 1);
680 if (!key) { 666 if (!key) {
681 if (debug) 667 log_debug_msg("Couldn't add a key.\n");
682 printf("Couldn't add a key.\n");
683 xmlFreeDoc(plist); 668 xmlFreeDoc(plist);
684 return IPHONE_E_DICT_ERROR; 669 return IPHONE_E_DICT_ERROR;
685 } 670 }
686 key = add_key_str_dict_element(plist, dict, "Request", "StartSession", 1); 671 key = add_key_str_dict_element(plist, dict, "Request", "StartSession", 1);
687 if (!key) { 672 if (!key) {
688 if (debug) 673 log_debug_msg("Couldn't add a key.\n");
689 printf("Couldn't add a key.\n");
690 xmlFreeDoc(plist); 674 xmlFreeDoc(plist);
691 return IPHONE_E_DICT_ERROR; 675 return IPHONE_E_DICT_ERROR;
692 } 676 }
@@ -719,8 +703,7 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c
719 //gnutls_anon_client_credentials_t anoncred; 703 //gnutls_anon_client_credentials_t anoncred;
720 gnutls_certificate_credentials_t xcred; 704 gnutls_certificate_credentials_t xcred;
721 705
722 if (debug) 706 log_debug_msg("We started the session OK, now trying GnuTLS\n");
723 printf("We started the session OK, now trying GnuTLS\n");
724 errno = 0; 707 errno = 0;
725 gnutls_global_init(); 708 gnutls_global_init();
726 //gnutls_anon_allocate_client_credentials(&anoncred); 709 //gnutls_anon_allocate_client_credentials(&anoncred);
@@ -743,32 +726,25 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c
743 } 726 }
744 gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. 727 gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me.
745 728
746 if (debug) 729 log_debug_msg("GnuTLS step 1...\n");
747 printf("GnuTLS step 1...\n");
748 gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control); 730 gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control);
749 if (debug) 731 log_debug_msg("GnuTLS step 2...\n");
750 printf("GnuTLS step 2...\n");
751 gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite); 732 gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite);
752 if (debug) 733 log_debug_msg("GnuTLS step 3...\n");
753 printf("GnuTLS step 3...\n");
754 gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead); 734 gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead);
755 if (debug) 735 log_debug_msg("GnuTLS step 4 -- now handshaking...\n");
756 printf("GnuTLS step 4 -- now handshaking...\n");
757 736
758 if (errno && debug) 737 if (errno)
759 printf("WARN: errno says %s before handshake!\n", strerror(errno)); 738 log_debug_msg("WARN: errno says %s before handshake!\n", strerror(errno));
760 return_me = gnutls_handshake(*control->ssl_session); 739 return_me = gnutls_handshake(*control->ssl_session);
761 if (debug) 740 log_debug_msg("GnuTLS handshake done...\n");
762 printf("GnuTLS handshake done...\n");
763 741
764 free_dictionary(dictionary); 742 free_dictionary(dictionary);
765 743
766 if (return_me != GNUTLS_E_SUCCESS) { 744 if (return_me != GNUTLS_E_SUCCESS) {
767 if (debug) 745 log_debug_msg("GnuTLS reported something wrong.\n");
768 printf("GnuTLS reported something wrong.\n");
769 gnutls_perror(return_me); 746 gnutls_perror(return_me);
770 if (debug) 747 log_debug_msg("oh.. errno says %s\n", strerror(errno));
771 printf("oh.. errno says %s\n", strerror(errno));
772 return IPHONE_E_SSL_ERROR; 748 return IPHONE_E_SSL_ERROR;
773 } else { 749 } else {
774 control->in_SSL = 1; 750 control->in_SSL = 1;
@@ -777,19 +753,17 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c
777 } 753 }
778 } 754 }
779 755
780 if (debug) { 756 log_debug_msg("Apparently failed negotiating with lockdownd.\n");
781 printf("Apparently failed negotiating with lockdownd.\n"); 757 log_debug_msg("Responding dictionary: \n");
782 printf("Responding dictionary: \n"); 758 for (i = 0; dictionary[i]; i += 2) {
783 for (i = 0; dictionary[i]; i += 2) { 759 log_debug_msg("\t%s: %s\n", dictionary[i], dictionary[i + 1]);
784 printf("\t%s: %s\n", dictionary[i], dictionary[i + 1]);
785 }
786 } 760 }
787 761
762
788 free_dictionary(dictionary); 763 free_dictionary(dictionary);
789 return IPHONE_E_SSL_ERROR; 764 return IPHONE_E_SSL_ERROR;
790 } else { 765 } else {
791 if (debug) 766 log_debug_msg("Didn't get enough bytes.\n");
792 printf("Didn't get enough bytes.\n");
793 return IPHONE_E_NOT_ENOUGH_DATA; 767 return IPHONE_E_NOT_ENOUGH_DATA;
794 } 768 }
795} 769}
@@ -807,21 +781,12 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size
807 int bytes = 0; 781 int bytes = 0;
808 iphone_lckd_client_t control; 782 iphone_lckd_client_t control;
809 control = (iphone_lckd_client_t) transport; 783 control = (iphone_lckd_client_t) transport;
810 if (debug) 784 log_debug_msg("lockdownd_secuwrite() called\n");
811 printf("lockdownd_secuwrite() called\n"); 785 log_debug_msg("pre-send\nlength = %zi\n", length);
812 if (debug)
813 printf("pre-send\nlength = %zi\n", length);
814 iphone_mux_send(control->connection, buffer, length, &bytes); 786 iphone_mux_send(control->connection, buffer, length, &bytes);
815 if (debug) 787 log_debug_msg("post-send\nsent %i bytes\n", bytes);
816 printf("post-send\nsent %i bytes\n", bytes);
817 if (debug) {
818 FILE *my_ssl_packet = fopen("sslpacketwrite.out", "w+");
819 fwrite(buffer, 1, length, my_ssl_packet);
820 fflush(my_ssl_packet);
821 printf("Wrote SSL packet to drive, too.\n");
822 fclose(my_ssl_packet);
823 }
824 788
789 dump_debug_buffer("sslpacketwrite.out", buffer, length);
825 return bytes; 790 return bytes;
826} 791}
827 792
@@ -839,8 +804,7 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_
839 char *hackhackhack = NULL; 804 char *hackhackhack = NULL;
840 iphone_lckd_client_t control; 805 iphone_lckd_client_t control;
841 control = (iphone_lckd_client_t) transport; 806 control = (iphone_lckd_client_t) transport;
842 if (debug) 807 log_debug_msg("lockdownd_securead() called\nlength = %zi\n", length);
843 printf("lockdownd_securead() called\nlength = %zi\n", length);
844 // Buffering hack! Throw what we've got in our "buffer" into the stream first, then get more. 808 // Buffering hack! Throw what we've got in our "buffer" into the stream first, then get more.
845 if (control->gtls_buffer_hack_len > 0) { 809 if (control->gtls_buffer_hack_len > 0) {
846 if (length > control->gtls_buffer_hack_len) { // If it's asking for more than we got 810 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_
849 memcpy(buffer, control->gtls_buffer_hack, control->gtls_buffer_hack_len); // Fill their buffer partially 813 memcpy(buffer, control->gtls_buffer_hack, control->gtls_buffer_hack_len); // Fill their buffer partially
850 free(control->gtls_buffer_hack); // free our memory, it's not chained anymore 814 free(control->gtls_buffer_hack); // free our memory, it's not chained anymore
851 control->gtls_buffer_hack_len = 0; // we don't have a hack buffer anymore 815 control->gtls_buffer_hack_len = 0; // we don't have a hack buffer anymore
852 if (debug) 816 log_debug_msg("Did a partial fill to help quench thirst for data\n");
853 printf("Did a partial fill to help quench thirst for data\n");
854 } else if (length < control->gtls_buffer_hack_len) { // If it's asking for less... 817 } else if (length < control->gtls_buffer_hack_len) { // If it's asking for less...
855 control->gtls_buffer_hack_len -= length; // subtract what they're asking for 818 control->gtls_buffer_hack_len -= length; // subtract what they're asking for
856 memcpy(buffer, control->gtls_buffer_hack, length); // fill their buffer 819 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_
859 free(control->gtls_buffer_hack); // Free the old one 822 free(control->gtls_buffer_hack); // Free the old one
860 control->gtls_buffer_hack = hackhackhack; // And make it the new one. 823 control->gtls_buffer_hack = hackhackhack; // And make it the new one.
861 hackhackhack = NULL; 824 hackhackhack = NULL;
862 if (debug) 825 log_debug_msg("Quenched the thirst for data; new hack length is %i\n", control->gtls_buffer_hack_len);
863 printf("Quenched the thirst for data; new hack length is %i\n", control->gtls_buffer_hack_len);
864 return length; // hand it over. 826 return length; // hand it over.
865 } else { // length == hack length 827 } else { // length == hack length
866 memcpy(buffer, control->gtls_buffer_hack, length); // copy our buffer into theirs 828 memcpy(buffer, control->gtls_buffer_hack, length); // copy our buffer into theirs
867 free(control->gtls_buffer_hack); // free our "obligation" 829 free(control->gtls_buffer_hack); // free our "obligation"
868 control->gtls_buffer_hack_len = 0; // free our "obligation" 830 control->gtls_buffer_hack_len = 0; // free our "obligation"
869 if (debug) 831 log_debug_msg("Satiated the thirst for data; now we have to eventually receive again.\n");
870 printf("Satiated the thirst for data; now we have to eventually receive again.\n");
871 return length; // hand it over 832 return length; // hand it over
872 } 833 }
873 } 834 }
874 // End buffering hack! 835 // End buffering hack!
875 char *recv_buffer = (char *) malloc(sizeof(char) * (length * 1000)); // ensuring nothing stupid happens 836 char *recv_buffer = (char *) malloc(sizeof(char) * (length * 1000)); // ensuring nothing stupid happens
876 837
877 if (debug) 838 log_debug_msg("pre-read\nclient wants %zi bytes\n", length);
878 printf("pre-read\nclient wants %zi bytes\n", length);
879 iphone_mux_recv(control->connection, recv_buffer, (length * 1000), &bytes); 839 iphone_mux_recv(control->connection, recv_buffer, (length * 1000), &bytes);
880 if (debug) 840 log_debug_msg("post-read\nwe got %i bytes\n", bytes);
881 printf("post-read\nwe got %i bytes\n", bytes); 841 if (bytes < 0) {
882 if (debug && bytes < 0) { 842 log_debug_msg("lockdownd_securead(): uh oh\n");
883 printf("lockdownd_securead(): uh oh\n"); 843 log_debug_msg
884 printf("I believe what we have here is a failure to communicate... libusb says %s but strerror says %s\n", 844 ("I believe what we have here is a failure to communicate... libusb says %s but strerror says %s\n",
885 usb_strerror(), strerror(errno)); 845 usb_strerror(), strerror(errno));
886 return bytes + 28; // an errno 846 return bytes + 28; // an errno
887 } 847 }
888 if (bytes >= length) { 848 if (bytes >= length) {
889 if (bytes > length) { 849 if (bytes > length) {
890 if (debug) 850 log_debug_msg
891 printf 851 ("lockdownd_securead: Client deliberately read less data than was there; resorting to GnuTLS buffering hack.\n");
892 ("lockdownd_securead: Client deliberately read less data than was there; resorting to GnuTLS buffering hack.\n");
893 if (!control->gtls_buffer_hack_len) { // if there's no hack buffer yet 852 if (!control->gtls_buffer_hack_len) { // if there's no hack buffer yet
894 //control->gtls_buffer_hack = strndup(recv_buffer+length, bytes-length); // strndup is NOT a good solution! 853 //control->gtls_buffer_hack = strndup(recv_buffer+length, bytes-length); // strndup is NOT a good solution!
895 control->gtls_buffer_hack_len += bytes - length; 854 control->gtls_buffer_hack_len += bytes - length;
@@ -905,12 +864,10 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_
905 memcpy(buffer + pos_start_fill, recv_buffer, length); 864 memcpy(buffer + pos_start_fill, recv_buffer, length);
906 free(recv_buffer); 865 free(recv_buffer);
907 if (bytes == length) { 866 if (bytes == length) {
908 if (debug) 867 log_debug_msg("Returning how much we received.\n");
909 printf("Returning how much we received.\n");
910 return bytes; 868 return bytes;
911 } else { 869 } else {
912 if (debug) 870 log_debug_msg("Returning what they want to hear.\nHack length: %i\n", control->gtls_buffer_hack_len);
913 printf("Returning what they want to hear.\nHack length: %i\n", control->gtls_buffer_hack_len);
914 return length; 871 return length;
915 } 872 }
916 } 873 }
@@ -988,13 +945,11 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
988 dictionary = read_dict_element_strings(dict); 945 dictionary = read_dict_element_strings(dict);
989 946
990 for (i = 0; dictionary[i]; i += 2) { 947 for (i = 0; dictionary[i]; i += 2) {
991 if (debug) 948 log_debug_msg("lockdownd_start_service() dictionary %s: %s\n", dictionary[i], dictionary[i + 1]);
992 printf("lockdownd_start_service() dictionary %s: %s\n", dictionary[i], dictionary[i + 1]);
993 949
994 if (!xmlStrcmp(dictionary[i], "Port")) { 950 if (!xmlStrcmp(dictionary[i], "Port")) {
995 port_loc = atoi(dictionary[i + 1]); 951 port_loc = atoi(dictionary[i + 1]);
996 if (debug) 952 log_debug_msg("lockdownd_start_service() atoi'd port: %i\n", port);
997 printf("lockdownd_start_service() atoi'd port: %i\n", port);
998 } 953 }
999 954
1000 if (!xmlStrcmp(dictionary[i], "Result")) { 955 if (!xmlStrcmp(dictionary[i], "Result")) {
@@ -1004,11 +959,9 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
1004 } 959 }
1005 } 960 }
1006 961
1007 if (debug) { 962 log_debug_msg("lockdownd_start_service(): DATA RECEIVED:\n\n");
1008 printf("lockdownd_start_service(): DATA RECEIVED:\n\n"); 963 log_debug_msg(XML_query);
1009 fwrite(XML_query, 1, bytes, stdout); 964 log_debug_msg("end data received by lockdownd_start_service()\n");
1010 printf("end data received by lockdownd_start_service()\n");
1011 }
1012 965
1013 free(XML_query); 966 free(XML_query);
1014 xmlFreeDoc(plist); 967 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 @@
27 27
28#include "usbmux.h" 28#include "usbmux.h"
29 29
30extern int debug;
31
32static iphone_umux_client_t *connlist = NULL; 30static iphone_umux_client_t *connlist = NULL;
33static int clients = 0; 31static int clients = 0;
34 32
@@ -151,8 +149,7 @@ iphone_error_t iphone_mux_new_client(iphone_device_t device, uint16_t src_port,
151 } else { 149 } else {
152 free(response); 150 free(response);
153 151
154 if (debug) 152 log_debug_msg("mux_connect: connection success\n");
155 printf("mux_connect: connection success\n");
156 new_connection->header->tcp_flags = 0x10; 153 new_connection->header->tcp_flags = 0x10;
157 new_connection->header->scnt = 1; 154 new_connection->header->scnt = 1;
158 new_connection->header->ocnt = 1; 155 new_connection->header->ocnt = 1;
@@ -189,12 +186,12 @@ iphone_error_t iphone_mux_free_client(iphone_umux_client_t client)
189 int bytes = 0; 186 int bytes = 0;
190 187
191 bytes = usb_bulk_write(client->phone->device, BULKOUT, (char *) client->header, sizeof(usbmux_tcp_header), 800); 188 bytes = usb_bulk_write(client->phone->device, BULKOUT, (char *) client->header, sizeof(usbmux_tcp_header), 800);
192 if (debug && bytes < 0) 189 if (bytes < 0)
193 printf("iphone_muxèfree_client(): when writing, libusb gave me the error: %s\n", usb_strerror()); 190 log_debug_msg("iphone_muxèfree_client(): when writing, libusb gave me the error: %s\n", usb_strerror());
194 191
195 bytes = usb_bulk_read(client->phone->device, BULKIN, (char *) client->header, sizeof(usbmux_tcp_header), 800); 192 bytes = usb_bulk_read(client->phone->device, BULKIN, (char *) client->header, sizeof(usbmux_tcp_header), 800);
196 if (debug && bytes < 0) 193 if (bytes < 0)
197 printf("get_iPhone(): when reading, libusb gave me the error: %s\n", usb_strerror()); 194 log_debug_msg("get_iPhone(): when reading, libusb gave me the error: %s\n", usb_strerror());
198 195
199 delete_connection(client); 196 delete_connection(client);
200 197
@@ -220,8 +217,7 @@ iphone_error_t iphone_mux_send(iphone_umux_client_t client, const char *data, ui
220 // client->scnt and client->ocnt should already be in host notation... 217 // client->scnt and client->ocnt should already be in host notation...
221 // we don't need to change them juuuust yet. 218 // we don't need to change them juuuust yet.
222 *sent_bytes = 0; 219 *sent_bytes = 0;
223 if (debug) 220 log_debug_msg("mux_send(): client wants to send %i bytes\n", datalen);
224 printf("mux_send(): client wants to send %i bytes\n", datalen);
225 char *buffer = (char *) malloc(sizeof(usbmux_tcp_header) + datalen + 2); // allow 2 bytes of safety padding 221 char *buffer = (char *) malloc(sizeof(usbmux_tcp_header) + datalen + 2); // allow 2 bytes of safety padding
226 // Set the length and pre-emptively htonl/htons it 222 // Set the length and pre-emptively htonl/htons it
227 client->header->length = htonl(sizeof(usbmux_tcp_header) + datalen); 223 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
235 memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen); 231 memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen);
236 232
237 // We have a buffer full of data, we should now send it to the phone. 233 // We have a buffer full of data, we should now send it to the phone.
238 if (debug) 234 log_debug_msg("actually sending %zi bytes of data at %p\n", sizeof(usbmux_tcp_header) + datalen, buffer);
239 printf("actually sending %zi bytes of data at %p\n", sizeof(usbmux_tcp_header) + datalen, buffer);
240 235
241 236
242 *sent_bytes = send_to_phone(client->phone, buffer, sizeof(usbmux_tcp_header) + datalen); 237 *sent_bytes = send_to_phone(client->phone, buffer, sizeof(usbmux_tcp_header) + datalen);
243 if (debug) 238 log_debug_msg("mux_send: sent %i bytes!\n", *sent_bytes);
244 printf("mux_send: sent %i bytes!\n", *sent_bytes);
245 // Now that we've sent it off, we can clean up after our sloppy selves. 239 // Now that we've sent it off, we can clean up after our sloppy selves.
246 if (debug) { 240 dump_debug_buffer("packet", buffer, *sent_bytes);
247 FILE *packet = fopen("packet", "a+");
248 fwrite(buffer, 1, *sent_bytes, packet);
249 fclose(packet);
250 printf("\n");
251 }
252
253 if (buffer) 241 if (buffer)
254 free(buffer); 242 free(buffer);
255 // Re-calculate scnt and ocnt 243 // Re-calculate scnt and ocnt
@@ -294,8 +282,7 @@ iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t
294 * a.) Check incoming packet's ports. If proper, follow proper buffering and receiving operation. 282 * a.) Check incoming packet's ports. If proper, follow proper buffering and receiving operation.
295 * 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. 283 * 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.
296 */ 284 */
297 if (debug) 285 log_debug_msg("mux_recv: datalen == %i\n", datalen);
298 printf("mux_recv: datalen == %i\n", datalen);
299 int bytes = 0, i = 0, complex = 0, offset = 0; 286 int bytes = 0, i = 0, complex = 0, offset = 0;
300 *recv_bytes = 0; 287 *recv_bytes = 0;
301 char *buffer = NULL; 288 char *buffer = NULL;
@@ -333,8 +320,7 @@ iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t
333 bytes = recv_from_phone(client->phone, buffer, 131072); 320 bytes = recv_from_phone(client->phone, buffer, 131072);
334 if (bytes < 28) { 321 if (bytes < 28) {
335 free(buffer); 322 free(buffer);
336 if (debug) 323 log_debug_msg("mux_recv: Did not even get the header.\n");
337 printf("mux_recv: Did not even get the header.\n");
338 return IPHONE_E_NOT_ENOUGH_DATA; 324 return IPHONE_E_NOT_ENOUGH_DATA;
339 } 325 }
340 326
@@ -390,7 +376,6 @@ iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t
390 } 376 }
391 377
392 // If we get to this point, 'tis probably bad. 378 // If we get to this point, 'tis probably bad.
393 if (debug) 379 log_debug_msg("mux_recv: Heisenbug: bytes and datalen not matching up\n");
394 printf("mux_recv: Heisenbug: bytes and datalen not matching up\n");
395 return IPHONE_E_UNKNOWN_ERROR; 380 return IPHONE_E_UNKNOWN_ERROR;
396} 381}
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 @@
24#include <stdio.h> 24#include <stdio.h>
25#include <string.h> 25#include <string.h>
26#include "userpref.h" 26#include "userpref.h"
27#include "utils.h"
27#include <string.h> 28#include <string.h>
28#include <stdlib.h> 29#include <stdlib.h>
29 30
@@ -35,7 +36,6 @@
35#define LIBIPHONE_ROOT_CERTIF "RootCertificate.pem" 36#define LIBIPHONE_ROOT_CERTIF "RootCertificate.pem"
36#define LIBIPHONE_HOST_CERTIF "HostCertificate.pem" 37#define LIBIPHONE_HOST_CERTIF "HostCertificate.pem"
37 38
38extern int debug;
39 39
40/** Creates a freedesktop compatible configuration directory for libiphone. 40/** Creates a freedesktop compatible configuration directory for libiphone.
41 */ 41 */
@@ -77,8 +77,7 @@ char *get_host_id()
77 g_key_file_free(key_file); 77 g_key_file_free(key_file);
78 g_free(config_file); 78 g_free(config_file);
79 79
80 if (debug) 80 log_debug_msg("get_host_id(): Using %s as HostID\n", host_id);
81 printf("get_host_id(): Using %s as HostID\n", host_id);
82 return host_id; 81 return host_id;
83} 82}
84 83
@@ -246,8 +245,7 @@ int init_config_file(char *host_id, gnutls_datum_t * root_key, gnutls_datum_t *
246 key_file = g_key_file_new(); 245 key_file = g_key_file_new();
247 246
248 /* Store in config file */ 247 /* Store in config file */
249 if (debug) 248 log_debug_msg("init_config_file(): setting hostID to %s\n", host_id);
250 printf("init_config_file(): setting hostID to %s\n", host_id);
251 g_key_file_set_value(key_file, "Global", "HostID", host_id); 249 g_key_file_set_value(key_file, "Global", "HostID", host_id);
252 250
253 /* Write config file on disk */ 251 /* 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 @@
1/*
2 * utils.c
3 * contains utilitary methos for logging and debugging
4 *
5 * Copyright (c) 2008 Jonathan Beck All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21#include <stdarg.h>
22#include <stdio.h>
23#include "utils.h"
24
25int toto_debug = 0;
26
27/**
28 * Sets the level of debugging. Currently the only acceptable values are 0 and
29 * 1.
30 *
31 * @param level Set to 0 for no debugging or 1 for debugging.
32 */
33void iphone_set_debug(int level)
34{
35 toto_debug = level;
36}
37
38
39
40void log_debug_msg(const char *format, ...)
41{
42#ifndef STRIP_DEBUG
43
44 va_list args;
45 /* run the real fprintf */
46 va_start(args, format);
47
48 if (toto_debug)
49 fprintf(stderr, format, args);
50
51 va_end(args);
52
53#endif
54}
55
56inline void log_debug_buffer(const char *data, const int length)
57{
58#ifndef STRIP_DEBUG
59
60 /* run the real fprintf */
61 if (toto_debug)
62 fwrite(data, 1, length, stderr);
63
64#endif
65}
66
67inline void dump_debug_buffer(const char *file, const char *data, const int length)
68{
69#ifndef STRIP_DEBUG
70
71 /* run the real fprintf */
72 if (toto_debug) {
73 FILE *my_ssl_packet = fopen(file, "w+");
74 fwrite(data, 1, length, my_ssl_packet);
75 fflush(my_ssl_packet);
76 fprintf(stderr, "Wrote SSL packet to drive, too.\n");
77 fclose(my_ssl_packet);
78 }
79#endif
80}
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 @@
1/*
2 * utils.h
3 * contains utilitary methos for logging and debugging
4 *
5 * Copyright (c) 2008 Jonathan Beck All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef UTILS_H
23#define UTILS_H
24
25#include "libiphone/libiphone.h"
26
27inline void log_debug_msg(const char *format, ...);
28inline void log_debug_buffer(const char *data, const int length);
29inline void dump_debug_buffer(const char *file, const char *data, const int length);
30#endif