diff --git a/src/cache.c b/src/cache.c index ebc279a5..3aa0ffb6 100644 --- a/src/cache.c +++ b/src/cache.c @@ -1,4 +1,4 @@ -/* +/* * Original Author: Oliver Lorenz (ol), olli@olorenz.org, https://olorenz.org * License: This is licensed under the same terms as uthash itself */ @@ -9,26 +9,6 @@ #include "cache.h" #include "uthash.h" -/** - * A cache entry - */ -struct cache_entry { - char *key; /** + #include "utils.h" #include "udprelay.h" #include "cache.h" @@ -84,14 +86,14 @@ int setinterface(int socket_fd, const char* interface_name) #endif static char *hash_key(const char *header, const int header_len, const sockaddr *addr) { - static char key[384]; + char key[384]; // calculate hash key memset(key, 0, 384); memcpy(key, addr.sa_data, 14); memcpy(key + 14, header, header_len); - return key; + return (char*) MD5((const uint8_t *)key, 14 + header_len, NULL); } static int parse_udprealy_header(const char* buf, const int buf_len, @@ -519,6 +521,8 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) { buf_len - offset, udprelay_header->atyp, host, port); char *addr_header = buf + offset - sizeof(udprelay_header->atyp); char *key = hash_key(addr_header, addr_header_len, &src_addr); + struct *conn_cache = server_ctx->conn_cache; + #ifdef UDPRELAY_LOCAL @@ -613,9 +617,13 @@ int udprelay(const char *server_host, const char *server_port, #endif int method, const char *iface) { - // inilitialize ev loop + // Inilitialize ev loop struct ev_loop *loop = EV_DEFAULT; + // Inilitialize cache + struct cache *conn_cache; + cache_create(&conn_cache, MAX_UDP_CONN_NUM, free_cb); + ////////////////////////////////////////////////// // Setup server context @@ -630,6 +638,7 @@ int udprelay(const char *server_host, const char *server_port, server_ctx->method = method; server_ctx->iface = iface; server_ctx->asyncns = asyncns; + server_ctx->conn_cache = conn_cache; ev_io_start(loop, &server_ctx.io); diff --git a/src/udprelay.h b/src/udprelay.h index f727dd9c..a1b0e592 100644 --- a/src/udprelay.h +++ b/src/udprelay.h @@ -11,6 +11,8 @@ #include "asyncns.h" #endif +#include "cache.h" + #define MAX_UDP_PACKET_SIZE (64 * 1024) struct server_ctx { @@ -18,6 +20,7 @@ struct server_ctx { int fd; int method; char *iface; + struct cache *conn_cache; #ifdef UDPRELAY_REMOTE asyncns_t *asyncns; #endif