[tor-commits] [obfsproxy/master] Header file hygiene part 1. May not compile on Windows or *BSD.

nickm at torproject.org nickm at torproject.org
Thu Jul 14 16:28:33 UTC 2011


commit ea61c56da371a96a914ba367fbef26957af127ab
Author: Zack Weinberg <zackw at panix.com>
Date:   Mon Jul 11 12:03:38 2011 -0700

    Header file hygiene part 1.  May not compile on Windows or *BSD.
    
    All header files (except tinytest_macros.h) now include all headers
    that should be required for their contents.  All source files have
    been pruned to the minimal set of headers which still permit
    compilation on Linux.  Also a certain amount of code reformatting
    (removal of trailing spaces, fix overlong lines, etc).
---
 src/crypt.c               |   14 ++---
 src/crypt.h               |    5 ++
 src/main.c                |    4 --
 src/network.c             |   17 ++----
 src/protocol.c            |   14 ++---
 src/protocol.h            |    4 +-
 src/protocols/dummy.c     |   30 +++++------
 src/protocols/dummy.h     |    1 -
 src/protocols/obfs2.c     |   52 +++++++++---------
 src/protocols/obfs2.h     |   11 ++--
 src/socks.c               |  106 ++++++++++++++++--------------------
 src/test/tinytest.c       |   10 ++--
 src/test/unittest.c       |    1 -
 src/test/unittest_crypt.c |   17 ++-----
 src/test/unittest_obfs2.c |  131 ++++++++++++++++++++++++---------------------
 src/test/unittest_socks.c |   13 ++---
 src/util.c                |   19 ++-----
 17 files changed, 205 insertions(+), 244 deletions(-)

diff --git a/src/crypt.c b/src/crypt.c
index 91868f2..43f6dfb 100644
--- a/src/crypt.c
+++ b/src/crypt.c
@@ -2,22 +2,18 @@
    See LICENSE for other credits and copying information
 */
 
-#include "config.h"
+#define CRYPT_PRIVATE
+#include "crypt.h"
 
 #include <assert.h>
-#include <string.h>
+#include <fcntl.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
-#include <fcntl.h>
-#include <stdint.h>
 
 #include <openssl/opensslv.h>
-#include <openssl/aes.h>
-#include <openssl/rand.h>
 #include <openssl/err.h>
-
-#define CRYPT_PRIVATE
-#include "crypt.h"
+#include <openssl/rand.h>
 
 #if OPENSSL_VERSION_NUMBER >= 0x0090800f
 #define USE_OPENSSL_RANDPOLL 1
diff --git a/src/crypt.h b/src/crypt.h
index 5681a4c..d87b1de 100644
--- a/src/crypt.h
+++ b/src/crypt.h
@@ -5,6 +5,8 @@
 #ifndef CRYPT_H
 #define CRYPT_H
 
+#include <stddef.h> /* for size_t */
+
 #define SHA256_LENGTH 32
 
 /* Stream cipher state */
@@ -43,6 +45,9 @@ void crypt_free(crypt_t *);
 int random_bytes(uchar *b, size_t n);
 
 #ifdef CRYPT_PRIVATE
