X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fprotocol.c;h=e2842abe5c7665ab341b1af86213c3f4f981b04b;hb=4b6c01b1d5383b1a7417244a31ad4652aab2d5db;hp=fd180f4faafab84fd4c3bfedf0bf465dbf96b13c;hpb=b1cae6a2011f704dc4d3b02972def561d5c6bcb9;p=meshlink diff --git a/src/protocol.c b/src/protocol.c index fd180f4f..e2842abe 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -42,7 +42,7 @@ static bool (*request_handlers[])(meshlink_handle_t *, connection_t *, const cha /* Request names */ -static char (*request_name[]) = { +static const char *request_name[] = { "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK", "STATUS", "ERROR", "TERMREQ", "PING", "PONG", @@ -66,15 +66,15 @@ bool check_id(const char *id) { /* Generic request routines - takes care of logging and error detection as well */ -bool send_request(meshlink_handle_t *mesh, connection_t *c, submesh_t *s, const char *format, ...) { +bool send_request(meshlink_handle_t *mesh, connection_t *c, const submesh_t *s, const char *format, ...) { + assert(c); + assert(format); + assert(*format); + va_list args; char request[MAXBUFSIZE]; int len; - if(!c) { - return false; - } - /* Use vsnprintf instead of vxasprintf: faster, no memory fragmentation, cleanup is automatic, and there is a limit on the input buffer anyway */ @@ -106,7 +106,11 @@ bool send_request(meshlink_handle_t *mesh, connection_t *c, submesh_t *s, const } } -void forward_request(meshlink_handle_t *mesh, connection_t *from, submesh_t *s, const char *request) { +void forward_request(meshlink_handle_t *mesh, connection_t *from, const submesh_t *s, const char *request) { + assert(from); + assert(request); + assert(*request); + logger(mesh, MESHLINK_DEBUG, "Forwarding %s from %s: %s", request_name[atoi(request)], from->name, request); // Create a temporary newline-terminated copy of the request @@ -124,21 +128,7 @@ void forward_request(meshlink_handle_t *mesh, connection_t *from, submesh_t *s, } bool receive_request(meshlink_handle_t *mesh, connection_t *c, const char *request) { - if(c->outgoing && mesh->proxytype == PROXY_HTTP && c->allow_request == ID) { - if(!request[0] || request[0] == '\r') { - return true; - } - - if(!strncasecmp(request, "HTTP/1.1 ", 9)) { - if(!strncmp(request + 9, "200", 3)) { - logger(mesh, MESHLINK_DEBUG, "Proxy request granted"); - return true; - } else { - logger(mesh, MESHLINK_DEBUG, "Proxy request rejected: %s", request + 9); - return false; - } - } - } + assert(request); int reqno = atoi(request); @@ -190,7 +180,7 @@ static void age_past_requests(event_loop_t *loop, void *data) { for splay_each(past_request_t, p, mesh->past_request_tree) { if(p->firstseen + request_timeout <= mesh->loop.now.tv_sec) { - splay_delete_node(mesh->past_request_tree, node), deleted++; + splay_delete_node(mesh->past_request_tree, splay_node), deleted++; } else { left++; } @@ -200,13 +190,17 @@ static void age_past_requests(event_loop_t *loop, void *data) { logger(mesh, MESHLINK_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left); } - if(left) - timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timeval) { - 10, rand() % 100000 - }); + if(left) { + timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timespec) { + 10, prng(mesh, TIMER_FUDGE) + }); + } } bool seen_request(meshlink_handle_t *mesh, const char *request) { + assert(request); + assert(*request); + past_request_t *new, p = {.request = request}; if(splay_search(mesh->past_request_tree, &p)) { @@ -216,16 +210,25 @@ bool seen_request(meshlink_handle_t *mesh, const char *request) { new = xmalloc(sizeof(*new)); new->request = xstrdup(request); new->firstseen = mesh->loop.now.tv_sec; + + if(!mesh->past_request_tree->head) { + timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timespec) { + 10, prng(mesh, TIMER_FUDGE) + }); + } + splay_insert(mesh->past_request_tree, new); - timeout_add(&mesh->loop, &mesh->past_request_timeout, age_past_requests, NULL, &(struct timeval) { - 10, rand() % 100000 - }); return false; } } void init_requests(meshlink_handle_t *mesh) { + assert(!mesh->past_request_tree); + mesh->past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request); + timeout_add(&mesh->loop, &mesh->past_request_timeout, age_past_requests, NULL, &(struct timespec) { + 0, 0 + }); } void exit_requests(meshlink_handle_t *mesh) {