[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r16255: Refactor tor_addr_from_string: it didnt need most of parse_a (in tor/trunk: . src/common)



Author: nickm
Date: 2008-07-30 09:04:26 -0400 (Wed, 30 Jul 2008)
New Revision: 16255

Modified:
   tor/trunk/
   tor/trunk/src/common/address.c
Log:
 r17434@tombo:  nickm | 2008-07-29 10:58:36 -0400
 Refactor tor_addr_from_string: it didnt need most of parse_addr_mask_port_range, and its dependence on that latter function made it less flexible.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r17434] on 49666b30-7950-49c5-bedf-9dc8f3168102

Modified: tor/trunk/src/common/address.c
===================================================================
--- tor/trunk/src/common/address.c	2008-07-30 13:04:19 UTC (rev 16254)
+++ tor/trunk/src/common/address.c	2008-07-30 13:04:26 UTC (rev 16255)
@@ -838,15 +838,31 @@
   return tor_strdup(buf);
 }
 
-/** Convert the string in <b>src</b> to a tor_addr_t <b>addr</b>.
+/** Convert the string in <b>src</b> to a tor_addr_t <b>addr</b>.  The string
+ * may be an IPv4 address, an IPv6 address, or an IPv6 address surrounded by
+ * square brackets.
  *
  *  Return an address family on success, or -1 if an invalid address string is
  *  provided. */
 int
 tor_addr_from_str(tor_addr_t *addr, const char *src)
 {
+  char *tmp = NULL; /* Holds substring if we got a dotted quad. */
+  int result;
   tor_assert(addr && src);
-  return tor_addr_parse_mask_ports(src, addr, NULL, NULL, NULL);
+  if (src[0] == '[' && src[1])
+    src = tmp = tor_strndup(src+1, strlen(src)-2);
+
+  if (tor_inet_pton(AF_INET6, src, &addr->addr.in6_addr) > 0) {
+    result = addr->family = AF_INET6;
+  } else if (tor_inet_pton(AF_INET, src, &addr->addr.in_addr) > 0) {
+    result = addr->family = AF_INET;
+  } else {
+    result = -1;
+  }
+
+  tor_free(tmp);
+  return result;
 }
 
 /** Set *<b>addr</b> to the IP address (if any) of whatever interface