diff options
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | client.c | 402 | ||||
| -rw-r--r-- | client.h | 69 | ||||
| -rw-r--r-- | device.c | 583 | ||||
| -rw-r--r-- | device.h | 19 | ||||
| -rw-r--r-- | log.c | 2 | ||||
| -rw-r--r-- | main.c | 61 | ||||
| -rw-r--r-- | usb-linux.c | 197 | ||||
| -rw-r--r-- | usb.h | 21 | ||||
| -rw-r--r-- | utils.c | 53 | ||||
| -rw-r--r-- | utils.h | 22 |
11 files changed, 1301 insertions, 130 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index cd4a427..e0e4e3f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
| @@ -10,7 +10,7 @@ set(LIBS ${LIBS} ${USB_LIBRARIES}) | |||
| 10 | 10 | ||
| 11 | #set(CMAKE_VERBOSE_MAKEFILE ON) | 11 | #set(CMAKE_VERBOSE_MAKEFILE ON) |
| 12 | 12 | ||
| 13 | add_definitions(-Wall -O2) | 13 | add_definitions(-Wall -O0 -g) |
| 14 | add_executable(usbmuxd main.c usb-linux.c log.c utils.c device.c client.c) | 14 | add_executable(usbmuxd main.c usb-linux.c log.c utils.c device.c client.c) |
| 15 | target_link_libraries(usbmuxd ${LIBS}) | 15 | target_link_libraries(usbmuxd ${LIBS}) |
| 16 | 16 | ||
| @@ -22,7 +22,409 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
| 22 | #include <config.h> | 22 | #include <config.h> |
| 23 | #endif | 23 | #endif |
| 24 | 24 | ||
| 25 | #include <stdlib.h> | ||
| 26 | #include <errno.h> | ||
| 27 | #include <unistd.h> | ||
| 28 | #include <sys/types.h> | ||
| 29 | #include <sys/socket.h> | ||
| 30 | #include <sys/un.h> | ||
| 31 | #include <arpa/inet.h> | ||
| 32 | |||
| 25 | #include "log.h" | 33 | #include "log.h" |
| 26 | #include "usb.h" | 34 | #include "usb.h" |
| 27 | #include "client.h" | 35 | #include "client.h" |
| 36 | #include "device.h" | ||
| 37 | |||
| 38 | #define CMD_BUF_SIZE 256 | ||
| 39 | #define REPLY_BUF_SIZE 1024 | ||
| 40 | |||
| 41 | enum client_state { | ||
| 42 | CLIENT_COMMAND, // waiting for command | ||
| 43 | CLIENT_LISTEN, // listening for devices | ||
| 44 | CLIENT_CONNECTING1, // issued connection request | ||
| 45 | CLIENT_CONNECTING2, // connection established, but waiting for response message to get sent | ||
| 46 | CLIENT_CONNECTED, // connected | ||
| 47 | CLIENT_DEAD | ||
| 48 | }; | ||
| 49 | |||
| 50 | struct mux_client { | ||
| 51 | int fd; | ||
| 52 | unsigned char *ob_buf; | ||
| 53 | int ob_size; | ||
| 54 | int ob_capacity; | ||
| 55 | unsigned char *ib_buf; | ||
| 56 | int ib_size; | ||
| 57 | int ib_capacity; | ||
| 58 | short events, devents; | ||
| 59 | uint32_t connect_tag; | ||
| 60 | int connect_device; | ||
| 61 | enum client_state state; | ||
| 62 | }; | ||
| 63 | |||
| 64 | static struct collection client_list; | ||
| 65 | |||
| 66 | int client_read(struct mux_client *client, void *buffer, int len) | ||
| 67 | { | ||
| 68 | usbmuxd_log(LL_SPEW, "client_read fd %d buf %p len %d", client->fd, buffer, len); | ||
| 69 | if(client->state != CLIENT_CONNECTED) { | ||
| 70 | usbmuxd_log(LL_ERROR, "Attempted to read from client %d not in CONNECTED state", client->fd); | ||
| 71 | return -1; | ||
| 72 | } | ||
| 73 | return recv(client->fd, buffer, len, 0); | ||
| 74 | } | ||
| 75 | |||
| 76 | int client_write(struct mux_client *client, void *buffer, int len) | ||
| 77 | { | ||
| 78 | usbmuxd_log(LL_SPEW, "client_write fd %d buf %p len %d", client->fd, buffer, len); | ||
| 79 | if(client->state != CLIENT_CONNECTED) { | ||
| 80 | usbmuxd_log(LL_ERROR, "Attempted to write to client %d not in CONNECTED state", client->fd); | ||
| 81 | return -1; | ||
| 82 | } | ||
| 83 | return send(client->fd, buffer, len, 0); | ||
| 84 | } | ||
| 85 | |||
| 86 | int client_set_events(struct mux_client *client, short events) | ||
| 87 | { | ||
| 88 | if((client->state != CLIENT_CONNECTED) && (client->state != CLIENT_CONNECTING2)) { | ||
| 89 | usbmuxd_log(LL_ERROR, "client_set_events to client %d not in CONNECTED state", client->fd); | ||
| 90 | return -1; | ||
| 91 | } | ||
| 92 | client->devents = events; | ||
| 93 | if(client->state == CLIENT_CONNECTED) | ||
| 94 | client->events = events; | ||
| 95 | return 0; | ||
| 96 | } | ||
| 97 | |||
| 98 | int client_accept(int listenfd) | ||
| 99 | { | ||
| 100 | struct sockaddr_un addr; | ||
| 101 | int cfd; | ||
| 102 | socklen_t len = sizeof(struct sockaddr_un); | ||
| 103 | cfd = accept(listenfd, (struct sockaddr *)&addr, &len); | ||
| 104 | if (cfd < 0) { | ||
| 105 | usbmuxd_log(LL_ERROR, "accept() failed (%s)", strerror(errno)); | ||
| 106 | return cfd; | ||
| 107 | } | ||
| 108 | |||
| 109 | struct mux_client *client; | ||
| 110 | client = malloc(sizeof(struct mux_client)); | ||
| 111 | memset(client, 0, sizeof(struct mux_client)); | ||
| 112 | |||
| 113 | client->fd = cfd; | ||
| 114 | client->ob_buf = malloc(REPLY_BUF_SIZE); | ||
| 115 | client->ob_size = 0; | ||
| 116 | client->ob_capacity = REPLY_BUF_SIZE; | ||
| 117 | client->ib_buf = malloc(CMD_BUF_SIZE); | ||
| 118 | client->ib_size = 0; | ||
| 119 | client->ib_capacity = CMD_BUF_SIZE; | ||
| 120 | client->state = CLIENT_COMMAND; | ||
| 121 | client->events = POLLIN; | ||
| 122 | |||
| 123 | collection_add(&client_list, client); | ||
| 124 | |||
| 125 | usbmuxd_log(LL_INFO, "New client on fd %d", client->fd); | ||
| 126 | return client->fd; | ||
| 127 | } | ||
| 128 | |||
| 129 | void client_close(struct mux_client *client) | ||
| 130 | { | ||
| 131 | usbmuxd_log(LL_INFO, "Disconnecting client fd %d", client->fd); | ||
| 132 | if(client->state == CLIENT_CONNECTING1 || client->state == CLIENT_CONNECTING2) { | ||
| 133 | usbmuxd_log(LL_INFO, "Client died mid-connect, aborting device %d connection", client->connect_device); | ||
| 134 | client->state = CLIENT_DEAD; | ||
| 135 | device_abort_connect(client->connect_device, client); | ||
| 136 | } | ||
| 137 | close(client->fd); | ||
| 138 | if(client->ob_buf) | ||
| 139 | free(client->ob_buf); | ||
| 140 | if(client->ib_buf) | ||
| 141 | free(client->ib_buf); | ||
| 142 | collection_remove(&client_list, client); | ||
| 143 | free(client); | ||
| 144 | } | ||
| 145 | |||
| 146 | void client_get_fds(struct fdlist *list) | ||
| 147 | { | ||
| 148 | FOREACH(struct mux_client *client, &client_list) { | ||
| 149 | fdlist_add(list, FD_CLIENT, client->fd, client->events); | ||
| 150 | } ENDFOREACH | ||
| 151 | } | ||
| 152 | |||
| 153 | static int send_pkt(struct mux_client *client, uint32_t tag, enum client_msgtype msg, void *payload, int payload_length) | ||
| 154 | { | ||
| 155 | struct client_header hdr; | ||
| 156 | hdr.version = CLIENT_PROTOCOL_VERSION; | ||
| 157 | hdr.length = sizeof(hdr) + payload_length; | ||
| 158 | hdr.message = msg; | ||
| 159 | hdr.tag = tag; | ||
| 160 | usbmuxd_log(LL_DEBUG, "send_pkt fd %d tag %d msg %d payload_length %d", client->fd, tag, msg, payload_length); | ||
| 161 | if((client->ob_capacity - client->ob_size) < hdr.length) { | ||
| 162 | usbmuxd_log(LL_ERROR, "Client %d output buffer full (%d bytes) while sending message %d (%d bytes)", client->fd, client->ob_capacity, hdr.message, hdr.length); | ||
| 163 | client_close(client); | ||
| 164 | return -1; | ||
| 165 | } | ||
| 166 | memcpy(client->ob_buf + client->ob_size, &hdr, sizeof(hdr)); | ||
| 167 | if(payload && payload_length) | ||
| 168 | memcpy(client->ob_buf + client->ob_size + sizeof(hdr), payload, payload_length); | ||
| 169 | client->ob_size += hdr.length; | ||
| 170 | client->events |= POLLOUT; | ||
| 171 | return hdr.length; | ||
| 172 | } | ||
| 173 | |||
| 174 | static int send_result(struct mux_client *client, uint32_t tag, uint32_t result) | ||
| 175 | { | ||
| 176 | return send_pkt(client, tag, MESSAGE_RESULT, &result, sizeof(uint32_t)); | ||
| 177 | } | ||
| 178 | |||
| 179 | int client_notify_connect(struct mux_client *client, enum client_result result) | ||
| 180 | { | ||
| 181 | usbmuxd_log(LL_SPEW, "client_notify_connect fd %d result %d", client->fd, result); | ||
| 182 | if(client->state == CLIENT_DEAD) | ||
| 183 | return -1; | ||
| 184 | if(client->state != CLIENT_CONNECTING1) { | ||
| 185 | usbmuxd_log(LL_ERROR, "client_notify_connect when client %d is not in CONNECTING1 state", client->fd); | ||
| 186 | return -1; | ||
| 187 | } | ||
| 188 | if(send_result(client, client->connect_tag, result) < 0) | ||
| 189 | return -1; | ||
| 190 | if(result == RESULT_OK) { | ||
| 191 | client->state = CLIENT_CONNECTING2; | ||
| 192 | client->events = POLLOUT; // wait for the result packet to go through | ||
| 193 | // no longer need this | ||
| 194 | free(client->ib_buf); | ||
| 195 | client->ib_buf = NULL; | ||
| 196 | } else { | ||
| 197 | client->state = CLIENT_COMMAND; | ||
| 198 | } | ||
| 199 | return 0; | ||
| 200 | } | ||
| 201 | |||
| 202 | static int notify_device(struct mux_client *client, struct device_info *dev) | ||
| 203 | { | ||
| 204 | struct client_msg_dev dmsg; | ||
| 205 | memset(&dmsg, 0, sizeof(dmsg)); | ||
| 206 | dmsg.device_id = dev->id; | ||
| 207 | strncpy(dmsg.device_serial, dev->serial, 256); | ||
| 208 | dmsg.device_serial[255] = 0; | ||
| 209 | dmsg.location = dev->location; | ||
| 210 | dmsg.device_pid = dev->pid; | ||
| 211 | return send_pkt(client, 0, MESSAGE_DEVICE_ADD, &dmsg, sizeof(dmsg)); | ||
| 212 | } | ||
| 213 | |||
| 214 | static int start_listen(struct mux_client *client) | ||
| 215 | { | ||
| 216 | struct device_info *devs; | ||
| 217 | struct device_info *dev; | ||
| 218 | int count, i; | ||
| 219 | |||
| 220 | client->state = CLIENT_LISTEN; | ||
| 221 | count = device_get_count(); | ||
| 222 | if(!count) | ||
| 223 | return 0; | ||
| 224 | devs = malloc(sizeof(struct device_info) * count); | ||
| 225 | count = device_get_list(devs); | ||
| 226 | |||
| 227 | // going to need a larger buffer for many devices | ||
| 228 | int needed_buffer = count * (sizeof(struct client_msg_dev) + sizeof(struct client_header)) + REPLY_BUF_SIZE; | ||
| 229 | if(client->ob_capacity < needed_buffer) { | ||
| 230 | usbmuxd_log(LL_DEBUG, "Enlarging client %d reply buffer %d -> %d to make space for device notifications", client->fd, client->ob_capacity, needed_buffer); | ||
| 231 | client->ob_buf = realloc(client->ob_buf, needed_buffer); | ||
| 232 | client->ob_capacity = needed_buffer; | ||
| 233 | } | ||
| 234 | dev = devs; | ||
| 235 | for(i=0; i<count; i++) { | ||
| 236 | if(notify_device(client, dev++) < 0) { | ||
| 237 | free(devs); | ||
| 238 | return -1; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | free(devs); | ||
| 242 | return count; | ||
| 243 | } | ||
| 244 | |||
| 245 | static int client_command(struct mux_client *client, struct client_header *hdr, const char *payload) | ||
| 246 | { | ||
| 247 | int res; | ||
| 248 | usbmuxd_log(LL_DEBUG, "Client command in fd %d len %d ver %d msg %d tag %d", client->fd, hdr->length, hdr->version, hdr->message, hdr->tag); | ||
| 249 | |||
| 250 | if(client->state != CLIENT_COMMAND) { | ||
| 251 | usbmuxd_log(LL_ERROR, "Client %d command received in the wrong state", client->fd); | ||
| 252 | if(send_result(client, hdr->tag, RESULT_BADCOMMAND) < 0) | ||
| 253 | return -1; | ||
| 254 | client_close(client); | ||
| 255 | return -1; | ||
| 256 | } | ||
| 257 | |||
| 258 | struct client_msg_connect *ch; | ||
| 259 | switch(hdr->message) { | ||
| 260 | case MESSAGE_LISTEN: | ||
| 261 | if(send_result(client, hdr->tag, 0) < 0) | ||
| 262 | return -1; | ||
| 263 | usbmuxd_log(LL_DEBUG, "Client %d now LISTENING", client->fd); | ||
| 264 | return start_listen(client); | ||
| 265 | case MESSAGE_CONNECT: | ||
| 266 | ch = (void*)payload; | ||
| 267 | usbmuxd_log(LL_DEBUG, "Client %d connection request to device %d port %d", client->fd, ch->device_id, ntohs(ch->port)); | ||
| 268 | res = device_start_connect(ch->device_id, ntohs(ch->port), client); | ||
| 269 | if(res < 0) { | ||
| 270 | if(send_result(client, hdr->tag, -res) < 0) | ||
| 271 | return -1; | ||
| 272 | } else { | ||
| 273 | client->connect_tag = hdr->tag; | ||
| 274 | client->connect_device = ch->device_id; | ||
| 275 | client->state = CLIENT_CONNECTING1; | ||
| 276 | } | ||
| 277 | return 0; | ||
| 278 | default: | ||
| 279 | usbmuxd_log(LL_ERROR, "Client %d invalid command %d", client->fd, hdr->message); | ||
| 280 | if(send_result(client, hdr->tag, RESULT_BADCOMMAND) < 0) | ||
| 281 | return -1; | ||
| 282 | return 0; | ||
| 283 | } | ||
| 284 | return -1; | ||
| 285 | } | ||
| 286 | |||
| 287 | static void process_send(struct mux_client *client) | ||
| 288 | { | ||
| 289 | int res; | ||
| 290 | if(!client->ob_size) { | ||
| 291 | usbmuxd_log(LL_WARNING, "Client %d OUT process but nothing to send?", client->fd); | ||
| 292 | client->events &= ~POLLOUT; | ||
| 293 | return; | ||
| 294 | } | ||
| 295 | res = send(client->fd, client->ob_buf, client->ob_size, 0); | ||
| 296 | if(res <= 0) { | ||
| 297 | usbmuxd_log(LL_ERROR, "Send to client fd %d failed: %d %s", client->fd, res, strerror(errno)); | ||
| 298 | client_close(client); | ||
| 299 | return; | ||
| 300 | } | ||
| 301 | if(res == client->ob_size) { | ||
| 302 | client->ob_size = 0; | ||
| 303 | client->events &= ~POLLOUT; | ||
| 304 | if(client->state == CLIENT_CONNECTING2) { | ||
| 305 | usbmuxd_log(LL_DEBUG, "Client %d switching to CONNECTED state", client->fd); | ||
| 306 | client->state = CLIENT_CONNECTED; | ||
| 307 | client->events = client->devents; | ||
| 308 | // no longer need this | ||
| 309 | free(client->ob_buf); | ||
| 310 | client->ob_buf = NULL; | ||
| 311 | } | ||
| 312 | } else { | ||
| 313 | client->ob_size -= res; | ||
| 314 | memmove(client->ob_buf, client->ob_buf + res, client->ob_size); | ||
| 315 | } | ||
| 316 | } | ||
| 317 | static void process_recv(struct mux_client *client) | ||
| 318 | { | ||
| 319 | int res; | ||
| 320 | int did_read = 0; | ||
| 321 | if(client->ib_size < sizeof(struct client_header)) { | ||
| 322 | res = recv(client->fd, client->ib_buf + client->ib_size, sizeof(struct client_header) - client->ib_size, 0); | ||
| 323 | if(res <= 0) { | ||
| 324 | if(res < 0) | ||
| 325 | usbmuxd_log(LL_ERROR, "Receive from client fd %d failed: %s", client->fd, strerror(errno)); | ||
| 326 | else | ||
| 327 | usbmuxd_log(LL_INFO, "Client %d connection closed", client->fd); | ||
| 328 | client_close(client); | ||
| 329 | return; | ||
| 330 | } | ||
| 331 | client->ib_size += res; | ||
| 332 | if(client->ib_size < sizeof(struct client_header)) | ||
| 333 | return; | ||
| 334 | did_read = 1; | ||
| 335 | } | ||
| 336 | struct client_header *hdr = (void*)client->ib_buf; | ||
| 337 | if(hdr->version != CLIENT_PROTOCOL_VERSION) { | ||
| 338 | usbmuxd_log(LL_INFO, "Client %d version mismatch: expected %d, got %d", client->fd, CLIENT_PROTOCOL_VERSION, hdr->version); | ||
| 339 | client_close(client); | ||
| 340 | } | ||
| 341 | if(hdr->length > client->ib_capacity) { | ||
| 342 | usbmuxd_log(LL_INFO, "Client %d message is too long (%d bytes)", client->fd, hdr->length); | ||
| 343 | client_close(client); | ||
| 344 | } | ||
| 345 | if(hdr->length < sizeof(struct client_header)) { | ||
| 346 | usbmuxd_log(LL_ERROR, "Client %d message is too short (%d bytes)", client->fd, hdr->length); | ||
| 347 | client_close(client); | ||
| 348 | } | ||
| 349 | if(client->ib_size < hdr->length) { | ||
| 350 | if(did_read) | ||
| 351 | return; //maybe we would block, so defer to next loop | ||
| 352 | res = recv(client->fd, client->ib_buf + client->ib_size, hdr->length - client->ib_size, 0); | ||
| 353 | if(res < 0) { | ||
| 354 | usbmuxd_log(LL_ERROR, "Receive from client fd %d failed: %s", client->fd, strerror(errno)); | ||
| 355 | client_close(client); | ||
| 356 | return; | ||
| 357 | } else if(res == 0) { | ||
| 358 | usbmuxd_log(LL_INFO, "Client %d connection closed", client->fd); | ||
| 359 | client_close(client); | ||
| 360 | return; | ||
| 361 | } | ||
| 362 | client->ib_size += res; | ||
| 363 | if(client->ib_size < hdr->length) | ||
| 364 | return; | ||
| 365 | } | ||
| 366 | client_command(client, hdr, (char *)(hdr+1)); | ||
| 367 | client->ib_size = 0; | ||
| 368 | } | ||
| 369 | |||
| 370 | void client_process(int fd, short events) | ||
| 371 | { | ||
| 372 | struct mux_client *client = NULL; | ||
| 373 | FOREACH(struct mux_client *lc, &client_list) { | ||
| 374 | if(lc->fd == fd) { | ||
| 375 | client = lc; | ||
| 376 | break; | ||
| 377 | } | ||
| 378 | } ENDFOREACH | ||
| 379 | |||
| 380 | if(!client) { | ||
| 381 | usbmuxd_log(LL_ERROR, "client_process: fd %d not found in client list", fd); | ||
| 382 | return; | ||
| 383 | } | ||
| 384 | |||
| 385 | if(client->state == CLIENT_CONNECTED) { | ||
| 386 | usbmuxd_log(LL_SPEW, "client_process in CONNECTED state"); | ||
| 387 | device_client_process(client->connect_device, client, events); | ||
| 388 | } else { | ||
| 389 | if(events & POLLIN) { | ||
| 390 | process_recv(client); | ||
| 391 | } else if(events & POLLOUT) { //not both in case client died as part of process_recv | ||
| 392 | process_send(client); | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 | } | ||
| 397 | |||
| 398 | void client_device_add(struct device_info *dev) | ||
| 399 | { | ||
| 400 | usbmuxd_log(LL_DEBUG, "client_device_add: id %d, location 0x%x, serial %s", dev->id, dev->location, dev->serial); | ||
| 401 | FOREACH(struct mux_client *client, &client_list) { | ||
| 402 | if(client->state == CLIENT_LISTEN) | ||
| 403 | notify_device(client, dev); | ||
| 404 | } ENDFOREACH | ||
| 405 | } | ||
| 406 | void client_device_remove(int device_id) | ||
| 407 | { | ||
| 408 | uint32_t id = device_id; | ||
| 409 | usbmuxd_log(LL_DEBUG, "client_device_remove: id %d", device_id); | ||
| 410 | FOREACH(struct mux_client *client, &client_list) { | ||
| 411 | if(client->state == CLIENT_LISTEN) | ||
| 412 | send_pkt(client, 0, MESSAGE_DEVICE_REMOVE, &id, sizeof(uint32_t)); | ||
| 413 | } ENDFOREACH | ||
| 414 | } | ||
| 415 | |||
| 416 | |||
| 417 | void client_init(void) | ||
| 418 | { | ||
| 419 | usbmuxd_log(LL_DEBUG, "client_init"); | ||
| 420 | collection_init(&client_list); | ||
| 421 | } | ||
| 28 | 422 | ||
| 423 | void client_shutdown(void) | ||
| 424 | { | ||
| 425 | usbmuxd_log(LL_DEBUG, "client_shutdown"); | ||
| 426 | FOREACH(struct mux_client *client, &client_list) { | ||
| 427 | client_close(client); | ||
| 428 | } ENDFOREACH | ||
| 429 | collection_free(&client_list); | ||
| 430 | } | ||
| @@ -21,7 +21,72 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
| 21 | #ifndef __CLIENT_H__ | 21 | #ifndef __CLIENT_H__ |
| 22 | #define __CLIENT_H__ | 22 | #define __CLIENT_H__ |
| 23 | 23 | ||
| 24 | #endif | 24 | #include <stdint.h> |
| 25 | |||
| 26 | struct device_info; | ||
| 27 | struct mux_client; | ||
| 28 | |||
| 29 | enum client_result { | ||
| 30 | RESULT_OK = 0, | ||
| 31 | RESULT_BADCOMMAND = 1, | ||
| 32 | RESULT_BADDEV = 2, | ||
| 33 | RESULT_CONNREFUSED = 3, | ||
| 34 | // ??? | ||
| 35 | // ??? | ||
| 36 | RESULT_BADVERSION = 6, | ||
| 37 | }; | ||
| 38 | |||
| 39 | enum client_msgtype { | ||
| 40 | MESSAGE_RESULT = 1, | ||
| 41 | MESSAGE_CONNECT = 2, | ||
| 42 | MESSAGE_LISTEN = 3, | ||
| 43 | MESSAGE_DEVICE_ADD = 4, | ||
| 44 | MESSAGE_DEVICE_REMOVE = 5, | ||
| 45 | //??? | ||
| 46 | //??? | ||
| 47 | //MESSAGE_PLIST = 8, | ||
| 48 | }; | ||
| 49 | |||
| 50 | #define CLIENT_PROTOCOL_VERSION 0 | ||
| 51 | |||
| 52 | struct client_header { | ||
| 53 | uint32_t length; | ||
| 54 | uint32_t version; | ||
| 55 | uint32_t message; | ||
| 56 | uint32_t tag; | ||
| 57 | }; | ||
| 58 | |||
| 59 | struct client_msg_result { | ||
| 60 | uint32_t result; | ||
| 61 | }; | ||
| 25 | 62 | ||
| 26 | void client_accept(int fd); | 63 | struct client_msg_connect { |
| 64 | uint32_t device_id; | ||
| 65 | uint16_t port; | ||
| 66 | }; | ||
| 67 | |||
| 68 | struct client_msg_dev { | ||
| 69 | uint32_t device_id; | ||
| 70 | uint16_t device_pid; | ||
| 71 | char device_serial[256]; | ||
| 72 | uint16_t padding; | ||
| 73 | uint32_t location; | ||
| 74 | }; | ||
| 75 | |||
| 76 | int client_read(struct mux_client *client, void *buffer, int len); | ||
| 77 | int client_write(struct mux_client *client, void *buffer, int len); | ||
| 78 | int client_set_events(struct mux_client *client, short events); | ||
| 79 | void client_close(struct mux_client *client); | ||
| 80 | int client_notify_connect(struct mux_client *client, enum client_result result); | ||
| 81 | |||
| 82 | void client_device_add(struct device_info *dev); | ||
| 83 | void client_device_remove(int device_id); | ||
| 84 | |||
| 85 | int client_accept(int fd); | ||
| 27 | void client_get_fds(struct fdlist *list); | 86 | void client_get_fds(struct fdlist *list); |
| 87 | void client_process(int fd, short events); | ||
| 88 | |||
| 89 | void client_init(void); | ||
| 90 | void client_shutdown(void); | ||
| 91 | |||
| 92 | #endif | ||
| @@ -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 | ||
| 35 | int next_device_id; | 39 | int 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 | |||
| 37 | enum mux_protocol { | 48 | enum 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 | ||
| 42 | enum mux_dev_state { | 53 | enum 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 | |||
| 59 | enum 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 | ||
| 48 | struct mux_header | 66 | struct mux_header |
| @@ -58,45 +76,67 @@ struct version_header | |||
| 58 | uint32_t padding; | 76 | uint32_t padding; |
| 59 | }; | 77 | }; |
| 60 | 78 | ||
| 79 | struct mux_device; | ||
| 80 | |||
| 81 | #define CONN_ACK_PENDING 1 | ||
| 82 | |||
| 83 | struct 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 | |||
| 61 | struct mux_device | 103 | struct 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 | ||
| 68 | static int num_devs; | 114 | static struct collection device_list; |
| 69 | static struct mux_device *device_list; | ||
| 70 | 115 | ||
| 71 | static int alloc_device(void) | 116 | uint64_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 | ||
| 84 | static int get_next_device_id(void) | 123 | static 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 | ||
| 99 | int send_packet(struct mux_device *dev, enum mux_protocol proto, void *header, void *data, int length) | 139 | static 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 | |||
| 181 | static 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 | |||
| 200 | static 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 | |||
| 224 | static 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 | |||
| 251 | int 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 | |||
| 306 | static 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 | |||
| 334 | void 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 | |||
| 390 | static 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 | |||
| 403 | void 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 | ||
| 141 | static void device_version_input(struct mux_device *dev, struct version_header *vh) | 420 | static 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 | ||
| 156 | static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned char *payload, int payload_length) | 445 | static 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 | |||
| 162 | void device_data_input(struct usb_device *usbdev, unsigned char *buffer, int length) | 513 | void 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 | ||
| 207 | int device_add(struct usb_device *dev) | 584 | int 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 | ||
| 229 | void device_remove(struct usb_device *dev) | 610 | void 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 | |||
| 632 | int 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 | |||
| 642 | int 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 | |||
| 658 | int 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 | |||
| 677 | void 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 | ||
| 242 | void device_init(void) | 697 | void 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 | ||
| 704 | void 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 | |||
| 251 | void device_shutdown(void) | 718 | void 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 | } |
| @@ -22,12 +22,31 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
| 22 | #define __DEVICE_H__ | 22 | #define __DEVICE_H__ |
| 23 | 23 | ||
| 24 | #include "usb.h" | 24 | #include "usb.h" |
| 25 | #include "client.h" | ||
| 26 | |||
| 27 | struct device_info { | ||
| 28 | int id; | ||
| 29 | const char *serial; | ||
| 30 | uint32_t location; | ||
| 31 | uint16_t pid; | ||
| 32 | }; | ||
| 25 | 33 | ||
| 26 | void device_data_input(struct usb_device *dev, unsigned char *buf, int length); | 34 | void device_data_input(struct usb_device *dev, unsigned char *buf, int length); |
| 27 | 35 | ||
| 28 | int device_add(struct usb_device *dev); | 36 | int device_add(struct usb_device *dev); |
| 29 | void device_remove(struct usb_device *dev); | 37 | void device_remove(struct usb_device *dev); |
| 30 | 38 | ||
| 39 | int device_start_connect(int device_id, uint16_t port, struct mux_client *client); | ||
| 40 | void device_client_process(int device_id, struct mux_client *client, short events); | ||
| 41 | void device_abort_connect(int device_id, struct mux_client *client); | ||
| 42 | |||
| 43 | int device_get_count(void); | ||
| 44 | int device_get_list(struct device_info *p); | ||
| 45 | |||
| 46 | int device_get_timeout(void); | ||
| 47 | void device_check_timeouts(void); | ||
| 48 | |||
| 31 | void device_init(void); | 49 | void device_init(void); |
| 50 | void device_kill_connections(void); | ||
| 32 | void device_shutdown(void); | 51 | void device_shutdown(void); |
| 33 | #endif | 52 | #endif |
| @@ -31,7 +31,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
| 31 | 31 | ||
| 32 | #include "log.h" | 32 | #include "log.h" |
| 33 | 33 | ||
| 34 | int log_level = LL_SPEW; | 34 | int log_level = LL_INFO; |
| 35 | 35 | ||
| 36 | void usbmuxd_log(enum loglevel level, const char *fmt, ...) | 36 | void usbmuxd_log(enum loglevel level, const char *fmt, ...) |
| 37 | { | 37 | { |
| @@ -28,6 +28,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
| 28 | #include <errno.h> | 28 | #include <errno.h> |
| 29 | #include <string.h> | 29 | #include <string.h> |
| 30 | #include <stdlib.h> | 30 | #include <stdlib.h> |
| 31 | #include <signal.h> | ||
| 31 | #include <unistd.h> | 32 | #include <unistd.h> |
| 32 | #include <sys/socket.h> | 33 | #include <sys/socket.h> |
| 33 | #include <sys/un.h> | 34 | #include <sys/un.h> |
| @@ -35,8 +36,12 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
| 35 | #include "log.h" | 36 | #include "log.h" |
| 36 | #include "usb.h" | 37 | #include "usb.h" |
| 37 | #include "device.h" | 38 | #include "device.h" |
| 39 | #include "client.h" | ||
| 38 | 40 | ||
| 39 | static const char *socket_path = "/tmp/usbmuxd"; //TODO: CHANGEME | 41 | static const char *socket_path = "/tmp/usbmuxd"; //TODO: CHANGEME |
| 42 | int should_exit; | ||
| 43 | |||
| 44 | struct sigaction sa_old; | ||
| 40 | 45 | ||
| 41 | int create_socket(void) { | 46 | int create_socket(void) { |
| 42 | struct sockaddr_un bind_addr; | 47 | struct sockaddr_un bind_addr; |
| @@ -70,29 +75,61 @@ int create_socket(void) { | |||
| 70 | return listenfd; | 75 | return listenfd; |
| 71 | } | 76 | } |
| 72 | 77 | ||
| 78 | void handle_signal(int sig) | ||
| 79 | { | ||
| 80 | if(sig == SIGINT) { | ||
| 81 | usbmuxd_log(LL_NOTICE,"Caught SIGINT"); | ||
| 82 | } else { | ||
| 83 | usbmuxd_log(LL_NOTICE,"Caught unknown signal %d", sig); | ||
| 84 | } | ||
| 85 | should_exit = 1; | ||
| 86 | sigaction(SIGINT, &sa_old, NULL); | ||
| 87 | } | ||
| 88 | |||
| 89 | void set_signal_handlers(void) | ||
| 90 | { | ||
| 91 | struct sigaction sa; | ||
| 92 | memset(&sa, 0, sizeof(struct sigaction)); | ||
| 93 | sa.sa_handler = handle_signal; | ||
| 94 | sigaction(SIGINT, &sa, &sa_old); | ||
| 95 | } | ||
| 96 | |||
| 73 | int main_loop(int listenfd) | 97 | int main_loop(int listenfd) |
| 74 | { | 98 | { |
| 75 | int to, cnt, i; | 99 | int to, cnt, i, dto; |
| 76 | struct fdlist pollfds; | 100 | struct fdlist pollfds; |
| 77 | 101 | ||
| 78 | while(1) { | 102 | while(!should_exit) { |
| 79 | usbmuxd_log(LL_FLOOD, "main_loop iteration"); | 103 | usbmuxd_log(LL_FLOOD, "main_loop iteration"); |
| 80 | to = usb_get_timeout(); | 104 | to = usb_get_timeout(); |
| 81 | usbmuxd_log(LL_FLOOD, "USB timeout is %d ms", to); | 105 | usbmuxd_log(LL_FLOOD, "USB timeout is %d ms", to); |
| 106 | dto = device_get_timeout(); | ||
| 107 | usbmuxd_log(LL_FLOOD, "Device timeout is %d ms", to); | ||
| 108 | if(dto < to) | ||
| 109 | to = dto; | ||
| 82 | 110 | ||
| 83 | fdlist_create(&pollfds); | 111 | fdlist_create(&pollfds); |
| 84 | fdlist_add(&pollfds, FD_LISTEN, listenfd, POLLIN); | 112 | fdlist_add(&pollfds, FD_LISTEN, listenfd, POLLIN); |
| 85 | usb_get_fds(&pollfds); | 113 | usb_get_fds(&pollfds); |
| 114 | client_get_fds(&pollfds); | ||
| 86 | usbmuxd_log(LL_FLOOD, "fd count is %d", pollfds.count); | 115 | usbmuxd_log(LL_FLOOD, "fd count is %d", pollfds.count); |
| 87 | 116 | ||
| 88 | cnt = poll(pollfds.fds, pollfds.count, to); | 117 | cnt = poll(pollfds.fds, pollfds.count, to); |
| 89 | usbmuxd_log(LL_FLOOD, "poll() returned %d", cnt); | 118 | usbmuxd_log(LL_FLOOD, "poll() returned %d", cnt); |
| 90 | 119 | ||
| 91 | if(cnt == 0) { | 120 | if(cnt == -1) { |
| 121 | if(errno == EINTR && should_exit) { | ||
| 122 | usbmuxd_log(LL_INFO, "event processing interrupted"); | ||
| 123 | fdlist_free(&pollfds); | ||
| 124 | return 0; | ||
| 125 | } | ||
| 126 | } else if(cnt == 0) { | ||
| 92 | if(usb_process() < 0) { | 127 | if(usb_process() < 0) { |
| 93 | usbmuxd_log(LL_FATAL, "usb_process() failed"); | 128 | usbmuxd_log(LL_FATAL, "usb_process() failed"); |
| 129 | fdlist_free(&pollfds); | ||
| 94 | return -1; | 130 | return -1; |
| 95 | } | 131 | } |
| 132 | device_check_timeouts(); | ||
| 96 | } else { | 133 | } else { |
| 97 | int done_usb = 0; | 134 | int done_usb = 0; |
| 98 | for(i=0; i<pollfds.count; i++) { | 135 | for(i=0; i<pollfds.count; i++) { |
| @@ -100,15 +137,27 @@ int main_loop(int listenfd) | |||
| 100 | if(!done_usb && pollfds.owners[i] == FD_USB) { | 137 | if(!done_usb && pollfds.owners[i] == FD_USB) { |
| 101 | if(usb_process() < 0) { | 138 | if(usb_process() < 0) { |
| 102 | usbmuxd_log(LL_FATAL, "usb_process() failed"); | 139 | usbmuxd_log(LL_FATAL, "usb_process() failed"); |
| 140 | fdlist_free(&pollfds); | ||
| 103 | return -1; | 141 | return -1; |
| 104 | } | 142 | } |
| 105 | done_usb = 1; | 143 | done_usb = 1; |
| 106 | } | 144 | } |
| 145 | if(pollfds.owners[i] == FD_LISTEN) { | ||
| 146 | if(client_accept(listenfd) < 0) { | ||
| 147 | usbmuxd_log(LL_FATAL, "client_accept() failed"); | ||
| 148 | fdlist_free(&pollfds); | ||
| 149 | return -1; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | if(pollfds.owners[i] == FD_CLIENT) { | ||
| 153 | client_process(pollfds.fds[i].fd, pollfds.fds[i].revents); | ||
| 154 | } | ||
| 107 | } | 155 | } |
| 108 | } | 156 | } |
| 109 | } | 157 | } |
| 110 | fdlist_free(&pollfds); | 158 | fdlist_free(&pollfds); |
| 111 | } | 159 | } |
| 160 | return 0; | ||
| 112 | } | 161 | } |
| 113 | 162 | ||
| 114 | int main(int argc, char *argv[]) | 163 | int main(int argc, char *argv[]) |
| @@ -117,12 +166,16 @@ int main(int argc, char *argv[]) | |||
| 117 | int res; | 166 | int res; |
| 118 | 167 | ||
| 119 | usbmuxd_log(LL_NOTICE, "usbmux v0.1 starting up"); | 168 | usbmuxd_log(LL_NOTICE, "usbmux v0.1 starting up"); |
| 169 | should_exit = 0; | ||
| 170 | |||
| 171 | set_signal_handlers(); | ||
| 120 | 172 | ||
| 121 | usbmuxd_log(LL_INFO, "Creating socket"); | 173 | usbmuxd_log(LL_INFO, "Creating socket"); |
| 122 | listenfd = create_socket(); | 174 | listenfd = create_socket(); |
| 123 | if(listenfd < 0) | 175 | if(listenfd < 0) |
| 124 | return 1; | 176 | return 1; |
| 125 | 177 | ||
| 178 | client_init(); | ||
| 126 | device_init(); | 179 | device_init(); |
| 127 | usbmuxd_log(LL_INFO, "Initializing USB"); | 180 | usbmuxd_log(LL_INFO, "Initializing USB"); |
| 128 | if((res = usb_init()) < 0) | 181 | if((res = usb_init()) < 0) |
| @@ -136,8 +189,10 @@ int main(int argc, char *argv[]) | |||
| 136 | usbmuxd_log(LL_FATAL, "main_loop failed"); | 189 | usbmuxd_log(LL_FATAL, "main_loop failed"); |
| 137 | 190 | ||
| 138 | usbmuxd_log(LL_NOTICE, "usbmux shutting down"); | 191 | usbmuxd_log(LL_NOTICE, "usbmux shutting down"); |
| 192 | device_kill_connections(); | ||
| 139 | usb_shutdown(); | 193 | usb_shutdown(); |
| 140 | device_shutdown(); | 194 | device_shutdown(); |
| 195 | client_shutdown(); | ||
| 141 | usbmuxd_log(LL_NOTICE, "Shutdown complete"); | 196 | usbmuxd_log(LL_NOTICE, "Shutdown complete"); |
| 142 | 197 | ||
| 143 | if(res < 0) | 198 | if(res < 0) |
diff --git a/usb-linux.c b/usb-linux.c index 27a7bb1..3a87ba6 100644 --- a/usb-linux.c +++ b/usb-linux.c | |||
| @@ -40,48 +40,50 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
| 40 | struct usb_device { | 40 | struct usb_device { |
| 41 | libusb_device_handle *dev; | 41 | libusb_device_handle *dev; |
| 42 | uint8_t bus, address; | 42 | uint8_t bus, address; |
| 43 | uint16_t vid, pid; | ||
| 43 | char serial[256]; | 44 | char serial[256]; |
| 44 | int alive; | 45 | int alive; |
| 45 | struct libusb_transfer *rx_xfer; | 46 | struct libusb_transfer *rx_xfer; |
| 47 | struct collection tx_xfers; | ||
| 46 | }; | 48 | }; |
| 47 | 49 | ||
| 48 | static int num_devs; | 50 | static struct collection device_list; |
| 49 | static struct usb_device *device_list; | ||
| 50 | 51 | ||
| 51 | static struct timeval next_dev_poll_time; | 52 | static struct timeval next_dev_poll_time; |
| 52 | 53 | ||
| 53 | static int alloc_device(void) | ||
| 54 | { | ||
| 55 | int i; | ||
| 56 | for(i=0; i<num_devs; i++) { | ||
| 57 | if(!device_list[i].dev) | ||
| 58 | return i; | ||
| 59 | } | ||
| 60 | num_devs++; | ||
| 61 | device_list = realloc(device_list, sizeof(*device_list) * num_devs); | ||
| 62 | memset(&device_list[num_devs-1], 0, sizeof(*device_list)); | ||
| 63 | return num_devs - 1; | ||
| 64 | } | ||
| 65 | |||
| 66 | static void usb_disconnect(struct usb_device *dev) | 54 | static void usb_disconnect(struct usb_device *dev) |
| 67 | { | 55 | { |
| 68 | if(!dev->dev) { | 56 | if(!dev->dev) { |
| 69 | return; | 57 | return; |
| 70 | } | 58 | } |
| 59 | |||
| 60 | // kill the rx xfer and tx xfers and try to make sure the callbacks get called before we free the device | ||
| 71 | if(dev->rx_xfer) { | 61 | if(dev->rx_xfer) { |
| 72 | // kill the rx xfer and try to make sure the rx callback gets called before we free the device | 62 | usbmuxd_log(LL_DEBUG, "usb_disconnect: cancelling RX xfer"); |
| 63 | libusb_cancel_transfer(dev->rx_xfer); | ||
| 64 | } | ||
| 65 | FOREACH(struct libusb_transfer *xfer, &dev->tx_xfers) { | ||
| 66 | usbmuxd_log(LL_DEBUG, "usb_disconnect: cancelling TX xfer %p", xfer); | ||
| 67 | libusb_cancel_transfer(xfer); | ||
| 68 | } ENDFOREACH | ||
| 69 | |||
| 70 | while(dev->rx_xfer || collection_count(&dev->tx_xfers)) { | ||
| 73 | struct timeval tv; | 71 | struct timeval tv; |
| 74 | int res; | 72 | int res; |
| 75 | // TODO: BUG: outstanding TX xfers are not listed but we need to free them | 73 | |
| 76 | libusb_cancel_transfer(dev->rx_xfer); | 74 | tv.tv_sec = 0; |
| 77 | tv.tv_sec = tv.tv_usec = 0; | 75 | tv.tv_usec = 1000; |
| 78 | if((res = libusb_handle_events_timeout(NULL, &tv)) < 0) { | 76 | if((res = libusb_handle_events_timeout(NULL, &tv)) < 0) { |
| 79 | usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout for device removal failed: %d", res); | 77 | usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout for usb_disconnect failed: %d", res); |
| 78 | break; | ||
| 80 | } | 79 | } |
| 81 | } | 80 | } |
| 81 | collection_free(&dev->tx_xfers); | ||
| 82 | libusb_release_interface(dev->dev, USB_INTERFACE); | 82 | libusb_release_interface(dev->dev, USB_INTERFACE); |
| 83 | libusb_close(dev->dev); | 83 | libusb_close(dev->dev); |
| 84 | dev->dev = NULL; | 84 | dev->dev = NULL; |
| 85 | collection_remove(&device_list, dev); | ||
| 86 | free(dev); | ||
| 85 | } | 87 | } |
| 86 | 88 | ||
| 87 | static void tx_callback(struct libusb_transfer *xfer) | 89 | static void tx_callback(struct libusb_transfer *xfer) |
| @@ -117,7 +119,9 @@ static void tx_callback(struct libusb_transfer *xfer) | |||
| 117 | // we'll do device_remove there too | 119 | // we'll do device_remove there too |
| 118 | dev->alive = 0; | 120 | dev->alive = 0; |
| 119 | } | 121 | } |
| 120 | free(xfer->buffer); | 122 | if(xfer->buffer) |
| 123 | free(xfer->buffer); | ||
| 124 | collection_remove(&dev->tx_xfers, xfer); | ||
| 121 | libusb_free_transfer(xfer); | 125 | libusb_free_transfer(xfer); |
| 122 | } | 126 | } |
| 123 | 127 | ||
| @@ -132,6 +136,21 @@ int usb_send(struct usb_device *dev, const unsigned char *buf, int length) | |||
| 132 | libusb_free_transfer(xfer); | 136 | libusb_free_transfer(xfer); |
| 133 | return res; | 137 | return res; |
| 134 | } | 138 | } |
| 139 | collection_add(&dev->tx_xfers, xfer);/* | ||
| 140 | if((length % 512) == 0) { | ||
| 141 | usbmuxd_log(LL_DEBUG, "Send ZLP"); | ||
| 142 | // Send Zero Length Packet | ||
| 143 | xfer = libusb_alloc_transfer(0); | ||
| 144 | void *buffer = malloc(1); | ||
| 145 | libusb_fill_bulk_transfer(xfer, dev->dev, BULK_OUT, buffer, 0, tx_callback, dev, 0); | ||
| 146 | xfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK; | ||
| 147 | if((res = libusb_submit_transfer(xfer)) < 0) { | ||
| 148 | usbmuxd_log(LL_ERROR, "Failed to submit TX ZLP transfer to device %d-%d: %d", dev->bus, dev->address, res); | ||
| 149 | libusb_free_transfer(xfer); | ||
| 150 | return res; | ||
| 151 | } | ||
| 152 | collection_add(&dev->tx_xfers, xfer); | ||
| 153 | }*/ | ||
| 135 | return 0; | 154 | return 0; |
| 136 | } | 155 | } |
| 137 | 156 | ||
| @@ -181,8 +200,8 @@ static int start_rx(struct usb_device *dev) | |||
| 181 | int res; | 200 | int res; |
| 182 | void *buf; | 201 | void *buf; |
| 183 | dev->rx_xfer = libusb_alloc_transfer(0); | 202 | dev->rx_xfer = libusb_alloc_transfer(0); |
| 184 | buf = malloc(USB_MTU); | 203 | buf = malloc(USB_MRU); |
| 185 | libusb_fill_bulk_transfer(dev->rx_xfer, dev->dev, BULK_IN, buf, USB_MTU, rx_callback, dev, 0); | 204 | libusb_fill_bulk_transfer(dev->rx_xfer, dev->dev, BULK_IN, buf, USB_MRU, rx_callback, dev, 0); |
| 186 | if((res = libusb_submit_transfer(dev->rx_xfer)) != 0) { | 205 | if((res = libusb_submit_transfer(dev->rx_xfer)) != 0) { |
| 187 | usbmuxd_log(LL_ERROR, "Failed to submit RX transfer to device %d-%d: %d", dev->bus, dev->address, res); | 206 | usbmuxd_log(LL_ERROR, "Failed to submit RX transfer to device %d-%d: %d", dev->bus, dev->address, res); |
| 188 | libusb_free_transfer(dev->rx_xfer); | 207 | libusb_free_transfer(dev->rx_xfer); |
| @@ -194,7 +213,7 @@ static int start_rx(struct usb_device *dev) | |||
| 194 | 213 | ||
| 195 | static int usb_discover(void) | 214 | static int usb_discover(void) |
| 196 | { | 215 | { |
| 197 | int cnt, i, j, res; | 216 | int cnt, i, res; |
| 198 | int valid_count = 0; | 217 | int valid_count = 0; |
| 199 | libusb_device **devs; | 218 | libusb_device **devs; |
| 200 | 219 | ||
| @@ -206,23 +225,26 @@ static int usb_discover(void) | |||
| 206 | 225 | ||
| 207 | usbmuxd_log(LL_SPEW, "usb_discover: scanning %d devices", cnt); | 226 | usbmuxd_log(LL_SPEW, "usb_discover: scanning %d devices", cnt); |
| 208 | 227 | ||
| 209 | for(j=0; j<num_devs; j++) { | 228 | FOREACH(struct usb_device *usbdev, &device_list) { |
| 210 | device_list[j].alive = 0; | 229 | usbdev->alive = 0; |
| 211 | } | 230 | } ENDFOREACH |
| 231 | |||
| 212 | for(i=0; i<cnt; i++) { | 232 | for(i=0; i<cnt; i++) { |
| 213 | // the following are non-blocking operations on the device list | 233 | // the following are non-blocking operations on the device list |
| 214 | libusb_device *dev = devs[i]; | 234 | libusb_device *dev = devs[i]; |
| 215 | uint8_t bus = libusb_get_bus_number(dev); | 235 | uint8_t bus = libusb_get_bus_number(dev); |
| 216 | uint8_t address = libusb_get_device_address(dev); | 236 | uint8_t address = libusb_get_device_address(dev); |
| 217 | struct libusb_device_descriptor devdesc; | 237 | struct libusb_device_descriptor devdesc; |
| 218 | for(j=0; j<num_devs; j++) { | 238 | int found = 0; |
| 219 | if(device_list[j].dev && device_list[j].bus == bus && device_list[j].address == address) { | 239 | FOREACH(struct usb_device *usbdev, &device_list) { |
| 240 | if(usbdev->bus == bus && usbdev->address == address) { | ||
| 220 | valid_count++; | 241 | valid_count++; |
| 221 | device_list[j].alive = 1; | 242 | usbdev->alive = 1; |
| 243 | found = 1; | ||
| 222 | break; | 244 | break; |
| 223 | } | 245 | } |
| 224 | } | 246 | } ENDFOREACH |
| 225 | if(j < num_devs) | 247 | if(found) |
| 226 | continue; //device already found | 248 | continue; //device already found |
| 227 | if((res = libusb_get_device_descriptor(dev, &devdesc)) != 0) { | 249 | if((res = libusb_get_device_descriptor(dev, &devdesc)) != 0) { |
| 228 | usbmuxd_log(LL_WARNING, "Could not get device descriptor for device %d-%d: %d", bus, address, res); | 250 | usbmuxd_log(LL_WARNING, "Could not get device descriptor for device %d-%d: %d", bus, address, res); |
| @@ -251,37 +273,45 @@ static int usb_discover(void) | |||
| 251 | libusb_close(handle); | 273 | libusb_close(handle); |
| 252 | continue; | 274 | continue; |
| 253 | } | 275 | } |
| 254 | int idx = alloc_device(); | 276 | struct usb_device *usbdev; |
| 277 | usbdev = malloc(sizeof(struct usb_device)); | ||
| 255 | 278 | ||
| 256 | if((res = libusb_get_string_descriptor_ascii(handle, devdesc.iSerialNumber, (uint8_t *)device_list[idx].serial, 256)) <= 0) { | 279 | if((res = libusb_get_string_descriptor_ascii(handle, devdesc.iSerialNumber, (uint8_t *)usbdev->serial, 256)) <= 0) { |
| 257 | usbmuxd_log(LL_WARNING, "Could not get serial number for device %d-%d: %d", bus, address, res); | 280 | usbmuxd_log(LL_WARNING, "Could not get serial number for device %d-%d: %d", bus, address, res); |
| 258 | libusb_release_interface(handle, USB_INTERFACE); | 281 | libusb_release_interface(handle, USB_INTERFACE); |
| 259 | libusb_close(handle); | 282 | libusb_close(handle); |
| 283 | free(usbdev); | ||
| 260 | continue; | 284 | continue; |
| 261 | } | 285 | } |
| 262 | device_list[idx].serial[res] = 0; | 286 | usbdev->serial[res] = 0; |
| 263 | device_list[idx].bus = bus; | 287 | usbdev->bus = bus; |
| 264 | device_list[idx].address = address; | 288 | usbdev->address = address; |
| 265 | device_list[idx].dev = handle; | 289 | usbdev->vid = devdesc.idVendor; |
| 266 | device_list[idx].alive = 1; | 290 | usbdev->pid = devdesc.idProduct; |
| 291 | usbdev->dev = handle; | ||
| 292 | usbdev->alive = 1; | ||
| 293 | collection_init(&usbdev->tx_xfers); | ||
| 294 | |||
| 295 | collection_add(&device_list, usbdev); | ||
| 267 | 296 | ||
| 268 | if(device_add(&device_list[idx]) < 0) { | 297 | if(device_add(usbdev) < 0) { |
| 269 | usb_disconnect(&device_list[j]); | 298 | usb_disconnect(usbdev); |
| 270 | continue; | 299 | continue; |
| 271 | } | 300 | } |
| 272 | if(start_rx(&device_list[idx]) < 0) { | 301 | if(start_rx(usbdev) < 0) { |
| 273 | device_remove(&device_list[j]); | 302 | device_remove(usbdev); |
| 274 | usb_disconnect(&device_list[j]); | 303 | usb_disconnect(usbdev); |
| 275 | continue; | 304 | continue; |
| 276 | } | 305 | } |
| 277 | valid_count++; | 306 | valid_count++; |
| 278 | } | 307 | } |
| 279 | for(j=0; j<num_devs; j++) { | 308 | FOREACH(struct usb_device *usbdev, &device_list) { |
| 280 | if(device_list[j].dev && !device_list[j].alive) { | 309 | if(!usbdev->alive) { |
| 281 | device_remove(&device_list[j]); | 310 | device_remove(usbdev); |
| 282 | usb_disconnect(&device_list[j]); | 311 | usb_disconnect(usbdev); |
| 283 | } | 312 | } |
| 284 | } | 313 | } ENDFOREACH |
| 314 | |||
| 285 | libusb_free_device_list(devs, 1); | 315 | libusb_free_device_list(devs, 1); |
| 286 | 316 | ||
| 287 | gettimeofday(&next_dev_poll_time, NULL); | 317 | gettimeofday(&next_dev_poll_time, NULL); |
| @@ -300,7 +330,7 @@ const char *usb_get_serial(struct usb_device *dev) | |||
| 300 | return dev->serial; | 330 | return dev->serial; |
| 301 | } | 331 | } |
| 302 | 332 | ||
| 303 | int usb_get_location(struct usb_device *dev) | 333 | uint32_t usb_get_location(struct usb_device *dev) |
| 304 | { | 334 | { |
| 305 | if(!dev->dev) { | 335 | if(!dev->dev) { |
| 306 | return 0; | 336 | return 0; |
| @@ -308,6 +338,14 @@ int usb_get_location(struct usb_device *dev) | |||
| 308 | return (dev->bus << 16) | dev->address; | 338 | return (dev->bus << 16) | dev->address; |
| 309 | } | 339 | } |
| 310 | 340 | ||
| 341 | uint16_t usb_get_pid(struct usb_device *dev) | ||
| 342 | { | ||
| 343 | if(!dev->dev) { | ||
| 344 | return 0; | ||
| 345 | } | ||
| 346 | return dev->pid; | ||
| 347 | } | ||
| 348 | |||
| 311 | void usb_get_fds(struct fdlist *list) | 349 | void usb_get_fds(struct fdlist *list) |
| 312 | { | 350 | { |
| 313 | const struct libusb_pollfd **usbfds; | 351 | const struct libusb_pollfd **usbfds; |
| @@ -360,7 +398,7 @@ int usb_get_timeout(void) | |||
| 360 | 398 | ||
| 361 | int usb_process(void) | 399 | int usb_process(void) |
| 362 | { | 400 | { |
| 363 | int i, res; | 401 | int res; |
| 364 | struct timeval tv; | 402 | struct timeval tv; |
| 365 | tv.tv_sec = tv.tv_usec = 0; | 403 | tv.tv_sec = tv.tv_usec = 0; |
| 366 | res = libusb_handle_events_timeout(NULL, &tv); | 404 | res = libusb_handle_events_timeout(NULL, &tv); |
| @@ -369,12 +407,12 @@ int usb_process(void) | |||
| 369 | return res; | 407 | return res; |
| 370 | } | 408 | } |
| 371 | // reap devices marked dead due to an RX error | 409 | // reap devices marked dead due to an RX error |
| 372 | for(i=0; i<num_devs; i++) { | 410 | FOREACH(struct usb_device *usbdev, &device_list) { |
| 373 | if(device_list[i].dev && !device_list[i].alive) { | 411 | if(!usbdev->alive) { |
| 374 | device_remove(&device_list[i]); | 412 | device_remove(usbdev); |
| 375 | usb_disconnect(&device_list[i]); | 413 | usb_disconnect(usbdev); |
| 376 | } | 414 | } |
| 377 | } | 415 | } ENDFOREACH |
| 378 | 416 | ||
| 379 | if(dev_poll_remain_ms() <= 0) { | 417 | if(dev_poll_remain_ms() <= 0) { |
| 380 | res = usb_discover(); | 418 | res = usb_discover(); |
| @@ -386,6 +424,39 @@ int usb_process(void) | |||
| 386 | return 0; | 424 | return 0; |
| 387 | } | 425 | } |
| 388 | 426 | ||
| 427 | int usb_process_timeout(int msec) | ||
| 428 | { | ||
| 429 | int res; | ||
| 430 | struct timeval tleft, tcur, tfin; | ||
| 431 | gettimeofday(&tcur, NULL); | ||
| 432 | tfin.tv_sec = tcur.tv_sec + (msec / 1000); | ||
| 433 | tfin.tv_usec = tcur.tv_usec + (msec % 1000) * 1000; | ||
| 434 | tfin.tv_sec += tfin.tv_usec / 1000000; | ||
| 435 | tfin.tv_usec %= 1000000; | ||
| 436 | while((tfin.tv_sec > tcur.tv_sec) || ((tfin.tv_sec == tcur.tv_sec) && (tfin.tv_usec > tcur.tv_usec))) { | ||
| 437 | tleft.tv_sec = tfin.tv_sec - tcur.tv_sec; | ||
| 438 | tleft.tv_usec = tfin.tv_usec - tcur.tv_usec; | ||
| 439 | if(tleft.tv_usec < 0) { | ||
| 440 | tleft.tv_usec += 1000000; | ||
| 441 | tleft.tv_sec -= 1; | ||
| 442 | } | ||
| 443 | res = libusb_handle_events_timeout(NULL, &tleft); | ||
| 444 | if(res < 0) { | ||
| 445 | usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout failed: %d", res); | ||
| 446 | return res; | ||
| 447 | } | ||
| 448 | // reap devices marked dead due to an RX error | ||
| 449 | FOREACH(struct usb_device *usbdev, &device_list) { | ||
| 450 | if(!usbdev->alive) { | ||
| 451 | device_remove(usbdev); | ||
| 452 | usb_disconnect(usbdev); | ||
| 453 | } | ||
| 454 | } ENDFOREACH | ||
| 455 | gettimeofday(&tcur, NULL); | ||
| 456 | } | ||
| 457 | return 0; | ||
| 458 | } | ||
| 459 | |||
| 389 | int usb_init(void) | 460 | int usb_init(void) |
| 390 | { | 461 | { |
| 391 | int res; | 462 | int res; |
| @@ -398,20 +469,18 @@ int usb_init(void) | |||
| 398 | return -1; | 469 | return -1; |
| 399 | } | 470 | } |
| 400 | 471 | ||
| 401 | num_devs = 1; | 472 | collection_init(&device_list); |
| 402 | device_list = malloc(sizeof(*device_list) * num_devs); | ||
| 403 | memset(device_list, 0, sizeof(*device_list) * num_devs); | ||
| 404 | 473 | ||
| 405 | return usb_discover(); | 474 | return usb_discover(); |
| 406 | } | 475 | } |
| 407 | 476 | ||
| 408 | void usb_shutdown(void) | 477 | void usb_shutdown(void) |
| 409 | { | 478 | { |
| 410 | int i; | ||
| 411 | usbmuxd_log(LL_DEBUG, "usb_shutdown"); | 479 | usbmuxd_log(LL_DEBUG, "usb_shutdown"); |
| 412 | for(i=0; i<num_devs; i++) | 480 | FOREACH(struct usb_device *usbdev, &device_list) { |
| 413 | usb_disconnect(&device_list[i]); | 481 | device_remove(usbdev); |
| 414 | free(device_list); | 482 | usb_disconnect(usbdev); |
| 415 | device_list = NULL; | 483 | } ENDFOREACH |
| 484 | collection_free(&device_list); | ||
| 416 | libusb_exit(NULL); | 485 | libusb_exit(NULL); |
| 417 | } | 486 | } |
| @@ -21,12 +21,27 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
| 21 | #ifndef __USB_H__ | 21 | #ifndef __USB_H__ |
| 22 | #define __USB_H__ | 22 | #define __USB_H__ |
| 23 | 23 | ||
| 24 | #include <stdint.h> | ||
| 24 | #include "utils.h" | 25 | #include "utils.h" |
| 25 | 26 | ||
| 26 | #define BULK_IN 0x85 | 27 | #define BULK_IN 0x85 |
| 27 | #define BULK_OUT 0x04 | 28 | #define BULK_OUT 0x04 |
| 28 | 29 | ||
| 29 | #define USB_MTU 65536 | 30 | // libusb fragments packets larger than this (usbfs limitation) |
| 31 | // on input, this creates race conditions and other issues | ||
| 32 | // I don't think the device uses larger packets | ||
| 33 | // if it does then we're going to have to implement proper framing... | ||
| 34 | #define USB_MRU 16384 | ||
| 35 | |||
| 36 | // max transmission packet size | ||
| 37 | // libusb fragments these too, but doesn't send ZLPs so we're safe | ||
| 38 | // but maybe we need to send a ZLP ourselves at the end (see usb-linux.h) | ||
| 39 | // we're using 3 * 16384 to optimize for the fragmentation | ||
| 40 | // this results in three URBs per full transfer, 32 USB packets each | ||
| 41 | // if there are ZLP issues this should make them show up too | ||
| 42 | #define USB_MTU (3 * 16384) | ||
| 43 | |||
| 44 | #define USB_PACKET_SIZE 512 | ||
| 30 | 45 | ||
| 31 | #define VID_APPLE 0x5ac | 46 | #define VID_APPLE 0x5ac |
| 32 | #define PID_IPHONE2G 0x1290 | 47 | #define PID_IPHONE2G 0x1290 |
| @@ -41,10 +56,12 @@ struct usb_device; | |||
| 41 | int usb_init(void); | 56 | int usb_init(void); |
| 42 | void usb_shutdown(void); | 57 | void usb_shutdown(void); |
| 43 | const char *usb_get_serial(struct usb_device *dev); | 58 | const char *usb_get_serial(struct usb_device *dev); |
| 44 | int usb_get_location(struct usb_device *dev); | 59 | uint32_t usb_get_location(struct usb_device *dev); |
| 60 | uint16_t usb_get_pid(struct usb_device *dev); | ||
| 45 | void usb_get_fds(struct fdlist *list); | 61 | void usb_get_fds(struct fdlist *list); |
| 46 | int usb_get_timeout(void); | 62 | int usb_get_timeout(void); |
| 47 | int usb_send(struct usb_device *dev, const unsigned char *buf, int length); | 63 | int usb_send(struct usb_device *dev, const unsigned char *buf, int length); |
| 48 | int usb_process(void); | 64 | int usb_process(void); |
| 65 | int usb_process_timeout(int msec); | ||
| 49 | 66 | ||
| 50 | #endif | 67 | #endif |
| @@ -23,7 +23,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
| 23 | #endif | 23 | #endif |
| 24 | 24 | ||
| 25 | #include <stdlib.h> | 25 | #include <stdlib.h> |
| 26 | #include <string.h> | ||
| 26 | #include "utils.h" | 27 | #include "utils.h" |
| 28 | #include "log.h" | ||
| 27 | 29 | ||
| 28 | void fdlist_create(struct fdlist *list) | 30 | void fdlist_create(struct fdlist *list) |
| 29 | { | 31 | { |
| @@ -55,3 +57,54 @@ void fdlist_free(struct fdlist *list) | |||
| 55 | free(list->fds); | 57 | free(list->fds); |
| 56 | list->fds = NULL; | 58 | list->fds = NULL; |
| 57 | } | 59 | } |
| 60 | |||
| 61 | void collection_init(struct collection *col) | ||
| 62 | { | ||
| 63 | col->list = malloc(sizeof(void *)); | ||
| 64 | memset(col->list, 0, sizeof(void *)); | ||
| 65 | col->capacity = 1; | ||
| 66 | } | ||
| 67 | |||
| 68 | void collection_free(struct collection *col) | ||
| 69 | { | ||
| 70 | free(col->list); | ||
| 71 | col->list = NULL; | ||
| 72 | col->capacity = 0; | ||
| 73 | } | ||
| 74 | |||
| 75 | void collection_add(struct collection *col, void *element) | ||
| 76 | { | ||
| 77 | int i; | ||
| 78 | for(i=0; i<col->capacity; i++) { | ||
| 79 | if(!col->list[i]) { | ||
| 80 | col->list[i] = element; | ||
| 81 | return; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | col->list = realloc(col->list, sizeof(void*) * col->capacity * 2); | ||
| 85 | memset(&col->list[col->capacity], 0, sizeof(void *) * col->capacity); | ||
| 86 | col->list[col->capacity] = element; | ||
| 87 | col->capacity *= 2; | ||
| 88 | } | ||
| 89 | |||
| 90 | void collection_remove(struct collection *col, void *element) | ||
| 91 | { | ||
| 92 | int i; | ||
| 93 | for(i=0; i<col->capacity; i++) { | ||
| 94 | if(col->list[i] == element) { | ||
| 95 | col->list[i] = NULL; | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | usbmuxd_log(LL_ERROR, "collection_remove: element %p not present in collection %p (cap %d)", element, col, col->capacity); | ||
| 100 | } | ||
| 101 | |||
| 102 | int collection_count(struct collection *col) | ||
| 103 | { | ||
| 104 | int i, cnt = 0; | ||
| 105 | for(i=0; i<col->capacity; i++) { | ||
| 106 | if(col->list[i]) | ||
| 107 | cnt++; | ||
| 108 | } | ||
| 109 | return cnt; | ||
| 110 | } | ||
| @@ -40,6 +40,26 @@ void fdlist_create(struct fdlist *list); | |||
| 40 | void fdlist_add(struct fdlist *list, enum fdowner owner, int fd, short events); | 40 | void fdlist_add(struct fdlist *list, enum fdowner owner, int fd, short events); |
| 41 | void fdlist_free(struct fdlist *list); | 41 | void fdlist_free(struct fdlist *list); |
| 42 | 42 | ||
| 43 | #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) | 43 | struct collection { |
| 44 | void **list; | ||
| 45 | int capacity; | ||
| 46 | }; | ||
| 47 | |||
| 48 | void collection_init(struct collection *col); | ||
| 49 | void collection_add(struct collection *col, void *element); | ||
| 50 | void collection_remove(struct collection *col, void *element); | ||
| 51 | int collection_count(struct collection *col); | ||
| 52 | void collection_free(struct collection *col); | ||
| 53 | |||
| 54 | #define FOREACH(var, col) \ | ||
| 55 | do { \ | ||
| 56 | int _iter; \ | ||
| 57 | for(_iter=0; _iter<(col)->capacity; _iter++) { \ | ||
| 58 | if(!(col)->list[_iter]) continue; \ | ||
| 59 | var = (col)->list[_iter]; | ||
| 60 | |||
| 61 | #define ENDFOREACH \ | ||
| 62 | } \ | ||
| 63 | } while(0); | ||
| 44 | 64 | ||
| 45 | #endif | 65 | #endif |
