Imported Upstream version 0.3.4
[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 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_logTargets_h_INCLUDED
34 #define ANYTUN_logTargets_h_INCLUDED
35
36 #include <string>
37 #include <map>
38
39 #ifdef LOG_SYSLOG
40 #include <syslog.h>
41 #endif
42
43 #ifdef LOG_FILE
44 #include <fstream>
45 #endif
46
47 #include "datatypes.h"
48
49 class LogTarget
50 {
51 public:
52   LogTarget();
53   LogTarget(int prio);
54   virtual ~LogTarget() {};
55
56   virtual void open() = 0;
57   virtual void close() = 0;
58   bool isOpen() { return opened; };
59
60   void enable() { enabled = true; };
61   void disable() { enabled = false; };
62   bool isEnabled() { return enabled; };
63
64   int getMaxPrio() { return max_prio; };
65   void setMaxPrio(int p) { max_prio = p; };
66
67   virtual void log(std::string msg, int prio) = 0;
68
69 protected:
70   bool opened;
71   bool enabled;
72   int max_prio;
73 };
74
75 class LogTargetList
76 {
77 public:
78   typedef enum { TARGET_UNKNOWN, TARGET_SYSLOG, TARGET_FILE,
79                  TARGET_STDOUT, TARGET_STDERR, TARGET_WINEVENTLOG
80                } target_type_t;
81
82   static target_type_t targetTypeFromString(std::string type);
83   static std::string targetTypeToString(target_type_t type);
84
85   ~LogTargetList();
86   LogTarget* add(std::string conf);
87   LogTarget* add(target_type_t type, int prio, std::string conf);
88   void clear();
89
90   void log(std::string msg, int prio);
91
92 private:
93   typedef std::multimap<target_type_t, LogTarget*> TargetsMap;
94   TargetsMap targets;
95 };
96
97
98 #ifdef LOG_SYSLOG
99 class LogTargetSyslog : public LogTarget
100 {
101 public:
102   static const int FAC_USER = LOG_USER;
103   static const int FAC_MAIL = LOG_MAIL;
104   static const int FAC_DAEMON = LOG_DAEMON;
105   static const int FAC_AUTH = LOG_AUTH;
106   static const int FAC_SYSLOG = LOG_SYSLOG;
107   static const int FAC_LPR = LOG_LPR;
108   static const int FAC_NEWS = LOG_NEWS;
109   static const int FAC_UUCP = LOG_UUCP;
110   static const int FAC_CRON = LOG_CRON;
111   static const int FAC_AUTHPRIV = LOG_AUTHPRIV;
112   static const int FAC_FTP = LOG_FTP;
113   static const int FAC_LOCAL0 = LOG_LOCAL0;
114   static const int FAC_LOCAL1 = LOG_LOCAL1;
115   static const int FAC_LOCAL2 = LOG_LOCAL2;
116   static const int FAC_LOCAL3 = LOG_LOCAL3;
117   static const int FAC_LOCAL4 = LOG_LOCAL4;
118   static const int FAC_LOCAL5 = LOG_LOCAL5;
119   static const int FAC_LOCAL6 = LOG_LOCAL6;
120   static const int FAC_LOCAL7 = LOG_LOCAL7;
121
122   static int facilityFromString(std::string fac);
123   static std::string facilityToString(int fac);
124
125   LogTargetSyslog(int prio, std::string conf);
126   ~LogTargetSyslog();
127
128   void open();
129   void close();
130   void log(std::string msg, int prio);
131   static bool duplicateAllowed() { return false; };
132
133   LogTargetSyslog& setLogName(std::string l);
134   std::string getLogName() const { return logname; }
135   LogTargetSyslog& setFacility(int f);
136   int getFacility() const { return facility; }
137
138 private:
139   std::string logname;
140   int facility;
141 };
142 #endif
143
144 #ifdef LOG_FILE
145 class LogTargetFile : public LogTarget
146 {
147 public:
148   LogTargetFile(int prio, std::string conf);
149   ~LogTargetFile();
150
151   void open();
152   void close();
153   void log(std::string msg, int prio);
154   static bool duplicateAllowed() { return true; };
155
156   LogTargetFile& setLogFilename(std::string l);
157   std::string getLogFilename() const { return logfilename; }
158
159 private:
160   std::string logfilename;
161   std::ofstream logfile;
162 };
163 #endif
164
165 #ifdef LOG_STDOUT
166 class LogTargetStdout : public LogTarget
167 {
168 public:
169   LogTargetStdout(int prio, std::ostream& s);
170   ~LogTargetStdout();
171
172   void open();
173   void close();
174   void log(std::string msg, int prio);
175   static bool duplicateAllowed() { return false; };
176
177 private:
178   std::ostream& stream;
179 };
180 #endif
181
182 #ifdef LOG_WINEVENTLOG
183 class LogTargetWinEventlog : public LogTarget
184 {
185 public:
186   static WORD prioToEventLogType(int prio);
187
188   LogTargetWinEventlog(int prio, std::string conf);
189   ~LogTargetWinEventlog();
190
191   void open();
192   void close();
193   void log(std::string msg, int prio);
194   static bool duplicateAllowed() { return false; };
195
196   LogTargetWinEventlog& setLogName(std::string l);
197   std::string getLogName() const { return logname; };
198
199 private:
200   std::string logname;
201   HANDLE h_event_source;
202 };
203 #endif
204
205 #endif