]> git.meshlink.io Git - meshlink/blob - src/route.c
Fix memory leaks from timers.
[meshlink] / src / route.c
1 /*
2     route.c -- routing
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "logger.h"
23 #include "meshlink_internal.h"
24 #include "net.h"
25 #include "route.h"
26 #include "utils.h"
27
28 static bool checklength(node_t *source, vpn_packet_t *packet, uint16_t length) {
29         if(packet->len < length) {
30                 logger(source->mesh, MESHLINK_WARNING, "Got too short packet from %s", source->name);
31                 return false;
32         } else {
33                 return true;
34         }
35 }
36
37 void route(meshlink_handle_t *mesh, node_t *source, vpn_packet_t *packet) {
38         // TODO: route on name or key
39
40         meshlink_packethdr_t *hdr = (meshlink_packethdr_t *) packet->data;
41         node_t *dest = lookup_node(mesh, (char *)hdr->destination);
42         logger(mesh, MESHLINK_DEBUG, "Routing packet from \"%s\" to \"%s\"\n", hdr->source, hdr->destination);
43
44         //Check Length
45         if(!checklength(source, packet, sizeof(*hdr))) {
46                 return;
47         }
48
49         if(dest == NULL) {
50                 //Lookup failed
51                 logger(mesh, MESHLINK_WARNING, "Can't lookup the destination of a packet in the route() function. This should never happen!\n");
52                 logger(mesh, MESHLINK_WARNING, "Destination was: %s\n", hdr->destination);
53                 return;
54         }
55
56         if(dest == mesh->self) {
57                 const void *payload = packet->data + sizeof(*hdr);
58                 size_t len = packet->len - sizeof(*hdr);
59
60                 char hex[len * 2 + 1];
61
62                 if(mesh->log_level <= MESHLINK_DEBUG) {
63                         bin2hex(payload, hex, len);        // don't do this unless it's going to be logged
64                 }
65
66                 logger(mesh, MESHLINK_DEBUG, "I received a packet for me with payload: %s\n", hex);
67
68                 if(mesh->receive_cb) {
69                         mesh->receive_cb(mesh, (meshlink_node_t *)source, payload, len);
70                 }
71
72                 return;
73         }
74
75         if(!dest->status.reachable) {
76                 //TODO: check what to do here, not just print a warning
77                 logger(mesh, MESHLINK_WARNING, "The destination of a packet in the route() function is unreachable. Dropping packet.\n");
78                 return;
79         }
80
81         if(dest == source) {
82                 logger(mesh, MESHLINK_ERROR, "Routing loop for packet from %s!", source->name);
83                 return;
84         }
85
86         send_packet(mesh, dest, packet);
87         return;
88 }