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))
61 remote_sync_port_
= 0;
62 remote_sync_addr_
= "";
67 ifconfig_param_local_
= "192.168.200.1";
68 ifconfig_param_remote_netmask_
= "255.255.255.0";
69 seq_window_size_
= 100;
80 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
81 else if(str == SHORT || str == LONG) \
84 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
85 else if(str == SHORT || str == LONG) \
88 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
89 else if(str == SHORT || str == LONG) \
91 if(argc < 1 || argv[i+1][0] == '-') \
93 std::stringstream tmp; \
100 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
101 else if(str == SHORT || str == LONG) \
104 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
106 std::stringstream tmp; \
107 tmp << argv[i+1] << " " << argv[i+2]; \
114 #define PARSE_HEXSTRING_PARAM(SHORT, LONG, VALUE) \
115 else if(str == SHORT || str == LONG) \
117 if(argc < 1 || argv[i+1][0] == '-') \
119 VALUE = Buffer(std::string(argv[i+1])); \
124 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
125 else if(str == SHORT || str == LONG) \
127 if(argc < 1 || argv[i+1][0] == '-') \
129 std::stringstream tmp(argv[i+1]); \
132 std::string tmp_line; \
133 getline(tmp,tmp_line,','); \
134 LIST.push(tmp_line); \
140 bool Options::parse(int argc
, char* argv
[])
146 std::queue
<std::string
> host_port_queue
;
147 for(int i
=1; argc
> 0; ++i
)
149 std::string
str(argv
[i
]);
152 if(str
== "-h" || str
== "--help")
154 PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", daemonize_
)
155 PARSE_SCALAR_PARAM("-s","--sender-id", sender_id_
)
156 PARSE_SCALAR_PARAM("-i","--interface", local_addr_
)
157 PARSE_SCALAR_PARAM("-p","--port", local_port_
)
158 PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_
)
159 PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_
)
160 PARSE_SCALAR_PARAM("-R","--remote-sync-host", remote_sync_addr_
)
161 PARSE_SCALAR_PARAM("-O","--remote-sync-port", remote_sync_port_
)
162 PARSE_SCALAR_PARAM("-r","--remote-host", remote_addr_
)
163 PARSE_SCALAR_PARAM("-o","--remote-port", remote_port_
)
164 PARSE_SCALAR_PARAM("-d","--dev", dev_name_
)
165 PARSE_SCALAR_PARAM("-t","--type", dev_type_
)
166 PARSE_SCALAR_PARAM2("-n","--ifconfig", ifconfig_param_local_
, ifconfig_param_remote_netmask_
)
167 PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_
)
168 PARSE_SCALAR_PARAM("-m","--mux", mux_
)
169 PARSE_SCALAR_PARAM("-c","--cipher", cipher_
)
170 PARSE_HEXSTRING_PARAM("-K","--key", key_
)
171 PARSE_HEXSTRING_PARAM("-A","--salt", salt_
)
172 PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_
)
173 PARSE_SCALAR_PARAM("-a","--auth-algo", auth_algo_
)
174 PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue
)
179 if(cipher_
== "null" && auth_algo_
== "null")
181 if((cipher_
!= "null" || auth_algo_
!= "null") && kd_prf_
== "null")
184 while(!host_port_queue
.empty())
186 std::stringstream
tmp_stream(host_port_queue
.front());
188 getline(tmp_stream
,oct
.host
,':');
189 if(!tmp_stream
.good())
191 tmp_stream
>> oct
.port
;
192 host_port_queue
.pop();
193 connect_to_
.push_back(oct
);
198 void Options::printUsage()
200 std::cout
<< "USAGE:" << std::endl
;
201 std::cout
<< "anytun [-h|--help] prints this..." << std::endl
;
202 // std::cout << " [-f|--config] <file> the config file" << std::endl;
203 std::cout
<< " [-D|--nodaemonize] don't run in background" << std::endl
;
204 std::cout
<< " [-s|--sender-id ] <sender id> the sender id to use" << std::endl
;
205 std::cout
<< " [-i|--interface] <ip-address> local anycast ip address to bind to" << std::endl
;
206 std::cout
<< " [-p|--port] <port> local anycast(data) port to bind to" << std::endl
;
207 std::cout
<< " [-I|--sync-interface] <ip-address> local unicast(sync) ip address to bind to" << std::endl
;
208 std::cout
<< " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl
;
209 std::cout
<< " [-M|--sync-hosts] <hostname|ip>:<port>[,<hostname|ip>:<port>[...]]"<< std::endl
;
210 std::cout
<< " remote hosts to sync with" << std::endl
;
211 std::cout
<< " [-r|--remote-host] <hostname|ip> remote host" << std::endl
;
212 std::cout
<< " [-o|--remote-port] <port> remote port" << std::endl
;
213 std::cout
<< " [-d|--dev] <name> device name" << std::endl
;
214 std::cout
<< " [-t|--type] <tun|tap> device type" << std::endl
;
215 std::cout
<< " [-n|--ifconfig] <local> the local address for the tun/tap device" << std::endl
216 << " <remote|netmask> the remote address(tun) or netmask(tap)" << std::endl
;
217 std::cout
<< " [-w|--window-size] <window size> seqence number window size" << std::endl
;
218 std::cout
<< " [-m|--mux] <mux-id> the multiplex id to use" << std::endl
;
219 std::cout
<< " [-c|--cipher] <cipher type> payload encryption algorithm" << std::endl
;
220 std::cout
<< " [-K|--key] <master key> master key to use for encryption" << std::endl
;
221 std::cout
<< " [-A|--salt] <master salt> master salt to use for encryption" << std::endl
;
222 std::cout
<< " [-k|--kd-prf] <kd-prf type> key derivation pseudo random function" << std::endl
;
223 std::cout
<< " [-a|--auth-algo] <algo type> message authentication algorithm" << std::endl
;
226 void Options::printOptions()
229 std::cout
<< "Options:" << std::endl
;
230 std::cout
<< "daemonize=" << daemonize_
<< std::endl
;
231 std::cout
<< "sender_id='" << sender_id_
<< "'" << std::endl
;
232 std::cout
<< "local_addr='" << local_addr_
<< "'" << std::endl
;
233 std::cout
<< "local_port='" << local_port_
<< "'" << std::endl
;
234 std::cout
<< "local_sync_addr='" << local_sync_addr_
<< "'" << std::endl
;
235 std::cout
<< "local_sync_port='" << local_sync_port_
<< "'" << std::endl
;
236 std::cout
<< "remote_addr='" << remote_addr_
<< "'" << std::endl
;
237 std::cout
<< "remote_port='" << remote_port_
<< "'" << std::endl
;
238 std::cout
<< "dev_name='" << dev_name_
<< "'" << std::endl
;
239 std::cout
<< "dev_type='" << dev_type_
<< "'" << std::endl
;
240 std::cout
<< "ifconfig_param_local='" << ifconfig_param_local_
<< "'" << std::endl
;
241 std::cout
<< "ifconfig_param_remote_netmask='" << ifconfig_param_remote_netmask_
<< "'" << std::endl
;
242 std::cout
<< "seq_window_size='" << seq_window_size_
<< "'" << std::endl
;
243 std::cout
<< "mux_id='" << mux_
<< "'" << std::endl
;
244 std::cout
<< "cipher='" << cipher_
<< "'" << std::endl
;
245 std::cout
<< "key=" << key_
.getHexDumpOneLine() << std::endl
;
246 std::cout
<< "salt=" << salt_
.getHexDumpOneLine() << std::endl
;
247 std::cout
<< "kd_prf='" << kd_prf_
<< "'" << std::endl
;
248 std::cout
<< "auth_algo='" << auth_algo_
<< "'" << std::endl
;
251 std::string
Options::getProgname()
258 Options
& Options::setProgname(std::string p
)
265 bool Options::getDaemonize()
270 Options
& Options::setDaemonize(bool d
)
276 ConnectToList
Options::getConnectTo()
282 sender_id_t
Options::getSenderId()
287 Options
& Options::setSenderId(sender_id_t s
)
293 std::string
Options::getLocalAddr()
299 Options
& Options::setLocalAddr(std::string l
)
306 std::string
Options::getLocalSyncAddr()
309 return local_sync_addr_
;
312 Options
& Options::setLocalSyncAddr(std::string l
)
315 local_sync_addr_
= l
;
319 u_int16_t
Options::getLocalPort()
324 Options
& Options::setLocalPort(u_int16_t l
)
330 u_int16_t
Options::getLocalSyncPort()
332 return local_sync_port_
;
335 Options
& Options::setLocalSyncPort(u_int16_t l
)
337 local_sync_port_
= l
;
341 u_int16_t
Options::getRemoteSyncPort()
343 return remote_sync_port_
;
346 Options
& Options::setRemoteSyncPort(u_int16_t l
)
348 remote_sync_port_
= l
;
352 std::string
Options::getRemoteSyncAddr()
355 return remote_sync_addr_
;
358 Options
& Options::setRemoteSyncAddr(std::string r
)
361 remote_sync_addr_
= r
;
365 std::string
Options::getRemoteAddr()
371 Options
& Options::setRemoteAddr(std::string r
)
378 u_int16_t
Options::getRemotePort()
383 Options
& Options::setRemotePort(u_int16_t r
)
389 Options
& Options::setRemoteAddrPort(std::string addr
, u_int16_t port
)
397 std::string
Options::getDevName()
403 std::string
Options::getDevType()
409 Options
& Options::setDevName(std::string d
)
416 Options
& Options::setDevType(std::string d
)
423 std::string
Options::getIfconfigParamLocal()
426 return ifconfig_param_local_
;
429 Options
& Options::setIfconfigParamLocal(std::string i
)
432 ifconfig_param_local_
= i
;
436 std::string
Options::getIfconfigParamRemoteNetmask()
439 return ifconfig_param_remote_netmask_
;
442 Options
& Options::setIfconfigParamRemoteNetmask(std::string i
)
445 ifconfig_param_remote_netmask_
= i
;
449 window_size_t
Options::getSeqWindowSize()
451 return seq_window_size_
;
454 Options
& Options::setSeqWindowSize(window_size_t s
)
456 seq_window_size_
= s
;
460 std::string
Options::getCipher()
466 Options
& Options::setCipher(std::string c
)
473 std::string
Options::getKdPrf()
479 Options
& Options::setKdPrf(std::string k
)
486 std::string
Options::getAuthAlgo()
492 Options
& Options::setAuthAlgo(std::string a
)
499 u_int16_t
Options::getMux()
505 Options
& Options::setMux(u_int16_t m
)
512 Buffer
Options::getKey()
518 Options
& Options::setKey(std::string k
)
525 Buffer
Options::getSalt()
531 Options
& Options::setSalt(std::string s
)