-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 timeout_t past_request_timeout;
+
+static void age_past_requests(void *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++;
+ else
+ left++;
+ }
+
+ if(left || deleted)
+ 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});
+}
+
+bool seen_request(const char *request) {
+ past_request_t *new, p = {NULL};
+
+ p.request = request;
+
+ if(splay_search(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});
+ 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 exit_requests(void) {
+ splay_delete_tree(past_request_tree);
+
+ timeout_del(&past_request_timeout);
+}