Skip to content

ios 13 support changes - Perfecto contribution #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions include/ios-webkit-debug-proxy/socket_manager.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,28 @@ extern "C" {
#include <stdint.h>
#include <stddef.h>

#include <libimobiledevice/libimobiledevice.h>
#include <libimobiledevice/service.h>

// 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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/char_buffer.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include "char_buffer.h"

void* connectionSSL = NULL;

#define MIN_LENGTH 1024

cb_t cb_new() {
Expand Down
1 change: 1 addition & 0 deletions src/ios_webkit_debug_proxy_main.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
50 changes: 43 additions & 7 deletions src/socket_manager.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <pthread.h>
#include <time.h>
#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
Expand All @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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) {
Expand Down
56 changes: 31 additions & 25 deletions src/webinspector.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
#include <libimobiledevice/installation_proxy.h>
#include <libimobiledevice/libimobiledevice.h>
#include <libimobiledevice/lockdown.h>
#include <libimobiledevice/service.h>

#include "char_buffer.h"
#include "webinspector.h"

#include "socket_manager.h"

#define WI_DEBUG 1

Expand All @@ -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;
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/websocket.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ typedef int8_t ws_state;
#define STATE_CLOSED 7



struct ws_private {
ws_state state;

Expand Down