diff options
Diffstat (limited to 'iproxy.c')
| -rw-r--r-- | iproxy.c | 160 |
1 files changed, 26 insertions, 134 deletions
| @@ -31,8 +31,9 @@ | |||
| 31 | #include <errno.h> | 31 | #include <errno.h> |
| 32 | #include <arpa/inet.h> | 32 | #include <arpa/inet.h> |
| 33 | #include <pthread.h> | 33 | #include <pthread.h> |
| 34 | #include "usbmuxd.h" | 34 | #include <usbmuxd.h> |
| 35 | #include "sock_stuff.h" | 35 | #include "sock_stuff.h" |
| 36 | #include "libusbmuxd.h" | ||
| 36 | 37 | ||
| 37 | static uint16_t listen_port = 0; | 38 | static uint16_t listen_port = 0; |
| 38 | static uint16_t device_port = 0; | 39 | static uint16_t device_port = 0; |
| @@ -46,43 +47,6 @@ struct client_data { | |||
| 46 | volatile int stop_stoc; | 47 | volatile int stop_stoc; |
| 47 | }; | 48 | }; |
| 48 | 49 | ||
| 49 | int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result) | ||
| 50 | { | ||
| 51 | struct usbmuxd_result res; | ||
| 52 | int recv_len; | ||
| 53 | int i; | ||
| 54 | uint32_t rrr[5]; | ||
| 55 | |||
| 56 | if (!result) { | ||
| 57 | return -EINVAL; | ||
| 58 | } | ||
| 59 | |||
| 60 | if ((recv_len = recv_buf(sfd, &res, sizeof(res))) <= 0) { | ||
| 61 | perror("recv"); | ||
| 62 | return -errno; | ||
| 63 | } else { | ||
| 64 | memcpy(&rrr, &res, recv_len); | ||
| 65 | for (i = 0; i < recv_len/4; i++) { | ||
| 66 | fprintf(stderr, "%08x ", rrr[i]); | ||
| 67 | } | ||
| 68 | fprintf(stderr, "\n"); | ||
| 69 | if ((recv_len == sizeof(res)) | ||
| 70 | && (res.header.length == recv_len) | ||
| 71 | && (res.header.reserved == 0) | ||
| 72 | && (res.header.type == USBMUXD_RESULT) | ||
| 73 | ) { | ||
| 74 | *result = res.result; | ||
| 75 | if (res.header.tag == tag) { | ||
| 76 | return 1; | ||
| 77 | } else { | ||
| 78 | return 0; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | return -1; | ||
| 84 | } | ||
| 85 | |||
| 86 | void *run_stoc_loop(void *arg) | 50 | void *run_stoc_loop(void *arg) |
| 87 | { | 51 | { |
| 88 | struct client_data *cdata = (struct client_data*)arg; | 52 | struct client_data *cdata = (struct client_data*)arg; |
| @@ -178,13 +142,7 @@ void *run_ctos_loop(void *arg) | |||
| 178 | void *acceptor_thread(void *arg) | 142 | void *acceptor_thread(void *arg) |
| 179 | { | 143 | { |
| 180 | struct client_data *cdata; | 144 | struct client_data *cdata; |
| 181 | int recv_len = 0; | 145 | usbmuxd_device_t *dev_list = NULL; |
| 182 | int scan_done; | ||
| 183 | int connected; | ||
| 184 | uint32_t pktlen; | ||
| 185 | unsigned char *buf; | ||
| 186 | struct usbmuxd_scan_request scan; | ||
| 187 | struct am_device_info device_info; | ||
| 188 | pthread_t ctos; | 146 | pthread_t ctos; |
| 189 | 147 | ||
| 190 | if (!arg) { | 148 | if (!arg) { |
| @@ -194,102 +152,36 @@ void *acceptor_thread(void *arg) | |||
| 194 | 152 | ||
| 195 | cdata = (struct client_data*)arg; | 153 | cdata = (struct client_data*)arg; |
| 196 | 154 | ||
| 197 | cdata->sfd = connect_unix_socket(USBMUXD_SOCKET_FILE); | 155 | if (usbmuxd_scan(&dev_list) != 0) { |
| 198 | if (cdata->sfd < 0) { | 156 | printf("Connecting to usbmuxd failed, terminating.\n"); |
| 199 | printf("error opening socket, terminating.\n"); | 157 | free(dev_list); |
| 200 | return NULL; | 158 | return NULL; |
| 201 | } | 159 | } |
| 202 | 160 | ||
| 203 | // send scan | 161 | if (!dev_list || dev_list[0].device_id == 0) { |
| 204 | scan.header.length = sizeof(struct usbmuxd_scan_request); | 162 | printf("No connected device found, terminating.\n"); |
| 205 | scan.header.reserved = 0; | 163 | free(dev_list); |
| 206 | scan.header.type = USBMUXD_SCAN; | 164 | return NULL; |
| 207 | scan.header.tag = 2; | 165 | } |
| 208 | |||
| 209 | scan_done = 0; | ||
| 210 | connected = 0; | ||
| 211 | |||
| 212 | fprintf(stdout, "sending scan packet\n"); | ||
| 213 | if (send(cdata->sfd, &scan, scan.header.length, 0) == scan.header.length) { | ||
| 214 | uint32_t res = -1; | ||
| 215 | // get response | ||
| 216 | if (usbmuxd_get_result(cdata->sfd, scan.header.tag, &res) && (res==0)) { | ||
| 217 | fprintf(stdout, "Got response to scan request!\n"); | ||
| 218 | scan_done = 1; | ||
| 219 | } else { | ||
| 220 | fprintf(stderr, "Did not get response to scan request (with result=0)...\n"); | ||
| 221 | close(cdata->sfd); | ||
| 222 | cdata->sfd = -1; | ||
| 223 | return NULL; | ||
| 224 | } | ||
| 225 | |||
| 226 | device_info.device_id = 0; | ||
| 227 | |||
| 228 | if (scan_done) { | ||
| 229 | // get all devices | ||
| 230 | while (1) { | ||
| 231 | if (recv_buf_timeout(cdata->sfd, &pktlen, 4, MSG_PEEK, 1000) == 4) { | ||
| 232 | buf = (unsigned char*)malloc(pktlen); | ||
| 233 | if (!buf) { | ||
| 234 | exit(-ENOMEM); | ||
| 235 | } | ||
| 236 | recv_len = recv_buf(cdata->sfd, buf, pktlen); | ||
| 237 | if (recv_len < pktlen) { | ||
| 238 | fprintf(stdout, "received less data than specified in header!\n"); | ||
| 239 | } | ||
| 240 | fprintf(stdout, "Received device data\n"); | ||
| 241 | //log_debug_buffer(stdout, (char*)buf, pktlen); | ||
| 242 | memcpy(&device_info, buf + sizeof(struct usbmuxd_header), sizeof(device_info)); | ||
| 243 | free(buf); | ||
| 244 | } else { | ||
| 245 | // we _should_ have all of them now. | ||
| 246 | // or perhaps an error occured. | ||
| 247 | break; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | if (device_info.device_id > 0) { | ||
| 253 | struct usbmuxd_connect_request c_req; | ||
| 254 | |||
| 255 | fprintf(stdout, "Requesting connecion to device %d port %d\n", device_info.device_id, device_port); | ||
| 256 | 166 | ||
| 257 | // try to connect to last device found | 167 | fprintf(stdout, "Requesting connecion to device %d port %d\n", dev_list[0].device_id, device_port); |
| 258 | c_req.header.length = sizeof(c_req); | ||
| 259 | c_req.header.reserved = 0; | ||
| 260 | c_req.header.type = USBMUXD_CONNECT; | ||
| 261 | c_req.header.tag = 3; | ||
| 262 | c_req.device_id = device_info.device_id; | ||
| 263 | c_req.tcp_dport = htons(device_port); | ||
| 264 | c_req.reserved = 0; | ||
| 265 | 168 | ||
| 266 | if (send_buf(cdata->sfd, &c_req, sizeof(c_req)) < 0) { | 169 | cdata->sfd = usbmuxd_connect(dev_list[0].device_id, device_port); |
| 267 | perror("send"); | 170 | free(dev_list); |
| 268 | } else { | 171 | if (cdata->sfd < 0) { |
| 269 | // read ACK | 172 | fprintf(stderr, "Error connecting to device!\n"); |
| 270 | res = -1; | 173 | } else { |
| 271 | fprintf(stdout, "Reading connect result...\n"); | 174 | cdata->stop_ctos = 0; |
| 272 | if (usbmuxd_get_result(cdata->sfd, c_req.header.tag, &res)) { | 175 | pthread_create(&ctos, NULL, run_ctos_loop, cdata); |
| 273 | if (res == 0) { | 176 | pthread_join(ctos, NULL); |
| 274 | fprintf(stdout, "Connect success!\n"); | 177 | } |
| 275 | connected = 1; | ||
| 276 | } else { | ||
| 277 | fprintf(stderr, "Connect failed, Error code=%d\n", res); | ||
| 278 | } | ||
| 279 | } | ||
| 280 | } | ||
| 281 | } | ||
| 282 | 178 | ||
| 283 | if (connected) { | 179 | if (cdata->fd > 0) { |
| 284 | cdata->stop_ctos = 0; | 180 | close(cdata->fd); |
| 285 | pthread_create(&ctos, NULL, run_ctos_loop, cdata); | 181 | } |
| 286 | pthread_join(ctos, NULL); | 182 | if (cdata->sfd > 0) { |
| 287 | } else { | 183 | close(cdata->sfd); |
| 288 | fprintf(stderr, "Error connecting to device!\n"); | ||
| 289 | } | ||
| 290 | } | 184 | } |
| 291 | close(cdata->fd); | ||
| 292 | close(cdata->sfd); | ||
| 293 | 185 | ||
| 294 | return NULL; | 186 | return NULL; |
| 295 | } | 187 | } |
