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-12-11 05:29:46 +00:00
|
|
|
#include "conf.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
|
|
|
|
2013-05-24 04:22:31 +00:00
|
|
|
static int overlay_frame_build_header(int packet_version, struct decode_context *context,
|
|
|
|
struct overlay_buffer *buff,
|
2013-08-30 07:54:52 +00:00
|
|
|
int queue, int type, int modifiers, char will_retransmit,
|
|
|
|
int ttl, int sequence,
|
2012-11-23 05:55:32 +00:00
|
|
|
struct broadcast *broadcast, struct subscriber *next_hop,
|
2013-04-23 06:32:39 +00:00
|
|
|
struct subscriber *destination, struct subscriber *source)
|
|
|
|
{
|
|
|
|
if (ttl < 0 || ttl > PAYLOAD_TTL_MAX)
|
|
|
|
return WHYF("invalid ttl=%d", ttl);
|
|
|
|
|
2012-11-26 04:22:49 +00:00
|
|
|
int flags = modifiers & (PAYLOAD_FLAG_CIPHERED | PAYLOAD_FLAG_SIGNED);
|
2013-08-30 07:54:52 +00:00
|
|
|
if (will_retransmit)
|
|
|
|
flags|=PAYLOAD_FLAG_ACK_SOON;
|
|
|
|
|
2012-11-26 04:22:49 +00:00
|
|
|
if (ttl==1 && !broadcast)
|
|
|
|
flags |= PAYLOAD_FLAG_ONE_HOP;
|
|
|
|
if (destination && destination==next_hop)
|
|
|
|
flags |= PAYLOAD_FLAG_ONE_HOP;
|
2012-11-23 05:55:32 +00:00
|
|
|
|
2012-11-26 04:22:49 +00:00
|
|
|
if (source == context->sender)
|
|
|
|
flags |= PAYLOAD_FLAG_SENDER_SAME;
|
2012-11-23 05:55:32 +00:00
|
|
|
|
2012-11-26 04:22:49 +00:00
|
|
|
if (!destination)
|
|
|
|
flags |= PAYLOAD_FLAG_TO_BROADCAST;
|
2012-11-23 05:55:32 +00:00
|
|
|
|
2012-11-27 00:54:03 +00:00
|
|
|
if (type!=OF_TYPE_DATA)
|
|
|
|
flags |= PAYLOAD_FLAG_LEGACY_TYPE;
|
|
|
|
|
2013-11-25 06:13:32 +00:00
|
|
|
ob_append_byte(buff, flags);
|
2012-11-26 04:22:49 +00:00
|
|
|
|
2013-11-25 06:13:32 +00:00
|
|
|
if (!(flags & PAYLOAD_FLAG_SENDER_SAME))
|
|
|
|
overlay_address_append(context, buff, source);
|
2012-11-23 05:55:32 +00:00
|
|
|
|
2012-11-26 04:22:49 +00:00
|
|
|
if (flags & PAYLOAD_FLAG_TO_BROADCAST){
|
2013-11-25 06:13:32 +00:00
|
|
|
if (!(flags & PAYLOAD_FLAG_ONE_HOP))
|
|
|
|
overlay_broadcast_append(buff, broadcast);
|
|
|
|
} else {
|
|
|
|
overlay_address_append(context, buff, destination);
|
|
|
|
if (!(flags & PAYLOAD_FLAG_ONE_HOP))
|
|
|
|
overlay_address_append(context, buff, next_hop);
|
2012-11-23 05:55:32 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 06:13:32 +00:00
|
|
|
if (!(flags & PAYLOAD_FLAG_ONE_HOP))
|
|
|
|
ob_append_byte(buff, ttl | ((queue&3)<<5));
|
2012-11-27 00:54:03 +00:00
|
|
|
|
2013-11-25 06:13:32 +00:00
|
|
|
if (flags & PAYLOAD_FLAG_LEGACY_TYPE)
|
|
|
|
ob_append_byte(buff, type);
|
2013-05-15 06:26:43 +00:00
|
|
|
|
2013-06-03 06:34:08 +00:00
|
|
|
if (packet_version >= 1)
|
2013-11-25 06:13:32 +00:00
|
|
|
ob_append_byte(buff, sequence);
|
2012-11-23 05:55:32 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-11-21 02:22:39 +00:00
|
|
|
}
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2013-08-08 05:50:31 +00:00
|
|
|
int overlay_frame_append_payload(struct decode_context *context, int encapsulation,
|
2013-08-30 07:54:52 +00:00
|
|
|
struct overlay_frame *p, struct overlay_buffer *b,
|
|
|
|
char will_retransmit)
|
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-07-12 01:06:41 +00:00
|
|
|
ob_checkpoint(b);
|
|
|
|
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.packetconstruction){
|
2012-11-21 02:22:39 +00:00
|
|
|
DEBUGF( "+++++\nFrame from %s to %s of type 0x%02x %s:",
|
2013-10-09 08:24:21 +00:00
|
|
|
alloca_tohex_sid_t(p->source->sid),
|
|
|
|
alloca_tohex_sid_t(p->destination->sid),p->type,
|
2012-11-21 02:22:39 +00:00
|
|
|
"append_payload stuffing into packet");
|
|
|
|
if (p->payload)
|
2013-12-10 06:33:30 +00:00
|
|
|
dump("payload contents", &p->payload->bytes[0], ob_position(p->payload));
|
2012-11-21 02:22:39 +00:00
|
|
|
}
|
|
|
|
|
2012-12-19 02:06:28 +00:00
|
|
|
struct broadcast *broadcast=NULL;
|
|
|
|
if ((!p->destination) && !is_all_matching(p->broadcast_id.id,BROADCAST_LEN,0)){
|
|
|
|
broadcast = &p->broadcast_id;
|
|
|
|
}
|
2013-08-30 07:54:52 +00:00
|
|
|
|
2013-05-24 04:22:31 +00:00
|
|
|
if (overlay_frame_build_header(p->packet_version, context, b,
|
2013-08-30 07:54:52 +00:00
|
|
|
p->queue, p->type, p->modifiers, will_retransmit,
|
|
|
|
p->ttl, p->mdp_sequence&0xFF,
|
2012-12-19 02:06:28 +00:00
|
|
|
broadcast, p->next_hop,
|
2013-11-25 06:13:32 +00:00
|
|
|
p->destination, p->source) == -1)
|
2012-11-23 05:55:32 +00:00
|
|
|
goto cleanup;
|
2012-07-12 01:06:41 +00:00
|
|
|
|
2013-11-25 06:13:32 +00:00
|
|
|
if (encapsulation == ENCAP_OVERLAY)
|
|
|
|
ob_append_ui16(b, ob_position(p->payload));
|
2013-07-15 05:59:14 +00:00
|
|
|
|
2013-11-25 06:13:32 +00:00
|
|
|
if (ob_position(p->payload))
|
|
|
|
ob_append_bytes(b, ob_ptr(p->payload), ob_position(p->payload));
|
|
|
|
|
|
|
|
if (!ob_overrun(b))
|
|
|
|
return 0;
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
cleanup:
|
|
|
|
ob_rewind(b);
|
|
|
|
return -1;
|
2011-08-08 14:41:46 +00:00
|
|
|
}
|
2013-02-14 03:48:56 +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 */
|
2013-11-25 06:13:32 +00:00
|
|
|
struct overlay_frame *out = emalloc(sizeof(struct overlay_frame));
|
|
|
|
if (out == NULL)
|
|
|
|
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
|
|
|
|
2013-11-25 06:13:32 +00:00
|
|
|
if (in->payload) {
|
|
|
|
if ((out->payload = ob_dup(in->payload)) == NULL) {
|
|
|
|
free(out);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-04-14 01:26:03 +00:00
|
|
|
return out;
|
|
|
|
}
|