]> git.meshlink.io Git - meshlink/blob - src/route.c
Refactory of route() function to have just one like in tinc1.1
[meshlink] / src / route.c
1 /*
2     route.c -- routing
3     Copyright (C) 2000-2005 Ivo Timmermans,
4                   2000-2013 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "logger.h"
24 #include "net.h"
25 #include "route.h"
26 #include "utils.h"
27 #include "libmeshlink.h"
28
29 rmode_t routing_mode = RMODE_ROUTER;
30 fmode_t forwarding_mode = FMODE_INTERNAL;
31 bmode_t broadcast_mode = BMODE_MST;
32 bool decrement_ttl = false;
33 bool directonly = false;
34 bool priorityinheritance = false;
35 int macexpire = 600;
36 bool overwrite_mac = false;
37 mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
38 bool pcap = false;
39
40 static bool ratelimit(int frequency) {
41         static time_t lasttime = 0;
42         static int count = 0;
43
44         if(lasttime == now.tv_sec) {
45                 if(count >= frequency)
46                         return true;
47         } else {
48                 lasttime = now.tv_sec;
49                 count = 0;
50         }
51
52         count++;
53         return false;
54 }
55
56 static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
57         if(packet->len < length) {
58                 logger(DEBUG_TRAFFIC, LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
59                 return false;
60         } else
61                 return true;
62 }
63
64 void route(node_t *source,vpn_packet_t *packet) {
65     // TODO: route on name or key
66
67     node_t* owner = NULL;
68     tincpackethdr* hdr = (tincpackethdr*)packet->data;
69     owner = lookup_node(hdr->destination);
70     if (owner == NULL) {
71     //Lookup failed
72     logger(DEBUG_TRAFFIC, LOG_WARNING, "Cant lookup the owner of a packet in the route() function. This should never happen \n");
73     return;
74     }
75
76     if (owner == myself ) {
77     //TODO: implement sending received data from meshlink library to the application
78     logger(DEBUG_TRAFFIC, LOG_WARNING, "I received a packet for me with payload: %s \n", packet->data + 46);
79     return;
80     }
81
82     if(!owner->status.reachable) {
83     //TODO: check what to do here, not just print a warning
84     logger(DEBUG_TRAFFIC, LOG_WARNING, "The owner of a packet in the route() function is unreachable. Dropping packet. \n");
85     return;
86     }
87
88     //TODO: I skipped here a lot of checks !
89
90     send_packet(owner,packet);
91     return;
92 }