2012-08-27 00:34:59 +00:00
|
|
|
/*
|
|
|
|
Serval Distributed Numbering Architecture (DNA)
|
|
|
|
Copyright (C) 2010 Paul Gardner-Stephen
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Smart-flooding of broadcast information is also a requirement. The long addresses help here, as we can make any address that begins
|
|
|
|
with the first 192 bits all ones be broadcast, and use the remaining 64 bits as a "broadcast packet identifier" (BPI).
|
|
|
|
Nodes can remember recently seen BPIs and not forward broadcast frames that have been seen recently. This should get us smart flooding
|
|
|
|
of the majority of a mesh (with some node mobility issues being a factor). We could refine this later, but it will do for now, especially
|
|
|
|
since for things like number resolution we are happy to send repeat requests.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serval.h"
|
2012-12-04 03:42:28 +00:00
|
|
|
#include "conf.h"
|
2012-11-07 06:12:45 +00:00
|
|
|
#include "str.h"
|
2012-08-27 00:34:59 +00:00
|
|
|
#include "overlay_address.h"
|
|
|
|
#include "overlay_buffer.h"
|
2012-09-19 04:46:40 +00:00
|
|
|
#include "overlay_packet.h"
|
2012-10-15 05:06:36 +00:00
|
|
|
#include <arpa/inet.h>
|
2012-08-27 00:34:59 +00:00
|
|
|
|
|
|
|
#define MAX_BPIS 1024
|
|
|
|
#define BPI_MASK 0x3ff
|
|
|
|
static struct broadcast bpilist[MAX_BPIS];
|
|
|
|
|
2012-11-30 03:15:08 +00:00
|
|
|
#define OA_CODE_SELF 0xff
|
|
|
|
#define OA_CODE_PREVIOUS 0xfe
|
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
// each node has 16 slots based on the next 4 bits of a subscriber id
|
|
|
|
// each slot either points to another tree node or a struct subscriber.
|
|
|
|
struct tree_node{
|
|
|
|
// bit flags for the type of object each element points to
|
|
|
|
int is_tree;
|
|
|
|
|
|
|
|
union{
|
|
|
|
struct tree_node *tree_nodes[16];
|
|
|
|
struct subscriber *subscribers[16];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct tree_node root;
|
|
|
|
|
|
|
|
struct subscriber *my_subscriber=NULL;
|
|
|
|
|
|
|
|
static unsigned char get_nibble(const unsigned char *sid, int pos){
|
|
|
|
unsigned char byte = sid[pos>>1];
|
|
|
|
if (!(pos&1))
|
|
|
|
byte=byte>>4;
|
|
|
|
return byte&0xF;
|
|
|
|
}
|
|
|
|
|
2012-09-14 02:12:58 +00:00
|
|
|
// find a subscriber struct from a whole or abbreviated subscriber id
|
2012-08-27 00:34:59 +00:00
|
|
|
struct subscriber *find_subscriber(const unsigned char *sid, int len, int create){
|
|
|
|
struct tree_node *ptr = &root;
|
|
|
|
int pos=0;
|
|
|
|
if (len!=SID_SIZE)
|
|
|
|
create =0;
|
|
|
|
|
|
|
|
do{
|
|
|
|
unsigned char nibble = get_nibble(sid, pos++);
|
|
|
|
|
|
|
|
if (ptr->is_tree & (1<<nibble)){
|
|
|
|
ptr = ptr->tree_nodes[nibble];
|
|
|
|
|
|
|
|
}else if(!ptr->subscribers[nibble]){
|
|
|
|
// subscriber is not yet known
|
|
|
|
|
|
|
|
if (create){
|
|
|
|
struct subscriber *ret=(struct subscriber *)malloc(sizeof(struct subscriber));
|
|
|
|
memset(ret,0,sizeof(struct subscriber));
|
|
|
|
ptr->subscribers[nibble]=ret;
|
|
|
|
bcopy(sid, ret->sid, SID_SIZE);
|
|
|
|
ret->abbreviate_len=pos;
|
|
|
|
}
|
|
|
|
return ptr->subscribers[nibble];
|
|
|
|
|
|
|
|
}else{
|
|
|
|
// there's a subscriber in this slot, does it match the rest of the sid we've been given?
|
|
|
|
struct subscriber *ret = ptr->subscribers[nibble];
|
|
|
|
if (memcmp(ret->sid,sid,len)==0){
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we need to insert this subscriber, we have to make a new tree node first
|
|
|
|
if (!create)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// create a new tree node and move the existing subscriber into it
|
|
|
|
struct tree_node *new=(struct tree_node *)malloc(sizeof(struct tree_node));
|
|
|
|
memset(new,0,sizeof(struct tree_node));
|
|
|
|
ptr->tree_nodes[nibble]=new;
|
|
|
|
ptr->is_tree |= (1<<nibble);
|
|
|
|
|
|
|
|
ptr=new;
|
|
|
|
nibble=get_nibble(ret->sid,pos);
|
|
|
|
ptr->subscribers[nibble]=ret;
|
|
|
|
ret->abbreviate_len=pos+1;
|
|
|
|
// then go around the loop again to compare the next nibble against the sid until we find an empty slot.
|
|
|
|
}
|
|
|
|
}while(pos < len*2);
|
|
|
|
|
|
|
|
// abbreviation is not unique
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Walk the subscriber tree, calling the callback function for each subscriber.
|
|
|
|
if start is a valid pointer, the first entry returned will be after this subscriber
|
|
|
|
if the callback returns non-zero, the process will stop.
|
|
|
|
*/
|
2012-09-07 00:31:34 +00:00
|
|
|
static int walk_tree(struct tree_node *node, int pos,
|
|
|
|
unsigned char *start, int start_len,
|
|
|
|
unsigned char *end, int end_len,
|
2012-08-27 00:34:59 +00:00
|
|
|
int(*callback)(struct subscriber *, void *), void *context){
|
2012-09-07 00:31:34 +00:00
|
|
|
int i=0, e=16;
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2012-09-07 00:31:34 +00:00
|
|
|
if (start && pos < start_len*2){
|
|
|
|
i=get_nibble(start,pos);
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 00:31:34 +00:00
|
|
|
if (end && pos < end_len*2){
|
|
|
|
e=get_nibble(end,pos) +1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;i<e;i++){
|
2012-08-27 00:34:59 +00:00
|
|
|
if (node->is_tree & (1<<i)){
|
2012-09-07 00:31:34 +00:00
|
|
|
if (walk_tree(node->tree_nodes[i], pos+1, start, start_len, end, end_len, callback, context))
|
2012-08-27 00:34:59 +00:00
|
|
|
return 1;
|
2012-09-07 00:31:34 +00:00
|
|
|
}else if(node->subscribers[i]){
|
2012-10-09 01:34:02 +00:00
|
|
|
if (callback(node->subscribers[i], context))
|
|
|
|
return 1;
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
2012-09-07 00:31:34 +00:00
|
|
|
// stop comparing the start sid after looking at the first branch of the tree
|
|
|
|
start=NULL;
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-10-09 01:34:02 +00:00
|
|
|
walk the tree, starting at start inclusive, calling the supplied callback function
|
2012-08-27 00:34:59 +00:00
|
|
|
*/
|
|
|
|
void enum_subscribers(struct subscriber *start, int(*callback)(struct subscriber *, void *), void *context){
|
2012-09-07 00:31:34 +00:00
|
|
|
walk_tree(&root, 0, start->sid, SID_SIZE, NULL, 0, callback, context);
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 02:19:12 +00:00
|
|
|
// quick test to make sure the specified route is valid.
|
|
|
|
int subscriber_is_reachable(struct subscriber *subscriber){
|
|
|
|
if (!subscriber)
|
|
|
|
return REACHABLE_NONE;
|
|
|
|
|
2012-11-16 05:24:01 +00:00
|
|
|
int ret = subscriber->reachable;
|
2012-09-19 00:20:29 +00:00
|
|
|
|
2012-11-16 05:24:01 +00:00
|
|
|
if (ret==REACHABLE_INDIRECT){
|
2012-09-10 01:25:12 +00:00
|
|
|
if (!subscriber->next_hop)
|
2012-11-16 05:24:01 +00:00
|
|
|
ret = REACHABLE_NONE;
|
2012-09-10 01:25:12 +00:00
|
|
|
|
|
|
|
// avoid infinite recursion...
|
2012-11-30 03:15:08 +00:00
|
|
|
else if (!(subscriber->next_hop->reachable & REACHABLE_DIRECT))
|
2012-11-16 05:24:01 +00:00
|
|
|
ret = REACHABLE_NONE;
|
|
|
|
else{
|
|
|
|
int r = subscriber_is_reachable(subscriber->next_hop);
|
2012-11-30 03:15:08 +00:00
|
|
|
if (r&REACHABLE_ASSUMED)
|
|
|
|
ret = REACHABLE_NONE;
|
|
|
|
else if (!(r & REACHABLE_DIRECT))
|
2012-11-16 05:24:01 +00:00
|
|
|
ret = REACHABLE_NONE;
|
|
|
|
}
|
2012-09-10 01:25:12 +00:00
|
|
|
}
|
2012-08-30 02:19:12 +00:00
|
|
|
|
2012-11-30 03:15:08 +00:00
|
|
|
if (ret & REACHABLE_DIRECT){
|
2012-09-10 01:25:12 +00:00
|
|
|
// make sure the interface is still up
|
|
|
|
if (!subscriber->interface)
|
2012-11-16 05:24:01 +00:00
|
|
|
ret=REACHABLE_NONE;
|
|
|
|
else if (subscriber->interface->state!=INTERFACE_STATE_UP)
|
|
|
|
ret=REACHABLE_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2012-08-30 02:19:12 +00:00
|
|
|
}
|
|
|
|
|
2012-09-18 02:56:30 +00:00
|
|
|
int set_reachable(struct subscriber *subscriber, int reachable){
|
|
|
|
if (subscriber->reachable==reachable)
|
|
|
|
return 0;
|
|
|
|
subscriber->reachable=reachable;
|
2012-11-07 23:53:57 +00:00
|
|
|
|
|
|
|
// These log messages are for use in tests. Changing them may break test scripts.
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayrouting) {
|
2012-11-07 23:53:57 +00:00
|
|
|
switch (reachable) {
|
|
|
|
case REACHABLE_NONE:
|
|
|
|
DEBUGF("NOT REACHABLE sid=%s", alloca_tohex_sid(subscriber->sid));
|
|
|
|
break;
|
|
|
|
case REACHABLE_SELF:
|
|
|
|
break;
|
|
|
|
case REACHABLE_INDIRECT:
|
|
|
|
DEBUGF("REACHABLE INDIRECTLY sid=%s", alloca_tohex_sid(subscriber->sid));
|
2012-12-04 04:17:57 +00:00
|
|
|
DEBUGF("(via %s, %d)",subscriber->next_hop?alloca_tohex_sid(subscriber->next_hop->sid):"NOONE!"
|
|
|
|
,subscriber->next_hop?subscriber->next_hop->reachable:0);
|
2012-11-07 23:53:57 +00:00
|
|
|
break;
|
|
|
|
case REACHABLE_UNICAST:
|
|
|
|
DEBUGF("REACHABLE VIA UNICAST sid=%s", alloca_tohex_sid(subscriber->sid));
|
|
|
|
break;
|
|
|
|
case REACHABLE_BROADCAST:
|
|
|
|
DEBUGF("REACHABLE VIA BROADCAST sid=%s", alloca_tohex_sid(subscriber->sid));
|
|
|
|
break;
|
2012-12-04 04:17:57 +00:00
|
|
|
case REACHABLE_UNICAST|REACHABLE_ASSUMED:
|
|
|
|
DEBUGF("ASSUMED REACHABLE VIA UNICAST sid=%s", alloca_tohex_sid(subscriber->sid));
|
|
|
|
break;
|
|
|
|
case REACHABLE_BROADCAST|REACHABLE_ASSUMED:
|
|
|
|
DEBUGF("ASSUMED REACHABLE VIA BROADCAST sid=%s", alloca_tohex_sid(subscriber->sid));
|
|
|
|
break;
|
2012-11-07 23:53:57 +00:00
|
|
|
}
|
2012-09-18 02:56:30 +00:00
|
|
|
}
|
2012-11-07 23:53:57 +00:00
|
|
|
|
2012-10-03 04:29:46 +00:00
|
|
|
/* Pre-emptively send a sas request */
|
2012-11-30 03:15:08 +00:00
|
|
|
if (!subscriber->sas_valid && reachable&REACHABLE)
|
2012-10-03 04:29:46 +00:00
|
|
|
keyring_send_sas_request(subscriber);
|
|
|
|
|
|
|
|
// Hacky layering violation... send our identity to a directory service
|
2012-12-04 04:17:57 +00:00
|
|
|
if (subscriber==directory_service)
|
2012-09-18 02:56:30 +00:00
|
|
|
directory_registration();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-06 05:51:31 +00:00
|
|
|
// mark the subscriber as reachable via reply unicast packet
|
|
|
|
int reachable_unicast(struct subscriber *subscriber, overlay_interface *interface, struct in_addr addr, int port){
|
2012-11-30 03:15:08 +00:00
|
|
|
if (subscriber->reachable&REACHABLE)
|
2012-12-07 01:41:48 +00:00
|
|
|
return -1;
|
2012-09-06 05:51:31 +00:00
|
|
|
|
|
|
|
if (subscriber->node)
|
2012-12-07 01:41:48 +00:00
|
|
|
return -1;
|
2012-09-06 05:51:31 +00:00
|
|
|
|
|
|
|
subscriber->interface = interface;
|
|
|
|
subscriber->address.sin_family = AF_INET;
|
|
|
|
subscriber->address.sin_addr = addr;
|
2012-09-07 04:06:02 +00:00
|
|
|
subscriber->address.sin_port = htons(port);
|
2012-10-03 04:29:46 +00:00
|
|
|
set_reachable(subscriber, REACHABLE_UNICAST);
|
2012-09-06 05:51:31 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-07 01:41:48 +00:00
|
|
|
int resolve_name(const char *name, struct in_addr *addr){
|
|
|
|
// TODO this can block, move to worker thread.
|
|
|
|
IN();
|
|
|
|
int ret=0;
|
|
|
|
struct addrinfo hint={
|
|
|
|
.ai_family=AF_INET,
|
|
|
|
};
|
|
|
|
struct addrinfo *addresses=NULL;
|
|
|
|
if (getaddrinfo(name, NULL, &hint, &addresses))
|
|
|
|
RETURN(WHYF("Failed to resolve %s",name));
|
|
|
|
|
|
|
|
if (addresses->ai_addr->sa_family==AF_INET){
|
|
|
|
*addr = ((struct sockaddr_in *)addresses->ai_addr)->sin_addr;
|
|
|
|
DEBUGF("Resolved %s into %s", name, inet_ntoa(*addr));
|
|
|
|
|
|
|
|
}else
|
|
|
|
ret=-1;
|
|
|
|
|
|
|
|
freeaddrinfo(addresses);
|
|
|
|
RETURN(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
// load a unicast address from configuration
|
2012-12-04 03:42:28 +00:00
|
|
|
int load_subscriber_address(struct subscriber *subscriber)
|
|
|
|
{
|
2012-12-04 04:17:57 +00:00
|
|
|
if (subscriber_is_reachable(subscriber)&REACHABLE)
|
|
|
|
return 0;
|
2012-12-04 03:42:28 +00:00
|
|
|
int i = config_host_list__get(&config.hosts, (const sid_t*)subscriber->sid);
|
|
|
|
// No unicast configuration? just return.
|
|
|
|
if (i == -1)
|
2012-09-14 02:20:45 +00:00
|
|
|
return 1;
|
2012-12-04 03:42:28 +00:00
|
|
|
const struct config_host *hostc = &config.hosts.av[i].value;
|
2012-12-08 04:39:41 +00:00
|
|
|
overlay_interface *interface = NULL;
|
|
|
|
if (*hostc->interface){
|
|
|
|
interface = overlay_interface_find_name(hostc->interface);
|
|
|
|
if (!interface)
|
|
|
|
return -1;
|
|
|
|
}
|
2012-12-04 04:17:57 +00:00
|
|
|
struct sockaddr_in addr;
|
2012-12-10 03:54:52 +00:00
|
|
|
bzero(&addr, sizeof(addr));
|
2012-12-07 03:39:55 +00:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr = hostc->address;
|
2012-12-08 04:39:41 +00:00
|
|
|
addr.sin_port = htons(hostc->port);
|
2012-12-07 01:41:48 +00:00
|
|
|
if (addr.sin_addr.s_addr==INADDR_NONE){
|
|
|
|
if (interface || overlay_interface_get_default()){
|
|
|
|
if (resolve_name(hostc->host, &addr.sin_addr))
|
|
|
|
return -1;
|
|
|
|
}else{
|
|
|
|
// interface isnt up yet
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2012-12-10 03:54:52 +00:00
|
|
|
DEBUGF("Loaded address %s:%d for %s", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port), alloca_tohex_sid(subscriber->sid));
|
2012-12-04 04:17:57 +00:00
|
|
|
return overlay_send_probe(subscriber, addr, interface);
|
2012-09-14 02:20:45 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
// generate a new random broadcast address
|
|
|
|
int overlay_broadcast_generate_address(struct broadcast *addr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0;i<BROADCAST_LEN;i++) addr->id[i]=random()&0xff;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// test if the broadcast address has been seen
|
|
|
|
int overlay_broadcast_drop_check(struct broadcast *addr)
|
|
|
|
{
|
|
|
|
/* Hash the BPI and see if we have seen it recently.
|
|
|
|
If so, drop the frame.
|
|
|
|
The occassional failure to supress a broadcast frame is not
|
|
|
|
something we are going to worry about just yet. For byzantine
|
|
|
|
robustness it is however required. */
|
|
|
|
int bpi_index=0;
|
|
|
|
int i;
|
|
|
|
for(i=0;i<BROADCAST_LEN;i++)
|
|
|
|
{
|
|
|
|
bpi_index=((bpi_index<<3)&0xfff8)+((bpi_index>>13)&0x7);
|
|
|
|
bpi_index^=addr->id[i];
|
|
|
|
}
|
|
|
|
bpi_index&=BPI_MASK;
|
|
|
|
|
|
|
|
if (memcmp(bpilist[bpi_index].id, addr->id, BROADCAST_LEN)){
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.broadcasts)
|
2012-08-27 00:34:59 +00:00
|
|
|
DEBUGF("BPI %s is new", alloca_tohex(addr->id, BROADCAST_LEN));
|
|
|
|
bcopy(addr->id, bpilist[bpi_index].id, BROADCAST_LEN);
|
|
|
|
return 0; /* don't drop */
|
|
|
|
}else{
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.broadcasts)
|
2012-08-27 00:34:59 +00:00
|
|
|
DEBUGF("BPI %s is a duplicate", alloca_tohex(addr->id, BROADCAST_LEN));
|
|
|
|
return 1; /* drop frame because we have seen this BPI recently */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 04:58:13 +00:00
|
|
|
int overlay_broadcast_append(struct overlay_buffer *b, struct broadcast *broadcast)
|
2012-08-27 00:34:59 +00:00
|
|
|
{
|
2012-11-26 04:58:13 +00:00
|
|
|
return ob_append_bytes(b, broadcast->id, BROADCAST_LEN);
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// append an appropriate abbreviation into the address
|
2012-11-22 22:34:42 +00:00
|
|
|
int overlay_address_append(struct decode_context *context, struct overlay_buffer *b, struct subscriber *subscriber)
|
2012-08-27 00:34:59 +00:00
|
|
|
{
|
2012-11-30 03:15:08 +00:00
|
|
|
if (!subscriber)
|
|
|
|
return WHY("No address supplied");
|
|
|
|
|
2012-11-22 22:34:42 +00:00
|
|
|
if (context && subscriber==context->sender){
|
2012-11-30 03:15:08 +00:00
|
|
|
if (ob_append_byte(b, OA_CODE_SELF))
|
|
|
|
return -1;
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2012-11-22 22:34:42 +00:00
|
|
|
}else if(context && subscriber==context->previous){
|
2012-11-30 03:15:08 +00:00
|
|
|
if (ob_append_byte(b, OA_CODE_PREVIOUS))
|
|
|
|
return -1;
|
2012-08-27 00:34:59 +00:00
|
|
|
|
|
|
|
}else{
|
2012-11-26 04:58:13 +00:00
|
|
|
int len=SID_SIZE;
|
|
|
|
if (subscriber->send_full){
|
|
|
|
subscriber->send_full=0;
|
|
|
|
}else{
|
|
|
|
len=(subscriber->abbreviate_len+2)/2;
|
|
|
|
if (subscriber->reachable==REACHABLE_SELF)
|
|
|
|
len++;
|
|
|
|
if (len>SID_SIZE)
|
|
|
|
len=SID_SIZE;
|
|
|
|
}
|
2012-11-30 03:15:08 +00:00
|
|
|
if (ob_append_byte(b, len))
|
|
|
|
return -1;
|
|
|
|
if (ob_append_bytes(b, subscriber->sid, len))
|
|
|
|
return -1;
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
2012-11-22 22:34:42 +00:00
|
|
|
if (context)
|
|
|
|
context->previous = subscriber;
|
2012-08-27 00:34:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-19 04:46:40 +00:00
|
|
|
static int add_explain_response(struct subscriber *subscriber, void *context){
|
|
|
|
struct decode_context *response = context;
|
|
|
|
if (!response->please_explain){
|
|
|
|
response->please_explain = calloc(sizeof(struct overlay_frame),1);
|
|
|
|
response->please_explain->payload=ob_new();
|
|
|
|
ob_limitsize(response->please_explain->payload, 1024);
|
|
|
|
}
|
2012-09-19 07:02:25 +00:00
|
|
|
|
|
|
|
// if one of our identities is unknown,
|
|
|
|
// the header of our next payload must include our full sid.
|
|
|
|
if (subscriber->reachable==REACHABLE_SELF)
|
|
|
|
subscriber->send_full = 1;
|
|
|
|
|
2012-09-19 04:46:40 +00:00
|
|
|
// add the whole subscriber id to the payload, stop if we run out of space
|
|
|
|
DEBUGF("Adding full sid by way of explanation %s", alloca_tohex_sid(subscriber->sid));
|
2012-11-26 04:58:13 +00:00
|
|
|
if (ob_append_byte(response->please_explain->payload, SID_SIZE))
|
|
|
|
return 1;
|
2012-09-19 04:46:40 +00:00
|
|
|
if (ob_append_bytes(response->please_explain->payload, subscriber->sid, SID_SIZE))
|
|
|
|
return 1;
|
2012-09-07 00:31:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-06 01:34:13 +00:00
|
|
|
static int find_subscr_buffer(struct decode_context *context, struct overlay_buffer *b, int len, struct subscriber **subscriber){
|
|
|
|
if (len<=0 || len>SID_SIZE){
|
|
|
|
return WHY("Invalid abbreviation length");
|
|
|
|
}
|
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
unsigned char *id = ob_get_bytes_ptr(b, len);
|
2012-12-06 01:34:13 +00:00
|
|
|
if (!id){
|
2012-09-06 07:08:11 +00:00
|
|
|
return WHY("Not enough space in buffer to parse address");
|
2012-12-06 01:34:13 +00:00
|
|
|
}
|
2012-09-19 04:46:40 +00:00
|
|
|
|
|
|
|
if (!subscriber){
|
|
|
|
WARN("Could not resolve address, no buffer supplied");
|
|
|
|
context->invalid_addresses=1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-26 04:58:13 +00:00
|
|
|
*subscriber=find_subscriber(id, len, 1);
|
2012-09-06 07:08:11 +00:00
|
|
|
|
|
|
|
if (!*subscriber){
|
2012-09-19 04:46:40 +00:00
|
|
|
context->invalid_addresses=1;
|
2012-09-06 07:08:11 +00:00
|
|
|
|
2012-09-19 04:46:40 +00:00
|
|
|
// generate a please explain in the passed in context
|
2012-09-07 00:31:34 +00:00
|
|
|
|
2012-09-19 04:46:40 +00:00
|
|
|
// add the abbreviation you told me about
|
|
|
|
if (!context->please_explain){
|
|
|
|
context->please_explain = calloc(sizeof(struct overlay_frame),1);
|
|
|
|
context->please_explain->payload=ob_new();
|
2012-10-09 01:34:02 +00:00
|
|
|
ob_limitsize(context->please_explain->payload, MDP_MTU);
|
2012-09-19 04:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// And I'll tell you about any subscribers I know that match this abbreviation,
|
|
|
|
// so you don't try to use an abbreviation that's too short in future.
|
|
|
|
walk_tree(&root, 0, id, len, id, len, add_explain_response, context);
|
|
|
|
|
|
|
|
INFOF("Asking for explanation of %s", alloca_tohex(id, len));
|
2012-11-26 04:58:13 +00:00
|
|
|
ob_append_byte(context->please_explain->payload, len);
|
2012-09-19 04:46:40 +00:00
|
|
|
ob_append_bytes(context->please_explain->payload, id, len);
|
|
|
|
|
|
|
|
}else{
|
2012-12-07 05:34:40 +00:00
|
|
|
if (context)
|
|
|
|
context->previous=*subscriber;
|
2012-09-06 07:08:11 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
|
|
|
|
2012-11-26 04:58:13 +00:00
|
|
|
int overlay_broadcast_parse(struct overlay_buffer *b, struct broadcast *broadcast)
|
|
|
|
{
|
|
|
|
return ob_get_bytes(b, broadcast->id, BROADCAST_LEN);
|
|
|
|
}
|
|
|
|
|
2012-09-06 07:08:11 +00:00
|
|
|
// returns 0 = success, -1 = fatal parsing error, 1 = unable to identify address
|
2012-11-26 04:58:13 +00:00
|
|
|
int overlay_address_parse(struct decode_context *context, struct overlay_buffer *b, struct subscriber **subscriber)
|
2012-08-27 00:34:59 +00:00
|
|
|
{
|
2012-11-26 04:58:13 +00:00
|
|
|
int len = ob_get(b);
|
2012-12-08 04:39:41 +00:00
|
|
|
if (len<0)
|
|
|
|
return WHY("Buffer too small");
|
2012-11-26 04:58:13 +00:00
|
|
|
|
|
|
|
switch(len){
|
2012-08-27 00:34:59 +00:00
|
|
|
case OA_CODE_SELF:
|
2012-11-26 04:58:13 +00:00
|
|
|
if (!context->sender){
|
2012-09-06 07:08:11 +00:00
|
|
|
INFO("Could not resolve address, sender has not been set");
|
2012-09-19 04:46:40 +00:00
|
|
|
context->invalid_addresses=1;
|
|
|
|
}else{
|
2012-11-22 22:34:42 +00:00
|
|
|
*subscriber=context->sender;
|
|
|
|
context->previous=context->sender;
|
2012-09-06 07:08:11 +00:00
|
|
|
}
|
2012-08-27 00:34:59 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case OA_CODE_PREVIOUS:
|
2012-11-26 04:58:13 +00:00
|
|
|
if (!context->previous){
|
2012-09-06 07:08:11 +00:00
|
|
|
INFO("Unable to decode previous address");
|
2012-09-19 04:46:40 +00:00
|
|
|
context->invalid_addresses=1;
|
2012-11-26 04:58:13 +00:00
|
|
|
}else{
|
|
|
|
*subscriber=context->previous;
|
2012-09-06 07:08:11 +00:00
|
|
|
}
|
2012-08-27 00:34:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-26 04:58:13 +00:00
|
|
|
return find_subscr_buffer(context, b, len, subscriber);
|
2012-09-19 04:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// once we've finished parsing a packet, complete and send a please explain if required.
|
|
|
|
int send_please_explain(struct decode_context *context, struct subscriber *source, struct subscriber *destination){
|
2012-11-23 05:55:32 +00:00
|
|
|
IN();
|
2012-12-04 04:17:57 +00:00
|
|
|
struct overlay_frame *frame=context->please_explain;
|
|
|
|
if (!frame)
|
2012-11-23 05:55:32 +00:00
|
|
|
RETURN(0);
|
2012-12-04 04:17:57 +00:00
|
|
|
frame->type = OF_TYPE_PLEASEEXPLAIN;
|
2012-09-19 04:46:40 +00:00
|
|
|
|
|
|
|
if (source)
|
2012-12-04 04:17:57 +00:00
|
|
|
frame->source = source;
|
2012-09-19 04:46:40 +00:00
|
|
|
else
|
2012-12-04 04:17:57 +00:00
|
|
|
frame->source = my_subscriber;
|
2012-09-19 04:46:40 +00:00
|
|
|
|
2012-12-04 04:17:57 +00:00
|
|
|
frame->source->send_full=1;
|
|
|
|
frame->destination = destination;
|
2012-11-30 03:15:08 +00:00
|
|
|
|
|
|
|
if (destination && (destination->reachable & REACHABLE)){
|
2012-12-04 04:17:57 +00:00
|
|
|
frame->ttl=64;
|
2012-09-19 04:46:40 +00:00
|
|
|
}else{
|
2012-12-04 04:17:57 +00:00
|
|
|
frame->ttl=1;// how will this work with olsr??
|
|
|
|
overlay_broadcast_generate_address(&frame->broadcast_id);
|
|
|
|
if (context->interface){
|
|
|
|
frame->destination_resolved=1;
|
|
|
|
frame->next_hop = destination;
|
|
|
|
frame->recvaddr=context->addr;
|
|
|
|
frame->interface=context->interface;
|
|
|
|
}
|
2012-09-19 04:46:40 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 04:17:57 +00:00
|
|
|
frame->queue=OQ_MESH_MANAGEMENT;
|
|
|
|
if (!overlay_payload_enqueue(frame))
|
2012-11-23 05:55:32 +00:00
|
|
|
RETURN(0);
|
2012-12-04 04:17:57 +00:00
|
|
|
op_free(frame);
|
2012-11-26 04:22:49 +00:00
|
|
|
RETURN(-1);
|
2012-09-19 04:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// process an incoming request for explanation of subscriber abbreviations
|
|
|
|
int process_explain(struct overlay_frame *frame){
|
|
|
|
struct overlay_buffer *b=frame->payload;
|
|
|
|
|
2012-11-23 05:38:23 +00:00
|
|
|
struct decode_context context;
|
|
|
|
bzero(&context, sizeof context);
|
2012-09-19 04:46:40 +00:00
|
|
|
|
2012-11-27 04:02:10 +00:00
|
|
|
while(ob_remaining(b)>0){
|
2012-11-26 04:58:13 +00:00
|
|
|
int len = ob_get(b);
|
|
|
|
if (len<=0 || len>SID_SIZE)
|
|
|
|
return WHY("Badly formatted explain message");
|
2012-09-19 04:46:40 +00:00
|
|
|
unsigned char *sid = ob_get_bytes_ptr(b, len);
|
2012-11-23 05:38:23 +00:00
|
|
|
if (!sid)
|
2012-11-26 04:58:13 +00:00
|
|
|
return WHY("Ran past end of buffer");
|
2012-09-19 04:46:40 +00:00
|
|
|
|
|
|
|
if (len==SID_SIZE){
|
|
|
|
// This message is also used to inform people of previously unknown subscribers
|
|
|
|
// make sure we know this one
|
|
|
|
find_subscriber(sid,len,1);
|
|
|
|
}else{
|
|
|
|
// reply to the sender with all subscribers that match this abbreviation
|
|
|
|
INFOF("Sending responses for %s", alloca_tohex(sid, len));
|
|
|
|
walk_tree(&root, 0, sid, len, sid, len, add_explain_response, &context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
send_please_explain(&context, frame->destination, frame->source);
|
|
|
|
return 0;
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|