2013-03-18 00:04:29 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013, Matthias Schiffer <mschiffer@universe-factory.net>
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "mmss.h"
|
|
|
|
|
2013-03-18 01:36:34 +01:00
|
|
|
#include <stdlib.h>
|
2013-03-18 00:04:29 +01:00
|
|
|
|
|
|
|
|
2013-03-18 03:34:12 +01:00
|
|
|
gmrf_addr_t gmrf_iface_get_addr(gmrf_t *gmrf, gmrf_iface_t *iface) {
|
2013-03-18 00:04:29 +01:00
|
|
|
return iface->address;
|
|
|
|
}
|
|
|
|
|
2013-03-18 03:34:12 +01:00
|
|
|
size_t gmrf_iface_get_mtu(gmrf_t *gmrf, gmrf_iface_t *iface) {
|
2013-03-18 00:04:29 +01:00
|
|
|
return iface->net->mtu;
|
|
|
|
}
|
|
|
|
|
2013-03-18 03:34:12 +01:00
|
|
|
static void enqueue(mmss_t *mmss, gmrf_iface_t *source, gmrf_iface_t *dest, const void *data, size_t len) {
|
2013-03-18 01:36:34 +01:00
|
|
|
mmss_packet_t *packet = calloc(1, sizeof(mmss_packet_t) + len);
|
|
|
|
|
2013-03-18 03:34:12 +01:00
|
|
|
packet->sent = mmss->now;
|
2013-03-18 01:36:34 +01:00
|
|
|
packet->source = source;
|
|
|
|
packet->dest = dest;
|
|
|
|
packet->len = len;
|
|
|
|
memcpy(packet->data, data, len);
|
|
|
|
|
2013-03-18 03:34:12 +01:00
|
|
|
mmss_queue_put(mmss, &mmss->packet_queue, packet, mmss->now+1);
|
2013-03-18 01:36:34 +01:00
|
|
|
}
|
|
|
|
|
2013-03-18 03:34:12 +01:00
|
|
|
bool gmrf_iface_send(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, size_t len, const gmrf_addr_t *dest) {
|
2013-03-18 01:36:34 +01:00
|
|
|
gmrf_iface_t *dest_iface;
|
|
|
|
for (dest_iface = iface->net->interfaces; dest_iface; dest_iface = dest_iface->network_next) {
|
|
|
|
if (gmrf_addr_equal(&dest_iface->address, dest)) {
|
2013-03-18 03:34:12 +01:00
|
|
|
enqueue(gmrf->mmss, iface, dest_iface, data, len);
|
2013-03-18 01:36:34 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-03-18 00:04:29 +01:00
|
|
|
}
|
|
|
|
|
2013-03-18 03:34:12 +01:00
|
|
|
bool gmrf_iface_send_bc(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, size_t len) {
|
2013-03-18 01:36:34 +01:00
|
|
|
gmrf_iface_t *dest_iface;
|
|
|
|
for (dest_iface = iface->net->interfaces; dest_iface; dest_iface = dest_iface->network_next)
|
2013-03-18 03:34:12 +01:00
|
|
|
enqueue(gmrf->mmss, iface, dest_iface, data, len);
|
2013-03-18 01:36:34 +01:00
|
|
|
|
|
|
|
return true;
|
2013-03-18 00:04:29 +01:00
|
|
|
}
|
2013-03-18 18:06:24 +01:00
|
|
|
|
|
|
|
void mmss_dispatch(mmss_packet_t *packet) {
|
|
|
|
packet->dest->node->proto->handle_packet(packet->dest->node, packet->dest->node->ctx, packet->dest,
|
|
|
|
&packet->source->address, packet->data, packet->len);
|
|
|
|
free(packet);
|
|
|
|
}
|