]> ruderich.org/simon Gitweb - tlsproxy/tlsproxy.git/commitdiff
src/connection.c: Reduce calls to gnutls_record_get_max_size().
authorSimon Ruderich <simon@ruderich.org>
Thu, 8 Sep 2011 13:55:58 +0000 (15:55 +0200)
committerSimon Ruderich <simon@ruderich.org>
Thu, 8 Sep 2011 13:55:58 +0000 (15:55 +0200)
Calling it once per connection is sufficient.

src/connection.c

index bfdac1c6db59df105ae2ac9fdc659be004da9e3a..1fb74cc155a86f2caa6dd59a6ae88d037910d8b7 100644 (file)
@@ -72,7 +72,8 @@ static int read_from_write_to(int from, int to);
 static void transfer_data_tls(int client, int server,
                               gnutls_session_t client_session,
                               gnutls_session_t server_session);
-static int read_from_write_to_tls(gnutls_session_t from, gnutls_session_t to);
+static int read_from_write_to_tls(gnutls_session_t from, gnutls_session_t to,
+                                  size_t buffer_size);
 
 static int connect_to_host(const char *hostname, const char *port);
 
@@ -675,6 +676,8 @@ static int read_from_write_to(int from, int to) {
 static void transfer_data_tls(int client, int server,
                               gnutls_session_t client_session,
                               gnutls_session_t server_session) {
+    size_t buffer_size;
+
     struct pollfd fds[2];
     fds[0].fd      = client;
     fds[0].events  = POLLIN | POLLPRI | POLLHUP | POLLERR;
@@ -683,6 +686,14 @@ static void transfer_data_tls(int client, int server,
     fds[1].events  = POLLIN | POLLPRI | POLLHUP | POLLERR;
     fds[1].revents = 0;
 
+    /* Get maximum possible buffer size. */
+    buffer_size = gnutls_record_get_max_size(client_session);
+    LOG(LOG_DEBUG, "transfer_data_tls(): suggested buffer size: %ld",
+                   (long int)buffer_size);
+    if (buffer_size > gnutls_record_get_max_size(server_session)) {
+        buffer_size = gnutls_record_get_max_size(server_session);
+    }
+
     for (;;) {
         int result = poll(fds, 2, -1 /* no timeout */);
         if (result < 0) {
@@ -692,14 +703,16 @@ static void transfer_data_tls(int client, int server,
 
         /* Data available from client. */
         if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
-            if (0 != read_from_write_to_tls(client_session, server_session)) {
+            if (0 != read_from_write_to_tls(client_session, server_session,
+                                            buffer_size)) {
                 /* EOF (or other error) */
                 break;
             }
         }
         /* Data available from server. */
         if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
-            if (0 != read_from_write_to_tls(server_session, client_session)) {
+            if (0 != read_from_write_to_tls(server_session, client_session,
+                                            buffer_size)) {
                 /* EOF (or other error) */
                 break;
             }
@@ -718,26 +731,19 @@ static void transfer_data_tls(int client, int server,
 
 /* Read available data from session from and write to session to. */
 static int read_from_write_to_tls(gnutls_session_t from,
-                                  gnutls_session_t to) {
-    size_t size;
+                                  gnutls_session_t to,
+                                  size_t buffer_size) {
     ssize_t size_read;
     ssize_t size_written;
     char buffer[16384];
 
-    /* Get maximum possible buffer size. */
-    size = gnutls_record_get_max_size(from);
-    LOG(LOG_DEBUG, "read_from_write_to_tls(): suggested buffer size: %ld",
-                   (long int)size);
-    if (size > gnutls_record_get_max_size(to)) {
-        size = gnutls_record_get_max_size(to);
-    }
-    if (size > sizeof(buffer)) {
-        size = sizeof(buffer);
+    if (buffer_size > sizeof(buffer)) {
+        buffer_size = sizeof(buffer);
     }
     LOG(LOG_DEBUG, "read_from_write_to_tls(): used buffer size: %ld",
-                   (long int)size);
+                   (long int)buffer_size);
 
-    size_read = gnutls_record_recv(from, buffer, size);
+    size_read = gnutls_record_recv(from, buffer, buffer_size);
     if (0 > size_read) {
         LOG(LOG_WARNING, "read_from_write_to_tls(): gnutls_record_recv(): %s",
                          gnutls_strerror((int)size_read));