[tor-commits] [tor/master] Refactor ipv[46].[ch]

nickm at torproject.org nickm at torproject.org
Tue Jul 10 18:48:20 UTC 2018


commit 5d8336c182777c36ebff561b420d67fcafb9a9f7
Author: Nick Mathewson <nickm at torproject.org>
Date:   Tue Jul 10 12:50:38 2018 -0400

    Refactor ipv[46].[ch]
    
    These are now combined into an inaddr.[ch], since their purpose is
    to implement functions for struct in_addr and struct in6_addr.
    
    The definitions for in6_addr and its allies are now in a separate
    header, inaddr_st.h.
    
    Closes ticket 26532.
---
 src/core/or/or.h                    |  3 +-
 src/lib/net/address.c               |  2 +-
 src/lib/net/address.h               |  2 +-
 src/lib/net/{ipv6.c => inaddr.c}    | 57 +++++++++++++++++++++++++++++++------
 src/lib/net/{ipv4.h => inaddr.h}    | 14 ++++++---
 src/lib/net/{ipv6.h => inaddr_st.h} | 21 +++++++-------
 src/lib/net/include.am              |  7 ++---
 src/lib/net/ipv4.c                  | 57 -------------------------------------
 src/tools/tor-gencert.c             |  2 +-
 9 files changed, 76 insertions(+), 89 deletions(-)

diff --git a/src/core/or/or.h b/src/core/or/or.h
index 6edfd21df..a9bef9404 100644
--- a/src/core/or/or.h
+++ b/src/core/or/or.h
@@ -49,8 +49,7 @@
 #include "lib/log/util_bug.h"
 #include "lib/malloc/util_malloc.h"
 #include "lib/net/address.h"
-#include "lib/net/ipv4.h"
-#include "lib/net/ipv6.h"
+#include "lib/net/inaddr.h"
 #include "lib/net/resolve.h"
 #include "lib/net/socket.h"
 #include "lib/string/compat_ctype.h"
diff --git a/src/lib/net/address.c b/src/lib/net/address.c
index f3eddca7b..fbdd9591d 100644
--- a/src/lib/net/address.c
+++ b/src/lib/net/address.c
@@ -43,7 +43,7 @@
 #include "lib/log/torlog.h"
 #include "lib/log/escape.h"
 #include "lib/malloc/util_malloc.h"
-#include "lib/net/ipv4.h"
+#include "lib/net/inaddr.h"
 #include "lib/string/compat_ctype.h"
 #include "lib/string/compat_string.h"
 #include "lib/string/parse_int.h"
diff --git a/src/lib/net/address.h b/src/lib/net/address.h
index f8ea573c3..444405ec1 100644
--- a/src/lib/net/address.h
+++ b/src/lib/net/address.h
@@ -14,7 +14,7 @@
 #include "orconfig.h"
 #include "lib/cc/torint.h"
 #include "lib/log/util_bug.h"
-#include "lib/net/ipv6.h"
+#include "lib/net/inaddr_st.h"
 #include "lib/net/nettypes.h"
 
 #ifdef HAVE_NETINET_IN_H
diff --git a/src/lib/net/ipv6.c b/src/lib/net/inaddr.c
similarity index 82%
rename from src/lib/net/ipv6.c
rename to src/lib/net/inaddr.c
index 630d6f1db..dcd8fcdd6 100644
--- a/src/lib/net/ipv6.c
+++ b/src/lib/net/inaddr.c
@@ -4,20 +4,20 @@
 /* See LICENSE for licensing information */
 
 /**
- * \file ipv6.c
- * \brief Functions for encoding and decoding IPv6 addresses
- *
- * (Because these functions are generic, they can also handle IPv4 addresses).
+ * \file inaddr.c
+ * \brief Convert in_addr and in6_addr to and from strings.
  **/
 
-#include "lib/net/ipv6.h"
-#include "lib/net/ipv4.h"
-#include "lib/string/util_string.h"
-#include "lib/string/compat_string.h"
+#include "lib/net/inaddr.h"
+
+#include "lib/cc/torint.h"
+#include "lib/log/util_bug.h"
+#include "lib/net/inaddr_st.h"
 #include "lib/string/compat_ctype.h"
+#include "lib/string/compat_string.h"
 #include "lib/string/printf.h"
 #include "lib/string/scanf.h"
-#include "lib/log/util_bug.h"
+#include "lib/string/util_string.h"
 
 #ifdef HAVE_ARPA_INET_H
 #include <arpa/inet.h>
