From 2eac55562beec060729975a7f7cca52ff9fc866c Mon Sep 17 00:00:00 2001 From: Max Lv Date: Sun, 5 Jan 2014 15:06:24 +0800 Subject: [PATCH] clean up, remove multiple warnings --- src/cache.c | 5 +++-- src/include.h | 13 +++++++++++++ src/jconf.h | 4 ++-- src/local.c | 2 +- src/local.h | 2 ++ src/server.c | 26 ++++++++++++++------------ src/server.h | 2 ++ src/udprelay.c | 30 ++++++++++++++---------------- src/udprelay.h | 23 +++++++---------------- 9 files changed, 58 insertions(+), 49 deletions(-) create mode 100644 src/include.h diff --git a/src/cache.c b/src/cache.c index abe878db..a73b6a01 100644 --- a/src/cache.c +++ b/src/cache.c @@ -95,6 +95,9 @@ int cache_remove(struct cache *cache, char *key) if (tmp) { HASH_DEL(cache->entries, tmp); + if (cache->free_cb) + cache->free_cb(tmp->data); + free(tmp); } return 0; @@ -119,7 +122,6 @@ int cache_remove(struct cache *cache, char *key) */ int cache_lookup(struct cache *cache, char *key, void *result) { - int rv; struct cache_entry *tmp = NULL; char **dirty_hack = result; @@ -160,7 +162,6 @@ int cache_insert(struct cache *cache, char *key, void *data) struct cache_entry *entry = NULL; struct cache_entry *tmp_entry = NULL; size_t key_len = 0; - int rv; if (!cache || !data) return EINVAL; diff --git a/src/include.h b/src/include.h new file mode 100644 index 00000000..51387efd --- /dev/null +++ b/src/include.h @@ -0,0 +1,13 @@ +#ifndef _INCLUDE_H +#define _INCLUDE_H + +int udprelay_init(const char *server_host, const char *server_port, +#ifdef UDPRELAY_LOCAL + const char *remote_host, const char *remote_port, +#endif +#ifdef UDPRELAY_REMOTE + asyncns_t *asyncns, +#endif + int method, int timeout, const char *interface_name); + +#endif // _INCLUDE_H diff --git a/src/jconf.h b/src/jconf.h index 2a9e9bba..b687bccc 100644 --- a/src/jconf.h +++ b/src/jconf.h @@ -8,8 +8,8 @@ typedef struct { - char *host; - char *port; + const char *host; + const char *port; } remote_addr_t; typedef struct diff --git a/src/local.c b/src/local.c index b56b0f93..2b4f2694 100644 --- a/src/local.c +++ b/src/local.c @@ -953,7 +953,7 @@ int main (int argc, char **argv) if (udprelay) { LOGD("udprelay enabled."); - udprelay_init(local_addr, local_port, remote_addr[0].host, remote_addr[0].port, m, iface); + udprelay_init(local_addr, local_port, remote_addr[0].host, remote_addr[0].port, m, listen_ctx.timeout, iface); } ev_run (loop, 0); diff --git a/src/local.h b/src/local.h index e4766d57..f67f2852 100644 --- a/src/local.h +++ b/src/local.h @@ -5,6 +5,8 @@ #include "encrypt.h" #include "jconf.h" +#include "include.h" + struct listen_ctx { ev_io io; diff --git a/src/server.c b/src/server.c index 0d8e171f..a4ea49f2 100644 --- a/src/server.c +++ b/src/server.c @@ -906,8 +906,8 @@ int main (int argc, char **argv) char *iface = NULL; int server_num = 0; - char *server_host[MAX_REMOTE_NUM]; - char *server_port = NULL; + const char *server_host[MAX_REMOTE_NUM]; + const char *server_port = NULL; int dns_thread_num = DNS_THREAD_NUM; @@ -1011,6 +1011,9 @@ int main (int argc, char **argv) // inilitialize ev loop struct ev_loop *loop = EV_DEFAULT; + // inilitialize listen context + struct listen_ctx listen_ctx; + // bind to each interface while (server_num > 0) { @@ -1032,22 +1035,21 @@ int main (int argc, char **argv) LOGD("server listening at port %s.", server_port); // Setup proxy context - struct listen_ctx *listen_ctx = malloc(sizeof(struct listen_ctx)); - listen_ctx->timeout = atoi(timeout); - listen_ctx->asyncns = asyncns; - listen_ctx->fd = listenfd; - listen_ctx->method = m; - listen_ctx->iface = iface; - - ev_io_init (&listen_ctx->io, accept_cb, listenfd, EV_READ); - ev_io_start (loop, &listen_ctx->io); + listen_ctx.timeout = atoi(timeout); + listen_ctx.asyncns = asyncns; + listen_ctx.fd = listenfd; + listen_ctx.method = m; + listen_ctx.iface = iface; + + ev_io_init (&listen_ctx.io, accept_cb, listenfd, EV_READ); + ev_io_start (loop, &listen_ctx.io); } // Setup UDP if (udprelay) { LOGD("udprelay enabled."); - udprelay_init(server_host[0], server_port, asyncns, m, iface); + udprelay_init(server_host[0], server_port, asyncns, m, listen_ctx.timeout, iface); } // start ev loop diff --git a/src/server.h b/src/server.h index 4f391942..b0d6287b 100644 --- a/src/server.h +++ b/src/server.h @@ -8,6 +8,8 @@ #include "jconf.h" #include "asyncns.h" +#include "include.h" + struct listen_ctx { ev_io io; diff --git a/src/udprelay.c b/src/udprelay.c index d3f0a889..d6b17eb7 100644 --- a/src/udprelay.c +++ b/src/udprelay.c @@ -52,9 +52,7 @@ #define BUF_SIZE MAX_UDP_PACKET_SIZE extern int verbose; -static char *iface; -static int remote_conn = 0; -static int server_conn = 0; +static const char *iface; #ifndef __MINGW32__ static int setnonblocking(int fd) @@ -156,7 +154,7 @@ static int parse_udprealy_header(const char* buf, const int buf_len, char *host, int create_remote_socket(int ipv6) { - int s, remote_sock; + int remote_sock; if (ipv6) { @@ -252,13 +250,14 @@ int create_server_socket(const char *host, const char *port) return server_sock; } -struct remote_ctx *new_remote_ctx(int fd) +struct remote_ctx *new_remote(int fd, struct server_ctx *server_ctx) { struct remote_ctx *ctx = malloc(sizeof(struct remote_ctx)); memset(ctx, 0, sizeof(struct remote_ctx)); - ctx->server_ctx = NULL; ctx->fd = fd; + ctx->server_ctx = server_ctx; ev_io_init(&ctx->io, remote_recv_cb, fd, EV_READ); + ev_timer_init(&ctx->watcher, remote_timeout_cb, server_ctx->timeout, server_ctx->timeout * 5); return ctx; } @@ -273,7 +272,7 @@ struct server_ctx * new_server_ctx(int fd) #ifdef UDPRELAY_REMOTE struct query_ctx *new_query_ctx(asyncns_query_t *query, - const uint8_t *buf, const int buf_len) + const char *buf, const int buf_len) { struct query_ctx *ctx = malloc(sizeof(struct query_ctx)); memset(ctx, 0, sizeof(struct query_ctx)); @@ -324,7 +323,6 @@ static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents) char *key = hash_key(remote_ctx->addr_header, remote_ctx->addr_header_len, &remote_ctx->src_addr); cache_remove(remote_ctx->server_ctx->conn_cache, key); - close_and_free_remote(EV_A_ remote_ctx); } #ifdef UDPRELAY_REMOTE @@ -394,7 +392,7 @@ static void query_resolve_cb(EV_P_ ev_timer *watcher, int revents) if (iface) setinterface(remotefd, iface); #endif - struct remote_ctx *remote_ctx = new_remote_ctx(remotefd); + struct remote_ctx *remote_ctx = new_remote(remotefd, query_ctx->server_ctx); remote_ctx->src_addr = query_ctx->src_addr; remote_ctx->dst_addr = *rp->ai_addr; remote_ctx->server_ctx = query_ctx->server_ctx; @@ -451,8 +449,8 @@ static void remote_recv_cb (EV_P_ ev_io *w, int revents) ev_timer_again(EV_A_ &remote_ctx->watcher); struct sockaddr src_addr; - int addr_len = sizeof(src_addr); - int addr_header_len = remote_ctx->addr_header_len; + unsigned int addr_len = sizeof(src_addr); + unsigned int addr_header_len = remote_ctx->addr_header_len; char *buf = malloc(BUF_SIZE); // recv @@ -518,8 +516,8 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) struct sockaddr src_addr; char *buf = malloc(BUF_SIZE); - int addr_len = sizeof(src_addr); - int offset = 0; + unsigned int addr_len = sizeof(src_addr); + unsigned int offset = 0; ssize_t buf_len = recvfrom(server_ctx->fd, buf, BUF_SIZE, 0, &src_addr, &addr_len); @@ -656,10 +654,9 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) setnonblocking(remotefd); // Init remote_ctx - remote_ctx = new_remote_ctx(remotefd); + remote_ctx = new_remote(remotefd, server_ctx); remote_ctx->src_addr = src_addr; remote_ctx->dst_addr = *result->ai_addr; - remote_ctx->server_ctx = server_ctx; remote_ctx->addr_header_len = addr_header_len; memcpy(remote_ctx->addr_header, addr_header, addr_header_len); @@ -752,7 +749,7 @@ int udprelay_init(const char *server_host, const char *server_port, #ifdef UDPRELAY_REMOTE asyncns_t *asyncns, #endif - int method, const char *interface_name) + int method, int timeout, const char *interface_name) { @@ -777,6 +774,7 @@ int udprelay_init(const char *server_host, const char *server_port, setnonblocking(serverfd); struct server_ctx *server_ctx = new_server_ctx(serverfd); + server_ctx->timeout = timeout; server_ctx->method = method; server_ctx->iface = iface; server_ctx->conn_cache = conn_cache; diff --git a/src/udprelay.h b/src/udprelay.h index 1de178c1..c20e6bb5 100644 --- a/src/udprelay.h +++ b/src/udprelay.h @@ -13,6 +13,8 @@ #include "cache.h" +#include "include.h" + #define MAX_UDP_PACKET_SIZE (64 * 1024) struct server_ctx @@ -20,15 +22,16 @@ struct server_ctx ev_io io; int fd; int method; - char *iface; + int timeout; + const char *iface; struct cache *conn_cache; char *buf; // server send from, remote recv into #ifdef UDPRELAY_REMOTE asyncns_t *asyncns; #endif #ifdef UDPRELAY_LOCAL - char *remote_host; - char *remote_port; + const char *remote_host; + const char *remote_port; #endif }; @@ -60,25 +63,13 @@ struct remote_ctx static void server_recv_cb (EV_P_ ev_io *w, int revents); static void remote_recv_cb (EV_P_ ev_io *w, int revents); -static void server_resolve_cb(EV_P_ ev_timer *watcher, int revents); static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents); static char *hash_key(const char *header, const int header_len, const struct sockaddr *addr); #ifdef UDPRELAY_REMOTE static void query_resolve_cb(EV_P_ ev_timer *watcher, int revents); #endif static void close_and_free_remote(EV_P_ struct remote_ctx *ctx); -static void close_and_free_server(EV_P_ struct server_ctx *server_ctx); - -struct remote_ctx* new_remote_ctx(int fd); -struct server_ctx* new_server(int fd); -int udprelay(const char *server_host, const char *server_port, -#ifdef UDPRELAY_LOCAL - const char *remote_host, const char *remote_port, -#endif -#ifdef UDPRELAY_REMOTE - asyncns_t *asyncns, -#endif - int method, const char *interface_name); +static struct remote_ctx* new_remote(int fd, struct server_ctx* server_ctx); #endif // _UDPRELAY_H