Send DNA lookup requests to configured directory service

This commit is contained in:
Jeremy Lakeman 2012-09-14 14:42:15 +09:30
parent 456bf3fdca
commit f845a18312
3 changed files with 45 additions and 22 deletions

View File

@ -461,6 +461,7 @@ int app_dna_lookup(int argc, const char *const *argv, struct command_line_option
{
if ((last_tx+interval)<now)
{
/* Send a broadcast packet, flooding across the local mesh network */
mdp.packetTypeAndFlags=MDP_TX|MDP_NOCRYPT;
/* set source address to a local address, and pick a random port */
@ -476,6 +477,18 @@ int app_dna_lookup(int argc, const char *const *argv, struct command_line_option
mdp.out.payload_length=strlen(did)+1;
overlay_mdp_send(&mdp,0,0);
/* Also send an encrypted unicast request to a configured directory service */
const char *directory_service = confValueGet("directory.service", NULL);
if (directory_service){
if (stowSid(mdp.out.dst.sid, 0, directory_service)==-1){
WHYF("Invalid directory server SID %s", directory_service);
}else{
mdp.packetTypeAndFlags=MDP_TX;
overlay_mdp_send(&mdp,0,0);
}
}
last_tx=now;
interval+=interval;
}

View File

@ -15,8 +15,6 @@
#include "serval.h"
#include "overlay_address.h"
#define MDP_DIRECTORY 999
struct subscriber *directory_service;
// send a registration packet
@ -25,11 +23,13 @@ static void directory_send(struct subscriber *directory_service, const unsigned
memset(&request, 0, sizeof(overlay_mdp_frame));
request.packetTypeAndFlags = MDP_TX;
bcopy(sid, request.out.src.sid, SID_SIZE);
request.out.src.port=MDP_PORT_NOREPLY;
bcopy(request.out.dst.sid, directory_service->sid, SID_SIZE);
request.out.dst.port=MDP_DIRECTORY;
bcopy(directory_service->sid, request.out.dst.sid, SID_SIZE);
request.out.dst.port=MDP_PORT_DIRECTORY;
request.out.payload_length = snprintf((char *)request.out.payload, sizeof(request.out.payload),
"%s|%s", did, name);
@ -61,18 +61,23 @@ static void directory_send_keyring(struct subscriber *directory_service){
}
static int load_directory_config(){
const char *sid_hex = confValueGet("directory.service", NULL);
if (!sid_hex)
return 0;
unsigned char sid[SID_SIZE];
if (stowSid(sid, 0, sid_hex)==-1)
return WHYF("Invalid directory server SID %s", sid_hex);
directory_service = find_subscriber(sid, SID_SIZE, 1);
if (!directory_service)
return WHYF("Failed to create subscriber record");
if (!directory_service){
const char *sid_hex = confValueGet("directory.service", NULL);
if (!sid_hex)
return 0;
unsigned char sid[SID_SIZE];
if (stowSid(sid, 0, sid_hex)==-1)
return WHYF("Invalid directory server SID %s", sid_hex);
directory_service = find_subscriber(sid, SID_SIZE, 1);
if (!directory_service)
return WHYF("Failed to create subscriber record");
// used by tests
INFOF("ADD DIRECTORY SERVICE %s", alloca_tohex_sid(directory_service->sid));
}
// always attempt to reload the address, may depend on DNS resolution
return load_subscriber_address(directory_service);
}
@ -80,9 +85,11 @@ int directory_interface_up(overlay_interface *interface){
// reload config, now that an interface is up
load_directory_config();
if (directory_service && subscriber_is_reachable(directory_service) != REACHABLE_NONE){
directory_send_keyring(directory_service);
if (directory_service){
if (subscriber_is_reachable(directory_service) != REACHABLE_NONE)
directory_send_keyring(directory_service);
else
DEBUGF("Directory service is not reachable");
}
return 0;
}

View File

@ -8,19 +8,19 @@
char last_add[256]="dummy";
void store(char *key, char *value){
fprintf(stderr,"Adding; \"%s\" = \"%s\"\n", key, value);
// used by tests
INFOF("PUBLISHED \"%s\" = \"%s\"", key, value);
strncpy(value, last_add, sizeof(last_add));
last_add[255]=0;
}
const char *retrieve(char *key){
fprintf(stderr, "Looking for; \"%s\"\n", key);
INFOF("RESOLVING \"%s\"", key);
// dummy code, just reply with the last record we've heard
return last_add;
}
void add_record(){
int ttl;
overlay_mdp_frame mdp;
@ -28,6 +28,9 @@ void add_record(){
if (!overlay_mdp_recv(&mdp, &ttl))
return;
if (mdp.packetTypeAndFlags|=MDP_NOCRYPT)
return WHY("Only encrypted packets will be considered for publishing");
// make sure the payload is a NULL terminated string
mdp.in.payload[mdp.in.payload_length]=0;