2012-09-14 02:20:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Serval Directory Service client
|
|
|
|
|
|
|
|
When servald starts, load the SID, IP (or domain name) & port of a directory server.
|
|
|
|
When an interface comes up with a route to this server, and periodically thereafter,
|
|
|
|
send our SID name and number to the configured server.
|
|
|
|
|
|
|
|
When we perform a lookup, send an additional copy of the request to the directory server.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serval.h"
|
2012-11-07 06:12:45 +00:00
|
|
|
#include "str.h"
|
2012-09-14 02:20:45 +00:00
|
|
|
#include "overlay_address.h"
|
2012-12-04 03:42:28 +00:00
|
|
|
#include "conf.h"
|
2013-10-16 03:00:00 +00:00
|
|
|
#include "keyring.h"
|
2012-09-14 02:20:45 +00:00
|
|
|
|
|
|
|
struct subscriber *directory_service;
|
|
|
|
|
2012-09-18 02:56:30 +00:00
|
|
|
static void directory_update(struct sched_ent *alarm);
|
|
|
|
|
|
|
|
static struct profile_total directory_timing={
|
|
|
|
.name="directory_update",
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sched_ent directory_alarm={
|
|
|
|
.function=directory_update,
|
|
|
|
.stats=&directory_timing,
|
|
|
|
};
|
2012-12-12 03:11:55 +00:00
|
|
|
#define DIRECTORY_UPDATE_INTERVAL 120000
|
2012-09-18 02:56:30 +00:00
|
|
|
|
2012-09-14 02:20:45 +00:00
|
|
|
// send a registration packet
|
2013-10-09 08:24:21 +00:00
|
|
|
static void directory_send(struct subscriber *directory_service, const sid_t *sidp, const char *did, const char *name)
|
|
|
|
{
|
2012-09-14 02:20:45 +00:00
|
|
|
overlay_mdp_frame request;
|
|
|
|
|
|
|
|
memset(&request, 0, sizeof(overlay_mdp_frame));
|
|
|
|
|
2012-09-14 05:12:15 +00:00
|
|
|
request.packetTypeAndFlags = MDP_TX;
|
|
|
|
|
2013-10-09 08:24:21 +00:00
|
|
|
request.out.src.sid = *sidp;
|
2012-09-14 02:20:45 +00:00
|
|
|
request.out.src.port=MDP_PORT_NOREPLY;
|
2012-12-15 23:35:32 +00:00
|
|
|
request.out.queue=OQ_ORDINARY;
|
2012-09-14 02:20:45 +00:00
|
|
|
|
2013-10-09 08:24:21 +00:00
|
|
|
request.out.dst.sid = directory_service->sid;
|
2012-09-14 05:12:15 +00:00
|
|
|
request.out.dst.port=MDP_PORT_DIRECTORY;
|
2012-09-14 02:20:45 +00:00
|
|
|
request.out.payload_length = snprintf((char *)request.out.payload, sizeof(request.out.payload),
|
|
|
|
"%s|%s", did, name);
|
2012-09-18 02:56:30 +00:00
|
|
|
// Used by tests
|
2013-10-09 08:24:21 +00:00
|
|
|
INFOF("Sending directory registration for %s*, %s, %s to %s*",
|
|
|
|
alloca_tohex_sid_t_trunc(*sidp, 14), did, name, alloca_tohex_sid_t_trunc(directory_service->sid, 14));
|
2012-09-14 02:20:45 +00:00
|
|
|
overlay_mdp_dispatch(&request, 0, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// send a registration packet for each unlocked identity
|
|
|
|
static void directory_send_keyring(struct subscriber *directory_service){
|
|
|
|
int cn=0, in=0, kp=0, k2;
|
|
|
|
|
|
|
|
for (; !keyring_sanitise_position(keyring, &cn, &in, &kp); ++kp){
|
|
|
|
keyring_identity *i = keyring->contexts[cn]->identities[in];
|
|
|
|
|
|
|
|
if (i->keypairs[kp]->type == KEYTYPE_CRYPTOBOX){
|
2013-10-09 08:24:21 +00:00
|
|
|
const sid_t *sidp = (const sid_t *) i->keypairs[0]->public_key;
|
2012-09-14 02:20:45 +00:00
|
|
|
|
|
|
|
for(k2=0; k2 < i->keypair_count; k2++){
|
|
|
|
if (i->keypairs[k2]->type==KEYTYPE_DID){
|
2012-09-18 02:56:30 +00:00
|
|
|
const char *unpackedDid = (const char *) i->keypairs[k2]->private_key;
|
|
|
|
const char *name = (const char *) i->keypairs[k2]->public_key;
|
2012-09-14 02:20:45 +00:00
|
|
|
|
2013-10-09 08:24:21 +00:00
|
|
|
directory_send(directory_service, sidp, unpackedDid, name);
|
2012-09-14 02:20:45 +00:00
|
|
|
// send the first DID only
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 05:28:36 +00:00
|
|
|
static int load_directory_config()
|
|
|
|
{
|
2013-10-09 08:24:21 +00:00
|
|
|
if (!directory_service && !is_sid_t_any(config.directory.service)) {
|
2012-12-04 03:42:28 +00:00
|
|
|
directory_service = find_subscriber(config.directory.service.binary, SID_SIZE, 1);
|
2012-09-14 05:12:15 +00:00
|
|
|
if (!directory_service)
|
|
|
|
return WHYF("Failed to create subscriber record");
|
|
|
|
// used by tests
|
2013-10-09 08:24:21 +00:00
|
|
|
INFOF("ADD DIRECTORY SERVICE %s", alloca_tohex_sid_t(directory_service->sid));
|
2012-09-14 05:12:15 +00:00
|
|
|
}
|
|
|
|
// always attempt to reload the address, may depend on DNS resolution
|
2012-09-14 02:20:45 +00:00
|
|
|
return load_subscriber_address(directory_service);
|
|
|
|
}
|
|
|
|
|
2012-09-18 02:56:30 +00:00
|
|
|
static void directory_update(struct sched_ent *alarm){
|
2012-09-14 02:20:45 +00:00
|
|
|
load_directory_config();
|
|
|
|
|
2012-09-14 05:12:15 +00:00
|
|
|
if (directory_service){
|
2013-08-08 05:50:31 +00:00
|
|
|
if (directory_service->reachable & REACHABLE){
|
2012-09-14 05:12:15 +00:00
|
|
|
directory_send_keyring(directory_service);
|
2012-09-18 02:56:30 +00:00
|
|
|
|
2012-11-12 00:08:24 +00:00
|
|
|
unschedule(alarm);
|
2012-09-18 02:56:30 +00:00
|
|
|
alarm->alarm = gettime_ms() + DIRECTORY_UPDATE_INTERVAL;
|
|
|
|
alarm->deadline = alarm->alarm + 10000;
|
|
|
|
schedule(alarm);
|
|
|
|
}else
|
2012-09-14 05:12:15 +00:00
|
|
|
DEBUGF("Directory service is not reachable");
|
2012-09-14 02:20:45 +00:00
|
|
|
}
|
2012-09-18 02:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int directory_service_init(){
|
|
|
|
directory_update(&directory_alarm);
|
2012-09-14 02:20:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-09-18 02:56:30 +00:00
|
|
|
|
|
|
|
// called when we discover a route to the directory service SID
|
|
|
|
int directory_registration(){
|
|
|
|
// give the route & SAS keys a moment to propagate
|
|
|
|
unschedule(&directory_alarm);
|
|
|
|
directory_alarm.alarm = gettime_ms() + 200;
|
|
|
|
directory_alarm.deadline = directory_alarm.alarm + 10000;
|
|
|
|
schedule(&directory_alarm);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|