optimize-random-device-open.patch
authorAndy Green <andy.green@linaro.org>
Thu, 10 Feb 2011 09:32:24 +0000 (09:32 +0000)
committerAndy Green <andy.green@linaro.org>
Thu, 10 Feb 2011 09:32:24 +0000 (09:32 +0000)
Signed-off-by: Andy Green <andy.green@linaro.org>
lib/client-handshake.c
lib/handshake.c
lib/libwebsockets.c
lib/parsers.c
lib/private-libwebsockets.h

index fa0f5a5..70b6c81 100644 (file)
@@ -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);
 
index e42aae5..28c02ef 100644 (file)
@@ -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 */
 
index afb0f07..d4bfe75 100644 (file)
@@ -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';
index bb0963f..6f06bcf 100644 (file)
@@ -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;
index a164252..300cfea 100644 (file)
@@ -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;