changed Foobar to anytun @ file header
[anytun.git] / src / anyConfOptions.cpp
blob05114cc3c3a86f14deb7c53f074f8c32a9e84ec1
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-2008 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
19 * Anytun is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
32 #include <iostream>
33 #include <queue>
34 #include <string>
35 #include <sstream>
37 #include "datatypes.h"
38 #include "anyConfOptions.h"
40 Options* Options::inst = NULL;
41 Mutex Options::instMutex;
42 Options& gOpt = Options::instance();
44 Options& Options::instance()
46 Lock lock(instMutex);
47 static instanceCleaner c;
48 if(!inst)
49 inst = new Options();
51 return *inst;
54 Options::Options() : key_(u_int32_t(0)), salt_(u_int32_t(0))
56 progname_ = "anytun-config";
57 remote_addr_ = "";
58 remote_port_ = 4444;
59 seq_window_size_ = 100;
60 kd_prf_ = "aes-ctr";
61 mux_ = 0;
64 Options::~Options()
68 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
69 else if(str == SHORT || str == LONG) \
70 VALUE = true;
72 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
73 else if(str == SHORT || str == LONG) \
74 VALUE = false;
76 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
77 else if(str == SHORT || str == LONG) \
78 { \
79 if(argc < 1 || argv[i+1][0] == '-') \
80 return false; \
81 std::stringstream tmp; \
82 tmp << argv[i+1]; \
83 tmp >> VALUE; \
84 argc--; \
85 i++; \
88 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
89 else if(str == SHORT || str == LONG) \
90 { \
91 if(argc < 2 || \
92 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
93 return false; \
94 std::stringstream tmp; \
95 tmp << argv[i+1] << " " << argv[i+2]; \
96 tmp >> VALUE1; \
97 tmp >> VALUE2; \
98 argc-=2; \
99 i+=2; \
102 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \
103 else if(str == SHORT || str == LONG) \
105 if(argc < 1 || argv[i+1][0] == '-') \
106 return false; \
107 VALUE = Buffer(std::string(argv[i+1])); \
108 for(size_t j=0; j < strlen(argv[i+1]); ++j) \
109 argv[i+1][j] = '#'; \
110 argc--; \
111 i++; \
114 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
115 else if(str == SHORT || str == LONG) \
117 if(argc < 1 || argv[i+1][0] == '-') \
118 return false; \
119 std::stringstream tmp(argv[i+1]); \
120 while (tmp.good()) \
122 std::string tmp_line; \
123 getline(tmp,tmp_line,','); \
124 LIST.push(tmp_line); \
126 argc--; \
127 i++; \
130 bool Options::parse(int argc, char* argv[])
132 Lock lock(mutex);
134 progname_ = argv[0];
135 argc--;
136 std::queue<std::string> route_queue;
137 for(int i=1; argc > 0; ++i)
139 std::string str(argv[i]);
140 argc--;
142 if(str == "-h" || str == "--help")
143 return false;
144 PARSE_SCALAR_PARAM("-r","--remote-host", remote_addr_)
145 PARSE_SCALAR_PARAM("-o","--remote-port", remote_port_)
146 PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_)
147 PARSE_SCALAR_PARAM("-m","--mux", mux_)
148 PARSE_HEXSTRING_PARAM_SEC("-K","--key", key_)
149 PARSE_HEXSTRING_PARAM_SEC("-A","--salt", salt_)
150 PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_)
151 PARSE_CSLIST_PARAM("-R","--route", route_queue)
152 else
153 return false;
156 while(!route_queue.empty())
158 std::stringstream tmp_stream(route_queue.front());
159 OptionRoute rt;
160 getline(tmp_stream,rt.net_addr,'/');
161 if(!tmp_stream.good())
162 return false;
163 tmp_stream >> rt.prefix_length;
164 route_queue.pop();
165 routes_.push_back(rt);
167 return true;
170 void Options::printUsage()
172 std::cout << "USAGE:" << std::endl;
173 std::cout << "anytun-config [-h|--help] prints this..." << std::endl;
174 std::cout << " [-r|--remote-host] <hostname|ip> remote host" << std::endl;
175 std::cout << " [-o|--remote-port] <port> remote port" << std::endl;
176 std::cout << " [-w|--window-size] <window size> seqence number window size" << std::endl;
177 std::cout << " [-m|--mux] <mux-id> the multiplex id to use" << std::endl;
178 std::cout << " [-K|--key] <master key> master key to use for encryption" << std::endl;
179 std::cout << " [-A|--salt] <master salt> master salt to use for encryption" << std::endl;
180 // std::cout << " [-k|--kd-prf] <kd-prf type> key derivation pseudo random function" << std::endl;
181 std::cout << " [-R|--route] <net>/<prefix length> add a route to connection, can be invoked several times" << std::endl;
184 void Options::printOptions()
186 Lock lock(mutex);
187 std::cout << "Options:" << std::endl;
188 std::cout << "remote_addr='" << remote_addr_ << "'" << std::endl;
189 std::cout << "remote_port='" << remote_port_ << "'" << std::endl;
190 std::cout << "seq_window_size='" << seq_window_size_ << "'" << std::endl;
191 std::cout << "mux_id='" << mux_ << "'" << std::endl;
192 std::cout << "key=" << key_.getHexDumpOneLine() << std::endl;
193 std::cout << "salt=" << salt_.getHexDumpOneLine() << std::endl;
194 std::cout << "kd_prf='" << kd_prf_ << "'" << std::endl;
196 std::cout << "routes:" << std::endl;
197 RouteList::const_iterator rit;
198 for(rit = routes_.begin(); rit != routes_.end(); ++rit)
199 std::cout << " " << rit->net_addr << "/" << rit->prefix_length << 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 window_size_t Options::getSeqWindowSize()
251 return seq_window_size_;
254 Options& Options::setSeqWindowSize(window_size_t s)
256 seq_window_size_ = s;
257 return *this;
261 std::string Options::getKdPrf()
263 Lock lock(mutex);
264 return kd_prf_;
267 Options& Options::setKdPrf(std::string k)
269 Lock lock(mutex);
270 kd_prf_ = k;
271 return *this;
274 u_int16_t Options::getMux()
276 Lock lock(mutex);
277 return mux_;
280 Options& Options::setMux(u_int16_t m)
282 Lock lock(mutex);
283 mux_ = m;
284 return *this;
287 Buffer Options::getKey()
289 Lock lock(mutex);
290 return key_;
293 Options& Options::setKey(std::string k)
295 Lock lock(mutex);
296 key_ = k;
297 return *this;
300 Buffer Options::getSalt()
302 Lock lock(mutex);
303 return salt_;
306 Options& Options::setSalt(std::string s)
308 Lock lock(mutex);
309 salt_ = s;
310 return *this;
313 RouteList Options::getRoutes()
315 Lock lock(mutex);
316 return routes_;