[tor-commits] [obfsproxy/master] Hoist (nearly-)ubiquitous includes and forward declarations to util.h.
nickm at torproject.org
nickm at torproject.org
Fri Sep 9 17:08:57 UTC 2011
commit 7184b10a0b08fd0cb7c5148be23898d38138f04e
Author: Zack Weinberg <zackw at panix.com>
Date: Mon Jul 25 16:26:55 2011 -0700
Hoist (nearly-)ubiquitous includes and forward declarations to util.h.
---
src/container.h | 2 --
src/crypt.c | 3 ---
src/crypt.h | 2 --
src/main.c | 2 --
src/network.c | 7 -------
src/network.h | 37 ++++---------------------------------
src/protocol.c | 4 ----
src/protocol.h | 33 ++++++++++++++-------------------
src/protocols/dummy.c | 2 --
src/protocols/dummy.h | 3 +--
src/protocols/obfs2.c | 2 --
src/protocols/obfs2.h | 3 +--
src/sha256.c | 1 -
src/sha256.h | 2 --
src/socks.c | 6 ------
src/socks.h | 4 ----
src/test/tinytest.c | 4 ----
src/util.c | 8 --------
src/util.h | 44 +++++++++++++++++++++++++++++++++++++++++---
19 files changed, 61 insertions(+), 108 deletions(-)
diff --git a/src/container.h b/src/container.h
index e1c1a07..129e4f9 100644
--- a/src/container.h
+++ b/src/container.h
@@ -6,8 +6,6 @@
#ifndef CONTAINER_H
#define CONTAINER_H
-#include <stdlib.h>
-#include <string.h>
#include <time.h>
/** A resizeable list of pointers, with associated helpful functionality.
diff --git a/src/crypt.c b/src/crypt.c
index 9019fc9..62d99fe 100644
--- a/src/crypt.c
+++ b/src/crypt.c
@@ -8,9 +8,6 @@
#include "crypt.h"
#include <fcntl.h>
-#include <limits.h>
-#include <stdlib.h>
-#include <string.h>
#include <unistd.h>
#include <openssl/opensslv.h>
diff --git a/src/crypt.h b/src/crypt.h
index beccda6..51bbc57 100644
--- a/src/crypt.h
+++ b/src/crypt.h
@@ -5,8 +5,6 @@
#ifndef CRYPT_H
#define CRYPT_H
-#include <stddef.h> /* for size_t */
-
#define SHA256_LENGTH 32
/* Stream cipher state */
diff --git a/src/main.c b/src/main.c
index cc69805..f6280c2 100644
--- a/src/main.c
+++ b/src/main.c
@@ -11,8 +11,6 @@
#include <errno.h>
#include <signal.h>
#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
#include <event2/event.h>
#include <event2/dns.h>
diff --git a/src/network.c b/src/network.c
index c4cf0c7..f7a94d3 100644
--- a/src/network.c
+++ b/src/network.c
@@ -13,18 +13,11 @@
#include "protocol.h"
#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/bufferevent_struct.h>
#include <event2/listener.h>
-#include <event2/util.h>
-
-#ifdef _WIN32
-#include <ws2tcpip.h> /* socklen_t */
-#endif
/* Terminology used in this file:
diff --git a/src/network.h b/src/network.h
index b6aa410..c5ef69e 100644
--- a/src/network.h
+++ b/src/network.h
@@ -5,51 +5,22 @@
#ifndef NETWORK_H
#define NETWORK_H
-struct event_base;
-struct protocol_params_t;
-
-#define LSN_SIMPLE_CLIENT 1
-#define LSN_SIMPLE_SERVER 2
-#define LSN_SOCKS_CLIENT 3
-
-enum recv_ret {
- /* Everything went fine. */
- RECV_GOOD=0,
- /* Something went bad. */
- RECV_BAD,
- /* ...need...more...data... */
- RECV_INCOMPLETE,
-
- /* Originally needed by the obfs2 protocol but it might get other
- users in the future.
- It means:
- "We have pending data that we have to send. You should do that by
- calling proto_send() immediately." */
- RECV_SEND_PENDING
-};
-
/* returns 1 on success, 0 on failure */
-int create_listener(struct event_base *base, struct protocol_params_t *params);
+int create_listener(struct event_base *base, protocol_params_t *params);
void free_all_listeners(void);
void start_shutdown(int barbaric);
#ifdef NETWORK_PRIVATE
-struct bufferevent;
-struct evconnlistener;
-struct socks_state_t;
-struct protocol_t;
-struct protocol_params_t;
-
typedef struct listener_t {
+ protocol_params_t *proto_params;
struct evconnlistener *listener;
- struct protocol_params_t *proto_params;
} listener_t;
typedef struct conn_t {
- struct protocol_t *proto;
- struct socks_state_t *socks_state;
+ protocol_t *proto;
+ socks_state_t *socks_state;
struct bufferevent *upstream;
struct bufferevent *downstream;
unsigned int mode : 30;
diff --git a/src/protocol.c b/src/protocol.c
index a010d16..2966e06 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -8,10 +8,6 @@
#include "protocols/obfs2.h"
#include "protocols/dummy.h"
-#include <stdlib.h>
-#include <string.h>
-
-
/**
All supported protocols should be put in this array.
It's used by main.c.
diff --git a/src/protocol.h b/src/protocol.h
index c023720..246ad1c 100644
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -5,11 +5,6 @@
#ifndef PROTOCOL_H
#define PROTOCOL_H
-#include "network.h" /* for recv_ret */
-#include <event2/util.h> /* for evutil_addrinfo */
-
-struct evbuffer;
-
/**
This struct defines the protocol-specific state for all connections
opened from a particular listener. Each protocol may extend this
@@ -17,12 +12,12 @@ struct evbuffer;
member of a larger structure (standard fake-inheritance-in-C
technique).
*/
-typedef struct protocol_params_t {
- const struct protocol_vtable *vtable;
+struct protocol_params_t {
+ const protocol_vtable *vtable;
struct evutil_addrinfo *target_addr;
struct evutil_addrinfo *listen_addr;
int mode;
-} protocol_params_t;
+};
/**
This struct defines the protocol-specific state for a particular
@@ -30,9 +25,9 @@ typedef struct protocol_params_t {
additional private data by embedding it as the first member of a
larger structure.
*/
-typedef struct protocol_t {
- const struct protocol_vtable *vtable;
-} protocol_t;
+struct protocol_t {
+ const protocol_vtable *vtable;
+};
/**
This struct defines a protocol and its methods; note that not all
@@ -42,7 +37,7 @@ typedef struct protocol_t {
principal interface between each individual protocol and generic
code. At present there is a static list of these objects in protocol.c.
*/
-typedef struct protocol_vtable
+struct protocol_vtable
{
/** The short name of this protocol. */
const char *name;
@@ -78,19 +73,19 @@ typedef struct protocol_vtable
struct evbuffer *source,
struct evbuffer *dest);
-} protocol_vtable;
+};
/**
Use this macro to define protocol_vtable objects; it ensures all
the methods are in the correct order and enforces a consistent
naming convention on protocol implementations.
*/
-#define DEFINE_PROTOCOL_VTABLE(name) \
- const struct protocol_vtable name##_vtable = { \
- #name, \
- name##_init, name##_fini, \
- name##_create, name##_destroy, \
- name##_handshake, name##_send, name##_recv \
+#define DEFINE_PROTOCOL_VTABLE(name) \
+ const protocol_vtable name##_vtable = { \
+ #name, \
+ name##_init, name##_fini, \
+ name##_create, name##_destroy, \
+ name##_handshake, name##_send, name##_recv \
}
protocol_params_t *proto_params_init(int n_options,
diff --git a/src/protocols/dummy.c b/src/protocols/dummy.c
index e6c4ca5..8ecce8f 100644
--- a/src/protocols/dummy.c
+++ b/src/protocols/dummy.c
@@ -7,8 +7,6 @@
#define PROTOCOL_DUMMY_PRIVATE
#include "dummy.h"
-#include <stdlib.h>
-#include <string.h>
#include <event2/buffer.h>
/* type-safe downcast wrappers */
diff --git a/src/protocols/dummy.h b/src/protocols/dummy.h
index 944fe1b..308c4f0 100644
--- a/src/protocols/dummy.h
+++ b/src/protocols/dummy.h
@@ -4,8 +4,7 @@
#ifndef PROTOCOL_DUMMY_H
#define PROTOCOL_DUMMY_H
-struct protocol_vtable;
-extern const struct protocol_vtable dummy_vtable;
+extern const protocol_vtable dummy_vtable;
#ifdef PROTOCOL_DUMMY_PRIVATE
diff --git a/src/protocols/obfs2.c b/src/protocols/obfs2.c
index 166c54a..6a6a975 100644
--- a/src/protocols/obfs2.c
+++ b/src/protocols/obfs2.c
@@ -7,8 +7,6 @@
#define PROTOCOL_OBFS2_PRIVATE
#include "obfs2.h"
-#include <stdlib.h>
-#include <string.h>
#include <event2/buffer.h>
/* type-safe downcast wrappers */
diff --git a/src/protocols/obfs2.h b/src/protocols/obfs2.h
index 107d15a..ce28217 100644
--- a/src/protocols/obfs2.h
+++ b/src/protocols/obfs2.h
@@ -5,8 +5,7 @@
#ifndef PROTOCOL_OBFS2_H
#define PROTOCOL_OBFS2_H
-struct protocol_vtable;
-extern const struct protocol_vtable obfs2_vtable;
+extern const protocol_vtable obfs2_vtable;
#ifdef PROTOCOL_OBFS2_PRIVATE
diff --git a/src/sha256.c b/src/sha256.c
index 7d273ae..fccbde6 100644
--- a/src/sha256.c
+++ b/src/sha256.c
@@ -7,7 +7,6 @@
#include "util.h"
#include "sha256.h"
-#include <string.h>
#include <arpa/inet.h> /* for htonl/ntohl */
#define STMT_BEGIN do {
diff --git a/src/sha256.h b/src/sha256.h
index b6a75be..6afdbfe 100644
--- a/src/sha256.h
+++ b/src/sha256.h
@@ -2,8 +2,6 @@
#ifndef SHA256_H
#define SHA256_H
-#include <stdint.h>
-
typedef struct sha256_state {
uint64_t length;
uint32_t state[8], curlen;
diff --git a/src/socks.c b/src/socks.c
index f1eada4..9941005 100644
--- a/src/socks.c
+++ b/src/socks.c
@@ -8,15 +8,9 @@
#include "socks.h"
#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
#include <event2/buffer.h>
-#ifdef _WIN32
-#include <ws2tcpip.h> /* sockaddr_in6 */
-#endif
-
/**
General SOCKS5 idea:
diff --git a/src/socks.h b/src/socks.h
index db10cb5..a06a11e 100644
--- a/src/socks.h
+++ b/src/socks.h
@@ -5,10 +5,6 @@
#ifndef SOCKS_H
#define SOCKS_H
-typedef struct socks_state_t socks_state_t;
-struct evbuffer;
-struct sockaddr;
-
enum socks_status_t {
/* Waiting for initial socks4 or socks5 message */
ST_WAITING,
diff --git a/src/test/tinytest.c b/src/test/tinytest.c
index 5151bcb..32e7b76 100644
--- a/src/test/tinytest.c
+++ b/src/test/tinytest.c
@@ -23,13 +23,9 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include "tinytest.h"
#include "tinytest_macros.h"
#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
diff --git a/src/util.c b/src/util.c
index 8e63944..167e867 100644
--- a/src/util.c
+++ b/src/util.c
@@ -6,18 +6,10 @@
#include <errno.h>
#include <fcntl.h>
-#include <limits.h>
#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
#include <unistd.h>
#include <event2/dns.h>
-#include <event2/util.h>
-
-#ifdef _WIN32
-#include <ws2tcpip.h> /* addrinfo */
-#endif
/** Any size_t larger than this amount is likely to be an underflow. */
#define SIZE_T_CEILING (SIZE_MAX/2 - 16)
diff --git a/src/util.h b/src/util.h
index cb98fbd..ede23e4 100644
--- a/src/util.h
+++ b/src/util.h
@@ -6,14 +6,25 @@
#define UTIL_H
#include "config.h"
+
+#include <limits.h>
#include <stdarg.h> /* va_list */
#include <stddef.h> /* size_t, ptrdiff_t, offsetof, NULL */
#include <stdint.h> /* intN_t, uintN_t */
+#include <stdlib.h>
+#include <string.h>
+
#include <event2/util.h> /* evutil_addrinfo */
+#ifdef _WIN32
+#include <ws2tcpip.h> /* addrinfo (event2/util.h should do this,
+ but it doesn't) */
+#endif
-struct sockaddr;
-struct event_base;
+struct bufferevent;
+struct evconnlistener;
+struct evbuffer;
struct evdns_base;
+struct event_base;
/***** Type annotations. *****/
@@ -49,7 +60,34 @@ char *xstrndup(const char *s, size_t maxsize) ATTR_MALLOC;
unsigned int ui64_log2(uint64_t u64);
-/***** Network functions. *****/
+/***** Network types and functions. *****/
+
+typedef struct protocol_t protocol_t;
+typedef struct protocol_params_t protocol_params_t;
+typedef struct protocol_vtable protocol_vtable;
+typedef struct socks_state_t socks_state_t;
+
+enum recv_ret {
+ /* Everything went fine. */
+ RECV_GOOD=0,
+ /* Something went bad. */
+ RECV_BAD,
+ /* ...need...more...data... */
+ RECV_INCOMPLETE,
+
+ /* Originally needed by the obfs2 protocol but it might get other
+ users in the future.
+ It means:
+ "We have pending data that we have to send. You should do that by
+ calling proto_send() immediately." */
+ RECV_SEND_PENDING
+};
+
+enum listen_mode {
+ LSN_SIMPLE_CLIENT = 1,
+ LSN_SIMPLE_SERVER,
+ LSN_SOCKS_CLIENT
+};
struct evutil_addrinfo *resolve_address_port(const char *address,
int nodns, int passive,
More information about the tor-commits
mailing list