+
+#include <openssl/aes.h>
+
 /* ==========
    These definitions are not part of the crypt interface.
    They're exposed here so that the unit tests can use them.
diff --git a/src/main.c b/src/main.c
index 0ba1f3f..8788067 100644
--- a/src/main.c
+++ b/src/main.c
@@ -13,10 +13,6 @@
 #include "util.h"
 #include "protocol.h"
 
-#ifdef _WIN32
-#include <Ws2tcpip.h>
-#endif
-
 #ifndef __GNUC__
 #define __attribute__(x)
 #endif
diff --git a/src/network.c b/src/network.c
index dfa086c..c6348fa 100644
--- a/src/network.c
+++ b/src/network.c
@@ -4,10 +4,10 @@
 
 #define NETWORK_PRIVATE
 #include "network.h"
+
 #include "util.h"
 #include "socks.h"
 #include "protocol.h"
-#include "socks.h"
 
 #include <assert.h>
 #include <errno.h>
@@ -19,10 +19,6 @@
 #include <event2/listener.h>
 #include <event2/util.h>
 
-#ifdef _WIN32
-#include <WS2tcpip.h>
-#endif
-
 struct listener_t {
   struct evconnlistener *listener;
   protocol_params_t *proto_params;
@@ -63,12 +59,11 @@ listener_new(struct event_base *base,
   }
 
   lsn->proto_params = proto_params;
-
   lsn->listener = evconnlistener_new_bind(base, simple_listener_cb, lsn,
                                           flags,
                                           -1,
-                                          lsn->proto_params->listen_address,
-                                          lsn->proto_params->listen_address_len);
+                                          proto_params->listen_address,
+                                          proto_params->listen_address_len);
 
   if (!lsn->listener) {
     log_warn("Failed to create listener!");
@@ -286,11 +281,11 @@ obfuscated_read_cb(struct bufferevent *bev, void *arg)
   r = proto_recv(conn->proto,
                  bufferevent_get_input(bev),
                  bufferevent_get_output(other));
-  
+
   if (r == RECV_BAD)
     conn_free(conn);
   else if (r == RECV_SEND_PENDING)
-    proto_send(conn->proto, 
+    proto_send(conn->proto,
                bufferevent_get_input(conn->input),
                bufferevent_get_output(conn->output));
 }
@@ -346,7 +341,7 @@ output_event_cb(struct bufferevent *bev, short what, void *arg)
      client and terminate the connection.
   */
   if (what & BEV_EVENT_ERROR) {
-    if ((conn->mode == LSN_SOCKS_CLIENT) && 
+    if ((conn->mode == LSN_SOCKS_CLIENT) &&
         (conn->socks_state) &&
         (socks_state_get_status(conn->socks_state) == ST_HAVE_ADDR)) {
       log_debug("Connection failed") ;
diff --git a/src/protocol.c b/src/protocol.c
index e306c8e..557a5cc 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -2,18 +2,16 @@
    See LICENSE for other credits and copying information
 */
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <assert.h>
-#include <string.h>
-
 #include "protocol.h"
-#include "network.h"
-
 #include "protocols/obfs2.h"
 #include "protocols/dummy.h"
 
-/** 
+#include <assert.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 1f0e7dd..6d5dcfe 100644
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -5,8 +5,10 @@
 #ifndef PROTOCOL_H
 #define PROTOCOL_H
 
+#include <stddef.h>   /* for size_t */
+#include "network.h"  /* for recv_ret */
+
 struct evbuffer;
-struct listener_t;
 struct sockaddr;
 
 #define DUMMY_PROTOCOL      1
diff --git a/src/protocols/dummy.c b/src/protocols/dummy.c
index 8db5d51..7a32b47 100644
--- a/src/protocols/dummy.c
+++ b/src/protocols/dummy.c
@@ -1,40 +1,36 @@
 /* Copyright 2011 Nick Mathewson, George Kadianakis
    See LICENSE for other credits and copying information
 */
+
+#include "dummy.h"
+#include "../protocol.h"
+#include "../util.h"
+
 #include <assert.h>
-#include <string.h>
-#include <stdlib.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
-#include <unistd.h>
-
-#include <openssl/rand.h>
 #include <event2/buffer.h>
 
-#include "dummy.h"
-#include "../network.h"
-#include "../util.h"
-#include "../protocol.h"
-#include "../network.h"
-
 static int dummy_send(void *nothing,
-                                struct evbuffer *source, struct evbuffer *dest);
+                      struct evbuffer *source, struct evbuffer *dest);
 static enum recv_ret dummy_recv(void *nothing, struct evbuffer *source,
                                 struct evbuffer *dest);
 static void usage(void);
-static int parse_and_set_options(int n_options, char **options, 
+static int parse_and_set_options(int n_options, char **options,
                                  struct protocol_params_t *params);
 
 static protocol_vtable *vtable=NULL;
 /**
-   This function sets up the protocol and populates 'listner'
+   This function sets up the protocol and populates 'params'
    according to 'options'.
 
    'options' is an array like this:
    {"dummy","socks","127.0.0.1:6666"}
-*/   
+*/
 int
-dummy_init(int n_options, char **options, 
+dummy_init(int n_options, char **options,
            struct protocol_params_t *params)
 {
   if (parse_and_set_options(n_options,options,params) < 0) {
@@ -127,7 +123,7 @@ static enum recv_ret
 dummy_recv(void *nothing,
            struct evbuffer *source, struct evbuffer *dest) {
   (void)nothing;
-  
+
   if (evbuffer_add_buffer(dest,source)<0)
     return RECV_BAD;
   else
diff --git a/src/protocols/dummy.h b/src/protocols/dummy.h
index 654e0a2..a3fa32d 100644
--- a/src/protocols/dummy.h
+++ b/src/protocols/dummy.h
@@ -5,7 +5,6 @@
 #define DUMMY_H
 
 struct protocol_t;
-struct evbuffer;
 struct protocol_params_t;
 
 int dummy_init(int n_options, char **options, struct protocol_params_t *lsn);
diff --git a/src/protocols/obfs2.c b/src/protocols/obfs2.c
index d297c16..c2169a1 100644
--- a/src/protocols/obfs2.c
+++ b/src/protocols/obfs2.c
@@ -2,22 +2,19 @@
    See LICENSE for other credits and copying information
 */
 
+#define CRYPT_PROTOCOL_PRIVATE
+#include "obfs2.h"
+
+#include "../protocol.h"
+#include "../util.h"
+
 #include <assert.h>
-#include <string.h>
-#include <stdlib.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
-#include <openssl/rand.h>
 #include <event2/buffer.h>
 
-#include "../crypt.h"
-#include "../network.h"
-#include "../util.h"
-#include "../protocol.h"
-#include "../network.h"
-
-#define CRYPT_PROTOCOL_PRIVATE
-#include "obfs2.h"
 
 static void obfs2_state_free(void *state);
 static int obfs2_send_initial_message(void *state, struct evbuffer *buf);
@@ -171,13 +168,13 @@ set_up_vtable(void)
   vtable = calloc(1, sizeof(protocol_vtable));
   if (!vtable)
     return -1;
-  
+
   vtable->destroy = obfs2_state_free;
   vtable->create = obfs2_new;
   vtable->handshake = obfs2_send_initial_message;
   vtable->send = obfs2_send;
   vtable->recv = obfs2_recv;
-  
+
   return 1;
 }
 
@@ -230,7 +227,7 @@ derive_key(void *s, const char *keytype)
 /**
    Derive and return padding key of type 'keytype' from the seeds
    currently set in state 's'.  Returns NULL on failure.
-*/   
+*/
 static crypt_t *
 derive_padding_key(void *s, const uchar *seed,
                    const char *keytype)
@@ -269,7 +266,7 @@ derive_padding_key(void *s, const uchar *seed,
 /**
    This is called everytime we get a connection for the obfs2
    protocol.
-   
+
    It sets up the protocol vtable in 'proto_struct' and then attempts
    to create and return a protocol state according to the protocol
    parameters 'params'.
@@ -280,10 +277,10 @@ obfs2_new(struct protocol_t *proto_struct,
 {
   assert(vtable);
   proto_struct->vtable = vtable;
-  
+
   return obfs2_state_new(params);
 }
-  
+
 /**
    Returns an obfs2 state according to the protocol parameters
    'params'. If something goes wrong it returns NULL.
@@ -314,8 +311,8 @@ obfs2_state_new(protocol_params_t *params)
   }
 
   if (params->shared_secret)
-    if (obfs2_state_set_shared_secret(state, 
-                                      params->shared_secret, 
+    if (obfs2_state_set_shared_secret(state,
+                                      params->shared_secret,
                                       params->shared_secret_len)<0)
       return NULL;
 
@@ -329,11 +326,11 @@ obfs2_state_new(protocol_params_t *params)
   return state;
 }
 
-/** 
+/**
     Sets the shared 'secret' to be used, on the protocol state 's'.
 */
 static int
-obfs2_state_set_shared_secret(void *s, const char *secret, 
+obfs2_state_set_shared_secret(void *s, const char *secret,
                               size_t secretlen)
 {
   assert(secret);
@@ -494,7 +491,7 @@ init_crypto(void *s)
 /* Called when we receive data in an evbuffer 'source': deobfuscates that data
  * and writes it to 'dest', by using protocol state 's' to get crypto keys.
  *
- * It returns: 
+ * It returns:
  * RECV_GOOD to say that everything went fine.
  * RECV_BAD to say that something went bad.
  * RECV_INCOMPLETE to say that we need more data to form an opinion.
@@ -549,12 +546,13 @@ obfs2_recv(void *s, struct evbuffer *source,
 
     /* Fall through here: if there is padding data waiting on the buffer, pull
        it off immediately. */
-    log_debug("%s(): Received key, expecting %d bytes of padding", __func__, plength);
+    log_debug("%s(): Received key, expecting %d bytes of padding",
+              __func__, plength);
   }
 
   /* If we have pending data to send, we set the return code
   appropriately so that we call proto_send() right after we get out of
-  here! */  
+  here! */
   if (state->pending_data_to_send)
     r = RECV_SEND_PENDING;
 
@@ -569,7 +567,7 @@ obfs2_recv(void *s, struct evbuffer *source,
       n = evbuffer_get_length(source);
     evbuffer_drain(source, n);
     state->padding_left_to_read -= n;
-    log_debug("%s(): Received %d bytes of padding; %d left to read", 
+    log_debug("%s(): Received %d bytes of padding; %d left to read",
               __func__, n, state->padding_left_to_read);
   }
 
@@ -586,8 +584,8 @@ obfs2_recv(void *s, struct evbuffer *source,
   return r;
 }
 
-/** 
-    Frees obfs2 state 's' 
+/**
+    Frees obfs2 state 's'
 */
 static void
 obfs2_state_free(void *s)
diff --git a/src/protocols/obfs2.h b/src/protocols/obfs2.h
index 608ae7a..a9ebfb1 100644
--- a/src/protocols/obfs2.h
+++ b/src/protocols/obfs2.h
@@ -5,25 +5,22 @@
 #ifndef OBFS2_H
 #define OBFS2_H
 
-#include <sys/types.h>
-
 typedef struct obfs2_state_t obfs2_state_t;
 struct evbuffer;
 struct protocol_t;
 struct protocol_params_t;
 struct listener_t;
 
-#define SHARED_SECRET_LENGTH SHA256_LENGTH
-
 int obfs2_init(int n_options, char **options, struct protocol_params_t *params);
 void *obfs2_new(struct protocol_t *proto_struct,
                 struct protocol_params_t *params);
-int parse_and_set_options(int n_options, char **options, 
+int parse_and_set_options(int n_options, char **options,
                           struct protocol_params_t *params);
 
+#ifdef CRYPT_PROTOCOL_PRIVATE
 
+#include "../crypt.h"
 
-#ifdef CRYPT_PROTOCOL_PRIVATE
 /* ==========
    These definitions are not part of the crypt_protocol interface.
    They're exposed here so that the unit tests can use them.
@@ -44,6 +41,8 @@ int parse_and_set_options(int n_options, char **options,
 #define INITIATOR_SEND_TYPE "Initiator obfuscated data"
 #define RESPONDER_SEND_TYPE "Responder obfuscated data"
 
+#define SHARED_SECRET_LENGTH SHA256_LENGTH
+
 struct obfs2_state_t {
   /** Current protocol state.  We start out waiting for key information.  Then
       we have a key and wait for padding to arrive.  Finally, we are sending
diff --git a/src/socks.c b/src/socks.c
index 7e6f6b2..b403be8 100644
--- a/src/socks.c
+++ b/src/socks.c
@@ -2,31 +2,17 @@
    See LICENSE for other credits and copying information
 */
 
-#include <sys/types.h>
-#ifdef _WIN32
-#include <Winsock2.h>
-#include <Ws2tcpip.h>
-#else
-#include <arpa/inet.h>
-#include <sys/socket.h>
-#endif
+#define SOCKS_PRIVATE
+#include "socks.h"
 
+#include "util.h"
 
 #include <assert.h>
-
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
 #include <errno.h>
-
-#define SOCKS_PRIVATE
-#include "socks.h"
-#include "util.h"
+#include <stdlib.h>
+#include <string.h>
 
 #include <event2/buffer.h>
-#include <event2/event.h>
-#include <event2/bufferevent.h>
-
 
 /**
    General SOCKS5 idea:
@@ -50,9 +36,6 @@
 */
 
 
-static enum socks_ret socks5_do_negotiation(struct evbuffer *dest,
-                                    unsigned int neg_was_success);
-
 typedef unsigned char uchar;
 
 socks_state_t *
@@ -98,7 +81,7 @@ socks_errno_to_reply(socks_state_t *state, int error)
       return SOCKS5_SUCCESS;
     else {
       switch (error) {
-      case ERR(ENETUNREACH): 
+      case ERR(ENETUNREACH):
         return SOCKS5_FAILED_NETUNREACH;
       case ERR(EHOSTUNREACH):
         return SOCKS5_FAILED_HOSTUNREACH;
@@ -106,7 +89,7 @@ socks_errno_to_reply(socks_state_t *state, int error)
         return SOCKS5_FAILED_REFUSED;
       default:
         return SOCKS5_FAILED_GENERAL;
-      }      
+      }
     }
   } else
     return -1;
@@ -117,16 +100,16 @@ socks_errno_to_reply(socks_state_t *state, int error)
 /**
    Takes a command request from 'source', it evaluates it and if it's
    legit it parses it into 'parsereq'.
-   
+
    It returns '1' if everything went fine.
    It returns '0' if we need more data from the client.
    It returns '-1' if we didn't like something.
    It returns '-2' if the client asked for something else than CONNECT.
    If that's the case we should send a reply back to the client
    telling him that we don't support it.
-   
-   Disclaimer: This is just a temporary documentation.  
-   
+
+   Disclaimer: This is just a temporary documentation.
+
    Client Request (Client -> Server)
 */
 enum socks_ret
@@ -216,7 +199,8 @@ socks5_handle_request(struct evbuffer *source, struct parsereq *parsereq)
     char a[16];
     assert(addrlen <= 16);
     memcpy(a, destaddr, addrlen);
-    if (evutil_inet_ntop(af, destaddr, parsereq->addr, sizeof(parsereq->addr)) == NULL)
+    if (evutil_inet_ntop(af, destaddr, parsereq->addr,
+                         sizeof(parsereq->addr)) == NULL)
       goto err;
   }
 
@@ -269,7 +253,8 @@ socks5_send_reply(struct evbuffer *reply_dest, socks_state_t *state,
       p[3] = SOCKS5_ATYP_FQDN;
     } else {
       addrlen = (state->parsereq.af == AF_INET) ? 4 : 16;
-      p[3] = (state->parsereq.af == AF_INET) ? SOCKS5_ATYP_IPV4 : SOCKS5_ATYP_IPV6;
+      p[3] = (state->parsereq.af == AF_INET)
+        ? SOCKS5_ATYP_IPV4 : SOCKS5_ATYP_IPV6;
       evutil_inet_pton(state->parsereq.af, state->parsereq.addr, addr);
     }
     port = htons(state->parsereq.port);
@@ -287,6 +272,31 @@ socks5_send_reply(struct evbuffer *reply_dest, socks_state_t *state,
 }
 
 /**
+   This function sends a method negotiation reply to 'dest'.
+   If 'neg_was_success' is true send a positive response,
+   otherwise send a negative one.
+   It returns -1 if no suitable negotiation methods were found,
+   or if there was an error during replying.
+
+   Method Negotiation Reply (Server -> Client):
+   | version | method selected |
+       1b           1b
+*/
+static enum socks_ret
+socks5_do_negotiation(struct evbuffer *dest, unsigned int neg_was_success)
+{
+  uchar reply[2];
+  reply[0] = SOCKS5_VERSION;
+
+  reply[1] = neg_was_success ? SOCKS5_METHOD_NOAUTH : SOCKS5_METHOD_FAIL;
+
+  if (evbuffer_add(dest, reply, 2) == -1 || !neg_was_success)
+    return SOCKS_BROKEN;
+  else
+    return SOCKS_GOOD;
+}
+
+/**
    This function handles the initial SOCKS5 packet in 'source' sent by
    the client, which negotiates the version and method of SOCKS.  If
    the packet is actually valid, we reply to 'dest'.
@@ -333,31 +343,6 @@ socks5_handle_negotiation(struct evbuffer *source,
   return socks5_do_negotiation(dest,found_noauth);
 }
 
-/**
-   This function sends a method negotiation reply to 'dest'.
-   If 'neg_was_success' is true send a positive response,
-   otherwise send a negative one.
-   It returns -1 if no suitable negotiation methods were found,
-   or if there was an error during replying.
-
-   Method Negotiation Reply (Server -> Client):
-   | version | method selected |
-       1b           1b
-*/
-static enum socks_ret
-socks5_do_negotiation(struct evbuffer *dest, unsigned int neg_was_success)
-{
-  uchar reply[2];
-  reply[0] = SOCKS5_VERSION;
-
-  reply[1] = neg_was_success ? SOCKS5_METHOD_NOAUTH : SOCKS5_METHOD_FAIL;
-
-  if (evbuffer_add(dest, reply, 2) == -1 || !neg_was_success)
-    return SOCKS_BROKEN;
-  else
-    return SOCKS_GOOD;
-}
-
 /* rename to socks4_handle_request or something. */
 enum socks_ret
 socks4_read_request(struct evbuffer *source, socks_state_t *state)
@@ -425,7 +410,8 @@ socks4_read_request(struct evbuffer *source, socks_state_t *state)
   } else {
     struct in_addr in;
     in.s_addr = htonl(ipaddr);
-    if (evutil_inet_ntop(AF_INET, &in, state->parsereq.addr, sizeof(state->parsereq.addr)) == NULL)
+    if (evutil_inet_ntop(AF_INET, &in, state->parsereq.addr,
+                         sizeof(state->parsereq.addr)) == NULL)
       return SOCKS_BROKEN;
   }
 
@@ -520,7 +506,7 @@ handle_socks(struct evbuffer *source, struct evbuffer *dest,
       if (r == SOCKS_GOOD) {
         socks_state->state = ST_HAVE_ADDR;
         return SOCKS_GOOD;
-      } else if (r == SOCKS_INCOMPLETE) 
+      } else if (r == SOCKS_INCOMPLETE)
         return SOCKS_INCOMPLETE;
       else if (r == SOCKS_CMD_NOT_CONNECT) {
         socks_state->broken = 1;
@@ -567,7 +553,8 @@ socks_state_set_address(socks_state_t *state, const struct sockaddr *sa)
   if (sa->sa_family == AF_INET) {
     const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
     port = sin->sin_port;
-    if (evutil_inet_ntop(AF_INET, &sin->sin_addr, state->parsereq.addr, sizeof(state->parsereq.addr)) == NULL)
+    if (evutil_inet_ntop(AF_INET, &sin->sin_addr, state->parsereq.addr,
+                         sizeof(state->parsereq.addr)) == NULL)
       return -1;
   } else if (sa->sa_family == AF_INET6) {
     if (state->version == 4) {
@@ -576,7 +563,8 @@ socks_state_set_address(socks_state_t *state, const struct sockaddr *sa)
     }
     const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
     port = sin6->sin6_port;
-    if (evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, state->parsereq.addr, sizeof(state->parsereq.addr)) == NULL)
+    if (evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, state->parsereq.addr,
+                         sizeof(state->parsereq.addr)) == NULL)
       return -1;
   } else {
     log_debug("Unknown address family %d", sa->sa_family);
diff --git a/src/test/tinytest.c b/src/test/tinytest.c
index 32410fd..e3107b2 100644
--- a/src/test/tinytest.c
+++ b/src/test/tinytest.c
@@ -23,15 +23,18 @@
  * 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>
-#include <assert.h>
 
 #ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #else
-#include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
 #endif
@@ -40,9 +43,6 @@
 #define __attribute__(x)
 #endif
 
-#include "tinytest.h"
-#include "tinytest_macros.h"
-
 #define LONGEST_TEST_NAME 16384
 
 static int in_tinytest_main = 0; /**< true if we're in tinytest_main().*/
diff --git a/src/test/unittest.c b/src/test/unittest.c
index 184a192..fed4bf3 100644
--- a/src/test/unittest.c
+++ b/src/test/unittest.c
@@ -1,7 +1,6 @@
 /* Copyright 2011 Nick Mathewson, George Kadianakis
    See LICENSE for other credits and copying information
 */
-#include <stdlib.h>
 
 #include "tinytest.h"
 #include "../crypt.h"
diff --git a/src/test/unittest_crypt.c b/src/test/unittest_crypt.c
index a65f07b..14c2c41 100644
--- a/src/test/unittest_crypt.c
+++ b/src/test/unittest_crypt.c
@@ -1,22 +1,15 @@
 /* Copyright 2011 Nick Mathewson, George Kadianakis
    See LICENSE for other credits and copying information
 */
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+
 #include "tinytest.h"
 #include "tinytest_macros.h"
 
-#include <openssl/aes.h>
-
+#define CRYPT_PRIVATE
 #include "../crypt.h"
 
-struct crypt_t {
-  AES_KEY key;
-  uchar ivec[AES_BLOCK_SIZE];
-  uchar ecount_buf[AES_BLOCK_SIZE];
-  unsigned int pos;
-};
+#include <stdio.h>
+#include <string.h>
 
 /* Test vectors for sha256 */
 static void
@@ -169,5 +162,3 @@ struct testcase_t crypt_tests[] = {
   T(rng,0),
   END_OF_TESTCASES
 };
-
-
diff --git a/src/test/unittest_obfs2.c b/src/test/unittest_obfs2.c
index ec5cc47..a7c8112 100644
--- a/src/test/unittest_obfs2.c
+++ b/src/test/unittest_obfs2.c
@@ -1,29 +1,28 @@
 /* Copyright 2011 Nick Mathewson, George Kadianakis
    See LICENSE for other credits and copying information
 */
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 
 #include "tinytest.h"
 #include "tinytest_macros.h"
 
-#include <event2/buffer.h>
-#include <openssl/aes.h>
-
-
 #define CRYPT_PROTOCOL_PRIVATE
 #define CRYPT_PRIVATE
+#include "../protocols/obfs2.h"
 #include "../crypt.h"
 #include "../util.h"
 #include "../protocol.h"
-#include "../protocols/obfs2.h"
-#include "../network.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <event2/buffer.h>
+
 
 static void
 test_proto_option_parsing(void *data)
 {
-  protocol_params_t *proto_params = calloc(1, sizeof(protocol_params_t));  
+  protocol_params_t *proto_params = calloc(1, sizeof(protocol_params_t));
   char *options[] = {"obfs2", "--shared-secret=a", "socks", "127.0.0.1:0"};
   int n_options = 4;
 
@@ -34,7 +33,7 @@ test_proto_option_parsing(void *data)
                             proto_params) == 1);
 
   /** two --dest. */
-  char *options2[] = {"obfs2", "--dest=127.0.0.1:5555", "--dest=a", 
+  char *options2[] = {"obfs2", "--dest=127.0.0.1:5555", "--dest=a",
                       "server", "127.0.0.1:5552"};
   n_options = 5;
 
@@ -42,7 +41,7 @@ test_proto_option_parsing(void *data)
                             proto_params) == -1);
 
   /** unknown arg */
-  char *options3[] = {"obfs2", "--gabura=a", 
+  char *options3[] = {"obfs2", "--gabura=a",
                       "server", "127.0.0.1:5552"};
   n_options = 4;
 
@@ -57,7 +56,7 @@ test_proto_option_parsing(void *data)
                             proto_params) == -1);
 
   /** wrong mode  */
-  char *options5[] = {"obfs2", "--dest=1:1", 
+  char *options5[] = {"obfs2", "--dest=1:1",
                       "gladiator", "127.0.0.1:5552"};
   n_options = 4;
 
@@ -65,7 +64,7 @@ test_proto_option_parsing(void *data)
                             proto_params) == -1);
 
   /** stupid listen addr.  */
-  char *options6[] = {"obfs2", "--dest=1:1", 
+  char *options6[] = {"obfs2", "--dest=1:1",
                       "server", "127.0.0.1:a"};
   n_options = 4;
 
@@ -73,7 +72,7 @@ test_proto_option_parsing(void *data)
                             proto_params) == -1);
 
   /** stupid dest addr.  */
-  char *options7[] = {"obfs2", "--dest=1:b", 
+  char *options7[] = {"obfs2", "--dest=1:b",
                       "server", "127.0.0.1:1"};
   n_options = 4;
 
@@ -81,7 +80,7 @@ test_proto_option_parsing(void *data)
                             proto_params) == -1);
 
   /** socks with dest.  */
