2011-12-21 09:55:05 +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.
|
|
|
|
*/
|
|
|
|
|
2012-02-23 02:15:42 +00:00
|
|
|
#include "serval.h"
|
2012-11-07 06:12:45 +00:00
|
|
|
#include "str.h"
|
2012-08-22 00:51:38 +00:00
|
|
|
#include "overlay_buffer.h"
|
2012-08-27 00:34:59 +00:00
|
|
|
#include "overlay_packet.h"
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2012-11-23 05:55:32 +00:00
|
|
|
int overlay_frame_build_header(struct decode_context *context, struct overlay_buffer *buff,
|
|
|
|
int queue, int type, int modifiers, int ttl,
|
|
|
|
struct broadcast *broadcast, struct subscriber *next_hop,
|
|
|
|
struct subscriber *destination, struct subscriber *source){
|
|
|
|
|
|
|
|
int x = buff->position;
|
|
|
|
|
|
|
|
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>64)
|
|
|
|
ttl=64;
|
|
|
|
|
|
|
|
if (ob_append_byte(buff, type_byte)) return -1;
|
2012-11-21 02:22:39 +00:00
|
|
|
if (ob_append_byte(buff, ttl)) return -1;
|
2012-11-23 05:55:32 +00:00
|
|
|
if (ob_append_rfs(buff, 2)) return -1;
|
|
|
|
|
|
|
|
if (broadcast){
|
|
|
|
if (overlay_broadcast_append(context, buff, broadcast)) return -1;
|
|
|
|
}else{
|
|
|
|
if (overlay_address_append(context, buff, next_hop)) return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (destination){
|
|
|
|
if (overlay_address_append(context, buff, destination)) return -1;
|
|
|
|
}else{
|
|
|
|
if (ob_append_byte(buff, OA_CODE_PREVIOUS)) return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overlay_address_append(context, buff, source)) return -1;
|
|
|
|
|
|
|
|
dump("written header", buff->bytes + x, buff->position - x);
|
|
|
|
|
|
|
|
return 0;
|
2012-11-21 02:22:39 +00:00
|
|
|
}
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2012-11-22 22:34:42 +00:00
|
|
|
int overlay_frame_append_payload(struct decode_context *context, overlay_interface *interface,
|
|
|
|
struct overlay_frame *p, struct subscriber *next_hop, struct overlay_buffer *b)
|
2011-08-17 01:22:17 +00:00
|
|
|
{
|
|
|
|
/* Convert a payload (frame) structure into a series of bytes.
|
|
|
|
Assumes that any encryption etc has already been done.
|
|
|
|
Will pick a next hop if one has not been chosen.
|
|
|
|
*/
|
|
|
|
|
2012-08-22 00:51:38 +00:00
|
|
|
struct overlay_buffer *headers;
|
2012-07-12 01:06:41 +00:00
|
|
|
|
2012-08-22 00:51:38 +00:00
|
|
|
headers=ob_new();
|
2011-08-08 14:41:46 +00:00
|
|
|
|
|
|
|
if (!headers) return WHY("could not allocate overlay buffer for headers");
|
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
ob_checkpoint(b);
|
|
|
|
|
2012-11-21 02:22:39 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-11-23 05:55:32 +00:00
|
|
|
if (overlay_frame_build_header(context, headers,
|
|
|
|
p->queue, p->type, p->modifiers, p->ttl,
|
|
|
|
(p->sendBroadcast?&p->broadcast_id:NULL), next_hop,
|
|
|
|
p->destination, p->source))
|
|
|
|
goto cleanup;
|
2012-07-12 01:06:41 +00:00
|
|
|
|
2012-11-23 05:55:32 +00:00
|
|
|
int hdr_len=headers->position - (headers->var_length_offset +2);
|
2012-07-12 01:06:41 +00:00
|
|
|
if (debug&DEBUG_PACKETCONSTRUCTION)
|
2012-11-23 05:55:32 +00:00
|
|
|
DEBUGF("Patching RFS for actual_len=%d\n",hdr_len + p->payload->position);
|
2012-11-23 03:04:01 +00:00
|
|
|
|
2012-11-23 05:55:32 +00:00
|
|
|
ob_set_ui16(headers,headers->var_length_offset,hdr_len + p->payload->position);
|
2011-08-08 14:41:46 +00:00
|
|
|
|
|
|
|
/* Write payload format plus total length of header bits */
|
2012-08-22 00:51:38 +00:00
|
|
|
if (ob_makespace(b,2+headers->position+p->payload->position)) {
|
2011-08-08 14:41:46 +00:00
|
|
|
/* Not enough space free in output buffer */
|
2012-01-10 09:57:36 +00:00
|
|
|
if (debug&DEBUG_PACKETFORMATS)
|
2012-07-12 01:06:41 +00:00
|
|
|
DEBUGF("Could not make enough space free in output buffer");
|
|
|
|
goto cleanup;
|
2011-08-08 14:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Package up headers and payload */
|
2012-08-22 00:51:38 +00:00
|
|
|
if (ob_append_bytes(b,headers->bytes,headers->position)) {
|
2012-07-12 01:06:41 +00:00
|
|
|
WHY("could not append header");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-08-22 00:51:38 +00:00
|
|
|
if (ob_append_bytes(b,p->payload->bytes,p->payload->position)) {
|
2012-07-12 01:06:41 +00:00
|
|
|
WHY("could not append payload");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-09-07 01:56:26 +00:00
|
|
|
|
2011-08-08 14:41:46 +00:00
|
|
|
ob_free(headers);
|
2012-07-12 01:06:41 +00:00
|
|
|
return 0;
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
cleanup:
|
|
|
|
ob_free(headers);
|
|
|
|
ob_rewind(b);
|
|
|
|
return -1;
|
2011-08-08 14:41:46 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
int op_free(struct overlay_frame *p)
|
2011-08-08 14:41:46 +00:00
|
|
|
{
|
|
|
|
if (!p) return WHY("Asked to free NULL");
|
|
|
|
if (p->prev&&p->prev->next==p) return WHY("p->prev->next still points here");
|
|
|
|
if (p->next&&p->next->prev==p) return WHY("p->next->prev still points here");
|
|
|
|
p->prev=NULL;
|
|
|
|
p->next=NULL;
|
2011-09-05 02:49:53 +00:00
|
|
|
if (p->payload) ob_free(p->payload);
|
2011-08-08 14:41:46 +00:00
|
|
|
p->payload=NULL;
|
|
|
|
free(p);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-09-05 02:49:53 +00:00
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
struct overlay_frame *op_dup(struct overlay_frame *in)
|
2012-04-14 01:26:03 +00:00
|
|
|
{
|
|
|
|
if (!in) return NULL;
|
|
|
|
|
|
|
|
/* clone the frame */
|
2012-08-27 00:34:59 +00:00
|
|
|
struct overlay_frame *out=malloc(sizeof(struct overlay_frame));
|
2012-10-15 05:06:36 +00:00
|
|
|
if (!out) { WHY("malloc() failed"); return NULL; }
|
2012-04-14 01:26:03 +00:00
|
|
|
|
|
|
|
/* copy main data structure */
|
2012-08-27 00:34:59 +00:00
|
|
|
bcopy(in,out,sizeof(struct overlay_frame));
|
2012-03-21 02:27:24 +00:00
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
if (in->payload)
|
|
|
|
out->payload=ob_dup(in->payload);
|
2012-04-14 01:26:03 +00:00
|
|
|
return out;
|
|
|
|
}
|