Imported start of brand new codebase for tinc 2.0.
[tinc] / rt / rt.c
1 /*
2     rt.c -- routing
3
4     Copyright (C) 2003-2004 Guus Sliepen <guus@tinc-vpn.org>,
5                   2003-2004 Ivo Timmermans <ivo@tinc-vpn.org>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21     $Id$
22 */
23
24 #include "system.h"
25
26 #include "cfg/cfg.h"
27 #include "rt/edge.h"
28 #include "rt/node.h"
29 #include "rt/rt.h"
30 #include "rt/subnet.h"
31 #include "support/xalloc.h"
32 #include "tnl/tnl.h"
33 #include "vnd/vnd.h"
34 #include "tincd.h"
35
36 vnd_t *rt_vnd = NULL;
37 int rt_af = AF_UNSPEC;
38 int rt_macexpire = 600;
39 int rt_maxtimeout = 900;
40 rt_mode_t rt_mode = RT_MODE_ROUTER;
41 bool rt_priorityinheritance = false;
42 bool rt_hostnames = false;
43
44 static bool rt_tnl_accept(tnl_t *t) {
45 }
46
47 static bool rt_vnd_recv(vnd_t *vnd, const char *buf, int len) {
48         route(myself, buf, len);
49 }
50
51 static bool rt_tnl_recv_packet(tnl_t *tnl, const char *buf, int len) {
52         route(tnl->data, buf, len);
53 }
54
55 static bool rt_tnl_recv_meta(tnl_t *tnl, const char *buf, int len) {
56         //route(tnl->data, buf, len);
57 }
58
59 static void rt_outgoing(char *wft) {
60 }
61
62 static void route(node_t *node, char *buf, int len) {
63 }
64
65 bool rt_init(void) {
66         char *bindtoaddress = NULL;
67         char *bindtointerface = NULL;
68         char *device = NULL;
69         char *iface = NULL;
70         char *port = NULL;
71         cfg_t *cfg;
72         subnet_t *subnet;
73         struct addrinfo hint, *ai, *aip;
74         int err;
75         int listeners;
76         char *connectto = NULL;
77         
78         cfg_choice_t mode_choice[] = {
79                 {"Router", RT_MODE_ROUTER},
80                 {"Switch", RT_MODE_SWITCH},
81                 {"Hub", RT_MODE_HUB},
82         };
83
84         cfg_choice_t af_choice[] = {
85                 {"IPv4", AF_INET},
86                 {"IPv6", AF_INET6},
87                 {"Any", AF_UNSPEC},
88         };
89
90         logger(LOG_INFO, _("rt: initialising"));
91
92         if(!subnet_init() || !node_init() || !edge_init())
93                 return false;
94
95         /* Read main configuration */
96
97         if(!cfg_get_choice(tinc_cfg, "AddressFamily", af_choice, AF_UNSPEC, &rt_af)
98                         || !cfg_get_string(tinc_cfg, "BindToAddress", NULL, &bindtoaddress)
99                         || !cfg_get_string(tinc_cfg, "BindToInterface", NULL, &bindtointerface)
100                         || !cfg_get_string(tinc_cfg, "Device", "/dev/net/tun", &device)
101                         || !cfg_get_bool(tinc_cfg, "Hostnames", false, &rt_hostnames)
102                         || !cfg_get_string(tinc_cfg, "Interface", tinc_netname, &iface)
103                         || !cfg_get_period(tinc_cfg, "MACExpire", 600, &rt_macexpire)
104                         || !cfg_get_period(tinc_cfg, "MaxTimeout", 3600, &rt_maxtimeout)
105                         || !cfg_get_choice(tinc_cfg, "Mode", mode_choice, RT_MODE_ROUTER, &rt_mode)
106                         || !cfg_get_bool(tinc_cfg, "PriorityInheritance", false, &rt_priorityinheritance))
107                 return false;
108
109         /* Read host configuration for myself */
110         
111         if(!cfg_get_string(myself->cfg, "Port", "655", &port))
112                 return false;
113
114         for(cfg = cfg_get(myself->cfg, "Subnet"); cfg; cfg = cfg_get_next(myself->cfg, cfg)) {
115                 if(!cfg_subnet(cfg, &subnet))
116                         return false;
117
118                 subnet->owner = myself;
119                 subnet_add(subnet);
120         }
121
122         /* Open the virtual network device */
123         
124         if(!cfg_get_string(tinc_cfg, "Device", "/dev/net/tun", &rt_vnd->device)
125                         || !cfg_get_string(tinc_cfg, "Interface", tinc_netname, &rt_vnd->interface)
126                         || !cfg_get_choice(tinc_cfg, "Mode", mode_choice, RT_MODE_ROUTER, rt_mode)) {
127                 vnd_free(rt_vnd);
128                 return false;
129         }
130         
131         rt_vnd->mode = (rt_mode == RT_MODE_ROUTER) ? VND_MODE_TUN : VND_MODE_TAP;
132         rt_vnd->recv = rt_vnd_recv;
133
134         if(!vnd_open(rt_vnd)) {
135                 vnd_free(rt_vnd);
136                 return false;
137         }
138         
139         /* Create listening sockets */
140
141         hint.ai_family = rt_af;
142         hint.ai_socktype = SOCK_STREAM;
143         hint.ai_protocol = IPPROTO_TCP;
144         hint.ai_flags = AI_PASSIVE;
145
146         err = getaddrinfo(bindtoaddress, port, &hint, &ai);
147
148         if(err || !ai) {
149                 logger(LOG_ERR, _("rt: system call '%s' failed: %s"), "getaddrinfo", gai_strerror(err));
150                 return false;
151         }
152
153         listeners = 0;
154
155         for(aip = ai; aip; aip = aip->ai_next) {
156                 tnl_listen_t *listener;
157                 
158                 clear(new(listener));
159                 listener->local.address = *(struct sockaddr_storage *)aip->ai_addr;
160                 listener->local.id = myself;
161                 // listener->local.cred = ...;
162
163                 if(tnl_listen(listener))
164                         listeners++;
165         }
166
167         freeaddrinfo(ai);
168
169         if(!listeners) {
170                 logger(LOG_ERR, _("rt: unable to create any listening socket!"));
171                 return false;
172         }
173
174         /* Setup outgoing connections */
175
176         for(cfg = cfg_get(tinc_cfg, "ConnectTo"); cfg; cfg = cfg_get_next(tinc_cfg, cfg)) {
177                 if(!cfg_string(cfg, NULL, &connectto))
178                         return false;
179
180                 if(!node_validname(connectto)) {
181                         logger(LOG_ERR, _("rt: invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
182                         free(connectto);
183                         continue;
184                 }
185
186                 rt_outgoing(connectto);
187         }
188
189         return true;
190 }
191
192 bool rt_exit(void) {
193         edge_exit();
194         node_exit();
195         subnet_exit();
196
197         logger(LOG_INFO, _("rt: exitting"));
198 }
199
200