-char (*request_name[]) = {
- "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
- "STATUS", "ERROR", "TERMREQ",
- "PING", "PONG",
-// "ADD_NODE", "DEL_NODE",
- "ADD_SUBNET", "DEL_SUBNET",
- "ADD_EDGE", "DEL_EDGE",
- "KEY_CHANGED", "REQ_KEY", "ANS_KEY",
- "PACKET",
-};
+static void free_past_request(past_request_t *r) {
+ if(r->request)
+ free(r->request);
+
+ free(r);
+}
+
+static struct event past_request_event;
+
+bool seen_request(char *request) {
+ past_request_t *new, p = {NULL};
+
+ p.request = request;
+
+ if(splay_search(past_request_tree, &p)) {
+ ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Already seen request");
+ return true;
+ } else {
+ new = xmalloc(sizeof *new);
+ new->request = xstrdup(request);
+ new->firstseen = time(NULL);
+ splay_insert(past_request_tree, new);
+ event_add(&past_request_event, &(struct timeval){10, 0});
+ return false;
+ }
+}
+
+static void age_past_requests(int fd, short events, void *data) {
+ splay_node_t *node, *next;
+ past_request_t *p;
+ int left = 0, deleted = 0;
+ time_t now = time(NULL);
+
+ for(node = past_request_tree->head; node; node = next) {
+ next = node->next;
+ p = node->data;
+
+ if(p->firstseen + pinginterval <= now)
+ splay_delete_node(past_request_tree, node), deleted++;
+ else
+ left++;
+ }
+
+ if(left || deleted)
+ ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Aging past requests: deleted %d, left %d",
+ deleted, left);
+
+ if(left)
+ event_add(&past_request_event, &(struct timeval){10, 0});
+}
+
+void init_requests(void) {
+ past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
+
+ timeout_set(&past_request_event, age_past_requests, NULL);
+}
+
+void exit_requests(void) {
+ splay_delete_tree(past_request_tree);
+
+ if(timeout_initialized(&past_request_event))
+ event_del(&past_request_event);
+}