2013-11-21 02:12:59 +00:00
|
|
|
/*
|
|
|
|
Serval DNA directory service client
|
2013-12-04 06:26:55 +00:00
|
|
|
Copyright (C) 2013 Serval Project Inc.
|
2013-11-21 02:12:59 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-05-23 08:19:00 +00:00
|
|
|
#include "serval_types.h"
|
|
|
|
#include "cli.h"
|
2012-11-07 06:12:45 +00:00
|
|
|
#include "str.h"
|
2012-09-14 02:20:45 +00:00
|
|
|
#include "overlay_address.h"
|
2014-02-05 04:56:56 +00:00
|
|
|
#include "overlay_packet.h"
|
|
|
|
#include "overlay_buffer.h"
|
2012-12-04 03:42:28 +00:00
|
|
|
#include "conf.h"
|
2013-10-16 03:00:00 +00:00
|
|
|
#include "keyring.h"
|
2014-05-23 08:19:00 +00:00
|
|
|
#include "serval.h" // for overlay_send_frame()
|
2012-09-14 02:20:45 +00:00
|
|
|
|
2015-05-25 02:16:37 +00:00
|
|
|
__thread struct subscriber *directory_service;
|
2012-09-14 02:20:45 +00:00
|
|
|
|
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
|
2014-02-05 04:56:56 +00:00
|
|
|
static void directory_send(struct subscriber *directory_service, struct subscriber *source, const char *did, const char *name)
|
2013-10-09 08:24:21 +00:00
|
|
|
{
|
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*",
|
2014-02-05 04:56:56 +00:00
|
|
|
alloca_tohex_sid_t_trunc(source->sid, 14), did, name, alloca_tohex_sid_t_trunc(directory_service->sid, 14));
|
|
|
|
|
|
|
|
struct internal_mdp_header header;
|
|
|
|
bzero(&header, sizeof header);
|
|
|
|
|
|
|
|
header.source = source;
|
|
|
|
header.source_port = MDP_PORT_NOREPLY;
|
|
|
|
header.destination = directory_service;
|
|
|
|
header.destination_port = MDP_PORT_DIRECTORY;
|
|
|
|
header.qos = OQ_ORDINARY;
|
|
|
|
char buff[256];
|
|
|
|
struct overlay_buffer *payload = ob_static((unsigned char*)buff, sizeof buff);
|
|
|
|
ob_limitsize(payload, snprintf(buff, sizeof buff, "%s|%s", did, name));
|
|
|
|
overlay_send_frame(&header, payload);
|
|
|
|
ob_free(payload);
|
2012-09-14 02:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send a registration packet for each unlocked identity
|
|
|
|
static void directory_send_keyring(struct subscriber *directory_service){
|
2014-10-31 03:13:23 +00:00
|
|
|
keyring_iterator it;
|
|
|
|
keyring_iterator_start(keyring, &it);
|
|
|
|
while(keyring_next_keytype(&it, KEYTYPE_DID)){
|
|
|
|
if (it.identity->subscriber && it.identity->subscriber->reachable == REACHABLE_SELF){
|
|
|
|
const char *unpackedDid = (const char *) it.keypair->private_key;
|
|
|
|
const char *name = (const char *) it.keypair->public_key;
|
|
|
|
directory_send(directory_service, it.identity->subscriber, unpackedDid, name);
|
2012-09-14 02:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-18 02:56:30 +00:00
|
|
|
static void directory_update(struct sched_ent *alarm){
|
2012-09-14 05:12:15 +00:00
|
|
|
if (directory_service){
|
2014-09-08 01:55:35 +00:00
|
|
|
// always attempt to reload the address, may depend on DNS resolution
|
|
|
|
load_subscriber_address(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
|
2015-07-06 08:19:49 +00:00
|
|
|
INFOF("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(){
|
2014-09-08 01:55:35 +00:00
|
|
|
if (is_sid_t_any(config.directory.service)) {
|
|
|
|
directory_service = NULL;
|
|
|
|
}else{
|
|
|
|
directory_service = find_subscriber(config.directory.service.binary, SID_SIZE, 1);
|
|
|
|
if (!directory_service){
|
|
|
|
WHYF("Failed to create subscriber record");
|
|
|
|
}else{
|
|
|
|
// used by tests
|
|
|
|
INFOF("ADD DIRECTORY SERVICE %s", alloca_tohex_sid_t(directory_service->sid));
|
|
|
|
}
|
|
|
|
}
|
2014-09-12 05:50:52 +00:00
|
|
|
unschedule(&directory_alarm);
|
2012-09-18 02:56:30 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|