diff options
| author | 2009-02-23 21:36:08 +0100 | |
|---|---|---|
| committer | 2009-02-23 21:36:08 +0100 | |
| commit | 471b064f182bf0770729546607b0aa920c46419b (patch) | |
| tree | 27ad358173733cb913e21462f9eab0461208400c | |
| parent | 2ae80d9dd4855b0dd3e0c74e2f6d03b2b6ecd7c0 (diff) | |
| download | usbmuxd-471b064f182bf0770729546607b0aa920c46419b.tar.gz usbmuxd-471b064f182bf0770729546607b0aa920c46419b.tar.bz2 | |
Small update. Now usbmuxd is only contacted when a client connects to
the local listen port. This allows re-connects afterwards.
| -rw-r--r-- | iproxy.c | 186 |
1 files changed, 150 insertions, 36 deletions
| @@ -36,9 +36,6 @@ | |||
| 36 | 36 | ||
| 37 | #define SOCKET_FILE "/var/run/usbmuxd" | 37 | #define SOCKET_FILE "/var/run/usbmuxd" |
| 38 | 38 | ||
| 39 | volatile int stop_ctos = 0; | ||
| 40 | volatile int stop_stoc = 0; | ||
| 41 | |||
| 42 | static uint16_t listen_port = 0; | 39 | static uint16_t listen_port = 0; |
| 43 | static uint16_t device_port = 0; | 40 | static uint16_t device_port = 0; |
| 44 | 41 | ||
| @@ -47,6 +44,8 @@ pthread_mutex_t smutex = PTHREAD_MUTEX_INITIALIZER; | |||
| 47 | struct client_data { | 44 | struct client_data { |
| 48 | int fd; | 45 | int fd; |
| 49 | int sfd; | 46 | int sfd; |
| 47 | volatile int stop_ctos; | ||
| 48 | volatile int stop_stoc; | ||
| 50 | }; | 49 | }; |
| 51 | 50 | ||
| 52 | int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result) | 51 | int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result) |
| @@ -95,7 +94,7 @@ void *run_stoc_loop(void *arg) | |||
| 95 | 94 | ||
| 96 | printf("%s: fd = %d\n", __func__, cdata->fd); | 95 | printf("%s: fd = %d\n", __func__, cdata->fd); |
| 97 | 96 | ||
| 98 | while (!stop_stoc && cdata->fd>0 && cdata->sfd>0) { | 97 | while (!cdata->stop_stoc && cdata->fd>0 && cdata->sfd>0) { |
| 99 | recv_len = recv_buf_timeout(cdata->sfd, buffer, sizeof(buffer), 0, 5000); | 98 | recv_len = recv_buf_timeout(cdata->sfd, buffer, sizeof(buffer), 0, 5000); |
| 100 | if (recv_len <= 0) { | 99 | if (recv_len <= 0) { |
| 101 | if (recv_len == 0) { | 100 | if (recv_len == 0) { |
| @@ -124,7 +123,7 @@ void *run_stoc_loop(void *arg) | |||
| 124 | } | 123 | } |
| 125 | close(cdata->fd); | 124 | close(cdata->fd); |
| 126 | cdata->fd = -1; | 125 | cdata->fd = -1; |
| 127 | stop_ctos = 1; | 126 | cdata->stop_ctos = 1; |
| 128 | 127 | ||
| 129 | return NULL; | 128 | return NULL; |
| 130 | } | 129 | } |
| @@ -139,10 +138,10 @@ void *run_ctos_loop(void *arg) | |||
| 139 | 138 | ||
| 140 | printf("%s: fd = %d\n", __func__, cdata->fd); | 139 | printf("%s: fd = %d\n", __func__, cdata->fd); |
| 141 | 140 | ||
| 142 | stop_stoc = 0; | 141 | cdata->stop_stoc = 0; |
| 143 | pthread_create(&stoc, NULL, run_stoc_loop, cdata); | 142 | pthread_create(&stoc, NULL, run_stoc_loop, cdata); |
| 144 | 143 | ||
| 145 | while (!stop_ctos && cdata->fd>0 && cdata->sfd>0) { | 144 | while (!cdata->stop_ctos && cdata->fd>0 && cdata->sfd>0) { |
| 146 | recv_len = recv_buf_timeout(cdata->fd, buffer, sizeof(buffer), 0, 5000); | 145 | recv_len = recv_buf_timeout(cdata->fd, buffer, sizeof(buffer), 0, 5000); |
| 147 | if (recv_len <= 0) { | 146 | if (recv_len <= 0) { |
| 148 | if (recv_len == 0) { | 147 | if (recv_len == 0) { |
| @@ -171,15 +170,16 @@ void *run_ctos_loop(void *arg) | |||
| 171 | } | 170 | } |
| 172 | close(cdata->fd); | 171 | close(cdata->fd); |
| 173 | cdata->fd = -1; | 172 | cdata->fd = -1; |
| 174 | stop_stoc = 1; | 173 | cdata->stop_stoc = 1; |
| 175 | 174 | ||
| 176 | pthread_join(stoc, NULL); | 175 | pthread_join(stoc, NULL); |
| 177 | 176 | ||
| 178 | return NULL; | 177 | return NULL; |
| 179 | } | 178 | } |
| 180 | 179 | ||
| 181 | int main(int argc, char **argv) | 180 | void *acceptor_thread(void *arg) |
| 182 | { | 181 | { |
| 182 | struct client_data *cdata; | ||
| 183 | int recv_len = 0; | 183 | int recv_len = 0; |
| 184 | int hello_done; | 184 | int hello_done; |
| 185 | int connected; | 185 | int connected; |
| @@ -187,7 +187,117 @@ int main(int argc, char **argv) | |||
| 187 | unsigned char *buf; | 187 | unsigned char *buf; |
| 188 | struct usbmux_header hello; | 188 | struct usbmux_header hello; |
| 189 | struct usbmux_dev_info device_info; | 189 | struct usbmux_dev_info device_info; |
| 190 | int sfd = -1; | 190 | pthread_t ctos; |
| 191 | |||
| 192 | if (!arg) { | ||
| 193 | fprintf(stderr, "invalid client_data provided!\n"); | ||
| 194 | return NULL; | ||
| 195 | } | ||
| 196 | |||
| 197 | cdata = (struct client_data*)arg; | ||
| 198 | |||
| 199 | cdata->sfd = connect_unix_socket(SOCKET_FILE); | ||
| 200 | if (cdata->sfd < 0) { | ||
| 201 | printf("error opening socket, terminating.\n"); | ||
| 202 | return NULL; | ||
| 203 | } | ||
| 204 | |||
| 205 | // send hello | ||
| 206 | hello.length = sizeof(struct usbmux_header); | ||
| 207 | hello.reserved = 0; | ||
| 208 | hello.type = usbmux_hello; | ||
| 209 | hello.tag = 2; | ||
| 210 | |||
| 211 | hello_done = 0; | ||
| 212 | connected = 0; | ||
| 213 | |||
| 214 | fprintf(stdout, "sending Hello packet\n"); | ||
| 215 | if (send(cdata->sfd, &hello, hello.length, 0) == hello.length) { | ||
| 216 | uint32_t res = -1; | ||
| 217 | // get response | ||
| 218 | if (usbmuxd_get_result(cdata->sfd, hello.tag, &res) && (res==0)) { | ||
| 219 | fprintf(stdout, "Got Hello Response!\n"); | ||
| 220 | hello_done = 1; | ||
| 221 | } else { | ||
| 222 | fprintf(stderr, "Did not get Hello response (with result=0)...\n"); | ||
| 223 | close(cdata->sfd); | ||
| 224 | cdata->sfd = -1; | ||
| 225 | return NULL; | ||
| 226 | } | ||
| 227 | |||
| 228 | device_info.device_id = 0; | ||
| 229 | |||
| 230 | if (hello_done) { | ||
| 231 | // get all devices | ||
| 232 | while (1) { | ||
| 233 | if (recv_buf_timeout(cdata->sfd, &pktlen, 4, MSG_PEEK, 1000) == 4) { | ||
| 234 | buf = (unsigned char*)malloc(pktlen); | ||
| 235 | if (!buf) { | ||
| 236 | exit(-ENOMEM); | ||
| 237 | } | ||
| 238 | recv_len = recv_buf(cdata->sfd, buf, pktlen); | ||
| 239 | if (recv_len < pktlen) { | ||
| 240 | fprintf(stdout, "received less data than specified in header!\n"); | ||
| 241 | } | ||
| 242 | fprintf(stdout, "Received device data\n"); | ||
| 243 | //log_debug_buffer(stdout, (char*)buf, pktlen); | ||
| 244 | memcpy(&device_info, buf + sizeof(struct usbmux_header), sizeof(device_info)); | ||
| 245 | free(buf); | ||
| 246 | } else { | ||
| 247 | // we _should_ have all of them now. | ||
| 248 | // or perhaps an error occured. | ||
| 249 | break; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | if (device_info.device_id > 0) { | ||
| 255 | struct usbmux_connect_request c_req; | ||
| 256 | |||
| 257 | fprintf(stdout, "Requesting connecion to device %d port %d\n", device_info.device_id, device_port); | ||
| 258 | |||
| 259 | // try to connect to last device found | ||
| 260 | c_req.header.length = sizeof(c_req); | ||
| 261 | c_req.header.reserved = 0; | ||
| 262 | c_req.header.type = usbmux_connect; | ||
| 263 | c_req.header.tag = 3; | ||
| 264 | c_req.device_id = device_info.device_id; | ||
| 265 | c_req.port = htons(device_port); | ||
| 266 | c_req.reserved = 0; | ||
| 267 | |||
| 268 | if (send_buf(cdata->sfd, &c_req, sizeof(c_req)) < 0) { | ||
| 269 | perror("send"); | ||
| 270 | } else { | ||
| 271 | // read ACK | ||
| 272 | res = -1; | ||
| 273 | fprintf(stdout, "Reading connect result...\n"); | ||
| 274 | if (usbmuxd_get_result(cdata->sfd, c_req.header.tag, &res)) { | ||
| 275 | if (res == 0) { | ||
| 276 | fprintf(stdout, "Connect success!\n"); | ||
| 277 | connected = 1; | ||
| 278 | } else { | ||
| 279 | fprintf(stderr, "Connect failed, Error code=%d\n", res); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | if (connected) { | ||
| 286 | cdata->stop_ctos = 0; | ||
| 287 | pthread_create(&ctos, NULL, run_ctos_loop, cdata); | ||
| 288 | pthread_join(ctos, NULL); | ||
| 289 | } else { | ||
| 290 | fprintf(stderr, "Error connecting to device!\n"); | ||
| 291 | } | ||
| 292 | } | ||
| 293 | close(cdata->sfd); | ||
| 294 | |||
| 295 | return NULL; | ||
| 296 | } | ||
| 297 | |||
| 298 | int main(int argc, char **argv) | ||
| 299 | { | ||
| 300 | int mysock = -1; | ||
| 191 | 301 | ||
| 192 | if (argc != 3) { | 302 | if (argc != 3) { |
| 193 | printf("usage: %s LOCAL_PORT DEVICE_PORT\n", argv[0]); | 303 | printf("usage: %s LOCAL_PORT DEVICE_PORT\n", argv[0]); |
| @@ -207,6 +317,34 @@ int main(int argc, char **argv) | |||
| 207 | return -EINVAL; | 317 | return -EINVAL; |
| 208 | } | 318 | } |
| 209 | 319 | ||
| 320 | // first create the listening socket endpoint waiting for connections. | ||
| 321 | mysock = create_socket(listen_port); | ||
| 322 | if (mysock < 0) { | ||
| 323 | fprintf(stderr, "Error creating socket: %s\n", strerror(errno)); | ||
| 324 | return -errno; | ||
| 325 | } else { | ||
| 326 | pthread_t acceptor; | ||
| 327 | struct sockaddr_in c_addr; | ||
| 328 | socklen_t len = sizeof(struct sockaddr_in); | ||
| 329 | struct client_data cdata; | ||
| 330 | int c_sock; | ||
| 331 | while (1) { | ||
| 332 | printf("waiting for connection\n"); | ||
| 333 | c_sock = accept(mysock, (struct sockaddr*)&c_addr, &len); | ||
| 334 | if (c_sock) { | ||
| 335 | printf("accepted connection, fd = %d\n", c_sock); | ||
| 336 | cdata.fd = c_sock; | ||
| 337 | pthread_create(&acceptor, NULL, acceptor_thread, &cdata); | ||
| 338 | pthread_join(acceptor, NULL); | ||
| 339 | } else { | ||
| 340 | break; | ||
| 341 | } | ||
| 342 | } | ||
| 343 | close(c_sock); | ||
| 344 | close(mysock); | ||
| 345 | } | ||
| 346 | |||
| 347 | /* | ||
| 210 | sfd = connect_unix_socket(SOCKET_FILE); | 348 | sfd = connect_unix_socket(SOCKET_FILE); |
| 211 | if (sfd < 0) { | 349 | if (sfd < 0) { |
| 212 | printf("error opening socket, terminating.\n"); | 350 | printf("error opening socket, terminating.\n"); |
| @@ -293,37 +431,13 @@ int main(int argc, char **argv) | |||
| 293 | } | 431 | } |
| 294 | 432 | ||
| 295 | if (connected) { | 433 | if (connected) { |
| 296 | int mysock = create_socket(listen_port); | 434 | // |
| 297 | if (mysock < 0) { | ||
| 298 | fprintf(stderr, "Error creating socket: %s\n", strerror(errno)); | ||
| 299 | } else { | ||
| 300 | pthread_t ctos; | ||
| 301 | struct sockaddr_in c_addr; | ||
| 302 | socklen_t len = sizeof(struct sockaddr_in); | ||
| 303 | struct client_data cdata; | ||
| 304 | int c_sock; | ||
| 305 | while (1) { | ||
| 306 | printf("waiting for connection\n"); | ||
| 307 | c_sock = accept(mysock, (struct sockaddr*)&c_addr, &len); | ||
| 308 | if (c_sock) { | ||
| 309 | printf("accepted connection, fd = %d\n", c_sock); | ||
| 310 | cdata.fd = c_sock; | ||
| 311 | cdata.sfd = sfd; | ||
| 312 | stop_ctos = 0; | ||
| 313 | pthread_create(&ctos, NULL, run_ctos_loop, &cdata); | ||
| 314 | pthread_join(ctos, NULL); | ||
| 315 | } else { | ||
| 316 | break; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | close(c_sock); | ||
| 320 | close(mysock); | ||
| 321 | } | ||
| 322 | } else { | 435 | } else { |
| 323 | fprintf(stderr, "No attached device found?!\n"); | 436 | fprintf(stderr, "No attached device found?!\n"); |
| 324 | } | 437 | } |
| 325 | } | 438 | } |
| 326 | close(sfd); | 439 | close(sfd); |
| 440 | */ | ||
| 327 | 441 | ||
| 328 | return 0; | 442 | return 0; |
| 329 | } | 443 | } |
