]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
relay: Refactor address checks into a function
authorteor <teor@torproject.org>
Tue, 14 Apr 2020 23:13:16 +0000 (09:13 +1000)
committerteor <teor@torproject.org>
Wed, 29 Apr 2020 12:43:09 +0000 (22:43 +1000)
No behaviour change.

Part of 33817.

src/feature/relay/circuitbuild_relay.c

index 96b46bb65afb47a146b5c001bf272ed28c2e823e..dd38a282585a7f0be51b084fc46b18d1f4c808e5 100644 (file)
@@ -119,6 +119,33 @@ circuit_extend_add_ed25519_helper(struct extend_cell_t *ec)
   return 0;
 }
 
+/* Check if the address and port in the tor_addr_port_t <b>ap</b> are valid,
+ * and are allowed by the current ExtendAllowPrivateAddresses config.
+ *
+ * If they are valid, return 0.
+ * Otherwise, if they are invalid, log a warning at <b>log_level</b>,
+ * and return -1.
+ */
+static int
+circuit_extend_addr_port_helper(const struct tor_addr_port_t *ap,
+                                int log_level)
+{
+  if (!tor_addr_port_is_valid_ap(ap, 0)) {
+    log_fn(log_level, LD_PROTOCOL,
+           "Client asked me to extend to zero destination port or addr.");
+    return -1;
+  }
+
+  if (tor_addr_is_internal(&ap->addr, 0) &&
+      !get_options()->ExtendAllowPrivateAddresses) {
+    log_fn(log_level, LD_PROTOCOL,
+           "Client asked me to extend to a private address.");
+    return -1;
+  }
+
+  return 0;
+}
+
 /* Before replying to an extend cell, check the link specifiers in the extend
  * cell <b>ec</b>, which was received on the circuit <b>circ</b>.
  *
@@ -139,16 +166,8 @@ circuit_extend_lspec_valid_helper(const struct extend_cell_t *ec,
     return -1;
   }
 
-  if (!tor_addr_port_is_valid_ap(&ec->orport_ipv4, 0)) {
-    log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
-           "Client asked me to extend to zero destination port or addr.");
-    return -1;
-  }
-
-  if (tor_addr_is_internal(&ec->orport_ipv4.addr, 0) &&
-      !get_options()->ExtendAllowPrivateAddresses) {
-    log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
-           "Client asked me to extend to a private address.");
+  if (circuit_extend_addr_port_helper(&ec->orport_ipv4,
+                                      LOG_PROTOCOL_WARN) < 0) {
     return -1;
   }