int accept_tunnel_conn(struct mobiletunnel *t) { struct sockaddr_in addr; socklen_t addr_size = sizeof(addr); int src_sock = accept(t->sock, (struct sockaddr *)&addr, &addr_size); // could not accept client? if (src_sock < 0) { return 0; } FLPRINT("Got new client!\n"); if (t->conn_count >= TUNNEL_MAX_CONNS) { fprintf(stderr, "Cannot accept client: too many connections!\n"); // too many connections, reject this client close(src_sock); return 0; } unsigned int device_id = AMDeviceGetConnectionID(t->device); int dst_sock; // connect to device if (USBMuxConnectByPort(device_id, htons(t->dst_port), &dst_sock) != 0) { fprintf(stderr, "Device refused connection to port %d!\n", t->dst_port); // connection failed, reject this client close(src_sock); return 0; } FLPRINT("Forwarding connection to device...\n"); // register connection between source and remove sockets add_tunnel_conn(t, src_sock, dst_sock); return 1; }
void* THREADPROCATTR wait_for_device(void* arg) { int iphonePort = (int)(intptr_t)arg; int ret; int handle = -1; restore_dev_t restore_dev; while (1) { int new_sock; if (s_target_device == NULL) { Sleep(100); continue; } { struct sockaddr_in sockAddrin; socklen_t len = sizeof(sockAddrin); new_sock = accept(sock, (struct sockaddr*) &sockAddrin , &len); } if (new_sock == -1) { fprintf(stderr, "accept() error\n"); fflush(stderr); continue; } fprintf(stderr, "Info: New connection...\n"); fflush(stderr); if (muxConn == 0) { ret = AMDeviceConnect(s_target_device); fprintf(stderr, "AMDeviceConnect() = 0x%x\n", ret); fflush(stderr); if (ret == ERR_SUCCESS) { muxConn = AMDeviceGetConnectionID(s_target_device); } else if (ret == -402653144) { // means recovery mode .. I think muxconn_t mux_tmp = AMDeviceGetConnectionID(s_target_device); fprintf(stderr, "muxConnTmp = %X\n", mux_tmp); muxConn = mux_tmp; // restore_dev = AMRestoreModeDeviceCreate(0, mux_tmp, 0); // fprintf(stderr, "restore_dev = %p\n", restore_dev); // if (restore_dev != NULL) { // AMRestoreModeDeviceReboot(restore_dev); // Sleep(5 * 1000); // } } else if (ret == -402653083) { // after we call 'reboot', api host is down muxconn_t mux_tmp = AMDeviceGetConnectionID(s_target_device); fprintf(stderr, "muxConnTmp = %X\n", mux_tmp); muxConn = mux_tmp; } else { fprintf(stderr, "AMDeviceConnect = %i\n", ret); goto error_connect; } } fprintf(stderr, "Device connected\n"); fflush(stderr); ret = USBMuxConnectByPort(muxConn, htons(iphonePort), &handle); if (ret != ERR_SUCCESS) { fprintf(stderr, "USBMuxConnectByPort = %x, handle=%x\n", ret, handle); goto error_service; } fprintf(stderr, "USBMuxConnectByPort OK\n"); { conn_struc* connection1; conn_struc* connection2; int lpThreadId; int lpThreadId2; pthread_t thread1; pthread_t thread2; connection1 = (conn_struc*)malloc(sizeof(conn_struc)); if (!connection1) { fprintf(stderr, "Malloc failed!\n"); continue; } connection2 = (conn_struc*)malloc(sizeof(conn_struc)); if (!connection2) { fprintf(stderr, "Malloc failed!\n"); continue; } connection1->from_handle = new_sock; connection1->to_handle = handle; connection2->from_handle = handle; connection2->to_handle = new_sock; fprintf(stderr, "sock handle newsock:%d iphone:%d\n", new_sock, handle); lpThreadId = pthread_create(&thread1, NULL, conn_forwarding_thread, (void*)connection1); lpThreadId2 = pthread_create(&thread2, NULL, conn_forwarding_thread, (void*)connection2); pthread_detach(thread2); pthread_detach(thread1); Sleep(100); } fflush(stderr); continue; error_connect: fprintf(stderr, "Error: Device Connect\n"); AMDeviceDisconnect(s_target_device); Sleep(1000); fflush(stderr); continue; error_service: fprintf(stderr, "Error: Device Service\n"); AMDeviceDisconnect(s_target_device); Sleep(1000); fflush(stderr); continue; } return NULL; }