patched isakmp debian packages
[anytun.git] / options.cpp
blobb427f5776b8c3037f739e5bed994cf5ad214d05a
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 <string>
33 #include <sstream>
35 #include "datatypes.h"
36 #include "options.h"
39 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
40 else if(str == SHORT || str == LONG) \
41 VALUE = true;
43 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
44 else if(str == SHORT || str == LONG) \
45 VALUE = false;
47 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
48 else if(str == SHORT || str == LONG) \
49 { \
50 if(argc < 1 || argv[i+1][0] == '-') \
51 return false; \
52 std::stringstream tmp; \
53 tmp << argv[i+1]; \
54 tmp >> VALUE; \
55 argc--; \
56 i++; \
59 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
60 else if(str == SHORT || str == LONG) \
61 { \
62 if(argc < 2 || \
63 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
64 return false; \
65 std::stringstream tmp; \
66 tmp << argv[i+1] << " " << argv[i+2]; \
67 tmp >> VALUE1; \
68 tmp >> VALUE2; \
69 argc-=2; \
70 i+=2; \
74 Options::Options()
76 progname_ = "anytun";
77 sender_id_ = 0;
78 local_addr_ = "";
79 local_port_ = 4444;
80 remote_addr_ = "";
81 remote_port_ = 4444;
82 dev_name_ = "tap";
83 ifconfig_param_local_ = "192.168.200.1";
84 ifconfig_param_remote_netmask_ = "255.255.255.0";
85 seq_window_size_ = 100;
86 cypher_ = "null";
87 auth_algo_ = "null";
90 bool Options::parse(int argc, char* argv[])
92 Lock lock(mutex);
94 progname_ = argv[0];
95 argc--;
97 for(int i=1; argc > 0; ++i)
99 std::string str(argv[i]);
100 argc--;
102 if(str == "-h" || str == "--help")
103 return false;
104 PARSE_SCALAR_PARAM("-s","--sender-id", sender_id_)
105 PARSE_SCALAR_PARAM("-i","--interface", local_addr_)
106 PARSE_SCALAR_PARAM("-p","--port", local_port_)
107 PARSE_SCALAR_PARAM("-r","--remote-host", remote_addr_)
108 PARSE_SCALAR_PARAM("-o","--remote-port", remote_port_)
109 PARSE_SCALAR_PARAM("-d","--dev", dev_name_)
110 PARSE_SCALAR_PARAM2("-n","--ifconfig", ifconfig_param_local_, ifconfig_param_remote_netmask_)
111 PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_)
112 PARSE_SCALAR_PARAM("-c","--cypher", cypher_)
113 PARSE_SCALAR_PARAM("-a","--auth-algo", auth_algo_)
114 else
115 return false;
117 return true;
120 void Options::printUsage()
122 std::cout << "USAGE:" << std::endl;
123 std::cout << "anytun [-h|--help] prints this..." << std::endl;
124 // std::cout << " [-f|--config] <file> the config file" << std::endl;
125 std::cout << " [-s|--sender-id ] <sender id> the sender id to use" << std::endl;
126 std::cout << " [-i|--interface] <interface> local interface to bind to" << std::endl;
127 std::cout << " [-p|--port] <port> local port to bind to" << std::endl;
128 std::cout << " [-r|--remote-host] <hostname/ip> remote host" << std::endl;
129 std::cout << " [-o|--remote-port] <port> remote port" << std::endl;
130 std::cout << " [-d|--dev] <name> device name/type" << std::endl;
131 std::cout << " [-n|--ifconfig] <local> the local address for the tun/tap device" << std::endl
132 << " <remote/netmask> the remote address(tun) or netmask(tap)" << std::endl;
133 std::cout << " [-w|--window-size] <window size> seqence number window size" << std::endl;
134 std::cout << " [-c|--cypher] <cypher type> type of cypher" << std::endl;
135 std::cout << " [-a|--auth-algo] <algo type> authentication algoritm" << std::endl;
138 void Options::printOptions()
140 Lock lock(mutex);
141 std::cout << "Options:" << std::endl;
142 std::cout << "sender_id='" << sender_id_ << "'" << std::endl;
143 std::cout << "local_addr='" << local_addr_ << "'" << std::endl;
144 std::cout << "local_port='" << local_port_ << "'" << std::endl;
145 std::cout << "remote_addr='" << remote_addr_ << "'" << std::endl;
146 std::cout << "remote_port='" << remote_port_ << "'" << std::endl;
147 std::cout << "dev_name='" << dev_name_ << "'" << std::endl;
148 std::cout << "ifconfig_param_local='" << ifconfig_param_local_ << "'" << std::endl;
149 std::cout << "ifconfig_param_remote_netmask='" << ifconfig_param_remote_netmask_ << "'" << std::endl;
150 std::cout << "seq_window_size='" << seq_window_size_ << "'" << std::endl;
151 std::cout << "cypher='" << cypher_ << "'" << std::endl;
152 std::cout << "auth_algo='" << auth_algo_ << "'" << std::endl;
155 std::string Options::getProgname()
157 Lock lock(mutex);
158 return progname_;
161 Options& Options::setProgname(std::string p)
163 Lock lock(mutex);
164 progname_ = p;
165 return *this;
168 sender_id_t Options::getSenderId()
170 return sender_id_;
173 Options& Options::setSenderId(sender_id_t s)
175 sender_id_ = s;
176 return *this;
179 std::string Options::getLocalAddr()
181 Lock lock(mutex);
182 return local_addr_;
185 Options& Options::setLocalAddr(std::string l)
187 Lock lock(mutex);
188 local_addr_ = l;
189 return *this;
192 u_int16_t Options::getLocalPort()
194 return local_port_;
197 Options& Options::setLocalPort(u_int16_t l)
199 local_port_ = l;
200 return *this;
203 std::string Options::getRemoteAddr()
205 Lock lock(mutex);
206 return remote_addr_;
209 Options& Options::setRemoteAddr(std::string r)
211 Lock lock(mutex);
212 remote_addr_ = r;
213 return *this;
216 u_int16_t Options::getRemotePort()
218 return remote_port_;
221 Options& Options::setRemotePort(u_int16_t r)
223 remote_port_ = r;
224 return *this;
227 Options& Options::setRemoteAddrPort(std::string addr, u_int16_t port)
229 Lock lock(mutex);
230 remote_addr_ = addr;
231 remote_port_ = port;
232 return *this;
235 std::string Options::getDevName()
237 Lock lock(mutex);
238 return dev_name_;
241 Options& Options::setDevName(std::string d)
243 Lock lock(mutex);
244 dev_name_ = d;
245 return *this;
248 std::string Options::getIfconfigParamLocal()
250 Lock lock(mutex);
251 return ifconfig_param_local_;
254 Options& Options::setIfconfigParamLocal(std::string i)
256 Lock lock(mutex);
257 ifconfig_param_local_ = i;
258 return *this;
261 std::string Options::getIfconfigParamRemoteNetmask()
263 Lock lock(mutex);
264 return ifconfig_param_remote_netmask_;
267 Options& Options::setIfconfigParamRemoteNetmask(std::string i)
269 Lock lock(mutex);
270 ifconfig_param_remote_netmask_ = i;
271 return *this;
274 window_size_t Options::getSeqWindowSize()
276 return seq_window_size_;
279 Options& Options::setSeqWindowSize(window_size_t s)
281 seq_window_size_ = s;
282 return *this;
285 std::string Options::getCypher()
287 Lock lock(mutex);
288 return cypher_;
291 Options& Options::setCypher(std::string c)
293 Lock lock(mutex);
294 cypher_ = c;
295 return *this;
298 std::string Options::getAuthAlgo()
300 Lock lock(mutex);
301 return auth_algo_;
304 Options& Options::setAuthAlgo(std::string a)
306 Lock lock(mutex);
307 auth_algo_ = a;
308 return *this;