diff options
Diffstat (limited to 'tools/iproxy.c')
-rw-r--r-- | tools/iproxy.c | 251 |
1 files changed, 0 insertions, 251 deletions
diff --git a/tools/iproxy.c b/tools/iproxy.c deleted file mode 100644 index 4469c48..0000000 --- a/tools/iproxy.c +++ /dev/null | |||
@@ -1,251 +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 | #endif | ||
44 | #include <pthread.h> | ||
45 | #include <netinet/in.h> | ||
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 | pthread_t stoc; | ||
109 | |||
110 | printf("%s: fd = %d\n", __func__, cdata->fd); | ||
111 | |||
112 | cdata->stop_stoc = 0; | ||
113 | pthread_create(&stoc, NULL, run_stoc_loop, cdata); | ||
114 | |||
115 | while (!cdata->stop_ctos && cdata->fd>0 && cdata->sfd>0) { | ||
116 | recv_len = recv_buf_timeout(cdata->fd, buffer, sizeof(buffer), 0, 5000); | ||
117 | if (recv_len <= 0) { | ||
118 | if (recv_len == 0) { | ||
119 | // try again | ||
120 | continue; | ||
121 | } else { | ||
122 | fprintf(stderr, "recv failed: %s\n", strerror(errno)); | ||
123 | break; | ||
124 | } | ||
125 | } else { | ||
126 | // printf("pulled %d bytes from client\n", recv_len); | ||
127 | // send to local socket | ||
128 | sent = send_buf(cdata->sfd, buffer, recv_len); | ||
129 | if (sent < recv_len) { | ||
130 | if (sent <= 0) { | ||
131 | fprintf(stderr, "send failed: %s\n", strerror(errno)); | ||
132 | break; | ||
133 | } else { | ||
134 | fprintf(stderr, "only sent %d from %d bytes\n", sent, recv_len); | ||
135 | } | ||
136 | } else { | ||
137 | // sending succeeded, receive from device | ||
138 | // printf("sent %d bytes to server\n", sent); | ||
139 | } | ||
140 | } | ||
141 | } | ||
142 | close(cdata->fd); | ||
143 | cdata->fd = -1; | ||
144 | cdata->stop_stoc = 1; | ||
145 | |||
146 | pthread_join(stoc, NULL); | ||
147 | |||
148 | return NULL; | ||
149 | } | ||
150 | |||
151 | void *acceptor_thread(void *arg) | ||
152 | { | ||
153 | struct client_data *cdata; | ||
154 | usbmuxd_device_info_t *dev_list = NULL; | ||
155 | pthread_t ctos; | ||
156 | int count; | ||
157 | |||
158 | if (!arg) { | ||
159 | fprintf(stderr, "invalid client_data provided!\n"); | ||
160 | return NULL; | ||
161 | } | ||
162 | |||
163 | cdata = (struct client_data*)arg; | ||
164 | |||
165 | if ((count = usbmuxd_get_device_list(&dev_list)) < 0) { | ||
166 | printf("Connecting to usbmuxd failed, terminating.\n"); | ||
167 | free(dev_list); | ||
168 | return NULL; | ||
169 | } | ||
170 | |||
171 | fprintf(stdout, "Number of available devices == %d\n", count); | ||
172 | |||
173 | if (dev_list == NULL || dev_list[0].handle == 0) { | ||
174 | printf("No connected device found, terminating.\n"); | ||
175 | free(dev_list); | ||
176 | return NULL; | ||
177 | } | ||
178 | |||
179 | fprintf(stdout, "Requesting connecion to device handle == %d (serial: %s), port %d\n", dev_list[0].handle, dev_list[0].uuid, device_port); | ||
180 | |||
181 | cdata->sfd = usbmuxd_connect(dev_list[0].handle, device_port); | ||
182 | free(dev_list); | ||
183 | if (cdata->sfd < 0) { | ||
184 | fprintf(stderr, "Error connecting to device!\n"); | ||
185 | } else { | ||
186 | cdata->stop_ctos = 0; | ||
187 | pthread_create(&ctos, NULL, run_ctos_loop, cdata); | ||
188 | pthread_join(ctos, NULL); | ||
189 | } | ||
190 | |||
191 | if (cdata->fd > 0) { | ||
192 | close(cdata->fd); | ||
193 | } | ||
194 | if (cdata->sfd > 0) { | ||
195 | close(cdata->sfd); | ||
196 | } | ||
197 | |||
198 | return NULL; | ||
199 | } | ||
200 | |||
201 | int main(int argc, char **argv) | ||
202 | { | ||
203 | int mysock = -1; | ||
204 | |||
205 | if (argc != 3) { | ||
206 | printf("usage: %s LOCAL_TCP_PORT DEVICE_TCP_PORT\n", argv[0]); | ||
207 | return 0; | ||
208 | } | ||
209 | |||
210 | listen_port = atoi(argv[1]); | ||
211 | device_port = atoi(argv[2]); | ||
212 | |||
213 | if (!listen_port) { | ||
214 | fprintf(stderr, "Invalid listen_port specified!\n"); | ||
215 | return -EINVAL; | ||
216 | } | ||
217 | |||
218 | if (!device_port) { | ||
219 | fprintf(stderr, "Invalid device_port specified!\n"); | ||
220 | return -EINVAL; | ||
221 | } | ||
222 | |||
223 | // first create the listening socket endpoint waiting for connections. | ||
224 | mysock = create_socket(listen_port); | ||
225 | if (mysock < 0) { | ||
226 | fprintf(stderr, "Error creating socket: %s\n", strerror(errno)); | ||
227 | return -errno; | ||
228 | } else { | ||
229 | pthread_t acceptor; | ||
230 | struct sockaddr_in c_addr; | ||
231 | socklen_t len = sizeof(struct sockaddr_in); | ||
232 | struct client_data cdata; | ||
233 | int c_sock; | ||
234 | while (1) { | ||
235 | printf("waiting for connection\n"); | ||
236 | c_sock = accept(mysock, (struct sockaddr*)&c_addr, &len); | ||
237 | if (c_sock) { | ||
238 | printf("accepted connection, fd = %d\n", c_sock); | ||
239 | cdata.fd = c_sock; | ||
240 | pthread_create(&acceptor, NULL, acceptor_thread, &cdata); | ||
241 | pthread_join(acceptor, NULL); | ||
242 | } else { | ||
243 | break; | ||
244 | } | ||
245 | } | ||
246 | close(c_sock); | ||
247 | close(mysock); | ||
248 | } | ||
249 | |||
250 | return 0; | ||
251 | } | ||