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 methods 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-2014 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/>.
35 * In addition, as a special exception, the copyright holders give
36 * permission to link the code of portions of this program with the
37 * OpenSSL library under certain conditions as described in each
38 * individual source file, and distribute linked combinations
40 * You must obey the GNU General Public License in all respects
41 * for all of the code used other than OpenSSL. If you modify
42 * file(s) with this exception, you may extend this exception to your
43 * version of the file(s), but you are not obligated to do so. If you
44 * do not wish to do so, delete this exception statement from your
45 * version. If you delete this exception statement from all source
46 * files in the program, then also delete it here.
49 #ifndef UANYTUN_log_targets_h_INCLUDED
50 #define UANYTUN_log_targets_h_INCLUDED
54 static char* get_time_formatted()
57 time_t t = time(NULL);
59 time_string = "<time read error>";
61 time_string = ctime(&t);
63 time_string = "<time format error>";
65 char* newline = strchr(time_string, '\n');
73 enum syslog_facility_enum { USER = LOG_USER, MAIL = LOG_MAIL,
74 DAEMON = LOG_DAEMON, AUTH = LOG_AUTH,
75 SYSLOG = LOG_SYSLOG, LPR = LOG_LPR,
76 NEWS = LOG_NEWS, UUCP = LOG_UUCP,
77 CRON = LOG_CRON, AUTHPRIV = LOG_AUTHPRIV,
78 FTP = LOG_FTP, LOCAL0 = LOG_LOCAL0,
79 LOCAL1 = LOG_LOCAL1, LOCAL2 = LOG_LOCAL2,
80 LOCAL3 = LOG_LOCAL3, LOCAL4 = LOG_LOCAL4,
81 LOCAL5 = LOG_LOCAL5, LOCAL6 = LOG_LOCAL6,
82 LOCAL7 = LOG_LOCAL7 };
83 typedef enum syslog_facility_enum syslog_facility_t;
85 struct log_target_syslog_param_struct {
87 syslog_facility_t facility_;
89 typedef struct log_target_syslog_param_struct log_target_syslog_param_t;
91 int log_target_syslog_init(log_target_t* self, const char* conf)
93 if(!self || (conf && conf[0] == 0))
96 self->param_ = malloc(sizeof(log_target_syslog_param_t));
101 const char* end = NULL;
103 logname = strdup("uanytun");
105 end = strchr(conf, ',');
107 size_t len = (size_t)(end - conf);
112 logname = malloc(len+1);
114 strncpy(logname, conf, len);
119 logname = strdup(conf);
126 ((log_target_syslog_param_t*)(self->param_))->logname_ = logname;
129 ((log_target_syslog_param_t*)(self->param_))->facility_ = DAEMON;
133 if(end[1] == 0 || end[1] == ',') {
139 const char* start = end + 1;
140 end = strchr(start, ',');
143 if(facilitynames[i].c_name == NULL) {
149 if(( end && !strncmp(start, facilitynames[i].c_name, (size_t)(end - start)) && facilitynames[i].c_name[(size_t)(end-start)] == 0) ||
150 (!end && !strcmp(start, facilitynames[i].c_name))) {
151 ((log_target_syslog_param_t*)(self->param_))->facility_ = facilitynames[i].c_val;
159 void log_target_syslog_open(log_target_t* self)
161 if(!self || !self->param_)
164 openlog(((log_target_syslog_param_t*)(self->param_))->logname_, LOG_PID, ((log_target_syslog_param_t*)(self->param_))->facility_);
168 void log_target_syslog_log(log_target_t* self, log_prio_t prio, const char* msg)
170 if(!self || !self->param_ || !self->opened_)
173 syslog((prio + 2) | ((log_target_syslog_param_t*)(self->param_))->facility_, "%s", msg);
176 void log_target_syslog_close(log_target_t* self)
182 void log_target_syslog_clear(log_target_t* self)
184 if(!self || !self->param_)
187 if(((log_target_syslog_param_t*)(self->param_))->logname_)
188 free(((log_target_syslog_param_t*)(self->param_))->logname_);
193 log_target_t* log_target_syslog_new()
195 log_target_t* tmp = malloc(sizeof(log_target_t));
199 tmp->type_ = TARGET_SYSLOG;
200 tmp->init = &log_target_syslog_init;
201 tmp->open = &log_target_syslog_open;
202 tmp->log = &log_target_syslog_log;
203 tmp->close = &log_target_syslog_close;
204 tmp->clear = &log_target_syslog_clear;
207 tmp->max_prio_ = NOTICE;
215 struct log_target_file_param_struct {
219 typedef struct log_target_file_param_struct log_target_file_param_t;
221 int log_target_file_init(log_target_t* self, const char* conf)
223 if(!self || (conf && conf[0] == 0))
226 self->param_ = malloc(sizeof(log_target_file_param_t));
232 logfilename = strdup("uanytun.log");
234 const char* end = strchr(conf, ',');
236 size_t len = (size_t)(end - conf);
241 logfilename = malloc(len+1);
243 strncpy(logfilename, conf, len);
244 logfilename[len] = 0;
248 logfilename = strdup(conf);
255 ((log_target_file_param_t*)(self->param_))->logfilename_ = logfilename;
256 ((log_target_file_param_t*)(self->param_))->file_ = NULL;
261 void log_target_file_open(log_target_t* self)
263 if(!self || !self->param_)
266 ((log_target_file_param_t*)(self->param_))->file_ = fopen(((log_target_file_param_t*)(self->param_))->logfilename_, "w");
267 if(((log_target_file_param_t*)(self->param_))->file_)
271 void log_target_file_log(log_target_t* self, log_prio_t prio, const char* msg)
273 if(!self || !self->param_ || !self->opened_)
276 fprintf(((log_target_file_param_t*)(self->param_))->file_, "%s %s: %s\n", get_time_formatted(), log_prio_to_string(prio), msg);
277 fflush(((log_target_file_param_t*)(self->param_))->file_);
280 void log_target_file_close(log_target_t* self)
282 if(!self || !self->param_)
285 fclose(((log_target_file_param_t*)(self->param_))->file_);
289 void log_target_file_clear(log_target_t* self)
291 if(!self || !self->param_)
294 if(((log_target_file_param_t*)(self->param_))->logfilename_)
295 free(((log_target_file_param_t*)(self->param_))->logfilename_);
301 log_target_t* log_target_file_new()
303 log_target_t* tmp = malloc(sizeof(log_target_t));
307 tmp->type_ = TARGET_FILE;
308 tmp->init = &log_target_file_init;
309 tmp->open = &log_target_file_open;
310 tmp->log = &log_target_file_log;
311 tmp->close = &log_target_file_close;
312 tmp->clear = &log_target_file_clear;
315 tmp->max_prio_ = NOTICE;
323 void log_target_stdout_log(log_target_t* self, log_prio_t prio, const char* msg)
325 printf("%s %s: %s\n", get_time_formatted(), log_prio_to_string(prio), msg);
328 log_target_t* log_target_stdout_new()
330 log_target_t* tmp = malloc(sizeof(log_target_t));
334 tmp->type_ = TARGET_STDOUT;
337 tmp->log = &log_target_stdout_log;
342 tmp->max_prio_ = NOTICE;
350 void log_target_stderr_log(log_target_t* self, log_prio_t prio, const char* msg)
352 fprintf(stderr, "%s %s: %s\n", get_time_formatted(), log_prio_to_string(prio), msg);
355 log_target_t* log_target_stderr_new()
357 log_target_t* tmp = malloc(sizeof(log_target_t));
361 tmp->type_ = TARGET_STDERR;
364 tmp->log = &log_target_stderr_log;
369 tmp->max_prio_ = NOTICE;