diff options
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/libusbmuxd.c | 42 | ||||
-rw-r--r-- | src/sock_stuff.h | 65 | ||||
-rw-r--r-- | src/socket.c (renamed from src/sock_stuff.c) | 113 | ||||
-rw-r--r-- | src/socket.h | 65 | ||||
-rw-r--r-- | tools/iproxy.c | 381 |
6 files changed, 340 insertions, 328 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 71f218c..4cd338e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,7 +8,7 @@ libusbmuxd_la_LDFLAGS = $(AM_LDFLAGS) -version-info $(LIBUSBMUXD_SO_VERSION) -no libusbmuxd_la_LIBADD = libusbmuxd_la_SOURCES = \ collection.c collection.h \ - sock_stuff.c sock_stuff.h \ + socket.c socket.h \ libusbmuxd.c if WIN32 diff --git a/src/libusbmuxd.c b/src/libusbmuxd.c index 1cf6dcb..97114a8 100644 --- a/src/libusbmuxd.c +++ b/src/libusbmuxd.c @@ -69,7 +69,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // usbmuxd protocol #include "usbmuxd-proto.h" // socket utility functions -#include "sock_stuff.h" +#include "socket.h" // misc utility functions #include "collection.h" @@ -111,9 +111,9 @@ static usbmuxd_device_info_t *devices_find(uint32_t handle) static int connect_usbmuxd_socket() { #if defined(WIN32) || defined(__CYGWIN__) - return connect_socket("127.0.0.1", USBMUXD_SOCKET_PORT); + return socket_connect("127.0.0.1", USBMUXD_SOCKET_PORT); #else - return connect_unix_socket(USBMUXD_SOCKET_FILE); + return socket_connect_unix(USBMUXD_SOCKET_FILE); #endif } @@ -169,7 +169,7 @@ static int receive_packet(int sfd, struct usbmuxd_header *header, void **payload header->message = 0; header->tag = 0; - recv_len = recv_buf_timeout(sfd, &hdr, sizeof(hdr), 0, timeout); + recv_len = socket_receive_timeout(sfd, &hdr, sizeof(hdr), 0, timeout); if (recv_len < 0) { return recv_len; } else if ((size_t)recv_len < sizeof(hdr)) { @@ -179,7 +179,7 @@ static int receive_packet(int sfd, struct usbmuxd_header *header, void **payload uint32_t payload_size = hdr.length - sizeof(hdr); if (payload_size > 0) { payload_loc = (char*)malloc(payload_size); - if (recv_buf_timeout(sfd, payload_loc, payload_size, 0, 5000) != (int)payload_size) { + if (socket_receive_timeout(sfd, payload_loc, payload_size, 0, 5000) != (int)payload_size) { DEBUG(1, "%s: Error receiving payload of size %d\n", __func__, payload_size); free(payload_loc); return -EBADMSG; @@ -204,7 +204,7 @@ static int receive_packet(int sfd, struct usbmuxd_header *header, void **payload memcpy(header, &hdr, sizeof(hdr)); return hdr.length; } - + plist_get_string_val(node, &message); if (message) { uint64_t val = 0; @@ -338,17 +338,17 @@ static int send_packet(int sfd, uint32_t message, uint32_t tag, void *payload, u if (payload && (payload_size > 0)) { header.length += payload_size; } - int sent = send_buf(sfd, &header, sizeof(header)); + int sent = socket_send(sfd, &header, sizeof(header)); if (sent != sizeof(header)) { DEBUG(1, "%s: ERROR: could not send packet header\n", __func__); return -1; } if (payload && (payload_size > 0)) { - sent += send_buf(sfd, payload, payload_size); + sent += socket_send(sfd, payload, payload_size); } if (sent != (int)header.length) { DEBUG(1, "%s: ERROR: could not send whole packet\n", __func__); - close_socket(sfd); + socket_close(sfd); return -1; } return sent; @@ -591,11 +591,11 @@ retry: tag = ++use_tag; if (send_listen_packet(sfd, tag) <= 0) { DEBUG(1, "%s: ERROR: could not send listen packet\n", __func__); - close_socket(sfd); + socket_close(sfd); return -1; } if (usbmuxd_get_result(sfd, tag, &res, NULL) && (res != 0)) { - close_socket(sfd); + socket_close(sfd); if ((res == RESULT_BADVERSION) && (proto_version == 1)) { proto_version = 0; goto retry; @@ -685,7 +685,7 @@ static void device_monitor_cleanup(void* data) } ENDFOREACH collection_free(&devices); - close_socket(listenfd); + socket_close(listenfd); listenfd = -1; } @@ -753,7 +753,7 @@ int usbmuxd_unsubscribe() { event_cb = NULL; - shutdown_socket(listenfd, SHUT_RDWR); + socket_shutdown(listenfd, SHUT_RDWR); #ifdef WIN32 if (devmon != NULL) { @@ -842,7 +842,7 @@ retry: if (res == RESULT_BADVERSION) { proto_version = 0; } - close_socket(sfd); + socket_close(sfd); try_list_devices = 0; goto retry; } @@ -856,7 +856,7 @@ retry: if (usbmuxd_get_result(sfd, tag, &res, NULL) && (res == 0)) { listen_success = 1; } else { - close_socket(sfd); + socket_close(sfd); if ((res == RESULT_BADVERSION) && (proto_version == 1)) { proto_version = 0; goto retry; @@ -918,7 +918,7 @@ retry: got_device_list: // explicitly close connection - close_socket(sfd); + socket_close(sfd); // create copy of device info entries from collection newlist = (usbmuxd_device_info_t*)malloc(sizeof(usbmuxd_device_info_t) * (collection_count(&tmpdevs) + 1)); @@ -1009,7 +1009,7 @@ retry: } else { if ((res == RESULT_BADVERSION) && (proto_version == 1)) { proto_version = 0; - close_socket(sfd); + socket_close(sfd); goto retry; } DEBUG(1, "%s: Connect failed, Error code=%d\n", __func__, res); @@ -1021,14 +1021,14 @@ retry: return sfd; } - close_socket(sfd); + socket_close(sfd); return -1; } int usbmuxd_disconnect(int sfd) { - return close_socket(sfd); + return socket_close(sfd); } int usbmuxd_send(int sfd, const char *data, uint32_t len, uint32_t *sent_bytes) @@ -1056,7 +1056,7 @@ int usbmuxd_send(int sfd, const char *data, uint32_t len, uint32_t *sent_bytes) int usbmuxd_recv_timeout(int sfd, char *data, uint32_t len, uint32_t *recv_bytes, unsigned int timeout) { - int num_recv = recv_buf_timeout(sfd, (void*)data, len, 0, timeout); + int num_recv = socket_receive_timeout(sfd, (void*)data, len, 0, timeout); if (num_recv < 0) { *recv_bytes = 0; return num_recv; @@ -1241,5 +1241,5 @@ void libusbmuxd_set_use_inotify(int set) void libusbmuxd_set_debug_level(int level) { libusbmuxd_debug = level; - sock_stuff_set_verbose(level); + socket_set_verbose(level); } diff --git a/src/sock_stuff.h b/src/sock_stuff.h deleted file mode 100644 index 5efcd27..0000000 --- a/src/sock_stuff.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - libusbmuxd - client library to talk to usbmuxd - -Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> -Copyright (C) 2009 Paul Sladen <libiphone@paul.sladen.org> -Copyright (C) 2009 Martin Szulecki <opensuse@sukimashita.com> - -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 General Public License for more details. - -You should have received a copy of the GNU Lesser General Public -License along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -*/ - -#ifndef __SOCK_STUFF_H -#define __SOCK_STUFF_H - -#include <stdint.h> - -enum fd_mode { - FDM_READ, - FDM_WRITE, - FDM_EXCEPT -}; -typedef enum fd_mode fd_mode; - -#ifdef WIN32 -#include <winsock2.h> -#define SHUT_RD SD_READ -#define SHUT_WR SD_WRITE -#define SHUT_RDWR SD_BOTH -#endif - -#ifndef WIN32 -int create_unix_socket(const char *filename); -int connect_unix_socket(const char *filename); -#endif -int create_socket(uint16_t port); -#if defined(WIN32) || defined(__CYGWIN__) -int connect_socket(const char *addr, uint16_t port); -#endif -int check_fd(int fd, fd_mode fdm, unsigned int timeout); - -int shutdown_socket(int fd, int how); -int close_socket(int fd); - -int recv_buf(int fd, void *data, size_t size); -int peek_buf(int fd, void *data, size_t size); -int recv_buf_timeout(int fd, void *data, size_t size, int flags, - unsigned int timeout); - -int send_buf(int fd, void *data, size_t size); - -void sock_stuff_set_verbose(int level); - -#endif /* __SOCK_STUFF_H */ diff --git a/src/sock_stuff.c b/src/socket.c index 609c8ad..c35de33 100644 --- a/src/sock_stuff.c +++ b/src/socket.c @@ -1,25 +1,23 @@ /* - libusbmuxd - client library to talk to usbmuxd - -Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> -Copyright (C) 2009 Paul Sladen <libiphone@paul.sladen.org> -Copyright (C) 2009 Martin Szulecki <opensuse@sukimashita.com> - -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 General Public License for more details. - -You should have received a copy of the GNU Lesser General Public -License along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -*/ + * socket.c + * + * Copyright (c) 2012 Martin Szulecki All Rights Reserved. + * Copyright (c) 2012 Nikias Bassen 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 <stdio.h> #include <stddef.h> @@ -30,8 +28,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include <sys/time.h> #include <sys/stat.h> #ifdef WIN32 -#include <windows.h> #include <winsock2.h> +#include <windows.h> static int wsa_init = 0; #else #include <sys/socket.h> @@ -40,19 +38,19 @@ static int wsa_init = 0; #include <netdb.h> #include <arpa/inet.h> #endif -#include "sock_stuff.h" +#include "socket.h" #define RECV_TIMEOUT 20000 static int verbose = 0; -void sock_stuff_set_verbose(int level) +void socket_set_verbose(int level) { verbose = level; } #ifndef WIN32 -int create_unix_socket(const char *filename) +int socket_create_unix(const char *filename) { struct sockaddr_un name; int sock; @@ -85,20 +83,20 @@ int create_unix_socket(const char *filename) if (bind(sock, (struct sockaddr *) &name, size) < 0) { perror("bind"); - close_socket(sock); + socket_close(sock); return -1; } if (listen(sock, 10) < 0) { perror("listen"); - close_socket(sock); + socket_close(sock); return -1; } return sock; } -int connect_unix_socket(const char *filename) +int socket_connect_unix(const char *filename) { struct sockaddr_un name; int sfd = -1; @@ -134,7 +132,7 @@ int connect_unix_socket(const char *filename) + strlen(name.sun_path) + 1); if (connect(sfd, (struct sockaddr *) &name, size) < 0) { - close_socket(sfd); + socket_close(sfd); if (verbose >= 2) fprintf(stderr, "%s: connect: %s\n", __func__, strerror(errno)); @@ -145,7 +143,7 @@ int connect_unix_socket(const char *filename) } #endif -int create_socket(uint16_t port) +int socket_create(uint16_t port) { int sfd = -1; int yes = 1; @@ -168,7 +166,7 @@ int create_socket(uint16_t port) if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int)) == -1) { perror("setsockopt()"); - close_socket(sfd); + socket_close(sfd); return -1; } @@ -179,21 +177,20 @@ int create_socket(uint16_t port) if (0 > bind(sfd, (struct sockaddr *) &saddr, sizeof(saddr))) { perror("bind()"); - close_socket(sfd); + socket_close(sfd); return -1; } if (listen(sfd, 1) == -1) { perror("listen()"); - close_socket(sfd); + socket_close(sfd); return -1; } return sfd; } -#if defined(WIN32) || defined(__CYGWIN__) -int connect_socket(const char *addr, uint16_t port) +int socket_connect(const char *addr, uint16_t port) { int sfd = -1; int yes = 1; @@ -235,7 +232,7 @@ int connect_socket(const char *addr, uint16_t port) if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int)) == -1) { perror("setsockopt()"); - close_socket(sfd); + socket_close(sfd); return -1; } @@ -246,15 +243,14 @@ int connect_socket(const char *addr, uint16_t port) if (connect(sfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { perror("connect"); - close_socket(sfd); + socket_close(sfd); return -2; } return sfd; } -#endif /* WIN32 || __CYGWIN__ */ -int check_fd(int fd, fd_mode fdm, unsigned int timeout) +int socket_check_fd(int fd, fd_mode fdm, unsigned int timeout) { fd_set fds; int sret; @@ -321,12 +317,33 @@ int check_fd(int fd, fd_mode fdm, unsigned int timeout) return sret; } -int shutdown_socket(int fd, int how) +int socket_accept(int fd, uint16_t port) +{ +#ifdef WIN32 + int addr_len; +#else + socklen_t addr_len; +#endif + int result; + struct sockaddr_in addr; + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_ANY); + addr.sin_port = htons(port); + + addr_len = sizeof(addr); + result = accept(fd, (struct sockaddr*)&addr, &addr_len); + + return result; +} + +int socket_shutdown(int fd, int how) { return shutdown(fd, how); } -int close_socket(int fd) { +int socket_close(int fd) { #ifdef WIN32 return closesocket(fd); #else @@ -334,24 +351,24 @@ int close_socket(int fd) { #endif } -int recv_buf(int fd, void *data, size_t length) +int socket_receive(int fd, void *data, size_t length) { - return recv_buf_timeout(fd, data, length, 0, RECV_TIMEOUT); + return socket_receive_timeout(fd, data, length, 0, RECV_TIMEOUT); } -int peek_buf(int fd, void *data, size_t length) +int socket_peek(int fd, void *data, size_t length) { - return recv_buf_timeout(fd, data, length, MSG_PEEK, RECV_TIMEOUT); + return socket_receive_timeout(fd, data, length, MSG_PEEK, RECV_TIMEOUT); } -int recv_buf_timeout(int fd, void *data, size_t length, int flags, +int socket_receive_timeout(int fd, void *data, size_t length, int flags, unsigned int timeout) { int res; int result; // check if data is available - res = check_fd(fd, FDM_READ, timeout); + res = socket_check_fd(fd, FDM_READ, timeout); if (res <= 0) { return res; } @@ -369,7 +386,7 @@ int recv_buf_timeout(int fd, void *data, size_t length, int flags, return result; } -int send_buf(int fd, void *data, size_t length) +int socket_send(int fd, void *data, size_t length) { return send(fd, data, length, 0); } diff --git a/src/socket.h b/src/socket.h new file mode 100644 index 0000000..c2b2599 --- /dev/null +++ b/src/socket.h @@ -0,0 +1,65 @@ +/* + * socket.h + * + * Copyright (c) 2012 Martin Szulecki All Rights Reserved. + * Copyright (c) 2012 Nikias Bassen 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 __SOCKET_SOCKET_H +#define __SOCKET_SOCKET_H + +#include <stdlib.h> +#include <stdint.h> + +enum fd_mode { + FDM_READ, + FDM_WRITE, + FDM_EXCEPT +}; +typedef enum fd_mode fd_mode; + +#ifdef WIN32 +#include <winsock2.h> +#define SHUT_RD SD_READ +#define SHUT_WR SD_WRITE +#define SHUT_RDWR SD_BOTH +#else +#include <sys/socket.h> +#endif + +#ifndef WIN32 +int socket_create_unix(const char *filename); +int socket_connect_unix(const char *filename); +#endif +int socket_create(uint16_t port); +int socket_connect(const char *addr, uint16_t port); +int socket_check_fd(int fd, fd_mode fdm, unsigned int timeout); +int socket_accept(int fd, uint16_t port); + +int socket_shutdown(int fd, int how); +int socket_close(int fd); + +int socket_receive(int fd, void *data, size_t size); +int socket_peek(int fd, void *data, size_t size); +int socket_receive_timeout(int fd, void *data, size_t size, int flags, + unsigned int timeout); + +int socket_send(int fd, void *data, size_t size); + +void socket_set_verbose(int level); + +#endif /* __SOCKET_SOCKET_H */ diff --git a/tools/iproxy.c b/tools/iproxy.c index f7c0827..abda951 100644 --- a/tools/iproxy.c +++ b/tools/iproxy.c @@ -1,29 +1,27 @@ /* - iproxy -- proxy that enables tcp service access to iPhone/iPod - -Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> -Copyright (C) 2009 Paul Sladen <libiphone@paul.sladen.org> - -Based upon iTunnel source code, Copyright (c) 2008 Jing Su. -http://www.cs.toronto.edu/~jingsu/itunnel/ - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program 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 General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -TODO: improve code... - -*/ + * iproxy.c -- proxy that enables tcp service access to iOS devices + * + * Copyright (C) 2014 Martin Szulecki <m.szulecki@libimobiledevice.org> + * Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> + * Copyright (C) 2009 Paul Sladen <libiphone@paul.sladen.org> + * + * Based upon iTunnel source code, Copyright (c) 2008 Jing Su. + * http://www.cs.toronto.edu/~jingsu/itunnel/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ #include <stdio.h> #include <stdlib.h> @@ -43,239 +41,236 @@ typedef unsigned int socklen_t; #include <pthread.h> #include <netinet/in.h> #endif -#include "sock_stuff.h" +#include "socket.h" #include "usbmuxd.h" static uint16_t listen_port = 0; static uint16_t device_port = 0; struct client_data { - int fd; - int sfd; - volatile int stop_ctos; - volatile int stop_stoc; + int fd; + int sfd; + volatile int stop_ctos; + volatile int stop_stoc; }; static void *run_stoc_loop(void *arg) { - struct client_data *cdata = (struct client_data*)arg; - int recv_len; - int sent; - char buffer[131072]; - - printf("%s: fd = %d\n", __func__, cdata->fd); - - while (!cdata->stop_stoc && cdata->fd>0 && cdata->sfd>0) { - recv_len = recv_buf_timeout(cdata->sfd, buffer, sizeof(buffer), 0, 5000); - if (recv_len <= 0) { - if (recv_len == 0) { - // try again - continue; - } else { - fprintf(stderr, "recv failed: %s\n", strerror(errno)); - break; - } - } else { -// printf("received %d bytes from server\n", recv_len); - // send to socket - sent = send_buf(cdata->fd, buffer, recv_len); - if (sent < recv_len) { - if (sent <= 0) { - fprintf(stderr, "send failed: %s\n", strerror(errno)); - break; + struct client_data *cdata = (struct client_data*)arg; + int recv_len; + int sent; + char buffer[131072]; + + printf("%s: fd = %d\n", __func__, cdata->fd); + + while (!cdata->stop_stoc && cdata->fd > 0 && cdata->sfd > 0) { + recv_len = socket_receive_timeout(cdata->sfd, buffer, sizeof(buffer), 0, 5000); + if (recv_len <= 0) { + if (recv_len == 0) { + // try again + continue; + } else { + fprintf(stderr, "recv failed: %s\n", strerror(errno)); + break; + } } else { - fprintf(stderr, "only sent %d from %d bytes\n", sent, recv_len); + // send to socket + sent = socket_send(cdata->fd, buffer, recv_len); + if (sent < recv_len) { + if (sent <= 0) { + fprintf(stderr, "send failed: %s\n", strerror(errno)); + break; + } else { + fprintf(stderr, "only sent %d from %d bytes\n", sent, recv_len); + } + } } - } else { - // sending succeeded, receive from device -// printf("pushed %d bytes to client\n", sent); - } } - } - close(cdata->fd); - cdata->fd = -1; - cdata->stop_ctos = 1; - return NULL; + close(cdata->fd); + + cdata->fd = -1; + cdata->stop_ctos = 1; + + return NULL; } static void *run_ctos_loop(void *arg) { - struct client_data *cdata = (struct client_data*)arg; - int recv_len; - int sent; - char buffer[131072]; + struct client_data *cdata = (struct client_data*)arg; + int recv_len; + int sent; + char buffer[131072]; #ifdef WIN32 - HANDLE stoc = NULL; + HANDLE stoc = NULL; #else - pthread_t stoc; + pthread_t stoc; #endif - printf("%s: fd = %d\n", __func__, cdata->fd); + printf("%s: fd = %d\n", __func__, cdata->fd); - cdata->stop_stoc = 0; + cdata->stop_stoc = 0; #ifdef WIN32 - stoc = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)run_stoc_loop, cdata, 0, NULL); + stoc = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)run_stoc_loop, cdata, 0, NULL); #else - pthread_create(&stoc, NULL, run_stoc_loop, cdata); + pthread_create(&stoc, NULL, run_stoc_loop, cdata); #endif - while (!cdata->stop_ctos && cdata->fd>0 && cdata->sfd>0) { - recv_len = recv_buf_timeout(cdata->fd, buffer, sizeof(buffer), 0, 5000); - if (recv_len <= 0) { - if (recv_len == 0) { - // try again - continue; - } else { - fprintf(stderr, "recv failed: %s\n", strerror(errno)); - break; - } - } else { -// printf("pulled %d bytes from client\n", recv_len); - // send to local socket - sent = send_buf(cdata->sfd, buffer, recv_len); - if (sent < recv_len) { - if (sent <= 0) { - fprintf(stderr, "send failed: %s\n", strerror(errno)); - break; + while (!cdata->stop_ctos && cdata->fd>0 && cdata->sfd>0) { + recv_len = socket_receive_timeout(cdata->fd, buffer, sizeof(buffer), 0, 5000); + if (recv_len <= 0) { + if (recv_len == 0) { + // try again + continue; + } else { + fprintf(stderr, "recv failed: %s\n", strerror(errno)); + break; + } } else { - fprintf(stderr, "only sent %d from %d bytes\n", sent, recv_len); + // send to local socket + sent = socket_send(cdata->sfd, buffer, recv_len); + if (sent < recv_len) { + if (sent <= 0) { + fprintf(stderr, "send failed: %s\n", strerror(errno)); + break; + } else { + fprintf(stderr, "only sent %d from %d bytes\n", sent, recv_len); + } + } } - } else { - // sending succeeded, receive from device -// printf("sent %d bytes to server\n", sent); - } } - } - close(cdata->fd); - cdata->fd = -1; - cdata->stop_stoc = 1; + + close(cdata->fd); + + cdata->fd = -1; + cdata->stop_stoc = 1; #ifdef WIN32 - WaitForSingleObject(stoc, INFINITE); + WaitForSingleObject(stoc, INFINITE); #else - pthread_join(stoc, NULL); + pthread_join(stoc, NULL); #endif - return NULL; + return NULL; } static void *acceptor_thread(void *arg) { - struct client_data *cdata; - usbmuxd_device_info_t *dev_list = NULL; + struct client_data *cdata; + usbmuxd_device_info_t *dev_list = NULL; #ifdef WIN32 - HANDLE ctos = NULL; + HANDLE ctos = NULL; #else - pthread_t ctos; + pthread_t ctos; #endif - int count; + int count; - if (!arg) { - fprintf(stderr, "invalid client_data provided!\n"); - return NULL; - } + if (!arg) { + fprintf(stderr, "invalid client_data provided!\n"); + return NULL; + } - cdata = (struct client_data*)arg; + cdata = (struct client_data*)arg; - if ((count = usbmuxd_get_device_list(&dev_list)) < 0) { - printf("Connecting to usbmuxd failed, terminating.\n"); - free(dev_list); - return NULL; - } + if ((count = usbmuxd_get_device_list(&dev_list)) < 0) { + printf("Connecting to usbmuxd failed, terminating.\n"); + free(dev_list); + return NULL; + } - fprintf(stdout, "Number of available devices == %d\n", count); + fprintf(stdout, "Number of available devices == %d\n", count); - if (dev_list == NULL || dev_list[0].handle == 0) { - printf("No connected device found, terminating.\n"); - free(dev_list); - return NULL; - } + if (dev_list == NULL || dev_list[0].handle == 0) { + printf("No connected device found, terminating.\n"); + free(dev_list); + return NULL; + } - fprintf(stdout, "Requesting connecion to device handle == %d (serial: %s), port %d\n", dev_list[0].handle, dev_list[0].udid, device_port); + fprintf(stdout, "Requesting connecion to device handle == %d (serial: %s), port %d\n", dev_list[0].handle, dev_list[0].udid, device_port); + + cdata->sfd = usbmuxd_connect(dev_list[0].handle, device_port); + free(dev_list); + if (cdata->sfd < 0) { + fprintf(stderr, "Error connecting to device!\n"); + } else { + cdata->stop_ctos = 0; - cdata->sfd = usbmuxd_connect(dev_list[0].handle, device_port); - free(dev_list); - if (cdata->sfd < 0) { - fprintf(stderr, "Error connecting to device!\n"); - } else { - cdata->stop_ctos = 0; #ifdef WIN32 - ctos = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)run_ctos_loop, cdata, 0, NULL); - WaitForSingleObject(ctos, INFINITE); + ctos = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)run_ctos_loop, cdata, 0, NULL); + WaitForSingleObject(ctos, INFINITE); #else - pthread_create(&ctos, NULL, run_ctos_loop, cdata); - pthread_join(ctos, NULL); + pthread_create(&ctos, NULL, run_ctos_loop, cdata); + pthread_join(ctos, NULL); #endif - } + } - if (cdata->fd > 0) { - close(cdata->fd); - } - if (cdata->sfd > 0) { - close(cdata->sfd); - } + if (cdata->fd > 0) { + close(cdata->fd); + } + if (cdata->sfd > 0) { + close(cdata->sfd); + } - return NULL; + return NULL; } int main(int argc, char **argv) { - int mysock = -1; + int mysock = -1; - if (argc != 3) { - printf("usage: %s LOCAL_TCP_PORT DEVICE_TCP_PORT\n", argv[0]); - return 0; - } - - listen_port = atoi(argv[1]); - device_port = atoi(argv[2]); - - if (!listen_port) { - fprintf(stderr, "Invalid listen_port specified!\n"); - return -EINVAL; - } - - if (!device_port) { - fprintf(stderr, "Invalid device_port specified!\n"); - return -EINVAL; - } - - // first create the listening socket endpoint waiting for connections. - mysock = create_socket(listen_port); - if (mysock < 0) { - fprintf(stderr, "Error creating socket: %s\n", strerror(errno)); - return -errno; - } else { + if (argc != 3) { + printf("usage: %s LOCAL_TCP_PORT DEVICE_TCP_PORT\n", argv[0]); + return 0; + } + + listen_port = atoi(argv[1]); + device_port = atoi(argv[2]); + + if (!listen_port) { + fprintf(stderr, "Invalid listen_port specified!\n"); + return -EINVAL; + } + + if (!device_port) { + fprintf(stderr, "Invalid device_port specified!\n"); + return -EINVAL; + } + + // first create the listening socket endpoint waiting for connections. + mysock = socket_create(listen_port); + if (mysock < 0) { + fprintf(stderr, "Error creating socket: %s\n", strerror(errno)); + return -errno; + } else { #ifdef WIN32 - HANDLE acceptor = NULL; + HANDLE acceptor = NULL; #else - pthread_t acceptor; + pthread_t acceptor; #endif - struct sockaddr_in c_addr; - socklen_t len = sizeof(struct sockaddr_in); - struct client_data cdata; - int c_sock; - while (1) { - printf("waiting for connection\n"); - c_sock = accept(mysock, (struct sockaddr*)&c_addr, &len); - if (c_sock) { - printf("accepted connection, fd = %d\n", c_sock); - cdata.fd = c_sock; + struct sockaddr_in c_addr; + socklen_t len = sizeof(struct sockaddr_in); + struct client_data cdata; + int c_sock; + while (1) { + printf("waiting for connection\n"); + c_sock = accept(mysock, (struct sockaddr*)&c_addr, &len); + if (c_sock) { + printf("accepted connection, fd = %d\n", c_sock); + cdata.fd = c_sock; #ifdef WIN32 - acceptor = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)acceptor_thread, &cdata, 0, NULL); - WaitForSingleObject(acceptor, INFINITE); + acceptor = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)acceptor_thread, &cdata, 0, NULL); + WaitForSingleObject(acceptor, INFINITE); #else - pthread_create(&acceptor, NULL, acceptor_thread, &cdata); - pthread_join(acceptor, NULL); + pthread_create(&acceptor, NULL, acceptor_thread, &cdata); + pthread_join(acceptor, NULL); #endif - } else { - break; - } + } else { + break; + } + } + close(c_sock); + close(mysock); } - close(c_sock); - close(mysock); - } - return 0; + return 0; } |