#include "constants.h" #include "mdp_client.h" #include #include #include struct item{ // simple tree structure struct item *_left; struct item *_right; char key[32]; char value[128]; time_ms_t expires; }; struct item *root; static struct item *create_item(const char *key){ struct item *ret=calloc(1,sizeof(struct item)); strncpy(ret->key,key,sizeof(ret->key)); ret->key[sizeof(ret->key) -1]=0; return ret; } static struct item *find_item(const char *key, int create){ if (!root){ if (create) root=create_item(key); return root; } struct item *item = root; while(item){ int c=strcmp(item->key, key); if (c==0) return item; if (c<0){ if (!item->_left){ if (create) item->_left=create_item(key); return item->_left; } item = item->_left; }else{ if (!item->_right){ if (create) item->_right=create_item(key); return item->_right; } item = item->_right; } } return NULL; } static void store(char *key, char *value){ struct item *item = find_item(key, 1); strncpy(item->value,value,sizeof(item->value)); item->value[sizeof(item->value) -1]=0; // expire after 20 minutes item->expires = gettime_ms()+1200000; // used by tests fprintf(stderr, "PUBLISHED \"%s\" = \"%s\"\n", key, value); } static const char *retrieve(char *key){ struct item *item = find_item(key, 0); if (item && item->expires > gettime_ms()){ return item->value; } return NULL; } static void add_record(){ int ttl; overlay_mdp_frame mdp; if (overlay_mdp_recv(&mdp, &ttl)) return; if (mdp.packetTypeAndFlags&MDP_NOCRYPT){ fprintf(stderr, "Only encrypted packets will be considered for publishing\n"); return; } // make sure the payload is a NULL terminated string mdp.in.payload[mdp.in.payload_length]=0; char *did=(char *)mdp.in.payload; int i=0; while(i0){ if (fds[0].revents & POLLIN) resolve_request(); if (fds[1].revents & POLLIN) add_record(); if (fds[0].revents & (POLLHUP | POLLERR)) break; } } overlay_mdp_client_done(); return 0; }