[or-cvs] r17820: {tor} Refactor sockaddr family match check into a new function (tor/trunk/src/or)

nickm at seul.org nickm at seul.org
Mon Dec 29 19:57:18 UTC 2008


Author: nickm
Date: 2008-12-29 14:57:17 -0500 (Mon, 29 Dec 2008)
New Revision: 17820

Modified:
   tor/trunk/src/or/connection.c
Log:
Refactor sockaddr family match check into a new function

Modified: tor/trunk/src/or/connection.c
===================================================================
--- tor/trunk/src/or/connection.c	2008-12-29 19:57:13 UTC (rev 17819)
+++ tor/trunk/src/or/connection.c	2008-12-29 19:57:17 UTC (rev 17820)
@@ -1030,6 +1030,26 @@
   return ok ? 0 : -1;
 }
 
+/** Check whether the socket family from an accepted socket <b>got</b> is the
+ * same as the one that <b>listener</b> is waiting for.  If it isn't, log
+ * a useful message and return -1.  Else return 0.
+ *
+ * This is annoying, but can apparently happen on some Darwins. */
+static int
+check_sockaddr_family_match(sa_family_t got, connection_t *listener)
+{
+  if (got != listener->socket_family) {
+    log_info(LD_BUG, "A listener connection returned a socket with a "
+             "mismatched family. %s for addr_family %d gave us a socket "
+             "with address family %d.  Dropping.",
+             conn_type_to_string(listener->type),
+             (int)listener->socket_family,
+             (int)got);
+    return -1;
+  }
+  return 0;
+}
+
 /** The listener connection <b>conn</b> told poll() it wanted to read.
  * Call accept() on conn-\>s, and add the new connection if necessary.
  */
@@ -1072,14 +1092,7 @@
   if (options->ConstrainedSockets)
     set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
 
-  if (remote->sa_family != conn->socket_family) {
-    /* This is annoying, but can apparently happen on some Darwins. */
-    log_info(LD_BUG, "A listener connection returned a socket with a "
-             "mismatched family. %s for addr_family %d gave us a socket "
-             "with address family %d.  Dropping.",
-             conn_type_to_string(conn->type),
-             (int)conn->socket_family,
-             (int)remote->sa_family);
+  if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
     tor_close_socket(news);
     return 0;
   }
@@ -1106,15 +1119,7 @@
       }
     }
 
-    /* Duplicate code. XXXX021 */
-    if (remote->sa_family != conn->socket_family) {
-      /* This is annoying, but can apparently happen on some Darwins. */
-      log_info(LD_BUG, "A listener connection returned a socket with a "
-               "mismatched family. %s for addr_family %d gave us a socket "
-               "with address family %d.  Dropping.",
-               conn_type_to_string(conn->type),
-               (int)conn->socket_family,
-               (int)remote->sa_family);
+    if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
       tor_close_socket(news);
       return 0;
     }



More information about the tor-commits mailing list