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 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING included with this
27 * distribution); if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include "datatypes.h"
39 Options
* Options::inst
= NULL
;
40 Mutex
Options::instMutex
;
41 Options
& gOpt
= Options::instance();
43 Options
& Options::instance()
46 static instanceCleaner c
;
53 Options::Options() : key_(u_int32_t(0)), salt_(u_int32_t(0))
59 chroot_dir_
= "/var/run/anytun";
65 remote_sync_port_
= 0;
66 remote_sync_addr_
= "";
71 ifconfig_param_local_
= "";
72 ifconfig_param_remote_netmask_
= "";
74 seq_window_size_
= 100;
85 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
86 else if(str == SHORT || str == LONG) \
89 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
90 else if(str == SHORT || str == LONG) \
93 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
94 else if(str == SHORT || str == LONG) \
96 if(argc < 1 || argv[i+1][0] == '-') \
98 std::stringstream tmp; \
105 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
106 else if(str == SHORT || str == LONG) \
109 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
111 std::stringstream tmp; \
112 tmp << argv[i+1] << " " << argv[i+2]; \
119 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \
120 else if(str == SHORT || str == LONG) \
122 if(argc < 1 || argv[i+1][0] == '-') \
124 VALUE = Buffer(std::string(argv[i+1])); \
125 for(size_t j=0; j < strlen(argv[i+1]); ++j) \
126 argv[i+1][j] = '#'; \
131 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
132 else if(str == SHORT || str == LONG) \
134 if(argc < 1 || argv[i+1][0] == '-') \
136 std::stringstream tmp(argv[i+1]); \
139 std::string tmp_line; \
140 getline(tmp,tmp_line,','); \
141 LIST.push(tmp_line); \
147 bool Options::parse(int argc
, char* argv
[])
153 std::queue
<std::string
> host_port_queue
;
154 for(int i
=1; argc
> 0; ++i
)
156 std::string
str(argv
[i
]);
159 if(str
== "-h" || str
== "--help")
161 PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", daemonize_
)
162 PARSE_BOOL_PARAM("-C","--chroot", chroot_
)
163 PARSE_SCALAR_PARAM("-u","--username", username_
)
164 PARSE_SCALAR_PARAM("-H","--chroot-dir", chroot_dir_
)
165 PARSE_SCALAR_PARAM("-P","--write-pid", pid_file_
)
166 PARSE_SCALAR_PARAM("-s","--sender-id", sender_id_
)
167 PARSE_SCALAR_PARAM("-i","--interface", local_addr_
)
168 PARSE_SCALAR_PARAM("-p","--port", local_port_
)
169 PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_
)
170 PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_
)
171 PARSE_SCALAR_PARAM("-R","--remote-sync-host", remote_sync_addr_
)
172 PARSE_SCALAR_PARAM("-O","--remote-sync-port", remote_sync_port_
)
173 PARSE_SCALAR_PARAM("-r","--remote-host", remote_addr_
)
174 PARSE_SCALAR_PARAM("-o","--remote-port", remote_port_
)
175 PARSE_SCALAR_PARAM("-d","--dev", dev_name_
)
176 PARSE_SCALAR_PARAM("-t","--type", dev_type_
)
177 PARSE_SCALAR_PARAM2("-n","--ifconfig", ifconfig_param_local_
, ifconfig_param_remote_netmask_
)
178 PARSE_SCALAR_PARAM("-x","--post-up-script", post_up_script_
)
179 PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_
)
180 PARSE_SCALAR_PARAM("-m","--mux", mux_
)
181 PARSE_SCALAR_PARAM("-c","--cipher", cipher_
)
182 PARSE_HEXSTRING_PARAM_SEC("-K","--key", key_
)
183 PARSE_HEXSTRING_PARAM_SEC("-A","--salt", salt_
)
184 PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_
)
185 PARSE_SCALAR_PARAM("-a","--auth-algo", auth_algo_
)
186 PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue
)
187 PARSE_CSLIST_PARAM("-X","--control-host", host_port_queue
)
192 if(cipher_
== "null" && auth_algo_
== "null")
194 if((cipher_
!= "null" || auth_algo_
!= "null") && kd_prf_
== "null")
197 while(!host_port_queue
.empty())
199 std::stringstream
tmp_stream(host_port_queue
.front());
201 getline(tmp_stream
,oct
.host
,':');
202 if(!tmp_stream
.good())
204 tmp_stream
>> oct
.port
;
205 host_port_queue
.pop();
206 connect_to_
.push_back(oct
);
211 void Options::printUsage()
213 std::cout
<< "USAGE:" << std::endl
;
214 std::cout
<< "anytun [-h|--help] prints this..." << std::endl
;
215 // std::cout << " [-f|--config] <file> the config file" << std::endl;
216 std::cout
<< " [-D|--nodaemonize] don't run in background" << std::endl
;
217 std::cout
<< " [-C|--chroot] chroot and drop privileges" << std::endl
;
218 std::cout
<< " [-u|--username] if chroot change to this user" << std::endl
;
219 std::cout
<< " [-H|--chroot-dir] chroot to this directory" << std::endl
;
220 std::cout
<< " [-P|--write-pid] write pid to this file" << std::endl
;
221 std::cout
<< " [-s|--sender-id ] <sender id> the sender id to use" << std::endl
;
222 std::cout
<< " [-i|--interface] <ip-address> local anycast ip address to bind to" << std::endl
;
223 std::cout
<< " [-p|--port] <port> local anycast(data) port to bind to" << std::endl
;
224 std::cout
<< " [-I|--sync-interface] <ip-address> local unicast(sync) ip address to bind to" << std::endl
;
225 std::cout
<< " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl
;
226 std::cout
<< " [-M|--sync-hosts] <hostname|ip>:<port>[,<hostname|ip>:<port>[...]]"<< std::endl
;
227 std::cout
<< " remote hosts to sync with" << std::endl
;
228 std::cout
<< " [-X|--control-host] <hostname|ip>:<port>"<< std::endl
;
229 std::cout
<< " fetch the config from this host" << std::endl
;
230 std::cout
<< " [-r|--remote-host] <hostname|ip> remote host" << std::endl
;
231 std::cout
<< " [-o|--remote-port] <port> remote port" << std::endl
;
232 std::cout
<< " [-d|--dev] <name> device name" << std::endl
;
233 std::cout
<< " [-t|--type] <tun|tap> device type" << std::endl
;
234 std::cout
<< " [-n|--ifconfig] <local> the local address for the tun/tap device" << std::endl
235 << " <remote|netmask> the remote address(tun) or netmask(tap)" << std::endl
;
236 std::cout
<< " [-x|--post-up-script] <script> script gets called after interface is created" << std::endl
;
237 std::cout
<< " [-w|--window-size] <window size> seqence number window size" << std::endl
;
238 std::cout
<< " [-m|--mux] <mux-id> the multiplex id to use" << std::endl
;
239 std::cout
<< " [-c|--cipher] <cipher type> payload encryption algorithm" << std::endl
;
240 std::cout
<< " [-K|--key] <master key> master key to use for encryption" << std::endl
;
241 std::cout
<< " [-A|--salt] <master salt> master salt to use for encryption" << std::endl
;
242 // std::cout << " [-k|--kd-prf] <kd-prf type> key derivation pseudo random function" << std::endl;
243 std::cout
<< " [-a|--auth-algo] <algo type> message authentication algorithm" << std::endl
;
246 void Options::printOptions()
249 std::cout
<< "Options:" << std::endl
;
250 std::cout
<< "daemonize=" << daemonize_
<< std::endl
;
251 std::cout
<< "chroot=" << chroot_
<< std::endl
;
252 std::cout
<< "username='" << username_
<< "'" << std::endl
;
253 std::cout
<< "chroot_dir='" << chroot_dir_
<< "'" << std::endl
;
254 std::cout
<< "pid_file='" << pid_file_
<< "'" << std::endl
;
255 std::cout
<< "sender_id='" << sender_id_
<< "'" << std::endl
;
256 std::cout
<< "local_addr='" << local_addr_
<< "'" << std::endl
;
257 std::cout
<< "local_port='" << local_port_
<< "'" << std::endl
;
258 std::cout
<< "local_sync_addr='" << local_sync_addr_
<< "'" << std::endl
;
259 std::cout
<< "local_sync_port='" << local_sync_port_
<< "'" << std::endl
;
260 std::cout
<< "remote_addr='" << remote_addr_
<< "'" << std::endl
;
261 std::cout
<< "remote_port='" << remote_port_
<< "'" << std::endl
;
262 std::cout
<< "dev_name='" << dev_name_
<< "'" << std::endl
;
263 std::cout
<< "dev_type='" << dev_type_
<< "'" << std::endl
;
264 std::cout
<< "ifconfig_param_local='" << ifconfig_param_local_
<< "'" << std::endl
;
265 std::cout
<< "ifconfig_param_remote_netmask='" << ifconfig_param_remote_netmask_
<< "'" << std::endl
;
266 std::cout
<< "post_up_script='" << post_up_script_
<< "'" << std::endl
;
267 std::cout
<< "seq_window_size='" << seq_window_size_
<< "'" << std::endl
;
268 std::cout
<< "mux_id='" << mux_
<< "'" << std::endl
;
269 std::cout
<< "cipher='" << cipher_
<< "'" << std::endl
;
270 std::cout
<< "key=" << key_
.getHexDumpOneLine() << std::endl
;
271 std::cout
<< "salt=" << salt_
.getHexDumpOneLine() << std::endl
;
272 std::cout
<< "kd_prf='" << kd_prf_
<< "'" << std::endl
;
273 std::cout
<< "auth_algo='" << auth_algo_
<< "'" << std::endl
;
276 std::string
Options::getProgname()
283 Options
& Options::setProgname(std::string p
)
290 bool Options::getDaemonize()
295 Options
& Options::setDaemonize(bool d
)
301 bool Options::getChroot()
306 Options
& Options::setChroot(bool c
)
312 std::string
Options::getUsername()
318 Options
& Options::setUsername(std::string u
)
325 std::string
Options::getChrootDir()
331 Options
& Options::setChrootDir(std::string c
)
338 std::string
Options::getPidFile()
344 Options
& Options::setPidFile(std::string p
)
351 ConnectToList
Options::getConnectTo()
357 sender_id_t
Options::getSenderId()
362 Options
& Options::setSenderId(sender_id_t s
)
368 std::string
Options::getLocalAddr()
374 Options
& Options::setLocalAddr(std::string l
)
381 std::string
Options::getLocalSyncAddr()
384 return local_sync_addr_
;
387 Options
& Options::setLocalSyncAddr(std::string l
)
390 local_sync_addr_
= l
;
394 u_int16_t
Options::getLocalPort()
399 Options
& Options::setLocalPort(u_int16_t l
)
405 u_int16_t
Options::getLocalSyncPort()
407 return local_sync_port_
;
410 Options
& Options::setLocalSyncPort(u_int16_t l
)
412 local_sync_port_
= l
;
416 u_int16_t
Options::getRemoteSyncPort()
418 return remote_sync_port_
;
421 Options
& Options::setRemoteSyncPort(u_int16_t l
)
423 remote_sync_port_
= l
;
427 std::string
Options::getRemoteSyncAddr()
430 return remote_sync_addr_
;
433 Options
& Options::setRemoteSyncAddr(std::string r
)
436 remote_sync_addr_
= r
;
440 std::string
Options::getRemoteAddr()
446 Options
& Options::setRemoteAddr(std::string r
)
453 u_int16_t
Options::getRemotePort()
458 Options
& Options::setRemotePort(u_int16_t r
)
464 Options
& Options::setRemoteAddrPort(std::string addr
, u_int16_t port
)
472 std::string
Options::getDevName()
478 std::string
Options::getDevType()
484 Options
& Options::setDevName(std::string d
)
491 Options
& Options::setDevType(std::string d
)
498 std::string
Options::getIfconfigParamLocal()
501 return ifconfig_param_local_
;
504 Options
& Options::setIfconfigParamLocal(std::string i
)
507 ifconfig_param_local_
= i
;
511 std::string
Options::getIfconfigParamRemoteNetmask()
514 return ifconfig_param_remote_netmask_
;
517 Options
& Options::setIfconfigParamRemoteNetmask(std::string i
)
520 ifconfig_param_remote_netmask_
= i
;
524 std::string
Options::getPostUpScript()
527 return post_up_script_
;
530 Options
& Options::setPostUpScript(std::string p
)
537 window_size_t
Options::getSeqWindowSize()
539 return seq_window_size_
;
542 Options
& Options::setSeqWindowSize(window_size_t s
)
544 seq_window_size_
= s
;
548 std::string
Options::getCipher()
554 Options
& Options::setCipher(std::string c
)
561 std::string
Options::getKdPrf()
567 Options
& Options::setKdPrf(std::string k
)
574 std::string
Options::getAuthAlgo()
580 Options
& Options::setAuthAlgo(std::string a
)
587 u_int16_t
Options::getMux()
593 Options
& Options::setMux(u_int16_t m
)
600 Buffer
Options::getKey()
606 Options
& Options::setKey(std::string k
)
613 Buffer
Options::getSalt()
619 Options
& Options::setSalt(std::string s
)