summaryrefslogtreecommitdiffstats
path: root/daemon/device.c
diff options
context:
space:
mode:
Diffstat (limited to 'daemon/device.c')
-rw-r--r--daemon/device.c751
1 files changed, 751 insertions, 0 deletions
diff --git a/daemon/device.c b/daemon/device.c
new file mode 100644
index 0000000..3a5883c
--- /dev/null
+++ b/daemon/device.c
@@ -0,0 +1,751 @@
1/*
2 usbmuxd - iPhone/iPod Touch USB multiplex server daemon
3
4Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 2 or version 3.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19*/
20
21#define _BSD_SOURCE
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include <sys/time.h>
28#include <netinet/in.h>
29#include <netinet/tcp.h>
30#include <stdlib.h>
31#include <string.h>
32#include <stdint.h>
33#include <inttypes.h>
34#include "device.h"
35#include "client.h"
36#include "usb.h"
37#include "log.h"
38
39int next_device_id;
40
41#define DEV_PKTBUF_SIZE 65536
42
43#define CONN_INBUF_SIZE 262144
44#define CONN_OUTBUF_SIZE 65536
45
46#define ACK_TIMEOUT 30
47
48enum mux_protocol {
49 MUX_PROTO_VERSION = 0,
50 MUX_PROTO_TCP = IPPROTO_TCP,
51};
52
53enum mux_dev_state {
54 MUXDEV_INIT, // sent version packet
55 MUXDEV_ACTIVE, // received version packet, active
56 MUXDEV_DEAD // dead
57};
58
59enum mux_conn_state {
60 CONN_CONNECTING, // SYN
61 CONN_CONNECTED, // SYN/SYNACK/ACK -> active
62 CONN_REFUSED, // RST received during SYN
63 CONN_DYING, // RST received
64 CONN_DEAD // being freed; used to prevent infinite recursion between client<->device freeing
65};
66
67struct mux_header
68{
69 uint32_t protocol;
70 uint32_t length;
71};
72
73struct version_header
74{
75 uint32_t major;
76 uint32_t minor;
77 uint32_t padding;
78};
79
80struct mux_device;
81
82#define CONN_ACK_PENDING 1
83
84struct mux_connection
85{
86 struct mux_device *dev;
87 struct mux_client *client;
88 enum mux_conn_state state;
89 uint16_t sport, dport;
90 uint32_t tx_seq, tx_ack, tx_acked, tx_win;
91 uint32_t rx_seq, rx_recvd, rx_ack, rx_win;
92 int max_payload;
93 int sendable;
94 int flags;
95 unsigned char *ib_buf;
96 int ib_size;
97 int ib_capacity;
98 unsigned char *ob_buf;
99 int ob_capacity;
100 short events;
101 uint64_t last_ack_time;
102};
103
104struct mux_device
105{
106 struct usb_device *usbdev;
107 int id;
108 enum mux_dev_state state;
109 struct collection connections;
110 uint16_t next_sport;
111 unsigned char *pktbuf;
112 int pktlen;
113};
114
115static struct collection device_list;
116
117uint64_t mstime64(void)
118{
119 struct timeval tv;
120 gettimeofday(&tv, NULL);
121 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
122}
123
124static int get_next_device_id(void)
125{
126 while(1) {
127 int ok = 1;
128 FOREACH(struct mux_device *dev, &device_list) {
129 if(dev->id == next_device_id) {
130 next_device_id++;
131 ok = 0;
132 break;
133 }
134 } ENDFOREACH
135 if(ok)
136 return next_device_id++;
137 }
138}
139
140static int send_packet(struct mux_device *dev, enum mux_protocol proto, void *header, const void *data, int length)
141{
142 unsigned char *buffer;
143 int hdrlen;
144 int res;
145
146 switch(proto) {
147 case MUX_PROTO_VERSION:
148 hdrlen = sizeof(struct version_header);
149 break;
150 case MUX_PROTO_TCP:
151 hdrlen = sizeof(struct tcphdr);
152 break;
153 default:
154 usbmuxd_log(LL_ERROR, "Invalid protocol %d for outgoing packet (dev %d hdr %p data %p len %d)", proto, dev->id, header, data, length);
155 return -1;
156 }
157 usbmuxd_log(LL_SPEW, "send_packet(%d, 0x%x, %p, %p, %d)", dev->id, proto, header, data, length);
158
159 int total = sizeof(struct mux_header) + hdrlen + length;
160
161 if(total > USB_MTU) {
162 usbmuxd_log(LL_ERROR, "Tried to send packet larger than USB MTU (hdr %d data %d total %d) to device %d", hdrlen, length, total, dev->id);
163 return -1;
164 }
165
166 buffer = malloc(total);
167 struct mux_header *mhdr = (struct mux_header *)buffer;
168 mhdr->protocol = htonl(proto);
169 mhdr->length = htonl(total);;
170 memcpy(buffer + sizeof(struct mux_header), header, hdrlen);
171 if(data && length)
172 memcpy(buffer + sizeof(struct mux_header) + hdrlen, data, length);
173
174 if((res = usb_send(dev->usbdev, buffer, total)) < 0) {
175 usbmuxd_log(LL_ERROR, "usb_send failed while sending packet (len %d) to device %d: %d", total, dev->id, res);
176 free(buffer);
177 return res;
178 }
179 return total;
180}
181
182static uint16_t find_sport(struct mux_device *dev)
183{
184 if(collection_count(&dev->connections) >= 65535)
185 return 0; //insanity
186
187 while(1) {
188 int ok = 1;
189 FOREACH(struct mux_connection *conn, &dev->connections) {
190 if(dev->next_sport == conn->sport) {
191 dev->next_sport++;
192 ok = 0;
193 break;
194 }
195 } ENDFOREACH
196 if(ok)
197 return dev->next_sport++;
198 }
199}
200
201static int send_anon_rst(struct mux_device *dev, uint16_t sport, uint16_t dport, uint32_t ack)
202{
203 struct tcphdr th;
204 memset(&th, 0, sizeof(th));
205 th.th_sport = htons(sport);
206 th.th_dport = htons(dport);
207 th.th_ack = htonl(ack);
208 th.th_flags = TH_RST;
209 th.th_off = sizeof(th) / 4;
210
211 usbmuxd_log(LL_DEBUG, "[OUT] dev=%d sport=%d dport=%d flags=0x%x", dev->id, sport, dport, th.th_flags);
212
213 int res = send_packet(dev, MUX_PROTO_TCP, &th, NULL, 0);
214 return res;
215}
216
217static int send_tcp(struct mux_connection *conn, uint8_t flags, const unsigned char *data, int length)
218{
219 struct tcphdr th;
220 memset(&th, 0, sizeof(th));
221 th.th_sport = htons(conn->sport);
222 th.th_dport = htons(conn->dport);
223 th.th_seq = htonl(conn->tx_seq);
224 th.th_ack = htonl(conn->tx_ack);
225 th.th_flags = flags;
226 th.th_off = sizeof(th) / 4;
227 th.th_win = htons(conn->tx_win >> 8);
228
229 usbmuxd_log(LL_DEBUG, "[OUT] dev=%d sport=%d dport=%d seq=%d ack=%d flags=0x%x window=%d[%d] len=%d",
230 conn->dev->id, conn->sport, conn->dport, conn->tx_seq, conn->tx_ack, flags, conn->tx_win, conn->tx_win >> 8, length);
231
232 int res = send_packet(conn->dev, MUX_PROTO_TCP, &th, data, length);
233 if(res >= 0) {
234 conn->tx_acked = conn->tx_ack;
235 conn->last_ack_time = mstime64();
236 conn->flags &= ~CONN_ACK_PENDING;
237 }
238 return res;
239}
240
241static void connection_teardown(struct mux_connection *conn)
242{
243 int res;
244 if(conn->state == CONN_DEAD)
245 return;
246 usbmuxd_log(LL_DEBUG, "connection_teardown dev %d sport %d dport %d", conn->dev->id, conn->sport, conn->dport);
247 if(conn->dev->state != MUXDEV_DEAD && conn->state != CONN_DYING && conn->state != CONN_REFUSED) {
248 res = send_tcp(conn, TH_RST, NULL, 0);
249 if(res < 0)
250 usbmuxd_log(LL_ERROR, "Error sending TCP RST to device %d (%d->%d)", conn->dev->id, conn->sport, conn->dport);
251 }
252 if(conn->client) {
253 if(conn->state == CONN_REFUSED || conn->state == CONN_CONNECTING) {
254 client_notify_connect(conn->client, RESULT_CONNREFUSED);
255 } else {
256 conn->state = CONN_DEAD;
257 client_close(conn->client);
258 }
259 }
260 if(conn->ib_buf)
261 free(conn->ib_buf);
262 if(conn->ob_buf)
263 free(conn->ob_buf);
264 collection_remove(&conn->dev->connections, conn);
265 free(conn);
266}
267
268int device_start_connect(int device_id, uint16_t dport, struct mux_client *client)
269{
270 struct mux_device *dev = NULL;
271 FOREACH(struct mux_device *cdev, &device_list) {
272 if(cdev->id == device_id) {
273 dev = cdev;
274 break;
275 }
276 } ENDFOREACH
277 if(!dev) {
278 usbmuxd_log(LL_WARNING, "Attempted to connect to nonexistent device %d", device_id);
279 return -RESULT_BADDEV;
280 }
281
282 uint16_t sport = find_sport(dev);
283 if(!sport) {
284 usbmuxd_log(LL_WARNING, "Unable to allocate port for device %d", device_id);
285 return -RESULT_BADDEV;
286 }
287
288 struct mux_connection *conn;
289 conn = malloc(sizeof(struct mux_connection));
290 memset(conn, 0, sizeof(struct mux_connection));
291
292 conn->dev = dev;
293 conn->client = client;
294 conn->state = CONN_CONNECTING;
295 conn->sport = sport;
296 conn->dport = dport;
297 conn->tx_seq = 0;
298 conn->tx_ack = 0;
299 conn->tx_acked = 0;
300 conn->tx_win = 131072;
301 conn->rx_recvd = 0;
302 conn->flags = 0;
303 conn->max_payload = USB_MTU - sizeof(struct mux_header) - sizeof(struct tcphdr);
304
305 conn->ob_buf = malloc(CONN_OUTBUF_SIZE);
306 conn->ob_capacity = CONN_OUTBUF_SIZE;
307 conn->ib_buf = malloc(CONN_INBUF_SIZE);
308 conn->ib_capacity = CONN_INBUF_SIZE;
309 conn->ib_size = 0;
310
311 int res;
312
313 res = send_tcp(conn, TH_SYN, NULL, 0);
314 if(res < 0) {
315 usbmuxd_log(LL_ERROR, "Error sending TCP SYN to device %d (%d->%d)", dev->id, sport, dport);
316 free(conn);
317 return -RESULT_CONNREFUSED; //bleh
318 }
319 collection_add(&dev->connections, conn);
320 return 0;
321}
322
323static void update_connection(struct mux_connection *conn)
324{
325 conn->sendable = conn->rx_win - (conn->tx_seq - conn->rx_ack);
326
327 if(conn->sendable > conn->ob_capacity)
328 conn->sendable = conn->ob_capacity;
329 if(conn->sendable > conn->max_payload)
330 conn->sendable = conn->max_payload;
331
332 if(conn->sendable > 0)
333 conn->events |= POLLIN;
334 else
335 conn->events &= ~POLLIN;
336
337 if(conn->ib_size)
338 conn->events |= POLLOUT;
339 else
340 conn->events &= ~POLLOUT;
341
342 if(conn->tx_acked != conn->tx_ack)
343 conn->flags |= CONN_ACK_PENDING;
344 else
345 conn->flags &= ~CONN_ACK_PENDING;
346
347 usbmuxd_log(LL_SPEW, "update_connection: sendable %d, events %d, flags %d", conn->sendable, conn->events, conn->flags);
348 client_set_events(conn->client, conn->events);
349}
350
351void device_client_process(int device_id, struct mux_client *client, short events)
352{
353 struct mux_connection *conn = NULL;
354 FOREACH(struct mux_device *dev, &device_list) {
355 if(dev->id == device_id) {
356 FOREACH(struct mux_connection *lconn, &dev->connections) {
357 if(lconn->client == client) {
358 conn = lconn;
359 break;
360 }
361 } ENDFOREACH
362 break;
363 }
364 } ENDFOREACH
365
366 if(!conn) {
367 usbmuxd_log(LL_WARNING, "Could not find connection for device %d client %p", device_id, client);
368 return;
369 }
370 usbmuxd_log(LL_SPEW, "device_client_process (%d)", events);
371
372 int res;
373 int size;
374 if(events & POLLOUT) {
375 size = client_write(conn->client, conn->ib_buf, conn->ib_size);
376 if(size <= 0) {
377 usbmuxd_log(LL_DEBUG, "error writing to client (%d)", size);
378 connection_teardown(conn);
379 return;
380 }
381 conn->tx_ack += size;
382 if(size == conn->ib_size) {
383 conn->ib_size = 0;
384 } else {
385 conn->ib_size -= size;
386 memmove(conn->ib_buf, conn->ib_buf + size, conn->ib_size);
387 }
388 }
389 if(events & POLLIN) {
390 size = client_read(conn->client, conn->ob_buf, conn->sendable);
391 if(size <= 0) {
392 usbmuxd_log(LL_DEBUG, "error reading from client (%d)", size);
393 connection_teardown(conn);
394 return;
395 }
396 res = send_tcp(conn, TH_ACK, conn->ob_buf, size);
397 if(res < 0) {
398 connection_teardown(conn);
399 return;
400 }
401 conn->tx_seq += size;
402 }
403
404 update_connection(conn);
405}
406
407static void connection_device_input(struct mux_connection *conn, unsigned char *payload, int payload_length)
408{
409 if((conn->ib_size + payload_length) > conn->ib_capacity) {
410 usbmuxd_log(LL_ERROR, "Input buffer overflow on device %d connection %d->%d (space=%d, payload=%d)", conn->dev->id, conn->sport, conn->dport, conn->ib_capacity-conn->ib_size, payload_length);
411 connection_teardown(conn);
412 return;
413 }
414 memcpy(conn->ib_buf + conn->ib_size, payload, payload_length);
415 conn->ib_size += payload_length;
416 conn->rx_recvd += payload_length;
417 update_connection(conn);
418}
419
420void device_abort_connect(int device_id, struct mux_client *client)
421{
422 FOREACH(struct mux_device *dev, &device_list) {
423 if(dev->id == device_id) {
424 FOREACH(struct mux_connection *conn, &dev->connections) {
425 if(conn->client == client) {
426 connection_teardown(conn);
427 return;
428 }
429 } ENDFOREACH
430 usbmuxd_log(LL_WARNING, "Attempted to abort for nonexistent connection for device %d", device_id);
431 return;
432 }
433 } ENDFOREACH
434 usbmuxd_log(LL_WARNING, "Attempted to abort connection for nonexistent device %d", device_id);
435}
436
437static void device_version_input(struct mux_device *dev, struct version_header *vh)
438{
439 if(dev->state != MUXDEV_INIT) {
440 usbmuxd_log(LL_WARNING, "Version packet from already initialized device %d", dev->id);
441 return;
442 }
443 vh->major = ntohl(vh->major);
444 vh->minor = ntohl(vh->minor);
445 if(vh->major != 1 || vh->minor != 0) {
446 usbmuxd_log(LL_ERROR, "Device %d has unknown version %d.%d\n", dev->id, vh->major, vh->minor);
447 collection_remove(&device_list, dev);
448 free(dev);
449 return;
450 }
451 usbmuxd_log(LL_NOTICE, "Connected to v%d.%d device %d on location 0x%x with serial number %s", vh->major, vh->minor, dev->id, usb_get_location(dev->usbdev), usb_get_serial(dev->usbdev));
452 dev->state = MUXDEV_ACTIVE;
453 collection_init(&dev->connections);
454 struct device_info info;
455 info.id = dev->id;
456 info.location = usb_get_location(dev->usbdev);
457 info.serial = usb_get_serial(dev->usbdev);
458 info.pid = usb_get_pid(dev->usbdev);
459 client_device_add(&info);
460}
461
462static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned char *payload, int payload_length)
463{
464 usbmuxd_log(LL_DEBUG, "[IN] dev=%d sport=%d dport=%d seq=%d ack=%d flags=0x%x window=%d[%d] len=%d",
465 dev->id, ntohs(th->th_sport), ntohs(th->th_dport), ntohl(th->th_seq), ntohl(th->th_ack), th->th_flags, ntohs(th->th_win) << 8, ntohs(th->th_win), payload_length);
466
467 uint16_t sport = ntohs(th->th_dport);
468 uint16_t dport = ntohs(th->th_sport);
469 struct mux_connection *conn = NULL;
470 FOREACH(struct mux_connection *lconn, &dev->connections) {
471 if(lconn->sport == sport && lconn->dport == dport) {
472 conn = lconn;
473 break;
474 }
475 } ENDFOREACH
476
477 if(!conn) {
478 usbmuxd_log(LL_WARNING, "No connection for device %d incoming packet %d->%d", dev->id, dport, sport);
479 if(!(th->th_flags & TH_RST)) {
480 if(send_anon_rst(dev, sport, dport, ntohl(th->th_seq)) < 0)
481 usbmuxd_log(LL_ERROR, "Error sending TCP RST to device %d (%d->%d)", conn->dev->id, sport, dport);
482 }
483 return;
484 }
485
486 conn->rx_seq = ntohl(th->th_seq);
487 conn->rx_ack = ntohl(th->th_ack);
488 conn->rx_win = ntohs(th->th_win) << 8;
489
490 if(th->th_flags & TH_RST) {
491 char *buf = malloc(payload_length+1);
492 memcpy(buf, payload, payload_length);
493 if(payload_length && (buf[payload_length-1] == '\n'))
494 buf[payload_length-1] = 0;
495 buf[payload_length] = 0;
496 usbmuxd_log(LL_DEBUG, "RST reason: %s", buf);
497 free(buf);
498 }
499
500 if(conn->state == CONN_CONNECTING) {
501 if(th->th_flags != (TH_SYN|TH_ACK)) {
502 if(th->th_flags & TH_RST)
503 conn->state = CONN_REFUSED;
504 usbmuxd_log(LL_INFO, "Connection refused by device %d (%d->%d)", dev->id, sport, dport);
505 connection_teardown(conn); //this also sends the notification to the client
506 } else {
507 conn->tx_seq++;
508 conn->tx_ack++;
509 conn->rx_recvd = conn->rx_seq;
510 if(send_tcp(conn, TH_ACK, NULL, 0) < 0) {
511 usbmuxd_log(LL_ERROR, "Error sending TCP ACK to device %d (%d->%d)", dev->id, sport, dport);
512 connection_teardown(conn);
513 return;
514 }
515 conn->state = CONN_CONNECTED;
516 if(client_notify_connect(conn->client, RESULT_OK) < 0) {
517 conn->client = NULL;
518 connection_teardown(conn);
519 }
520 update_connection(conn);
521 }
522 } else if(conn->state == CONN_CONNECTED) {
523 if(th->th_flags != TH_ACK) {
524 usbmuxd_log(LL_INFO, "Connection reset by device %d (%d->%d)", dev->id, sport, dport);
525 if(th->th_flags & TH_RST)
526 conn->state = CONN_DYING;
527 connection_teardown(conn);
528 } else {
529 connection_device_input(conn, payload, payload_length);
530 }
531 }
532}
533
534void device_data_input(struct usb_device *usbdev, unsigned char *buffer, int length)
535{
536 struct mux_device *dev = NULL;
537 FOREACH(struct mux_device *tdev, &device_list) {
538 if(tdev->usbdev == usbdev) {
539 dev = tdev;
540 break;
541 }
542 } ENDFOREACH
543 if(!dev) {
544 usbmuxd_log(LL_WARNING, "Cannot find device entry for RX input from USB device %p on location 0x%x", usbdev, usb_get_location(usbdev));
545 return;
546 }
547
548 if(!length)
549 return;
550
551 usbmuxd_log(LL_SPEW, "Mux data input for device %p: %p len %d", dev, buffer, length);
552
553 // handle broken up transfers
554 if(dev->pktlen) {
555 memcpy(dev->pktbuf + dev->pktlen, buffer, length);
556 struct mux_header *mhdr = (struct mux_header *)dev->pktbuf;
557 if((length < USB_MRU) || (ntohl(mhdr->length) == length)) {
558 buffer = dev->pktbuf;
559 length += dev->pktlen;
560 dev->pktlen = 0;
561 usbmuxd_log(LL_SPEW, "Gathered mux data from buffer (total size: %d)", length);
562 } else {
563 dev->pktlen += length;
564 usbmuxd_log(LL_SPEW, "Appended mux data to buffer (total size: %d)", dev->pktlen);
565 return;
566 }
567 } else {
568 struct mux_header *mhdr = (struct mux_header *)buffer;
569 if((length == USB_MRU) && (length < ntohl(mhdr->length))) {
570 memcpy(dev->pktbuf, buffer, length);
571 dev->pktlen = length;
572 usbmuxd_log(LL_SPEW, "Copied mux data to buffer (size: %d)", dev->pktlen);
573 return;
574 }
575 }
576
577 struct mux_header *mhdr = (struct mux_header *)buffer;
578
579 if(ntohl(mhdr->length) != length) {
580 usbmuxd_log(LL_ERROR, "Incoming packet size mismatch (dev %d, expected %d, got %d)", dev->id, ntohl(mhdr->length), length);
581 return;
582 }
583
584 struct tcphdr *th;
585 unsigned char *payload;
586 int payload_length;
587
588 switch(ntohl(mhdr->protocol)) {
589 case MUX_PROTO_VERSION:
590 device_version_input(dev, (struct version_header *)(mhdr+1));
591 break;
592 case MUX_PROTO_TCP:
593 th = (struct tcphdr *)(mhdr+1);
594 payload = (unsigned char *)(th+1);
595 payload_length = length - sizeof(struct tcphdr) - sizeof(struct mux_header);
596 device_tcp_input(dev, (struct tcphdr *)(mhdr+1), payload, payload_length);
597 break;
598 default:
599 usbmuxd_log(LL_ERROR, "Incoming packet for device %d has unknown protocol 0x%x)", dev->id, ntohl(mhdr->protocol));
600 break;
601 }
602
603}
604
605int device_add(struct usb_device *usbdev)
606{
607 int res;
608 int id = get_next_device_id();
609 struct mux_device *dev;
610 usbmuxd_log(LL_NOTICE, "Connecting to new device on location 0x%x as ID %d", usb_get_location(usbdev), id);
611 dev = malloc(sizeof(struct mux_device));
612 dev->id = id;
613 dev->usbdev = usbdev;
614 dev->state = MUXDEV_INIT;
615 dev->next_sport = 1;
616 dev->pktbuf = malloc(DEV_PKTBUF_SIZE);
617 dev->pktlen = 0;
618 struct version_header vh;
619 vh.major = htonl(1);
620 vh.minor = htonl(0);
621 vh.padding = 0;
622 if((res = send_packet(dev, MUX_PROTO_VERSION, &vh, NULL, 0)) < 0) {
623 usbmuxd_log(LL_ERROR, "Error sending version request packet to device %d\n", id);
624 free(dev);
625 return res;
626 }
627 collection_add(&device_list, dev);
628 return 0;
629}
630
631void device_remove(struct usb_device *usbdev)
632{
633 FOREACH(struct mux_device *dev, &device_list) {
634 if(dev->usbdev == usbdev) {
635 usbmuxd_log(LL_NOTICE, "Removed device %d on location 0x%x", dev->id, usb_get_location(usbdev));
636 if(dev->state == MUXDEV_ACTIVE) {
637 dev->state = MUXDEV_DEAD;
638 FOREACH(struct mux_connection *conn, &dev->connections) {
639 connection_teardown(conn);
640 } ENDFOREACH
641 client_device_remove(dev->id);
642 collection_free(&dev->connections);
643 }
644 collection_remove(&device_list, dev);
645 free(dev->pktbuf);
646 free(dev);
647 return;
648 }
649 } ENDFOREACH
650 usbmuxd_log(LL_WARNING, "Cannot find device entry while removing USB device %p on location 0x%x", usbdev, usb_get_location(usbdev));
651}
652
653int device_get_count(void)
654{
655 int count = 0;
656 FOREACH(struct mux_device *dev, &device_list) {
657 if(dev->state == MUXDEV_ACTIVE)
658 count++;
659 } ENDFOREACH
660 return count;
661}
662
663int device_get_list(struct device_info *p)
664{
665 int count = 0;
666 FOREACH(struct mux_device *dev, &device_list) {
667 if(dev->state == MUXDEV_ACTIVE) {
668 p->id = dev->id;
669 p->serial = usb_get_serial(dev->usbdev);
670 p->location = usb_get_location(dev->usbdev);
671 p->pid = usb_get_pid(dev->usbdev);
672 count++;
673 p++;
674 }
675 } ENDFOREACH
676 return count;
677}
678
679int device_get_timeout(void)
680{
681 uint64_t oldest = (uint64_t)-1;
682 FOREACH(struct mux_device *dev, &device_list) {
683 if(dev->state == MUXDEV_ACTIVE) {
684 FOREACH(struct mux_connection *conn, &dev->connections) {
685 if((conn->state == CONN_CONNECTED) && (conn->flags & CONN_ACK_PENDING) && conn->last_ack_time < oldest)
686 oldest = conn->last_ack_time;
687 } ENDFOREACH
688 }
689 } ENDFOREACH
690 uint64_t ct = mstime64();
691 if(oldest == -1)
692 return 100000; //meh
693 if((ct - oldest) > ACK_TIMEOUT)
694 return 0;
695 return ACK_TIMEOUT - (ct - oldest);
696}
697
698void device_check_timeouts(void)
699{
700 uint64_t ct = mstime64();
701 FOREACH(struct mux_device *dev, &device_list) {
702 if(dev->state == MUXDEV_ACTIVE) {
703 FOREACH(struct mux_connection *conn, &dev->connections) {
704 if((conn->state == CONN_CONNECTED) &&
705 (conn->flags & CONN_ACK_PENDING) &&
706 (ct - conn->last_ack_time) > ACK_TIMEOUT) {
707 usbmuxd_log(LL_DEBUG, "Sending ACK due to expired timeout (%" PRIu64 " -> %" PRIu64 ")", conn->last_ack_time, ct);
708 if(send_tcp(conn, TH_ACK, NULL, 0) < 0) {
709 usbmuxd_log(LL_ERROR, "Error sending TCP ACK to device %d (%d->%d)", dev->id, conn->sport, conn->dport);
710 connection_teardown(conn);
711 }
712 }
713 } ENDFOREACH
714 }
715 } ENDFOREACH
716}
717
718void device_init(void)
719{
720 usbmuxd_log(LL_DEBUG, "device_init");
721 collection_init(&device_list);
722 next_device_id = 1;
723}
724
725void device_kill_connections(void)
726{
727 usbmuxd_log(LL_DEBUG, "device_kill_connections");
728 FOREACH(struct mux_device *dev, &device_list) {
729 if(dev->state != MUXDEV_INIT) {
730 FOREACH(struct mux_connection *conn, &dev->connections) {
731 connection_teardown(conn);
732 } ENDFOREACH
733 }
734 } ENDFOREACH
735 // give USB a while to send the final connection RSTs and the like
736 usb_process_timeout(100);
737}
738
739void device_shutdown(void)
740{
741 usbmuxd_log(LL_DEBUG, "device_shutdown");
742 FOREACH(struct mux_device *dev, &device_list) {
743 FOREACH(struct mux_connection *conn, &dev->connections) {
744 connection_teardown(conn);
745 } ENDFOREACH
746 collection_free(&dev->connections);
747 collection_remove(&device_list, dev);
748 free(dev);
749 } ENDFOREACH
750 collection_free(&device_list);
751}