svn cleanup
[anytun.git] / openvpn / route.h
blob7b7bf37ab91dee06da84d05e9174f4733dae9d68
1 /*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
8 * Copyright (C) 2002-2005 OpenVPN Solutions LLC <info@openvpn.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Support routines for adding/deleting network routes.
29 #ifndef ROUTE_H
30 #define ROUTE_H
32 #include "tun.h"
33 #include "misc.h"
35 #define MAX_ROUTES 100
37 #ifdef WIN32
39 * Windows route methods
41 #define ROUTE_METHOD_IPAPI 0 /* use IP helper API */
42 #define ROUTE_METHOD_EXE 1 /* use route.exe */
43 #define ROUTE_METHOD_MASK 1
44 #endif
47 * Route add flags (must stay clear of ROUTE_METHOD bits)
49 #define ROUTE_DELETE_FIRST 2
51 struct route_special_addr
53 in_addr_t remote_endpoint;
54 bool remote_endpoint_defined;
55 in_addr_t net_gateway;
56 bool net_gateway_defined;
57 in_addr_t remote_host;
58 bool remote_host_defined;
61 struct route_option {
62 const char *network;
63 const char *netmask;
64 const char *gateway;
65 const char *metric;
68 struct route_option_list {
69 int n;
70 bool redirect_default_gateway;
71 bool redirect_local;
72 bool redirect_def1;
73 struct route_option routes[MAX_ROUTES];
76 struct route {
77 bool defined;
78 const struct route_option *option;
79 in_addr_t network;
80 in_addr_t netmask;
81 in_addr_t gateway;
82 bool metric_defined;
83 int metric;
86 struct route_list {
87 bool routes_added;
88 struct route_special_addr spec;
89 bool redirect_default_gateway;
90 bool redirect_local;
91 bool redirect_def1;
92 bool did_redirect_default_gateway;
94 int n;
95 struct route routes[MAX_ROUTES];
98 #if P2MP
99 /* internal OpenVPN route */
100 struct iroute {
101 in_addr_t network;
102 int netbits;
103 struct iroute *next;
105 #endif
107 struct route_option_list *new_route_option_list (struct gc_arena *a);
109 struct route_list *new_route_list (struct gc_arena *a);
111 void add_route_to_option_list (struct route_option_list *l,
112 const char *network,
113 const char *netmask,
114 const char *gateway,
115 const char *metric);
117 void clear_route_list (struct route_list *rl);
119 bool init_route_list (struct route_list *rl,
120 const struct route_option_list *opt,
121 const char *remote_endpoint,
122 in_addr_t remote_host,
123 struct env_set *es);
125 void add_routes (struct route_list *rl,
126 const struct tuntap *tt,
127 unsigned int flags,
128 const struct env_set *es);
130 void delete_routes (struct route_list *rl,
131 const struct tuntap *tt,
132 unsigned int flags,
133 const struct env_set *es);
135 void setenv_routes (struct env_set *es, const struct route_list *rl);
137 #ifdef ENABLE_DEBUG
138 void print_route_options (const struct route_option_list *rol,
139 int level);
140 #endif
142 void print_routes (const struct route_list *rl, int level);
144 #ifdef WIN32
146 void show_routes (int msglev);
147 bool test_routes (const struct route_list *rl, const struct tuntap *tt);
148 bool add_route_ipapi (const struct route *r, const struct tuntap *tt);
149 bool del_route_ipapi (const struct route *r, const struct tuntap *tt);
151 #else
152 static inline bool test_routes (const struct route_list *rl, const struct tuntap *tt) { return true; }
153 #endif
155 bool netmask_to_netbits (const in_addr_t network, const in_addr_t netmask, int *netbits);
157 static inline in_addr_t
158 netbits_to_netmask (const int netbits)
160 const int addrlen = sizeof (in_addr_t) * 8;
161 in_addr_t mask = 0;
162 if (netbits > 0 && netbits <= addrlen)
163 mask = ~0 << (addrlen-netbits);
164 return mask;
167 #endif