@@ -26,6 +26,45 @@
 #include <stdlib.h>
 #include <string.h>
 
+#ifdef _WIN32
+#include <winsock2.h>
+#endif
+
+/** Set *addr to the IP address (in dotted-quad notation) stored in *str.
+ * Return 1 on success, 0 if *str is badly formatted.
+ * (Like inet_aton(str,addr), but works on Windows and Solaris.)
+ */
+int
+tor_inet_aton(const char *str, struct in_addr* addr)
+{
+  unsigned a,b,c,d;
+  char more;
+  if (tor_sscanf(str, "%3u.%3u.%3u.%3u%c", &a,&b,&c,&d,&more) != 4)
+    return 0;
+  if (a > 255) return 0;
+  if (b > 255) return 0;
+  if (c > 255) return 0;
+  if (d > 255) return 0;
+  addr->s_addr = htonl((a<<24) | (b<<16) | (c<<8) | d);
+  return 1;
+}
+
+/** Given an IPv4 in_addr struct *<b>in</b> (in network order, as usual),
+ *  write it as a string into the <b>buf_len</b>-byte buffer in
+ *  <b>buf</b>. Returns a non-negative integer on success.
+ *  Returns -1 on failure.
+ */
+int
+tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len)
+{
+  uint32_t a = ntohl(in->s_addr);
+  return tor_snprintf(buf, buf_len, "%d.%d.%d.%d",
+                      (int)(uint8_t)((a>>24)&0xff),
+                      (int)(uint8_t)((a>>16)&0xff),
+                      (int)(uint8_t)((a>>8 )&0xff),
+                      (int)(uint8_t)((a    )&0xff));
+}
+
 /** Given <b>af</b>==AF_INET and <b>src</b> a struct in_addr, or
  * <b>af</b>==AF_INET6 and <b>src</b> a struct in6_addr, try to format the
  * address and store it in the <b>len</b>-byte buffer <b>dst</b>.  Returns
diff --git a/src/lib/net/ipv4.h b/src/lib/net/inaddr.h
similarity index 66%
rename from src/lib/net/ipv4.h
rename to src/lib/net/inaddr.h
index 0127f09e0..121025a12 100644
--- a/src/lib/net/ipv4.h
+++ b/src/lib/net/inaddr.h
@@ -4,18 +4,24 @@
 /* See LICENSE for licensing information */
 
 /**
- * \file ipv4.h
- * \brief Header for ipv4.c
+ * \file inaddr.h
+ * \brief Header for inaddr.c.
  **/
-#ifndef TOR_IPV4_H
-#define TOR_IPV4_H
 
+#ifndef TOR_INADDR_H
+#define TOR_INADDR_H
+
+#include "orconfig.h"
 #include <stddef.h>
 
 struct in_addr;
+
 int tor_inet_aton(const char *str, struct in_addr *addr);
 /** Length of a buffer to allocate to hold the results of tor_inet_ntoa.*/
 #define INET_NTOA_BUF_LEN 16
 int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len);
 
+const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
+int tor_inet_pton(int af, const char *src, void *dst);
+
 #endif
diff --git a/src/lib/net/ipv6.h b/src/lib/net/inaddr_st.h
similarity index 89%
rename from src/lib/net/ipv6.h
rename to src/lib/net/inaddr_st.h
index 4e5ef4da8..cc72621e9 100644
--- a/src/lib/net/ipv6.h
+++ b/src/lib/net/inaddr_st.h
@@ -4,25 +4,29 @@
 /* See LICENSE for licensing information */
 
 /**
- * \file ipv6.h
- * \brief Header for ipv6.c
+ * \file inaddr_st.h
+ *
+ * \brief Define in6_addr, its members, and related types on platforms that
+ *    lack it.
  **/
 
-#ifndef TOR_IPV6_H
-#define TOR_IPV6_H
+#ifndef TOR_INADDR_ST_H
+#define TOR_INADDR_ST_H
 
-#include "orconfig.h"
-#include <stddef.h>
 #ifdef HAVE_NETINET_IN6_H
 #include <netinet/in6.h>
 #endif
+
 #ifdef _WIN32
 #include <winsock2.h>
 #include <ws2tcpip.h>
 #include <windows.h>
 #endif
+
 #include "lib/cc/torint.h"
 
+struct in_addr;
+
 /** Implementation of struct in6_addr for platforms that do not have it.
  * Generally, these platforms are ones without IPv6 support, but we want to
  * have a working in6_addr there anyway, so we can use it to parse IPv6
@@ -85,7 +89,4 @@ struct sockaddr_in6 {
 };
 #endif /* !defined(HAVE_STRUCT_SOCKADDR_IN6) */
 
