[tor-commits] [tor/master] Extract connection_t into its own header.

nickm at torproject.org nickm at torproject.org
Mon Jun 18 18:18:42 UTC 2018


commit bba998af65c9a414c56824f5779d2c47cf5a6b44
Author: Nick Mathewson <nickm at torproject.org>
Date:   Fri Jun 15 11:05:56 2018 -0400

    Extract connection_t into its own header.
    
    Now the entire connection_t hierarchy is extracted from or.h
---
 src/or/config.c                 |   1 +
 src/or/connection.c             |  26 ++++++++
 src/or/connection.h             |  34 +----------
 src/or/connection_st.h          | 132 ++++++++++++++++++++++++++++++++++++++++
 src/or/control_connection_st.h  |   1 +
 src/or/dir_connection_st.h      |   2 +
 src/or/edge_connection_st.h     |   2 +
 src/or/include.am               |   1 +
 src/or/listener_connection_st.h |   2 +
 src/or/or.h                     | 122 +------------------------------------
 src/or/or_connection_st.h       |   2 +
 src/test/test_helpers.c         |   2 +
 12 files changed, 175 insertions(+), 152 deletions(-)

diff --git a/src/or/config.c b/src/or/config.c
index 51ecca431..ca495aa97 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -116,6 +116,7 @@
 #include "dirauth/dirvote.h"
 #include "dirauth/mode.h"
 
+#include "connection_st.h"
 #include "port_cfg_st.h"
 
 #ifdef HAVE_SYSTEMD
diff --git a/src/or/connection.c b/src/or/connection.c
index 3c5330154..867c4d72d 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -183,6 +183,18 @@ TO_LISTENER_CONN(connection_t *c)
   return DOWNCAST(listener_connection_t, c);
 }
 
+size_t
+connection_get_inbuf_len(connection_t *conn)
+{
+  return conn->inbuf ? buf_datalen(conn->inbuf) : 0;
+}
+
+size_t
+connection_get_outbuf_len(connection_t *conn)
+{
+    return conn->outbuf ? buf_datalen(conn->outbuf) : 0;
+}
+
 /**
  * Return the human-readable name for the connection type <b>type</b>
  */
@@ -4835,6 +4847,20 @@ kill_conn_list_for_oos, (smartlist_t *conns))
              smartlist_len(conns));
 }
 
+/** Check if a connection is on the way out so the OOS handler doesn't try
+ * to kill more than it needs. */
+int
+connection_is_moribund(connection_t *conn)
+{
+  if (conn != NULL &&
+      (conn->conn_array_index < 0 ||
+       conn->marked_for_close)) {
+    return 1;
+  } else {
+    return 0;
+  }
+}
+
 /** Out-of-Sockets handler; n_socks is the current number of open
  * sockets, and failed is non-zero if a socket exhaustion related
  * error immediately preceded this call.  This is where to do
diff --git a/src/or/connection.h b/src/or/connection.h
index df3388239..0ab8962b4 100644
--- a/src/or/connection.h
+++ b/src/or/connection.h
@@ -161,23 +161,8 @@ void connection_buf_add_compress(const char *string, size_t len,
                                  dir_connection_t *conn, int done);
 void connection_buf_add_buf(connection_t *conn, buf_t *buf);
 
-/* DOCDOC connection_get_inbuf_len */
-static size_t connection_get_inbuf_len(connection_t *conn);
-/* DOCDOC connection_get_outbuf_len */
-static size_t connection_get_outbuf_len(connection_t *conn);
-
-static inline size_t
-connection_get_inbuf_len(connection_t *conn)
-{
-  return conn->inbuf ? buf_datalen(conn->inbuf) : 0;
-}
-
-static inline size_t
-connection_get_outbuf_len(connection_t *conn)
-{
-    return conn->outbuf ? buf_datalen(conn->outbuf) : 0;
-}
-
+size_t connection_get_inbuf_len(connection_t *conn);
+size_t connection_get_outbuf_len(connection_t *conn);
 connection_t *connection_get_by_global_id(uint64_t id);
 
 connection_t *connection_get_by_type(int type);
