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))
60 remote_sync_port_
= 0;
61 remote_sync_addr_
= "";
66 ifconfig_param_local_
= "192.168.200.1";
67 ifconfig_param_remote_netmask_
= "255.255.255.0";
68 seq_window_size_
= 100;
79 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
80 else if(str == SHORT || str == LONG) \
83 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
84 else if(str == SHORT || str == LONG) \
87 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
88 else if(str == SHORT || str == LONG) \
90 if(argc < 1 || argv[i+1][0] == '-') \
92 std::stringstream tmp; \
99 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
100 else if(str == SHORT || str == LONG) \
103 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
105 std::stringstream tmp; \
106 tmp << argv[i+1] << " " << argv[i+2]; \
113 #define PARSE_HEXSTRING_PARAM(SHORT, LONG, VALUE) \
114 else if(str == SHORT || str == LONG) \
116 if(argc < 1 || argv[i+1][0] == '-') \
118 VALUE = Buffer(std::string(argv[i+1])); \
123 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
124 else if(str == SHORT || str == LONG) \
126 if(argc < 1 || argv[i+1][0] == '-') \
128 std::stringstream tmp(argv[i+1]); \
131 std::string tmp_line; \
132 getline(tmp,tmp_line,','); \
133 LIST.push(tmp_line); \
139 bool Options::parse(int argc
, char* argv
[])
145 std::queue
<std::string
> host_port_queue
;
146 for(int i
=1; argc
> 0; ++i
)
148 std::string
str(argv
[i
]);
151 if(str
== "-h" || str
== "--help")
153 PARSE_SCALAR_PARAM("-s","--sender-id", sender_id_
)
154 PARSE_SCALAR_PARAM("-i","--interface", local_addr_
)
155 PARSE_SCALAR_PARAM("-p","--port", local_port_
)
156 PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_
)
157 PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_
)
158 PARSE_SCALAR_PARAM("-R","--remote-sync-host", remote_sync_addr_
)
159 PARSE_SCALAR_PARAM("-O","--remote-sync-port", remote_sync_port_
)
160 PARSE_SCALAR_PARAM("-r","--remote-host", remote_addr_
)
161 PARSE_SCALAR_PARAM("-o","--remote-port", remote_port_
)
162 PARSE_SCALAR_PARAM("-d","--dev", dev_name_
)
163 PARSE_SCALAR_PARAM("-t","--type", dev_type_
)
164 PARSE_SCALAR_PARAM2("-n","--ifconfig", ifconfig_param_local_
, ifconfig_param_remote_netmask_
)
165 PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_
)
166 PARSE_SCALAR_PARAM("-m","--mux", mux_
)
167 PARSE_SCALAR_PARAM("-c","--cipher", cipher_
)
168 PARSE_HEXSTRING_PARAM("-K","--key", key_
)
169 PARSE_HEXSTRING_PARAM("-a","--salt", salt_
)
170 PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_
)
171 PARSE_SCALAR_PARAM("-a","--auth-algo", auth_algo_
)
172 PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue
)
177 if(cipher_
== "null")
179 if(cipher_
!= "null" && kd_prf_
== "null")
182 while(!host_port_queue
.empty())
184 std::stringstream
tmp_stream(host_port_queue
.front());
186 getline(tmp_stream
,oct
.host
,':');
187 if(!tmp_stream
.good())
189 tmp_stream
>> oct
.port
;
190 host_port_queue
.pop();
191 connect_to_
.push_back(oct
);
196 void Options::printUsage()
198 std::cout
<< "USAGE:" << std::endl
;
199 std::cout
<< "anytun [-h|--help] prints this..." << std::endl
;
200 // std::cout << " [-f|--config] <file> the config file" << std::endl;
201 std::cout
<< " [-s|--sender-id ] <sender id> the sender id to use" << std::endl
;
202 std::cout
<< " [-i|--interface] <ip-address> local anycast ip address to bind to" << std::endl
;
203 std::cout
<< " [-p|--port] <port> local anycast(data) port to bind to" << std::endl
;
204 std::cout
<< " [-I|--sync-interface] <ip-address> local unicast(sync) ip address to bind to" << std::endl
;
205 std::cout
<< " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl
;
206 std::cout
<< " [-M|--sync-hosts] <hostname|ip>:<port>[,<hostname|ip>:<port>[...]]"<< std::endl
;
207 std::cout
<< " remote hosts to sync with" << std::endl
;
208 std::cout
<< " [-r|--remote-host] <hostname|ip> remote host" << std::endl
;
209 std::cout
<< " [-o|--remote-port] <port> remote port" << std::endl
;
210 std::cout
<< " [-d|--dev] <name> device name" << std::endl
;
211 std::cout
<< " [-t|--type] <tun|tap> device type" << std::endl
;
212 std::cout
<< " [-n|--ifconfig] <local> the local address for the tun/tap device" << std::endl
213 << " <remote|netmask> the remote address(tun) or netmask(tap)" << std::endl
;
214 std::cout
<< " [-w|--window-size] <window size> seqence number window size" << std::endl
;
215 std::cout
<< " [-m|--mux] <mux-id> the multiplex id to use" << std::endl
;
216 std::cout
<< " [-c|--cipher] <cipher type> payload encryption algorithm" << std::endl
;
217 std::cout
<< " [-K|--key] <master key> master key to use for encryption" << std::endl
;
218 std::cout
<< " [-a|--salt] <master salt> master salt to use for encryption" << std::endl
;
219 std::cout
<< " [-k|--kd-prf] <kd-prf type> key derivation pseudo random function" << std::endl
;
220 std::cout
<< " [-a|--auth-algo] <algo type> message authentication algorithm" << std::endl
;
223 void Options::printOptions()
226 std::cout
<< "Options:" << std::endl
;
227 std::cout
<< "sender_id='" << sender_id_
<< "'" << std::endl
;
228 std::cout
<< "local_addr='" << local_addr_
<< "'" << std::endl
;
229 std::cout
<< "local_port='" << local_port_
<< "'" << std::endl
;
230 std::cout
<< "local_sync_addr='" << local_sync_addr_
<< "'" << std::endl
;
231 std::cout
<< "local_sync_port='" << local_sync_port_
<< "'" << std::endl
;
232 std::cout
<< "remote_addr='" << remote_addr_
<< "'" << std::endl
;
233 std::cout
<< "remote_port='" << remote_port_
<< "'" << std::endl
;
234 std::cout
<< "dev_name='" << dev_name_
<< "'" << std::endl
;
235 std::cout
<< "dev_type='" << dev_type_
<< "'" << std::endl
;
236 std::cout
<< "ifconfig_param_local='" << ifconfig_param_local_
<< "'" << std::endl
;
237 std::cout
<< "ifconfig_param_remote_netmask='" << ifconfig_param_remote_netmask_
<< "'" << std::endl
;
238 std::cout
<< "seq_window_size='" << seq_window_size_
<< "'" << std::endl
;
239 std::cout
<< "mux_id='" << mux_
<< "'" << std::endl
;
240 std::cout
<< "cipher='" << cipher_
<< "'" << std::endl
;
241 std::cout
<< "key=" << key_
.getHexDumpOneLine() << std::endl
;
242 std::cout
<< "salt=" << salt_
.getHexDumpOneLine() << std::endl
;
243 std::cout
<< "kd_prf='" << kd_prf_
<< "'" << std::endl
;
244 std::cout
<< "auth_algo='" << auth_algo_
<< "'" << std::endl
;
247 std::string
Options::getProgname()
254 Options
& Options::setProgname(std::string p
)
261 ConnectToList
Options::getConnectTo()
267 sender_id_t
Options::getSenderId()
272 Options
& Options::setSenderId(sender_id_t s
)
278 std::string
Options::getLocalAddr()
284 Options
& Options::setLocalAddr(std::string l
)
291 std::string
Options::getLocalSyncAddr()
294 return local_sync_addr_
;
297 Options
& Options::setLocalSyncAddr(std::string l
)
300 local_sync_addr_
= l
;
304 u_int16_t
Options::getLocalPort()
309 Options
& Options::setLocalPort(u_int16_t l
)
315 u_int16_t
Options::getLocalSyncPort()
317 return local_sync_port_
;
320 Options
& Options::setLocalSyncPort(u_int16_t l
)
322 local_sync_port_
= l
;
326 u_int16_t
Options::getRemoteSyncPort()
328 return remote_sync_port_
;
331 Options
& Options::setRemoteSyncPort(u_int16_t l
)
333 remote_sync_port_
= l
;
337 std::string
Options::getRemoteSyncAddr()
340 return remote_sync_addr_
;
343 Options
& Options::setRemoteSyncAddr(std::string r
)
346 remote_sync_addr_
= r
;
350 std::string
Options::getRemoteAddr()
356 Options
& Options::setRemoteAddr(std::string r
)
363 u_int16_t
Options::getRemotePort()
368 Options
& Options::setRemotePort(u_int16_t r
)
374 Options
& Options::setRemoteAddrPort(std::string addr
, u_int16_t port
)
382 std::string
Options::getDevName()
388 std::string
Options::getDevType()
394 Options
& Options::setDevName(std::string d
)
401 Options
& Options::setDevType(std::string d
)
408 std::string
Options::getIfconfigParamLocal()
411 return ifconfig_param_local_
;
414 Options
& Options::setIfconfigParamLocal(std::string i
)
417 ifconfig_param_local_
= i
;
421 std::string
Options::getIfconfigParamRemoteNetmask()
424 return ifconfig_param_remote_netmask_
;
427 Options
& Options::setIfconfigParamRemoteNetmask(std::string i
)
430 ifconfig_param_remote_netmask_
= i
;
434 window_size_t
Options::getSeqWindowSize()
436 return seq_window_size_
;
439 Options
& Options::setSeqWindowSize(window_size_t s
)
441 seq_window_size_
= s
;
445 std::string
Options::getCipher()
451 Options
& Options::setCipher(std::string c
)
458 std::string
Options::getKdPrf()
464 Options
& Options::setKdPrf(std::string k
)
471 std::string
Options::getAuthAlgo()
477 Options
& Options::setAuthAlgo(std::string a
)
484 u_int16_t
Options::getMux()
490 Options
& Options::setMux(u_int16_t m
)
497 Buffer
Options::getKey()
503 Options
& Options::setKey(std::string k
)
510 Buffer
Options::getSalt()
516 Options
& Options::setSalt(std::string s
)