-  char *options8[] = {"obfs2", "--dest=1:2", 
+  char *options8[] = {"obfs2", "--dest=1:2",
                       "socks", "127.0.0.1:1"};
   n_options = 4;
 
@@ -89,7 +88,7 @@ test_proto_option_parsing(void *data)
                             proto_params) == -1);
 
   /** socks with dest.  */
-  char *options9[] = {"obfs2", "--shared-secret=a", 
+  char *options9[] = {"obfs2", "--shared-secret=a",
                       "server", "127.0.0.1:1"};
   n_options = 4;
 
@@ -111,11 +110,13 @@ test_proto_setup(void *data)
   struct protocol_t *server_proto = NULL;
 
   protocol_params_t *proto_params_client = calloc(1, sizeof(protocol_params_t));
-  char *options_client[] = {"obfs2", "--shared-secret=hahaha", "socks", "127.0.0.1:1800"};
+  char *options_client[] = {"obfs2", "--shared-secret=hahaha",
+                            "socks", "127.0.0.1:1800"};
   int n_options_client = 4;
 
   protocol_params_t *proto_params_serv = calloc(1, sizeof(protocol_params_t));
-  char *options_server[] = {"obfs2", "--shared-secret=hahaha", "--dest=127.0.0.1:1500", 
+  char *options_server[] = {"obfs2", "--shared-secret=hahaha",
+                            "--dest=127.0.0.1:1500",
                            "server", "127.0.0.1:1800"};
   int n_options_server = 5;
 
@@ -151,12 +152,14 @@ test_proto_handshake(void *data)
   struct protocol_t *server_proto = NULL;
 
   protocol_params_t *proto_params_client = calloc(1, sizeof(protocol_params_t));
-  char *options_client[] = {"obfs2", "--shared-secret=hahaha", "socks", "127.0.0.1:1800"};
+  char *options_client[] = {"obfs2", "--shared-secret=hahaha",
+                            "socks", "127.0.0.1:1800"};
   int n_options_client = 4;
 
   protocol_params_t *proto_params_serv = calloc(1, sizeof(protocol_params_t));
-  char *options_server[] = {"obfs2", "--shared-secret=hahaha", "--dest=127.0.0.1:1500", 
-                           "server", "127.0.0.1:1800"};
+  char *options_server[] = {"obfs2", "--shared-secret=hahaha",
+                            "--dest=127.0.0.1:1500",
+                            "server", "127.0.0.1:1800"};
   int n_options_server = 5;
 
   tt_assert(set_up_protocol(n_options_client, options_client,
@@ -178,8 +181,8 @@ test_proto_handshake(void *data)
   /* We create a client handshake message and pass it to output_buffer */
   tt_int_op(0, <=, proto_handshake(client_proto, output_buffer));
 
-  /* We simulate the server receiving and processing the client's handshake message,
-     by using proto_recv() on the output_buffer */
+  /* We simulate the server receiving and processing the client's
+     handshake message, by using proto_recv() on the output_buffer */
   tt_assert(RECV_GOOD == proto_recv(server_proto, output_buffer, dummy_buffer));
 
   /* Now, we create the server's handshake and pass it to output_buffer */
@@ -223,17 +226,19 @@ test_proto_transfer(void *data)
   struct evbuffer *dummy_buffer = NULL;
   output_buffer = evbuffer_new();
   dummy_buffer = evbuffer_new();
-  
+
   struct protocol_t *client_proto = NULL;
   struct protocol_t *server_proto = NULL;
 
   protocol_params_t *proto_params_client = calloc(1, sizeof(protocol_params_t));
-  char *options_client[] = {"obfs2", "--shared-secret=hahaha", "socks", "127.0.0.1:1800"};
+  char *options_client[] = {"obfs2", "--shared-secret=hahaha",
+                            "socks", "127.0.0.1:1800"};
   int n_options_client = 4;
 
   protocol_params_t *proto_params_serv = calloc(1, sizeof(protocol_params_t));
-  char *options_server[] = {"obfs2", "--shared-secret=hahaha", "--dest=127.0.0.1:1500", 
-                           "server", "127.0.0.1:1800"};
+  char *options_server[] = {"obfs2", "--shared-secret=hahaha",
+                            "--dest=127.0.0.1:1500",
+                            "server", "127.0.0.1:1800"};
   int n_options_server = 5;
 
   tt_assert(set_up_protocol(n_options_client, options_client,
@@ -254,11 +259,9 @@ test_proto_transfer(void *data)
 
   /* Handshake */
   tt_int_op(0, <=, proto_handshake(client_proto, output_buffer));
-  tt_assert(RECV_GOOD == proto_recv(server_proto, 
-                                                    output_buffer, dummy_buffer));
+  tt_assert(RECV_GOOD == proto_recv(server_proto, output_buffer, dummy_buffer));
   tt_int_op(0, <=, proto_handshake(server_proto, output_buffer));
-  tt_assert(RECV_GOOD == proto_recv(client_proto, 
-                                                    output_buffer, dummy_buffer));
+  tt_assert(RECV_GOOD == proto_recv(client_proto, output_buffer, dummy_buffer));
   /* End of Handshake */
 
   /* Now let's pass some data around. */
@@ -269,8 +272,7 @@ test_proto_transfer(void *data)
   evbuffer_add(dummy_buffer, msg1, 54);
   proto_send(client_proto, dummy_buffer, output_buffer);
 
-  tt_assert(RECV_GOOD == proto_recv(server_proto, 
-                                                    output_buffer, dummy_buffer));
+  tt_assert(RECV_GOOD == proto_recv(server_proto, output_buffer, dummy_buffer));
 
   n = evbuffer_peek(dummy_buffer, -1, NULL, &v[0], 2);
   tt_int_op(n, !=, -1);
@@ -286,8 +288,7 @@ test_proto_transfer(void *data)
   evbuffer_add(dummy_buffer, msg2, 55);
   tt_int_op(0, <=, proto_send(server_proto, dummy_buffer, output_buffer));
 
-  tt_assert(RECV_GOOD == proto_recv(client_proto, 
-                                                    output_buffer, dummy_buffer));
+  tt_assert(RECV_GOOD == proto_recv(client_proto, output_buffer, dummy_buffer));
 
   n = evbuffer_peek(dummy_buffer, -1, NULL, &v[1], 2);
   tt_int_op(0, ==, strncmp(msg2, v[1].iov_base, 55));
@@ -322,7 +323,7 @@ test_proto_transfer(void *data)
    Afterwards we will verify that they both got the correct keys.
    That's right, this unit test is loco . */
 static void
-test_proto_splitted_handshake(void *data)
+test_proto_split_handshake(void *data)
 {
   obfs2_state_t *client_state = NULL;
   obfs2_state_t *server_state = NULL;
@@ -336,12 +337,14 @@ test_proto_splitted_handshake(void *data)
   struct protocol_t *server_proto = NULL;
 
   protocol_params_t *proto_params_client = calloc(1, sizeof(protocol_params_t));
-  char *options_client[] = {"obfs2", "--shared-secret=hahaha", "socks", "127.0.0.1:1800"};
+  char *options_client[] = {"obfs2", "--shared-secret=hahaha",
+                            "socks", "127.0.0.1:1800"};
   int n_options_client = 4;
 
   protocol_params_t *proto_params_serv = calloc(1, sizeof(protocol_params_t));
-  char *options_server[] = {"obfs2", "--shared-secret=hahaha", "--dest=127.0.0.1:1500", 
-                           "server", "127.0.0.1:1800"};
+  char *options_server[] = {"obfs2", "--shared-secret=hahaha",
+                            "--dest=127.0.0.1:1500",
+                            "server", "127.0.0.1:1800"};
   int n_options_server = 5;
 
   tt_assert(set_up_protocol(n_options_client, options_client,
@@ -382,16 +385,19 @@ test_proto_splitted_handshake(void *data)
   memcpy(msgclient_1, seed1, OBFUSCATE_SEED_LENGTH);
   memcpy(msgclient_1+OBFUSCATE_SEED_LENGTH, &magic, 4);
   memcpy(msgclient_1+OBFUSCATE_SEED_LENGTH+4, &send_plength1, 4);
-  tt_int_op(0, <=, random_bytes(msgclient_1+OBFUSCATE_SEED_LENGTH+8, plength1_msg1));
+  tt_int_op(0, <=, random_bytes(msgclient_1+OBFUSCATE_SEED_LENGTH+8,
+                                plength1_msg1));
 
-  stream_crypt(client_state->send_padding_crypto, msgclient_1+OBFUSCATE_SEED_LENGTH, 8+plength1_msg1);
+  stream_crypt(client_state->send_padding_crypto,
+               msgclient_1+OBFUSCATE_SEED_LENGTH, 8+plength1_msg1);
 
   /* Client sends handshake part 1 */
-  evbuffer_add(output_buffer, msgclient_1, OBFUSCATE_SEED_LENGTH+8+plength1_msg1);
+  evbuffer_add(output_buffer, msgclient_1,
+               OBFUSCATE_SEED_LENGTH+8+plength1_msg1);
 
   /* Server receives handshake part 1 */
   tt_assert(RECV_INCOMPLETE == proto_recv(server_proto,
-                                                          output_buffer, dummy_buffer));
+                                          output_buffer, dummy_buffer));
 
   tt_assert(server_state->state == ST_WAIT_FOR_PADDING);
 
@@ -403,8 +409,7 @@ test_proto_splitted_handshake(void *data)
   evbuffer_add(output_buffer, msgclient_2, plength1_msg2);
 
   /* Server receives handshake part 2 */
-  tt_assert(RECV_GOOD == proto_recv(server_proto, 
-                                                    output_buffer, dummy_buffer));
+  tt_assert(RECV_GOOD == proto_recv(server_proto, output_buffer, dummy_buffer));
 
   tt_assert(server_state->state == ST_OPEN);
 
@@ -427,14 +432,15 @@ test_proto_splitted_handshake(void *data)
   memcpy(msgserver_1+OBFUSCATE_SEED_LENGTH, &magic, 4);
   memcpy(msgserver_1+OBFUSCATE_SEED_LENGTH+4, &send_plength2, 4);
 
-  stream_crypt(server_state->send_padding_crypto, msgserver_1+OBFUSCATE_SEED_LENGTH, 8);
+  stream_crypt(server_state->send_padding_crypto,
+               msgserver_1+OBFUSCATE_SEED_LENGTH, 8);
 
   /* Server sends handshake part 1 */
   evbuffer_add(output_buffer, msgserver_1, OBFUSCATE_SEED_LENGTH+8);
 
   /* Client receives handshake part 1 */
-  tt_assert(RECV_INCOMPLETE == proto_recv(client_proto, 
-                                                    output_buffer, dummy_buffer));
+  tt_assert(RECV_INCOMPLETE == proto_recv(client_proto,
+                                          output_buffer, dummy_buffer));
 
   tt_assert(client_state->state == ST_WAIT_FOR_PADDING);
 
@@ -446,8 +452,7 @@ test_proto_splitted_handshake(void *data)
   evbuffer_add(output_buffer, msgserver_2, plength2);
 
   /* Client receives handshake part 2 */
-  tt_assert(RECV_GOOD == proto_recv(client_proto, 
-                                                    output_buffer, dummy_buffer));
+  tt_assert(RECV_GOOD == proto_recv(client_proto, output_buffer, dummy_buffer));
 
   tt_assert(client_state->state == ST_OPEN);
 
@@ -498,12 +503,14 @@ test_proto_wrong_handshake_magic(void *data)
   struct protocol_t *server_proto = NULL;
 
   protocol_params_t *proto_params_client = calloc(1, sizeof(protocol_params_t));
-  char *options_client[] = {"obfs2", "--shared-secret=hahaha", "socks", "127.0.0.1:1800"};
+  char *options_client[] = {"obfs2", "--shared-secret=hahaha",
+                            "socks", "127.0.0.1:1800"};
   int n_options_client = 4;
 
   protocol_params_t *proto_params_serv = calloc(1, sizeof(protocol_params_t));
-  char *options_server[] = {"obfs2", "--shared-secret=hahaha", "--dest=127.0.0.1:1500", 
-                           "server", "127.0.0.1:1800"};
+  char *options_server[] = {"obfs2", "--shared-secret=hahaha",
+                            "--dest=127.0.0.1:1500",
+                            "server", "127.0.0.1:1800"};
   int n_options_server = 5;
 
   tt_assert(set_up_protocol(n_options_client, options_client,
@@ -543,8 +550,7 @@ test_proto_wrong_handshake_magic(void *data)
 
   evbuffer_add(output_buffer, msg, OBFUSCATE_SEED_LENGTH+8+plength);
 
-  tt_assert(RECV_BAD == proto_recv(server_proto, 
-                                                   output_buffer, dummy_buffer));
+  tt_assert(RECV_BAD == proto_recv(server_proto, output_buffer, dummy_buffer));
 
   tt_assert(server_state->state == ST_WAIT_FOR_KEY);
 
@@ -584,12 +590,14 @@ test_proto_wrong_handshake_plength(void *data)
 
 
   protocol_params_t *proto_params_client = calloc(1, sizeof(protocol_params_t));
-  char *options_client[] = {"obfs2", "--shared-secret=hahaha", "socks", "127.0.0.1:1800"};
+  char *options_client[] = {"obfs2", "--shared-secret=hahaha",
+                            "socks", "127.0.0.1:1800"};
   int n_options_client = 4;
 
   protocol_params_t *proto_params_serv = calloc(1, sizeof(protocol_params_t));
-  char *options_server[] = {"obfs2", "--shared-secret=hahaha", "--dest=127.0.0.1:1500", 
-                           "server", "127.0.0.1:1800"};
+  char *options_server[] = {"obfs2", "--shared-secret=hahaha",
+                            "--dest=127.0.0.1:1500",
+                            "server", "127.0.0.1:1800"};
   int n_options_server = 5;
 
   tt_assert(set_up_protocol(n_options_client, options_client,
@@ -628,8 +636,7 @@ test_proto_wrong_handshake_plength(void *data)
   evbuffer_add(output_buffer, msg, OBFUSCATE_SEED_LENGTH+8+plength);
 
 
-  tt_assert(RECV_BAD == proto_recv(server_proto, 
-                                                   output_buffer, dummy_buffer));
+  tt_assert(RECV_BAD == proto_recv(server_proto, output_buffer, dummy_buffer));
 
   tt_assert(server_state->state == ST_WAIT_FOR_KEY);
 
@@ -658,7 +665,7 @@ struct testcase_t protocol_tests[] = {
   T(setup, 0),
   T(handshake, 0),
   T(transfer, 0),
-  T(splitted_handshake, 0),
+  T(split_handshake, 0),
   T(wrong_handshake_magic, 0),
   T(wrong_handshake_plength, 0),
   END_OF_TESTCASES
diff --git a/src/test/unittest_socks.c b/src/test/unittest_socks.c
index 1719f8e..0841136 100644
--- a/src/test/unittest_socks.c
+++ b/src/test/unittest_socks.c
@@ -1,20 +1,19 @@
 /* Copyright 2011 Nick Mathewson, George Kadianakis
    See LICENSE for other credits and copying information
 */
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 
 #include "tinytest.h"
 #include "tinytest_macros.h"
 
-#include <event2/buffer.h>
-
 #define SOCKS_PRIVATE
 #include "../socks.h"
 #include "../crypt.h"
-#include "../util.h"
-#include "../protocols/obfs2.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#include <event2/buffer.h>
+
 
 /**
    This function tests the negotiation phase of the SOCKS5 protocol.
diff --git a/src/util.c b/src/util.c
index 9ac2260..6f5302b 100644
--- a/src/util.c
+++ b/src/util.c
@@ -2,25 +2,18 @@
    See LICENSE for other credits and copying information
 */
 
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include "util.h"
 
 #include <assert.h>
-#include <limits.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 
-#include "util.h"
-
-#ifdef _WIN32
-#include <Ws2tcpip.h>
-#endif
-
-#include <event2/util.h>
 #include <event2/dns.h>
+#include <event2/util.h>
 
 /** Any size_t larger than this amount is likely to be an underflow. */
 #define SIZE_T_CEILING  (SIZE_MAX/2 - 16)





More information about the tor-commits mailing list