summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Mikkel Kamstrup Erlandsen2014-03-19 13:28:25 +0100
committerGravatar Martin Szulecki2014-03-24 17:01:30 +0100
commitc4e9a48bfd362290822cc845b077801801d0197a (patch)
tree03d660ef97bba71b586d3bcd03c4d7460b437798
parent16428372666a32f7493b7b4f949d6ed5c37118ef (diff)
downloadusbmuxd-c4e9a48bfd362290822cc845b077801801d0197a.tar.gz
usbmuxd-c4e9a48bfd362290822cc845b077801801d0197a.tar.bz2
client: add a bunch of comments and function docs
-rw-r--r--src/client.c35
-rw-r--r--src/device.c56
2 files changed, 90 insertions, 1 deletions
diff --git a/src/client.c b/src/client.c
index 1e15a1e..c6a7ce8 100644
--- a/src/client.c
+++ b/src/client.c
@@ -71,6 +71,14 @@ struct mux_client {
71static struct collection client_list; 71static struct collection client_list;
72pthread_mutex_t client_list_mutex; 72pthread_mutex_t client_list_mutex;
73 73
74/**
75 * Receive raw data from the client socket.
76 *
77 * @param client Client to read from.
78 * @param buffer Buffer to store incoming data.
79 * @param len Max number of bytes to read.
80 * @return Same as recv() system call. Number of bytes read; when < 0 errno will be set.
81 */
74int client_read(struct mux_client *client, void *buffer, uint32_t len) 82int client_read(struct mux_client *client, void *buffer, uint32_t len)
75{ 83{
76 usbmuxd_log(LL_SPEW, "client_read fd %d buf %p len %d", client->fd, buffer, len); 84 usbmuxd_log(LL_SPEW, "client_read fd %d buf %p len %d", client->fd, buffer, len);
@@ -81,6 +89,14 @@ int client_read(struct mux_client *client, void *buffer, uint32_t len)
81 return recv(client->fd, buffer, len, 0); 89 return recv(client->fd, buffer, len, 0);
82} 90}
83 91
92/**
93 * Send raw data to the client socket.
94 *
95 * @param client Client to send to.
96 * @param buffer The data to send.
97 * @param len Number of bytes to write.
98 * @return Same as system call send(). Number of bytes written; when < 0 errno will be set.
99 */
84int client_write(struct mux_client *client, void *buffer, uint32_t len) 100int client_write(struct mux_client *client, void *buffer, uint32_t len)
85{ 101{
86 usbmuxd_log(LL_SPEW, "client_write fd %d buf %p len %d", client->fd, buffer, len); 102 usbmuxd_log(LL_SPEW, "client_write fd %d buf %p len %d", client->fd, buffer, len);
@@ -91,6 +107,16 @@ int client_write(struct mux_client *client, void *buffer, uint32_t len)
91 return send(client->fd, buffer, len, 0); 107 return send(client->fd, buffer, len, 0);
92} 108}
93 109
110/**
111 * Set event mask to use for ppoll()ing the client socket.
112 * Typically POLLOUT and/or POLLIN. Note that this overrides
113 * the current mask, that is, it is not ORing the argument
114 * into the current mask.
115 *
116 * @param client The client to set the event mask on.
117 * @param events The event mask to sert.
118 * @return 0 on success, -1 on error.
119 */
94int client_set_events(struct mux_client *client, short events) 120int client_set_events(struct mux_client *client, short events)
95{ 121{
96 if((client->state != CLIENT_CONNECTED) && (client->state != CLIENT_CONNECTING2)) { 122 if((client->state != CLIENT_CONNECTED) && (client->state != CLIENT_CONNECTING2)) {
@@ -103,6 +129,15 @@ int client_set_events(struct mux_client *client, short events)
103 return 0; 129 return 0;
104} 130}
105 131
132/**
133 * Wait for an inbound connection on the usbmuxd socket
134 * and create a new mux_client instance for it, and store
135 * the client in the client list.
136 *
137 * @param listenfd the socket fd to accept() on.
138 * @return The connection fd for the client, or < 0 for error
139 * in which case errno will be set.
140 */
106int client_accept(int listenfd) 141int client_accept(int listenfd)
107{ 142{
108 struct sockaddr_un addr; 143 struct sockaddr_un addr;
diff --git a/src/device.c b/src/device.c
index ce0f28f..15f829b 100644
--- a/src/device.c
+++ b/src/device.c
@@ -329,6 +329,13 @@ int device_start_connect(int device_id, uint16_t dport, struct mux_client *clien
329 return 0; 329 return 0;
330} 330}
331 331
332/**
333 * Examine the state of a connection's buffers and
334 * update all connection flags and masks accordingly.
335 * Does not do I/O.
336 *
337 * @param conn The connection to update.
338 */
332static void update_connection(struct mux_connection *conn) 339static void update_connection(struct mux_connection *conn)
333{ 340{
334 uint32_t sent = conn->tx_seq - conn->rx_ack; 341 uint32_t sent = conn->tx_seq - conn->rx_ack;
@@ -362,8 +369,18 @@ static void update_connection(struct mux_connection *conn)
362 client_set_events(conn->client, conn->events); 369 client_set_events(conn->client, conn->events);
363} 370}
364 371
372/**
373 * Flush input and output buffers for a client connection.
374 *
375 * @param device_id Numeric id for the device.
376 * @param client The client to flush buffers for.
377 * @param events event mask for the client. POLLOUT means that
378 * the client is ready to receive data, POLLIN that it has
379 * data to be read (and send along to the device).
380 */
365void device_client_process(int device_id, struct mux_client *client, short events) 381void device_client_process(int device_id, struct mux_client *client, short events)
366{ 382{
383 // Find the connection for the given device_id
367 struct mux_connection *conn = NULL; 384 struct mux_connection *conn = NULL;
368 pthread_mutex_lock(&device_list_mutex); 385 pthread_mutex_lock(&device_list_mutex);
369 FOREACH(struct mux_device *dev, &device_list) { 386 FOREACH(struct mux_device *dev, &device_list) {
@@ -388,6 +405,8 @@ void device_client_process(int device_id, struct mux_client *client, short event
388 int res; 405 int res;
389 int size; 406 int size;
390 if(events & POLLOUT) { 407 if(events & POLLOUT) {
408 // Client is ready to receive data, send what we have
409 // in the client's connection buffer
391 size = client_write(conn->client, conn->ib_buf, conn->ib_size); 410 size = client_write(conn->client, conn->ib_buf, conn->ib_size);
392 if(size <= 0) { 411 if(size <= 0) {
393 usbmuxd_log(LL_DEBUG, "error writing to client (%d)", size); 412 usbmuxd_log(LL_DEBUG, "error writing to client (%d)", size);
@@ -403,6 +422,8 @@ void device_client_process(int device_id, struct mux_client *client, short event
403 } 422 }
404 } 423 }
405 if(events & POLLIN) { 424 if(events & POLLIN) {
425 // There is inbound trafic on the client socket,
426 // convert it to tcp and send to the device
406 size = client_read(conn->client, conn->ob_buf, conn->sendable); 427 size = client_read(conn->client, conn->ob_buf, conn->sendable);
407 if(size <= 0) { 428 if(size <= 0) {
408 if (size < 0) { 429 if (size < 0) {
@@ -422,6 +443,23 @@ void device_client_process(int device_id, struct mux_client *client, short event
422 update_connection(conn); 443 update_connection(conn);
423} 444}
424 445
446/**
447 * Copy a payload to a connection's in-buffer and
448 * set the POLLOUT event mask on the connection so
449 * the next main_loop iteration will dispatch the
450 * buffer if the connection socket is writable.
451 *
452 * Connection buffers are flushed in the
453 * device_client_process() function.
454 *
455 * @param conn The connection to add incoming data to.
456 * @param payload Payload to prepare for writing.
457 * The payload will be copied immediately so you are
458 * free to alter or free the payload buffer when this
459 * function returns.
460 * @param payload_length number of bytes to copy from from
461 * the payload.
462 */
425static void connection_device_input(struct mux_connection *conn, unsigned char *payload, uint32_t payload_length) 463static void connection_device_input(struct mux_connection *conn, unsigned char *payload, uint32_t payload_length)
426{ 464{
427 if((conn->ib_size + payload_length) > conn->ib_capacity) { 465 if((conn->ib_size + payload_length) > conn->ib_capacity) {
@@ -483,6 +521,14 @@ static void device_version_input(struct mux_device *dev, struct version_header *
483 preflight_worker_device_add(&info); 521 preflight_worker_device_add(&info);
484} 522}
485 523
524/**
525 * Handle an incoming TCP packet from the device.
526 *
527 * @param dev The device handle TCP input on.
528 * @param th Pointer to the TCP header struct.
529 * @param payload Payload data.
530 * @param payload_length Number of bytes in payload.
531 */
486static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned char *payload, uint32_t payload_length) 532static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned char *payload, uint32_t payload_length)
487{ 533{
488 uint16_t sport = ntohs(th->th_dport); 534 uint16_t sport = ntohs(th->th_dport);
@@ -561,6 +607,14 @@ static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned
561 } 607 }
562} 608}
563 609
610/**
611 * Take input data from the device that has been read into a buffer
612 * and dispatch it to the right protocol backend (eg. TCP).
613 *
614 * @param usbdev
615 * @param buffer
616 * @param length
617 */
564void device_data_input(struct usb_device *usbdev, unsigned char *buffer, uint32_t length) 618void device_data_input(struct usb_device *usbdev, unsigned char *buffer, uint32_t length)
565{ 619{
566 struct mux_device *dev = NULL; 620 struct mux_device *dev = NULL;
@@ -595,7 +649,7 @@ void device_data_input(struct usb_device *usbdev, unsigned char *buffer, uint32_
595 dev->pktlen = 0; 649 dev->pktlen = 0;
596 return; 650 return;
597 } 651 }
598 memcpy(dev->pktbuf + dev->pktlen, buffer, length); 652 memcpy(dev->pktbuf + dev->pktlen, buffer, length);
599 struct mux_header *mhdr = (struct mux_header *)dev->pktbuf; 653 struct mux_header *mhdr = (struct mux_header *)dev->pktbuf;
600 if((length < USB_MRU) || (ntohl(mhdr->length) == (length + dev->pktlen))) { 654 if((length < USB_MRU) || (ntohl(mhdr->length) == (length + dev->pktlen))) {
601 buffer = dev->pktbuf; 655 buffer = dev->pktbuf;