@@ -254,20 +239,7 @@ MOCK_DECL(void, clock_skew_warning,
            log_domain_mask_t domain, const char *received,
            const char *source));
 
-/** Check if a connection is on the way out so the OOS handler doesn't try
- * to kill more than it needs. */
-static inline int
-connection_is_moribund(connection_t *conn)
-{
-  if (conn != NULL &&
-      (conn->conn_array_index < 0 ||
-       conn->marked_for_close)) {
-    return 1;
-  } else {
-    return 0;
-  }
-}
-
+int connection_is_moribund(connection_t *conn);
 void connection_check_oos(int n_socks, int failed);
 
 #ifdef CONNECTION_PRIVATE
diff --git a/src/or/connection_st.h b/src/or/connection_st.h
new file mode 100644
index 000000000..b65238693
--- /dev/null
+++ b/src/or/connection_st.h
@@ -0,0 +1,132 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2017, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifndef CONNECTION_ST_H
+#define CONNECTION_ST_H
+
+struct buf_t;
+
+/** Description of a connection to another host or process, and associated
+ * data.
+ *
+ * A connection is named based on what it's connected to -- an "OR
+ * connection" has a Tor node on the other end, an "exit
+ * connection" has a website or other server on the other end, and an
+ * "AP connection" has an application proxy (and thus a user) on the
+ * other end.
+ *
+ * Every connection has a type and a state.  Connections never change
+ * their type, but can go through many state changes in their lifetime.
+ *
+ * Every connection has two associated input and output buffers.
+ * Listeners don't use them.  For non-listener connections, incoming
+ * data is appended to conn->inbuf, and outgoing data is taken from
+ * conn->outbuf.  Connections differ primarily in the functions called
+ * to fill and drain these buffers.
+ */
+typedef struct connection_t {
+  uint32_t magic; /**< For memory debugging: must equal one of
+                   * *_CONNECTION_MAGIC. */
+
+  uint8_t state; /**< Current state of this connection. */
+  unsigned int type:5; /**< What kind of connection is this? */
+  unsigned int purpose:5; /**< Only used for DIR and EXIT types currently. */
+
+  /* The next fields are all one-bit booleans. Some are only applicable to
+   * connection subtypes, but we hold them here anyway, to save space.
+   */
+  unsigned int read_blocked_on_bw:1; /**< Boolean: should we start reading
+                            * again once the bandwidth throttler allows it? */
+  unsigned int write_blocked_on_bw:1; /**< Boolean: should we start writing
+                             * again once the bandwidth throttler allows
+                             * writes? */
+  unsigned int hold_open_until_flushed:1; /**< Despite this connection's being
+                                      * marked for close, do we flush it
+                                      * before closing it? */
+  unsigned int inbuf_reached_eof:1; /**< Boolean: did read() return 0 on this
+                                     * conn? */
+  /** Set to 1 when we're inside connection_flushed_some to keep us from
+   * calling connection_handle_write() recursively. */
+  unsigned int in_flushed_some:1;
+  /** True if connection_handle_write is currently running on this connection.
+   */
+  unsigned int in_connection_handle_write:1;
+
+  /* For linked connections:
+   */
+  unsigned int linked:1; /**< True if there is, or has been, a linked_conn. */
+  /** True iff we'd like to be notified about read events from the
+   * linked conn. */
+  unsigned int reading_from_linked_conn:1;
+  /** True iff we're willing to write to the linked conn. */
+  unsigned int writing_to_linked_conn:1;
+  /** True iff we're currently able to read on the linked conn, and our
+   * read_event should be made active with libevent. */
+  unsigned int active_on_link:1;
+  /** True iff we've called connection_close_immediate() on this linked
+   * connection. */
+  unsigned int linked_conn_is_closed:1;
+
+  /** CONNECT/SOCKS proxy client handshake state (for outgoing connections). */
+  unsigned int proxy_state:4;
+
+  /** Our socket; set to TOR_INVALID_SOCKET if this connection is closed,
+   * or has no socket. */
+  tor_socket_t s;
+  int conn_array_index; /**< Index into the global connection array. */
+
+  struct event *read_event; /**< Libevent event structure. */
+  struct event *write_event; /**< Libevent event structure. */
+  struct buf_t *inbuf; /**< Buffer holding data read over this connection. */
+  struct buf_t *outbuf; /**< Buffer holding data to write over this
+                         * connection. */
+  size_t outbuf_flushlen; /**< How much data should we try to flush from the
+                           * outbuf? */
+  time_t timestamp_last_read_allowed; /**< When was the last time libevent said
+                                       * we could read? */
+  time_t timestamp_last_write_allowed; /**< When was the last time libevent
+                                        * said we could write? */
+
+  time_t timestamp_created; /**< When was this connection_t created? */
+
+  int socket_family; /**< Address family of this connection's socket.  Usually
+                      * AF_INET, but it can also be AF_UNIX, or AF_INET6 */
+  tor_addr_t addr; /**< IP that socket "s" is directly connected to;
+                    * may be the IP address for a proxy or pluggable transport,
+                    * see "address" for the address of the final destination.
+                    */
+  uint16_t port; /**< If non-zero, port that socket "s" is directly connected
+                  * to; may be the port for a proxy or pluggable transport,
+                  * see "address" for the port at the final destination. */
+  uint16_t marked_for_close; /**< Should we close this conn on the next
+                              * iteration of the main loop? (If true, holds
+                              * the line number where this connection was
+                              * marked.) */
+  const char *marked_for_close_file; /**< For debugging: in which file were
+                                      * we marked for close? */
+  char *address; /**< FQDN (or IP) and port of the final destination for this
+                  * connection; this is always the remote address, it is
+                  * passed to a proxy or pluggable transport if one in use.
+                  * See "addr" and "port" for the address that socket "s" is
+                  * directly connected to.
+                  * strdup into this, because free_connection() frees it. */
+  /** Another connection that's connected to this one in lieu of a socket. */
+  struct connection_t *linked_conn;
+
+  /** Unique identifier for this connection on this Tor instance. */
+  uint64_t global_identifier;
+
+  /** Bytes read since last call to control_event_conn_bandwidth_used().
+   * Only used if we're configured to emit CONN_BW events. */
+  uint32_t n_read_conn_bw;
+
+  /** Bytes written since last call to control_event_conn_bandwidth_used().
+   * Only used if we're configured to emit CONN_BW events. */
+  uint32_t n_written_conn_bw;
+} connection_t;
+
+#endif
+
diff --git a/src/or/control_connection_st.h b/src/or/control_connection_st.h
index 2d8efec1f..7770b54d5 100644
--- a/src/or/control_connection_st.h
+++ b/src/or/control_connection_st.h
@@ -8,6 +8,7 @@
 #define CONTROL_CONNECTION_ST_H
 
 #include "or.h"
