Imported Upstream version 0.3.5
[anytun.git] / src / logTargets.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 methods 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-2014 Markus Grüneis, 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  *  In addition, as a special exception, the copyright holders give
33  *  permission to link the code of portions of this program with the
34  *  OpenSSL library under certain conditions as described in each
35  *  individual source file, and distribute linked combinations
36  *  including the two.
37  *  You must obey the GNU General Public License in all respects
38  *  for all of the code used other than OpenSSL.  If you modify
39  *  file(s) with this exception, you may extend this exception to your
40  *  version of the file(s), but you are not obligated to do so.  If you
41  *  do not wish to do so, delete this exception statement from your
42  *  version.  If you delete this exception statement from all source
43  *  files in the program, then also delete it here.
44  */
45
46 #ifndef ANYTUN_logTargets_h_INCLUDED
47 #define ANYTUN_logTargets_h_INCLUDED
48
49 #include <string>
50 #include <map>
51
52 #ifdef LOG_SYSLOG
53 #include <syslog.h>
54 #endif
55
56 #ifdef LOG_FILE
57 #include <fstream>
58 #endif
59
60 #include "datatypes.h"
61
62 class LogTarget
63 {
64 public:
65   LogTarget();
66   LogTarget(int prio);
67   virtual ~LogTarget() {};
68
69   virtual void open() = 0;
70   virtual void close() = 0;
71   bool isOpen() { return opened; };
72
73   void enable() { enabled = true; };
74   void disable() { enabled = false; };
75   bool isEnabled() { return enabled; };
76
77   int getMaxPrio() { return max_prio; };
78   void setMaxPrio(int p) { max_prio = p; };
79
80   virtual void log(std::string msg, int prio) = 0;
81
82 protected:
83   bool opened;
84   bool enabled;
85   int max_prio;
86 };
87
88 class LogTargetList
89 {
90 public:
91   typedef enum { TARGET_UNKNOWN, TARGET_SYSLOG, TARGET_FILE,
92                  TARGET_STDOUT, TARGET_STDERR, TARGET_WINEVENTLOG
93                } target_type_t;
94
95   static target_type_t targetTypeFromString(std::string type);
96   static std::string targetTypeToString(target_type_t type);
97
98   ~LogTargetList();
99   LogTarget* add(std::string conf);
100   LogTarget* add(target_type_t type, int prio, std::string conf);
101   void clear();
102
103   void log(std::string msg, int prio);
104
105 private:
106   typedef std::multimap<target_type_t, LogTarget*> TargetsMap;
107   TargetsMap targets;
108 };
109
110
111 #ifdef LOG_SYSLOG
112 class LogTargetSyslog : public LogTarget
113 {
114 public:
115   static const int FAC_USER = LOG_USER;
116   static const int FAC_MAIL = LOG_MAIL;
117   static const int FAC_DAEMON = LOG_DAEMON;
118   static const int FAC_AUTH = LOG_AUTH;
119   static const int FAC_SYSLOG = LOG_SYSLOG;
120   static const int FAC_LPR = LOG_LPR;
121   static const int FAC_NEWS = LOG_NEWS;
122   static const int FAC_UUCP = LOG_UUCP;
123   static const int FAC_CRON = LOG_CRON;
124   static const int FAC_AUTHPRIV = LOG_AUTHPRIV;
125   static const int FAC_FTP = LOG_FTP;
126   static const int FAC_LOCAL0 = LOG_LOCAL0;
127   static const int FAC_LOCAL1 = LOG_LOCAL1;
128   static const int FAC_LOCAL2 = LOG_LOCAL2;
129   static const int FAC_LOCAL3 = LOG_LOCAL3;
130   static const int FAC_LOCAL4 = LOG_LOCAL4;
131   static const int FAC_LOCAL5 = LOG_LOCAL5;
132   static const int FAC_LOCAL6 = LOG_LOCAL6;
133   static const int FAC_LOCAL7 = LOG_LOCAL7;
134
135   static int facilityFromString(std::string fac);
136   static std::string facilityToString(int fac);
137
138   LogTargetSyslog(int prio, std::string conf);
139   ~LogTargetSyslog();
140
141   void open();
142   void close();
143   void log(std::string msg, int prio);
144   static bool duplicateAllowed() { return false; };
145
146   LogTargetSyslog& setLogName(std::string l);
147   std::string getLogName() const { return logname; }
148   LogTargetSyslog& setFacility(int f);
149   int getFacility() const { return facility; }
150
151 private:
152   std::string logname;
153   int facility;
154 };
155 #endif
156
157 #ifdef LOG_FILE
158 class LogTargetFile : public LogTarget
159 {
160 public:
161   LogTargetFile(int prio, std::string conf);
162   ~LogTargetFile();
163
164   void open();
165   void close();
166   void log(std::string msg, int prio);
167   static bool duplicateAllowed() { return true; };
168
169   LogTargetFile& setLogFilename(std::string l);
170   std::string getLogFilename() const { return logfilename; }
171
172 private:
173   std::string logfilename;
174   std::ofstream logfile;
175 };
176 #endif
177
178 #ifdef LOG_STDOUT
179 class LogTargetStdout : public LogTarget
180 {
181 public:
182   LogTargetStdout(int prio, std::ostream& s);
183   ~LogTargetStdout();
184
185   void open();
186   void close();
187   void log(std::string msg, int prio);
188   static bool duplicateAllowed() { return false; };
189
190 private:
191   std::ostream& stream;
192 };
193 #endif
194
195 #ifdef LOG_WINEVENTLOG
196 class LogTargetWinEventlog : public LogTarget
197 {
198 public:
199   static WORD prioToEventLogType(int prio);
200
201   LogTargetWinEventlog(int prio, std::string conf);
202   ~LogTargetWinEventlog();
203
204   void open();
205   void close();
206   void log(std::string msg, int prio);
207   static bool duplicateAllowed() { return false; };
208
209   LogTargetWinEventlog& setLogName(std::string l);
210   std::string getLogName() const { return logname; };
211
212 private:
213   std::string logname;
214   HANDLE h_event_source;
215 };
216 #endif
217
218 #endif