diff --git a/directory_service.c b/directory_service.c index fae71877..19f532f8 100644 --- a/directory_service.c +++ b/directory_service.c @@ -38,6 +38,7 @@ struct item{ }; struct item *root=NULL; +static uint8_t allow_duplicates = 1; static struct item *create_item(const char *key){ struct item *ret=calloc(1,sizeof(struct item)); @@ -62,7 +63,7 @@ static struct item *find_item(const char *key){ return NULL; } -static void add_item(char *key, char *value){ +static int add_item(const char *key, const char *value){ struct item *item = root, **last_ptr=&root; while(item){ int c=strcmp(item->key, key); @@ -70,8 +71,9 @@ static void add_item(char *key, char *value){ c=strcmp(item->value, value); if (c==0){ item->expires = gettime_ms()+1200000; - return; - } + return 1; + }else if(!allow_duplicates) + return -1; } if (c<0){ last_ptr = &item->_left; @@ -87,38 +89,47 @@ static void add_item(char *key, char *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); + return 0; } -static void add_record(int mdp_sockfd){ - int ttl; - overlay_mdp_frame mdp; +static int add_record(int mdp_sockfd){ + struct mdp_header header; + uint8_t payload[MDP_MTU]; - if (overlay_mdp_recv(mdp_sockfd, &mdp, MDP_PORT_DIRECTORY, &ttl)) - return; + ssize_t len = mdp_recv(mdp_sockfd, &header, payload, sizeof payload); + if (len==-1) + return WHY_perror("mdp_recv"); - if (mdp.packetTypeAndFlags&MDP_NOCRYPT){ - fprintf(stderr, "Only encrypted packets will be considered for publishing\n"); - return; - } + if (header.flags & (MDP_FLAG_NO_CRYPT|MDP_FLAG_NO_SIGN)) + return WHY("Only encrypted packets will be considered for publishing\n"); // make sure the payload is a NULL terminated string - mdp.out.payload[mdp.out.payload_length]=0; + payload[len]=0; - char *did=(char *)mdp.out.payload; - int i=0; - while(i