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-2008 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 #include "datatypes.h"
50 #include "log_targets.h"
52 const char* log_prio_to_string(log_prio_t prio)
55 case ERROR: return "ERROR";
56 case WARNING: return "WARNING";
57 case NOTICE: return "NOTICE";
58 case INFO: return "INFO";
59 case DEBUG: return "DEBUG";
64 log_target_type_t log_target_parse_type(const char* conf)
67 return TARGET_UNKNOWN;
69 if(!strncmp(conf, "syslog", 6)) return TARGET_SYSLOG;
70 if(!strncmp(conf, "file", 4)) return TARGET_FILE;
71 if(!strncmp(conf, "stdout", 6)) return TARGET_STDOUT;
72 if(!strncmp(conf, "stderr", 6)) return TARGET_STDERR;
74 return TARGET_UNKNOWN;
77 int log_targets_target_exists(log_targets_t* targets, log_target_type_t type)
79 if(!targets && !targets->first_)
82 log_target_t* tmp = targets->first_;
84 if(tmp->type_ == type)
91 int log_targets_add(log_targets_t* targets, const char* conf)
96 log_target_t* new_target = NULL;
97 int duplicates_allowed = 0;
98 switch(log_target_parse_type(conf)) {
99 case TARGET_SYSLOG: new_target = log_target_syslog_new(); break;
100 case TARGET_FILE: new_target = log_target_file_new(); duplicates_allowed = 1; break;
101 case TARGET_STDOUT: new_target = log_target_stdout_new(); break;
102 case TARGET_STDERR: new_target = log_target_stderr_new(); break;
108 if(!duplicates_allowed && log_targets_target_exists(targets, new_target->type_)) {
113 const char* prioptr = strchr(conf, ':');
114 if(!prioptr || prioptr[1] == 0) {
119 if(!isdigit(prioptr[0]) || (prioptr[1] != 0 && prioptr[1] != ',')) {
123 new_target->max_prio_ = prioptr[0] - '0';
124 if(new_target->max_prio_ > 0)
125 new_target->enabled_ = 1;
127 if(new_target->init != NULL) {
128 const char* confptr = NULL;
132 int ret = (*new_target->init)(new_target, confptr);
139 if(new_target->open != NULL)
140 (*new_target->open)(new_target);
143 if(!targets->first_) {
144 targets->first_ = new_target;
147 log_target_t* tmp = targets->first_;
151 tmp->next_ = new_target;
156 void log_targets_log(log_targets_t* targets, log_prio_t prio, const char* msg)
161 log_target_t* tmp = targets->first_;
163 if(tmp->log != NULL && tmp->enabled_ && tmp->max_prio_ >= prio)
164 (*tmp->log)(tmp, prio, msg);
170 void log_targets_clear(log_targets_t* targets)
175 while(targets->first_) {
176 log_target_t* tmp = targets->first_;
177 targets->first_ = tmp->next_;
178 if(tmp->close != NULL)
180 if(tmp->clear != NULL)
189 stdlog.max_prio_ = 0;
190 stdlog.targets_.first_ = NULL;
195 log_targets_clear(&stdlog.targets_);
198 void update_max_prio()
200 log_target_t* tmp = stdlog.targets_.first_;
202 if(tmp->enabled_ && tmp->max_prio_ > stdlog.max_prio_)
203 stdlog.max_prio_ = tmp->max_prio_;
209 int log_add_target(const char* conf)
214 int ret = log_targets_add(&stdlog.targets_, conf);
215 if(!ret) update_max_prio();
219 void log_printf(log_prio_t prio, const char* fmt, ...)
221 if(stdlog.max_prio_ < prio)
224 static char msg[MSG_LENGTH_MAX];
228 vsnprintf(msg, MSG_LENGTH_MAX, fmt, args);
231 log_targets_log(&stdlog.targets_, prio, msg);
234 void log_print_hex_dump(log_prio_t prio, const u_int8_t* buf, u_int32_t len)
236 if(stdlog.max_prio_ < prio)
239 static char msg[MSG_LENGTH_MAX];
242 snprintf(msg, MSG_LENGTH_MAX, "(NULL)");
246 int offset = snprintf(msg, MSG_LENGTH_MAX, "dump(%d): ", len);
249 u_int8_t* ptr = &msg[offset];
251 for(i=0; i < len; i++) {
252 if(((i+1)*3) >= (MSG_LENGTH_MAX - offset))
254 snprintf(ptr, 3, "%02X ", buf[i]);
258 log_targets_log(&stdlog.targets_, prio, msg);