Imported Upstream version 0.3
[anytun.git] / src / options.h
1 /*
2  *  anytun
3  *
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.
12  *
13  *
14  *  Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, 
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
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.
22  *
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.
27  *
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/>.
30  */
31
32 #ifndef _OPTIONS_H_
33 #define _OPTIONS_H_
34
35 #include "datatypes.h"
36 #include "buffer.h"
37 #include "threadUtils.hpp"
38 #include <list>
39
40 class syntax_error : public std::runtime_error
41 {
42 public:
43   syntax_error(std::string t, int32_t p) : runtime_error(t), pos(p) {};
44   int32_t pos;
45 };
46 std::ostream& operator<<(std::ostream& stream, syntax_error const& error);
47
48 class OptionHost
49 {
50 public:
51   OptionHost() : addr(""), port("") {};
52   OptionHost(std::string addrPort) { init(addrPort); };
53   OptionHost(std::string a, std::string p) : addr(a), port(p) {};
54
55   void init(std::string addrPort);
56
57   std::string addr;
58         std::string port;
59 };
60 typedef std::list<OptionHost> HostList;
61 std::istream& operator>>(std::istream& stream, OptionHost& host);
62
63 class OptionNetwork
64 {
65 public:
66   OptionNetwork() : net_addr(""), prefix_length(0) {};
67   OptionNetwork(std::string network) { init(network); };
68   OptionNetwork(std::string n, u_int16_t p) : net_addr(n), prefix_length(p) {};
69
70   void init(std::string network);
71
72   std::string net_addr;
73   u_int16_t prefix_length;
74 };
75 typedef std::list<OptionNetwork> NetworkList;
76 std::istream& operator>>(std::istream& stream, OptionNetwork& network);
77
78 typedef std::list<std::string> StringList;
79
80 typedef enum { ROLE_LEFT, ROLE_RIGHT } role_t;
81 std::ostream& operator<<(std::ostream& stream, role_t const& role);
82
83 class Options
84 {
85 public:
86   static Options& instance();
87
88   bool parse(int argc, char* argv[]);
89   void parse_post();
90   void printUsage();
91   void printOptions();
92
93   std::string getProgname();
94   Options& setProgname(std::string p);
95   bool getDaemonize();
96   Options& setDaemonize(bool d);
97   std::string getUsername();
98   Options& setUsername(std::string u);
99   std::string getGroupname();
100   Options& setGroupname(std::string g);
101   std::string getChrootDir();
102   Options& setChrootDir(std::string c);
103   std::string getPidFile();
104   Options& setPidFile(std::string p);
105
106   StringList getLogTargets();
107
108   std::string getFileName();
109   Options& setFileName(std::string f);
110   std::string getBindToAddr();
111   Options& setBindToAddr(std::string b);
112   std::string getBindToPort();
113   Options& setBindToPort(std::string b);
114
115   ResolvAddrType getResolvAddrType();
116   Options& setResolvAddrType(ResolvAddrType r);
117   std::string getLocalAddr();
118   Options& setLocalAddr(std::string l);
119   std::string getLocalPort();
120   Options& setLocalPort(std::string l);
121   std::string getRemoteAddr();
122   Options& setRemoteAddr(std::string r);
123   std::string getRemotePort();
124   Options& setRemotePort(std::string r);
125
126   std::string getLocalSyncAddr();
127   Options& setLocalSyncAddr(std::string l);
128   std::string getLocalSyncPort();
129   Options& setLocalSyncPort(std::string l);
130         HostList getRemoteSyncHosts();
131
132   std::string getDevName();
133   Options& setDevName(std::string d);
134   std::string getDevType();
135   Options& setDevType(std::string d);
136   OptionNetwork getIfconfigParam();
137   Options& setIfconfigParam(OptionNetwork i);
138   std::string getPostUpScript();
139   Options& setPostUpScript(std::string p);
140   NetworkList getRoutes();
141
142   sender_id_t getSenderId();
143   Options& setSenderId(sender_id_t s);
144   mux_t getMux();
145   Options& setMux(mux_t m);
146   window_size_t getSeqWindowSize();
147   Options& setSeqWindowSize(window_size_t s);
148
149   std::string getCipher();
150   Options& setCipher(std::string c);
151   std::string getAuthAlgo();
152   Options& setAuthAlgo(std::string a);
153   u_int32_t getAuthTagLength();
154   Options& setAuthTagLength(u_int32_t a);
155   std::string getKdPrf();
156   Options& setKdPrf(std::string k);
157   role_t getRole();
158   Options& setRole(role_t r);
159   std::string getPassphrase();
160   Options& setPassphrase(std::string p);
161   Options& setKey(std::string k);
162   Buffer getKey();
163   Options& setSalt(std::string s);
164   Buffer getSalt();
165
166
167 private:
168   Options();
169   ~Options();
170   Options(const Options &l);
171   void operator=(const Options &l);
172
173   static Options* inst;
174   static ::Mutex instMutex;
175   class instanceCleaner {
176     public: ~instanceCleaner() {
177       if(Options::inst != 0)
178         delete Options::inst;
179     }
180   };
181   friend class instanceCleaner;
182
183   ::SharedMutex mutex;
184
185
186   bool cluster_opts;
187   bool connection_opts;
188
189   std::string progname_;
190   bool daemonize_;
191   std::string username_;
192   std::string groupname_;
193   std::string chroot_dir_;
194   std::string pid_file_;
195
196   StringList log_targets_;
197
198   std::string file_name_;
199   OptionHost bind_to_;
200
201   ResolvAddrType resolv_addr_type_;
202   OptionHost local_;
203   OptionHost remote_;
204
205   OptionHost local_sync_;
206         HostList remote_sync_hosts_;
207
208   std::string dev_name_;
209   std::string dev_type_;
210   OptionNetwork ifconfig_param_;
211   std::string post_up_script_;
212   NetworkList routes_;
213
214   sender_id_t sender_id_;
215   mux_t mux_;
216   window_size_t seq_window_size_;
217
218   std::string cipher_;
219   std::string auth_algo_;
220   u_int32_t auth_tag_length_;
221   std::string kd_prf_;
222   role_t role_;
223   std::string passphrase_;
224   Buffer key_;
225   Buffer salt_;
226 };
227
228 extern Options& gOpt;
229
230 #endif