summaryrefslogtreecommitdiffstats
path: root/usbmuxd/device.c
diff options
context:
space:
mode:
authorGravatar Hector Martin2009-05-05 00:38:06 +0200
committerGravatar Hector Martin2009-05-05 00:38:06 +0200
commitbd335df6954fe5e9419499820cd082c27de111f1 (patch)
tree4d7ccd2e767083baa2c41880081780de57665cf7 /usbmuxd/device.c
parent9bf93e406de3a06cee0a1452bf1da3c6f697ee31 (diff)
downloadusbmuxd-bd335df6954fe5e9419499820cd082c27de111f1.tar.gz
usbmuxd-bd335df6954fe5e9419499820cd082c27de111f1.tar.bz2
Move usbmuxd to its own folder
Diffstat (limited to 'usbmuxd/device.c')
-rw-r--r--usbmuxd/device.c731
1 files changed, 731 insertions, 0 deletions
diff --git a/usbmuxd/device.c b/usbmuxd/device.c
new file mode 100644
index 0000000..c6ef75b
--- /dev/null
+++ b/usbmuxd/device.c
@@ -0,0 +1,731 @@
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 60
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_tcp(struct mux_connection *conn, uint8_t flags, const unsigned char *data, int length)
202{
203 struct tcphdr th;
204 memset(&th, 0, sizeof(th));
205 th.th_sport = htons(conn->sport);
206 th.th_dport = htons(conn->dport);
207 th.th_seq = htonl(conn->tx_seq);
208 th.th_ack = htonl(conn->tx_ack);
209 th.th_flags = flags;
210 th.th_off = sizeof(th) / 4;
211 th.th_win = htons(conn->tx_win >> 8);
212
213 usbmuxd_log(LL_DEBUG, "[OUT] dev=%d sport=%d dport=%d seq=%d ack=%d flags=0x%x window=%d[%d] len=%d",
214 conn->dev->id, conn->sport, conn->dport, conn->tx_seq, conn->tx_ack, flags, conn->tx_win, conn->tx_win >> 8, length);
215
216 int res = send_packet(conn->dev, MUX_PROTO_TCP, &th, data, length);
217 if(res >= 0) {
218 conn->tx_acked = conn->tx_ack;
219 conn->last_ack_time = mstime64();
220 conn->flags &= ~CONN_ACK_PENDING;
221 }
222 return res;
223}
224
225static void connection_teardown(struct mux_connection *conn)
226{
227 int res;
228 if(conn->state == CONN_DEAD)
229 return;
230 usbmuxd_log(LL_DEBUG, "connection_teardown dev %d sport %d dport %d", conn->dev->id, conn->sport, conn->dport);
231 if(conn->dev->state != MUXDEV_DEAD && conn->state != CONN_DYING && conn->state != CONN_REFUSED) {
232 res = send_tcp(conn, TH_RST, NULL, 0);
233 if(res < 0)
234 usbmuxd_log(LL_ERROR, "Error sending TCP RST to device %d (%d->%d)", conn->dev->id, conn->sport, conn->dport);
235 }
236 if(conn->client) {
237 if(conn->state == CONN_REFUSED || conn->state == CONN_CONNECTING) {
238 client_notify_connect(conn->client, RESULT_CONNREFUSED);
239 } else {
240 conn->state = CONN_DEAD;
241 client_close(conn->client);
242 }
243 }
244 if(conn->ib_buf)
245 free(conn->ib_buf);
246 if(conn->ob_buf)
247 free(conn->ob_buf);
248 collection_remove(&conn->dev->connections, conn);
249 free(conn);
250}
251
252int device_start_connect(int device_id, uint16_t dport, struct mux_client *client)
253{
254 struct mux_device *dev = NULL;
255 FOREACH(struct mux_device *cdev, &device_list) {
256 if(cdev->id == device_id) {
257 dev = cdev;
258 break;
259 }
260 } ENDFOREACH
261 if(!dev) {
262 usbmuxd_log(LL_WARNING, "Attempted to connect to nonexistent device %d", device_id);
263 return -RESULT_BADDEV;
264 }
265
266 uint16_t sport = find_sport(dev);
267 if(!sport) {
268 usbmuxd_log(LL_WARNING, "Unable to allocate port for device %d", device_id);
269 return -RESULT_BADDEV;
270 }
271
272 struct mux_connection *conn;
273 conn = malloc(sizeof(struct mux_connection));
274 memset(conn, 0, sizeof(struct mux_connection));
275
276 conn->dev = dev;
277 conn->client = client;
278 conn->state = CONN_CONNECTING;
279 conn->sport = sport;
280 conn->dport = dport;
281 conn->tx_seq = 0;
282 conn->tx_ack = 0;
283 conn->tx_acked = 0;
284 conn->tx_win = 131072;
285 conn->rx_recvd = 0;
286 conn->flags = 0;
287 conn->max_payload = USB_MTU - sizeof(struct mux_header) - sizeof(struct tcphdr);
288
289 conn->ob_buf = malloc(CONN_OUTBUF_SIZE);
290 conn->ob_capacity = CONN_OUTBUF_SIZE;
291 conn->ib_buf = malloc(CONN_INBUF_SIZE);
292 conn->ib_capacity = CONN_INBUF_SIZE;
293 conn->ib_size = 0;
294
295 int res;
296
297 res = send_tcp(conn, TH_SYN, NULL, 0);
298 if(res < 0) {
299 usbmuxd_log(LL_ERROR, "Error sending TCP SYN to device %d (%d->%d)", dev->id, sport, dport);
300 free(conn);
301 return -RESULT_CONNREFUSED; //bleh
302 }
303 collection_add(&dev->connections, conn);
304 return 0;
305}
306
307static void update_connection(struct mux_connection *conn)
308{
309 conn->sendable = conn->rx_win - (conn->tx_seq - conn->rx_ack);
310
311 if(conn->sendable > conn->ob_capacity)
312 conn->sendable = conn->ob_capacity;
313 if(conn->sendable > conn->max_payload)
314 conn->sendable = conn->max_payload;
315
316 if(conn->sendable > 0)
317 conn->events |= POLLIN;
318 else
319 conn->events &= ~POLLIN;
320
321 if(conn->ib_size)
322 conn->events |= POLLOUT;
323 else
324 conn->events &= ~POLLOUT;
325
326 if(conn->tx_acked != conn->tx_ack)
327 conn->flags |= CONN_ACK_PENDING;
328 else
329 conn->flags &= ~CONN_ACK_PENDING;
330
331 usbmuxd_log(LL_SPEW, "update_connection: sendable %d, events %d, flags %d", conn->sendable, conn->events, conn->flags);
332 client_set_events(conn->client, conn->events);
333}
334
335void device_client_process(int device_id, struct mux_client *client, short events)
336{
337 struct mux_connection *conn = NULL;
338 FOREACH(struct mux_device *dev, &device_list) {
339 if(dev->id == device_id) {
340 FOREACH(struct mux_connection *lconn, &dev->connections) {
341 if(lconn->client == client) {
342 conn = lconn;
343 break;
344 }
345 } ENDFOREACH
346 break;
347 }
348 } ENDFOREACH
349
350 if(!conn) {
351 usbmuxd_log(LL_WARNING, "Could not find connection for device %d client %p", device_id, client);
352 return;
353 }
354 usbmuxd_log(LL_SPEW, "device_client_process (%d)", events);
355
356 int res;
357 int size;
358 if(events & POLLOUT) {
359 size = client_write(conn->client, conn->ib_buf, conn->ib_size);
360 if(size <= 0) {
361 usbmuxd_log(LL_DEBUG, "error writing to client (%d)", size);
362 connection_teardown(conn);
363 return;
364 }
365 conn->tx_ack += size;
366 if(size == conn->ib_size) {
367 conn->ib_size = 0;
368 } else {
369 conn->ib_size -= size;
370 memmove(conn->ib_buf, conn->ib_buf + size, conn->ib_size);
371 }
372 }
373 if(events & POLLIN) {
374 size = client_read(conn->client, conn->ob_buf, conn->sendable);
375 if(size <= 0) {
376 usbmuxd_log(LL_DEBUG, "error reading from client (%d)", size);
377 connection_teardown(conn);
378 return;
379 }
380 res = send_tcp(conn, TH_ACK, conn->ob_buf, size);
381 if(res < 0) {
382 connection_teardown(conn);
383 return;
384 }
385 conn->tx_seq += size;
386 }
387
388 update_connection(conn);
389}
390
391static void connection_device_input(struct mux_connection *conn, unsigned char *payload, int payload_length)
392{
393 if((conn->ib_size + payload_length) > conn->ib_capacity) {
394 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);
395 connection_teardown(conn);
396 return;
397 }
398 memcpy(conn->ib_buf + conn->ib_size, payload, payload_length);
399 conn->ib_size += payload_length;
400 conn->rx_recvd += payload_length;
401 update_connection(conn);
402}
403
404void device_abort_connect(int device_id, struct mux_client *client)
405{
406 FOREACH(struct mux_device *dev, &device_list) {
407 if(dev->id == device_id) {
408 FOREACH(struct mux_connection *conn, &dev->connections) {
409 if(conn->client == client) {
410 connection_teardown(conn);
411 return;
412 }
413 } ENDFOREACH
414 usbmuxd_log(LL_WARNING, "Attempted to abort for nonexistent connection for device %d", device_id);
415 return;
416 }
417 } ENDFOREACH
418 usbmuxd_log(LL_WARNING, "Attempted to abort connection for nonexistent device %d", device_id);
419}
420
421static void device_version_input(struct mux_device *dev, struct version_header *vh)
422{
423 if(dev->state != MUXDEV_INIT) {
424 usbmuxd_log(LL_WARNING, "Version packet from already initialized device %d", dev->id);
425 return;
426 }
427 vh->major = ntohl(vh->major);
428 vh->minor = ntohl(vh->minor);
429 if(vh->major != 1 || vh->minor != 0) {
430 usbmuxd_log(LL_ERROR, "Device %d has unknown version %d.%d\n", dev->id, vh->major, vh->minor);
431 collection_remove(&device_list, dev);
432 free(dev);
433 return;
434 }
435 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));
436 dev->state = MUXDEV_ACTIVE;
437 collection_init(&dev->connections);
438 struct device_info info;
439 info.id = dev->id;
440 info.location = usb_get_location(dev->usbdev);
441 info.serial = usb_get_serial(dev->usbdev);
442 info.pid = usb_get_pid(dev->usbdev);
443 client_device_add(&info);
444}
445
446static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned char *payload, int payload_length)
447{
448 usbmuxd_log(LL_DEBUG, "[IN] dev=%d sport=%d dport=%d seq=%d ack=%d flags=0x%x window=%d[%d] len=%d",
449 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);
450
451 uint16_t sport = ntohs(th->th_dport);
452 uint16_t dport = ntohs(th->th_sport);
453 struct mux_connection *conn = NULL;
454 FOREACH(struct mux_connection *lconn, &dev->connections) {
455 if(lconn->sport == sport && lconn->dport == dport) {
456 conn = lconn;
457 break;
458 }
459 } ENDFOREACH
460
461 if(!conn) {
462 usbmuxd_log(LL_WARNING, "No connection for device %d incoming packet %d->%d", dev->id, dport, sport);
463 return;
464 }
465
466 conn->rx_seq = ntohl(th->th_seq);
467 conn->rx_ack = ntohl(th->th_ack);
468 conn->rx_win = ntohs(th->th_win) << 8;
469
470 if(th->th_flags & TH_RST) {
471 char *buf = malloc(payload_length+1);
472 memcpy(buf, payload, payload_length);
473 if(payload_length && (buf[payload_length-1] == '\n'))
474 buf[payload_length-1] = 0;
475 buf[payload_length] = 0;
476 usbmuxd_log(LL_DEBUG, "RST reason: %s", buf);
477 free(buf);
478 }
479
480 if(conn->state == CONN_CONNECTING) {
481 if(th->th_flags != (TH_SYN|TH_ACK)) {
482 if(th->th_flags & TH_RST)
483 conn->state = CONN_REFUSED;
484 usbmuxd_log(LL_INFO, "Connection refused by device %d (%d->%d)", dev->id, sport, dport);
485 connection_teardown(conn); //this also sends the notification to the client
486 } else {
487 conn->tx_seq++;
488 conn->tx_ack++;
489 conn->rx_recvd = conn->rx_seq;
490 if(send_tcp(conn, TH_ACK, NULL, 0) < 0) {
491 usbmuxd_log(LL_ERROR, "Error sending TCP ACK to device %d (%d->%d)", dev->id, sport, dport);
492 connection_teardown(conn);
493 return;
494 }
495 conn->state = CONN_CONNECTED;
496 if(client_notify_connect(conn->client, RESULT_OK) < 0) {
497 conn->client = NULL;
498 connection_teardown(conn);
499 }
500 update_connection(conn);
501 }
502 } else if(conn->state == CONN_CONNECTED) {
503 if(th->th_flags != TH_ACK) {
504 usbmuxd_log(LL_INFO, "Connection reset by device %d (%d->%d)", dev->id, sport, dport);
505 if(th->th_flags & TH_RST)
506 conn->state = CONN_DYING;
507 connection_teardown(conn);
508 } else {
509 connection_device_input(conn, payload, payload_length);
510 }
511 }
512}
513
514void device_data_input(struct usb_device *usbdev, unsigned char *buffer, int length)
515{
516 struct mux_device *dev = NULL;
517 FOREACH(struct mux_device *tdev, &device_list) {
518 if(tdev->usbdev == usbdev) {
519 dev = tdev;
520 break;
521 }
522 } ENDFOREACH
523 if(!dev) {
524 usbmuxd_log(LL_WARNING, "Cannot find device entry for RX input from USB device %p on location 0x%x", usbdev, usb_get_location(usbdev));
525 return;
526 }
527
528 if(!length)
529 return;
530
531 usbmuxd_log(LL_SPEW, "Mux data input for device %p: %p len %d", dev, buffer, length);
532
533 // handle broken up transfers
534 if(dev->pktlen) {
535 memcpy(dev->pktbuf + dev->pktlen, buffer, length);
536 struct mux_header *mhdr = (struct mux_header *)dev->pktbuf;
537 if((length < USB_MRU) || (ntohl(mhdr->length) == length)) {
538 buffer = dev->pktbuf;
539 length += dev->pktlen;
540 dev->pktlen = 0;
541 usbmuxd_log(LL_SPEW, "Gathered mux data from buffer (total size: %d)", length);
542 } else {
543 dev->pktlen += length;
544 usbmuxd_log(LL_SPEW, "Appended mux data to buffer (total size: %d)", dev->pktlen);
545 return;
546 }
547 } else {
548 struct mux_header *mhdr = (struct mux_header *)buffer;
549 if((length == USB_MRU) && (length < ntohl(mhdr->length))) {
550 memcpy(dev->pktbuf, buffer, length);
551 dev->pktlen = length;
552 usbmuxd_log(LL_SPEW, "Copied mux data to buffer (size: %d)", dev->pktlen);
553 return;
554 }
555 }
556
557 struct mux_header *mhdr = (struct mux_header *)buffer;
558
559 if(ntohl(mhdr->length) != length) {
560 usbmuxd_log(LL_ERROR, "Incoming packet size mismatch (dev %d, expected %d, got %d)", dev->id, ntohl(mhdr->length), length);
561 return;
562 }
563
564 struct tcphdr *th;
565 unsigned char *payload;
566 int payload_length;
567
568 switch(ntohl(mhdr->protocol)) {
569 case MUX_PROTO_VERSION:
570 device_version_input(dev, (struct version_header *)(mhdr+1));
571 break;
572 case MUX_PROTO_TCP:
573 th = (struct tcphdr *)(mhdr+1);
574 payload = (unsigned char *)(th+1);
575 payload_length = length - sizeof(struct tcphdr) - sizeof(struct mux_header);
576 device_tcp_input(dev, (struct tcphdr *)(mhdr+1), payload, payload_length);
577 break;
578 default:
579 usbmuxd_log(LL_ERROR, "Incoming packet for device %d has unknown protocol 0x%x)", dev->id, ntohl(mhdr->protocol));
580 break;
581 }
582
583}
584
585int device_add(struct usb_device *usbdev)
586{
587 int res;
588 int id = get_next_device_id();
589 struct mux_device *dev;
590 usbmuxd_log(LL_NOTICE, "Connecting to new device on location 0x%x as ID %d", usb_get_location(usbdev), id);
591 dev = malloc(sizeof(struct mux_device));
592 dev->id = id;
593 dev->usbdev = usbdev;
594 dev->state = MUXDEV_INIT;
595 dev->next_sport = 1;
596 dev->pktbuf = malloc(DEV_PKTBUF_SIZE);
597 dev->pktlen = 0;
598 struct version_header vh;
599 vh.major = htonl(1);
600 vh.minor = htonl(0);
601 vh.padding = 0;
602 if((res = send_packet(dev, MUX_PROTO_VERSION, &vh, NULL, 0)) < 0) {
603 usbmuxd_log(LL_ERROR, "Error sending version request packet to device %d\n", id);
604 free(dev);
605 return res;
606 }
607 collection_add(&device_list, dev);
608 return 0;
609}
610
611void device_remove(struct usb_device *usbdev)
612{
613 FOREACH(struct mux_device *dev, &device_list) {
614 if(dev->usbdev == usbdev) {
615 usbmuxd_log(LL_NOTICE, "Removed device %d on location 0x%x", dev->id, usb_get_location(usbdev));
616 if(dev->state == MUXDEV_ACTIVE) {
617 dev->state = MUXDEV_DEAD;
618 FOREACH(struct mux_connection *conn, &dev->connections) {
619 connection_teardown(conn);
620 } ENDFOREACH
621 client_device_remove(dev->id);
622 collection_free(&dev->connections);
623 }
624 collection_remove(&device_list, dev);
625 free(dev->pktbuf);
626 free(dev);
627 return;
628 }
629 } ENDFOREACH
630 usbmuxd_log(LL_WARNING, "Cannot find device entry while removing USB device %p on location 0x%x", usbdev, usb_get_location(usbdev));
631}
632
633int device_get_count(void)
634{
635 int count = 0;
636 FOREACH(struct mux_device *dev, &device_list) {
637 if(dev->state == MUXDEV_ACTIVE)
638 count++;
639 } ENDFOREACH
640 return count;
641}
642
643int device_get_list(struct device_info *p)
644{
645 int count = 0;
646 FOREACH(struct mux_device *dev, &device_list) {
647 if(dev->state == MUXDEV_ACTIVE) {
648 p->id = dev->id;
649 p->serial = usb_get_serial(dev->usbdev);
650 p->location = usb_get_location(dev->usbdev);
651 p->pid = usb_get_pid(dev->usbdev);
652 count++;
653 p++;
654 }
655 } ENDFOREACH
656 return count;
657}
658
659int device_get_timeout(void)
660{
661 uint64_t oldest = (uint64_t)-1;
662 FOREACH(struct mux_device *dev, &device_list) {
663 if(dev->state == MUXDEV_ACTIVE) {
664 FOREACH(struct mux_connection *conn, &dev->connections) {
665 if((conn->state == CONN_CONNECTED) && (conn->flags & CONN_ACK_PENDING) && conn->last_ack_time < oldest)
666 oldest = conn->last_ack_time;
667 } ENDFOREACH
668 }
669 } ENDFOREACH
670 uint64_t ct = mstime64();
671 if(oldest == -1)
672 return 100000; //meh
673 if((ct - oldest) > ACK_TIMEOUT)
674 return 0;
675 return ACK_TIMEOUT - (ct - oldest);
676}
677
678void device_check_timeouts(void)
679{
680 uint64_t ct = mstime64();
681 FOREACH(struct mux_device *dev, &device_list) {
682 if(dev->state == MUXDEV_ACTIVE) {
683 FOREACH(struct mux_connection *conn, &dev->connections) {
684 if((conn->state == CONN_CONNECTED) &&
685 (conn->flags & CONN_ACK_PENDING) &&
686 (ct - conn->last_ack_time) > ACK_TIMEOUT) {
687 usbmuxd_log(LL_DEBUG, "Sending ACK due to expired timeout (%" PRIu64 " -> %" PRIu64 ")", conn->last_ack_time, ct);
688 if(send_tcp(conn, TH_ACK, NULL, 0) < 0) {
689 usbmuxd_log(LL_ERROR, "Error sending TCP ACK to device %d (%d->%d)", dev->id, conn->sport, conn->dport);
690 connection_teardown(conn);
691 }
692 }
693 } ENDFOREACH
694 }
695 } ENDFOREACH
696}
697
698void device_init(void)
699{
700 usbmuxd_log(LL_DEBUG, "device_init");
701 collection_init(&device_list);
702 next_device_id = 1;
703}
704
705void device_kill_connections(void)
706{
707 usbmuxd_log(LL_DEBUG, "device_kill_connections");
708 FOREACH(struct mux_device *dev, &device_list) {
709 if(dev->state != MUXDEV_INIT) {
710 FOREACH(struct mux_connection *conn, &dev->connections) {
711 connection_teardown(conn);
712 } ENDFOREACH
713 }
714 } ENDFOREACH
715 // give USB a while to send the final connection RSTs and the like
716 usb_process_timeout(100);
717}
718
719void device_shutdown(void)
720{
721 usbmuxd_log(LL_DEBUG, "device_shutdown");
722 FOREACH(struct mux_device *dev, &device_list) {
723 FOREACH(struct mux_connection *conn, &dev->connections) {
724 connection_teardown(conn);
725 } ENDFOREACH
726 collection_free(&dev->connections);
727 collection_remove(&device_list, dev);
728 free(dev);
729 } ENDFOREACH
730 collection_free(&device_list);
731}