added generic tool code
[anytun.git] / options.cpp
blob2a2b144c326ceed7a57f2ecb13af26a4dccd07a8
1 /*
2 * anytun
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
31 #include <iostream>
32 #include <queue>
33 #include <string>
34 #include <sstream>
36 #include "datatypes.h"
37 #include "options.h"
39 Options* Options::inst = NULL;
40 Mutex Options::instMutex;
41 Options& gOpt = Options::instance();
43 Options& Options::instance()
45 Lock lock(instMutex);
46 static instanceCleaner c;
47 if(!inst)
48 inst = new Options();
50 return *inst;
53 Options::Options() : key_(u_int32_t(0)), salt_(u_int32_t(0))
55 progname_ = "anytun";
56 sender_id_ = 0;
57 local_addr_ = "";
58 local_port_ = 4444;
59 local_sync_port_ = 0;
60 remote_sync_port_ = 0;
61 remote_sync_addr_ = "";
62 remote_addr_ = "";
63 remote_port_ = 4444;
64 dev_name_ = "tap";
65 dev_type_ = "";
66 ifconfig_param_local_ = "192.168.200.1";
67 ifconfig_param_remote_netmask_ = "255.255.255.0";
68 seq_window_size_ = 100;
69 cipher_ = "aes-ctr";
70 kd_prf_ = "aes-ctr";
71 auth_algo_ = "sha1";
72 mux_ = 0;
75 Options::~Options()
79 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
80 else if(str == SHORT || str == LONG) \
81 VALUE = true;
83 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
84 else if(str == SHORT || str == LONG) \
85 VALUE = false;
87 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
88 else if(str == SHORT || str == LONG) \
89 { \
90 if(argc < 1 || argv[i+1][0] == '-') \
91 return false; \
92 std::stringstream tmp; \
93 tmp << argv[i+1]; \
94 tmp >> VALUE; \
95 argc--; \
96 i++; \
99 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
100 else if(str == SHORT || str == LONG) \
102 if(argc < 2 || \
103 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
104 return false; \
105 std::stringstream tmp; \
106 tmp << argv[i+1] << " " << argv[i+2]; \
107 tmp >> VALUE1; \
108 tmp >> VALUE2; \
109 argc-=2; \
110 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] == '-') \
117 return false; \
118 VALUE = Buffer(std::string(argv[i+1])); \
119 argc--; \
120 i++; \
123 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
124 else if(str == SHORT || str == LONG) \
126 if(argc < 1 || argv[i+1][0] == '-') \
127 return false; \
128 std::stringstream tmp(argv[i+1]); \
129 while (tmp.good()) \
131 std::string tmp_line; \
132 getline(tmp,tmp_line,','); \
133 LIST.push(tmp_line); \
135 argc--; \
136 i++; \
139 bool Options::parse(int argc, char* argv[])
141 Lock lock(mutex);
143 progname_ = argv[0];
144 argc--;
145 std::queue<std::string> host_port_queue;
146 for(int i=1; argc > 0; ++i)
148 std::string str(argv[i]);
149 argc--;
151 if(str == "-h" || str == "--help")
152 return false;
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)
173 else
174 return false;
177 if(cipher_ == "null")
178 kd_prf_ = "null";
179 if(cipher_ != "null" && kd_prf_ == "null")
180 kd_prf_ = "aes-ctr";
182 while(!host_port_queue.empty())
184 std::stringstream tmp_stream(host_port_queue.front());
185 OptionConnectTo oct;
186 getline(tmp_stream,oct.host,':');
187 if(!tmp_stream.good())
188 return false;
189 tmp_stream >> oct.port;
190 host_port_queue.pop();
191 connect_to_.push_back(oct);
193 return true;
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()
225 Lock lock(mutex);
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()
249 Lock lock(mutex);
250 return progname_;
254 Options& Options::setProgname(std::string p)
256 Lock lock(mutex);
257 progname_ = p;
258 return *this;
261 ConnectToList Options::getConnectTo()
263 Lock lock(mutex);
264 return connect_to_;
267 sender_id_t Options::getSenderId()
269 return sender_id_;
272 Options& Options::setSenderId(sender_id_t s)
274 sender_id_ = s;
275 return *this;
278 std::string Options::getLocalAddr()
280 Lock lock(mutex);
281 return local_addr_;
284 Options& Options::setLocalAddr(std::string l)
286 Lock lock(mutex);
287 local_addr_ = l;
288 return *this;
291 std::string Options::getLocalSyncAddr()
293 Lock lock(mutex);
294 return local_sync_addr_;
297 Options& Options::setLocalSyncAddr(std::string l)
299 Lock lock(mutex);
300 local_sync_addr_ = l;
301 return *this;
304 u_int16_t Options::getLocalPort()
306 return local_port_;
309 Options& Options::setLocalPort(u_int16_t l)
311 local_port_ = l;
312 return *this;
315 u_int16_t Options::getLocalSyncPort()
317 return local_sync_port_;
320 Options& Options::setLocalSyncPort(u_int16_t l)
322 local_sync_port_ = l;
323 return *this;
326 u_int16_t Options::getRemoteSyncPort()
328 return remote_sync_port_;
331 Options& Options::setRemoteSyncPort(u_int16_t l)
333 remote_sync_port_ = l;
334 return *this;
337 std::string Options::getRemoteSyncAddr()
339 Lock lock(mutex);
340 return remote_sync_addr_;
343 Options& Options::setRemoteSyncAddr(std::string r)
345 Lock lock(mutex);
346 remote_sync_addr_ = r;
347 return *this;
350 std::string Options::getRemoteAddr()
352 Lock lock(mutex);
353 return remote_addr_;
356 Options& Options::setRemoteAddr(std::string r)
358 Lock lock(mutex);
359 remote_addr_ = r;
360 return *this;
363 u_int16_t Options::getRemotePort()
365 return remote_port_;
368 Options& Options::setRemotePort(u_int16_t r)
370 remote_port_ = r;
371 return *this;
374 Options& Options::setRemoteAddrPort(std::string addr, u_int16_t port)
376 Lock lock(mutex);
377 remote_addr_ = addr;
378 remote_port_ = port;
379 return *this;
382 std::string Options::getDevName()
384 Lock lock(mutex);
385 return dev_name_;
388 std::string Options::getDevType()
390 Lock lock(mutex);
391 return dev_type_;
394 Options& Options::setDevName(std::string d)
396 Lock lock(mutex);
397 dev_name_ = d;
398 return *this;
401 Options& Options::setDevType(std::string d)
403 Lock lock(mutex);
404 dev_type_ = d;
405 return *this;
408 std::string Options::getIfconfigParamLocal()
410 Lock lock(mutex);
411 return ifconfig_param_local_;
414 Options& Options::setIfconfigParamLocal(std::string i)
416 Lock lock(mutex);
417 ifconfig_param_local_ = i;
418 return *this;
421 std::string Options::getIfconfigParamRemoteNetmask()
423 Lock lock(mutex);
424 return ifconfig_param_remote_netmask_;
427 Options& Options::setIfconfigParamRemoteNetmask(std::string i)
429 Lock lock(mutex);
430 ifconfig_param_remote_netmask_ = i;
431 return *this;
434 window_size_t Options::getSeqWindowSize()
436 return seq_window_size_;
439 Options& Options::setSeqWindowSize(window_size_t s)
441 seq_window_size_ = s;
442 return *this;
445 std::string Options::getCipher()
447 Lock lock(mutex);
448 return cipher_;
451 Options& Options::setCipher(std::string c)
453 Lock lock(mutex);
454 cipher_ = c;
455 return *this;
458 std::string Options::getKdPrf()
460 Lock lock(mutex);
461 return kd_prf_;
464 Options& Options::setKdPrf(std::string k)
466 Lock lock(mutex);
467 kd_prf_ = k;
468 return *this;
471 std::string Options::getAuthAlgo()
473 Lock lock(mutex);
474 return auth_algo_;
477 Options& Options::setAuthAlgo(std::string a)
479 Lock lock(mutex);
480 auth_algo_ = a;
481 return *this;
484 u_int16_t Options::getMux()
486 Lock lock(mutex);
487 return mux_;
490 Options& Options::setMux(u_int16_t m)
492 Lock lock(mutex);
493 mux_ = m;
494 return *this;
497 Buffer Options::getKey()
499 Lock lock(mutex);
500 return key_;
503 Options& Options::setKey(std::string k)
505 Lock lock(mutex);
506 key_ = k;
507 return *this;
510 Buffer Options::getSalt()
512 Lock lock(mutex);
513 return salt_;
516 Options& Options::setSalt(std::string s)
518 Lock lock(mutex);
519 salt_ = s;
520 return *this;