Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / ipsec-tools / src / racoon / plog.c
1 /* $Id: plog.c,v 1.6 2004/07/12 20:15:08 ludvigm Exp $ */
2
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  * 
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include "config.h"
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <errno.h>
41 #ifdef HAVE_STDARG_H
42 #include <stdarg.h>
43 #else
44 #include <varargs.h>
45 #endif
46 #if TIME_WITH_SYS_TIME
47 # include <sys/time.h>
48 # include <time.h>
49 #else
50 # if HAVE_SYS_TIME_H
51 #  include <sys/time.h>
52 # else
53 #  include <time.h>
54 # endif
55 #endif
56 #include <ctype.h>
57 #include <err.h>
58
59 #include "var.h"
60 #include "misc.h"
61 #include "plog.h"
62 #include "logger.h"
63 #include "debug.h"
64 #include "gcmalloc.h"
65
66 #ifndef VA_COPY
67 # define VA_COPY(dst,src) memcpy(&(dst), &(src), sizeof(va_list))
68 #endif
69
70 char *pname = NULL;
71 u_int32_t loglevel = LLV_BASE;
72 int f_foreground = 0;
73
74 int print_location = 0;
75
76 static struct log *logp = NULL;
77 static char *logfile = NULL;
78
79 static char *plog_common __P((int, const char *, const char *));
80
81 static struct plogtags {
82         char *name;
83         int priority;
84 } ptab[] = {
85         { "(not defined)",      0, },
86         { "INFO",               LOG_INFO, },
87         { "NOTIFY",             LOG_INFO, },
88         { "WARNING",            LOG_INFO, },
89         { "ERROR",              LOG_INFO, },
90         { "DEBUG",              LOG_DEBUG, },
91         { "DEBUG2",             LOG_DEBUG, },
92 };
93
94 static char *
95 plog_common(pri, fmt, func)
96         int pri;
97         const char *fmt, *func;
98 {
99         static char buf[800];   /* XXX shoule be allocated every time ? */
100         char *p;
101         int reslen, len;
102
103         p = buf;
104         reslen = sizeof(buf);
105
106         if (logfile || f_foreground) {
107                 time_t t;
108                 struct tm *tm;
109
110                 t = time(0);
111                 tm = localtime(&t);
112                 len = strftime(p, reslen, "%Y-%m-%d %T: ", tm);
113                 p += len;
114                 reslen -= len;
115         }
116
117         if (pri < ARRAYLEN(ptab)) {
118                 len = snprintf(p, reslen, "%s: ", ptab[pri].name);
119                 if (len >= 0 && len < reslen) {
120                         p += len;
121                         reslen -= len;
122                 } else
123                         *p = '\0';
124         }
125
126         if (print_location)
127                 snprintf(p, reslen, "%s: %s", func, fmt);
128         else
129                 snprintf(p, reslen, "%s", fmt);
130
131         return buf;
132 }
133
134 void
135 plog(int pri, const char *func, struct sockaddr *sa, const char *fmt, ...)
136 {
137         va_list ap;
138
139         va_start(ap, fmt);
140         plogv(pri, func, sa, fmt, ap);
141         va_end(ap);
142 }
143
144 void
145 plogv(int pri, const char *func, struct sockaddr *sa,
146         const char *fmt, va_list ap)
147 {
148         char *newfmt;
149         va_list ap_bak;
150
151         if (pri > loglevel)
152                 return;
153
154         newfmt = plog_common(pri, fmt, func);
155
156         VA_COPY(ap_bak, ap);
157         
158         if (f_foreground)
159                 vprintf(newfmt, ap);
160
161         if (logfile)
162                 log_vaprint(logp, newfmt, ap_bak);
163         else {
164                 if (pri < ARRAYLEN(ptab))
165                         vsyslog(ptab[pri].priority, newfmt, ap_bak);
166                 else
167                         vsyslog(LOG_ALERT, newfmt, ap_bak);
168         }
169 }
170
171 void
172 plogdump(pri, data, len)
173         int pri;
174         void *data;
175         size_t len;
176 {
177         caddr_t buf;
178         size_t buflen;
179         int i, j;
180
181         if (pri > loglevel)
182                 return;
183
184         /*
185          * 2 words a bytes + 1 space 4 bytes + 1 newline 32 bytes
186          * + 2 newline + '\0'
187          */
188         buflen = (len * 2) + (len / 4) + (len / 32) + 3;
189         buf = racoon_malloc(buflen);
190
191         i = 0;
192         j = 0;
193         while (j < len) {
194                 if (j % 32 == 0)
195                         buf[i++] = '\n';
196                 else
197                 if (j % 4 == 0)
198                         buf[i++] = ' ';
199                 snprintf(&buf[i], buflen - i, "%02x",
200                         ((unsigned char *)data)[j] & 0xff);
201                 i += 2;
202                 j++;
203         }
204         if (buflen - i >= 2) {
205                 buf[i++] = '\n';
206                 buf[i] = '\0';
207         }
208         plog(pri, LOCATION, NULL, "%s", buf);
209
210         racoon_free(buf);
211 }
212
213 void
214 ploginit()
215 {
216         if (logfile) {
217                 logp = log_open(250, logfile);
218                 if (logp == NULL)
219                         errx(1, "ERROR: failed to open log file %s.", logfile);
220                 return;
221         }
222
223         openlog(pname, LOG_NDELAY, LOG_DAEMON);
224 }
225
226 void
227 plogset(file)
228         char *file;
229 {
230         if (logfile != NULL)
231                 racoon_free(logfile);
232         logfile = strdup(file);
233 }
234