]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
conn: Add a function to return a list of connection by state
authorDavid Goulet <dgoulet@torproject.org>
Tue, 18 Jul 2017 15:41:41 +0000 (11:41 -0400)
committerDavid Goulet <dgoulet@torproject.org>
Thu, 24 Aug 2017 17:03:28 +0000 (13:03 -0400)
This will be useful to the hidden service subsystem that needs to go over all
connections of a certain state to attach them to a hidden service circuit.

Signed-off-by: David Goulet <dgoulet@torproject.org>
src/or/connection.c
src/or/connection.h

index 5c65e886c0d5ded70fa177027418d2b433eaa4e7..31a682387d6794669a4c19ddcc1666ffa0d05a7e 100644 (file)
@@ -4102,6 +4102,27 @@ connection_write_to_buf_impl_,(const char *string, size_t len,
   }
 }
 
+#define CONN_GET_ALL_TEMPLATE(var, test) \
+  STMT_BEGIN \
+    smartlist_t *conns = get_connection_array();   \
+    smartlist_t *ret_conns = smartlist_new();     \
+    SMARTLIST_FOREACH_BEGIN(conns, connection_t *, var) { \
+      if (var && (test) && !var->marked_for_close) \
+        smartlist_add(ret_conns, var); \
+    } SMARTLIST_FOREACH_END(var);                                            \
+    return ret_conns; \
+  STMT_END
+
+/* Return a list of connections that aren't close and matches the given state.
+ * The returned list can be empty and must be freed using smartlist_free().
+ * The caller does NOT have owernship of the objects in the list so it must
+ * not free them nor reference them as they can disapear. */
+smartlist_t *
+connection_list_by_type_state(int type, int state)
+{
+  CONN_GET_ALL_TEMPLATE(conn, (conn->type == type && conn->state == state));
+}
+
 /** Return a connection_t * from get_connection_array() that satisfies test on
  * var, and that is not marked for close. */
 #define CONN_GET_TEMPLATE(var, test)               \
index 36e45aef389e1e5b9b082e03d8de78f74f67b407..0bcf0ccdceae1e8e7a4fc9406f8cdc3a5319b029 100644 (file)
@@ -182,6 +182,7 @@ MOCK_DECL(connection_t *,connection_get_by_type_addr_port_purpose,(int type,
 connection_t *connection_get_by_type_state(int type, int state);
 connection_t *connection_get_by_type_state_rendquery(int type, int state,
                                                      const char *rendquery);
+smartlist_t *connection_list_by_type_state(int type, int state);
 smartlist_t *connection_dir_list_by_purpose_and_resource(
                                                   int purpose,
                                                   const char *resource);