diff --git a/lib/base64-decode.c b/lib/base64-decode.c index 428b9c65..95d4ba4a 100644 --- a/lib/base64-decode.c +++ b/lib/base64-decode.c @@ -66,33 +66,21 @@ lws_b64_encode_string(const char *in, int in_len, char *out, int out_size) } else triple[i] = 0; } - if (len) { + if (!len) + continue; - if (done + 4 >= out_size) - return -1; + if (done + 4 >= out_size) + return -1; - *out++ = encode[triple[0] >> 2]; - *out++ = encode[((triple[0] & 0x03) << 4) | - ((triple[1] & 0xf0) >> 4)]; - *out++ = (len > 1 ? encode[((triple[1] & 0x0f) << 2) | + *out++ = encode[triple[0] >> 2]; + *out++ = encode[((triple[0] & 0x03) << 4) | + ((triple[1] & 0xf0) >> 4)]; + *out++ = (len > 1 ? encode[((triple[1] & 0x0f) << 2) | ((triple[2] & 0xc0) >> 6)] : '='); - *out++ = (len > 2 ? encode[triple[2] & 0x3f] : '='); + *out++ = (len > 2 ? encode[triple[2] & 0x3f] : '='); - done += 4; - line += 4; - } -#if 0 - if (line >= 72) { - - if (done + 2 >= out_size) - return -1; - - *out++ = '\r'; - *out++ = '\n'; - done += 2; - line = 0; - } -#endif + done += 4; + line += 4; } if (done + 1 >= out_size) @@ -168,10 +156,10 @@ lws_b64_selftest(void) char buf[64]; int n; int test; - static const char *plaintext[] = { + static const char * const plaintext[] = { "sanity check base 64" }; - static const char *coded[] = { + static const char * const coded[] = { "c2FuaXR5IGNoZWNrIGJhc2UgNjQ=" }; diff --git a/lib/client-handshake.c b/lib/client-handshake.c index 18f22201..58761ca7 100644 --- a/lib/client-handshake.c +++ b/lib/client-handshake.c @@ -2,7 +2,7 @@ #include -struct libwebsocket * __libwebsocket_client_connect_2( +struct libwebsocket *__libwebsocket_client_connect_2( struct libwebsocket_context *context, struct libwebsocket *wsi ) { @@ -15,7 +15,7 @@ struct libwebsocket * __libwebsocket_client_connect_2( char pkt[512]; int opt = 1; #if defined(__APPLE__) - struct protoent* tcp_proto; + struct protoent *tcp_proto; #endif debug("__libwebsocket_client_connect_2\n"); @@ -66,10 +66,12 @@ struct libwebsocket * __libwebsocket_client_connect_2( /* Disable Nagle */ #if !defined(__APPLE__) - setsockopt(wsi->sock, SOL_TCP, TCP_NODELAY, (const void *)&opt, sizeof(opt)); + setsockopt(wsi->sock, SOL_TCP, TCP_NODELAY, + (const void *)&opt, sizeof(opt)); #else - tcp_proto = getprotobyname("TCP"); - setsockopt(wsi->sock, tcp_proto->p_proto, TCP_NODELAY, &opt, sizeof(opt)); + tcp_proto = getprotobyname("TCP"); + setsockopt(wsi->sock, tcp_proto->p_proto, TCP_NODELAY, + &opt, sizeof(opt)); #endif /* Set receiving timeout */ @@ -78,7 +80,7 @@ struct libwebsocket * __libwebsocket_client_connect_2( setsockopt(wsi->sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof tv); if (connect(wsi->sock, (struct sockaddr *)&server_addr, - sizeof(struct sockaddr)) == -1) { + sizeof(struct sockaddr)) == -1) { fprintf(stderr, "Connect failed\n"); goto oom4; } @@ -167,7 +169,7 @@ bail1: * the server, or just one. The server will pick the one it * likes best. * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest - * protocol supported, or the specific protocol ordinal + * protocol supported, or the specific protocol ordinal * * This function creates a connection to a remote server */ @@ -227,10 +229,12 @@ libwebsocket_client_connect(struct libwebsocket_context *context, if (wsi->c_path == NULL) goto bail1; strcpy(wsi->c_path, path); + wsi->c_host = malloc(strlen(host) + 1); if (wsi->c_host == NULL) goto oom1; strcpy(wsi->c_host, host); + if (origin) { wsi->c_origin = malloc(strlen(origin) + 1); strcpy(wsi->c_origin, origin); @@ -238,6 +242,7 @@ libwebsocket_client_connect(struct libwebsocket_context *context, goto oom2; } else wsi->c_origin = NULL; + if (protocol) { wsi->c_protocol = malloc(strlen(protocol) + 1); if (wsi->c_protocol == NULL) @@ -246,7 +251,6 @@ libwebsocket_client_connect(struct libwebsocket_context *context, } else wsi->c_protocol = NULL; - /* set up appropriate masking */ wsi->xor_mask = xor_no_mask; diff --git a/lib/handshake.c b/lib/handshake.c index 6809be41..3455b393 100644 --- a/lib/handshake.c +++ b/lib/handshake.c @@ -21,6 +21,9 @@ #include "private-libwebsockets.h" +#define LWS_CPYAPP(ptr, str) { strcpy(ptr, str); ptr += strlen(str); } +#define LWS_CPYAPP_TOKEN(ptr, tok) { strcpy(p, wsi->utf8_token[tok].token); \ + p += wsi->utf8_token[tok].token_len; } static int interpret_key(const char *key, unsigned long *result) @@ -33,12 +36,13 @@ interpret_key(const char *key, unsigned long *result) int rem = 0; while (*p) { - if (isdigit(*p)) { - if (digit_pos == sizeof(digits) - 1) - return -1; - digits[digit_pos++] = *p; + if (!isdigit(*p)) { + p++; + continue; } - p++; + if (digit_pos == sizeof(digits) - 1) + return -1; + digits[digit_pos++] = *p++; } digits[digit_pos] = '\0'; if (!digit_pos) @@ -91,8 +95,9 @@ handshake_00(struct libwebsocket_context *context, struct libwebsocket *wsi) goto bail; /* allocate the per-connection user memory (if any) */ - if (wsi->protocol->per_session_data_size && !libwebsocket_ensure_user_space(wsi)) - goto bail; + if (wsi->protocol->per_session_data_size && + !libwebsocket_ensure_user_space(wsi)) + goto bail; /* create the response packet */ @@ -111,41 +116,28 @@ handshake_00(struct libwebsocket_context *context, struct libwebsocket *wsi) } p = response; - strcpy(p, "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" - "Upgrade: WebSocket\x0d\x0a"); - p += strlen("HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" - "Upgrade: WebSocket\x0d\x0a"); - strcpy(p, "Connection: Upgrade\x0d\x0a" - "Sec-WebSocket-Origin: "); - p += strlen("Connection: Upgrade\x0d\x0a" - "Sec-WebSocket-Origin: "); + LWS_CPYAPP(p, "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" + "Upgrade: WebSocket\x0d\x0a" + "Connection: Upgrade\x0d\x0a" + "Sec-WebSocket-Origin: "); strcpy(p, wsi->utf8_token[WSI_TOKEN_ORIGIN].token); p += wsi->utf8_token[WSI_TOKEN_ORIGIN].token_len; #ifdef LWS_OPENSSL_SUPPORT - if (wsi->ssl) { - strcpy(p, "\x0d\x0aSec-WebSocket-Location: wss://"); - p += strlen("\x0d\x0aSec-WebSocket-Location: wss://"); - } else { + if (wsi->ssl) + LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Location: wss://"); + else #endif - strcpy(p, "\x0d\x0aSec-WebSocket-Location: ws://"); - p += strlen("\x0d\x0aSec-WebSocket-Location: ws://"); -#ifdef LWS_OPENSSL_SUPPORT - } -#endif - strcpy(p, wsi->utf8_token[WSI_TOKEN_HOST].token); - p += wsi->utf8_token[WSI_TOKEN_HOST].token_len; - strcpy(p, wsi->utf8_token[WSI_TOKEN_GET_URI].token); - p += wsi->utf8_token[WSI_TOKEN_GET_URI].token_len; + LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Location: ws://"); + + LWS_CPYAPP_TOKEN(p, WSI_TOKEN_HOST); + LWS_CPYAPP_TOKEN(p, WSI_TOKEN_GET_URI); if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token) { - strcpy(p, "\x0d\x0aSec-WebSocket-Protocol: "); - p += strlen("\x0d\x0aSec-WebSocket-Protocol: "); - strcpy(p, wsi->utf8_token[WSI_TOKEN_PROTOCOL].token); - p += wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len; + LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: "); + LWS_CPYAPP_TOKEN(p, WSI_TOKEN_PROTOCOL); } - strcpy(p, "\x0d\x0a\x0d\x0a"); - p += strlen("\x0d\x0a\x0d\x0a"); + LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a"); /* convert the two keys into 32-bit integers */ @@ -232,7 +224,7 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi) int accept_len; char *c; char ext_name[128]; - struct libwebsocket_extension * ext; + struct libwebsocket_extension *ext; int ext_count = 0; int more = 1; @@ -266,8 +258,9 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi) } /* allocate the per-connection user memory (if any) */ - if (wsi->protocol->per_session_data_size && !libwebsocket_ensure_user_space(wsi)) - goto bail; + if (wsi->protocol->per_session_data_size && + !libwebsocket_ensure_user_space(wsi)) + goto bail; /* create the response packet */ @@ -283,20 +276,15 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi) } p = response; - strcpy(p, "HTTP/1.1 101 Switching Protocols\x0d\x0a" - "Upgrade: WebSocket\x0d\x0a"); - p += strlen("HTTP/1.1 101 Switching Protocols\x0d\x0a" - "Upgrade: WebSocket\x0d\x0a"); - strcpy(p, "Connection: Upgrade\x0d\x0a" - "Sec-WebSocket-Accept: "); - p += strlen("Connection: Upgrade\x0d\x0a" - "Sec-WebSocket-Accept: "); + LWS_CPYAPP(p, "HTTP/1.1 101 Switching Protocols\x0d\x0a" + "Upgrade: WebSocket\x0d\x0a" + "Connection: Upgrade\x0d\x0a" + "Sec-WebSocket-Accept: "); strcpy(p, accept_buf); p += accept_len; if (wsi->ietf_spec_revision == 4) { - strcpy(p, "\x0d\x0aSec-WebSocket-Nonce: "); - p += strlen("\x0d\x0aSec-WebSocket-Nonce: "); + LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Nonce: "); /* select the nonce */ @@ -328,10 +316,8 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi) } if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token) { - strcpy(p, "\x0d\x0aSec-WebSocket-Protocol: "); - p += strlen("\x0d\x0aSec-WebSocket-Protocol: "); - strcpy(p, wsi->utf8_token[WSI_TOKEN_PROTOCOL].token); - p += wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len; + LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: "); + LWS_CPYAPP_TOKEN(p, WSI_TOKEN_PROTOCOL); } /* @@ -347,11 +333,12 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi) */ c = wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token; - debug("wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token = %s\n", wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token); + debug("wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token = %s\n", + wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token); wsi->count_active_extensions = 0; n = 0; while (more) { - + if (*c && (*c != ',' && *c != ' ' && *c != '\t')) { ext_name[n] = *c++; if (n < sizeof(ext_name) - 1) @@ -405,13 +392,12 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi) } /* apply it */ - + if (ext_count) *p++ = ','; - else { - strcpy(p, "\x0d\x0aSec-WebSocket-Extensions: "); - p += strlen("\x0d\x0aSec-WebSocket-Extensions: "); - } + else + LWS_CPYAPP(p, + "\x0d\x0aSec-WebSocket-Extensions: "); p += sprintf(p, "%s", ext_name); ext_count++; @@ -423,7 +409,7 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi) memset(wsi->active_extensions_user[ wsi->count_active_extensions], 0, ext->per_session_data_size); - + wsi->active_extensions[ wsi->count_active_extensions] = ext; @@ -448,8 +434,7 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi) /* end of response packet */ - strcpy(p, "\x0d\x0a\x0d\x0a"); - p += strlen("\x0d\x0a\x0d\x0a"); + LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a"); if (wsi->ietf_spec_revision == 4) { @@ -477,7 +462,7 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi) if (!lws_any_extension_handled(context, wsi, LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX, - response, p - response)) { + response, p - response)) { /* okay send the handshake response accepting the connection */ @@ -549,8 +534,8 @@ bail: */ int -libwebsocket_read(struct libwebsocket_context *context, struct libwebsocket *wsi, - unsigned char * buf, size_t len) +libwebsocket_read(struct libwebsocket_context *context, + struct libwebsocket *wsi, unsigned char * buf, size_t len) { size_t n; @@ -579,7 +564,7 @@ libwebsocket_read(struct libwebsocket_context *context, struct libwebsocket *wsi default: break; } - + /* LWS_CONNMODE_WS_SERVING */ for (n = 0; n < len; n++) @@ -605,7 +590,7 @@ libwebsocket_read(struct libwebsocket_context *context, struct libwebsocket *wsi } if (!wsi->protocol) - fprintf(stderr, "NULL protocol coming on libwebsocket_read\n"); + fprintf(stderr, "NULL protocol at libwebsocket_read\n"); /* * It's websocket diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index b05f79e1..ebd54ba5 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -114,11 +114,11 @@ libwebsockets_decode_ssl_error(void) static int -interface_to_sa(const char* ifname, struct sockaddr_in *addr, size_t addrlen) +interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen) { int rc = -1; #ifdef WIN32 - // TODO + /* TODO */ #else struct ifaddrs *ifr; struct ifaddrs *ifc; @@ -134,7 +134,7 @@ interface_to_sa(const char* ifname, struct sockaddr_in *addr, size_t addrlen) if (sin->sin_family != AF_INET) continue; memcpy(addr, sin, addrlen); - rc = 0; + rc = 0; } freeifaddrs(ifr); @@ -313,16 +313,14 @@ just_kill_connection: /* tell the user it's all over for this guy */ if (wsi->protocol && wsi->protocol->callback && - ((old_state == WSI_STATE_ESTABLISHED) || - (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) || - (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) { + ((old_state == WSI_STATE_ESTABLISHED) || + (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) || + (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) { debug("calling back CLOSED\n"); wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED, wsi->user_space, NULL, 0); } else - debug("not calling back closed due to old_state=%d\n", - old_state); - + debug("not calling back closed, old_state=%d\n", old_state); /* deallocate any active extension contexts */ @@ -393,7 +391,7 @@ just_kill_connection: /** * libwebsockets_hangup_on_client() - Server calls to terminate client - * connection + * connection * @context: libwebsockets context * @fd: Connection socket descriptor */ @@ -420,9 +418,9 @@ libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd) * @rip_len: Length of client address IP buffer * * This function fills in @name and @rip with the name and IP of - * the client connected with socket descriptor @fd. Names may be - * truncated if there is not enough room. If either cannot be - * determined, they will be returned as valid zero-length strings. + * the client connected with socket descriptor @fd. Names may be + * truncated if there is not enough room. If either cannot be + * determined, they will be returned as valid zero-length strings. */ void @@ -446,7 +444,7 @@ libwebsockets_get_peer_addresses(int fd, char *name, int name_len, perror("getpeername"); return; } - + host = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr, AF_INET); if (host == NULL) { @@ -478,7 +476,7 @@ libwebsockets_get_peer_addresses(int fd, char *name, int name_len, #ifdef AF_LOCAL else { un = (struct sockaddr_un *)p; - strncpy(ip, un->sun_path, sizeof(ip) -1); + strncpy(ip, un->sun_path, sizeof(ip) - 1); ip[sizeof(ip) - 1] = '\0'; } #endif @@ -513,13 +511,13 @@ libwebsockets_SHA1(const unsigned char *d, size_t n, unsigned char *md) void libwebsockets_00_spaceout(char *key, int spaces, int seed) { char *p; - + key++; while (spaces--) { if (*key && (seed & 1)) key++; seed >>= 1; - + p = key + strlen(key); while (p >= key) { p[1] = p[0]; @@ -535,7 +533,7 @@ void libwebsockets_00_spam(char *key, int count, int seed) key++; while (count--) { - + if (*key && (seed & 1)) key++; seed >>= 1; @@ -624,7 +622,7 @@ lws_handle_POLLOUT_event(struct libwebsocket_context *context, LWS_EXT_CALLBACK_PACKET_TX_PRESEND, wsi->active_extensions_user[n], &eff_buf, 0); if (m < 0) { - fprintf(stderr, "extension reports fatal error\n"); + fprintf(stderr, "ext reports fatal error\n"); return -1; } if (m) @@ -717,7 +715,7 @@ libwebsocket_service_timeout_check(struct libwebsocket_context *context, if (!wsi->pending_timeout) return; - + /* * if we went beyond the allowed time, kill the * connection @@ -742,7 +740,7 @@ libwebsocket_create_new_server_wsi(struct libwebsocket_context *context) return NULL; } - memset(new_wsi, 0, sizeof (struct libwebsocket)); + memset(new_wsi, 0, sizeof(struct libwebsocket)); new_wsi->count_active_extensions = 0; new_wsi->pending_timeout = NO_PENDING_TIMEOUT; @@ -787,8 +785,8 @@ libwebsockets_generate_client_handshake(struct libwebsocket_context *context, struct libwebsocket_extension *ext; struct libwebsocket_extension *ext1; int ext_count = 0; - unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + MAX_BROADCAST_PAYLOAD + - LWS_SEND_BUFFER_POST_PADDING]; + unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + + MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING]; static const char magic_websocket_guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; @@ -853,10 +851,8 @@ libwebsockets_generate_client_handshake(struct libwebsocket_context *context, unsigned int count; char challenge[16]; - libwebsockets_get_random(context, &spaces_1, - sizeof(char)); - libwebsockets_get_random(context, &spaces_2, - sizeof(char)); + libwebsockets_get_random(context, &spaces_1, sizeof(char)); + libwebsockets_get_random(context, &spaces_2, sizeof(char)); spaces_1 = (spaces_1 % 12) + 1; spaces_2 = (spaces_2 % 12) + 1; @@ -904,17 +900,14 @@ libwebsockets_generate_client_handshake(struct libwebsocket_context *context, "Connection: Upgrade\x0d\x0aHost: %s\x0d\x0a", wsi->c_host); if (wsi->c_origin) - p += sprintf(p, "Origin: %s\x0d\x0a", - wsi->c_origin); + p += sprintf(p, "Origin: %s\x0d\x0a", wsi->c_origin); if (wsi->c_protocol) p += sprintf(p, "Sec-WebSocket-Protocol: %s" "\x0d\x0a", wsi->c_protocol); - p += sprintf(p, "Sec-WebSocket-Key1: %s\x0d\x0a", - key_1); - p += sprintf(p, "Sec-WebSocket-Key2: %s\x0d\x0a", - key_2); + p += sprintf(p, "Sec-WebSocket-Key1: %s\x0d\x0a", key_1); + p += sprintf(p, "Sec-WebSocket-Key2: %s\x0d\x0a", key_2); /* give userland a chance to append, eg, cookies */ @@ -955,13 +948,13 @@ libwebsockets_generate_client_handshake(struct libwebsocket_context *context, p += sprintf(p, "Sec-WebSocket-Extensions: "); - ext =context->extensions; + ext = context->extensions; while (ext && ext->callback) { n = 0; ext1 = context->extensions; - while (ext1 && ext1->callback) { + while (ext1 && ext1->callback) { n |= ext1->callback(context, ext1, wsi, LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION, NULL, (char *)ext->name, 0); @@ -969,9 +962,7 @@ libwebsockets_generate_client_handshake(struct libwebsocket_context *context, ext1++; } - if (n) { - - /* an extension vetos us */ + if (n) { /* an extension vetos us */ debug("ext %s vetoed\n", (char *)ext->name); ext++; continue; @@ -1030,7 +1021,9 @@ libwebsockets_generate_client_handshake(struct libwebsocket_context *context, issue_hdr: -// puts(pkt); +#if 0 + puts(pkt); +#endif /* done with these now */ @@ -1046,8 +1039,8 @@ int lws_client_interpret_server_handshake(struct libwebsocket_context *context, struct libwebsocket *wsi) { - unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + MAX_BROADCAST_PAYLOAD + - LWS_SEND_BUFFER_POST_PADDING]; + unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + + MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING]; char pkt[1024]; char *p = &pkt[0]; const char *pc; @@ -1089,16 +1082,14 @@ lws_client_interpret_server_handshake(struct libwebsocket_context *context, } strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token); - if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, - "101", 3)) { + if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, "101", 3)) { fprintf(stderr, "libwebsocket_client_handshake " "server sent bad HTTP response '%s'\n", wsi->utf8_token[WSI_TOKEN_HTTP].token); goto bail3; } - if (wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len < - 16) { + if (wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len < 16) { fprintf(stderr, "libwebsocket_client_handshake " "challenge reply too short %d\n", wsi->utf8_token[ @@ -1117,22 +1108,28 @@ lws_client_interpret_server_handshake(struct libwebsocket_context *context, * Now let's confirm it sent all the necessary headers */ #if 0 - fprintf(stderr, "WSI_TOKEN_HTTP: %d\n", wsi->utf8_token[WSI_TOKEN_HTTP].token_len); - fprintf(stderr, "WSI_TOKEN_UPGRADE: %d\n", wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len); - fprintf(stderr, "WSI_TOKEN_CONNECTION: %d\n", wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len); - fprintf(stderr, "WSI_TOKEN_ACCEPT: %d\n", wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len); - fprintf(stderr, "WSI_TOKEN_NONCE: %d\n", wsi->utf8_token[WSI_TOKEN_NONCE].token_len); - fprintf(stderr, "WSI_TOKEN_PROTOCOL: %d\n", wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len); + fprintf(stderr, "WSI_TOKEN_HTTP: %d\n", + wsi->utf8_token[WSI_TOKEN_HTTP].token_len); + fprintf(stderr, "WSI_TOKEN_UPGRADE: %d\n", + wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len); + fprintf(stderr, "WSI_TOKEN_CONNECTION: %d\n", + wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len); + fprintf(stderr, "WSI_TOKEN_ACCEPT: %d\n", + wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len); + fprintf(stderr, "WSI_TOKEN_NONCE: %d\n", + wsi->utf8_token[WSI_TOKEN_NONCE].token_len); + fprintf(stderr, "WSI_TOKEN_PROTOCOL: %d\n", + wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len); #endif - if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len || - !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len || - !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len || - !wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len || - (!wsi->utf8_token[WSI_TOKEN_NONCE].token_len && + if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len || + !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len || + !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len || + !wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len || + (!wsi->utf8_token[WSI_TOKEN_NONCE].token_len && wsi->ietf_spec_revision == 4) || - (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len && - wsi->c_protocol != NULL)) { - debug("libwebsocket_client_handshake " + (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len && + wsi->c_protocol != NULL)) { + debug("libwebsocket_client_handshake " "missing required header(s)\n"); pkt[len] = '\0'; debug("%s", pkt); @@ -1173,9 +1170,11 @@ lws_client_interpret_server_handshake(struct libwebsocket_context *context, select_protocol: pc = wsi->c_protocol; if (pc == NULL) - fprintf(stderr, "lws_client_interpret_server_handshake: NULL c_protocol\n"); + fprintf(stderr, "lws_client_interpret_server_handshake: " + "NULL c_protocol\n"); else - debug("lws_client_interpret_server_handshake: cPprotocol='%s'\n", pc); + debug("lws_client_interpret_server_handshake: " + "cPprotocol='%s'\n", pc); /* * confirm the protocol the server wants to talk was in the list @@ -1184,7 +1183,8 @@ select_protocol: if (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len) { - fprintf(stderr, "lws_client_interpret_server_handshake WSI_TOKEN_PROTOCOL is null\n"); + fprintf(stderr, "lws_client_interpret_server_handshake " + "WSI_TOKEN_PROTOCOL is null\n"); /* * no protocol name to work from, * default to first protocol @@ -1197,11 +1197,10 @@ select_protocol: } while (*pc && !okay) { - if ((!strncmp(pc, - wsi->utf8_token[WSI_TOKEN_PROTOCOL].token, - wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len)) && - (pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == ',' || - pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == '\0')) { + if ((!strncmp(pc, wsi->utf8_token[WSI_TOKEN_PROTOCOL].token, + wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len)) && + (pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == ',' || + pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == '\0')) { okay = 1; continue; } @@ -1216,7 +1215,6 @@ select_protocol: if (wsi->c_protocol) free(wsi->c_protocol); - if (!okay) { fprintf(stderr, "libwebsocket_client_handshake server " "sent bad protocol '%s'\n", @@ -1248,7 +1246,7 @@ select_protocol: /* instantiate the accepted extensions */ if (!wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token_len) { - debug("no client extenstions allowed by server \n"); + debug("no client extenstions allowed by server\n"); goto check_accept; } @@ -1370,11 +1368,12 @@ check_accept: strcpy(p, magic_websocket_04_masking_guid); SHA1(buf, strlen((char *)buf), wsi->masking_key_04); } - accept_ok: +accept_ok: /* allocate the per-connection user memory (if any) */ - if (wsi->protocol->per_session_data_size && !libwebsocket_ensure_user_space(wsi)) - goto bail2; + if (wsi->protocol->per_session_data_size && + !libwebsocket_ensure_user_space(wsi)) + goto bail2; /* clear his proxy connection timeout */ @@ -1385,15 +1384,13 @@ check_accept: wsi->state = WSI_STATE_ESTABLISHED; wsi->mode = LWS_CONNMODE_WS_CLIENT; - fprintf(stderr, "handshake OK for protocol %s\n", - wsi->protocol->name); + fprintf(stderr, "handshake OK for protocol %s\n", wsi->protocol->name); /* call him back to inform him he is up */ wsi->protocol->callback(context, wsi, - LWS_CALLBACK_CLIENT_ESTABLISHED, - wsi->user_space, - NULL, 0); + LWS_CALLBACK_CLIENT_ESTABLISHED, + wsi->user_space, NULL, 0); /* * inform all extensions, not just active ones since they @@ -1431,7 +1428,7 @@ bail2: * libwebsocket_service_fd() - Service polled socket with something waiting * @context: Websocket context * @pollfd: The pollfd entry describing the socket fd and which events - * happened. + * happened. * * This function closes any active connections and then frees the * context. After calling this, any further use of the context is @@ -1442,8 +1439,8 @@ int libwebsocket_service_fd(struct libwebsocket_context *context, struct pollfd *pollfd) { - unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + MAX_BROADCAST_PAYLOAD + - LWS_SEND_BUFFER_POST_PADDING]; + unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + + MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING]; struct libwebsocket *wsi; struct libwebsocket *new_wsi; int n; @@ -1521,8 +1518,8 @@ libwebsocket_service_fd(struct libwebsocket_context *context, /* Disable Nagle */ opt = 1; - setsockopt(accept_fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&opt, - sizeof(opt)); + setsockopt(accept_fd, IPPROTO_TCP, TCP_NODELAY, + (const void *)&opt, sizeof(opt)); /* * look at who we connected to and give user code a chance @@ -1532,7 +1529,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context, if ((context->protocols[0].callback)(context, wsi, LWS_CALLBACK_FILTER_NETWORK_CONNECTION, - (void*)(long)accept_fd, NULL, 0)) { + (void *)(long)accept_fd, NULL, 0)) { debug("Callback denied network connection\n"); #ifdef WIN32 closesocket(accept_fd); @@ -1584,7 +1581,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context, free(new_wsi); break; } - + debug("accepted new SSL conn " "port %u on fd=%d SSL ver %s\n", ntohs(cli_addr.sin_port), accept_fd, @@ -1617,7 +1614,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context, case LWS_CONNMODE_BROADCAST_PROXY_LISTENER: /* as we are listening, POLLIN means accept() is needed */ - + if (!pollfd->revents & POLLIN) break; @@ -1645,7 +1642,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context, /* create a dummy wsi for the connection and add it */ new_wsi = malloc(sizeof(struct libwebsocket)); - memset(new_wsi, 0, sizeof (struct libwebsocket)); + memset(new_wsi, 0, sizeof(struct libwebsocket)); new_wsi->sock = accept_fd; new_wsi->mode = LWS_CONNMODE_BROADCAST_PROXY; new_wsi->state = WSI_STATE_ESTABLISHED; @@ -1688,9 +1685,10 @@ libwebsocket_service_fd(struct libwebsocket_context *context, */ if (pollfd->revents & POLLOUT) - if (lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) { - libwebsocket_close_and_free_session(context, wsi, - LWS_CLOSE_STATUS_NORMAL); + if (lws_handle_POLLOUT_event(context, wsi, + pollfd) < 0) { + libwebsocket_close_and_free_session( + context, wsi, LWS_CLOSE_STATUS_NORMAL); return 1; } @@ -1802,8 +1800,8 @@ libwebsocket_service_fd(struct libwebsocket_context *context, fprintf(stderr, "SSL connect error %s\n", ERR_error_string(ERR_get_error(), ssl_err_buf)); - libwebsocket_close_and_free_session(context, wsi, - LWS_CLOSE_STATUS_NOSTATUS); + libwebsocket_close_and_free_session(context, + wsi, LWS_CLOSE_STATUS_NOSTATUS); return 1; } @@ -1828,7 +1826,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context, #endif p = libwebsockets_generate_client_handshake(context, wsi, p); - if (p ==NULL) + if (p == NULL) return 1; /* send our request to the server */ @@ -1900,7 +1898,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context, } /* - * hs may also be coming in multiple packets, there is a 5-second + * hs may also be coming in multiple packets, there is a 5-sec * libwebsocket timeout still active here too, so if parsing did * not complete just wait for next packet coming in this state */ @@ -1920,15 +1918,17 @@ bail3: if (wsi->c_protocol) free(wsi->c_protocol); libwebsocket_close_and_free_session(context, wsi, - LWS_CLOSE_STATUS_NOSTATUS); + LWS_CLOSE_STATUS_NOSTATUS); return 1; case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT: - fprintf(stderr, "LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n"); + fprintf(stderr, + "LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n"); break; case LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD: - fprintf(stderr, "LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n"); + fprintf(stderr, + "LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n"); break; @@ -1976,13 +1976,13 @@ bail3: fprintf(stderr, "Socket read returned %d\n", eff_buf.token_len); if (errno != EINTR) - libwebsocket_close_and_free_session(context, wsi, - LWS_CLOSE_STATUS_NOSTATUS); + libwebsocket_close_and_free_session(context, + wsi, LWS_CLOSE_STATUS_NOSTATUS); return 1; } if (!eff_buf.token_len) { libwebsocket_close_and_free_session(context, wsi, - LWS_CLOSE_STATUS_NOSTATUS); + LWS_CLOSE_STATUS_NOSTATUS); return 1; } @@ -2012,9 +2012,11 @@ bail3: wsi->active_extensions_user[n], &eff_buf, 0); if (m < 0) { - fprintf(stderr, "Extension reports fatal error\n"); - libwebsocket_close_and_free_session(context, wsi, - LWS_CLOSE_STATUS_NOSTATUS); + fprintf(stderr, + "Extension reports fatal error\n"); + libwebsocket_close_and_free_session( + context, wsi, + LWS_CLOSE_STATUS_NOSTATUS); return 1; } if (m) @@ -2025,7 +2027,8 @@ bail3: if (eff_buf.token_len) { n = libwebsocket_read(context, wsi, - (unsigned char *)eff_buf.token, eff_buf.token_len); + (unsigned char *)eff_buf.token, + eff_buf.token_len); if (n < 0) /* we closed wsi */ return 1; @@ -2167,8 +2170,8 @@ libwebsocket_service(struct libwebsocket_context *context, int timeout_ms) int lws_any_extension_handled(struct libwebsocket_context *context, - struct libwebsocket *wsi, - enum libwebsocket_extension_callback_reasons r, + struct libwebsocket *wsi, + enum libwebsocket_extension_callback_reasons r, void *v, size_t len) { int n; @@ -2191,7 +2194,7 @@ lws_any_extension_handled(struct libwebsocket_context *context, void * lws_get_extension_user_matching_ext(struct libwebsocket *wsi, - struct libwebsocket_extension * ext) + struct libwebsocket_extension *ext) { int n = 0; @@ -2220,7 +2223,7 @@ lws_get_extension_user_matching_ext(struct libwebsocket *wsi, int libwebsocket_callback_on_writable(struct libwebsocket_context *context, - struct libwebsocket *wsi) + struct libwebsocket *wsi) { int n; int handled = 0; @@ -2247,7 +2250,8 @@ libwebsocket_callback_on_writable(struct libwebsocket_context *context, } if (n == context->fds_count) - fprintf(stderr, "libwebsocket_callback_on_writable: failed to find socket %d\n", wsi->sock); + fprintf(stderr, "libwebsocket_callback_on_writable: " + "failed to find socket %d\n", wsi->sock); /* external POLL support via protocol 0 */ context->protocols[0].callback(context, wsi, @@ -2410,7 +2414,7 @@ OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) * static */ context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index); - + n = context->protocols[0].callback(NULL, NULL, LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION, x509_ctx, ssl, preverify_ok); @@ -2439,7 +2443,7 @@ OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) * entry that has a NULL callback pointer. * It's not const because we write the owning_server member * @extensions: NULL or array of libwebsocket_extension structs listing the - * extensions this context supports + * extensions this context supports * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want * to listen using SSL, set to the filepath to fetch the * server cert from, otherwise NULL for unencrypted @@ -2506,7 +2510,7 @@ libwebsocket_create_context(int port, const char *interf, WORD wVersionRequested; WSADATA wsaData; int err; - HMODULE wsdll; + HMODULE wsdll; /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */ wVersionRequested = MAKEWORD(2, 2); @@ -2520,16 +2524,13 @@ libwebsocket_create_context(int port, const char *interf, return NULL; } - wsdll = GetModuleHandle("Ws2_32.dll"); - if (wsdll) - { - poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll"); - } + /* default to a poll() made out of select() */ + poll = emulated_poll; - if (!poll) - { - poll = emulated_poll; - } + /* if windows socket lib available, use his WSAPoll */ + wsdll = GetModuleHandle("Ws2_32.dll"); + if (wsdll) + poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll"); } #endif @@ -2583,7 +2584,7 @@ libwebsocket_create_context(int port, const char *interf, p = getenv("http_proxy"); if (p) { strncpy(context->http_proxy_address, p, - sizeof context->http_proxy_address - 1); + sizeof context->http_proxy_address - 1); context->http_proxy_address[ sizeof context->http_proxy_address - 1] = '\0'; @@ -2661,8 +2662,8 @@ libwebsocket_create_context(int port, const char *interf, } /* client context */ - if (port == CONTEXT_PORT_NO_LISTEN) - { + + if (port == CONTEXT_PORT_NO_LISTEN) { method = (SSL_METHOD *)SSLv23_client_method(); if (!method) { fprintf(stderr, "problem creating ssl method: %s\n", @@ -2697,12 +2698,13 @@ libwebsocket_create_context(int port, const char *interf, LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS, context->ssl_client_ctx, NULL, 0); } + /* as a server, are we requiring clients to identify themselves? */ if (options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) { /* absolutely require the client cert */ - + SSL_CTX_set_verify(context->ssl_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, OpenSSL_verify_callback); @@ -2769,12 +2771,13 @@ libwebsocket_create_context(int port, const char *interf, } /* allow us to restart even if old sockets in TIME_WAIT */ - setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, sizeof(opt)); - + setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, + (const void *)&opt, sizeof(opt)); /* Disable Nagle */ opt = 1; - setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (const void *)&opt, sizeof(opt)); + setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, + (const void *)&opt, sizeof(opt)); bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; @@ -2794,7 +2797,7 @@ libwebsocket_create_context(int port, const char *interf, } wsi = malloc(sizeof(struct libwebsocket)); - memset(wsi, 0, sizeof (struct libwebsocket)); + memset(wsi, 0, sizeof(struct libwebsocket)); wsi->sock = sockfd; wsi->count_active_extensions = 0; wsi->mode = LWS_CONNMODE_SERVER_LISTENER; @@ -2804,7 +2807,7 @@ libwebsocket_create_context(int port, const char *interf, fprintf(stderr, " Listening on port %d\n", port); /* list in the internal poll array */ - + context->fds[context->fds_count].fd = sockfd; context->fds[context->fds_count++].events = POLLIN; @@ -2815,7 +2818,11 @@ libwebsocket_create_context(int port, const char *interf, } - /* drop any root privs for this process */ + /* + * drop any root privs for this process + * to listen on port < 1023 we would have needed root, but now we are + * listening, we don't want the power for anything else + */ #ifdef WIN32 #else if (gid != -1) @@ -2832,7 +2839,8 @@ libwebsocket_create_context(int port, const char *interf, protocols[context->count_protocols].callback; context->count_protocols++) { - fprintf(stderr, " Protocol: %s\n", protocols[context->count_protocols].name); + fprintf(stderr, " Protocol: %s\n", + protocols[context->count_protocols].name); protocols[context->count_protocols].owning_server = context; protocols[context->count_protocols].protocol_index = @@ -2845,7 +2853,8 @@ libwebsocket_create_context(int port, const char *interf, } /* allow us to restart even if old sockets in TIME_WAIT */ - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, sizeof(opt)); + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, + sizeof(opt)); bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; @@ -2876,7 +2885,7 @@ libwebsocket_create_context(int port, const char *interf, /* dummy wsi per broadcast proxy socket */ wsi = malloc(sizeof(struct libwebsocket)); - memset(wsi, 0, sizeof (struct libwebsocket)); + memset(wsi, 0, sizeof(struct libwebsocket)); wsi->sock = fd; wsi->mode = LWS_CONNMODE_BROADCAST_PROXY_LISTENER; wsi->count_active_extensions = 0; @@ -3034,7 +3043,7 @@ libwebsockets_broadcast(const struct libwebsocket_protocols *protocol, struct libwebsocket_context *context = protocol->owning_server; int n; int m; - struct libwebsocket * wsi; + struct libwebsocket *wsi; if (!protocol->broadcast_socket_user_fd) { /* @@ -3115,7 +3124,8 @@ libwebsocket_ensure_user_space(struct libwebsocket *wsi) "conn user space\n"); return NULL; } - memset(wsi->user_space, 0, wsi->protocol->per_session_data_size); + memset(wsi->user_space, 0, + wsi->protocol->per_session_data_size); } return wsi->user_space; } diff --git a/lib/parsers.c b/lib/parsers.c index c3bf7ae4..61f30456 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -200,7 +200,7 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c) wsi->current_alloc_len = LWS_INITIAL_HDR_ALLOC; wsi->utf8_token[wsi->parser_state].token = - malloc(wsi->current_alloc_len); + malloc(wsi->current_alloc_len); wsi->utf8_token[wsi->parser_state].token_len = 0; } @@ -304,7 +304,9 @@ libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c) int handled; int m; -// fprintf(stderr, "RX: %02X ", c); +#if 0 + fprintf(stderr, "RX: %02X ", c); +#endif switch (wsi->lws_rx_parse_state) { case LWS_RXPS_NEW: @@ -704,7 +706,8 @@ issue: case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED: - if (wsi->ietf_spec_revision < 4 || (wsi->all_zero_nonce && wsi->ietf_spec_revision >= 5)) + if (wsi->ietf_spec_revision < 4 || + (wsi->all_zero_nonce && wsi->ietf_spec_revision >= 5)) wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + (wsi->rx_user_buffer_head++)] = c; else @@ -922,7 +925,8 @@ int libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c) if (wsi->ietf_spec_revision < 7) switch (c & 0xf) { case LWS_WS_OPCODE_04__CONTINUATION: - wsi->opcode = LWS_WS_OPCODE_07__CONTINUATION; + wsi->opcode = + LWS_WS_OPCODE_07__CONTINUATION; break; case LWS_WS_OPCODE_04__CLOSE: wsi->opcode = LWS_WS_OPCODE_07__CLOSE; @@ -934,14 +938,16 @@ int libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c) wsi->opcode = LWS_WS_OPCODE_07__PONG; break; case LWS_WS_OPCODE_04__TEXT_FRAME: - wsi->opcode = LWS_WS_OPCODE_07__TEXT_FRAME; + wsi->opcode = + LWS_WS_OPCODE_07__TEXT_FRAME; break; case LWS_WS_OPCODE_04__BINARY_FRAME: - wsi->opcode = LWS_WS_OPCODE_07__BINARY_FRAME; + wsi->opcode = + LWS_WS_OPCODE_07__BINARY_FRAME; break; default: fprintf(stderr, "reserved opcodes not " - "usable pre v7 protocol\n"); + "usable pre v7 protocol\n"); return -1; } else @@ -993,7 +999,7 @@ int libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c) else { if (c) wsi->lws_rx_parse_state = - LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; + LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; else { wsi->lws_rx_parse_state = LWS_RXPS_NEW; goto spill; @@ -1016,7 +1022,7 @@ int libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c) else { if (wsi->rx_packet_length) wsi->lws_rx_parse_state = - LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; + LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; else { wsi->lws_rx_parse_state = LWS_RXPS_NEW; goto spill; @@ -1082,7 +1088,7 @@ int libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c) else { if (wsi->rx_packet_length) wsi->lws_rx_parse_state = - LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; + LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; else { wsi->lws_rx_parse_state = LWS_RXPS_NEW; goto spill; @@ -1200,12 +1206,14 @@ spill: debug("seen server's close ack\n"); return -1; } - fprintf(stderr, "client sees server close packet len = %d\n", wsi->rx_user_buffer_head); + fprintf(stderr, "client sees server close packet " + "len = %d\n", wsi->rx_user_buffer_head); /* parrot the close packet payload back */ n = libwebsocket_write(wsi, (unsigned char *) &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING], wsi->rx_user_buffer_head, LWS_WRITE_CLOSE); - fprintf(stderr, "client writing close ack returned %d\n", n); + fprintf(stderr, "client writing close " + "ack returned %d\n", n); wsi->state = WSI_STATE_RETURNED_CLOSE_ALREADY; /* close the connection */ return -1; @@ -1351,7 +1359,7 @@ libwebsocket_0405_frame_mask_generate(struct libwebsocket *wsi) */ memcpy(buf, wsi->frame_masking_nonce_04, 4); - + memcpy(buf + 4, wsi->masking_key_04, 20); /* concatenate the nonce with the connection key then hash it */ @@ -1373,7 +1381,7 @@ void lws_stderr_hexdump(unsigned char *buf, size_t len) start = n; fprintf(stderr, "%04X: ", start); - + for (m = 0; m < 16 && n < len; m++) fprintf(stderr, "%02X ", buf[n++]); while (m++ < 16) @@ -1419,14 +1427,13 @@ int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len) return -1; } if (m) /* handled */ { -// fprintf(stderr, "ext sent it\n"); +/* fprintf(stderr, "ext sent it\n"); */ return 0; } } - if (!wsi->sock) { + if (!wsi->sock) fprintf(stderr, "** error 0 sock but expected to send\n"); - } /* * nope, send it on the socket directly @@ -1492,7 +1499,7 @@ lws_issue_raw_ext_access(struct libwebsocket *wsi, LWS_EXT_CALLBACK_PACKET_TX_PRESEND, wsi->active_extensions_user[n], &eff_buf, 0); if (m < 0) { - fprintf(stderr, "Extension reports fatal error\n"); + fprintf(stderr, "Extension: fatal error\n"); return -1; } if (m) @@ -1507,7 +1514,7 @@ lws_issue_raw_ext_access(struct libwebsocket *wsi, if (eff_buf.token_len) if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token, - eff_buf.token_len)) + eff_buf.token_len)) return -1; /* we used up what we had */ @@ -1572,7 +1579,8 @@ int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, int pre = 0; int post = 0; int shift = 7; - int masked7 = wsi->mode == LWS_CONNMODE_WS_CLIENT && wsi->xor_mask != xor_no_mask; + int masked7 = wsi->mode == LWS_CONNMODE_WS_CLIENT && + wsi->xor_mask != xor_no_mask; unsigned char *dropmask = NULL; unsigned char is_masked_bit = 0; @@ -1665,7 +1673,7 @@ int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, * v5 mandates the first byte of close packet * in both client and server directions */ - + switch (wsi->ietf_spec_revision) { case 0: case 4: @@ -1675,7 +1683,7 @@ int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, if (len < 1) len = 1; - + switch (wsi->mode) { case LWS_CONNMODE_WS_SERVING: /* @@ -1780,7 +1788,8 @@ int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, * to control the raw packet payload content */ - if (!(protocol & LWS_WRITE_CLIENT_IGNORE_XOR_MASK) && wsi->xor_mask != xor_no_mask) { + if (!(protocol & LWS_WRITE_CLIENT_IGNORE_XOR_MASK) && + wsi->xor_mask != xor_no_mask) { if (libwebsocket_0405_frame_mask_generate(wsi)) { fprintf(stderr, "libwebsocket_write: " @@ -1801,7 +1810,8 @@ int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, * in v7, just mask the payload */ for (n = 0; n < (int)len; n++) - dropmask[n + 4] = wsi->xor_mask(wsi, dropmask[n + 4]); + dropmask[n + 4] = + wsi->xor_mask(wsi, dropmask[n + 4]); if (wsi->ietf_spec_revision < 7) { @@ -1813,7 +1823,8 @@ int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, if (dropmask) /* copy the frame nonce into place */ - memcpy(dropmask, wsi->frame_masking_nonce_04, 4); + memcpy(dropmask, + wsi->frame_masking_nonce_04, 4); } else { if (wsi->ietf_spec_revision < 7) { @@ -1914,7 +1925,8 @@ int libwebsockets_serve_http_file(struct libwebsocket *wsi, const char *file, "Server: libwebsockets\x0d\x0a" "Content-Type: %s\x0d\x0a" "Content-Length: %u\x0d\x0a" - "\x0d\x0a", content_type, (unsigned int)stat_buf.st_size); + "\x0d\x0a", content_type, + (unsigned int)stat_buf.st_size); libwebsocket_write(wsi, (unsigned char *)buf, p - buf, LWS_WRITE_HTTP); diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index b4b63acb..b4779e68 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -100,7 +100,7 @@ void debug(const char *format, ...) * but happily have something equivalent in the SO_NOSIGPIPE flag. */ #ifdef __APPLE__ -#define MSG_NOSIGNAL SO_NOSIGPIPE +#define MSG_NOSIGNAL SO_NOSIGPIPE #endif @@ -236,7 +236,7 @@ struct libwebsocket_context { unsigned long last_timeout_check_s; int fd_random; - + #ifdef LWS_OPENSSL_SUPPORT int use_ssl; SSL_CTX *ssl_ctx; @@ -269,7 +269,7 @@ struct libwebsocket { const struct libwebsocket_protocols *protocol; struct libwebsocket_extension * active_extensions[LWS_MAX_EXTENSIONS_ACTIVE]; - void * active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE]; + void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE]; int count_active_extensions; enum lws_connection_states state; @@ -347,8 +347,9 @@ libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi, unsigned char *buf, size_t len); extern int -libwebsocket_read(struct libwebsocket_context *context, struct libwebsocket *wsi, - unsigned char * buf, size_t len); +libwebsocket_read(struct libwebsocket_context *context, + struct libwebsocket *wsi, + unsigned char *buf, size_t len); extern int lws_b64_selftest(void); @@ -383,8 +384,8 @@ extern void libwebsocket_service_timeout_check(struct libwebsocket_context *context, struct libwebsocket *wsi, unsigned int sec); -extern struct libwebsocket * __libwebsocket_client_connect_2( - struct libwebsocket_context *context, +extern struct libwebsocket * +__libwebsocket_client_connect_2(struct libwebsocket_context *context, struct libwebsocket *wsi); extern struct libwebsocket * @@ -400,13 +401,13 @@ lws_handle_POLLOUT_event(struct libwebsocket_context *context, extern int lws_any_extension_handled(struct libwebsocket_context *context, - struct libwebsocket *wsi, - enum libwebsocket_extension_callback_reasons r, - void *v, size_t len); + struct libwebsocket *wsi, + enum libwebsocket_extension_callback_reasons r, + void *v, size_t len); extern void * lws_get_extension_user_matching_ext(struct libwebsocket *wsi, - struct libwebsocket_extension * ext); + struct libwebsocket_extension *ext); extern int lws_client_interpret_server_handshake(struct libwebsocket_context *context, diff --git a/lib/sha-1.c b/lib/sha-1.c index 2bd2494a..62d4f6f8 100644 --- a/lib/sha-1.c +++ b/lib/sha-1.c @@ -45,10 +45,7 @@ #define BYTE_ORDER LITTLE_ENDIAN #endif -typedef unsigned char u_int8_t; -typedef unsigned int u_int32_t; typedef unsigned __int64 u_int64_t; -typedef void* caddr_t; #undef __P #ifndef __P @@ -59,7 +56,7 @@ typedef void* caddr_t; #endif #endif -#define bzero(b,len) (memset((b), '\0', (len)), (void) 0) +#define bzero(b, len) (memset((b), '\0', (len)), (void) 0) #else #include @@ -70,18 +67,18 @@ typedef void* caddr_t; struct sha1_ctxt { union { - u_int8_t b8[20]; - u_int32_t b32[5]; + unsigned char b8[20]; + unsigned int b32[5]; } h; union { - u_int8_t b8[8]; - u_int64_t b64[1]; + unsigned char b8[8]; + u_int64_t b64[1]; } c; union { - u_int8_t b8[64]; - u_int32_t b32[16]; + unsigned char b8[64]; + unsigned int b32[16]; } m; - u_int8_t count; + unsigned char count; }; /* sanity check */ @@ -94,7 +91,7 @@ struct sha1_ctxt { #ifndef unsupported /* constant table */ -static u_int32_t _K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 }; +static unsigned int _K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 }; #define K(t) _K[(t) / 20] #define F0(b, c, d) (((b) & (c)) | ((~(b)) & (d))) @@ -116,7 +113,7 @@ static u_int32_t _K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 }; ctxt->c.b64[0] += 8; \ if (COUNT % 64 == 0) \ sha1_step(ctxt); \ - } + } #define PUTPAD(x) { \ ctxt->m.b8[(COUNT % 64)] = (x); \ @@ -124,16 +121,15 @@ static u_int32_t _K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 }; COUNT %= 64; \ if (COUNT % 64 == 0) \ sha1_step(ctxt); \ - } + } static void sha1_step __P((struct sha1_ctxt *)); static void sha1_step(struct sha1_ctxt *ctxt) { - u_int32_t a, b, c, d, e; + unsigned int a, b, c, d, e, tmp; size_t t, s; - u_int32_t tmp; #if BYTE_ORDER == LITTLE_ENDIAN struct sha1_ctxt tctxt; @@ -263,7 +259,7 @@ sha1_pad(struct sha1_ctxt *ctxt) } void -sha1_loop(struct sha1_ctxt *ctxt, const u_int8_t *input, size_t len) +sha1_loop(struct sha1_ctxt *ctxt, const unsigned char *input, size_t len) { size_t gaplen; size_t gapstart; @@ -288,11 +284,11 @@ sha1_loop(struct sha1_ctxt *ctxt, const u_int8_t *input, size_t len) } void -sha1_result(struct sha1_ctxt *ctxt, caddr_t digest0) +sha1_result(struct sha1_ctxt *ctxt, void *digest0) { - u_int8_t *digest; + unsigned char *digest; - digest = (u_int8_t *)digest0; + digest = (unsigned char *)digest0; sha1_pad(ctxt); #if BYTE_ORDER == BIG_ENDIAN memcpy(digest, &ctxt->h.b8[0], 20); @@ -321,7 +317,7 @@ SHA1(const unsigned char *d, size_t n, unsigned char *md) sha1_init(&ctx); sha1_loop(&ctx, d, n); - sha1_result(&ctx, (caddr_t)md); + sha1_result(&ctx, (void *)md); return md; } diff --git a/test-server/test-client.c b/test-server/test-client.c index 3fecd47d..9d1d9efc 100644 --- a/test-server/test-client.c +++ b/test-server/test-client.c @@ -59,7 +59,7 @@ enum demo_protocols { /* dumb_increment protocol */ static int -callback_dumb_increment(struct libwebsocket_context * this, +callback_dumb_increment(struct libwebsocket_context *this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len) @@ -102,7 +102,7 @@ callback_dumb_increment(struct libwebsocket_context * this, static int -callback_lws_mirror(struct libwebsocket_context * this, +callback_lws_mirror(struct libwebsocket_context *this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len) @@ -291,18 +291,22 @@ int main(int argc, char **argv) /* create a client websocket using mirror protocol */ - wsi_mirror = libwebsocket_client_connect(context, address, port, - use_ssl, "/", argv[optind], argv[optind], - protocols[PROTOCOL_LWS_MIRROR].name, ietf_version); + wsi_mirror = libwebsocket_client_connect(context, + address, port, use_ssl, "/", + argv[optind], argv[optind], + protocols[PROTOCOL_LWS_MIRROR].name, + ietf_version); if (wsi_mirror == NULL) { - fprintf(stderr, "libwebsocket dumb connect failed\n"); + fprintf(stderr, "libwebsocket " + "dumb connect failed\n"); return -1; } mirror_lifetime = 10 + (random() & 1023); - fprintf(stderr, "opened mirror connection with %d lifetime\n", mirror_lifetime); + fprintf(stderr, "opened mirror connection with " + "%d lifetime\n", mirror_lifetime); } else { diff --git a/test-server/test-fraggle.c b/test-server/test-fraggle.c index cdb4f568..873bee3e 100644 --- a/test-server/test-fraggle.c +++ b/test-server/test-fraggle.c @@ -56,7 +56,7 @@ struct per_session_data__fraggle { }; static int -callback_fraggle(struct libwebsocket_context * context, +callback_fraggle(struct libwebsocket_context *context, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len) @@ -179,7 +179,7 @@ callback_fraggle(struct libwebsocket_context * context, bp[1] = psf->sum >> 16; bp[2] = psf->sum >> 8; bp[3] = psf->sum; - + n = libwebsocket_write(wsi, (unsigned char *)bp, 4, LWS_WRITE_BINARY); @@ -231,8 +231,8 @@ static struct option options[] = { { "port", required_argument, NULL, 'p' }, { "ssl", no_argument, NULL, 's' }, { "killmask", no_argument, NULL, 'k' }, - { "interface", required_argument, NULL, 'i' }, - { "client", no_argument, NULL, 'c' }, + { "interface", required_argument, NULL, 'i' }, + { "client", no_argument, NULL, 'c' }, { NULL, 0, 0, 0 } }; @@ -248,7 +248,7 @@ int main(int argc, char **argv) struct libwebsocket_context *context; int opts = 0; char interface_name[128] = ""; - const char * interface = NULL; + const char *interface = NULL; struct libwebsocket *wsi; const char *address; int server_port = port; diff --git a/test-server/test-server.c b/test-server/test-server.c index c1cfab81..f054b162 100644 --- a/test-server/test-server.c +++ b/test-server/test-server.c @@ -61,7 +61,7 @@ enum demo_protocols { /* this protocol server (always the first one) just knows how to do HTTP */ -static int callback_http(struct libwebsocket_context * context, +static int callback_http(struct libwebsocket_context *context, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len) @@ -150,7 +150,7 @@ dump_handshake_info(struct lws_tokens *lwst) /*[WSI_TOKEN_HTTP] =*/ "Http", /*[WSI_TOKEN_MUXURL] =*/ "MuxURL", }; - + for (n = 0; n < WSI_TOKEN_COUNT; n++) { if (lwst[n].token == NULL) continue; @@ -174,7 +174,7 @@ struct per_session_data__dumb_increment { }; static int -callback_dumb_increment(struct libwebsocket_context * context, +callback_dumb_increment(struct libwebsocket_context *context, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len) @@ -188,7 +188,8 @@ callback_dumb_increment(struct libwebsocket_context * context, switch (reason) { case LWS_CALLBACK_ESTABLISHED: - fprintf(stderr, "callback_dumb_increment: LWS_CALLBACK_ESTABLISHED\n"); + fprintf(stderr, "callback_dumb_increment: " + "LWS_CALLBACK_ESTABLISHED\n"); pss->number = 0; break; @@ -257,7 +258,7 @@ static int ringbuffer_head; static int -callback_lws_mirror(struct libwebsocket_context * context, +callback_lws_mirror(struct libwebsocket_context *context, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len) @@ -268,7 +269,8 @@ callback_lws_mirror(struct libwebsocket_context * context, switch (reason) { case LWS_CALLBACK_ESTABLISHED: - fprintf(stderr, "callback_lws_mirror: LWS_CALLBACK_ESTABLISHED\n"); + fprintf(stderr, "callback_lws_mirror: " + "LWS_CALLBACK_ESTABLISHED\n"); pss->ringbuffer_tail = ringbuffer_head; pss->wsi = wsi; break; @@ -380,7 +382,7 @@ static struct option options[] = { { "port", required_argument, NULL, 'p' }, { "ssl", no_argument, NULL, 's' }, { "killmask", no_argument, NULL, 'k' }, - { "interface", required_argument, NULL, 'i' }, + { "interface", required_argument, NULL, 'i' }, { "closetest", no_argument, NULL, 'c' }, { NULL, 0, 0, 0 } }; @@ -399,7 +401,7 @@ int main(int argc, char **argv) struct libwebsocket_context *context; int opts = 0; char interface_name[128] = ""; - const char * interface = NULL; + const char *interface = NULL; #ifdef LWS_NO_FORK unsigned int oldus = 0; #endif