working version with crypto
[anytun.git] / anyctrOptions.cpp
blobd6b0689b6c6237d6281cae80eb5c8d4ad9f76871
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(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 argc--; \
110 i++; \
113 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
114 else if(str == SHORT || str == LONG) \
116 if(argc < 1 || argv[i+1][0] == '-') \
117 return false; \
118 std::stringstream tmp(argv[i+1]); \
119 while (tmp.good()) \
121 std::string tmp_line; \
122 getline(tmp,tmp_line,','); \
123 LIST.push(tmp_line); \
125 argc--; \
126 i++; \
129 bool Options::parse(int argc, char* argv[])
131 Lock lock(mutex);
133 progname_ = argv[0];
134 argc--;
135 std::queue<std::string> host_port_queue;
136 for(int i=1; argc > 0; ++i)
138 std::string str(argv[i]);
139 argc--;
141 if(str == "-h" || str == "--help")
142 return false;
143 PARSE_SCALAR_PARAM("-r","--remote-host", remote_addr_)
144 PARSE_SCALAR_PARAM("-o","--remote-port", remote_port_)
145 PARSE_SCALAR_PARAM("-n","--prefix", ifconfig_param_remote_netmask_)
146 PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_)
147 PARSE_SCALAR_PARAM("-m","--mux", mux_)
148 PARSE_SCALAR_PARAM("-l","--prefix-len", network_prefix_length_)
149 PARSE_HEXSTRING_PARAM("-K","--key", key_)
150 PARSE_HEXSTRING_PARAM("-A","--salt", salt_)
151 PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_)
152 else
153 return false;
156 while(!host_port_queue.empty())
158 std::stringstream tmp_stream(host_port_queue.front());
159 OptionConnectTo oct;
160 getline(tmp_stream,oct.host,':');
161 if(!tmp_stream.good())
162 return false;
163 tmp_stream >> oct.port;
164 host_port_queue.pop();
165 connect_to_.push_back(oct);
167 return true;
170 void Options::printUsage()
172 std::cout << "USAGE:" << std::endl;
173 std::cout << "anytun-config" << std::endl;
174 std::cout << " [-h|--help] prints this..." << std::endl;
175 std::cout << " [-r|--remote-host] <hostname|ip> remote host" << std::endl;
176 std::cout << " [-o|--remote-port] <port> remote port" << std::endl;
177 std::cout << " [-n|--prefix] <remote net> remote subnet for route" << std::endl;
178 std::cout << " [-w|--window-size] <window size> seqence number window size" << std::endl;
179 std::cout << " [-m|--mux] <mux-id> the multiplex id to use" << std::endl;
180 std::cout << " [-l|--prefix-len] <prefix length> network prefix length" << std::endl;
181 std::cout << " [-K|--key] <master key> master key to use for encryption" << std::endl;
182 std::cout << " [-A|--salt] <master salt> master salt to use for encryption" << std::endl;
183 std::cout << " [-k|--kd-prf] <kd-prf type> key derivation pseudo random function" << std::endl;
186 void Options::printOptions()
188 Lock lock(mutex);
189 std::cout << "Options:" << std::endl;
190 std::cout << "remote_addr='" << remote_addr_ << "'" << std::endl;
191 std::cout << "remote_port='" << remote_port_ << "'" << std::endl;
192 std::cout << "ifconfig_param_local='" << ifconfig_param_local_ << "'" << std::endl;
193 std::cout << "ifconfig_param_remote_netmask='" << ifconfig_param_remote_netmask_ << "'" << std::endl;
194 std::cout << "seq_window_size='" << seq_window_size_ << "'" << std::endl;
195 std::cout << "mux_id='" << mux_ << "'" << std::endl;
196 std::cout << "network_prefix_length='" << network_prefix_length_ << "'" << std::endl;
197 std::cout << "key=" << key_.getHexDumpOneLine() << std::endl;
198 std::cout << "salt=" << salt_.getHexDumpOneLine() << std::endl;
199 std::cout << "kd_prf='" << kd_prf_ << "'" << std::endl;
202 std::string Options::getProgname()
204 Lock lock(mutex);
205 return progname_;
209 Options& Options::setProgname(std::string p)
211 Lock lock(mutex);
212 progname_ = p;
213 return *this;
217 std::string Options::getRemoteAddr()
219 Lock lock(mutex);
220 return remote_addr_;
223 Options& Options::setRemoteAddr(std::string r)
225 Lock lock(mutex);
226 remote_addr_ = r;
227 return *this;
230 u_int16_t Options::getRemotePort()
232 return remote_port_;
235 Options& Options::setRemotePort(u_int16_t r)
237 remote_port_ = r;
238 return *this;
241 Options& Options::setRemoteAddrPort(std::string addr, u_int16_t port)
243 Lock lock(mutex);
244 remote_addr_ = addr;
245 remote_port_ = port;
246 return *this;
249 std::string Options::getIfconfigParamRemoteNetmask()
251 Lock lock(mutex);
252 return ifconfig_param_remote_netmask_;
255 Options& Options::setIfconfigParamRemoteNetmask(std::string i)
257 Lock lock(mutex);
258 ifconfig_param_remote_netmask_ = i;
259 return *this;
262 window_size_t Options::getSeqWindowSize()
264 return seq_window_size_;
267 Options& Options::setSeqWindowSize(window_size_t s)
269 seq_window_size_ = s;
270 return *this;
274 std::string Options::getKdPrf()
276 Lock lock(mutex);
277 return kd_prf_;
280 Options& Options::setKdPrf(std::string k)
282 Lock lock(mutex);
283 kd_prf_ = k;
284 return *this;
287 u_int16_t Options::getMux()
289 Lock lock(mutex);
290 return mux_;
293 Options& Options::setMux(u_int16_t m)
295 Lock lock(mutex);
296 mux_ = m;
297 return *this;
300 u_int16_t Options::getNetworkPrefixLength()
302 Lock lock(mutex);
303 return network_prefix_length_;
306 Options& Options::setNetworkPrefixLength(u_int16_t l)
308 Lock lock(mutex);
309 network_prefix_length_ = l;
310 return *this;
313 Buffer Options::getKey()
315 Lock lock(mutex);
316 return key_;
319 Options& Options::setKey(std::string k)
321 Lock lock(mutex);
322 key_ = k;
323 return *this;
326 Buffer Options::getSalt()
328 Lock lock(mutex);
329 return salt_;
332 Options& Options::setSalt(std::string s)
334 Lock lock(mutex);
335 salt_ = s;
336 return *this;