another test
[anytun.git] / anymuxOptions.cpp
blob92726e73ebc691fdd6e70e2cd2ce45e9c8659877
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 "anymuxOptions.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()
55 progname_ = "anymux";
56 local_port_ = 1234;
57 file_name_ = "";
60 Options::~Options()
64 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
65 else if(str == SHORT || str == LONG) \
66 VALUE = true;
68 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
69 else if(str == SHORT || str == LONG) \
70 VALUE = false;
72 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
73 else if(str == SHORT || str == LONG) \
74 { \
75 if(argc < 1 || argv[i+1][0] == '-') \
76 return false; \
77 std::stringstream tmp; \
78 tmp << argv[i+1]; \
79 tmp >> VALUE; \
80 argc--; \
81 i++; \
84 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
85 else if(str == SHORT || str == LONG) \
86 { \
87 if(argc < 2 || \
88 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
89 return false; \
90 std::stringstream tmp; \
91 tmp << argv[i+1] << " " << argv[i+2]; \
92 tmp >> VALUE1; \
93 tmp >> VALUE2; \
94 argc-=2; \
95 i+=2; \
98 #define PARSE_HEXSTRING_PARAM(SHORT, LONG, VALUE) \
99 else if(str == SHORT || str == LONG) \
101 if(argc < 1 || argv[i+1][0] == '-') \
102 return false; \
103 VALUE = Buffer(std::string(argv[i+1])); \
104 argc--; \
105 i++; \
108 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
109 else if(str == SHORT || str == LONG) \
111 if(argc < 1 || argv[i+1][0] == '-') \
112 return false; \
113 std::stringstream tmp(argv[i+1]); \
114 while (tmp.good()) \
116 std::string tmp_line; \
117 getline(tmp,tmp_line,','); \
118 LIST.push(tmp_line); \
120 argc--; \
121 i++; \
124 bool Options::parse(int argc, char* argv[])
126 Lock lock(mutex);
128 progname_ = argv[0];
129 argc--;
131 for(int i=1; argc > 0; ++i)
133 std::string str(argv[i]);
134 argc--;
136 if(str == "-h" || str == "--help")
137 return false;
138 PARSE_SCALAR_PARAM("-p","--port", local_port_)
139 PARSE_SCALAR_PARAM("-f","--file", file_name_)
140 else
141 return false;
144 return true;
147 void Options::printUsage()
149 std::cout << "USAGE:" << std::endl;
150 std::cout << "anymux [-h|--help] prints this..." << std::endl;
151 std::cout << " [-p|--port] <port> local port to bind to" << std::endl;
152 std::cout << " [-f|--file] <path> path to file" << std::endl;
155 void Options::printOptions()
157 Lock lock(mutex);
158 std::cout << "Options:" << std::endl;
159 std::cout << "local_port='" << local_port_ << "'" << std::endl;
162 std::string Options::getProgname()
164 Lock lock(mutex);
165 return progname_;
169 Options& Options::setProgname(std::string p)
171 Lock lock(mutex);
172 progname_ = p;
173 return *this;
176 std::string Options::getFileName()
178 Lock lock(mutex);
179 return file_name_;
183 Options& Options::setFileName(std::string f)
185 Lock lock(mutex);
186 file_name_ = f;
187 return *this;
190 u_int16_t Options::getLocalPort()
192 return local_port_;
195 Options& Options::setLocalPort(u_int16_t l)
197 local_port_ = l;
198 return *this;