diff options
| author | 2013-09-17 11:30:01 +0200 | |
|---|---|---|
| committer | 2013-09-17 11:30:01 +0200 | |
| commit | f4758e8b15cd30fe3f7f18de42e2ea20bc5696f0 (patch) | |
| tree | 671e85e639b689b0b888a0f51c7dd5e15d408930 /src/device.c | |
| parent | 10939f3ad5755d1117f20df2b97c0cbbd83bbcbe (diff) | |
| download | usbmuxd-f4758e8b15cd30fe3f7f18de42e2ea20bc5696f0.tar.gz usbmuxd-f4758e8b15cd30fe3f7f18de42e2ea20bc5696f0.tar.bz2 | |
remove libusbmuxd sources and adapt source tree to use autotools
libusbmuxd has been split off and is now managed in a separate repository.
By the time of this commit, the repository is:
git clone http://git.sukimashita.com/libusbmuxd.git
Diffstat (limited to 'src/device.c')
| -rw-r--r-- | src/device.c | 781 |
1 files changed, 781 insertions, 0 deletions
diff --git a/src/device.c b/src/device.c new file mode 100644 index 0000000..8c786a7 --- /dev/null +++ b/src/device.c | |||
| @@ -0,0 +1,781 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 | |||
| 39 | int next_device_id; | ||
| 40 | |||
| 41 | #define DEV_MRU 65536 | ||
| 42 | |||
| 43 | #define CONN_INBUF_SIZE 262144 | ||
| 44 | #define CONN_OUTBUF_SIZE 65536 | ||
| 45 | |||
| 46 | #define ACK_TIMEOUT 30 | ||
| 47 | |||
| 48 | enum mux_protocol { | ||
| 49 | MUX_PROTO_VERSION = 0, | ||
| 50 | MUX_PROTO_TCP = IPPROTO_TCP, | ||
| 51 | }; | ||
| 52 | |||
| 53 | enum mux_dev_state { | ||
| 54 | MUXDEV_INIT, // sent version packet | ||
| 55 | MUXDEV_ACTIVE, // received version packet, active | ||
| 56 | MUXDEV_DEAD // dead | ||
| 57 | }; | ||
| 58 | |||
| 59 | enum 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 | |||
| 67 | struct mux_header | ||
| 68 | { | ||
| 69 | uint32_t protocol; | ||
| 70 | uint32_t length; | ||
| 71 | }; | ||
| 72 | |||
| 73 | struct version_header | ||
| 74 | { | ||
| 75 | uint32_t major; | ||
| 76 | uint32_t minor; | ||
| 77 | uint32_t padding; | ||
| 78 | }; | ||
| 79 | |||
| 80 | struct mux_device; | ||
| 81 | |||
| 82 | #define CONN_ACK_PENDING 1 | ||
| 83 | |||
| 84 | struct 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 | uint32_t max_payload; | ||
| 93 | uint32_t sendable; | ||
| 94 | int flags; | ||
| 95 | unsigned char *ib_buf; | ||
| 96 | uint32_t ib_size; | ||
| 97 | uint32_t ib_capacity; | ||
| 98 | unsigned char *ob_buf; | ||
| 99 | uint32_t ob_capacity; | ||
| 100 | short events; | ||
| 101 | uint64_t last_ack_time; | ||
| 102 | }; | ||
| 103 | |||
| 104 | struct 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 | uint32_t pktlen; | ||
| 113 | }; | ||
| 114 | |||
| 115 | static struct collection device_list; | ||
| 116 | |||
| 117 | uint64_t mstime64(void) | ||
| 118 | { | ||
| 119 | struct timeval tv; | ||
| 120 | gettimeofday(&tv, NULL); | ||
| 121 | return tv.tv_sec * 1000 + tv.tv_usec / 1000; | ||
| 122 | } | ||
| 123 | |||
| 124 | static 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 | |||
| 140 | static 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 | |||
| 182 | static 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 | |||
| 201 | static 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 | |||
| 217 | static 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 | |||
| 241 | static 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 | |||
| 268 | int 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 | |||
| 323 | static void update_connection(struct mux_connection *conn) | ||
| 324 | { | ||
| 325 | uint32_t sent = conn->tx_seq - conn->rx_ack; | ||
| 326 | |||
| 327 | if(conn->rx_win > sent) | ||
| 328 | conn->sendable = conn->rx_win - sent; | ||
| 329 | else | ||
| 330 | conn->sendable = 0; | ||
| 331 | |||
| 332 | if(conn->sendable > conn->ob_capacity) | ||
| 333 | conn->sendable = conn->ob_capacity; | ||
| 334 | if(conn->sendable > conn->max_payload) | ||
| 335 | conn->sendable = conn->max_payload; | ||
| 336 | |||
| 337 | if(conn->sendable > 0) | ||
| 338 | conn->events |= POLLIN; | ||
| 339 | else | ||
| 340 | conn->events &= ~POLLIN; | ||
| 341 | |||
| 342 | if(conn->ib_size) | ||
| 343 | conn->events |= POLLOUT; | ||
| 344 | else | ||
| 345 | conn->events &= ~POLLOUT; | ||
| 346 | |||
| 347 | if(conn->tx_acked != conn->tx_ack) | ||
| 348 | conn->flags |= CONN_ACK_PENDING; | ||
| 349 | else | ||
| 350 | conn->flags &= ~CONN_ACK_PENDING; | ||
| 351 | |||
| 352 | usbmuxd_log(LL_SPEW, "update_connection: sendable %d, events %d, flags %d", conn->sendable, conn->events, conn->flags); | ||
| 353 | client_set_events(conn->client, conn->events); | ||
| 354 | } | ||
| 355 | |||
| 356 | void device_client_process(int device_id, struct mux_client *client, short events) | ||
| 357 | { | ||
| 358 | struct mux_connection *conn = NULL; | ||
| 359 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 360 | if(dev->id == device_id) { | ||
| 361 | FOREACH(struct mux_connection *lconn, &dev->connections) { | ||
| 362 | if(lconn->client == client) { | ||
| 363 | conn = lconn; | ||
| 364 | break; | ||
| 365 | } | ||
| 366 | } ENDFOREACH | ||
| 367 | break; | ||
| 368 | } | ||
| 369 | } ENDFOREACH | ||
| 370 | |||
| 371 | if(!conn) { | ||
| 372 | usbmuxd_log(LL_WARNING, "Could not find connection for device %d client %p", device_id, client); | ||
| 373 | return; | ||
| 374 | } | ||
| 375 | usbmuxd_log(LL_SPEW, "device_client_process (%d)", events); | ||
| 376 | |||
| 377 | int res; | ||
| 378 | int size; | ||
| 379 | if(events & POLLOUT) { | ||
| 380 | size = client_write(conn->client, conn->ib_buf, conn->ib_size); | ||
| 381 | if(size <= 0) { | ||
| 382 | usbmuxd_log(LL_DEBUG, "error writing to client (%d)", size); | ||
| 383 | connection_teardown(conn); | ||
| 384 | return; | ||
| 385 | } | ||
| 386 | conn->tx_ack += size; | ||
| 387 | if(size == conn->ib_size) { | ||
| 388 | conn->ib_size = 0; | ||
| 389 | } else { | ||
| 390 | conn->ib_size -= size; | ||
| 391 | memmove(conn->ib_buf, conn->ib_buf + size, conn->ib_size); | ||
| 392 | } | ||
| 393 | } | ||
| 394 | if(events & POLLIN) { | ||
| 395 | size = client_read(conn->client, conn->ob_buf, conn->sendable); | ||
| 396 | if(size <= 0) { | ||
| 397 | usbmuxd_log(LL_DEBUG, "error reading from client (%d)", size); | ||
| 398 | connection_teardown(conn); | ||
| 399 | return; | ||
| 400 | } | ||
| 401 | res = send_tcp(conn, TH_ACK, conn->ob_buf, size); | ||
| 402 | if(res < 0) { | ||
| 403 | connection_teardown(conn); | ||
| 404 | return; | ||
| 405 | } | ||
| 406 | conn->tx_seq += size; | ||
| 407 | } | ||
| 408 | |||
| 409 | update_connection(conn); | ||
| 410 | } | ||
| 411 | |||
| 412 | static void connection_device_input(struct mux_connection *conn, unsigned char *payload, uint32_t payload_length) | ||
| 413 | { | ||
| 414 | if((conn->ib_size + payload_length) > conn->ib_capacity) { | ||
| 415 | 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); | ||
| 416 | connection_teardown(conn); | ||
| 417 | return; | ||
| 418 | } | ||
| 419 | memcpy(conn->ib_buf + conn->ib_size, payload, payload_length); | ||
| 420 | conn->ib_size += payload_length; | ||
| 421 | conn->rx_recvd += payload_length; | ||
| 422 | update_connection(conn); | ||
| 423 | } | ||
| 424 | |||
| 425 | void device_abort_connect(int device_id, struct mux_client *client) | ||
| 426 | { | ||
| 427 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 428 | if(dev->id == device_id) { | ||
| 429 | FOREACH(struct mux_connection *conn, &dev->connections) { | ||
| 430 | if(conn->client == client) { | ||
| 431 | connection_teardown(conn); | ||
| 432 | return; | ||
| 433 | } | ||
| 434 | } ENDFOREACH | ||
| 435 | usbmuxd_log(LL_WARNING, "Attempted to abort for nonexistent connection for device %d", device_id); | ||
| 436 | return; | ||
| 437 | } | ||
| 438 | } ENDFOREACH | ||
| 439 | usbmuxd_log(LL_WARNING, "Attempted to abort connection for nonexistent device %d", device_id); | ||
| 440 | } | ||
| 441 | |||
| 442 | static void device_version_input(struct mux_device *dev, struct version_header *vh) | ||
| 443 | { | ||
| 444 | if(dev->state != MUXDEV_INIT) { | ||
| 445 | usbmuxd_log(LL_WARNING, "Version packet from already initialized device %d", dev->id); | ||
| 446 | return; | ||
| 447 | } | ||
| 448 | vh->major = ntohl(vh->major); | ||
| 449 | vh->minor = ntohl(vh->minor); | ||
| 450 | if(vh->major != 1 || vh->minor != 0) { | ||
| 451 | usbmuxd_log(LL_ERROR, "Device %d has unknown version %d.%d\n", dev->id, vh->major, vh->minor); | ||
| 452 | collection_remove(&device_list, dev); | ||
| 453 | free(dev); | ||
| 454 | return; | ||
| 455 | } | ||
| 456 | 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)); | ||
| 457 | dev->state = MUXDEV_ACTIVE; | ||
| 458 | collection_init(&dev->connections); | ||
| 459 | struct device_info info; | ||
| 460 | info.id = dev->id; | ||
| 461 | info.location = usb_get_location(dev->usbdev); | ||
| 462 | info.serial = usb_get_serial(dev->usbdev); | ||
| 463 | info.pid = usb_get_pid(dev->usbdev); | ||
| 464 | client_device_add(&info); | ||
| 465 | } | ||
| 466 | |||
| 467 | static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned char *payload, uint32_t payload_length) | ||
| 468 | { | ||
| 469 | uint16_t sport = ntohs(th->th_dport); | ||
| 470 | uint16_t dport = ntohs(th->th_sport); | ||
| 471 | struct mux_connection *conn = NULL; | ||
| 472 | |||
| 473 | usbmuxd_log(LL_DEBUG, "[IN] dev=%d sport=%d dport=%d seq=%d ack=%d flags=0x%x window=%d[%d] len=%d", | ||
| 474 | dev->id, dport, sport, ntohl(th->th_seq), ntohl(th->th_ack), th->th_flags, ntohs(th->th_win) << 8, ntohs(th->th_win), payload_length); | ||
| 475 | |||
| 476 | if(dev->state != MUXDEV_ACTIVE) { | ||
| 477 | usbmuxd_log(LL_ERROR, "Received TCP packet from device %d but the device isn't active yet, discarding\n", dev->id); | ||
| 478 | return; | ||
| 479 | } | ||
| 480 | |||
| 481 | FOREACH(struct mux_connection *lconn, &dev->connections) { | ||
| 482 | if(lconn->sport == sport && lconn->dport == dport) { | ||
| 483 | conn = lconn; | ||
| 484 | break; | ||
| 485 | } | ||
| 486 | } ENDFOREACH | ||
| 487 | |||
| 488 | if(!conn) { | ||
| 489 | usbmuxd_log(LL_INFO, "No connection for device %d incoming packet %d->%d", dev->id, dport, sport); | ||
| 490 | if(!(th->th_flags & TH_RST)) { | ||
| 491 | if(send_anon_rst(dev, sport, dport, ntohl(th->th_seq)) < 0) | ||
| 492 | usbmuxd_log(LL_ERROR, "Error sending TCP RST to device %d (%d->%d)", conn->dev->id, sport, dport); | ||
| 493 | } | ||
| 494 | return; | ||
| 495 | } | ||
| 496 | |||
| 497 | conn->rx_seq = ntohl(th->th_seq); | ||
| 498 | conn->rx_ack = ntohl(th->th_ack); | ||
| 499 | conn->rx_win = ntohs(th->th_win) << 8; | ||
| 500 | |||
| 501 | if(th->th_flags & TH_RST) { | ||
| 502 | char *buf = malloc(payload_length+1); | ||
| 503 | memcpy(buf, payload, payload_length); | ||
| 504 | if(payload_length && (buf[payload_length-1] == '\n')) | ||
| 505 | buf[payload_length-1] = 0; | ||
| 506 | buf[payload_length] = 0; | ||
| 507 | usbmuxd_log(LL_DEBUG, "RST reason: %s", buf); | ||
| 508 | free(buf); | ||
| 509 | } | ||
| 510 | |||
| 511 | if(conn->state == CONN_CONNECTING) { | ||
| 512 | if(th->th_flags != (TH_SYN|TH_ACK)) { | ||
| 513 | if(th->th_flags & TH_RST) | ||
| 514 | conn->state = CONN_REFUSED; | ||
| 515 | usbmuxd_log(LL_INFO, "Connection refused by device %d (%d->%d)", dev->id, sport, dport); | ||
| 516 | connection_teardown(conn); //this also sends the notification to the client | ||
| 517 | } else { | ||
| 518 | conn->tx_seq++; | ||
| 519 | conn->tx_ack++; | ||
| 520 | conn->rx_recvd = conn->rx_seq; | ||
| 521 | if(send_tcp(conn, TH_ACK, NULL, 0) < 0) { | ||
| 522 | usbmuxd_log(LL_ERROR, "Error sending TCP ACK to device %d (%d->%d)", dev->id, sport, dport); | ||
| 523 | connection_teardown(conn); | ||
| 524 | return; | ||
| 525 | } | ||
| 526 | conn->state = CONN_CONNECTED; | ||
| 527 | if(client_notify_connect(conn->client, RESULT_OK) < 0) { | ||
| 528 | conn->client = NULL; | ||
| 529 | connection_teardown(conn); | ||
| 530 | } | ||
| 531 | update_connection(conn); | ||
| 532 | } | ||
| 533 | } else if(conn->state == CONN_CONNECTED) { | ||
| 534 | if(th->th_flags != TH_ACK) { | ||
| 535 | usbmuxd_log(LL_INFO, "Connection reset by device %d (%d->%d)", dev->id, sport, dport); | ||
| 536 | if(th->th_flags & TH_RST) | ||
| 537 | conn->state = CONN_DYING; | ||
| 538 | connection_teardown(conn); | ||
| 539 | } else { | ||
| 540 | connection_device_input(conn, payload, payload_length); | ||
| 541 | } | ||
| 542 | } | ||
| 543 | } | ||
| 544 | |||
| 545 | void device_data_input(struct usb_device *usbdev, unsigned char *buffer, uint32_t length) | ||
| 546 | { | ||
| 547 | struct mux_device *dev = NULL; | ||
| 548 | FOREACH(struct mux_device *tdev, &device_list) { | ||
| 549 | if(tdev->usbdev == usbdev) { | ||
| 550 | dev = tdev; | ||
| 551 | break; | ||
| 552 | } | ||
| 553 | } ENDFOREACH | ||
| 554 | if(!dev) { | ||
| 555 | usbmuxd_log(LL_WARNING, "Cannot find device entry for RX input from USB device %p on location 0x%x", usbdev, usb_get_location(usbdev)); | ||
| 556 | return; | ||
| 557 | } | ||
| 558 | |||
| 559 | if(!length) | ||
| 560 | return; | ||
| 561 | |||
| 562 | // sanity check (should never happen with current USB implementation) | ||
| 563 | if((length > USB_MRU) || (length > DEV_MRU)) { | ||
| 564 | usbmuxd_log(LL_ERROR, "Too much data received from USB (%d), file a bug", length); | ||
| 565 | return; | ||
| 566 | } | ||
| 567 | |||
| 568 | usbmuxd_log(LL_SPEW, "Mux data input for device %p: %p len %d", dev, buffer, length); | ||
| 569 | |||
| 570 | // handle broken up transfers | ||
| 571 | if(dev->pktlen) { | ||
| 572 | if((length + dev->pktlen) > DEV_MRU) { | ||
| 573 | usbmuxd_log(LL_ERROR, "Incoming split packet is too large (%d so far), dropping!", length + dev->pktlen); | ||
| 574 | dev->pktlen = 0; | ||
| 575 | return; | ||
| 576 | } | ||
| 577 | memcpy(dev->pktbuf + dev->pktlen, buffer, length); | ||
| 578 | struct mux_header *mhdr = (struct mux_header *)dev->pktbuf; | ||
| 579 | if((length < USB_MRU) || (ntohl(mhdr->length) == (length + dev->pktlen))) { | ||
| 580 | buffer = dev->pktbuf; | ||
| 581 | length += dev->pktlen; | ||
| 582 | dev->pktlen = 0; | ||
| 583 | usbmuxd_log(LL_SPEW, "Gathered mux data from buffer (total size: %d)", length); | ||
| 584 | } else { | ||
| 585 | dev->pktlen += length; | ||
| 586 | usbmuxd_log(LL_SPEW, "Appended mux data to buffer (total size: %d)", dev->pktlen); | ||
| 587 | return; | ||
| 588 | } | ||
| 589 | } else { | ||
| 590 | struct mux_header *mhdr = (struct mux_header *)buffer; | ||
| 591 | if((length == USB_MRU) && (length < ntohl(mhdr->length))) { | ||
| 592 | memcpy(dev->pktbuf, buffer, length); | ||
| 593 | dev->pktlen = length; | ||
| 594 | usbmuxd_log(LL_SPEW, "Copied mux data to buffer (size: %d)", dev->pktlen); | ||
| 595 | return; | ||
| 596 | } | ||
| 597 | } | ||
| 598 | |||
| 599 | struct mux_header *mhdr = (struct mux_header *)buffer; | ||
| 600 | |||
| 601 | if(ntohl(mhdr->length) != length) { | ||
| 602 | usbmuxd_log(LL_ERROR, "Incoming packet size mismatch (dev %d, expected %d, got %d)", dev->id, ntohl(mhdr->length), length); | ||
| 603 | return; | ||
| 604 | } | ||
| 605 | |||
| 606 | struct tcphdr *th; | ||
| 607 | unsigned char *payload; | ||
| 608 | uint32_t payload_length; | ||
| 609 | |||
| 610 | switch(ntohl(mhdr->protocol)) { | ||
| 611 | case MUX_PROTO_VERSION: | ||
| 612 | if(length < (sizeof(struct mux_header) + sizeof(struct version_header))) { | ||
| 613 | usbmuxd_log(LL_ERROR, "Incoming version packet is too small (%d)", length); | ||
| 614 | return; | ||
| 615 | } | ||
| 616 | device_version_input(dev, (struct version_header *)(mhdr+1)); | ||
| 617 | break; | ||
| 618 | case MUX_PROTO_TCP: | ||
| 619 | if(length < (sizeof(struct mux_header) + sizeof(struct tcphdr))) { | ||
| 620 | usbmuxd_log(LL_ERROR, "Incoming TCP packet is too small (%d)", length); | ||
| 621 | return; | ||
| 622 | } | ||
| 623 | th = (struct tcphdr *)(mhdr+1); | ||
| 624 | payload = (unsigned char *)(th+1); | ||
| 625 | payload_length = length - sizeof(struct tcphdr) - sizeof(struct mux_header); | ||
| 626 | device_tcp_input(dev, (struct tcphdr *)(mhdr+1), payload, payload_length); | ||
| 627 | break; | ||
| 628 | default: | ||
| 629 | usbmuxd_log(LL_ERROR, "Incoming packet for device %d has unknown protocol 0x%x)", dev->id, ntohl(mhdr->protocol)); | ||
| 630 | break; | ||
| 631 | } | ||
| 632 | |||
| 633 | } | ||
| 634 | |||
| 635 | int device_add(struct usb_device *usbdev) | ||
| 636 | { | ||
| 637 | int res; | ||
| 638 | int id = get_next_device_id(); | ||
| 639 | struct mux_device *dev; | ||
| 640 | usbmuxd_log(LL_NOTICE, "Connecting to new device on location 0x%x as ID %d", usb_get_location(usbdev), id); | ||
| 641 | dev = malloc(sizeof(struct mux_device)); | ||
| 642 | dev->id = id; | ||
| 643 | dev->usbdev = usbdev; | ||
| 644 | dev->state = MUXDEV_INIT; | ||
| 645 | dev->next_sport = 1; | ||
| 646 | dev->pktbuf = malloc(DEV_MRU); | ||
| 647 | dev->pktlen = 0; | ||
| 648 | struct version_header vh; | ||
| 649 | vh.major = htonl(1); | ||
| 650 | vh.minor = htonl(0); | ||
| 651 | vh.padding = 0; | ||
| 652 | if((res = send_packet(dev, MUX_PROTO_VERSION, &vh, NULL, 0)) < 0) { | ||
| 653 | usbmuxd_log(LL_ERROR, "Error sending version request packet to device %d", id); | ||
| 654 | free(dev); | ||
| 655 | return res; | ||
| 656 | } | ||
| 657 | collection_add(&device_list, dev); | ||
| 658 | return 0; | ||
| 659 | } | ||
| 660 | |||
| 661 | void device_remove(struct usb_device *usbdev) | ||
| 662 | { | ||
| 663 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 664 | if(dev->usbdev == usbdev) { | ||
| 665 | usbmuxd_log(LL_NOTICE, "Removed device %d on location 0x%x", dev->id, usb_get_location(usbdev)); | ||
| 666 | if(dev->state == MUXDEV_ACTIVE) { | ||
| 667 | dev->state = MUXDEV_DEAD; | ||
| 668 | FOREACH(struct mux_connection *conn, &dev->connections) { | ||
| 669 | connection_teardown(conn); | ||
| 670 | } ENDFOREACH | ||
| 671 | client_device_remove(dev->id); | ||
| 672 | collection_free(&dev->connections); | ||
| 673 | } | ||
| 674 | collection_remove(&device_list, dev); | ||
| 675 | free(dev->pktbuf); | ||
| 676 | free(dev); | ||
| 677 | return; | ||
| 678 | } | ||
| 679 | } ENDFOREACH | ||
| 680 | usbmuxd_log(LL_WARNING, "Cannot find device entry while removing USB device %p on location 0x%x", usbdev, usb_get_location(usbdev)); | ||
| 681 | } | ||
| 682 | |||
| 683 | int device_get_count(void) | ||
| 684 | { | ||
| 685 | int count = 0; | ||
| 686 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 687 | if(dev->state == MUXDEV_ACTIVE) | ||
| 688 | count++; | ||
| 689 | } ENDFOREACH | ||
| 690 | return count; | ||
| 691 | } | ||
| 692 | |||
| 693 | int device_get_list(struct device_info *p) | ||
| 694 | { | ||
| 695 | int count = 0; | ||
| 696 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 697 | if(dev->state == MUXDEV_ACTIVE) { | ||
| 698 | p->id = dev->id; | ||
| 699 | p->serial = usb_get_serial(dev->usbdev); | ||
| 700 | p->location = usb_get_location(dev->usbdev); | ||
| 701 | p->pid = usb_get_pid(dev->usbdev); | ||
| 702 | count++; | ||
| 703 | p++; | ||
| 704 | } | ||
| 705 | } ENDFOREACH | ||
| 706 | return count; | ||
| 707 | } | ||
| 708 | |||
| 709 | int device_get_timeout(void) | ||
| 710 | { | ||
| 711 | uint64_t oldest = (uint64_t)-1; | ||
| 712 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 713 | if(dev->state == MUXDEV_ACTIVE) { | ||
| 714 | FOREACH(struct mux_connection *conn, &dev->connections) { | ||
| 715 | if((conn->state == CONN_CONNECTED) && (conn->flags & CONN_ACK_PENDING) && conn->last_ack_time < oldest) | ||
| 716 | oldest = conn->last_ack_time; | ||
| 717 | } ENDFOREACH | ||
| 718 | } | ||
| 719 | } ENDFOREACH | ||
| 720 | uint64_t ct = mstime64(); | ||
| 721 | if(oldest == -1) | ||
| 722 | return 100000; //meh | ||
| 723 | if((ct - oldest) > ACK_TIMEOUT) | ||
| 724 | return 0; | ||
| 725 | return ACK_TIMEOUT - (ct - oldest); | ||
| 726 | } | ||
| 727 | |||
| 728 | void device_check_timeouts(void) | ||
| 729 | { | ||
| 730 | uint64_t ct = mstime64(); | ||
| 731 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 732 | if(dev->state == MUXDEV_ACTIVE) { | ||
| 733 | FOREACH(struct mux_connection *conn, &dev->connections) { | ||
| 734 | if((conn->state == CONN_CONNECTED) && | ||
| 735 | (conn->flags & CONN_ACK_PENDING) && | ||
| 736 | (ct - conn->last_ack_time) > ACK_TIMEOUT) { | ||
| 737 | usbmuxd_log(LL_DEBUG, "Sending ACK due to expired timeout (%" PRIu64 " -> %" PRIu64 ")", conn->last_ack_time, ct); | ||
| 738 | if(send_tcp(conn, TH_ACK, NULL, 0) < 0) { | ||
| 739 | usbmuxd_log(LL_ERROR, "Error sending TCP ACK to device %d (%d->%d)", dev->id, conn->sport, conn->dport); | ||
| 740 | connection_teardown(conn); | ||
| 741 | } | ||
| 742 | } | ||
| 743 | } ENDFOREACH | ||
| 744 | } | ||
| 745 | } ENDFOREACH | ||
| 746 | } | ||
| 747 | |||
| 748 | void device_init(void) | ||
| 749 | { | ||
| 750 | usbmuxd_log(LL_DEBUG, "device_init"); | ||
| 751 | collection_init(&device_list); | ||
| 752 | next_device_id = 1; | ||
| 753 | } | ||
| 754 | |||
| 755 | void device_kill_connections(void) | ||
| 756 | { | ||
| 757 | usbmuxd_log(LL_DEBUG, "device_kill_connections"); | ||
| 758 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 759 | if(dev->state != MUXDEV_INIT) { | ||
| 760 | FOREACH(struct mux_connection *conn, &dev->connections) { | ||
| 761 | connection_teardown(conn); | ||
| 762 | } ENDFOREACH | ||
| 763 | } | ||
| 764 | } ENDFOREACH | ||
| 765 | // give USB a while to send the final connection RSTs and the like | ||
| 766 | usb_process_timeout(100); | ||
| 767 | } | ||
| 768 | |||
| 769 | void device_shutdown(void) | ||
| 770 | { | ||
| 771 | usbmuxd_log(LL_DEBUG, "device_shutdown"); | ||
| 772 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 773 | FOREACH(struct mux_connection *conn, &dev->connections) { | ||
| 774 | connection_teardown(conn); | ||
| 775 | } ENDFOREACH | ||
| 776 | collection_free(&dev->connections); | ||
| 777 | collection_remove(&device_list, dev); | ||
| 778 | free(dev); | ||
| 779 | } ENDFOREACH | ||
| 780 | collection_free(&device_list); | ||
| 781 | } | ||
