Encapsulate payload header details

This commit is contained in:
Jeremy Lakeman 2012-11-21 12:52:39 +10:30
parent c4b90a108b
commit 4ca6db34d6
7 changed files with 90 additions and 120 deletions

View File

@ -153,10 +153,8 @@ schedule(&_sched_##X); }
#undef SCHEDULE
while(1) {
/* Check for activitiy and respond to it */
fd_poll();
}
/* Check for activitiy and respond to it */
while(fd_poll());
return 0;
}

View File

@ -121,12 +121,9 @@ int overlay_route_add_advertisements(overlay_interface *interface, struct overla
ob_checkpoint(e);
if (ob_append_byte(e,OF_TYPE_NODEANNOUNCE))
// assume we might fill the whole packet
if (overlay_packet_append_header(e, OF_TYPE_NODEANNOUNCE, 1, e->sizeLimit - e->position))
return WHY("could not add node advertisement header");
ob_append_byte(e,1); /* TTL */
// assume we might fill the packet
ob_append_rfs(e, e->sizeLimit - e->position);
/* Add address fields */
struct broadcast broadcast;

View File

@ -373,24 +373,16 @@ int overlay_add_selfannouncement(int interface,struct overlay_buffer *b)
time_ms_t now = gettime_ms();
/* Header byte */
if (ob_append_byte(b, OF_TYPE_SELFANNOUNCE))
return WHY("Could not add self-announcement header");
/* A TTL for this frame.
XXX - BATMAN uses various TTLs, but I think that it may just be better to have all TTL=1,
and have the onward nodes selectively choose which nodes to on-announce. If we prioritise
newly arrived nodes somewhat (or at least reserve some slots for them), then we can still
get the good news travels fast property of BATMAN, but without having to flood in the formal
sense. */
if (ob_append_byte(b,1))
return WHY("Could not add TTL to self-announcement");
/* XXX - BATMAN uses various TTLs, but I think that it may just be better to have all TTL=1,
and have the onward nodes selectively choose which nodes to on-announce. If we prioritise
newly arrived nodes somewhat (or at least reserve some slots for them), then we can still
get the good news travels fast property of BATMAN, but without having to flood in the formal
sense. */
/* Add space for Remaining Frame Size field. This will always be a single byte
for self-announcments as they are always <256 bytes. */
if (ob_append_rfs(b,1+8+1+SID_SIZE+4+4+1))
return WHY("Could not add RFS for self-announcement frame");
for self-announcments as they are always <256 bytes. */
if (overlay_packet_append_header(b, OF_TYPE_SELFANNOUNCE, 1, 1+8+1+SID_SIZE+4+4+1))
return WHY("Could not add self-announcement header");
/* Add next-hop address. Always link-local broadcast for self-announcements */
struct broadcast broadcast_id;
overlay_broadcast_generate_address(&broadcast_id);

View File

@ -22,44 +22,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "overlay_buffer.h"
#include "overlay_packet.h"
static int op_append_type(struct overlay_buffer *headers, struct overlay_frame *p)
{
unsigned char c[3];
// pack the QOS queue into the modifiers
int q_bits = p->queue;
if (q_bits>0) q_bits--;
if (q_bits>3) q_bits=3;
p->modifiers = (p->modifiers & ~OF_QUEUE_BITS)|q_bits;
switch(p->type&OF_TYPE_FLAG_BITS)
{
case OF_TYPE_FLAG_NORMAL:
c[0]=p->type|p->modifiers;
if (debug&DEBUG_PACKETFORMATS) DEBUGF("type resolves to %02x",c[0]);
if (ob_append_bytes(headers,c,1)) return -1;
break;
case OF_TYPE_FLAG_E12:
c[0]=(p->type&OF_MODIFIER_BITS)|OF_TYPE_EXTENDED12;
c[1]=(p->type>>4)&0xff;
if (debug&DEBUG_PACKETFORMATS) DEBUGF("type resolves to %02x%02x",c[0],c[1]);
if (ob_append_bytes(headers,c,2)) return -1;
break;
case OF_TYPE_FLAG_E20:
c[0]=(p->type&OF_MODIFIER_BITS)|OF_TYPE_EXTENDED20;
c[1]=(p->type>>4)&0xff;
c[2]=(p->type>>12)&0xff;
if (debug&DEBUG_PACKETFORMATS) DEBUGF("type resolves to %02x%02x%02x",c[0],c[1],c[2]);
if (ob_append_bytes(headers,c,3)) return -1;
break;
default:
/* Don't know this type of frame */
WHY("Asked for format frame with unknown TYPE_FLAG bits");
return -1;
}
return 0;
unsigned char magic_header[]={/* Magic */ 'O',0x10,
/* Version */ 0x00,0x01};
int overlay_packet_init_header(struct outgoing_packet *packet){
return ob_append_bytes(packet->buffer,magic_header,4);
}
int overlay_packet_append_header(struct overlay_buffer *buff, int type, int ttl, int approx_size){
if (ob_append_byte(buff, type)) return -1;
if (ob_append_byte(buff, ttl)) return -1;
return ob_append_rfs(buff, approx_size);
}
int overlay_frame_append_payload(overlay_interface *interface, struct overlay_frame *p, struct subscriber *next_hop, struct overlay_buffer *b)
{
@ -76,33 +50,36 @@ int overlay_frame_append_payload(overlay_interface *interface, struct overlay_fr
ob_checkpoint(b);
if (debug&DEBUG_PACKETCONSTRUCTION)
dump_payload(p,"append_payload stuffing into packet");
if (debug&DEBUG_PACKETCONSTRUCTION){
DEBUGF( "+++++\nFrame from %s to %s of type 0x%02x %s:",
alloca_tohex_sid(p->source->sid),
alloca_tohex_sid(p->destination->sid),p->type,
"append_payload stuffing into packet");
if (p->payload)
dump("payload contents", &p->payload->bytes[0],p->payload->position);
}
/* Build header */
/* Write fields into binary structure in correct order */
/* Write out type field byte(s) */
if (op_append_type(headers,p))
goto cleanup;
/* Write out TTL */
if (p->ttl>64)
p->ttl=64;
if (ob_append_byte(headers,p->ttl))
goto cleanup;
/* Length. This is the fun part, because we cannot calculate how many bytes we need until
{
int q_bits = p->queue;
if (q_bits>0) q_bits--;
if (q_bits>3) q_bits=3;
int type = p->type|p->modifiers|q_bits;
if (p->ttl>64)
p->ttl=64;
/* Length. This is the fun part, because we cannot calculate how many bytes we need until
we have abbreviated the addresses, and the length encoding we use varies according to the
length encoded. The simple option of running the abbreviations twice won't work because
we rely on context for abbreviating the addresses. So we write it initially and then patch it
after.
*/
int max_len=((SID_SIZE+3)*3+headers->position+p->payload->position);
if (debug&DEBUG_PACKETCONSTRUCTION)
DEBUGF("Appending RFS for max_len=%d\n",max_len);
ob_append_rfs(headers,max_len);
*/
int max_len=((SID_SIZE+3)*3+headers->position+p->payload->position);
if (overlay_packet_append_header(headers, type, p->ttl, max_len))
goto cleanup;
}
int addrs_start=headers->position;
@ -156,17 +133,6 @@ cleanup:
return -1;
}
int dump_payload(struct overlay_frame *p, char *message)
{
DEBUGF( "+++++\nFrame from %s to %s of type 0x%02x %s:",
alloca_tohex_sid(p->source->sid),
alloca_tohex_sid(p->destination->sid),p->type,
message?message:"");
if (p->payload)
dump("payload contents", &p->payload->bytes[0],p->payload->position);
return 0;
}
int op_free(struct overlay_frame *p)
{
if (!p) return WHY("Asked to free NULL");

View File

@ -1,3 +1,22 @@
/*
Copyright (C) 2012 Serval Project Inc
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.
*/
#include "serval.h"
#include "overlay_buffer.h"
#include "overlay_packet.h"
@ -32,9 +51,6 @@ typedef struct overlay_txqueue {
overlay_txqueue overlay_tx[OQ_MAX];
unsigned char magic_header[]={/* Magic */ 'O',0x10,
/* Version */ 0x00,0x01};
struct outgoing_packet{
overlay_interface *interface;
int i;
@ -215,6 +231,23 @@ int overlay_payload_enqueue(struct overlay_frame *p)
return 0;
}
static void overlay_add_sender_header(struct overlay_buffer *buff, overlay_interface *interface)
{
// add a badly formatted dummy self announce payload to tell people we sent this.
overlay_packet_append_header(buff, OF_TYPE_SELFANNOUNCE, 1, SID_SIZE + 2);
/* from me, to me, via me
(it's shorter than an actual broadcast,
and receivers wont try to process it
since its not going to have a payload body anyway) */
overlay_address_append_self(interface, buff);
overlay_address_set_sender(my_subscriber);
ob_append_byte(buff, OA_CODE_PREVIOUS);
ob_append_byte(buff, OA_CODE_PREVIOUS);
ob_patch_rfs(buff, COMPUTE_RFS_LENGTH);
}
static void
overlay_init_packet(struct outgoing_packet *packet, overlay_interface *interface, int tick){
packet->interface = interface;
@ -223,7 +256,8 @@ overlay_init_packet(struct outgoing_packet *packet, overlay_interface *interface
packet->buffer=ob_new();
packet->add_advertisements=1;
ob_limitsize(packet->buffer, packet->interface->mtu);
ob_append_bytes(packet->buffer,magic_header,4);
overlay_packet_init_header(packet);
overlay_address_clear();
@ -237,21 +271,7 @@ overlay_init_packet(struct outgoing_packet *packet, overlay_interface *interface
overlay_route_add_advertisements(packet->interface, packet->buffer);
}else{
// add a badly formatted dummy self announce payload to tell people we sent this.
ob_append_byte(packet->buffer, OF_TYPE_SELFANNOUNCE);
ob_append_byte(packet->buffer, 1);
ob_append_rfs(packet->buffer, SID_SIZE + 2);
/* from me, to me, via me
(it's shorter than an actual broadcast,
and receivers wont try to process it
since its not going to have a payload body anyway) */
overlay_address_append_self(interface, packet->buffer);
overlay_address_set_sender(my_subscriber);
ob_append_byte(packet->buffer, OA_CODE_PREVIOUS);
ob_append_byte(packet->buffer, OA_CODE_PREVIOUS);
ob_patch_rfs(packet->buffer, COMPUTE_RFS_LENGTH);
overlay_add_sender_header(packet->buffer, interface);
}
}

View File

@ -157,11 +157,8 @@ int overlay_rhizome_add_advertisements(int interface_number, struct overlay_buff
if (slots<1) { RETURN(WHY("No room for node advertisements")); }
if (ob_append_byte(e,OF_TYPE_RHIZOME_ADVERT))
if (overlay_packet_append_header(e, OF_TYPE_RHIZOME_ADVERT, 1, (1+11+1+2+RHIZOME_BAR_BYTES)))
RETURN(WHY("could not add rhizome bundle advertisement header"));
ob_append_byte(e, 1); /* TTL (1 byte) */
ob_append_rfs(e,1+11+1+2+RHIZOME_BAR_BYTES/* RFS */);
/* Stuff in dummy address fields (11 bytes) */
struct broadcast broadcast_id;

View File

@ -447,6 +447,8 @@ time_ms_t overlay_time_until_next_tick();
int overlay_add_selfannouncement();
int overlay_frame_append_payload(overlay_interface *interface, struct overlay_frame *p, struct subscriber *next_hop, struct overlay_buffer *b);
int overlay_packet_init_header(struct outgoing_packet *packet);
int overlay_packet_append_header(struct overlay_buffer *buff, int type, int ttl, int approx_size);
int overlay_interface_args(const char *arg);
int overlay_rhizome_add_advertisements(int interface_number,struct overlay_buffer *e);
int overlay_add_local_identity(unsigned char *s);
@ -591,8 +593,6 @@ int overlay_mdp_dispatch(overlay_mdp_frame *mdp,int userGeneratedFrameP,
struct sockaddr_un *recvaddr,int recvaddlen);
int overlay_mdp_dnalookup_reply(const sockaddr_mdp *dstaddr, const unsigned char *resolved_sid, const char *uri, const char *did, const char *name);
int dump_payload(struct overlay_frame *p, char *message);
int urandombytes(unsigned char *x,unsigned long long xlen);
#ifdef MALLOC_PARANOIA