4 Copyright (C) 2003-2004 Guus Sliepen <guus@tinc-vpn.org>,
5 2003-2004 Ivo Timmermans <ivo@tinc-vpn.org>
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.
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.
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.
30 #include "rt/subnet.h"
31 #include "support/xalloc.h"
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;
44 static bool rt_tnl_accept(tnl_t *t) {
47 static bool rt_vnd_recv(vnd_t *vnd, const char *buf, int len) {
48 route(myself, buf, len);
51 static bool rt_tnl_recv_packet(tnl_t *tnl, const char *buf, int len) {
52 route(tnl->data, buf, len);
55 static bool rt_tnl_recv_meta(tnl_t *tnl, const char *buf, int len) {
56 //route(tnl->data, buf, len);
59 static void rt_outgoing(char *wft) {
62 static void route(node_t *node, char *buf, int len) {
66 char *bindtoaddress = NULL;
67 char *bindtointerface = NULL;
73 struct addrinfo hint, *ai, *aip;
76 char *connectto = NULL;
78 cfg_choice_t mode_choice[] = {
79 {"Router", RT_MODE_ROUTER},
80 {"Switch", RT_MODE_SWITCH},
84 cfg_choice_t af_choice[] = {
90 logger(LOG_INFO, _("rt: initialising"));
92 if(!subnet_init() || !node_init() || !edge_init())
95 /* Read main configuration */
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))
109 /* Read host configuration for myself */
111 if(!cfg_get_string(myself->cfg, "Port", "655", &port))
114 for(cfg = cfg_get(myself->cfg, "Subnet"); cfg; cfg = cfg_get_next(myself->cfg, cfg)) {
115 if(!cfg_subnet(cfg, &subnet))
118 subnet->owner = myself;
122 /* Open the virtual network device */
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)) {
131 rt_vnd->mode = (rt_mode == RT_MODE_ROUTER) ? VND_MODE_TUN : VND_MODE_TAP;
132 rt_vnd->recv = rt_vnd_recv;
134 if(!vnd_open(rt_vnd)) {
139 /* Create listening sockets */
141 hint.ai_family = rt_af;
142 hint.ai_socktype = SOCK_STREAM;
143 hint.ai_protocol = IPPROTO_TCP;
144 hint.ai_flags = AI_PASSIVE;
146 err = getaddrinfo(bindtoaddress, port, &hint, &ai);
149 logger(LOG_ERR, _("rt: system call '%s' failed: %s"), "getaddrinfo", gai_strerror(err));
155 for(aip = ai; aip; aip = aip->ai_next) {
156 tnl_listen_t *listener;
158 clear(new(listener));
159 listener->local.address = *(struct sockaddr_storage *)aip->ai_addr;
160 listener->local.id = myself;
161 // listener->local.cred = ...;
163 if(tnl_listen(listener))
170 logger(LOG_ERR, _("rt: unable to create any listening socket!"));
174 /* Setup outgoing connections */
176 for(cfg = cfg_get(tinc_cfg, "ConnectTo"); cfg; cfg = cfg_get_next(tinc_cfg, cfg)) {
177 if(!cfg_string(cfg, NULL, &connectto))
180 if(!node_validname(connectto)) {
181 logger(LOG_ERR, _("rt: invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
186 rt_outgoing(connectto);
197 logger(LOG_INFO, _("rt: exitting"));