+#include "connection_st.h"
 
 /** Subtype of connection_t for an connection to a controller. */
 struct control_connection_t {
diff --git a/src/or/dir_connection_st.h b/src/or/dir_connection_st.h
index df1e9b551..379f787df 100644
--- a/src/or/dir_connection_st.h
+++ b/src/or/dir_connection_st.h
@@ -7,6 +7,8 @@
 #ifndef DIR_CONNECTION_ST_H
 #define DIR_CONNECTION_ST_H
 
+#include "connection_st.h"
+
 /** Subtype of connection_t for an "directory connection" -- that is, an HTTP
  * connection to retrieve or serve directory material. */
 struct dir_connection_t {
diff --git a/src/or/edge_connection_st.h b/src/or/edge_connection_st.h
index 3cffdea32..7ef56bf06 100644
--- a/src/or/edge_connection_st.h
+++ b/src/or/edge_connection_st.h
@@ -9,6 +9,8 @@
 
 #include "or.h"
 
+#include "connection_st.h"
+
 /** Subtype of connection_t for an "edge connection" -- that is, an entry (ap)
  * connection, or an exit. */
 struct edge_connection_t {
diff --git a/src/or/include.am b/src/or/include.am
index f3bbaa11c..de263e9b8 100644
--- a/src/or/include.am
+++ b/src/or/include.am
@@ -197,6 +197,7 @@ ORHEADERS = \
 	src/or/config.h					\
 	src/or/confparse.h				\
 	src/or/connection.h				\
+	src/or/connection_st.h				\
 	src/or/connection_edge.h			\
 	src/or/connection_or.h				\
 	src/or/conscache.h				\
diff --git a/src/or/listener_connection_st.h b/src/or/listener_connection_st.h
index 67718c5a3..63588c99a 100644
--- a/src/or/listener_connection_st.h
+++ b/src/or/listener_connection_st.h
@@ -7,6 +7,8 @@
 #ifndef LISTENER_CONNECTION_ST_H
 #define LISTENER_CONNECTION_ST_H
 
+#include "connection_st.h"
+
 /** Subtype of connection_t; used for a listener socket. */
 struct listener_connection_t {
   connection_t base_;
diff --git a/src/or/or.h b/src/or/or.h
index 990f7f94e..c3506d3ff 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -1260,127 +1260,6 @@ typedef struct server_port_cfg_t server_port_cfg_t;
 #define CONTROL_CONNECTION_MAGIC 0x8abc765du
 #define LISTENER_CONNECTION_MAGIC 0x1a1ac741u
 
-struct buf_t;
-
-/** Description of a connection to another host or process, and associated
- * data.
- *
- * A connection is named based on what it's connected to -- an "OR
- * connection" has a Tor node on the other end, an "exit
- * connection" has a website or other server on the other end, and an
- * "AP connection" has an application proxy (and thus a user) on the
- * other end.
- *
- * Every connection has a type and a state.  Connections never change
- * their type, but can go through many state changes in their lifetime.
- *
- * Every connection has two associated input and output buffers.
- * Listeners don't use them.  For non-listener connections, incoming
- * data is appended to conn->inbuf, and outgoing data is taken from
- * conn->outbuf.  Connections differ primarily in the functions called
- * to fill and drain these buffers.
- */
-typedef struct connection_t {
-  uint32_t magic; /**< For memory debugging: must equal one of
-                   * *_CONNECTION_MAGIC. */
-
-  uint8_t state; /**< Current state of this connection. */
-  unsigned int type:5; /**< What kind of connection is this? */
-  unsigned int purpose:5; /**< Only used for DIR and EXIT types currently. */
-
-  /* The next fields are all one-bit booleans. Some are only applicable to
-   * connection subtypes, but we hold them here anyway, to save space.
-   */
-  unsigned int read_blocked_on_bw:1; /**< Boolean: should we start reading
-                            * again once the bandwidth throttler allows it? */
-  unsigned int write_blocked_on_bw:1; /**< Boolean: should we start writing
-                             * again once the bandwidth throttler allows
-                             * writes? */
-  unsigned int hold_open_until_flushed:1; /**< Despite this connection's being
-                                      * marked for close, do we flush it
-                                      * before closing it? */
-  unsigned int inbuf_reached_eof:1; /**< Boolean: did read() return 0 on this
-                                     * conn? */
-  /** Set to 1 when we're inside connection_flushed_some to keep us from
-   * calling connection_handle_write() recursively. */
-  unsigned int in_flushed_some:1;
-  /** True if connection_handle_write is currently running on this connection.
-   */
-  unsigned int in_connection_handle_write:1;
-
-  /* For linked connections:
-   */
-  unsigned int linked:1; /**< True if there is, or has been, a linked_conn. */
-  /** True iff we'd like to be notified about read events from the
-   * linked conn. */
-  unsigned int reading_from_linked_conn:1;
-  /** True iff we're willing to write to the linked conn. */
-  unsigned int writing_to_linked_conn:1;
-  /** True iff we're currently able to read on the linked conn, and our
-   * read_event should be made active with libevent. */
-  unsigned int active_on_link:1;
-  /** True iff we've called connection_close_immediate() on this linked
-   * connection. */
-  unsigned int linked_conn_is_closed:1;
-
-  /** CONNECT/SOCKS proxy client handshake state (for outgoing connections). */
-  unsigned int proxy_state:4;
-
-  /** Our socket; set to TOR_INVALID_SOCKET if this connection is closed,
-   * or has no socket. */
-  tor_socket_t s;
-  int conn_array_index; /**< Index into the global connection array. */
-
-  struct event *read_event; /**< Libevent event structure. */
-  struct event *write_event; /**< Libevent event structure. */
-  struct buf_t *inbuf; /**< Buffer holding data read over this connection. */
-  struct buf_t *outbuf; /**< Buffer holding data to write over this
-                         * connection. */
-  size_t outbuf_flushlen; /**< How much data should we try to flush from the
-                           * outbuf? */
-  time_t timestamp_last_read_allowed; /**< When was the last time libevent said
-                                       * we could read? */
-  time_t timestamp_last_write_allowed; /**< When was the last time libevent
-                                        * said we could write? */
-
-  time_t timestamp_created; /**< When was this connection_t created? */
-
-  int socket_family; /**< Address family of this connection's socket.  Usually
-                      * AF_INET, but it can also be AF_UNIX, or AF_INET6 */
-  tor_addr_t addr; /**< IP that socket "s" is directly connected to;
-                    * may be the IP address for a proxy or pluggable transport,
-                    * see "address" for the address of the final destination.
-                    */
-  uint16_t port; /**< If non-zero, port that socket "s" is directly connected
-                  * to; may be the port for a proxy or pluggable transport,
-                  * see "address" for the port at the final destination. */
-  uint16_t marked_for_close; /**< Should we close this conn on the next
-                              * iteration of the main loop? (If true, holds
-                              * the line number where this connection was
-                              * marked.) */
-  const char *marked_for_close_file; /**< For debugging: in which file were
-                                      * we marked for close? */
-  char *address; /**< FQDN (or IP) and port of the final destination for this
-                  * connection; this is always the remote address, it is
-                  * passed to a proxy or pluggable transport if one in use.
-                  * See "addr" and "port" for the address that socket "s" is
-                  * directly connected to.
-                  * strdup into this, because free_connection() frees it. */
-  /** Another connection that's connected to this one in lieu of a socket. */
-  struct connection_t *linked_conn;
-
-  /** Unique identifier for this connection on this Tor instance. */
-  uint64_t global_identifier;
-
-  /** Bytes read since last call to control_event_conn_bandwidth_used().
-   * Only used if we're configured to emit CONN_BW events. */
-  uint32_t n_read_conn_bw;
-
-  /** Bytes written since last call to control_event_conn_bandwidth_used().
-   * Only used if we're configured to emit CONN_BW events. */
-  uint32_t n_written_conn_bw;
-} connection_t;
-
 /** Minimum length of the random part of an AUTH_CHALLENGE cell. */
 #define OR_AUTH_CHALLENGE_LEN 32
 
@@ -1458,6 +1337,7 @@ typedef struct or_handshake_state_t or_handshake_state_t;
  * drops below this size. */
 #define OR_CONN_LOWWATER (16*1024)
 
+typedef struct connection_t connection_t;
 typedef struct control_connection_t control_connection_t;
 typedef struct dir_connection_t dir_connection_t;
 typedef struct edge_connection_t edge_connection_t;
diff --git a/src/or/or_connection_st.h b/src/or/or_connection_st.h
index bccfd18f6..dd775bc8a 100644
--- a/src/or/or_connection_st.h
+++ b/src/or/or_connection_st.h
@@ -7,6 +7,8 @@
 #ifndef OR_CONNECTION_ST_H
 #define OR_CONNECTION_ST_H
 
+#include "connection_st.h"
+
 /** Subtype of connection_t for an "OR connection" -- that is, one that speaks
  * cells over TLS. */
 struct or_connection_t {
diff --git a/src/test/test_helpers.c b/src/test/test_helpers.c
index 1db5e9064..86aeabb7c 100644
--- a/src/test/test_helpers.c
+++ b/src/test/test_helpers.c
@@ -24,6 +24,8 @@
 #include "relay.h"
 #include "routerlist.h"
 
+#include "connection_st.h"
+
 #include "test.h"
 #include "test_helpers.h"
 #include "test_connection.h"





More information about the tor-commits mailing list