Imported Upstream version 0.3.2
[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 } target_type_t;
80
81   static target_type_t targetTypeFromString(std::string type);
82   static std::string targetTypeToString(target_type_t type);
83
84   ~LogTargetList();
85   LogTarget* add(std::string conf);
86   LogTarget* add(target_type_t type, int prio, std::string conf);
87   void clear();
88   
89   void log(std::string msg, int prio);
90
91 private:
92   typedef std::multimap<target_type_t, LogTarget*> TargetsMap;
93   TargetsMap targets;
94 };
95
96
97 #ifdef LOG_SYSLOG
98 class LogTargetSyslog : public LogTarget
99 {
100 public:
101   static const int FAC_USER = LOG_USER;
102   static const int FAC_MAIL = LOG_MAIL;
103   static const int FAC_DAEMON = LOG_DAEMON;
104   static const int FAC_AUTH = LOG_AUTH;
105   static const int FAC_SYSLOG = LOG_SYSLOG;
106   static const int FAC_LPR = LOG_LPR;
107   static const int FAC_NEWS = LOG_NEWS;
108   static const int FAC_UUCP = LOG_UUCP;
109   static const int FAC_CRON = LOG_CRON;
110   static const int FAC_AUTHPRIV = LOG_AUTHPRIV;
111   static const int FAC_FTP = LOG_FTP;
112   static const int FAC_LOCAL0 = LOG_LOCAL0;
113   static const int FAC_LOCAL1 = LOG_LOCAL1;
114   static const int FAC_LOCAL2 = LOG_LOCAL2;
115   static const int FAC_LOCAL3 = LOG_LOCAL3;
116   static const int FAC_LOCAL4 = LOG_LOCAL4;
117   static const int FAC_LOCAL5 = LOG_LOCAL5;
118   static const int FAC_LOCAL6 = LOG_LOCAL6;
119   static const int FAC_LOCAL7 = LOG_LOCAL7;
120
121   static int facilityFromString(std::string fac);
122   static std::string facilityToString(int fac);
123
124   LogTargetSyslog(int prio, std::string conf);
125   ~LogTargetSyslog();
126
127   void open();
128   void close();
129   void log(std::string msg, int prio);  
130   static bool duplicateAllowed() { return false; };
131
132   LogTargetSyslog& setLogName(std::string l); 
133   std::string getLogName() const { return logname; }
134   LogTargetSyslog& setFacility(int f);
135   int getFacility() const { return facility; }
136   
137 private:
138   std::string logname;
139   int facility;
140 };
141 #endif
142
143 #ifdef LOG_FILE
144 class LogTargetFile : public LogTarget
145 {
146 public:
147   LogTargetFile(int prio, std::string conf);
148   ~LogTargetFile();
149
150   void open();
151   void close();
152   void log(std::string msg, int prio);  
153   static bool duplicateAllowed() { return true; };
154
155   LogTargetFile& setLogFilename(std::string l); 
156   std::string getLogFilename() const { return logfilename; }
157   
158 private:
159   std::string logfilename;
160   std::ofstream logfile;
161 };
162 #endif
163
164 #ifdef LOG_STDOUT
165 class LogTargetStdout : public LogTarget
166 {
167 public:
168   LogTargetStdout(int prio, std::ostream& s);
169   ~LogTargetStdout();
170
171   void open();
172   void close();
173   void log(std::string msg, int prio);  
174   static bool duplicateAllowed() { return false; };
175
176 private:
177   std::ostream& stream;
178 };
179 #endif
180
181 #ifdef LOG_WINEVENTLOG
182 class LogTargetWinEventlog : public LogTarget
183 {
184 public:
185   static WORD prioToEventLogType(int prio);
186
187   LogTargetWinEventlog(int prio, std::string conf);
188   ~LogTargetWinEventlog();
189
190   void open();
191   void close();
192   void log(std::string msg, int prio);  
193   static bool duplicateAllowed() { return false; };
194
195   LogTargetWinEventlog& setLogName(std::string l); 
196   std::string getLogName() const { return logname; };
197
198 private:
199   std::string logname;
200   HANDLE h_event_source;
201 };
202 #endif
203
204 #endif