#include "conf.h"
#include "connection.h"
#include "logger.h"
+#include "meshlink_internal.h"
#include "meta.h"
#include "protocol.h"
#include "utils.h"
/* Jumptable for the request handlers */
-static bool (*request_handlers[])(connection_t *, const char *) = {
+static bool (*request_handlers[])(meshlink_handle_t *, connection_t *, const char *) = {
id_h, NULL, NULL, NULL /* metakey_h, challenge_h, chal_reply_h */, ack_h,
status_h, error_h, termreq_h,
ping_h, pong_h,
"ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET", "CONTROL",
};
-static splay_tree_t *past_request_tree;
-
bool check_id(const char *id) {
if(!id || !*id)
return false;
/* Generic request routines - takes care of logging and error
detection as well */
-bool send_request(connection_t *c, const char *format, ...) {
+bool send_request(meshlink_handle_t *mesh, connection_t *c, const char *format, ...) {
va_list args;
char request[MAXBUFSIZE];
int len;
request[len++] = '\n';
- if(c == everyone) {
- broadcast_meta(NULL, request, len);
+ if(c == mesh->everyone) {
+ broadcast_meta(mesh, NULL, request, len);
return true;
} else
- return send_meta(c, request, len);
+ return send_meta(mesh, c, request, len);
}
-void forward_request(connection_t *from, const char *request) {
+void forward_request(meshlink_handle_t *mesh, connection_t *from, const char *request) {
logger(DEBUG_META, LOG_DEBUG, "Forwarding %s from %s (%s): %s", request_name[atoi(request)], from->name, from->hostname, request);
// Create a temporary newline-terminated copy of the request
char tmp[len + 1];
memcpy(tmp, request, len);
tmp[len] = '\n';
- broadcast_meta(from, tmp, sizeof tmp);
+ broadcast_meta(mesh, from, tmp, sizeof tmp);
}
-bool receive_request(connection_t *c, const char *request) {
- if(c->outgoing && proxytype == PROXY_HTTP && c->allow_request == ID) {
+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)) {
return false;
}
- if(!request_handlers[reqno](c, request)) {
+ if(!request_handlers[reqno](mesh, c, request)) {
/* Something went wrong. Probably scriptkiddies. Terminate. */
logger(DEBUG_ALWAYS, LOG_ERR, "Error while processing %s from %s (%s)", request_name[reqno], c->name, c->hostname);
static void free_past_request(past_request_t *r) {
if(r->request)
- free((char *)r->request);
+ free((void *)r->request);
free(r);
}
-static timeout_t past_request_timeout;
-
-static void age_past_requests(void *data) {
+static void age_past_requests(event_loop_t *loop, void *data) {
+ meshlink_handle_t *mesh = loop->data;
int left = 0, deleted = 0;
- for splay_each(past_request_t, p, past_request_tree) {
- if(p->firstseen + pinginterval <= now.tv_sec)
- splay_delete_node(past_request_tree, node), deleted++;
+ for splay_each(past_request_t, p, mesh->past_request_tree) {
+ if(p->firstseen + mesh->pinginterval <= mesh->loop.now.tv_sec)
+ splay_delete_node(mesh->past_request_tree, node), deleted++;
else
left++;
}
logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
if(left)
- timeout_set(&past_request_timeout, &(struct timeval){10, rand() % 100000});
+ timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timeval){10, rand() % 100000});
}
-bool seen_request(const char *request) {
+bool seen_request(meshlink_handle_t *mesh, const char *request) {
past_request_t *new, p = {NULL};
p.request = request;
- if(splay_search(past_request_tree, &p)) {
+ if(splay_search(mesh->past_request_tree, &p)) {
logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Already seen request");
return true;
} else {
new = xmalloc(sizeof *new);
new->request = xstrdup(request);
- new->firstseen = now.tv_sec;
- splay_insert(past_request_tree, new);
- timeout_add(&past_request_timeout, age_past_requests, NULL, &(struct timeval){10, rand() % 100000});
+ new->firstseen = mesh->loop.now.tv_sec;
+ 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(void) {
- past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
+void init_requests(meshlink_handle_t *mesh) {
+ mesh->past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
}
-void exit_requests(void) {
- splay_delete_tree(past_request_tree);
+void exit_requests(meshlink_handle_t *mesh) {
+ if(mesh->past_request_tree)
+ splay_delete_tree(mesh->past_request_tree);
+ mesh->past_request_tree = NULL;
- timeout_del(&past_request_timeout);
+ timeout_del(&mesh->loop, &mesh->past_request_timeout);
}