Browse Source

WiP

pull/14/head
Max Lv 11 years ago
parent
commit
8b4b7898c6
3 changed files with 48 additions and 55 deletions
  1. 24
      src/cache.c
  2. 72
      src/udprelay.c
  3. 7
      src/udprelay.h

24
src/cache.c

@ -12,7 +12,7 @@
/**
* A cache entry
*/
struct client_cache_entry {
struct cache_entry {
char *key; /**<The key */
void *data; /**<Payload */
UT_hash_handle hh; /**<Hash Handle for uthash */
@ -22,10 +22,10 @@ struct client_cache_entry {
/**
* A cache object
*/
struct client_cache {
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 client_cache_entry *entries; /**<Head pointer for uthash */
struct cache_entry *entries; /**<Head pointer for uthash */
void (*free_cb) (void *element);/**<Callback function to free cache entries */
};
@ -39,10 +39,10 @@ struct client_cache {
@return EINVAL if dst is NULL, ENOMEM if malloc fails, 0 otherwise
*/
int client_cache_create(struct client_cache **dst, const size_t capacity,
int cache_create(struct cache **dst, const size_t capacity,
void (*free_cb) (void *element))
{
struct client_cache *new = NULL;
struct cache *new = NULL;
int rv;
if (!dst)
@ -76,9 +76,9 @@ err_out:
@return EINVAL if cache is NULL, 0 otherwise
*/
int client_cache_delete(struct client_cache *cache, int keep_data)
int cache_delete(struct cache *cache, int keep_data)
{
struct client_cache_entry *entry, *tmp;
struct cache_entry *entry, *tmp;
int rv;
if (!cache)
@ -122,10 +122,10 @@ int client_cache_delete(struct client_cache *cache, int keep_data)
@return EINVAL if cache is NULL, 0 otherwise
*/
int client_cache_lookup(struct client_cache *cache, char *key, void *result)
int cache_lookup(struct cache *cache, char *key, void *result)
{
int rv;
struct client_cache_entry *tmp = NULL;
struct cache_entry *tmp = NULL;
char **dirty_hack = result;
if (!cache || !key || !result)
@ -161,10 +161,10 @@ int client_cache_lookup(struct client_cache *cache, char *key, void *result)
@return EINVAL if cache is NULL, ENOMEM if malloc fails, 0 otherwise
*/
int client_cache_insert(struct client_cache *cache, char *key, void *data)
int cache_insert(struct cache *cache, char *key, void *data)
{
struct client_cache_entry *entry = NULL;
struct client_cache_entry *tmp_entry = NULL;
struct cache_entry *entry = NULL;
struct cache_entry *tmp_entry = NULL;
size_t key_len = 0;
int rv;

72
src/udprelay.c

@ -242,7 +242,7 @@ static void query_resolve_cb(EV_P_ ev_timer *watcher, int revents) {
#endif
struct remote_ctx *remote_ctx = new_remote_ctx(remotefd);
remote_ctx->addr = *rp->ai_addr;
remote_ctx->dst_addr = *rp->ai_addr;
remote_ctx->server_ctx = query_ctx->server_ctx;
ev_io_start(EV_A_ &remote_ctx->io);
@ -379,13 +379,14 @@ void close_and_free_remote(EN_P_ struct remote_ctx *ctx) {
static void server_recv_cb (EV_P_ ev_io *w, int revents) {
struct server_ctx *server_ctx = (struct server_ctx *)w;
struct udprelay_header *header;
struct sockaddr src_addr;
uint8_t *buf = malloc(BUF_SIZE);
int addr_len = sizeof(server->src_addr);
int addr_len = sizeof(src_addr);
int offset = 0;
ssize_t r = recvfrom(server_ctx->fd, buf, BUF_SIZE,
0, &server->src_addr, &addr_len);
0, &src_addr, &addr_len);
if (r == -1) {
// error on recv
@ -447,8 +448,36 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) {
*/
#ifdef UDPRELAY_LOCAL
struct addrinfo hints;
struct addrinfo *result;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_DGRAM; /* We want a UDP socket */
int s = getaddrinfo(server_ctx->remote_host, server_ctx->remote_port,
&hints, &result);
if (s != 0 || result == NULL) {
LOGE("getaddrinfo: %s", gai_strerror(s));
return;
}
// Bind to any port
int remotefd = create_remote_socket(result->ai_family == AF_INET6);
if (remotefd < 0) {
ERROR("udprelay bind() error..");
return
}
setnonblocking(remotefd);
struct remote_ctx *remote_ctx = new_remote_ctx(remotefd);
remote_ctx->src_addr = src_addr;
remote_ctx->dst_addr = *result->ai_addr;
remote_ctx->server_ctx = server_ctx;
ev_io_start(EV_A_ &remote_ctx->io);
struct remote_ctx *remote_ctx = server_ctx->remote_ctx;
freeaddrinfo(result);
if (header->frag) {
LOGE("drop a message since frag is not 0");
@ -460,7 +489,7 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) {
ss_encrypt_all(BLOCK_SIZE, buf, &r, server_ctx->method);
int w = sendto(server_ctx->fd, buf, r, 0, &remote_ctx->addr, sizeof(remote_ctx->addr));
int w = sendto(server_ctx->fd, buf, r, 0, &remote_ctx->dst_addr, sizeof(remote_ctx->dst_addr));
if (w == -1) {
ERROR("udprelay_server_sendto");
@ -564,39 +593,6 @@ int udprelay(const char *server_host, const char *server_port,
ev_io_start(loop, &server_ctx.io);
#ifdef UDPRELAY_LOCAL
//////////////////////////////////////////////////
// Setup remote context
// Bind to any port
int remotefd = create_remote_socket(1);
if (remotefd < 0) {
FATAL("udprelay bind() error..");
}
setnonblocking(remotefd);
struct addrinfo hints;
struct addrinfo *result;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_DGRAM; /* We want a UDP socket */
int s = getaddrinfo(remote_host, remote_port, &hints, &result);
if (s != 0) {
LOGE("getaddrinfo: %s", gai_strerror(s));
return -1;
}
struct remote_ctx *remote_ctx = new_remote_ctx(remotefd);
remote_ctx->addr = *result->ai_addr;
server_ctx->remote_ctx = remote_ctx;
remote_ctx->server_ctx = server_ctx;
ev_io_start(loop, &remote_ctx->io);
freeaddrinfo(result);
#endif
return 0;
}

7
src/udprelay.h

@ -18,10 +18,6 @@ struct server_ctx {
int fd;
int method;
char *iface;
struct sockaddr addr;
#ifdef UDPRELAY_LOCAL
struct remote_ctx *remote_ctx;
#endif
#ifdef UDPRELAY_REMOTE
asyncns_t *asyncns;
#endif
@ -31,6 +27,7 @@ struct server_ctx {
struct query_ctx {
ev_timer resolve_watcher;
asyncns_query_t *query;
struct sockaddr src_addr;
int buf_len;
char *buf; // server send from, remote recv into
struct server_ctx *server_ctx;
@ -42,7 +39,7 @@ struct remote_ctx {
int fd;
int buf_len;
char *buf; // remote send from, server recv into
struct sockaddr addr;
struct sockaddr dst_addr;
struct server_ctx *server_ctx;
#ifdef UDPRELAY_REMOTE
ev_timer watcher;

Loading…
Cancel
Save