mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-01-18 10:46:23 +00:00
New payload header format
This commit is contained in:
parent
5a5853f38a
commit
f13db5405e
16
constants.h
16
constants.h
@ -87,7 +87,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
#define OVERLAY_MAX_LOCAL_IDENTITIES 256
|
||||
|
||||
/* Overlay mesh packet codes */
|
||||
#define OF_TYPE_BITS 0xf0
|
||||
#define OF_TYPE_SELFANNOUNCE 0x10 /* BATMAN style announcement frames */
|
||||
#define OF_TYPE_SELFANNOUNCE_ACK 0x20 /* BATMAN style "I saw your announcment" frames */
|
||||
#define OF_TYPE_DATA 0x30 /* Ordinary data frame.
|
||||
@ -101,16 +100,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
#define OF_TYPE_PLEASEEXPLAIN 0x60 /* Request for resolution of an abbreviated address */
|
||||
#define OF_TYPE_NODEANNOUNCE 0x70
|
||||
|
||||
/* Modifiers that indicate the disposition of the frame */
|
||||
#define OF_MODIFIER_BITS 0x0f
|
||||
#define PAYLOAD_FLAG_SENDER_SAME (1<<0)
|
||||
#define PAYLOAD_FLAG_TO_BROADCAST (1<<1)
|
||||
#define PAYLOAD_FLAG_ONE_HOP (1<<2)
|
||||
#define PAYLOAD_FLAG_LONG_PAYLOAD (1<<3)
|
||||
#define PAYLOAD_FLAG_CIPHERED (1<<4)
|
||||
#define PAYLOAD_FLAG_SIGNED (1<<5)
|
||||
|
||||
/* Crypto/security options */
|
||||
#define OF_CRYPTO_NONE 0x00
|
||||
#define OF_CRYPTO_CIPHERED 0x04 /* Encrypted frame */
|
||||
#define OF_CRYPTO_SIGNED 0x08 /* signed frame */
|
||||
|
||||
/* QOS packet queue bits */
|
||||
#define OF_QUEUE_BITS 0x03
|
||||
#define OF_CRYPTO_CIPHERED PAYLOAD_FLAG_CIPHERED /* Encrypted frame */
|
||||
#define OF_CRYPTO_SIGNED PAYLOAD_FLAG_SIGNED /* signed frame */
|
||||
|
||||
/* Keep track of last 32 observations of a node.
|
||||
Hopefully this is enough, if not, we will increase.
|
||||
|
@ -546,11 +546,11 @@ int send_please_explain(struct decode_context *context, struct subscriber *sourc
|
||||
else
|
||||
context->please_explain->source = my_subscriber;
|
||||
|
||||
if (destination){
|
||||
if (destination && destination->reachable!=REACHABLE_NONE){
|
||||
context->please_explain->destination = destination;
|
||||
context->please_explain->ttl=64;
|
||||
}else{
|
||||
context->please_explain->ttl=2;// how will this work with olsr??
|
||||
context->please_explain->ttl=1;// how will this work with olsr??
|
||||
overlay_broadcast_generate_address(&context->please_explain->broadcast_id);
|
||||
}
|
||||
|
||||
@ -559,7 +559,7 @@ int send_please_explain(struct decode_context *context, struct subscriber *sourc
|
||||
if (!overlay_payload_enqueue(context->please_explain))
|
||||
RETURN(0);
|
||||
op_free(context->please_explain);
|
||||
RETURN(0);
|
||||
RETURN(-1);
|
||||
}
|
||||
|
||||
// process an incoming request for explanation of subscriber abbreviations
|
||||
|
@ -176,9 +176,6 @@ int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, s
|
||||
struct decode_context context;
|
||||
bzero(&context, sizeof context);
|
||||
|
||||
if (len<HEADERFIELDS_LEN)
|
||||
return WHY("Packet is too short");
|
||||
|
||||
time_ms_t now = gettime_ms();
|
||||
struct overlay_buffer *b = ob_static(packet, len);
|
||||
ob_limitsize(b, len);
|
||||
@ -213,49 +210,74 @@ int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, s
|
||||
|
||||
while(b->position < b->sizeLimit){
|
||||
context.invalid_addresses=0;
|
||||
|
||||
int flags = ob_get(b);
|
||||
|
||||
/* Get normal form of packet type and modifiers */
|
||||
f.type=flags&OF_TYPE_BITS;
|
||||
f.modifiers=flags&OF_MODIFIER_BITS;
|
||||
|
||||
f.queue = (f.modifiers & OF_QUEUE_BITS) +1;
|
||||
|
||||
/* Get time to live */
|
||||
f.ttl=ob_get(b);
|
||||
f.ttl--;
|
||||
|
||||
/* Decode length of remainder of frame */
|
||||
int payload_len=ob_get_ui16(b);
|
||||
|
||||
if (payload_len <=0)
|
||||
/* we fell off the end of the packet? */
|
||||
break;
|
||||
|
||||
dump("decoding header", b->bytes + b->position, payload_len);
|
||||
|
||||
int next_payload = b->position + payload_len;
|
||||
|
||||
/* Always attempt to resolve all of the addresses in a packet, or we could fail to understand an important payload
|
||||
eg, peer sends two payloads travelling in opposite directions;
|
||||
[Next, Dest, Sender] forwarding a payload we just send, so Sender == Me
|
||||
[Next, Dest, Sender] delivering a payload to us so Next == Me
|
||||
|
||||
But Next would be encoded as OA_CODE_PREVIOUS, so we must parse all three addresses,
|
||||
even if Next is obviously not intended for us
|
||||
*/
|
||||
|
||||
struct subscriber *nexthop=NULL;
|
||||
bzero(f.broadcast_id.id, BROADCAST_LEN);
|
||||
int process=1;
|
||||
int forward=1;
|
||||
int flags = ob_get(b);
|
||||
|
||||
// if the structure of the addresses looks wrong, stop immediately
|
||||
if (overlay_address_parse(&context, b, &f.broadcast_id, &nexthop)
|
||||
|| overlay_address_parse(&context, b, NULL, &f.destination)
|
||||
|| overlay_address_parse(&context, b, NULL, &f.source)){
|
||||
break;
|
||||
if (flags & PAYLOAD_FLAG_SENDER_SAME){
|
||||
f.source = context.sender;
|
||||
}else{
|
||||
if (overlay_address_parse(&context, b, NULL, &f.source))
|
||||
break;
|
||||
if (!f.source || f.source->reachable==REACHABLE_SELF)
|
||||
process=forward=0;
|
||||
}
|
||||
|
||||
if (flags & PAYLOAD_FLAG_TO_BROADCAST){
|
||||
if (!(flags & PAYLOAD_FLAG_ONE_HOP)){
|
||||
if (overlay_address_parse(&context, b, &f.broadcast_id, NULL))
|
||||
break;
|
||||
if (overlay_broadcast_drop_check(&f.broadcast_id)){
|
||||
process=forward=0;
|
||||
if (debug&DEBUG_OVERLAYFRAMES)
|
||||
DEBUGF("Ignoring duplicate broadcast (%s)", alloca_tohex(f.broadcast_id.id, BROADCAST_LEN));
|
||||
}
|
||||
}
|
||||
f.destination=NULL;
|
||||
}else{
|
||||
if (overlay_address_parse(&context, b, NULL, &f.destination))
|
||||
break;
|
||||
|
||||
if (!f.destination || f.destination->reachable!=REACHABLE_SELF){
|
||||
process=0;
|
||||
}
|
||||
|
||||
if (!(flags & PAYLOAD_FLAG_ONE_HOP)){
|
||||
if (overlay_address_parse(&context, b, NULL, &nexthop))
|
||||
break;
|
||||
|
||||
if (!nexthop || nexthop->reachable!=REACHABLE_SELF){
|
||||
forward=0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (flags & PAYLOAD_FLAG_ONE_HOP){
|
||||
f.ttl=1;
|
||||
}else{
|
||||
int ttl_qos = ob_get(b);
|
||||
f.ttl = ttl_qos & 0x1F;
|
||||
f.queue = (ttl_qos >> 5) & 3;
|
||||
}
|
||||
f.ttl--;
|
||||
if (f.ttl<=0)
|
||||
forward=0;
|
||||
f.type=ob_get(b);
|
||||
if (f.type<0)
|
||||
break;
|
||||
|
||||
f.modifiers=flags;
|
||||
|
||||
// TODO allow for one byte length
|
||||
int payload_len = ob_get_ui16(b);
|
||||
|
||||
if (payload_len <=0)
|
||||
break;
|
||||
|
||||
int next_payload = b->position + payload_len;
|
||||
|
||||
// if we can't understand one of the addresses, skip processing the payload
|
||||
if (context.invalid_addresses)
|
||||
goto next;
|
||||
@ -270,26 +292,8 @@ int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, s
|
||||
DEBUGF("Next hop %s", alloca_tohex_sid(nexthop->sid));
|
||||
}
|
||||
|
||||
// ignore any payload we sent
|
||||
if (f.source->reachable==REACHABLE_SELF){
|
||||
if (debug&DEBUG_OVERLAYFRAMES)
|
||||
DEBUGF("Ignoring payload from myself (%s)", alloca_tohex_sid(f.source->sid));
|
||||
if (!process && !forward)
|
||||
goto next;
|
||||
}
|
||||
|
||||
// skip unicast payloads that aren't for me
|
||||
if (nexthop && nexthop->reachable!=REACHABLE_SELF){
|
||||
if (debug&DEBUG_OVERLAYFRAMES)
|
||||
DEBUGF("Ignoring payload that is not meant for me (%s)", alloca_tohex_sid(nexthop->sid));
|
||||
goto next;
|
||||
}
|
||||
|
||||
// skip broadcast payloads we've already seen
|
||||
if ((!nexthop) && overlay_broadcast_drop_check(&f.broadcast_id)){
|
||||
if (debug&DEBUG_OVERLAYFRAMES)
|
||||
DEBUGF("Ignoring duplicate broadcast (%s)", alloca_tohex(f.broadcast_id.id, BROADCAST_LEN));
|
||||
goto next;
|
||||
}
|
||||
|
||||
f.payload = ob_slice(b, b->position, next_payload - b->position);
|
||||
if (!f.payload){
|
||||
@ -300,13 +304,12 @@ int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, s
|
||||
ob_limitsize(f.payload, next_payload - b->position);
|
||||
|
||||
// forward payloads that are for someone else or everyone
|
||||
if ((!f.destination) ||
|
||||
(f.destination->reachable != REACHABLE_SELF && f.destination->reachable != REACHABLE_NONE)){
|
||||
if (forward){
|
||||
overlay_forward_payload(&f);
|
||||
}
|
||||
|
||||
// process payloads that are for me or everyone
|
||||
if ((!f.destination) || f.destination->reachable==REACHABLE_SELF){
|
||||
if (process){
|
||||
process_incoming_frame(now, interface, &f, &context);
|
||||
}
|
||||
|
||||
|
@ -27,35 +27,42 @@ int overlay_frame_build_header(struct decode_context *context, struct overlay_bu
|
||||
struct broadcast *broadcast, struct subscriber *next_hop,
|
||||
struct subscriber *destination, struct subscriber *source){
|
||||
|
||||
int x = buff->position;
|
||||
int flags = modifiers & (PAYLOAD_FLAG_CIPHERED | PAYLOAD_FLAG_SIGNED);
|
||||
|
||||
int q_bits = queue;
|
||||
if (q_bits>0) q_bits--;
|
||||
if (q_bits>3) q_bits=3;
|
||||
int type_byte = type|modifiers|q_bits;
|
||||
if (ttl==1 && !broadcast)
|
||||
flags |= PAYLOAD_FLAG_ONE_HOP;
|
||||
if (destination && destination==next_hop)
|
||||
flags |= PAYLOAD_FLAG_ONE_HOP;
|
||||
|
||||
if (ttl>64)
|
||||
ttl=64;
|
||||
if (source == context->sender)
|
||||
flags |= PAYLOAD_FLAG_SENDER_SAME;
|
||||
|
||||
if (ob_append_byte(buff, type_byte)) return -1;
|
||||
if (ob_append_byte(buff, ttl)) return -1;
|
||||
if (ob_append_rfs(buff, 2)) return -1;
|
||||
if (!destination)
|
||||
flags |= PAYLOAD_FLAG_TO_BROADCAST;
|
||||
|
||||
if (broadcast){
|
||||
if (overlay_broadcast_append(context, buff, broadcast)) return -1;
|
||||
}else{
|
||||
if (overlay_address_append(context, buff, next_hop)) return -1;
|
||||
if (ob_append_byte(buff, flags)) return -1;
|
||||
|
||||
if (!(flags & PAYLOAD_FLAG_SENDER_SAME)){
|
||||
if (overlay_address_append(context, buff, source)) return -1;
|
||||
}
|
||||
|
||||
if (destination){
|
||||
if (flags & PAYLOAD_FLAG_TO_BROADCAST){
|
||||
if (!(flags & PAYLOAD_FLAG_ONE_HOP)){
|
||||
if (overlay_broadcast_append(context, buff, broadcast)) return -1;
|
||||
}
|
||||
}else{
|
||||
if (overlay_address_append(context, buff, destination)) return -1;
|
||||
}else{
|
||||
if (ob_append_byte(buff, OA_CODE_PREVIOUS)) return -1;
|
||||
if (!(flags & PAYLOAD_FLAG_ONE_HOP)){
|
||||
if (overlay_address_append(context, buff, next_hop)) return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (overlay_address_append(context, buff, source)) return -1;
|
||||
if (!(flags & PAYLOAD_FLAG_ONE_HOP)){
|
||||
if (ob_append_byte(buff, ttl | ((queue&3)<<5))) return -1;
|
||||
}
|
||||
if (ob_append_byte(buff, type)) return -1;
|
||||
|
||||
dump("written header", buff->bytes + x, buff->position - x);
|
||||
if (ob_append_rfs(buff, 2)) return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user