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

Handle debugging through utilitary functions

parent bbd813da
......@@ -26,6 +26,7 @@
extern "C" {
#endif
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
......
This diff is collapsed.
......@@ -7,7 +7,7 @@ bin_PROGRAMS = iphoneclient libiphone-initconf lckd-client
iphoneclient_SOURCES = main.c
iphoneclient_LDADD = libiphone.la
libiphone_initconf_SOURCES = initconf.c userpref.c lockdown.c plist.c usbmux.c iphone.c
libiphone_initconf_SOURCES = initconf.c userpref.c lockdown.c plist.c usbmux.c iphone.c utils.c
libiphone_initconf_CFLAGS = $(libgthread2_CFLAGS) $(AM_CFLAGS)
libiphone_initconf_LDFLAGS = $(libgthread2_LIBS) $(AM_LDFLAGS)
......@@ -17,4 +17,4 @@ lckd_client_LDFLAGS = -lreadline $(AM_LDFLAGS)
lckd_client_LDADD = libiphone.la
lib_LTLIBRARIES = libiphone.la
libiphone_la_SOURCES = usbmux.c iphone.c plist.c lockdown.c AFC.c userpref.c
libiphone_la_SOURCES = usbmux.c iphone.c plist.c lockdown.c AFC.c userpref.c utils.c
......@@ -26,9 +26,10 @@
#include <gnutls/x509.h>
#include <glib.h>
#include "libiphone/libiphone.h"
#include "userpref.h"
#include "lockdown.h"
#include "utils.h"
/** Generates a 2048 byte key, split into a function so that it can be run in a
* thread.
......
......@@ -21,24 +21,13 @@
#include "usbmux.h"
#include "iphone.h"
#include "utils.h"
#include <arpa/inet.h>
#include <usb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int iphone_debug = 0;
/**
* Sets the level of debugging. Currently the only acceptable values are 0 and
* 1.
*
* @param level Set to 0 for no debugging or 1 for debugging.
*/
void iphone_set_debug(int level)
{
iphone_debug = level;
}
/**
* Given a USB bus and device number, returns a device handle to the iPhone on
......@@ -90,19 +79,19 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t
}
iphone_free_device(phone);
if (iphone_debug)
fprintf(stderr, "iphone_get_specific_device: iPhone not found\n");
log_debug_msg("iphone_get_specific_device: iPhone not found\n");
return IPHONE_E_NO_DEVICE;
found:
// Send the version command to the phone
version = version_header();
bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800);
if (bytes < 20 && iphone_debug) {
fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n");
if (bytes < 20) {
log_debug_msg("get_iPhone(): libusb did NOT send enough!\n");
if (bytes < 0) {
fprintf(stderr, "get_iPhone(): libusb gave me the error %d: %s (%s)\n",
bytes, usb_strerror(), strerror(-bytes));
log_debug_msg("get_iPhone(): libusb gave me the error %d: %s (%s)\n",
bytes, usb_strerror(), strerror(-bytes));
}
}
// Read the phone's response
......@@ -112,11 +101,9 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t
if (bytes < 20) {
free(version);
iphone_free_device(phone);
if (iphone_debug)
fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n");
if (iphone_debug && bytes < 0)
fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n",
bytes, usb_strerror(), strerror(-bytes));
log_debug_msg("get_iPhone(): Invalid version message -- header too short.\n");
if (bytes < 0)
log_debug_msg("get_iPhone(): libusb error message %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes));
return IPHONE_E_NOT_ENOUGH_DATA;
}
// Check for correct version
......@@ -130,14 +117,12 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t
// Bad header
iphone_free_device(phone);
free(version);
if (iphone_debug)
fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number.");
log_debug_msg("get_iPhone(): Received a bad header/invalid version number.");
return IPHONE_E_BAD_HEADER;
}
// If it got to this point it's gotta be bad
if (iphone_debug)
fprintf(stderr, "get_iPhone(): Unknown error.\n");
log_debug_msg("get_iPhone(): Unknown error.\n");
iphone_free_device(phone);
free(version);
return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad
......@@ -218,14 +203,13 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen)
if (!phone)
return -1;
if (iphone_debug)
fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data);
log_debug_msg("send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data);
bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800);
if (bytes < datalen) {
if (iphone_debug && bytes < 0)
fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(),
strerror(-bytes));
if (bytes < 0)
log_debug_msg("send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(),
strerror(-bytes));
return -1;
} else {
return bytes;
......@@ -250,14 +234,12 @@ int recv_from_phone(iphone_device_t phone, char *data, int datalen)
if (!phone)
return -1;
if (iphone_debug)
fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen);
log_debug_msg("recv_from_phone(): attempting to receive %i bytes\n", datalen);
bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500);
if (bytes < 0) {
if (iphone_debug)
fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(),
strerror(-bytes));
log_debug_msg("recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(),
strerror(-bytes));
return -1;
}
......
This diff is collapsed.
......@@ -27,8 +27,6 @@
#include "usbmux.h"
extern int debug;
static iphone_umux_client_t *connlist = NULL;
static int clients = 0;
......@@ -151,8 +149,7 @@ iphone_error_t iphone_mux_new_client(iphone_device_t device, uint16_t src_port,
} else {
free(response);
if (debug)
printf("mux_connect: connection success\n");
log_debug_msg("mux_connect: connection success\n");
new_connection->header->tcp_flags = 0x10;
new_connection->header->scnt = 1;
new_connection->header->ocnt = 1;
......@@ -189,12 +186,12 @@ iphone_error_t iphone_mux_free_client(iphone_umux_client_t client)
int bytes = 0;
bytes = usb_bulk_write(client->phone->device, BULKOUT, (char *) client->header, sizeof(usbmux_tcp_header), 800);
if (debug && bytes < 0)
printf("iphone_muxèfree_client(): when writing, libusb gave me the error: %s\n", usb_strerror());
if (bytes < 0)
log_debug_msg("iphone_muxèfree_client(): when writing, libusb gave me the error: %s\n", usb_strerror());
bytes = usb_bulk_read(client->phone->device, BULKIN, (char *) client->header, sizeof(usbmux_tcp_header), 800);
if (debug && bytes < 0)
printf("get_iPhone(): when reading, libusb gave me the error: %s\n", usb_strerror());
if (bytes < 0)
log_debug_msg("get_iPhone(): when reading, libusb gave me the error: %s\n", usb_strerror());
delete_connection(client);
......@@ -220,8 +217,7 @@ iphone_error_t iphone_mux_send(iphone_umux_client_t client, const char *data, ui
// client->scnt and client->ocnt should already be in host notation...
// we don't need to change them juuuust yet.
*sent_bytes = 0;
if (debug)
printf("mux_send(): client wants to send %i bytes\n", datalen);
log_debug_msg("mux_send(): client wants to send %i bytes\n", datalen);
char *buffer = (char *) malloc(sizeof(usbmux_tcp_header) + datalen + 2); // allow 2 bytes of safety padding
// Set the length and pre-emptively htonl/htons it
client->header->length = htonl(sizeof(usbmux_tcp_header) + datalen);
......@@ -235,21 +231,13 @@ iphone_error_t iphone_mux_send(iphone_umux_client_t client, const char *data, ui
memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen);
// We have a buffer full of data, we should now send it to the phone.
if (debug)
printf("actually sending %zi bytes of data at %p\n", sizeof(usbmux_tcp_header) + datalen, buffer);
log_debug_msg("actually sending %zi bytes of data at %p\n", sizeof(usbmux_tcp_header) + datalen, buffer);
*sent_bytes = send_to_phone(client->phone, buffer, sizeof(usbmux_tcp_header) + datalen);
if (debug)
printf("mux_send: sent %i bytes!\n", *sent_bytes);
log_debug_msg("mux_send: sent %i bytes!\n", *sent_bytes);
// Now that we've sent it off, we can clean up after our sloppy selves.
if (debug) {
FILE *packet = fopen("packet", "a+");
fwrite(buffer, 1, *sent_bytes, packet);
fclose(packet);
printf("\n");
}
dump_debug_buffer("packet", buffer, *sent_bytes);
if (buffer)
free(buffer);
// Re-calculate scnt and ocnt
......@@ -294,8 +282,7 @@ iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t
* a.) Check incoming packet's ports. If proper, follow proper buffering and receiving operation.
* b.) If not, find the client the ports belong to and fill that client's buffer, then return mux_recv with the same args to try again.
*/
if (debug)
printf("mux_recv: datalen == %i\n", datalen);
log_debug_msg("mux_recv: datalen == %i\n", datalen);
int bytes = 0, i = 0, complex = 0, offset = 0;
*recv_bytes = 0;
char *buffer = NULL;
......@@ -333,8 +320,7 @@ iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t
bytes = recv_from_phone(client->phone, buffer, 131072);
if (bytes < 28) {
free(buffer);
if (debug)
printf("mux_recv: Did not even get the header.\n");
log_debug_msg("mux_recv: Did not even get the header.\n");
return IPHONE_E_NOT_ENOUGH_DATA;
}
......@@ -390,7 +376,6 @@ iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t
}
// If we get to this point, 'tis probably bad.
if (debug)
printf("mux_recv: Heisenbug: bytes and datalen not matching up\n");
log_debug_msg("mux_recv: Heisenbug: bytes and datalen not matching up\n");
return IPHONE_E_UNKNOWN_ERROR;
}
......@@ -24,6 +24,7 @@
#include <stdio.h>
#include <string.h>
#include "userpref.h"
#include "utils.h"
#include <string.h>
#include <stdlib.h>
......@@ -35,7 +36,6 @@
#define LIBIPHONE_ROOT_CERTIF "RootCertificate.pem"
#define LIBIPHONE_HOST_CERTIF "HostCertificate.pem"
extern int debug;
/** Creates a freedesktop compatible configuration directory for libiphone.
*/
......@@ -77,8 +77,7 @@ char *get_host_id()
g_key_file_free(key_file);
g_free(config_file);
if (debug)
printf("get_host_id(): Using %s as HostID\n", host_id);
log_debug_msg("get_host_id(): Using %s as HostID\n", host_id);
return host_id;
}
......@@ -246,8 +245,7 @@ int init_config_file(char *host_id, gnutls_datum_t * root_key, gnutls_datum_t *
key_file = g_key_file_new();
/* Store in config file */
if (debug)
printf("init_config_file(): setting hostID to %s\n", host_id);
log_debug_msg("init_config_file(): setting hostID to %s\n", host_id);
g_key_file_set_value(key_file, "Global", "HostID", host_id);
/* Write config file on disk */
......
/*
* 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