Commit 0b2cfd2c authored by Jonathan Beck's avatar Jonathan Beck

Handle debugging through utilitary functions

parent bbd813da
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
extern "C" { extern "C" {
#endif #endif
#include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
......
...@@ -27,16 +27,13 @@ ...@@ -27,16 +27,13 @@
// This is the maximum size an AFC data packet can be // This is the maximum size an AFC data packet can be
const int MAXIMUM_PACKET_SIZE = (2 << 15) - 32; const int MAXIMUM_PACKET_SIZE = (2 << 15) - 32;
extern int debug;
/** Locks an AFC client, done for thread safety stuff /** Locks an AFC client, done for thread safety stuff
* *
* @param client The AFC client connection to lock * @param client The AFC client connection to lock
*/ */
static void afc_lock(iphone_afc_client_t client) static void afc_lock(iphone_afc_client_t client)
{ {
if (debug) log_debug_msg("Locked\n");
fprintf(stderr, "Locked\n");
while (client->lock) { while (client->lock) {
usleep(500); // they say it's obsolete, but whatever usleep(500); // they say it's obsolete, but whatever
} }
...@@ -49,8 +46,7 @@ static void afc_lock(iphone_afc_client_t client) ...@@ -49,8 +46,7 @@ static void afc_lock(iphone_afc_client_t client)
*/ */
static void afc_unlock(iphone_afc_client_t client) static void afc_unlock(iphone_afc_client_t client)
{ // just to be pretty { // just to be pretty
if (debug) log_debug_msg("Unlocked\n");
fprintf(stderr, "Unlocked\n");
client->lock = 0; client->lock = 0;
} }
...@@ -156,16 +152,12 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int ...@@ -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)); memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket));
offset = client->afc_packet->this_length - sizeof(AFCPacket); offset = client->afc_packet->this_length - sizeof(AFCPacket);
if (debug) log_debug_msg("dispatch_AFC_packet: Offset: %i\n", offset);
fprintf(stderr, "dispatch_AFC_packet: Offset: %i\n", offset);
if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) { if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) {
if (debug) { log_debug_msg("dispatch_AFC_packet: Length did not resemble what it was supposed");
fprintf(stderr, "dispatch_AFC_packet: Length did not resemble what it was supposed"); log_debug_msg("to based on the packet.\n");
fprintf(stderr, "to based on the packet.\n"); log_debug_msg("length minus offset: %i\n", length - offset);
fprintf(stderr, "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);
fprintf(stderr, "rest of packet: %i\n",
client->afc_packet->entire_length - client->afc_packet->this_length);
}
free(buffer); free(buffer);
return -1; return -1;
} }
...@@ -176,32 +168,25 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int ...@@ -176,32 +168,25 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int
return bytes; return bytes;
} }
if (debug) { log_debug_msg("dispatch_AFC_packet: sent the first now go with the second\n");
fprintf(stderr, "dispatch_AFC_packet: sent the first now go with the second\n"); log_debug_msg("Length: %i\n", length - offset);
fprintf(stderr, "Length: %i\n", length - offset); log_debug_msg("Buffer: \n");
fprintf(stderr, "Buffer: \n"); log_debug_msg(data + offset);
fwrite(data + offset, 1, length - offset, stdout);
}
iphone_mux_send(client->connection, data + offset, length - offset, &bytes); iphone_mux_send(client->connection, data + offset, length - offset, &bytes);
return bytes; return bytes;
} else { } else {
if (debug) log_debug_msg("dispatch_AFC_packet doin things the old way\n");
fprintf(stderr, "dispatch_AFC_packet doin things the old way\n");
char *buffer = (char *) malloc(sizeof(char) * client->afc_packet->this_length); char *buffer = (char *) malloc(sizeof(char) * client->afc_packet->this_length);
if (debug) log_debug_msg("dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length);
fprintf(stderr, "dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length);
memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket));
if (debug) log_debug_msg("dispatch_AFC_packet packet data follows\n");
fprintf(stderr, "dispatch_AFC_packet packet data follows\n");
if (length > 0) { if (length > 0) {
memcpy(buffer + sizeof(AFCPacket), data, length); memcpy(buffer + sizeof(AFCPacket), data, length);
buffer[sizeof(AFCPacket) + length] = '\0'; buffer[sizeof(AFCPacket) + length] = '\0';
} }
if (debug) log_debug_buffer(buffer, client->afc_packet->this_length);
fwrite(buffer, 1, client->afc_packet->this_length, stdout); log_debug_msg("\n");
if (debug)
fprintf(stderr, "\n");
iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes); iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes);
if (buffer) { if (buffer) {
...@@ -257,29 +242,22 @@ static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) ...@@ -257,29 +242,22 @@ static int receive_AFC_data(iphone_afc_client_t client, char **dump_here)
free(buffer); free(buffer);
if (r_packet->operation == AFC_ERROR && !(client->afc_packet->operation == AFC_DELETE && param1 == 7)) { if (r_packet->operation == AFC_ERROR && !(client->afc_packet->operation == AFC_DELETE && param1 == 7)) {
if (debug) log_debug_msg("Oops? Bad operation code received: 0x%X, operation=0x%X, param1=%d\n",
fprintf(stderr,
"Oops? Bad operation code received: 0x%X, operation=0x%X, param1=%d\n",
r_packet->operation, client->afc_packet->operation, param1); r_packet->operation, client->afc_packet->operation, param1);
recv_len = r_packet->entire_length - r_packet->this_length; recv_len = r_packet->entire_length - r_packet->this_length;
free(r_packet); free(r_packet);
if (debug) log_debug_msg("recv_len=%d\n", recv_len);
fprintf(stderr, "recv_len=%d\n", recv_len);
if (param1 == 0) { if (param1 == 0) {
if (debug) log_debug_msg("... false alarm, but still\n");
fprintf(stderr, "... false alarm, but still\n");
*dump_here = NULL; *dump_here = NULL;
return 0; return 0;
} else { } else {
if (debug) log_debug_msg("Errno %i\n", param1);
fprintf(stderr, "Errno %i\n", param1);
} }
*dump_here = NULL; *dump_here = NULL;
return -1; return -1;
} else { } else {
if (debug) log_debug_msg("Operation code %x\nFull length %i and this length %i\n",
fprintf(stderr,
"Operation code %x\nFull length %i and this length %i\n",
r_packet->operation, r_packet->entire_length, r_packet->this_length); r_packet->operation, 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,25 +272,19 @@ static int receive_AFC_data(iphone_afc_client_t client, char **dump_here)
final_buffer = (char *) malloc(sizeof(char) * recv_len); final_buffer = (char *) malloc(sizeof(char) * recv_len);
while (current_count < recv_len) { while (current_count < recv_len) {
iphone_mux_recv(client->connection, buffer, recv_len - current_count, &bytes); iphone_mux_recv(client->connection, buffer, recv_len - current_count, &bytes);
if (debug) log_debug_msg("receive_AFC_data: still collecting packets\n");
fprintf(stderr, "receive_AFC_data: still collecting packets\n");
if (bytes < 0) { if (bytes < 0) {
if (debug) log_debug_msg("receive_AFC_data: mux_recv failed: %d\n", bytes);
fprintf(stderr, "receive_AFC_data: mux_recv failed: %d\n", bytes);
break; break;
} }
if (bytes > recv_len - current_count) { if (bytes > recv_len - current_count) {
if (debug) log_debug_msg("receive_AFC_data: mux_recv delivered too much data\n");
fprintf(stderr, "receive_AFC_data: mux_recv delivered too much data\n");
break; break;
} }
if (bytes > 7 && strstr(buffer, "CFA6LPAA")) { if (bytes > 7 && strstr(buffer, "CFA6LPAA")) {
if (debug) log_debug_msg("receive_AFC_data: WARNING: there is AFC data in this packet at %ti\n",
fprintf(stderr,
"receive_AFC_data: WARNING: there is AFC data in this packet at %ti\n",
strstr(buffer, "CFA6LPAA") - buffer); strstr(buffer, "CFA6LPAA") - buffer);
if (debug) log_debug_msg("receive_AFC_data: the total packet length is %i\n", bytes);
fprintf(stderr, "receive_AFC_data: the total packet length is %i\n", bytes);
} }
memcpy(final_buffer + current_count, buffer, bytes); memcpy(final_buffer + current_count, buffer, bytes);
...@@ -701,8 +673,7 @@ iphone_afc_open_file(iphone_afc_client_t client, const char *filename, ...@@ -701,8 +673,7 @@ iphone_afc_open_file(iphone_afc_client_t client, const char *filename,
free(data); free(data);
if (bytes <= 0) { if (bytes <= 0) {
if (debug) log_debug_msg("afc_open_file: Didn't receive a response to the command\n");
fprintf(stderr, "afc_open_file: Didn't receive a response to the command\n");
afc_unlock(client); afc_unlock(client);
return IPHONE_E_NOT_ENOUGH_DATA; return IPHONE_E_NOT_ENOUGH_DATA;
} }
...@@ -718,8 +689,7 @@ iphone_afc_open_file(iphone_afc_client_t client, const char *filename, ...@@ -718,8 +689,7 @@ iphone_afc_open_file(iphone_afc_client_t client, const char *filename,
*file = file_loc; *file = file_loc;
return IPHONE_E_SUCCESS; return IPHONE_E_SUCCESS;
} else { } else {
if (debug) log_debug_msg("afc_open_file: Didn't get any further data\n");
fprintf(stderr, "afc_open_file: Didn't get any further data\n");
afc_unlock(client); afc_unlock(client);
return IPHONE_E_NOT_ENOUGH_DATA; 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 ...@@ -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) if (!client || !client->afc_packet || !client->connection || !file)
return IPHONE_E_INVALID_ARG; return IPHONE_E_INVALID_ARG;
if (debug) log_debug_msg("afc_read_file called for length %i\n", length);
fprintf(stderr, "afc_read_file called for length %i\n", length);
afc_lock(client); afc_lock(client);
// Looping here to get around the maximum amount of data that // Looping here to get around the maximum amount of data that
// recieve_AFC_data can handle // recieve_AFC_data can handle
while (current_count < length) { while (current_count < length) {
if (debug) log_debug_msg("afc_read_file: current count is %i but length is %i\n", current_count, length);
fprintf(stderr, "afc_read_file: current count is %i but length is %i\n", current_count, length);
// Send the read command // Send the read command
AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); 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,8 +742,7 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d
} }
// Receive the data // Receive the data
bytes_loc = receive_AFC_data(client, &input); bytes_loc = receive_AFC_data(client, &input);
if (debug) log_debug_msg("afc_read_file: bytes returned: %i\n", bytes_loc);
fprintf(stderr, "afc_read_file: bytes returned: %i\n", bytes_loc);
if (bytes_loc < 0) { if (bytes_loc < 0) {
if (input) if (input)
free(input); free(input);
...@@ -790,8 +757,7 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d ...@@ -790,8 +757,7 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d
// success // success
} else { } else {
if (input) { if (input) {
if (debug) log_debug_msg("afc_read_file: %d\n", bytes_loc);
fprintf(stderr, "afc_read_file: %d\n", bytes_loc);
memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc); memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc);
free(input); free(input);
input = NULL; input = NULL;
...@@ -799,8 +765,7 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d ...@@ -799,8 +765,7 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d
} }
} }
} }
if (debug) log_debug_msg("afc_read_file: returning current_count as %i\n", current_count);
fprintf(stderr, "afc_read_file: returning current_count as %i\n", current_count);
afc_unlock(client); afc_unlock(client);
*bytes = current_count; *bytes = current_count;
...@@ -831,8 +796,7 @@ iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file, ...@@ -831,8 +796,7 @@ iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file,
afc_lock(client); afc_lock(client);
if (debug) log_debug_msg("afc_write_file: Write length: %i\n", length);
fprintf(stderr, "afc_write_file: Write length: %i\n", length);
// Divide the file into segments. // Divide the file into segments.
for (i = 0; i < segments; i++) { 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,8 +857,7 @@ iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file,
bytes_loc = receive_AFC_data(client, &acknowledgement); bytes_loc = receive_AFC_data(client, &acknowledgement);
afc_unlock(client); afc_unlock(client);
if (bytes_loc < 0) { if (bytes_loc < 0) {
if (debug) log_debug_msg("afc_write_file: uh oh?\n");
fprintf(stderr, "afc_write_file: uh oh?\n");
} }
*bytes = current_count; *bytes = current_count;
return IPHONE_E_SUCCESS; return IPHONE_E_SUCCESS;
...@@ -916,8 +879,7 @@ iphone_error_t iphone_afc_close_file(iphone_afc_client_t client, iphone_afc_file ...@@ -916,8 +879,7 @@ iphone_error_t iphone_afc_close_file(iphone_afc_client_t client, iphone_afc_file
afc_lock(client); afc_lock(client);
if (debug) log_debug_msg("afc_close_file: File handle %i\n", file->filehandle);
fprintf(stderr, "afc_close_file: File handle %i\n", file->filehandle);
// Send command // Send command
memcpy(buffer, &file->filehandle, sizeof(uint32)); memcpy(buffer, &file->filehandle, sizeof(uint32));
......
...@@ -7,7 +7,7 @@ bin_PROGRAMS = iphoneclient libiphone-initconf lckd-client ...@@ -7,7 +7,7 @@ bin_PROGRAMS = iphoneclient libiphone-initconf lckd-client
iphoneclient_SOURCES = main.c iphoneclient_SOURCES = main.c
iphoneclient_LDADD = libiphone.la 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_CFLAGS = $(libgthread2_CFLAGS) $(AM_CFLAGS)
libiphone_initconf_LDFLAGS = $(libgthread2_LIBS) $(AM_LDFLAGS) libiphone_initconf_LDFLAGS = $(libgthread2_LIBS) $(AM_LDFLAGS)
...@@ -17,4 +17,4 @@ lckd_client_LDFLAGS = -lreadline $(AM_LDFLAGS) ...@@ -17,4 +17,4 @@ lckd_client_LDFLAGS = -lreadline $(AM_LDFLAGS)
lckd_client_LDADD = libiphone.la lckd_client_LDADD = libiphone.la
lib_LTLIBRARIES = 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
...@@ -26,9 +26,10 @@ ...@@ -26,9 +26,10 @@
#include <gnutls/x509.h> #include <gnutls/x509.h>
#include <glib.h> #include <glib.h>
#include "libiphone/libiphone.h"
#include "userpref.h" #include "userpref.h"
#include "lockdown.h" #include "lockdown.h"
#include "utils.h"
/** Generates a 2048 byte key, split into a function so that it can be run in a /** Generates a 2048 byte key, split into a function so that it can be run in a
* thread. * thread.
......
...@@ -21,24 +21,13 @@ ...@@ -21,24 +21,13 @@
#include "usbmux.h" #include "usbmux.h"
#include "iphone.h" #include "iphone.h"
#include "utils.h"
#include <arpa/inet.h> #include <arpa/inet.h>
#include <usb.h> #include <usb.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.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 * Given a USB bus and device number, returns a device handle to the iPhone on
...@@ -90,18 +79,18 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t ...@@ -90,18 +79,18 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t
} }
iphone_free_device(phone); 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; return IPHONE_E_NO_DEVICE;
found: found:
// Send the version command to the phone // Send the version command to the phone
version = version_header(); version = version_header();
bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800); bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800);
if (bytes < 20 && iphone_debug) { if (bytes < 20) {
fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n"); log_debug_msg("get_iPhone(): libusb did NOT send enough!\n");
if (bytes < 0) { if (bytes < 0) {
fprintf(stderr, "get_iPhone(): libusb gave me the error %d: %s (%s)\n", log_debug_msg("get_iPhone(): libusb gave me the error %d: %s (%s)\n",
bytes, usb_strerror(), strerror(-bytes)); bytes, usb_strerror(), strerror(-bytes));
} }
} }
...@@ -112,11 +101,9 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t ...@@ -112,11 +101,9 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t
if (bytes < 20) { if (bytes < 20) {
free(version); free(version);
iphone_free_device(phone); iphone_free_device(phone);
if (iphone_debug) log_debug_msg("get_iPhone(): Invalid version message -- header too short.\n");
fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n"); if (bytes < 0)
if (iphone_debug && bytes < 0) log_debug_msg("get_iPhone(): libusb error message %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes));
fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n",
bytes, usb_strerror(), strerror(-bytes));
return IPHONE_E_NOT_ENOUGH_DATA; return IPHONE_E_NOT_ENOUGH_DATA;
} }
// Check for correct version // 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,14 +117,12 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t
// Bad header // Bad header
iphone_free_device(phone); iphone_free_device(phone);
free(version); free(version);
if (iphone_debug) log_debug_msg("get_iPhone(): Received a bad header/invalid version number.");
fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number.");
return IPHONE_E_BAD_HEADER; return IPHONE_E_BAD_HEADER;
} }
// If it got to this point it's gotta be bad // If it got to this point it's gotta be bad
if (iphone_debug) log_debug_msg("get_iPhone(): Unknown error.\n");
fprintf(stderr, "get_iPhone(): Unknown error.\n");
iphone_free_device(phone); iphone_free_device(phone);
free(version); free(version);
return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad
...@@ -218,13 +203,12 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen) ...@@ -218,13 +203,12 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen)
if (!phone) if (!phone)
return -1; return -1;
if (iphone_debug) log_debug_msg("send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data);
fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data);
bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800); bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800);
if (bytes < datalen) { if (bytes < datalen) {
if (iphone_debug && bytes < 0) if (bytes < 0)
fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), log_debug_msg("send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(),
strerror(-bytes)); strerror(-bytes));
return -1; return -1;
} else { } else {
...@@ -250,13 +234,11 @@ int recv_from_phone(iphone_device_t phone, char *data, int datalen) ...@@ -250,13 +234,11 @@ int recv_from_phone(iphone_device_t phone, char *data, int datalen)
if (!phone) if (!phone)
return -1; return -1;
if (iphone_debug) log_debug_msg("recv_from_phone(): attempting to receive %i bytes\n", datalen);
fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen);
bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500); bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500);
if (bytes < 0) { if (bytes < 0) {
if (iphone_debug) log_debug_msg("recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(),
fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(),
strerror(-bytes)); strerror(-bytes));
return -1; return -1;
} }
......
...@@ -30,8 +30,6 @@ ...@@ -30,8 +30,6 @@
#include <libtasn1.h> #include <libtasn1.h>
#include <gnutls/x509.h> #include <gnutls/x509.h>
extern int debug;
const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = { const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = {
{"PKCS1", 536872976, 0}, {"PKCS1", 536872976, 0},
{0, 1073741836, 0}, {0, 1073741836, 0},
...@@ -176,13 +174,8 @@ iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uin ...@@ -176,13 +174,8 @@ iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uin
length = htonl(length); length = htonl(length);
memcpy(real_query, &length, sizeof(length)); memcpy(real_query, &length, sizeof(length));
memcpy(real_query + 4, raw_data, ntohl(length)); memcpy(real_query + 4, raw_data, ntohl(length));
if (debug) { log_debug_msg("lockdownd_send(): made the query, sending it along\n");
printf("lockdownd_send(): made the query, sending it along\n"); dump_debug_buffer("grpkt", real_query, ntohl(length) + 4);
FILE *packet = fopen("grpkt", "w");
fwrite(real_query, 1, ntohl(length) + 4, packet);
fclose(packet);
packet = NULL;
}
if (!client->in_SSL) if (!client->in_SSL)
ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes); 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,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)); gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length));
ret = IPHONE_E_SUCCESS; ret = IPHONE_E_SUCCESS;
} }
if (debug) log_debug_msg("lockdownd_send(): sent it!\n");
printf("lockdownd_send(): sent it!\n");
free(real_query); free(real_query);
*sent_bytes = bytes; *sent_bytes = bytes;
return ret; return ret;
...@@ -215,8 +207,7 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) ...@@ -215,8 +207,7 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control)
int bytes = 0, i = 0; int bytes = 0, i = 0;
iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
if (debug) log_debug_msg("lockdownd_hello() called\n");
printf("lockdownd_hello() called\n");
dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); dict = add_child_to_plist(plist, "dict", "\n", NULL, 0);
key = add_key_str_dict_element(plist, dict, "Request", "QueryType", 1); key = add_key_str_dict_element(plist, dict, "Request", "QueryType", 1);
char *XML_content; char *XML_content;
...@@ -246,8 +237,7 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) ...@@ -246,8 +237,7 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control)
for (i = 0; dictionary[i]; i += 2) { for (i = 0; dictionary[i]; i += 2) {
if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) {
if (debug) log_debug_msg("lockdownd_hello(): success\n");
printf("lockdownd_hello(): success\n");
ret = IPHONE_E_SUCCESS; ret = IPHONE_E_SUCCESS;
break; break;
} }
...@@ -479,11 +469,9 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch ...@@ -479,11 +469,9 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch
if (ret != IPHONE_E_SUCCESS) if (ret != IPHONE_E_SUCCESS)
return ret; return ret;
if (debug) { log_debug_msg("lockdown_pair_device: iPhone's response to our pair request:\n");
printf("lockdown_pair_device: iPhone's response to our pair request:\n"); log_debug_msg(XML_content);
fwrite(XML_content, 1, bytes, stdout); log_debug_msg("\n\n");
printf("\n\n");
}
plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0);
if (!plist) { if (!plist) {
...@@ -519,13 +507,11 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch ...@@ -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 */ /* store public key in config if pairing succeeded */
if (success) { if (success) {
if (debug) log_debug_msg("lockdownd_pair_device: pair success\n");
printf("lockdownd_pair_device: pair success\n");
store_device_public_key(uid, public_key_b64); store_device_public_key(uid, public_key_b64);
ret = IPHONE_E_SUCCESS; ret = IPHONE_E_SUCCESS;
} else { } else {
if (debug) log_debug_msg("lockdownd_pair_device: pair failure\n");
printf("lockdownd_pair_device: pair failure\n");
ret = IPHONE_E_PAIRING_FAILED; ret = IPHONE_E_PAIRING_FAILED;
} }
free(public_key_b64); free(public_key_b64);
...@@ -678,15 +664,13 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c ...@@ -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); key = add_key_str_dict_element(plist, dict, "HostID", HostID, 1);
if (!key) { if (!key) {
if (debug) log_debug_msg("Couldn't add a key.\n");
printf("Couldn't add a key.\n");
xmlFreeDoc(plist); xmlFreeDoc(plist);
return IPHONE_E_DICT_ERROR; return IPHONE_E_DICT_ERROR;
} }
key = add_key_str_dict_element(plist, dict, "Request", "StartSession", 1); key = add_key_str_dict_element(plist, dict, "Request", "StartSession", 1);
if (!key) { if (!key) {
if (debug) log_debug_msg("Couldn't add a key.\n");
printf("Couldn't add a key.\n");
xmlFreeDoc(plist); xmlFreeDoc(plist);
return IPHONE_E_DICT_ERROR; return IPHONE_E_DICT_ERROR;
} }
...@@ -719,8 +703,7 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c ...@@ -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_anon_client_credentials_t anoncred;
gnutls_certificate_credentials_t xcred; gnutls_certificate_credentials_t xcred;
if (debug) log_debug_msg("We started the session OK, now trying GnuTLS\n");
printf("We started the session OK, now trying GnuTLS\n");
errno = 0; errno = 0;
gnutls_global_init(); gnutls_global_init();
//gnutls_anon_allocate_client_credentials(&anoncred); //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,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. gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me.
if (debug) log_debug_msg("GnuTLS step 1...\n");
printf("GnuTLS step 1...\n");
gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control); gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control);
if (debug) log_debug_msg("GnuTLS step 2...\n");
printf("GnuTLS step 2...\n");
gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite); gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite);
if (debug) log_debug_msg("GnuTLS step 3...\n");
printf("GnuTLS step 3...\n");
gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead); gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead);
if (debug) log_debug_msg("GnuTLS step 4 -- now handshaking...\n");
printf("GnuTLS step 4 -- now handshaking...\n");
if (errno && debug) if (errno)
printf("WARN: errno says %s before handshake!\n", strerror(errno)); log_debug_msg("WARN: errno says %s before handshake!\n", strerror(errno));
return_me = gnutls_handshake(*control->ssl_session); return_me = gnutls_handshake(*control->ssl_session);
if (debug) log_debug_msg("GnuTLS handshake done...\n");
printf("GnuTLS handshake done...\n");
free_dictionary(dictionary); free_dictionary(dictionary);
if (return_me != GNUTLS_E_SUCCESS) { if (return_me != GNUTLS_E_SUCCESS) {
if (debug) log_debug_msg("GnuTLS reported something wrong.\n");
printf("GnuTLS reported something wrong.\n");
gnutls_perror(return_me); gnutls_perror(return_me);
if (debug) log_debug_msg("oh.. errno says %s\n", strerror(errno));
printf("oh.. errno says %s\n", strerror(errno));
return IPHONE_E_SSL_ERROR; return IPHONE_E_SSL_ERROR;
} else { } else {
control->in_SSL = 1; control->in_SSL = 1;
...@@ -777,19 +753,17 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c ...@@ -777,19 +753,17 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c
} }
} }
if (debug) { log_debug_msg("Apparently failed negotiating with lockdownd.\n");
printf("Apparently failed negotiating with lockdownd.\n"); log_debug_msg("Responding dictionary: \n");
printf("Responding dictionary: \n");
for (i = 0; dictionary[i]; i += 2) { for (i = 0; dictionary[i]; i += 2) {
printf("\t%s: %s\n", dictionary[i], dictionary[i + 1]); log_debug_msg("\t%s: %s\n", dictionary[i], dictionary[i + 1]);
}
} }
free_dictionary(dictionary); free_dictionary(dictionary);
return IPHONE_E_SSL_ERROR; return IPHONE_E_SSL_ERROR;
} else { } else {
if (debug) log_debug_msg("Didn't get enough bytes.\n");
printf("Didn't get enough bytes.\n");
return IPHONE_E_NOT_ENOUGH_DATA; return IPHONE_E_NOT_ENOUGH_DATA;
} }
} }
...@@ -807,21 +781,12 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size ...@@ -807,21 +781,12 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size
int bytes = 0; int bytes = 0;
iphone_lckd_client_t control; iphone_lckd_client_t control;
control = (iphone_lckd_client_t) transport; control = (iphone_lckd_client_t) transport;
if (debug) log_debug_msg("lockdownd_secuwrite() called\n");
printf("lockdownd_secuwrite() called\n"); log_debug_msg("pre-send\nlength = %zi\n", length);
if (debug)
printf("pre-send\nlength = %zi\n", length);
iphone_mux_send(control->connection, buffer, length, &bytes); iphone_mux_send(control->connection, buffer, length, &bytes);
if (debug) log_debug_msg("post-send\nsent %i bytes\n", bytes);
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);
}
dump_debug_buffer("sslpacketwrite.out", buffer, length);
return bytes; return bytes;
} }
...@@ -839,8 +804,7 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ ...@@ -839,8 +804,7 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_
char *hackhackhack = NULL; char *hackhackhack = NULL;
iphone_lckd_client_t control; iphone_lckd_client_t control;
control = (iphone_lckd_client_t) transport; control = (iphone_lckd_client_t) transport;
if (debug) log_debug_msg("lockdownd_securead() called\nlength = %zi\n", length);
printf("lockdownd_securead() called\nlength = %zi\n", length);
// Buffering hack! Throw what we've got in our "buffer" into the stream first, then get more. // 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 (control->gtls_buffer_hack_len > 0) {
if (length > control->gtls_buffer_hack_len) { // If it's asking for more than we got 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,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 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 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 control->gtls_buffer_hack_len = 0; // we don't have a hack buffer anymore
if (debug) log_debug_msg("Did a partial fill to help quench thirst for data\n");
printf("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... } 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 control->gtls_buffer_hack_len -= length; // subtract what they're asking for
memcpy(buffer, control->gtls_buffer_hack, length); // fill their buffer memcpy(buffer, control->gtls_buffer_hack, length); // fill their buffer
...@@ -859,36 +822,32 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ ...@@ -859,36 +822,32 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_
free(control->gtls_buffer_hack); // Free the old one free(control->gtls_buffer_hack); // Free the old one
control->gtls_buffer_hack = hackhackhack; // And make it the new one. control->gtls_buffer_hack = hackhackhack; // And make it the new one.
hackhackhack = NULL; hackhackhack = NULL;
if (debug) log_debug_msg("Quenched the thirst for data; new hack length is %i\n", control->gtls_buffer_hack_len);
printf("Quenched the thirst for data; new hack length is %i\n", control->gtls_buffer_hack_len);
return length; // hand it over. return length; // hand it over.
} else { // length == hack length } else { // length == hack length
memcpy(buffer, control->gtls_buffer_hack, length); // copy our buffer into theirs memcpy(buffer, control->gtls_buffer_hack, length); // copy our buffer into theirs
free(control->gtls_buffer_hack); // free our "obligation" free(control->gtls_buffer_hack); // free our "obligation"
control->gtls_buffer_hack_len = 0; // free our "obligation" control->gtls_buffer_hack_len = 0; // free our "obligation"
if (debug) log_debug_msg("Satiated the thirst for data; now we have to eventually receive again.\n");
printf("Satiated the thirst for data; now we have to eventually receive again.\n");
return length; // hand it over return length; // hand it over
} }
} }
// End buffering hack! // End buffering hack!
char *recv_buffer = (char *) malloc(sizeof(char) * (length * 1000)); // ensuring nothing stupid happens char *recv_buffer = (char *) malloc(sizeof(char) * (length * 1000)); // ensuring nothing stupid happens
if (debug) log_debug_msg("pre-read\nclient wants %zi bytes\n", length);
printf("pre-read\nclient wants %zi bytes\n", length);
iphone_mux_recv(control->connection, recv_buffer, (length * 1000), &bytes); iphone_mux_recv(control->connection, recv_buffer, (length * 1000), &bytes);
if (debug) log_debug_msg("post-read\nwe got %i bytes\n", bytes);
printf("post-read\nwe got %i bytes\n", bytes); if (bytes < 0) {
if (debug && bytes < 0) { log_debug_msg("lockdownd_securead(): uh oh\n");
printf("lockdownd_securead(): uh oh\n"); log_debug_msg
printf("I believe what we have here is a failure to communicate... libusb says %s but strerror says %s\n", ("I believe what we have here is a failure to communicate... libusb says %s but strerror says %s\n",
usb_strerror(), strerror(errno)); usb_strerror(), strerror(errno));
return bytes + 28; // an errno return bytes + 28; // an errno
} }
if (bytes >= length) { if (bytes >= length) {
if (bytes > length) { if (bytes > length) {
if (debug) log_debug_msg
printf
("lockdownd_securead: Client deliberately read less data than was there; resorting to GnuTLS buffering hack.\n"); ("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 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 = strndup(recv_buffer+length, bytes-length); // strndup is NOT a good solution!
...@@ -905,12 +864,10 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ ...@@ -905,12 +864,10 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_
memcpy(buffer + pos_start_fill, recv_buffer, length); memcpy(buffer + pos_start_fill, recv_buffer, length);
free(recv_buffer); free(recv_buffer);
if (bytes == length) { if (bytes == length) {
if (debug) log_debug_msg("Returning how much we received.\n");
printf("Returning how much we received.\n");
return bytes; return bytes;
} else { } else {
if (debug) log_debug_msg("Returning what they want to hear.\nHack length: %i\n", control->gtls_buffer_hack_len);
printf("Returning what they want to hear.\nHack length: %i\n", control->gtls_buffer_hack_len);
return length; return length;
} }
} }
...@@ -988,13 +945,11 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char ...@@ -988,13 +945,11 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
dictionary = read_dict_element_strings(dict); dictionary = read_dict_element_strings(dict);
for (i = 0; dictionary[i]; i += 2) { for (i = 0; dictionary[i]; i += 2) {
if (debug) log_debug_msg("lockdownd_start_service() dictionary %s: %s\n", dictionary[i], dictionary[i + 1]);
printf("lockdownd_start_service() dictionary %s: %s\n", dictionary[i], dictionary[i + 1]);
if (!xmlStrcmp(dictionary[i], "Port")) { if (!xmlStrcmp(dictionary[i], "Port")) {
port_loc = atoi(dictionary[i + 1]); port_loc = atoi(dictionary[i + 1]);
if (debug) log_debug_msg("lockdownd_start_service() atoi'd port: %i\n", port);
printf("lockdownd_start_service() atoi'd port: %i\n", port);
} }
if (!xmlStrcmp(dictionary[i], "Result")) { if (!xmlStrcmp(dictionary[i], "Result")) {
...@@ -1004,11 +959,9 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char ...@@ -1004,11 +959,9 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
} }
} }
if (debug) { log_debug_msg("lockdownd_start_service(): DATA RECEIVED:\n\n");
printf("lockdownd_start_service(): DATA RECEIVED:\n\n"); log_debug_msg(XML_query);
fwrite(XML_query, 1, bytes, stdout); log_debug_msg("end data received by lockdownd_start_service()\n");
printf("end data received by lockdownd_start_service()\n");
}
free(XML_query); free(XML_query);
xmlFreeDoc(plist); xmlFreeDoc(plist);
......
...@@ -27,8 +27,6 @@ ...@@ -27,8 +27,6 @@
#include "usbmux.h" #include "usbmux.h"
extern int debug;
static iphone_umux_client_t *connlist = NULL; static iphone_umux_client_t *connlist = NULL;
static int clients = 0; static int clients = 0;
...@@ -151,8 +149,7 @@ iphone_error_t iphone_mux_new_client(iphone_device_t device, uint16_t src_port, ...@@ -151,8 +149,7 @@ iphone_error_t iphone_mux_new_client(iphone_device_t device, uint16_t src_port,
} else { } else {
free(response); free(response);
if (debug) log_debug_msg("mux_connect: connection success\n");
printf("mux_connect: connection success\n");
new_connection->header->tcp_flags = 0x10; new_connection->header->tcp_flags = 0x10;
new_connection->header->scnt = 1; new_connection->header->scnt = 1;
new_connection->header->ocnt = 1; new_connection->header->ocnt = 1;
...@@ -189,12 +186,12 @@ iphone_error_t iphone_mux_free_client(iphone_umux_client_t client) ...@@ -189,12 +186,12 @@ iphone_error_t iphone_mux_free_client(iphone_umux_client_t client)
int bytes = 0; int bytes = 0;
bytes = usb_bulk_write(client->phone->device, BULKOUT, (char *) client->header, sizeof(usbmux_tcp_header), 800); bytes = usb_bulk_write(client->phone->device, BULKOUT, (char *) client->header, sizeof(usbmux_tcp_header), 800);
if (debug && bytes < 0) if (bytes < 0)
printf("iphone_muxèfree_client(): when writing, libusb gave me the error: %s\n", usb_strerror()); 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); bytes = usb_bulk_read(client->phone->device, BULKIN, (char *) client->header, sizeof(usbmux_tcp_header), 800);
if (debug && bytes < 0) if (bytes < 0)
printf("get_iPhone(): when reading, libusb gave me the error: %s\n", usb_strerror()); log_debug_msg("get_iPhone(): when reading, libusb gave me the error: %s\n", usb_strerror());
delete_connection(client); delete_connection(client);
...@@ -220,8 +217,7 @@ iphone_error_t iphone_mux_send(iphone_umux_client_t client, const char *data, ui ...@@ -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... // client->scnt and client->ocnt should already be in host notation...
// we don't need to change them juuuust yet. // we don't need to change them juuuust yet.
*sent_bytes = 0; *sent_bytes = 0;
if (debug) log_debug_msg("mux_send(): client wants to send %i bytes\n", datalen);
printf("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 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 // Set the length and pre-emptively htonl/htons it
client->header->length = htonl(sizeof(usbmux_tcp_header) + datalen); 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,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); memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen);
// We have a buffer full of data, we should now send it to the phone. // We have a buffer full of data, we should now send it to the phone.
if (debug) log_debug_msg("actually sending %zi bytes of data at %p\n", sizeof(usbmux_tcp_header) + datalen, buffer);
printf("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); *sent_bytes = send_to_phone(client->phone, buffer, sizeof(usbmux_tcp_header) + datalen);
if (debug) log_debug_msg("mux_send: sent %i bytes!\n", *sent_bytes);
printf("mux_send: sent %i bytes!\n", *sent_bytes);
// Now that we've sent it off, we can clean up after our sloppy selves. // Now that we've sent it off, we can clean up after our sloppy selves.
if (debug) { dump_debug_buffer("packet", buffer, *sent_bytes);
FILE *packet = fopen("packet", "a+");
fwrite(buffer, 1, *sent_bytes, packet);
fclose(packet);
printf("\n");
}
if (buffer) if (buffer)
free(buffer); free(buffer);
// Re-calculate scnt and ocnt // 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,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. * 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. * 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) log_debug_msg("mux_recv: datalen == %i\n", datalen);
printf("mux_recv: datalen == %i\n", datalen);
int bytes = 0, i = 0, complex = 0, offset = 0; int bytes = 0, i = 0, complex = 0, offset = 0;
*recv_bytes = 0; *recv_bytes = 0;
char *buffer = NULL; char *buffer = NULL;
...@@ -333,8 +320,7 @@ iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t ...@@ -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); bytes = recv_from_phone(client->phone, buffer, 131072);
if (bytes < 28) { if (bytes < 28) {
free(buffer); free(buffer);
if (debug) log_debug_msg("mux_recv: Did not even get the header.\n");
printf("mux_recv: Did not even get the header.\n");
return IPHONE_E_NOT_ENOUGH_DATA; 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 ...@@ -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 we get to this point, 'tis probably bad.
if (debug) log_debug_msg("mux_recv: Heisenbug: bytes and datalen not matching up\n");
printf("mux_recv: Heisenbug: bytes and datalen not matching up\n");
return IPHONE_E_UNKNOWN_ERROR; return IPHONE_E_UNKNOWN_ERROR;
} }
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "userpref.h" #include "userpref.h"
#include "utils.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -35,7 +36,6 @@ ...@@ -35,7 +36,6 @@
#define LIBIPHONE_ROOT_CERTIF "RootCertificate.pem" #define LIBIPHONE_ROOT_CERTIF "RootCertificate.pem"
#define LIBIPHONE_HOST_CERTIF "HostCertificate.pem" #define LIBIPHONE_HOST_CERTIF "HostCertificate.pem"
extern int debug;
/** Creates a freedesktop compatible configuration directory for libiphone. /** Creates a freedesktop compatible configuration directory for libiphone.
*/ */
...@@ -77,8 +77,7 @@ char *get_host_id() ...@@ -77,8 +77,7 @@ char *get_host_id()
g_key_file_free(key_file); g_key_file_free(key_file);
g_free(config_file); g_free(config_file);
if (debug) log_debug_msg("get_host_id(): Using %s as HostID\n", host_id);
printf("get_host_id(): Using %s as HostID\n", host_id);
return host_id; return host_id;
} }
...@@ -246,8 +245,7 @@ int init_config_file(char *host_id, gnutls_datum_t * root_key, gnutls_datum_t * ...@@ -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(); key_file = g_key_file_new();
/* Store in config file */ /* Store in config file */
if (debug) log_debug_msg("init_config_file(): setting hostID to %s\n", host_id);
printf("init_config_file(): setting hostID to %s\n", host_id);
g_key_file_set_value(key_file, "Global", "HostID", host_id); g_key_file_set_value(key_file, "Global", "HostID", host_id);
/* Write config file on disk */ /* Write config file on disk */
......
/*
* 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
}
/*
* 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment