4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
19 * Anytun is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
37 #include "datatypes.h"
40 Options
* Options::inst
= NULL
;
41 Mutex
Options::instMutex
;
42 Options
& gOpt
= Options::instance();
44 Options
& Options::instance()
47 static instanceCleaner c
;
54 Options::Options() : key_(u_int32_t(0)), salt_(u_int32_t(0))
60 chroot_dir_
= "/var/run/anytun";
66 remote_sync_port_
= 0;
67 remote_sync_addr_
= "";
72 ifconfig_param_local_
= "";
73 ifconfig_param_remote_netmask_
= "";
75 seq_window_size_
= 100;
86 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
87 else if(str == SHORT || str == LONG) \
90 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
91 else if(str == SHORT || str == LONG) \
94 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
95 else if(str == SHORT || str == LONG) \
97 if(argc < 1 || argv[i+1][0] == '-') \
99 std::stringstream tmp; \
106 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
107 else if(str == SHORT || str == LONG) \
110 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
112 std::stringstream tmp; \
113 tmp << argv[i+1] << " " << argv[i+2]; \
120 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \
121 else if(str == SHORT || str == LONG) \
123 if(argc < 1 || argv[i+1][0] == '-') \
125 VALUE = Buffer(std::string(argv[i+1])); \
126 for(size_t j=0; j < strlen(argv[i+1]); ++j) \
127 argv[i+1][j] = '#'; \
132 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
133 else if(str == SHORT || str == LONG) \
135 if(argc < 1 || argv[i+1][0] == '-') \
137 std::stringstream tmp(argv[i+1]); \
140 std::string tmp_line; \
141 getline(tmp,tmp_line,','); \
142 LIST.push(tmp_line); \
148 bool Options::parse(int argc
, char* argv
[])
154 std::queue
<std::string
> host_port_queue
;
155 for(int i
=1; argc
> 0; ++i
)
157 std::string
str(argv
[i
]);
160 if(str
== "-h" || str
== "--help")
162 PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", daemonize_
)
163 PARSE_BOOL_PARAM("-C","--chroot", chroot_
)
164 PARSE_SCALAR_PARAM("-u","--username", username_
)
165 PARSE_SCALAR_PARAM("-H","--chroot-dir", chroot_dir_
)
166 PARSE_SCALAR_PARAM("-P","--write-pid", pid_file_
)
167 PARSE_SCALAR_PARAM("-s","--sender-id", sender_id_
)
168 PARSE_SCALAR_PARAM("-i","--interface", local_addr_
)
169 PARSE_SCALAR_PARAM("-p","--port", local_port_
)
170 PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_
)
171 PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_
)
172 PARSE_SCALAR_PARAM("-R","--remote-sync-host", remote_sync_addr_
)
173 PARSE_SCALAR_PARAM("-O","--remote-sync-port", remote_sync_port_
)
174 PARSE_SCALAR_PARAM("-r","--remote-host", remote_addr_
)
175 PARSE_SCALAR_PARAM("-o","--remote-port", remote_port_
)
176 PARSE_SCALAR_PARAM("-d","--dev", dev_name_
)
177 PARSE_SCALAR_PARAM("-t","--type", dev_type_
)
178 PARSE_SCALAR_PARAM2("-n","--ifconfig", ifconfig_param_local_
, ifconfig_param_remote_netmask_
)
179 PARSE_SCALAR_PARAM("-x","--post-up-script", post_up_script_
)
180 PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_
)
181 PARSE_SCALAR_PARAM("-m","--mux", mux_
)
182 PARSE_SCALAR_PARAM("-c","--cipher", cipher_
)
183 PARSE_HEXSTRING_PARAM_SEC("-K","--key", key_
)
184 PARSE_HEXSTRING_PARAM_SEC("-A","--salt", salt_
)
185 PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_
)
186 PARSE_SCALAR_PARAM("-a","--auth-algo", auth_algo_
)
187 PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue
)
188 PARSE_CSLIST_PARAM("-X","--control-host", host_port_queue
)
193 if(cipher_
== "null" && auth_algo_
== "null")
195 if((cipher_
!= "null" || auth_algo_
!= "null") && kd_prf_
== "null")
198 if(dev_name_
== "" && dev_type_
== "")
201 while(!host_port_queue
.empty())
203 std::stringstream
tmp_stream(host_port_queue
.front());
205 getline(tmp_stream
,oct
.host
,':');
206 if(!tmp_stream
.good())
208 tmp_stream
>> oct
.port
;
209 host_port_queue
.pop();
210 connect_to_
.push_back(oct
);
215 void Options::printUsage()
217 std::cout
<< "USAGE:" << std::endl
;
218 std::cout
<< "anytun [-h|--help] prints this..." << std::endl
;
219 // std::cout << " [-f|--config] <file> the config file" << std::endl;
220 std::cout
<< " [-D|--nodaemonize] don't run in background" << std::endl
;
221 std::cout
<< " [-C|--chroot] chroot and drop privileges" << std::endl
;
222 std::cout
<< " [-u|--username] <username> if chroot change to this user" << std::endl
;
223 std::cout
<< " [-H|--chroot-dir] <path> chroot to this directory" << std::endl
;
224 std::cout
<< " [-P|--write-pid] <path> write pid to this file" << std::endl
;
225 std::cout
<< " [-s|--sender-id ] <sender id> the sender id to use" << std::endl
;
226 std::cout
<< " [-i|--interface] <ip-address> local anycast ip address to bind to" << std::endl
;
227 std::cout
<< " [-p|--port] <port> local anycast(data) port to bind to" << std::endl
;
228 std::cout
<< " [-I|--sync-interface] <ip-address> local unicast(sync) ip address to bind to" << std::endl
;
229 std::cout
<< " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl
;
230 std::cout
<< " [-M|--sync-hosts] <hostname|ip>:<port>[,<hostname|ip>:<port>[...]]"<< std::endl
;
231 std::cout
<< " remote hosts to sync with" << std::endl
;
232 std::cout
<< " [-X|--control-host] <hostname|ip>:<port>"<< std::endl
;
233 std::cout
<< " fetch the config from this host" << std::endl
;
234 std::cout
<< " [-r|--remote-host] <hostname|ip> remote host" << std::endl
;
235 std::cout
<< " [-o|--remote-port] <port> remote port" << std::endl
;
236 std::cout
<< " [-d|--dev] <name> device name" << std::endl
;
237 std::cout
<< " [-t|--type] <tun|tap> device type" << std::endl
;
238 std::cout
<< " [-n|--ifconfig] <local> the local address for the tun/tap device" << std::endl
239 << " <remote|netmask> the remote address(tun) or netmask(tap)" << std::endl
;
240 std::cout
<< " [-x|--post-up-script] <script> script gets called after interface is created" << std::endl
;
241 std::cout
<< " [-w|--window-size] <window size> seqence number window size" << std::endl
;
242 std::cout
<< " [-m|--mux] <mux-id> the multiplex id to use" << std::endl
;
243 std::cout
<< " [-c|--cipher] <cipher type> payload encryption algorithm" << std::endl
;
244 std::cout
<< " [-K|--key] <master key> master key to use for encryption" << std::endl
;
245 std::cout
<< " [-A|--salt] <master salt> master salt to use for encryption" << std::endl
;
246 // std::cout << " [-k|--kd-prf] <kd-prf type> key derivation pseudo random function" << std::endl;
247 std::cout
<< " [-a|--auth-algo] <algo type> message authentication algorithm" << std::endl
;
250 void Options::printOptions()
253 std::cout
<< "Options:" << std::endl
;
254 std::cout
<< "daemonize=" << daemonize_
<< std::endl
;
255 std::cout
<< "chroot=" << chroot_
<< std::endl
;
256 std::cout
<< "username='" << username_
<< "'" << std::endl
;
257 std::cout
<< "chroot_dir='" << chroot_dir_
<< "'" << std::endl
;
258 std::cout
<< "pid_file='" << pid_file_
<< "'" << std::endl
;
259 std::cout
<< "sender_id='" << sender_id_
<< "'" << std::endl
;
260 std::cout
<< "local_addr='" << local_addr_
<< "'" << std::endl
;
261 std::cout
<< "local_port='" << local_port_
<< "'" << std::endl
;
262 std::cout
<< "local_sync_addr='" << local_sync_addr_
<< "'" << std::endl
;
263 std::cout
<< "local_sync_port='" << local_sync_port_
<< "'" << std::endl
;
264 std::cout
<< "remote_addr='" << remote_addr_
<< "'" << std::endl
;
265 std::cout
<< "remote_port='" << remote_port_
<< "'" << std::endl
;
266 std::cout
<< "dev_name='" << dev_name_
<< "'" << std::endl
;
267 std::cout
<< "dev_type='" << dev_type_
<< "'" << std::endl
;
268 std::cout
<< "ifconfig_param_local='" << ifconfig_param_local_
<< "'" << std::endl
;
269 std::cout
<< "ifconfig_param_remote_netmask='" << ifconfig_param_remote_netmask_
<< "'" << std::endl
;
270 std::cout
<< "post_up_script='" << post_up_script_
<< "'" << std::endl
;
271 std::cout
<< "seq_window_size='" << seq_window_size_
<< "'" << std::endl
;
272 std::cout
<< "mux_id='" << mux_
<< "'" << std::endl
;
273 std::cout
<< "cipher='" << cipher_
<< "'" << std::endl
;
274 std::cout
<< "key=" << key_
.getHexDumpOneLine() << std::endl
;
275 std::cout
<< "salt=" << salt_
.getHexDumpOneLine() << std::endl
;
276 std::cout
<< "kd_prf='" << kd_prf_
<< "'" << std::endl
;
277 std::cout
<< "auth_algo='" << auth_algo_
<< "'" << std::endl
;
280 std::string
Options::getProgname()
287 Options
& Options::setProgname(std::string p
)
294 bool Options::getDaemonize()
299 Options
& Options::setDaemonize(bool d
)
305 bool Options::getChroot()
310 Options
& Options::setChroot(bool c
)
316 std::string
Options::getUsername()
322 Options
& Options::setUsername(std::string u
)
329 std::string
Options::getChrootDir()
335 Options
& Options::setChrootDir(std::string c
)
342 std::string
Options::getPidFile()
348 Options
& Options::setPidFile(std::string p
)
355 ConnectToList
Options::getConnectTo()
361 sender_id_t
Options::getSenderId()
366 Options
& Options::setSenderId(sender_id_t s
)
372 std::string
Options::getLocalAddr()
378 Options
& Options::setLocalAddr(std::string l
)
385 std::string
Options::getLocalSyncAddr()
388 return local_sync_addr_
;
391 Options
& Options::setLocalSyncAddr(std::string l
)
394 local_sync_addr_
= l
;
398 u_int16_t
Options::getLocalPort()
403 Options
& Options::setLocalPort(u_int16_t l
)
409 u_int16_t
Options::getLocalSyncPort()
411 return local_sync_port_
;
414 Options
& Options::setLocalSyncPort(u_int16_t l
)
416 local_sync_port_
= l
;
420 u_int16_t
Options::getRemoteSyncPort()
422 return remote_sync_port_
;
425 Options
& Options::setRemoteSyncPort(u_int16_t l
)
427 remote_sync_port_
= l
;
431 std::string
Options::getRemoteSyncAddr()
434 return remote_sync_addr_
;
437 Options
& Options::setRemoteSyncAddr(std::string r
)
440 remote_sync_addr_
= r
;
444 std::string
Options::getRemoteAddr()
450 Options
& Options::setRemoteAddr(std::string r
)
457 u_int16_t
Options::getRemotePort()
462 Options
& Options::setRemotePort(u_int16_t r
)
468 Options
& Options::setRemoteAddrPort(std::string addr
, u_int16_t port
)
476 std::string
Options::getDevName()
482 std::string
Options::getDevType()
488 Options
& Options::setDevName(std::string d
)
495 Options
& Options::setDevType(std::string d
)
502 std::string
Options::getIfconfigParamLocal()
505 return ifconfig_param_local_
;
508 Options
& Options::setIfconfigParamLocal(std::string i
)
511 ifconfig_param_local_
= i
;
515 std::string
Options::getIfconfigParamRemoteNetmask()
518 return ifconfig_param_remote_netmask_
;
521 Options
& Options::setIfconfigParamRemoteNetmask(std::string i
)
524 ifconfig_param_remote_netmask_
= i
;
528 std::string
Options::getPostUpScript()
531 return post_up_script_
;
534 Options
& Options::setPostUpScript(std::string p
)
541 window_size_t
Options::getSeqWindowSize()
543 return seq_window_size_
;
546 Options
& Options::setSeqWindowSize(window_size_t s
)
548 seq_window_size_
= s
;
552 std::string
Options::getCipher()
558 Options
& Options::setCipher(std::string c
)
565 std::string
Options::getKdPrf()
571 Options
& Options::setKdPrf(std::string k
)
578 std::string
Options::getAuthAlgo()
584 Options
& Options::setAuthAlgo(std::string a
)
591 u_int16_t
Options::getMux()
597 Options
& Options::setMux(u_int16_t m
)
604 Buffer
Options::getKey()
610 Options
& Options::setKey(std::string k
)
617 Buffer
Options::getSalt()
623 Options
& Options::setSalt(std::string s
)