diff --git a/include/ios-webkit-debug-proxy/socket_manager.h b/include/ios-webkit-debug-proxy/socket_manager.h old mode 100644 new mode 100755 index 014c5268..02625554 --- a/include/ios-webkit-debug-proxy/socket_manager.h +++ b/include/ios-webkit-debug-proxy/socket_manager.h @@ -15,17 +15,28 @@ extern "C" { #include #include +#include +#include + // Bind a server port, return the file descriptor (or -1 for error). int sm_listen(int port); // Connect to a server, return the file descriptor (or -1 for error). int sm_connect(const char *socket_addr); - - + typedef uint8_t sm_status; #define SM_ERROR 1 #define SM_SUCCESS 0 +#define SSL_ERROR_NONE 0 +#define SSL_ERROR_SSL 1 +#define SSL_ERROR_WANT_READ 2 +#define SSL_ERROR_WANT_WRITE 3 +#define SSL_ERROR_WANT_X509_LOOKUP 4 +#define SSL_ERROR_SYSCALL 5 /* look at error stack/return value/errno */ +#define SSL_ERROR_ZERO_RETURN 6 +#define SSL_ERROR_WANT_CONNECT 7 +#define SSL_ERROR_WANT_ACCEPT 8 struct sm_private; typedef struct sm_private *sm_private_t; @@ -76,6 +87,24 @@ struct sm_struct { sm_private_t private_state; }; + +// based on libimobiledevice/src/idevice.h +struct service_client_private +{ + idevice_connection_t connection; +}; +enum connection_type { + CONNECTION_USBMUXD = 1 +}; +struct idevice_connection_private { + char *udid; // added in v1.1.6 + enum connection_type type; + void *data; + void *ssl_data; +}; + + + #ifdef __cplusplus } #endif diff --git a/src/char_buffer.c b/src/char_buffer.c old mode 100644 new mode 100755 index 70ea6eb2..f313e8f8 --- a/src/char_buffer.c +++ b/src/char_buffer.c @@ -12,6 +12,8 @@ #include "char_buffer.h" +void* connectionSSL = NULL; + #define MIN_LENGTH 1024 cb_t cb_new() { diff --git a/src/ios_webkit_debug_proxy_main.c b/src/ios_webkit_debug_proxy_main.c old mode 100644 new mode 100755 index b185d081..858066d4 --- a/src/ios_webkit_debug_proxy_main.c +++ b/src/ios_webkit_debug_proxy_main.c @@ -66,6 +66,7 @@ int main(int argc, char** argv) { signal(SIGINT, on_signal); signal(SIGTERM, on_signal); + #ifdef WIN32 WSADATA wsa_data; int res = WSAStartup(MAKEWORD(2,2), &wsa_data); diff --git a/src/socket_manager.c b/src/socket_manager.c old mode 100644 new mode 100755 index cbf0031d..b8a21cba --- a/src/socket_manager.c +++ b/src/socket_manager.c @@ -26,12 +26,15 @@ #include #include #include +#include +#include #endif #include "char_buffer.h" #include "socket_manager.h" #include "hash_table.h" + #if defined(__MACH__) || defined(WIN32) #define SIZEOF_FD_SET sizeof(struct fd_set) #define RECV_FLAGS 0 @@ -40,6 +43,9 @@ #define RECV_FLAGS MSG_DONTWAIT #endif +extern idevice_connection_t connectionSSL; +#define IS_SSL_FD(fd) if(connectionSSL!=NULL && (fd == (int)(long)connectionSSL->data)) + struct sm_private { struct timeval timeout; // fds: @@ -373,8 +379,22 @@ sm_status sm_remove_fd(sm_t self, int fd) { return ret; } -sm_status sm_send(sm_t self, int fd, const char *data, size_t length, - void* value) { +sm_status sm_send(sm_t self, int fd, const char *data, size_t length, void* value) +{ + IS_SSL_FD(fd) + { + uint32_t sent_bytes = 0; + if( idevice_connection_send(connectionSSL, data, length, &sent_bytes) != IDEVICE_E_SUCCESS) + { + // wait 200 msec... + usleep(200 *1000); // SSL_ERROR_WANT_WRITE? - to do expose the SSL error by libimobiledevice hedaer... + if( idevice_connection_send(connectionSSL, data, length, &sent_bytes) != IDEVICE_E_SUCCESS) + return SM_ERROR; // SSL_ERROR_SSL??? + + } + return SM_SUCCESS; + } + sm_private_t my = self->private_state; sm_sendq_t sendq = (sm_sendq_t)ht_get_value(my->fd_to_sendq, HT_KEY(fd)); const char *head = data; @@ -531,13 +551,28 @@ void sm_resend(sm_t self, int fd) { sendq = nextq; } } + +void sm_recv(sm_t self, int fd) +{ + sm_private_t my = self->private_state; + my->curr_recv_fd = fd; -void sm_recv(sm_t self, int fd) { - sm_private_t my = self->private_state; - my->curr_recv_fd = fd; while (1) { - ssize_t read_bytes = recv(fd, my->tmp_buf, my->tmp_buf_length, RECV_FLAGS); - if (read_bytes < 0) { + ssize_t read_bytes = 0; + IS_SSL_FD(fd) + { + idevice_error_t error = idevice_connection_receive(connectionSSL, + my->tmp_buf, + my->tmp_buf_length, + (uint32_t*)&read_bytes); + if (error != IDEVICE_E_SUCCESS) + break; // SSL_ERROR_WANT_READ ? + } + else { + read_bytes = recv(fd, my->tmp_buf, my->tmp_buf_length, RECV_FLAGS); + } + if (read_bytes < 0) + { #ifdef WIN32 if (WSAGetLastError() != WSAEWOULDBLOCK) { fprintf(stderr, "recv failed with error %d\n", WSAGetLastError()); @@ -558,6 +593,7 @@ void sm_recv(sm_t self, int fd) { } } my->curr_recv_fd = 0; + } int sm_select(sm_t self, int timeout_secs) { diff --git a/src/webinspector.c b/src/webinspector.c old mode 100644 new mode 100755 index 9192010d..d27898d2 --- a/src/webinspector.c +++ b/src/webinspector.c @@ -26,10 +26,11 @@ #include #include #include +#include #include "char_buffer.h" #include "webinspector.h" - +#include "socket_manager.h" #define WI_DEBUG 1 @@ -39,6 +40,8 @@ // some arbitrarly limit, to catch bad packets #define MAX_BODY_LENGTH 1<<26 +extern idevice_connection_t connectionSSL; + struct wi_private { bool partials_supported; cb_t in; @@ -52,31 +55,22 @@ struct wi_private { // #ifndef HAVE_IDEVICE_CONNECTION_GET_FD -// based on libimobiledevice/src/idevice.h -enum connection_type { - CONNECTION_USBMUXD = 1 -}; -struct idevice_connection_private { - char *udid; // added in v1.1.6 - enum connection_type type; - void *data; - void *ssl_data; -}; -wi_status idevice_connection_get_fd(idevice_connection_t connection, - int *to_fd) { - if (!connection || !to_fd) { - return WI_ERROR; - } - idevice_connection_private *c = ( - (sizeof(*connection) == sizeof(idevice_connection_private)) ? - (idevice_connection_private *) connection : NULL); - if (!c || c->type != CONNECTION_USBMUXD || c->data <= 0 || c->ssl_data) { - perror("Invalid idevice_connection struct. Please verify that " - __FILE__ "'s idevice_connection_private matches your version of" - " libimbiledevice/src/idevice.h"); +wi_status idevice_connection_get_fd(idevice_connection_t connection, int *to_fd) +{ + if (!connection || !to_fd) return WI_ERROR; + + idevice_connection_private *c = ((sizeof(*connection) == sizeof(idevice_connection_private)) ? (idevice_connection_private *) connection : NULL); + + if (!c || c->type != CONNECTION_USBMUXD || c->data <= 0 || c->ssl_data) + { + perror("Invalid idevice_connection struct. Please verify that " + __FILE__ "'s idevice_connection_private matches your version of" + " libimbiledevice/src/idevice.h"); + return WI_ERROR; } + int fd = (int)(long)c->data; struct stat fd_stat; if (fstat(fd, &fd_stat) < 0 || !S_ISSOCK(fd_stat.st_mode)) { @@ -98,6 +92,7 @@ int wi_connect(const char *device_id, char **to_device_id, lockdownd_client_t client = NULL; idevice_connection_t connection = NULL; int fd = -1; + int vers[3] = {0, 0, 0}; // get phone if (idevice_new(&phone, device_id)) { @@ -128,7 +123,7 @@ int wi_connect(const char *device_id, char **to_device_id, } if (to_device_os_version && !lockdownd_get_value(client, NULL, "ProductVersion", &node)) { - int vers[3] = {0, 0, 0}; + char *s_version = NULL; plist_get_string_val(node, &s_version); if (s_version && sscanf(s_version, "%d.%d.%d", @@ -156,6 +151,17 @@ int wi_connect(const char *device_id, char **to_device_id, goto leave_cleanup; } + if(vers[0]>=13) { + service_client_t client_srv = (service_client_t)malloc(sizeof(struct service_client_private)); + client_srv->connection = connection; + + /* enable SSL if requested */ + if (service->ssl_enabled == 1){ + service_enable_ssl(client_srv); + connectionSSL = client_srv->connection; + } + } + if (client) { // not needed anymore lockdownd_client_free(client); @@ -208,7 +214,7 @@ int wi_connect(const char *device_id, char **to_device_id, #endif // don't call usbmuxd_disconnect(fd)! //idevice_disconnect(connection); - free(connection); + //free(connection); // connectionSSL reuses - keep it... lockdownd_client_free(client); idevice_free(phone); return ret; diff --git a/src/websocket.c b/src/websocket.c old mode 100644 new mode 100755 index 3437deff..8740a4bd --- a/src/websocket.c +++ b/src/websocket.c @@ -32,6 +32,7 @@ typedef int8_t ws_state; #define STATE_CLOSED 7 + struct ws_private { ws_state state;