4 * uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full
5 * featured implementation uAnytun has no support for multiple connections
6 * or synchronisation. It is a small single threaded implementation intended
7 * to act as a client on small platforms.
8 * The secure anycast tunneling protocol (satp) defines a protocol used
9 * for communication between any combination of unicast and anycast
10 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
11 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
12 * ethernet, ip, arp ...). satp directly includes cryptography and
13 * message authentication based on the methodes used by SRTP. It is
14 * intended to deliver a generic, scaleable and secure solution for
15 * tunneling and relaying of packets of any protocol.
18 * Copyright (C) 2007-2010 Christian Pointner <equinox@anytun.org>
20 * This file is part of uAnytun.
22 * uAnytun is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
27 * uAnytun is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
36 #ifndef UANYTUN_log_targets_h_INCLUDED
37 #define UANYTUN_log_targets_h_INCLUDED
41 static char* get_time_formatted()
44 time_t t = time(NULL);
46 time_string = "<time read error>";
48 time_string = ctime(&t);
50 time_string = "<time format error>";
52 char* newline = strchr(time_string, '\n');
60 enum syslog_facility_enum { USER = LOG_USER, MAIL = LOG_MAIL,
61 DAEMON = LOG_DAEMON, AUTH = LOG_AUTH,
62 SYSLOG = LOG_SYSLOG, LPR = LOG_LPR,
63 NEWS = LOG_NEWS, UUCP = LOG_UUCP,
64 CRON = LOG_CRON, AUTHPRIV = LOG_AUTHPRIV,
65 FTP = LOG_FTP, LOCAL0 = LOG_LOCAL0,
66 LOCAL1 = LOG_LOCAL1, LOCAL2 = LOG_LOCAL2,
67 LOCAL3 = LOG_LOCAL3, LOCAL4 = LOG_LOCAL4,
68 LOCAL5 = LOG_LOCAL5, LOCAL6 = LOG_LOCAL6,
69 LOCAL7 = LOG_LOCAL7 };
70 typedef enum syslog_facility_enum syslog_facility_t;
72 struct log_target_syslog_param_struct {
74 syslog_facility_t facility_;
76 typedef struct log_target_syslog_param_struct log_target_syslog_param_t;
78 int log_target_syslog_init(log_target_t* self, const char* conf)
80 if(!self || (conf && conf[0] == 0))
83 self->param_ = malloc(sizeof(log_target_syslog_param_t));
88 const char* end = NULL;
90 logname = strdup("uanytun");
92 end = strchr(conf, ',');
94 size_t len = (size_t)(end - conf);
99 logname = malloc(len+1);
101 strncpy(logname, conf, len);
106 logname = strdup(conf);
113 ((log_target_syslog_param_t*)(self->param_))->logname_ = logname;
116 ((log_target_syslog_param_t*)(self->param_))->facility_ = DAEMON;
120 if(end[1] == 0 || end[1] == ',') {
126 const char* start = end + 1;
127 end = strchr(start, ',');
130 if(facilitynames[i].c_name == NULL) {
136 if(( end && !strncmp(start, facilitynames[i].c_name, (size_t)(end - start)) && facilitynames[i].c_name[(size_t)(end-start)] == 0) ||
137 (!end && !strcmp(start, facilitynames[i].c_name))) {
138 ((log_target_syslog_param_t*)(self->param_))->facility_ = facilitynames[i].c_val;
146 void log_target_syslog_open(log_target_t* self)
148 if(!self || !self->param_)
151 openlog(((log_target_syslog_param_t*)(self->param_))->logname_, LOG_PID, ((log_target_syslog_param_t*)(self->param_))->facility_);
155 void log_target_syslog_log(log_target_t* self, log_prio_t prio, const char* msg)
157 if(!self || !self->param_ || !self->opened_)
160 syslog((prio + 2) | ((log_target_syslog_param_t*)(self->param_))->facility_, "%s", msg);
163 void log_target_syslog_close(log_target_t* self)
169 void log_target_syslog_clear(log_target_t* self)
171 if(!self || !self->param_)
174 if(((log_target_syslog_param_t*)(self->param_))->logname_)
175 free(((log_target_syslog_param_t*)(self->param_))->logname_);
180 log_target_t* log_target_syslog_new()
182 log_target_t* tmp = malloc(sizeof(log_target_t));
186 tmp->type_ = TARGET_SYSLOG;
187 tmp->init = &log_target_syslog_init;
188 tmp->open = &log_target_syslog_open;
189 tmp->log = &log_target_syslog_log;
190 tmp->close = &log_target_syslog_close;
191 tmp->clear = &log_target_syslog_clear;
194 tmp->max_prio_ = NOTICE;
202 struct log_target_file_param_struct {
206 typedef struct log_target_file_param_struct log_target_file_param_t;
208 int log_target_file_init(log_target_t* self, const char* conf)
210 if(!self || (conf && conf[0] == 0))
213 self->param_ = malloc(sizeof(log_target_file_param_t));
219 logfilename = strdup("uanytun.log");
221 const char* end = strchr(conf, ',');
223 size_t len = (size_t)(end - conf);
228 logfilename = malloc(len+1);
230 strncpy(logfilename, conf, len);
231 logfilename[len] = 0;
235 logfilename = strdup(conf);
242 ((log_target_file_param_t*)(self->param_))->logfilename_ = logfilename;
243 ((log_target_file_param_t*)(self->param_))->file_ = NULL;
248 void log_target_file_open(log_target_t* self)
250 if(!self || !self->param_)
253 ((log_target_file_param_t*)(self->param_))->file_ = fopen(((log_target_file_param_t*)(self->param_))->logfilename_, "w");
254 if(((log_target_file_param_t*)(self->param_))->file_)
258 void log_target_file_log(log_target_t* self, log_prio_t prio, const char* msg)
260 if(!self || !self->param_ || !self->opened_)
263 fprintf(((log_target_file_param_t*)(self->param_))->file_, "%s %s: %s\n", get_time_formatted(), log_prio_to_string(prio), msg);
264 fflush(((log_target_file_param_t*)(self->param_))->file_);
267 void log_target_file_close(log_target_t* self)
269 if(!self || !self->param_)
272 fclose(((log_target_file_param_t*)(self->param_))->file_);
276 void log_target_file_clear(log_target_t* self)
278 if(!self || !self->param_)
281 if(((log_target_file_param_t*)(self->param_))->logfilename_)
282 free(((log_target_file_param_t*)(self->param_))->logfilename_);
288 log_target_t* log_target_file_new()
290 log_target_t* tmp = malloc(sizeof(log_target_t));
294 tmp->type_ = TARGET_FILE;
295 tmp->init = &log_target_file_init;
296 tmp->open = &log_target_file_open;
297 tmp->log = &log_target_file_log;
298 tmp->close = &log_target_file_close;
299 tmp->clear = &log_target_file_clear;
302 tmp->max_prio_ = NOTICE;
310 void log_target_stdout_log(log_target_t* self, log_prio_t prio, const char* msg)
312 printf("%s %s: %s\n", get_time_formatted(), log_prio_to_string(prio), msg);
315 log_target_t* log_target_stdout_new()
317 log_target_t* tmp = malloc(sizeof(log_target_t));
321 tmp->type_ = TARGET_STDOUT;
324 tmp->log = &log_target_stdout_log;
329 tmp->max_prio_ = NOTICE;
337 void log_target_stderr_log(log_target_t* self, log_prio_t prio, const char* msg)
339 fprintf(stderr, "%s %s: %s\n", get_time_formatted(), log_prio_to_string(prio), msg);
342 log_target_t* log_target_stderr_new()
344 log_target_t* tmp = malloc(sizeof(log_target_t));
348 tmp->type_ = TARGET_STDERR;
351 tmp->log = &log_target_stderr_log;
356 tmp->max_prio_ = NOTICE;