diff options
Diffstat (limited to 'tools/iproxy.c')
| -rw-r--r-- | tools/iproxy.c | 281 |
1 files changed, 0 insertions, 281 deletions
diff --git a/tools/iproxy.c b/tools/iproxy.c deleted file mode 100644 index e4e39f6..0000000 --- a/tools/iproxy.c +++ /dev/null | |||
| @@ -1,281 +0,0 @@ | |||
| 1 | /* | ||
| 2 | iproxy -- proxy that enables tcp service access to iPhone/iPod | ||
| 3 | |||
| 4 | Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> | ||
| 5 | Copyright (C) 2009 Paul Sladen <libiphone@paul.sladen.org> | ||
| 6 | |||
| 7 | Based upon iTunnel source code, Copyright (c) 2008 Jing Su. | ||
| 8 | http://www.cs.toronto.edu/~jingsu/itunnel/ | ||
| 9 | |||
| 10 | This program is free software; you can redistribute it and/or modify | ||
| 11 | it under the terms of the GNU General Public License as published by | ||
| 12 | the Free Software Foundation; either version 2 of the License, or | ||
| 13 | (at your option) any later version. | ||
| 14 | |||
| 15 | This program is distributed in the hope that it will be useful, | ||
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 18 | GNU General Public License for more details. | ||
| 19 | |||
| 20 | You should have received a copy of the GNU General Public License | ||
| 21 | along with this program; if not, write to the Free Software | ||
| 22 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 23 | |||
| 24 | TODO: improve code... | ||
| 25 | |||
| 26 | */ | ||
| 27 | |||
| 28 | #include <stdio.h> | ||
| 29 | #include <stdlib.h> | ||
| 30 | #include <string.h> | ||
| 31 | #include <fcntl.h> | ||
| 32 | #include <stddef.h> | ||
| 33 | #include <unistd.h> | ||
| 34 | #include <errno.h> | ||
| 35 | #ifdef WIN32 | ||
| 36 | #include <windows.h> | ||
| 37 | #include <winsock2.h> | ||
| 38 | typedef unsigned int socklen_t; | ||
| 39 | #else | ||
| 40 | #include <sys/socket.h> | ||
| 41 | #include <sys/un.h> | ||
| 42 | #include <arpa/inet.h> | ||
| 43 | #include <pthread.h> | ||
| 44 | #include <netinet/in.h> | ||
| 45 | #endif | ||
| 46 | #include "sock_stuff.h" | ||
| 47 | #include "usbmuxd.h" | ||
| 48 | |||
| 49 | static uint16_t listen_port = 0; | ||
| 50 | static uint16_t device_port = 0; | ||
| 51 | |||
| 52 | struct client_data { | ||
| 53 | int fd; | ||
| 54 | int sfd; | ||
| 55 | volatile int stop_ctos; | ||
| 56 | volatile int stop_stoc; | ||
| 57 | }; | ||
| 58 | |||
| 59 | void *run_stoc_loop(void *arg) | ||
| 60 | { | ||
| 61 | struct client_data *cdata = (struct client_data*)arg; | ||
| 62 | int recv_len; | ||
| 63 | int sent; | ||
| 64 | char buffer[131072]; | ||
| 65 | |||
| 66 | printf("%s: fd = %d\n", __func__, cdata->fd); | ||
| 67 | |||
| 68 | while (!cdata->stop_stoc && cdata->fd>0 && cdata->sfd>0) { | ||
| 69 | recv_len = recv_buf_timeout(cdata->sfd, buffer, sizeof(buffer), 0, 5000); | ||
| 70 | if (recv_len <= 0) { | ||
| 71 | if (recv_len == 0) { | ||
| 72 | // try again | ||
| 73 | continue; | ||
| 74 | } else { | ||
| 75 | fprintf(stderr, "recv failed: %s\n", strerror(errno)); | ||
| 76 | break; | ||
| 77 | } | ||
| 78 | } else { | ||
| 79 | // printf("received %d bytes from server\n", recv_len); | ||
| 80 | // send to socket | ||
| 81 | sent = send_buf(cdata->fd, buffer, recv_len); | ||
| 82 | if (sent < recv_len) { | ||
| 83 | if (sent <= 0) { | ||
| 84 | fprintf(stderr, "send failed: %s\n", strerror(errno)); | ||
| 85 | break; | ||
| 86 | } else { | ||
| 87 | fprintf(stderr, "only sent %d from %d bytes\n", sent, recv_len); | ||
| 88 | } | ||
| 89 | } else { | ||
| 90 | // sending succeeded, receive from device | ||
| 91 | // printf("pushed %d bytes to client\n", sent); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | close(cdata->fd); | ||
| 96 | cdata->fd = -1; | ||
| 97 | cdata->stop_ctos = 1; | ||
| 98 | |||
| 99 | return NULL; | ||
| 100 | } | ||
| 101 | |||
| 102 | void *run_ctos_loop(void *arg) | ||
| 103 | { | ||
| 104 | struct client_data *cdata = (struct client_data*)arg; | ||
| 105 | int recv_len; | ||
| 106 | int sent; | ||
| 107 | char buffer[131072]; | ||
| 108 | #ifdef WIN32 | ||
| 109 | HANDLE stoc = NULL; | ||
| 110 | #else | ||
| 111 | pthread_t stoc; | ||
| 112 | #endif | ||
| 113 | |||
| 114 | printf("%s: fd = %d\n", __func__, cdata->fd); | ||
| 115 | |||
| 116 | cdata->stop_stoc = 0; | ||
| 117 | #ifdef WIN32 | ||
| 118 | stoc = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)run_stoc_loop, cdata, 0, NULL); | ||
| 119 | #else | ||
| 120 | pthread_create(&stoc, NULL, run_stoc_loop, cdata); | ||
| 121 | #endif | ||
| 122 | |||
| 123 | while (!cdata->stop_ctos && cdata->fd>0 && cdata->sfd>0) { | ||
| 124 | recv_len = recv_buf_timeout(cdata->fd, buffer, sizeof(buffer), 0, 5000); | ||
| 125 | if (recv_len <= 0) { | ||
| 126 | if (recv_len == 0) { | ||
| 127 | // try again | ||
| 128 | continue; | ||
| 129 | } else { | ||
| 130 | fprintf(stderr, "recv failed: %s\n", strerror(errno)); | ||
| 131 | break; | ||
| 132 | } | ||
| 133 | } else { | ||
| 134 | // printf("pulled %d bytes from client\n", recv_len); | ||
| 135 | // send to local socket | ||
| 136 | sent = send_buf(cdata->sfd, buffer, recv_len); | ||
| 137 | if (sent < recv_len) { | ||
| 138 | if (sent <= 0) { | ||
| 139 | fprintf(stderr, "send failed: %s\n", strerror(errno)); | ||
| 140 | break; | ||
| 141 | } else { | ||
| 142 | fprintf(stderr, "only sent %d from %d bytes\n", sent, recv_len); | ||
| 143 | } | ||
| 144 | } else { | ||
| 145 | // sending succeeded, receive from device | ||
| 146 | // printf("sent %d bytes to server\n", sent); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | close(cdata->fd); | ||
| 151 | cdata->fd = -1; | ||
| 152 | cdata->stop_stoc = 1; | ||
| 153 | |||
| 154 | #ifdef WIN32 | ||
| 155 | WaitForSingleObject(stoc, INFINITE); | ||
| 156 | #else | ||
| 157 | pthread_join(stoc, NULL); | ||
| 158 | #endif | ||
| 159 | |||
| 160 | return NULL; | ||
| 161 | } | ||
| 162 | |||
| 163 | void *acceptor_thread(void *arg) | ||
| 164 | { | ||
| 165 | struct client_data *cdata; | ||
| 166 | usbmuxd_device_info_t *dev_list = NULL; | ||
| 167 | #ifdef WIN32 | ||
| 168 | HANDLE ctos = NULL; | ||
| 169 | #else | ||
| 170 | pthread_t ctos; | ||
| 171 | #endif | ||
| 172 | int count; | ||
| 173 | |||
| 174 | if (!arg) { | ||
| 175 | fprintf(stderr, "invalid client_data provided!\n"); | ||
| 176 | return NULL; | ||
| 177 | } | ||
| 178 | |||
| 179 | cdata = (struct client_data*)arg; | ||
| 180 | |||
| 181 | if ((count = usbmuxd_get_device_list(&dev_list)) < 0) { | ||
| 182 | printf("Connecting to usbmuxd failed, terminating.\n"); | ||
| 183 | free(dev_list); | ||
| 184 | return NULL; | ||
| 185 | } | ||
| 186 | |||
| 187 | fprintf(stdout, "Number of available devices == %d\n", count); | ||
| 188 | |||
| 189 | if (dev_list == NULL || dev_list[0].handle == 0) { | ||
| 190 | printf("No connected device found, terminating.\n"); | ||
| 191 | free(dev_list); | ||
| 192 | return NULL; | ||
| 193 | } | ||
| 194 | |||
| 195 | fprintf(stdout, "Requesting connecion to device handle == %d (serial: %s), port %d\n", dev_list[0].handle, dev_list[0].udid, device_port); | ||
| 196 | |||
| 197 | cdata->sfd = usbmuxd_connect(dev_list[0].handle, device_port); | ||
| 198 | free(dev_list); | ||
| 199 | if (cdata->sfd < 0) { | ||
| 200 | fprintf(stderr, "Error connecting to device!\n"); | ||
| 201 | } else { | ||
| 202 | cdata->stop_ctos = 0; | ||
| 203 | #ifdef WIN32 | ||
| 204 | ctos = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)run_ctos_loop, cdata, 0, NULL); | ||
| 205 | WaitForSingleObject(ctos, INFINITE); | ||
| 206 | #else | ||
| 207 | pthread_create(&ctos, NULL, run_ctos_loop, cdata); | ||
| 208 | pthread_join(ctos, NULL); | ||
| 209 | #endif | ||
| 210 | } | ||
| 211 | |||
| 212 | if (cdata->fd > 0) { | ||
| 213 | close(cdata->fd); | ||
| 214 | } | ||
| 215 | if (cdata->sfd > 0) { | ||
| 216 | close(cdata->sfd); | ||
| 217 | } | ||
| 218 | |||
| 219 | return NULL; | ||
| 220 | } | ||
| 221 | |||
| 222 | int main(int argc, char **argv) | ||
| 223 | { | ||
| 224 | int mysock = -1; | ||
| 225 | |||
| 226 | if (argc != 3) { | ||
| 227 | printf("usage: %s LOCAL_TCP_PORT DEVICE_TCP_PORT\n", argv[0]); | ||
| 228 | return 0; | ||
| 229 | } | ||
| 230 | |||
| 231 | listen_port = atoi(argv[1]); | ||
| 232 | device_port = atoi(argv[2]); | ||
| 233 | |||
| 234 | if (!listen_port) { | ||
| 235 | fprintf(stderr, "Invalid listen_port specified!\n"); | ||
| 236 | return -EINVAL; | ||
| 237 | } | ||
| 238 | |||
| 239 | if (!device_port) { | ||
| 240 | fprintf(stderr, "Invalid device_port specified!\n"); | ||
| 241 | return -EINVAL; | ||
| 242 | } | ||
| 243 | |||
| 244 | // first create the listening socket endpoint waiting for connections. | ||
| 245 | mysock = create_socket(listen_port); | ||
| 246 | if (mysock < 0) { | ||
| 247 | fprintf(stderr, "Error creating socket: %s\n", strerror(errno)); | ||
| 248 | return -errno; | ||
| 249 | } else { | ||
| 250 | #ifdef WIN32 | ||
| 251 | HANDLE acceptor = NULL; | ||
| 252 | #else | ||
| 253 | pthread_t acceptor; | ||
| 254 | #endif | ||
| 255 | struct sockaddr_in c_addr; | ||
| 256 | socklen_t len = sizeof(struct sockaddr_in); | ||
| 257 | struct client_data cdata; | ||
| 258 | int c_sock; | ||
| 259 | while (1) { | ||
| 260 | printf("waiting for connection\n"); | ||
| 261 | c_sock = accept(mysock, (struct sockaddr*)&c_addr, &len); | ||
| 262 | if (c_sock) { | ||
| 263 | printf("accepted connection, fd = %d\n", c_sock); | ||
| 264 | cdata.fd = c_sock; | ||
| 265 | #ifdef WIN32 | ||
| 266 | acceptor = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)acceptor_thread, &cdata, 0, NULL); | ||
| 267 | WaitForSingleObject(acceptor, INFINITE); | ||
| 268 | #else | ||
| 269 | pthread_create(&acceptor, NULL, acceptor_thread, &cdata); | ||
| 270 | pthread_join(acceptor, NULL); | ||
| 271 | #endif | ||
| 272 | } else { | ||
| 273 | break; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | close(c_sock); | ||
| 277 | close(mysock); | ||
| 278 | } | ||
| 279 | |||
| 280 | return 0; | ||
| 281 | } | ||