-const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
-int tor_inet_pton(int af, const char *src, void *dst);
-
-#endif
+#endif /* TOR_INADDR_ST_H */
diff --git a/src/lib/net/include.am b/src/lib/net/include.am
index 6fda17361..67db0d5af 100644
--- a/src/lib/net/include.am
+++ b/src/lib/net/include.am
@@ -10,8 +10,7 @@ src_lib_libtor_net_a_SOURCES =			\
 	src/lib/net/alertsock.c                 \
 	src/lib/net/buffers_net.c		\
 	src/lib/net/gethostname.c		\
-	src/lib/net/ipv4.c			\
-	src/lib/net/ipv6.c			\
+	src/lib/net/inaddr.c			\
 	src/lib/net/resolve.c			\
 	src/lib/net/socket.c
 
@@ -25,8 +24,8 @@ noinst_HEADERS +=				\
 	src/lib/net/alertsock.h                 \
 	src/lib/net/buffers_net.h		\
 	src/lib/net/gethostname.h		\
-	src/lib/net/ipv4.h			\
-	src/lib/net/ipv6.h			\
+	src/lib/net/inaddr.h			\
+	src/lib/net/inaddr_st.h			\
 	src/lib/net/nettypes.h			\
 	src/lib/net/resolve.h			\
 	src/lib/net/socket.h			\
diff --git a/src/lib/net/ipv4.c b/src/lib/net/ipv4.c
deleted file mode 100644
index db1429f49..000000000
--- a/src/lib/net/ipv4.c
+++ /dev/null
@@ -1,57 +0,0 @@
-/* Copyright (c) 2003-2004, Roger Dingledine
- * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2018, The Tor Project, Inc. */
-/* See LICENSE for licensing information */
-
-/**
- * \file ipv4.c
- * \brief Functions for encoding and decoding IPv4 addresses into strings
- **/
-
-#include "orconfig.h"
-#include "lib/cc/torint.h"
-#include "lib/net/ipv4.h"
-#include "lib/string/printf.h"
-#include "lib/string/scanf.h"
-
-#ifdef HAVE_ARPA_INET_H
-#include <arpa/inet.h>
-#endif
-#ifdef _WIN32
-#include <winsock2.h>
-#endif
-
-/** Set *addr to the IP address (in dotted-quad notation) stored in *str.
- * Return 1 on success, 0 if *str is badly formatted.
- * (Like inet_aton(str,addr), but works on Windows and Solaris.)
- */
-int
-tor_inet_aton(const char *str, struct in_addr* addr)
-{
-  unsigned a,b,c,d;
-  char more;
-  if (tor_sscanf(str, "%3u.%3u.%3u.%3u%c", &a,&b,&c,&d,&more) != 4)
-    return 0;
-  if (a > 255) return 0;
-  if (b > 255) return 0;
-  if (c > 255) return 0;
-  if (d > 255) return 0;
-  addr->s_addr = htonl((a<<24) | (b<<16) | (c<<8) | d);
-  return 1;
-}
-
-/** Given an IPv4 in_addr struct *<b>in</b> (in network order, as usual),
- *  write it as a string into the <b>buf_len</b>-byte buffer in
- *  <b>buf</b>. Returns a non-negative integer on success.
- *  Returns -1 on failure.
- */
-int
-tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len)
-{
-  uint32_t a = ntohl(in->s_addr);
-  return tor_snprintf(buf, buf_len, "%d.%d.%d.%d",
-                      (int)(uint8_t)((a>>24)&0xff),
-                      (int)(uint8_t)((a>>16)&0xff),
-                      (int)(uint8_t)((a>>8 )&0xff),
-                      (int)(uint8_t)((a    )&0xff));
-}
diff --git a/src/tools/tor-gencert.c b/src/tools/tor-gencert.c
index ce032ed64..c1cda0797 100644
--- a/src/tools/tor-gencert.c
+++ b/src/tools/tor-gencert.c
@@ -41,7 +41,7 @@ ENABLE_GCC_WARNING(redundant-decls)
 #include "lib/log/torlog.h"
 #include "lib/malloc/util_malloc.h"
 #include "lib/net/address.h"
-#include "lib/net/ipv4.h"
+#include "lib/net/inaddr.h"
 #include "lib/string/compat_string.h"
 #include "lib/string/printf.h"
 





More information about the tor-commits mailing list