From: Andy Green Date: Thu, 10 Feb 2011 09:32:24 +0000 (+0000) Subject: optimize-random-device-open.patch X-Git-Tag: 1.2~193 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=44eee688ac9e15d924be90e85efb9bd26d02c482;p=profile%2Fivi%2Flibwebsockets.git optimize-random-device-open.patch Signed-off-by: Andy Green --- diff --git a/lib/client-handshake.c b/lib/client-handshake.c index fa0f5a5..70b6c81 100644 --- a/lib/client-handshake.c +++ b/lib/client-handshake.c @@ -122,7 +122,6 @@ libwebsocket_client_connect(struct libwebsocket_context *this, char buf[150]; char key_b64[150]; char hash[20]; - int fd; struct pollfd pfd; static const char magic_websocket_guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; @@ -314,20 +313,12 @@ libwebsocket_client_connect(struct libwebsocket_context *this, * create the random key */ - fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY); - if (fd < 1) { - fprintf(stderr, "Unable to open random device %s\n", - SYSTEM_RANDOM_FILEPATH); - goto bail2; - } - n = read(fd, hash, 16); + n = read(this->fd_random, hash, 16); if (n != 16) { fprintf(stderr, "Unable to read from random device %s\n", SYSTEM_RANDOM_FILEPATH); - close(fd); goto bail2; } - close(fd); lws_b64_encode_string(hash, 16, key_b64, sizeof key_b64); diff --git a/lib/handshake.c b/lib/handshake.c index e42aae5..28c02ef 100644 --- a/lib/handshake.c +++ b/lib/handshake.c @@ -236,7 +236,6 @@ handshake_0405(struct libwebsocket *wsi) char *response; char *p; char *m = mask_summing_buf; - int fd; int nonce_len; int accept_len; @@ -318,15 +317,7 @@ handshake_0405(struct libwebsocket *wsi) /* select the nonce */ - fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY); - if (fd < 1) { - fprintf(stderr, "Unable to open random device %s\n", - SYSTEM_RANDOM_FILEPATH); - if (wsi->user_space) - free(wsi->user_space); - goto bail; - } - n = read(fd, hash, 16); + n = read(wsi->protocol->owning_server->fd_random, hash, 16); if (n != 16) { fprintf(stderr, "Unable to read from random device %s %d\n", SYSTEM_RANDOM_FILEPATH, n); @@ -334,7 +325,6 @@ handshake_0405(struct libwebsocket *wsi) free(wsi->user_space); goto bail; } - close(fd); /* encode the nonce */ diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index afb0f07..d4bfe75 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -249,15 +249,16 @@ libwebsocket_context_destroy(struct libwebsocket_context *this) break; } + close(this->fd_random); + #ifdef LWS_OPENSSL_SUPPORT - if (this && this->ssl_ctx) + if (this->ssl_ctx) SSL_CTX_free(this->ssl_ctx); - if (this && this->ssl_client_ctx) + if (this->ssl_client_ctx) SSL_CTX_free(this->ssl_client_ctx); #endif - if (this) - free(this); + free(this); } /** @@ -675,6 +676,13 @@ libwebsocket_create_context(int port, this->http_proxy_address[0] = '\0'; this->options = options; + this->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY); + if (this->fd_random < 0) { + fprintf(stderr, "Unable to open random device %s %d\n", + SYSTEM_RANDOM_FILEPATH, this->fd_random); + return NULL; + } + /* find canonical hostname */ hostname[(sizeof hostname) - 1] = '\0'; diff --git a/lib/parsers.c b/lib/parsers.c index bb0963f..6f06bcf 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -988,25 +988,18 @@ int libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi, static int libwebsocket_0405_frame_mask_generate(struct libwebsocket *wsi) { - int fd; char buf[4 + 20]; int n; /* fetch the per-frame nonce */ - fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "Unable to open random device %s %d\n", - SYSTEM_RANDOM_FILEPATH, fd); - return 1; - } - n = read(fd, wsi->frame_masking_nonce_04, 4); + n = read(wsi->protocol->owning_server->fd_random, + wsi->frame_masking_nonce_04, 4); if (n != 4) { fprintf(stderr, "Unable to read from random device %s %d\n", SYSTEM_RANDOM_FILEPATH, n); return 1; } - close(fd); /* start masking from first byte of masking key buffer */ wsi->frame_mask_index = 0; diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index a164252..300cfea 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -177,6 +177,9 @@ struct libwebsocket_context { char canonical_hostname[1024]; unsigned int http_proxy_port; unsigned int options; + + int fd_random; + #ifdef LWS_OPENSSL_SUPPORT int use_ssl; SSL_CTX *ssl_ctx;