big svn cleanup
[anytun.git] / src / anyctrOptions.cpp
blob3c770bce977022266e27663e47abaa9df3f407a0
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 "anyctrOptions.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-config";
56 remote_addr_ = "";
57 remote_port_ = 4444;
58 ifconfig_param_remote_netmask_ = "255.255.255.0";
59 seq_window_size_ = 100;
60 kd_prf_ = "aes-ctr";
61 mux_ = 0;
62 network_prefix_length_ = 32;
65 Options::~Options()
69 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
70 else if(str == SHORT || str == LONG) \
71 VALUE = true;
73 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
74 else if(str == SHORT || str == LONG) \
75 VALUE = false;
77 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
78 else if(str == SHORT || str == LONG) \
79 { \
80 if(argc < 1 || argv[i+1][0] == '-') \
81 return false; \
82 std::stringstream tmp; \
83 tmp << argv[i+1]; \
84 tmp >> VALUE; \
85 argc--; \
86 i++; \
89 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
90 else if(str == SHORT || str == LONG) \
91 { \
92 if(argc < 2 || \
93 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
94 return false; \
95 std::stringstream tmp; \
96 tmp << argv[i+1] << " " << argv[i+2]; \
97 tmp >> VALUE1; \
98 tmp >> VALUE2; \
99 argc-=2; \
100 i+=2; \
103 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \
104 else if(str == SHORT || str == LONG) \
106 if(argc < 1 || argv[i+1][0] == '-') \
107 return false; \
108 VALUE = Buffer(std::string(argv[i+1])); \
109 for(size_t j=0; j < strlen(argv[i+1]); ++j) \
110 argv[i+1][j] = '#'; \
111 argc--; \
112 i++; \
115 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
116 else if(str == SHORT || str == LONG) \
118 if(argc < 1 || argv[i+1][0] == '-') \
119 return false; \
120 std::stringstream tmp(argv[i+1]); \
121 while (tmp.good()) \
123 std::string tmp_line; \
124 getline(tmp,tmp_line,','); \
125 LIST.push(tmp_line); \
127 argc--; \
128 i++; \
131 bool Options::parse(int argc, char* argv[])
133 Lock lock(mutex);
135 progname_ = argv[0];
136 argc--;
137 std::queue<std::string> host_port_queue;
138 for(int i=1; argc > 0; ++i)
140 std::string str(argv[i]);
141 argc--;
143 if(str == "-h" || str == "--help")
144 return false;
145 PARSE_SCALAR_PARAM("-r","--remote-host", remote_addr_)
146 PARSE_SCALAR_PARAM("-o","--remote-port", remote_port_)
147 PARSE_SCALAR_PARAM("-n","--prefix", ifconfig_param_remote_netmask_)
148 PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_)
149 PARSE_SCALAR_PARAM("-m","--mux", mux_)
150 PARSE_SCALAR_PARAM("-l","--prefix-len", network_prefix_length_)
151 PARSE_HEXSTRING_PARAM_SEC("-K","--key", key_)
152 PARSE_HEXSTRING_PARAM_SEC("-A","--salt", salt_)
153 PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_)
154 else
155 return false;
158 while(!host_port_queue.empty())
160 std::stringstream tmp_stream(host_port_queue.front());
161 OptionConnectTo oct;
162 getline(tmp_stream,oct.host,':');
163 if(!tmp_stream.good())
164 return false;
165 tmp_stream >> oct.port;
166 host_port_queue.pop();
167 connect_to_.push_back(oct);
169 return true;
172 void Options::printUsage()
174 std::cout << "USAGE:" << std::endl;
175 std::cout << "anytun-config" << std::endl;
176 std::cout << " [-h|--help] prints this..." << std::endl;
177 std::cout << " [-r|--remote-host] <hostname|ip> remote host" << std::endl;
178 std::cout << " [-o|--remote-port] <port> remote port" << std::endl;
179 std::cout << " [-n|--prefix] <remote net> remote subnet for route" << std::endl;
180 std::cout << " [-w|--window-size] <window size> seqence number window size" << std::endl;
181 std::cout << " [-m|--mux] <mux-id> the multiplex id to use" << std::endl;
182 std::cout << " [-l|--prefix-len] <prefix length> network prefix length" << std::endl;
183 std::cout << " [-K|--key] <master key> master key to use for encryption" << std::endl;
184 std::cout << " [-A|--salt] <master salt> master salt to use for encryption" << std::endl;
185 std::cout << " [-k|--kd-prf] <kd-prf type> key derivation pseudo random function" << std::endl;
188 void Options::printOptions()
190 Lock lock(mutex);
191 std::cout << "Options:" << std::endl;
192 std::cout << "remote_addr='" << remote_addr_ << "'" << std::endl;
193 std::cout << "remote_port='" << remote_port_ << "'" << std::endl;
194 std::cout << "ifconfig_param_local='" << ifconfig_param_local_ << "'" << std::endl;
195 std::cout << "ifconfig_param_remote_netmask='" << ifconfig_param_remote_netmask_ << "'" << std::endl;
196 std::cout << "seq_window_size='" << seq_window_size_ << "'" << std::endl;
197 std::cout << "mux_id='" << mux_ << "'" << std::endl;
198 std::cout << "network_prefix_length='" << network_prefix_length_ << "'" << std::endl;
199 std::cout << "key=" << key_.getHexDumpOneLine() << std::endl;
200 std::cout << "salt=" << salt_.getHexDumpOneLine() << std::endl;
201 std::cout << "kd_prf='" << kd_prf_ << "'" << std::endl;
204 std::string Options::getProgname()
206 Lock lock(mutex);
207 return progname_;
211 Options& Options::setProgname(std::string p)
213 Lock lock(mutex);
214 progname_ = p;
215 return *this;
219 std::string Options::getRemoteAddr()
221 Lock lock(mutex);
222 return remote_addr_;
225 Options& Options::setRemoteAddr(std::string r)
227 Lock lock(mutex);
228 remote_addr_ = r;
229 return *this;
232 u_int16_t Options::getRemotePort()
234 return remote_port_;
237 Options& Options::setRemotePort(u_int16_t r)
239 remote_port_ = r;
240 return *this;
243 Options& Options::setRemoteAddrPort(std::string addr, u_int16_t port)
245 Lock lock(mutex);
246 remote_addr_ = addr;
247 remote_port_ = port;
248 return *this;
251 std::string Options::getIfconfigParamRemoteNetmask()
253 Lock lock(mutex);
254 return ifconfig_param_remote_netmask_;
257 Options& Options::setIfconfigParamRemoteNetmask(std::string i)
259 Lock lock(mutex);
260 ifconfig_param_remote_netmask_ = i;
261 return *this;
264 window_size_t Options::getSeqWindowSize()
266 return seq_window_size_;
269 Options& Options::setSeqWindowSize(window_size_t s)
271 seq_window_size_ = s;
272 return *this;
276 std::string Options::getKdPrf()
278 Lock lock(mutex);
279 return kd_prf_;
282 Options& Options::setKdPrf(std::string k)
284 Lock lock(mutex);
285 kd_prf_ = k;
286 return *this;
289 u_int16_t Options::getMux()
291 Lock lock(mutex);
292 return mux_;
295 Options& Options::setMux(u_int16_t m)
297 Lock lock(mutex);
298 mux_ = m;
299 return *this;
302 u_int16_t Options::getNetworkPrefixLength()
304 Lock lock(mutex);
305 return network_prefix_length_;
308 Options& Options::setNetworkPrefixLength(u_int16_t l)
310 Lock lock(mutex);
311 network_prefix_length_ = l;
312 return *this;
315 Buffer Options::getKey()
317 Lock lock(mutex);
318 return key_;
321 Options& Options::setKey(std::string k)
323 Lock lock(mutex);
324 key_ = k;
325 return *this;
328 Buffer Options::getSalt()
330 Lock lock(mutex);
331 return salt_;
334 Options& Options::setSalt(std::string s)
336 Lock lock(mutex);
337 salt_ = s;
338 return *this;