diff options
author | Till Kamppeter <till.kamppeter@gmail.com> | 2017-04-26 13:10:56 -0300 |
---|---|---|
committer | Till Kamppeter <till.kamppeter@gmail.com> | 2017-04-26 13:10:56 -0300 |
commit | 630de2460f0785bd3c16bd9d6297b549531c8a97 (patch) | |
tree | 8b89b705e35c2436b13ff05ebaaf1f57d9b67cf6 | |
parent | 0515236d7cb237e73eadb68721af81d7482560d9 (diff) |
Replace C++ comments ("// ...") by standard C comments ("/* ... */")
-rw-r--r-- | src/http.c | 152 | ||||
-rw-r--r-- | src/http.h | 4 | ||||
-rw-r--r-- | src/ippusbxd.c | 84 | ||||
-rw-r--r-- | src/logging.c | 16 | ||||
-rw-r--r-- | src/logging.h | 2 | ||||
-rw-r--r-- | src/options.h | 8 | ||||
-rw-r--r-- | src/tcp.c | 50 | ||||
-rw-r--r-- | src/usb.c | 178 | ||||
-rw-r--r-- | src/usb.h | 2 |
9 files changed, 248 insertions, 248 deletions
@@ -51,25 +51,25 @@ void message_free(struct http_message_t *msg) static void packet_check_completion(struct http_packet_t *pkt) { struct http_message_t *msg = pkt->parent_message; - // Msg full + /* Msg full */ if (msg->claimed_size && msg->received_size >= msg->claimed_size) { msg->is_completed = 1; NOTE("http: Message completed: Received size >= claimed size"); - // Sanity check + /* Sanity check */ if (msg->spare_filled > 0) ERR_AND_EXIT("Msg spare not empty upon completion"); } - // Pkt full + /* Pkt full */ if (pkt->expected_size && pkt->filled_size >= pkt->expected_size) { pkt->is_completed = 1; NOTE("http: Packet completed: Packet full"); } - // Pkt over capacity + /* Pkt over capacity */ if (pkt->filled_size > pkt->buffer_capacity) { - // Santiy check + /* Santiy check */ ERR_AND_EXIT("Overflowed packet buffer"); } } @@ -86,32 +86,32 @@ static int doesMatch(const char *matcher, size_t matcher_len, static int inspect_header_field(struct http_packet_t *pkt, size_t header_size, char *key, size_t key_size) { - // Find key + /* Find key */ uint8_t *pos = memmem(pkt->buffer, header_size, key, key_size); if (pos == NULL) return -1; - // Find first digit + /* Find first digit */ size_t number_pos = (size_t) (pos - pkt->buffer) + key_size; while (number_pos < pkt->filled_size && !isdigit(pkt->buffer[number_pos])) ++number_pos; - // Find next non-digit + /* Find next non-digit */ size_t number_end = number_pos; while (number_end < pkt->filled_size && isdigit(pkt->buffer[number_end])) ++number_end; - // Failed to find next non-digit - // header field might be broken + /* Failed to find next non-digit + header field might be broken */ if (number_end >= pkt->filled_size) return -1; - // Temporary stringification of buffer for atoi() + /* Temporary stringification of buffer for atoi() */ uint8_t original_char = pkt->buffer[number_end]; pkt->buffer[number_end] = '\0'; int val = atoi((const char *)(pkt->buffer + number_pos)); - // Restore buffer + /* Restore buffer */ pkt->buffer[number_end] = original_char; return val; } @@ -129,7 +129,7 @@ static void packet_store_excess(struct http_packet_t *pkt) size_t non_spare = pkt->expected_size; NOTE("HTTP: Storing %d bytes of excess", spare_size); - // Align to BUFFER_STEP + /* Align to BUFFER_STEP */ size_t needed_size = 0; needed_size += spare_size / BUFFER_STEP; needed_size += (spare_size % BUFFER_STEP) > 0 ? BUFFER_STEP : 0; @@ -160,7 +160,7 @@ static void packet_take_spare(struct http_packet_t *pkt) if (pkt->filled_size > 0) ERR_AND_EXIT("pkt should be empty when loading msg spare"); - // Take message's buffer + /* Take message's buffer */ size_t msg_size = msg->spare_capacity; size_t msg_filled = msg->spare_filled; uint8_t *msg_buffer = msg->spare_buffer; @@ -176,14 +176,14 @@ static void packet_take_spare(struct http_packet_t *pkt) static ssize_t packet_find_chunked_size(struct http_packet_t *pkt) { - // NOTE: - // chunks can have trailers which are - // tacked on http header fields. - // NOTE: - // chunks may also have extensions. - // No one uses or supports them. - - // Find end of size string + /* NOTE: + chunks can have trailers which are + tacked on http header fields. + NOTE: + chunks may also have extensions. + No one uses or supports them. */ + + /* Find end of size string */ if (pkt->filled_size >= SSIZE_MAX) ERR_AND_EXIT("Buffer beyond sane size"); @@ -195,25 +195,25 @@ static ssize_t packet_find_chunked_size(struct http_packet_t *pkt) uint8_t *buf = pkt->buffer; if (size_end < 0) { - // No extension + /* No extension */ if (i + 1 < max && - (buf[i] == '\r' && // CR - buf[i + 1] == '\n')) { // LF + (buf[i] == '\r' && /* CR */ + buf[i + 1] == '\n')) { /* LF */ size_end = i + 1; miniheader_end = size_end; delimiter_start = i; break; } - // No extension - if (buf[i] == '\n') { // LF + /* No extension */ + if (buf[i] == '\n') { /* LF */ size_end = i; miniheader_end = size_end; delimiter_start = i; break; } - // Has extensions + /* Has extensions */ if (buf[i] == ';') { size_end = i; continue; @@ -221,14 +221,14 @@ static ssize_t packet_find_chunked_size(struct http_packet_t *pkt) } if (miniheader_end < 0) { if (i + 1 < max && - (buf[i] == '\r' && // CR - buf[i + 1] == '\n')) { // LF + (buf[i] == '\r' && /* CR */ + buf[i + 1] == '\n')) { /* LF */ miniheader_end = i + 1; delimiter_start = i; break; } - if (buf[i] == '\n') { // LF + if (buf[i] == '\n') { /* LF */ miniheader_end = i; delimiter_start = i; break; @@ -237,14 +237,14 @@ static ssize_t packet_find_chunked_size(struct http_packet_t *pkt) } if (miniheader_end < 0) { - // NOTE: knowing just the size field - // is not enough since the extensions - // are not included in the size + /* NOTE: knowing just the size field + is not enough since the extensions + are not included in the size */ NOTE("failed to find chunk mini-header so far"); return -1; } - // Temporary stringification for strtol() + /* Temporary stringification for strtol() */ uint8_t original_char = *(pkt->buffer + size_end); *(pkt->buffer + size_end) = '\0'; size_t size = strtoul((char *)pkt->buffer, NULL, 16); @@ -254,31 +254,31 @@ static ssize_t packet_find_chunked_size(struct http_packet_t *pkt) ERR_AND_EXIT("chunk size is insane"); if (size > 0) { - // Regular chunk - ssize_t chunk_size = (ssize_t) size; // Chunk body - chunk_size += miniheader_end + 1; // Mini-header - chunk_size += 2; // Trailing CRLF + /* Regular chunk */ + ssize_t chunk_size = (ssize_t) size; /* Chunk body */ + chunk_size += miniheader_end + 1; /* Mini-header */ + chunk_size += 2; /* Trailing CRLF */ NOTE("HTTP: Chunk size: %lu", chunk_size); return (ssize_t) chunk_size; } - // Terminator chunk - // May have trailers in body + /* Terminator chunk + May have trailers in body */ ssize_t full_size = -1; for (ssize_t i = delimiter_start; i < max; i++) { uint8_t *buf = pkt->buffer; if (i + 3 < max && - (buf[i] == '\r' && // CR - buf[i + 1] == '\n' && // LF - buf[i + 2] == '\r' && // CR - buf[i + 3] == '\n')) { // LF + (buf[i] == '\r' && /* CR */ + buf[i + 1] == '\n' && /* LF */ + buf[i + 2] == '\r' && /* CR */ + buf[i + 3] == '\n')) { /* LF */ full_size = i + 4; break; } if (i + 1 < max && - buf[i] == '\n' && // LF - buf[i + 1] == '\n') { // LF + buf[i] == '\n' && /* LF */ + buf[i + 1] == '\n') { /* LF */ full_size = i + 2; break; } @@ -306,9 +306,9 @@ static ssize_t packet_get_header_size(struct http_packet_t *pkt) * http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.3 */ - // Find header + /* Find header */ for (size_t i = 0; i < pkt->filled_size && i < SSIZE_MAX; i++) { - // two \r\n pairs + /* two \r\n pairs */ if ((i + 3) < pkt->filled_size && '\r' == pkt->buffer[i] && '\n' == pkt->buffer[i + 1] && @@ -318,7 +318,7 @@ static ssize_t packet_get_header_size(struct http_packet_t *pkt) goto found; } - // two \n pairs + /* two \n pairs */ if ((i + 1) < pkt->filled_size && '\n' == pkt->buffer[i] && '\n' == pkt->buffer[i + 1]) { @@ -361,12 +361,12 @@ enum http_request_t packet_find_type(struct http_packet_t *pkt) ssize_t header_size_raw = packet_get_header_size(pkt); if (header_size_raw < 0) { - // We don't have the header yet + /* We don't have the header yet */ goto do_ret; } size_t header_size = (size_t) header_size_raw; - // Try Transfer-Encoding Chunked + /* Try Transfer-Encoding Chunked */ char xfer_encode_str[] = "Transfer-Encoding: chunked"; size_t xfer_encode_str_size = sizeof(xfer_encode_str) - 1; uint8_t *xfer_encode_pos = memmem(pkt->buffer, header_size, @@ -378,7 +378,7 @@ enum http_request_t packet_find_type(struct http_packet_t *pkt) goto do_ret; } - // Try Content-Length + /* Try Content-Length */ char content_length_str[] = "Content-Length: "; ssize_t contlen_size = inspect_header_field(pkt, header_size, @@ -389,8 +389,8 @@ enum http_request_t packet_find_type(struct http_packet_t *pkt) goto do_ret; } - // GET requests (start with GET) or answers from the server (start - // with HTTP) + /* GET requests (start with GET) or answers from the server (start + with HTTP) */ if (doesMatch("GET", 3, pkt->buffer, pkt->filled_size) || doesMatch("HTTP", 4, pkt->buffer, pkt->filled_size)) { size = header_size; @@ -398,7 +398,7 @@ enum http_request_t packet_find_type(struct http_packet_t *pkt) goto do_ret; } - // No size was detectable yet header was found + /* No size was detectable yet header was found */ type = HTTP_UNKNOWN; size = 0; @@ -412,7 +412,7 @@ size_t packet_pending_bytes(struct http_packet_t *pkt) { struct http_message_t *msg = pkt->parent_message; - // Check Cache + /* Check Cache */ if (pkt->expected_size > 0) goto pending_known; @@ -420,13 +420,13 @@ size_t packet_pending_bytes(struct http_packet_t *pkt) msg->type = packet_find_type(pkt); if (HTTP_CHUNKED == msg->type) { - // Note: this was the packet with the - // header of our chunked message. + /* Note: this was the packet with the + header of our chunked message. */ - // Save any non-header data we got + /* Save any non-header data we got */ ssize_t header_size = packet_get_header_size(pkt); - // Sanity check + /* Sanity check */ if (header_size < 0 || (size_t)header_size > pkt->filled_size) ERR_AND_EXIT("HTTP: Could not find header twice"); @@ -441,12 +441,12 @@ size_t packet_pending_bytes(struct http_packet_t *pkt) if (HTTP_CHUNKED == msg->type) { if (pkt->filled_size == 0) { - // Grab chunk's mini-header + /* Grab chunk's mini-header */ goto pending_known; } if (pkt->expected_size == 0) { - // Check chunk's mini-header + /* Check chunk's mini-header */ ssize_t size = packet_find_chunked_size(pkt); if (size <= 0) { ERR("============================================="); @@ -466,16 +466,16 @@ size_t packet_pending_bytes(struct http_packet_t *pkt) goto pending_known; } if (HTTP_HEADER_ONLY == msg->type) { - // Note: we can only know it is header only - // when the buffer already contains the header. - // So this next call cannot fail. + /* Note: we can only know it is header only + when the buffer already contains the header. + So this next call cannot fail. */ pkt->expected_size = (size_t) packet_get_header_size(pkt); msg->claimed_size = pkt->expected_size; goto pending_known; } if (HTTP_CONTENT_LENGTH == msg->type) { - // Note: find_header() has - // filled msg's claimed_size + /* Note: find_header() has + filled msg's claimed_size */ msg->claimed_size = msg->claimed_size; pkt->expected_size = msg->claimed_size; goto pending_known; @@ -483,7 +483,7 @@ size_t packet_pending_bytes(struct http_packet_t *pkt) pending_known: - // Save excess data + /* Save excess data */ if (pkt->expected_size && pkt->filled_size > pkt->expected_size) packet_store_excess(pkt); @@ -493,13 +493,13 @@ size_t packet_pending_bytes(struct http_packet_t *pkt) if (expected == 0) expected = pkt->buffer_capacity; - // Sanity check + /* Sanity check */ if (expected < pkt->filled_size) ERR_AND_EXIT("Expected cannot be larger than filled"); size_t pending = expected - pkt->filled_size; - // Expand buffer as needed + /* Expand buffer as needed */ while (pending + pkt->filled_size > pkt->buffer_capacity) { ssize_t size_added = packet_expand(pkt); if (size_added < 0) { @@ -535,7 +535,7 @@ void packet_mark_received(struct http_packet_t *pkt, size_t received) ERR_AND_EXIT("Overflowed packet's buffer"); if (pkt->expected_size && pkt->filled_size > pkt->expected_size) { - // Store excess data + /* Store excess data */ packet_store_excess(pkt); } } @@ -555,7 +555,7 @@ struct http_packet_t *packet_new(struct http_message_t *parent_msg) pkt->parent_message = parent_msg; pkt->expected_size = 0; - // Claim any spare data from prior packets + /* Claim any spare data from prior packets */ packet_take_spare(pkt); if (pkt->buffer == NULL) { @@ -566,7 +566,7 @@ struct http_packet_t *packet_new(struct http_message_t *parent_msg) return NULL; } - // Assemble packet + /* Assemble packet */ pkt->buffer = buf; pkt->buffer_capacity = capacity; pkt->filled_size = 0; @@ -581,7 +581,7 @@ void packet_free(struct http_packet_t *pkt) free(pkt); } -#define MAX_PACKET_SIZE (1 << 26) // 64MiB +#define MAX_PACKET_SIZE (1 << 26) /* 64MiB */ ssize_t packet_expand(struct http_packet_t *pkt) { size_t cur_size = pkt->buffer_capacity; @@ -594,7 +594,7 @@ ssize_t packet_expand(struct http_packet_t *pkt) uint8_t *new_buf = realloc(pkt->buffer, new_size); if (new_buf == NULL) { - // If realloc fails the original buffer is still valid + /* If realloc fails the original buffer is still valid */ WARN("Failed to expand packet"); return 0; } @@ -34,13 +34,13 @@ struct http_message_t { size_t unreceived_size; uint8_t is_completed; - // Detected from child packets + /* Detected from child packets */ size_t claimed_size; size_t received_size; }; struct http_packet_t { - // Cache + /* Cache */ size_t header_size; size_t filled_size; diff --git a/src/ippusbxd.c b/src/ippusbxd.c index 36681a4..f3b6964 100644 --- a/src/ippusbxd.c +++ b/src/ippusbxd.c @@ -137,25 +137,25 @@ static void *service_connection(void *arg_void) NOTE("Thread #%d: Starting", thread_num); - // Detach this thread so that the main thread does not need to join this thread - // after termination for clean-up + /* Detach this thread so that the main thread does not need to join this thread + after termination for clean-up */ pthread_detach(pthread_self()); - // Register clean-up handler + /* Register clean-up handler */ pthread_cleanup_push(cleanup_handler, &thread_num); - // Allow immediate cancelling of this thread + /* Allow immediate cancelling of this thread */ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); - // classify priority + /* classify priority */ struct usb_conn_t *usb = NULL; int usb_failed = 0; while (!arg->tcp->is_closed && usb_failed == 0 && !g_options.terminate) { struct http_message_t *server_msg = NULL; struct http_message_t *client_msg = NULL; - // Client's request + /* Client's request */ client_msg = http_message_new(); if (client_msg == NULL) { ERR("Thread #%d: Failed to create client message", thread_num); @@ -199,8 +199,8 @@ static void *service_connection(void *arg_void) thread_num, client_msg, pkt, pkt->filled_size, hexdump(pkt->buffer, (int)pkt->filled_size)); - // In no-printer mode we simply ignore passing the - // client message on to the printer + /* In no-printer mode we simply ignore passing the + client message on to the printer */ if (arg->usb_sock != NULL) { if (usb_conn_packet_send(usb, pkt) != 0) { ERR("Thread #%d: M %p P %p: Interface #%d: Unable to send client package via USB", @@ -229,7 +229,7 @@ static void *service_connection(void *arg_void) goto cleanup_subconn; - // Server's response + /* Server's response */ server_msg = http_message_new(); if (server_msg == NULL) { ERR("Thread #%d: Failed to create server message", @@ -252,16 +252,16 @@ static void *service_connection(void *arg_void) goto cleanup_subconn; } } else { - // In no-printer mode we "invent" the answer - // of the printer, a simple HTML message as - // a pseudo web interface + /* In no-printer mode we "invent" the answer + of the printer, a simple HTML message as + a pseudo web interface */ pkt = packet_new(server_msg); snprintf((char*)(pkt->buffer), pkt->buffer_capacity - 1, "HTTP/1.1 200 OK\r\nContent-Type: text/html; name=ippusbxd.html; charset=UTF-8\r\n\r\n<html><h2>ippusbxd</h2><p>Debug/development mode without connection to IPP-over-USB printer</p></html>\r\n"); pkt->filled_size = 183; - // End the TCP connection, so that a - // web browser does not wait for more data + /* End the TCP connection, so that a + web browser does not wait for more data */ server_msg->is_completed = 1; arg->tcp->is_closed = 1; } @@ -314,7 +314,7 @@ static void *service_connection(void *arg_void) tcp_conn_close(arg->tcp); free(arg); - // Execute clean-up handler + /* Execute clean-up handler */ pthread_cleanup_pop(1); pthread_exit(NULL); @@ -322,10 +322,10 @@ static void *service_connection(void *arg_void) static void start_daemon() { - // Capture USB device if not in no-printer mode + /* Capture USB device if not in no-printer mode */ struct usb_sock_t *usb_sock; - // Termination flag + /* Termination flag */ g_options.terminate = 0; if (g_options.noprinter_mode == 0) { @@ -335,7 +335,7 @@ static void start_daemon() } else usb_sock = NULL; - // Capture a socket + /* Capture a socket */ uint16_t desired_port = g_options.desired_port; g_options.tcp_socket = NULL; g_options.tcp6_socket = NULL; @@ -344,14 +344,14 @@ static void start_daemon() g_options.tcp6_socket = tcp6_open(desired_port, g_options.interface); if (g_options.tcp_socket || g_options.tcp6_socket || g_options.only_desired_port) break; - // Search for a free port + /* Search for a free port */ desired_port ++; - // We failed with 0 as port number or we reached the max - // port number + /* We failed with 0 as port number or we reached the max + port number */ if (desired_port == 1 || desired_port == 0) - // IANA recommendation of 49152 to 65535 for ephemeral - // ports - // https://en.wikipedia.org/wiki/Ephemeral_port + /* IANA recommendation of 49152 to 65535 for ephemeral + ports + https://en.wikipedia.org/wiki/Ephemeral_port */ desired_port = 49152; NOTE("Access to desired port failed, trying alternative port %d", desired_port); } @@ -374,15 +374,15 @@ static void start_daemon() NOTE("Port: %d, IPv4 %savailable, IPv6 %savailable", g_options.real_port, g_options.tcp_socket ? "" : "not ", g_options.tcp6_socket ? "" : "not "); - // Lose connection to caller + /* Lose connection to caller */ uint16_t pid; if (!g_options.nofork_mode && (pid = fork()) > 0) { printf("%u|", pid); exit(0); } - // Redirect SIGINT and SIGTERM so that we do a proper shutdown, unregistering - // the printer from DNS-SD + /* Redirect SIGINT and SIGTERM so that we do a proper shutdown, unregistering + the printer from DNS-SD */ #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ sigset(SIGTERM, sigterm_handler); sigset(SIGINT, sigterm_handler); @@ -405,18 +405,18 @@ static void start_daemon() NOTE("Using signal handler SIGNAL"); #endif /* HAVE_SIGSET */ - // Register for unplug event + /* Register for unplug event */ if (usb_can_callback(usb_sock)) usb_register_callback(usb_sock); - // DNS-SD-broadcast the printer on the local machine so - // that cups-browsed and ippfind will discover it + /* DNS-SD-broadcast the printer on the local machine so + that cups-browsed and ippfind will discover it */ if (usb_sock && g_options.nobroadcast == 0) { if (dnssd_init() == -1) goto cleanup_tcp; } - // Main loop + /* Main loop */ int i = 0; pthread_mutex_init(&thread_register_mutex, NULL); while (!g_options.terminate) { @@ -431,8 +431,8 @@ static void start_daemon() args->thread_num = i; args->usb_sock = usb_sock; - // For each request/response round we use the socket (IPv4 or - // IPv6) which receives data first + /* For each request/response round we use the socket (IPv4 or + IPv6) which receives data first */ args->tcp = tcp_conn_select(g_options.tcp_socket, g_options.tcp6_socket); if (g_options.terminate) goto cleanup_thread; @@ -469,13 +469,13 @@ static void start_daemon() } cleanup_tcp: - // Stop DNS-SD advertising of the printer + /* Stop DNS-SD advertising of the printer */ if (g_options.dnssd_data != NULL) dnssd_shutdown(); - // Cancel communication threads which did not terminate by themselves when - // stopping ippusbxd, so that no USB communication with the printer can - // happen after the final reset + /* Cancel communication threads which did not terminate by themselves when + stopping ippusbxd, so that no USB communication with the printer can + happen after the final reset */ while (num_service_threads) { NOTE("Thread #%d did not terminate, canceling it now ...", service_threads[0]->thread_num); @@ -485,17 +485,17 @@ static void start_daemon() usleep(1000000); } - // Wait for USB unplug event observer thread to terminate + /* Wait for USB unplug event observer thread to terminate */ pthread_join(g_options.usb_event_thread_handle, NULL); - // TCP clean-up + /* TCP clean-up */ if (g_options.tcp_socket!= NULL) tcp_close(g_options.tcp_socket); if (g_options.tcp6_socket!= NULL) tcp_close(g_options.tcp6_socket); cleanup_usb: - // USB clean-up and final reset of the printer + /* USB clean-up and final reset of the printer */ if (usb_sock != NULL) usb_close(usb_sock); return; @@ -560,7 +560,7 @@ int main(int argc, char *argv[]) case 'P': { long long port = 0; - // Request specific port + /* Request specific port */ port = atoi(optarg); if (port < 0) { ERR("Port number must be non-negative"); @@ -579,7 +579,7 @@ int main(int argc, char *argv[]) break; } case 'i': - // Request a specific network interface + /* Request a specific network interface */ g_options.interface = strdup(optarg); break; case 'l': diff --git a/src/logging.c b/src/logging.c index eba1e15..e60beb9 100644 --- a/src/logging.c +++ b/src/logging.c @@ -47,23 +47,23 @@ char* hexdump (void *addr, int len) { return "*** Failed to allocate memory for hex dump! ***"; outbufp = outbuf; - // Process every byte in the data. + /* Process every byte in the data. */ for (i = 0; i < len; i++) { - if ((i % 16) == 0) { // Multiple of 16 means new line (with line offset). - if (i != 0) { // Just don't print ASCII for the zeroth line. + if ((i % 16) == 0) { /* Multiple of 16 means new line (with line offset). */ + if (i != 0) { /* Just don't print ASCII for the zeroth line. */ sprintf (outbufp, " %s\n", linebuf); outbufp += strlen(linebuf) + 3; } - // Output the offset. + /* Output the offset. */ sprintf (outbufp, " %08x ", i); outbufp += 11; } - // Now the hex code for the specific character. + /* Now the hex code for the specific character. */ sprintf (outbufp, " %02x", pc[i]); outbufp += 3; - // And store a printable ASCII character for later. + /* And store a printable ASCII character for later. */ if ((pc[i] < 0x20) || (pc[i] > 0x7e)) linebuf[i % 16] = '.'; else @@ -71,14 +71,14 @@ char* hexdump (void *addr, int len) { linebuf[(i % 16) + 1] = '\0'; } - // Pad out last line if not exactly 16 characters. + /* Pad out last line if not exactly 16 characters. */ while ((i % 16) != 0) { sprintf (outbufp, " "); outbufp += 3; i++; } - // And print the final ASCII bit. + /* And print the final ASCII bit. */ sprintf (outbufp, " %s\n", linebuf); return outbuf; diff --git a/src/logging.h b/src/logging.h index 45b00d8..1f584ca 100644 --- a/src/logging.h +++ b/src/logging.h @@ -13,7 +13,7 @@ * limitations under the License. */ #pragma once -#include <pthread.h> // For pthread_self() +#include <pthread.h> /* For pthread_self() */ #include "options.h" #include "dnssd.h" #define TID() (pthread_self()) diff --git a/src/options.h b/src/options.h index 96ab798..2de31a2 100644 --- a/src/options.h +++ b/src/options.h @@ -24,21 +24,21 @@ enum log_target { }; struct options { - // Runtime configuration + /* Runtime configuration */ uint16_t desired_port; int only_desired_port; uint16_t real_port; char *interface; enum log_target log_destination; - // Behavior + /* Behavior */ int help_mode; int verbose_mode; int nofork_mode; int noprinter_mode; int nobroadcast; - // Printer identity + /* Printer identity */ unsigned char *serial_num; int vendor_id; int product_id; @@ -46,7 +46,7 @@ struct options { int device; char *device_id; - // Global variables + /* Global variables */ int terminate; dnssd_t *dnssd_data; pthread_t usb_event_thread_handle; @@ -43,24 +43,24 @@ struct tcp_sock_t *tcp_open(uint16_t port, char* interface) goto error; } - // Open [S]ocket [D]escriptor + /* Open [S]ocket [D]escriptor */ this->sd = -1; this->sd = socket(AF_INET, SOCK_STREAM, 0); if (this->sd < 0) { ERR("IPv4 socket open failed"); goto error; } - // Set SO_REUSEADDR option to allow for a clean host/port unbinding even with - // pending requests on shutdown of ippusbxd. Otherwise the port will stay - // unavailable for a certain kernel-defined timeout. See also - // http://stackoverflow.com/questions/10619952/how-to-completely-destroy-a-socket-connection-in-c + /* Set SO_REUSEADDR option to allow for a clean host/port unbinding even with + pending requests on shutdown of ippusbxd. Otherwise the port will stay + unavailable for a certain kernel-defined timeout. See also + http://stackoverflow.com/questions/10619952/how-to-completely-destroy-a-socket-connection-in-c */ int true = 1; if (setsockopt(this->sd, SOL_SOCKET, SO_REUSEADDR, &true, sizeof(int)) == -1) { ERR("IPv4 setting socket options failed"); goto error; } - // Find the IP address for the selected interface + /* Find the IP address for the selected interface */ struct ifaddrs *ifaddr, *ifa; getifaddrs(&ifaddr); for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { @@ -75,17 +75,17 @@ struct tcp_sock_t *tcp_open(uint16_t port, char* interface) goto error; } - // Configure socket params + /* Configure socket params */ struct sockaddr_in addr, *if_addr; if_addr = (struct sockaddr_in *) ifa->ifa_addr; memset(&addr, 0, sizeof addr); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = if_addr->sin_addr.s_addr; - //addr.sin_addr.s_addr = htonl(0xC0A8000F); + /* addr.sin_addr.s_addr = htonl(0xC0A8000F); */ NOTE("IPv4: Binding to %s:%d", inet_ntoa(if_addr->sin_addr), port); - // Bind to the interface/IP/port + /* Bind to the interface/IP/port */ if (bind(this->sd, (struct sockaddr *)&addr, sizeof addr) < 0) { @@ -95,7 +95,7 @@ struct tcp_sock_t *tcp_open(uint16_t port, char* interface) goto error; } - // Let kernel over-accept max number of connections + /* Let kernel over-accept max number of connections */ if (listen(this->sd, HTTP_MAX_PENDING_CONNS) < 0) { ERR("IPv4 listen failed on socket"); goto error; @@ -121,24 +121,24 @@ struct tcp_sock_t *tcp6_open(uint16_t port, char* interface) goto error; } - // Open [S]ocket [D]escriptor + /* Open [S]ocket [D]escriptor */ this->sd = -1; this->sd = socket(AF_INET6, SOCK_STREAM, 0); if (this->sd < 0) { ERR("Ipv6 socket open failed"); goto error; } - // Set SO_REUSEADDR option to allow for a clean host/port unbinding even with - // pending requests on shutdown of ippusbxd. Otherwise the port will stay - // unavailable for a certain kernel-defined timeout. See also - // http://stackoverflow.com/questions/10619952/how-to-completely-destroy-a-socket-connection-in-c + /* Set SO_REUSEADDR option to allow for a clean host/port unbinding even with + pending requests on shutdown of ippusbxd. Otherwise the port will stay + unavailable for a certain kernel-defined timeout. See also + http://stackoverflow.com/questions/10619952/how-to-completely-destroy-a-socket-connection-in-c */ int true = 1; if (setsockopt(this->sd, SOL_SOCKET, SO_REUSEADDR, &true, sizeof(int)) == -1) { ERR("IPv6 setting socket options failed"); goto error; } - // Find the IP address for the selected interface + /* Find the IP address for the selected interface */ struct ifaddrs *ifaddr, *ifa; getifaddrs(&ifaddr); for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { @@ -153,7 +153,7 @@ struct tcp_sock_t *tcp6_open(uint16_t port, char* interface) goto error; } - // Configure socket params + /* Configure socket params */ struct sockaddr_in6 addr, *if_addr; char buf[64]; if_addr = (struct sockaddr_in6 *) ifa->ifa_addr; @@ -170,7 +170,7 @@ struct tcp_sock_t *tcp6_open(uint16_t port, char* interface) } NOTE("IPv6: Binding to [%s]:%d", buf, port); - // Bind to the interface/IP/port + /* Bind to the interface/IP/port */ if (bind(this->sd, (struct sockaddr *)&addr, sizeof addr) < 0) { @@ -180,7 +180,7 @@ struct tcp_sock_t *tcp6_open(uint16_t port, char* interface) goto error; } - // Let kernel over-accept max number of connections + /* Let kernel over-accept max number of connections */ if (listen(this->sd, HTTP_MAX_PENDING_CONNS) < 0) { ERR("IPv6 listen failed on socket"); goto error; @@ -224,7 +224,7 @@ uint16_t tcp_port_number_get(struct tcp_sock_t *sock) struct http_packet_t *tcp_packet_get(struct tcp_conn_t *tcp, struct http_message_t *msg) { - // Alloc packet ==---------------------------------------------------== + /* Alloc packet ==---------------------------------------------------== */ struct http_packet_t *pkt = packet_new(msg); if (pkt == NULL) { ERR("failed to create packet for incoming tcp message"); @@ -258,7 +258,7 @@ struct http_packet_t *tcp_packet_get(struct tcp_conn_t *tcp, if (gotten_size == 0) { tcp->is_closed = 1; if (pkt->filled_size == 0) { - // Client closed TCP conn + /* Client closed TCP conn */ goto error; } else { break; @@ -364,10 +364,10 @@ struct tcp_conn_t *tcp_conn_select(struct tcp_sock_t *sock, void tcp_conn_close(struct tcp_conn_t *conn) { - // Unbind host/port cleanly even with pending requests. Otherwise - // the port will stay unavailable for a certain kernel-defined - // timeout. See also - // http://stackoverflow.com/questions/10619952/how-to-completely-destroy-a-socket-connection-in-c + /* Unbind host/port cleanly even with pending requests. Otherwise + the port will stay unavailable for a certain kernel-defined + timeout. See also + http://stackoverflow.com/questions/10619952/how-to-completely-destroy-a-socket-connection-in-c */ shutdown(conn->sd, SHUT_RDWR); close(conn->sd); @@ -62,7 +62,7 @@ static int count_ippoverusb_interfaces(struct libusb_config_descriptor *config) interface_num, alt_num, alt->bInterfaceClass, alt->bInterfaceSubClass, alt->bInterfaceProtocol); - // Check for IPP over USB interfaces + /* Check for IPP over USB interfaces */ if (!is_ippusb_interface(alt)) continue; @@ -98,14 +98,14 @@ static int is_our_device(libusb_device *dev, libusb_device_handle *handle = NULL; int status = libusb_open(dev, &handle); if (status != 0) { - // Device turned off or disconnected, we cannot retrieve its - // serial number any more, so we identify it via bus and device - // addresses + /* Device turned off or disconnected, we cannot retrieve its + serial number any more, so we identify it via bus and device + addresses */ return (bus == libusb_get_bus_number(dev) && dev_addr == libusb_get_device_address(dev)); } else { - // Device is turned on and connected, read out its serial number - // and use the serial number for identification + /* Device is turned on and connected, read out its serial number + and use the serial number for identification */ status = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, serial, SERIAL_MAX); @@ -139,16 +139,16 @@ int get_device_id(struct libusb_device_handle *handle, return (-1); } - // Extract the length of the device ID string from the first two - // bytes. The 1284 spec says the length is stored MSB first... + /* Extract the length of the device ID string from the first two + bytes. The 1284 spec says the length is stored MSB first... */ length = (int)((((unsigned)buffer[0] & 255) << 8) | ((unsigned)buffer[1] & 255)); - // Check to see if the length is larger than our buffer or less than 14 bytes - // (the minimum valid device ID is "MFG:x;MDL:y;" with 2 bytes for the - // length). - // If the length is out-of-range, assume that the vendor incorrectly - // implemented the 1284 spec and re-read the length as LSB first,.. + /* Check to see if the length is larger than our buffer or less than 14 bytes + (the minimum valid device ID is "MFG:x;MDL:y;" with 2 bytes for the + length). + If the length is out-of-range, assume that the vendor incorrectly + implemented the 1284 spec and re-read the length as LSB first, ... */ if (length > bufsize || length < 14) length = (int)((((unsigned)buffer[1] & 255) << 8) | ((unsigned)buffer[0] & 255)); @@ -157,7 +157,7 @@ int get_device_id(struct libusb_device_handle *handle, length = bufsize; if (length < 14) { - // Invalid device ID, clear it! + /* Invalid device ID, clear it! */ *buffer = '\0'; return (-1); } @@ -188,7 +188,7 @@ struct usb_sock_t *usb_open() goto error; } - // Discover device and count interfaces ==---------------------------== + /* Discover device and count interfaces ==---------------------------== */ int selected_config = -1; unsigned int selected_ipp_interface_count = 0; int auto_pick = !((g_options.vendor_id && @@ -219,7 +219,7 @@ struct usb_sock_t *usb_open() if (!is_our_device(candidate, desc)) continue; - // Save VID/PID for exit-on-unplug + /* Save VID/PID for exit-on-unplug */ if (g_options.vendor_id == 0) g_options.vendor_id = desc.idVendor; if (g_options.product_id == 0) @@ -252,7 +252,7 @@ struct usb_sock_t *usb_open() goto found_device; } - // CONFTEST: Two or more interfaces are required + /* CONFTEST: Two or more interfaces are required */ if (interface_count == 1) { CONF("usb device has only one ipp interface " "in violation of standard"); @@ -276,14 +276,14 @@ struct usb_sock_t *usb_open() goto error; } - // Open the printer ==-----------------------------------------------== + /* Open the printer ==-----------------------------------------------== */ status = libusb_open(printer_device, &usb->printer); if (status != 0) { ERR("failed to open device"); goto error; } - // Open every IPP-USB interface ==-----------------------------------== + /* Open every IPP-USB interface ==-----------------------------------== */ usb->num_interfaces = selected_ipp_interface_count; usb->interfaces = calloc(usb->num_interfaces, sizeof(*usb->interfaces)); @@ -315,7 +315,7 @@ struct usb_sock_t *usb_open() const struct libusb_interface_descriptor *alt = NULL; alt = &interf->altsetting[alt_num]; - // Get the IEE-1284 device ID + /* Get the IEE-1284 device ID */ if (usb->device_id == NULL) { usb->device_id = calloc(2048, sizeof(char)); if (usb->device_id == NULL) { @@ -337,7 +337,7 @@ struct usb_sock_t *usb_open() } } - // Skip non-IPP-USB interfaces + /* Skip non-IPP-USB interfaces */ if (!is_ippusb_interface(alt)) continue; @@ -348,7 +348,7 @@ struct usb_sock_t *usb_open() uf->libusb_interface_index = alt->bInterfaceNumber; uf->interface_alt = alt_num; - // Store interface's two endpoints + /* Store interface's two endpoints */ for (int end_i = 0; end_i < alt->bNumEndpoints; end_i++) { const struct libusb_endpoint_descriptor *end; @@ -356,8 +356,8 @@ struct usb_sock_t *usb_open() usb->max_packet_size = end->wMaxPacketSize; - // High bit set means endpoint - // is an INPUT or IN endpoint. + /* High bit set means endpoint + is an INPUT or IN endpoint. */ uint8_t address = end->bEndpointAddress; if (address & 0x80) uf->endpoint_in = address; @@ -378,7 +378,7 @@ struct usb_sock_t *usb_open() libusb_free_config_descriptor(config); libusb_free_device_list(device_list, 1); - // Pour interfaces into pool ==--------------------------------------== + /* Pour interfaces into pool ==--------------------------------------== */ usb->num_avail = usb->num_interfaces; usb->interface_pool = calloc(usb->num_avail, sizeof(*usb->interface_pool)); @@ -391,14 +391,14 @@ struct usb_sock_t *usb_open() } NOTE("USB interfaces pool: %d interfaces", usb->num_avail); - // Stale lock + /* Stale lock */ status_lock = sem_init(&usb->num_staled_lock, 0, 1); if (status_lock != 0) { ERR("Failed to create num_staled lock"); goto error; } - // Pool management lock + /* Pool management lock */ status_lock = sem_init(&usb->pool_manage_lock, 0, 1); if (status_lock != 0) { ERR("Failed to create pool management lock"); @@ -425,7 +425,7 @@ struct usb_sock_t *usb_open() void usb_close(struct usb_sock_t *usb) { - // Release interfaces + /* Release interfaces */ for (uint32_t i = 0; i < usb->num_interfaces; i++) { int number = usb->interfaces[i].interface_number; libusb_release_interface(usb->printer, number); @@ -481,18 +481,18 @@ static int LIBUSB_CALL usb_exit_on_unplug(libusb_context *context, libusb_get_device_descriptor(device, &desc); if (is_our_device(device, desc)) { - // We prefer an immediate shutdown with only DNS-SD and TCP - // clean-up here as by a regular sgutdown request via termination - // flag g_options.terminate there can still happen USB - // communication attempts with long timeouts, making ippusbxd get - // stuck for a significant time. This way we immediately stop the - // DNS-SD advertising and release the host/port binding. - - // Unregister DNS-SD for printer on Avahi + /* We prefer an immediate shutdown with only DNS-SD and TCP + clean-up here as by a regular sgutdown request via termination + flag g_options.terminate there can still happen USB + communication attempts with long timeouts, making ippusbxd get + stuck for a significant time. This way we immediately stop the + DNS-SD advertising and release the host/port binding. */ + + /* Unregister DNS-SD for printer on Avahi */ if (g_options.dnssd_data != NULL) dnssd_shutdown(); - // TCP clean-up + /* TCP clean-up */ if (g_options.tcp_socket!= NULL) tcp_close(g_options.tcp_socket); if (g_options.tcp6_socket!= NULL) @@ -511,8 +511,8 @@ static void *usb_pump_events(void *user_data) NOTE("USB unplug event observer thread starting"); while (!g_options.terminate) { - // NOTE: This is a blocking call so - // no need for sleep() + /* NOTE: This is a blocking call so + no need for sleep() */ struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 500000; @@ -531,13 +531,13 @@ void usb_register_callback(struct usb_sock_t *usb) int status = libusb_hotplug_register_callback(NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, - // Note: libusb's enum has no default value - // a bug has been filled with libusb. - // Please switch the below line to 0 - // once the issue has been fixed in - // deployed versions of libusb - // https://github.com/libusb/libusb/issues/35 - // 0, + /* Note: libusb's enum has no default value + a bug has been filled with libusb. + Please switch the below line to 0 + once the issue has been fixed in + deployed versions of libusb + https://github.com/libusb/libusb/issues/35 */ + /* 0, */ LIBUSB_HOTPLUG_ENUMERATE, g_options.vendor_id, g_options.product_id, @@ -635,7 +635,7 @@ struct usb_conn_t *usb_conn_acquire(struct usb_sock_t *usb) conn->interface = usb->interfaces + conn->interface_index; struct usb_interface *uf = conn->interface; - // Sanity check: Is the interface still free + /* Sanity check: Is the interface still free */ if (sem_trywait(&uf->lock)) { ERR("Interface #%d (%d) already in use!", conn->interface_index, @@ -643,23 +643,23 @@ struct usb_conn_t *usb_conn_acquire(struct usb_sock_t *usb) goto acquire_error; } - // Make kernel release interface + /* Make kernel release interface */ if (libusb_kernel_driver_active(usb->printer, uf->libusb_interface_index) == 1) { - // Only linux supports this - // other platforms will fail - // thus we ignore the error code - // it either works or it does not + /* Only linux supports this + other platforms will fail + thus we ignore the error code + it either works or it does not */ libusb_detach_kernel_driver(usb->printer, uf->libusb_interface_index); } - // Claim the whole interface + /* Claim the whole interface */ int status = 0; do { - // Spinlock-like - // Libusb does not offer a blocking call - // so we're left with a spinlock + /* Spinlock-like + Libusb does not offer a blocking call + so we're left with a spinlock */ status = libusb_claim_interface(usb->printer, uf->libusb_interface_index); if (status) NOTE("Failed to claim interface %d, retrying", conn->interface_index); switch (status) { @@ -677,12 +677,12 @@ struct usb_conn_t *usb_conn_acquire(struct usb_sock_t *usb) if (g_options.terminate) goto acquire_error; - // Select the IPP-USB alt setting of the interface + /* Select the IPP-USB alt setting of the interface */ libusb_set_interface_alt_setting(usb->printer, uf->libusb_interface_index, uf->interface_alt); - // Take successfully acquired interface from the pool + /* Take successfully acquired interface from the pool */ usb->num_taken++; usb->num_avail--; } @@ -703,21 +703,21 @@ void usb_conn_release(struct usb_conn_t *conn) { int status = 0; do { - // Spinlock-like - // Libusb does not offer a blocking call - // so we're left with a spinlock + /* Spinlock-like + libusb does not offer a blocking call + so we're left with a spinlock */ status = libusb_release_interface(usb->printer, conn->interface->libusb_interface_index); if (status) NOTE("Failed to release interface %d, retrying", conn->interface_index); } while (status != 0 && !g_options.terminate); - // Return usb interface to pool + /* Return usb interface to pool */ usb->num_taken--; usb->num_avail++; uint32_t slot = usb->num_taken; usb->interface_pool[slot] = conn->interface_index; - // Release our interface lock + /* Release our interface lock */ sem_post(&conn->interface->lock); free(conn); @@ -728,7 +728,7 @@ void usb_conn_release(struct usb_conn_t *conn) int usb_conn_packet_send(struct usb_conn_t *conn, struct http_packet_t *pkt) { int size_sent = 0; - const int timeout = 1000; // 1 sec + const int timeout = 1000; /* 1 sec */ int num_timeouts = 0; size_t sent = 0; size_t pending = pkt->filled_size; @@ -754,7 +754,7 @@ int usb_conn_packet_send(struct usb_conn_t *conn, struct http_packet_t *pkt) return -1; } - // Sleep for tenth of a second + /* Sleep for tenth of a second */ struct timespec sleep_dur; sleep_dur.tv_sec = 0; sleep_dur.tv_nsec = 100000000; @@ -791,8 +791,8 @@ struct http_packet_t *usb_conn_packet_get(struct usb_conn_t *conn, struct http_m goto cleanup; } - // File packet - const int timeout = 1000; // 1 sec + /* File packet */ + const int timeout = 1000; /* 1 sec */ size_t read_size_ulong = packet_pending_bytes(pkt); if (read_size_ulong == 0) return pkt; @@ -803,10 +803,10 @@ struct http_packet_t *usb_conn_packet_get(struct usb_conn_t *conn, struct http_m goto cleanup; int read_size = (int)read_size_ulong; - // Pad read_size to multiple of usb's max packet size + /* Pad read_size to multiple of usb's max packet size */ read_size += (512 - (read_size % 512)) % 512; - // Expand buffer if needed + /* Expand buffer if needed */ if (pkt->buffer_capacity < pkt->filled_size + read_size_ulong) if (packet_expand(pkt) < 0) { ERR("Failed to ensure room for usb pkt"); @@ -845,26 +845,26 @@ struct http_packet_t *usb_conn_packet_get(struct usb_conn_t *conn, struct http_m usb_conn_mark_moving(conn); } else { - // Performance Test --------------- - // How long we sleep here has a - // dramatic affect on how long it - // takes to load a page. - // Earlier versions waited a tenth - // of a second which resulted in - // minute long page loads. - // On my HP printer the most obvious - // bottleneck is the "Unified.js" file - // which weighs 517.87KB. My profiling - // looked at how shortening this sleep - // could improve this file's load times. - // The cycle count is from perf and - // covers an entire page load. - // - // Below are my results: - // 1 in 100 == 2447ms, 261M cycles - // 1 in 1,000 == 483ms, 500M cycles - // 5 in 10,000 == 433ms, 800M cycles - // 1 in 10,000 == 320ms, 3000M cycles + /* Performance Test --------------- + How long we sleep here has a + dramatic affect on how long it + takes to load a page. + Earlier versions waited a tenth + of a second which resulted in + minute long page loads. + On my HP printer the most obvious + bottleneck is the "Unified.js" file + which weighs 517.87KB. My profiling + looked at how shortening this sleep + could improve this file's load times. + The cycle count is from perf and + covers an entire page load. + + Below are my results: + 1 in 100 == 2447ms, 261M cycles + 1 in 1,000 == 483ms, 500M cycles + 5 in 10,000 == 433ms, 800M cycles + 1 in 10,000 == 320ms, 3000M cycles */ #define TIMEOUT_RATIO (10000 / 5) static uint64_t stale_timeout = CONN_STALE_THRESHHOLD * TIMEOUT_RATIO; @@ -17,7 +17,7 @@ #include <libusb.h> #include <semaphore.h> -// In seconds +/* In seconds */ #define PRINTER_CRASH_TIMEOUT_RECEIVE (60 * 60 * 6) #define PRINTER_CRASH_TIMEOUT_ANSWER 5 #define CONN_STALE_THRESHHOLD 5 |