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