idevice_error_t idevice_connect(idevice_t device, uint16_t port, idevice_connection_t *connection) { if (!device) { return IDEVICE_E_INVALID_ARG; } if (device->conn_type == CONNECTION_USBMUXD) { int sfd = usbmuxd_connect((uint32_t)(long)device->conn_data, port); if (sfd < 0) { debug_info("ERROR: Connecting to usbmuxd failed: %d (%s)", sfd, strerror(-sfd)); return IDEVICE_E_UNKNOWN_ERROR; } idevice_connection_t new_connection = (idevice_connection_t)malloc(sizeof(struct idevice_connection_private)); new_connection->type = CONNECTION_USBMUXD; new_connection->data = (void*)(long)sfd; new_connection->ssl_data = NULL; idevice_get_udid(device, &new_connection->udid); *connection = new_connection; return IDEVICE_E_SUCCESS; } else { debug_info("Unknown connection type %d", device->conn_type); } return IDEVICE_E_UNKNOWN_ERROR; }
static void *acceptor_thread(void *arg) { struct client_data *cdata; usbmuxd_device_info_t *dev_list = NULL; #ifdef WIN32 HANDLE ctos = NULL; #else pthread_t ctos; #endif int count; if (!arg) { fprintf(stderr, "invalid client_data provided!\n"); return NULL; } 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; } 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; } 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; #ifdef WIN32 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); #endif } if (cdata->fd > 0) { close(cdata->fd); } if (cdata->sfd > 0) { close(cdata->sfd); } return NULL; }
/** * Set up a connection to the given device. * * @param device The device to connect to. * @param dst_port The destination port to connect to. * @param connection Pointer to an iphone_connection_t that will be filled * with the necessary data of the connection. * * @return IPHONE_E_SUCCESS if ok, otherwise an error code. */ iphone_error_t iphone_device_connect(iphone_device_t device, uint16_t dst_port, iphone_connection_t *connection) { if (!device) { return IPHONE_E_INVALID_ARG; } if (device->conn_type == CONNECTION_USBMUXD) { int sfd = usbmuxd_connect((uint32_t)(device->conn_data), dst_port); if (sfd < 0) { log_debug_msg("%s: ERROR: Connecting to usbmuxd failed: %d (%s)\n", __func__, sfd, strerror(-sfd)); return IPHONE_E_UNKNOWN_ERROR; } iphone_connection_t new_connection = (iphone_connection_t)malloc(sizeof(struct iphone_connection_int)); new_connection->type = CONNECTION_USBMUXD; new_connection->data = (void*)sfd; *connection = new_connection; return IPHONE_E_SUCCESS; } else { log_debug_msg("%s: Unknown connection type %d\n", __func__, device->conn_type); } return IPHONE_E_UNKNOWN_ERROR; }