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