Browse Source

WiP

pull/14/head
Max Lv 11 years ago
parent
commit
a9ae518968
5 changed files with 42 additions and 29 deletions
  1. 22
      src/cache.c
  2. 30
      src/cache.h
  3. 1
      src/jconf.h
  4. 15
      src/udprelay.c
  5. 3
      src/udprelay.h

22
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; /**<The key */
void *data; /**<Payload */
UT_hash_handle hh; /**<Hash Handle for uthash */
};
#define KEY_MAX_LENGTH 32
/**
* A cache object
*/
struct cache {
size_t max_entries; /**<Amount of entries this cache object can hold */
pthread_rwlock_t cache_lock; /**<A lock for concurrent access */
struct cache_entry *entries; /**<Head pointer for uthash */
void (*free_cb) (void *element);/**<Callback function to free cache entries */
};
/** Creates a new cache object
@param dst

30
src/cache.h

@ -6,12 +6,32 @@
#ifndef _CACHE_
#define _CACHE_
struct client_cache;
#define KEY_MAX_LENGTH 32
extern int client_cache_create(struct foo_cache **dst, const size_t capacity,
/**
* A cache entry
*/
struct cache_entry {
char *key; /**<The key */
void *data; /**<Payload */
UT_hash_handle hh; /**<Hash Handle for uthash */
};
/**
* A cache object
*/
struct cache {
size_t max_entries; /**<Amount of entries this cache object can hold */
pthread_rwlock_t cache_lock; /**<A lock for concurrent access */
struct cache_entry *entries; /**<Head pointer for uthash */
void (*free_cb) (void *element);/**<Callback function to free cache entries */
};
extern int cache_create(struct cache **dst, const size_t capacity,
void (*free_cb) (void *element));
extern int client_cache_delete(struct foo_cache *cache, int keep_data);
extern int client_cache_lookup(struct foo_cache *cache, char *key, void *result);
extern int client_cache_insert(struct foo_cache *cache, char *key, void *data);
extern int cache_delete(struct cache *cache, int keep_data);
extern int cache_lookup(struct cache *cache, char *key, void *result);
extern int cache_insert(struct cache *cache, char *key, void *data);
#endif

1
src/jconf.h

@ -4,6 +4,7 @@
#define MAX_REMOTE_NUM 10
#define MAX_CONF_SIZE 16 * 1024
#define DNS_THREAD_NUM 4
#define MAX_UDP_CONN_NUM 4096
typedef struct {
int remote_num;

15
src/udprelay.c

@ -24,6 +24,8 @@
#define SET_INTERFACE
#endif
#include <openssl/md5.h>
#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);

3
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

Loading…
Cancel
Save