]>
Commit | Line | Data |
---|---|---|
ac2f15b3 | 1 | /* $KAME: plog.c,v 1.23 2002/05/07 08:56:19 sakane Exp $ */ |
7ba0088d A |
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 <sys/types.h> | |
33 | #include <sys/param.h> | |
34 | ||
35 | #include <stdlib.h> | |
36 | #include <stdio.h> | |
37 | #include <string.h> | |
38 | #include <errno.h> | |
39 | #ifdef HAVE_STDARG_H | |
40 | #include <stdarg.h> | |
41 | #else | |
42 | #include <varargs.h> | |
43 | #endif | |
44 | #if TIME_WITH_SYS_TIME | |
45 | # include <sys/time.h> | |
46 | # include <time.h> | |
47 | #else | |
48 | # if HAVE_SYS_TIME_H | |
49 | # include <sys/time.h> | |
50 | # else | |
51 | # include <time.h> | |
52 | # endif | |
53 | #endif | |
54 | #include <ctype.h> | |
55 | #include <err.h> | |
56 | ||
57 | #include "var.h" | |
58 | #include "misc.h" | |
59 | #include "plog.h" | |
60 | #include "logger.h" | |
61 | #include "debug.h" | |
62 | #include "gcmalloc.h" | |
63 | ||
64 | char *pname = NULL; | |
65 | u_int32_t loglevel = LLV_BASE; | |
66 | ||
67 | static struct log *logp = NULL; | |
68 | static char *logfile = NULL; | |
69 | ||
70 | static char *plog_common __P((int, const char *, const char *)); | |
71 | ||
72 | static struct plogtags { | |
73 | char *name; | |
74 | int priority; | |
75 | } ptab[] = { | |
76 | { "(not defined)", 0, }, | |
77 | { "INFO", LOG_INFO, }, | |
78 | { "NOTIFY", LOG_INFO, }, | |
ac2f15b3 | 79 | { "WARNING", LOG_INFO, }, |
7ba0088d A |
80 | { "ERROR", LOG_INFO, }, |
81 | { "DEBUG", LOG_DEBUG, }, | |
82 | { "DEBUG2", LOG_DEBUG, }, | |
83 | }; | |
84 | ||
85 | static char * | |
86 | plog_common(pri, fmt, func) | |
87 | int pri; | |
88 | const char *fmt, *func; | |
89 | { | |
90 | static char buf[800]; /* XXX shoule be allocated every time ? */ | |
91 | char *p; | |
92 | int reslen, len; | |
93 | ||
94 | p = buf; | |
95 | reslen = sizeof(buf); | |
96 | ||
97 | if (logfile || f_foreground) { | |
98 | time_t t; | |
99 | struct tm *tm; | |
100 | ||
101 | t = time(0); | |
102 | tm = localtime(&t); | |
103 | len = strftime(p, reslen, "%Y-%m-%d %T: ", tm); | |
104 | p += len; | |
105 | reslen -= len; | |
106 | } | |
107 | ||
108 | if (pri < ARRAYLEN(ptab)) { | |
109 | len = snprintf(p, reslen, "%s: ", ptab[pri].name); | |
110 | if (len >= 0 && len < reslen) { | |
111 | p += len; | |
112 | reslen -= len; | |
113 | } else | |
114 | *p = '\0'; | |
115 | } | |
116 | ||
117 | snprintf(p, reslen, "%s: %s", func, fmt); | |
118 | ||
119 | return buf; | |
120 | } | |
121 | ||
122 | void | |
123 | plog(int pri, const char *func, struct sockaddr *sa, const char *fmt, ...) | |
124 | { | |
125 | va_list ap; | |
126 | ||
127 | va_start(ap, fmt); | |
128 | plogv(pri, func, sa, fmt, ap); | |
129 | va_end(ap); | |
130 | } | |
131 | ||
132 | void | |
133 | plogv(int pri, const char *func, struct sockaddr *sa, | |
134 | const char *fmt, va_list ap) | |
135 | { | |
136 | char *newfmt; | |
137 | ||
138 | if (pri > loglevel) | |
139 | return; | |
140 | ||
141 | newfmt = plog_common(pri, fmt, func); | |
142 | ||
143 | if (f_foreground) | |
144 | vprintf(newfmt, ap); | |
ac2f15b3 A |
145 | |
146 | /* | |
147 | * If we're not running in the foreground and the loglevel is | |
148 | * set to the default, don't dump LLV_INFO message to the log. | |
149 | */ | |
150 | if (!f_foreground && pri == LLV_INFO && loglevel == LLV_BASE) return; | |
7ba0088d A |
151 | |
152 | if (logfile) | |
153 | log_vaprint(logp, newfmt, ap); | |
154 | else { | |
155 | if (pri < ARRAYLEN(ptab)) | |
156 | vsyslog(ptab[pri].priority, newfmt, ap); | |
157 | else | |
158 | vsyslog(LOG_ALERT, newfmt, ap); | |
159 | } | |
160 | } | |
161 | ||
162 | void | |
163 | plogdump(pri, data, len) | |
164 | int pri; | |
165 | void *data; | |
166 | size_t len; | |
167 | { | |
168 | caddr_t buf; | |
169 | size_t buflen; | |
170 | int i, j; | |
171 | ||
172 | if (pri > loglevel) | |
173 | return; | |
174 | ||
175 | /* | |
176 | * 2 words a bytes + 1 space 4 bytes + 1 newline 32 bytes | |
177 | * + 2 newline + '\0' | |
178 | */ | |
179 | buflen = (len * 2) + (len / 4) + (len / 32) + 3; | |
180 | buf = racoon_malloc(buflen); | |
181 | ||
182 | i = 0; | |
183 | j = 0; | |
184 | while (j < len) { | |
185 | if (j % 32 == 0) | |
186 | buf[i++] = '\n'; | |
187 | else | |
188 | if (j % 4 == 0) | |
189 | buf[i++] = ' '; | |
190 | snprintf(&buf[i], buflen - i, "%02x", | |
191 | ((unsigned char *)data)[j] & 0xff); | |
192 | i += 2; | |
193 | j++; | |
194 | } | |
195 | if (buflen - i >= 2) { | |
196 | buf[i++] = '\n'; | |
197 | buf[i] = '\0'; | |
198 | } | |
199 | plog(pri, LOCATION, NULL, "%s", buf); | |
200 | ||
201 | racoon_free(buf); | |
202 | } | |
203 | ||
204 | void | |
205 | ploginit() | |
206 | { | |
207 | if (logfile) { | |
208 | logp = log_open(250, logfile); | |
209 | if (logp == NULL) | |
210 | errx(1, "ERROR: failed to open log file %s.", logfile); | |
211 | return; | |
212 | } | |
213 | ||
214 | openlog(pname, LOG_NDELAY, LOG_DAEMON); | |
215 | } | |
216 | ||
217 | void | |
218 | plogset(file) | |
219 | char *file; | |
220 | { | |
221 | if (logfile != NULL) | |
222 | racoon_free(logfile); | |
223 | logfile = strdup(file); | |
224 | } | |
225 |