1 /*
2 * Event Logging API
3 * Copyright (c) 2003 BalaBit IT Ltd.
4 * All rights reserved.
5 * Author: Balazs Scheidler
6 *
7 * $Id: evttags.c,v 1.4 2004/08/20 19:46:29 bazsi Exp $
8 *
9 * Some of the ideas are based on the discussions on the log-analysis
10 * mailing list (http://www.loganalysis.org/).
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of BalaBit nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY BALABIT AND CONTRIBUTORS S IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 */
37
38 /*
39 * This module implements tag support functions.
40 */
41
42 #include "evt_internals.h"
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <stdarg.h>
48 #include <assert.h>
49
50 #ifdef _MSC_VER
51 #ifndef snprintf
52 #define snprintf _snprintf
53 #endif
54 #endif
55
56 void
evt_tag_free(EVTTAG * et)57 evt_tag_free(EVTTAG *et)
58 {
59 free(et->et_tag);
60 free(et->et_value);
61 free(et);
62 }
63
64 EVTTAG *
evt_tag_str(const char * tag,const char * value)65 evt_tag_str(const char *tag, const char *value)
66 {
67 EVTTAG *p;
68
69 /* neither tag nor value can be NULL */
70 assert(tag);
71 if (!value)
72 value = "(null)";
73
74 p = (EVTTAG *) malloc(sizeof(EVTTAG));
75 if (p)
76 {
77 p->et_tag = strdup(tag);
78 p->et_value = strdup(value);
79 }
80 return p;
81 }
82
83 EVTTAG *
evt_tag_int(const char * tag,int value)84 evt_tag_int(const char *tag, int value)
85 {
86 char buf[32]; /* a 64 bit int fits into 20 characters */
87
88 snprintf(buf, sizeof(buf), "%d", value);
89 return evt_tag_str(tag, buf);
90 }
91
92 EVTTAG *
evt_tag_long(const char * tag,long long value)93 evt_tag_long(const char *tag, long long value)
94 {
95 char buf[32]; /* a 64 bit int fits into 20 characters */
96
97 snprintf(buf, sizeof(buf), "%lld", value);
98 return evt_tag_str(tag, buf);
99 }
100
101 EVTTAG *
evt_tag_errno(const char * tag,int err)102 evt_tag_errno(const char *tag, int err)
103 {
104 char buf[128];
105
106 snprintf(buf, sizeof(buf), "%s (%d)", strerror(err), err);
107 return evt_tag_str(tag, buf);
108 }
109
110 EVTTAG *
evt_tag_printf(const char * tag,const char * format,...)111 evt_tag_printf(const char *tag, const char *format, ...)
112 {
113 va_list ap;
114 char buf[1024];
115
116 va_start(ap, format);
117 vsnprintf(buf, sizeof(buf), format, ap);
118 va_end(ap);
119 return evt_tag_str(tag, buf);
120 }
121
122 EVTTAG *
evt_tag_inaddr(const char * tag,const struct in_addr * addr)123 evt_tag_inaddr(const char *tag, const struct in_addr *addr)
124 {
125 char buf[64];
126
127 if (addr)
128 inet_ntop(AF_INET, addr, buf, sizeof(buf));
129 else
130 strncpy(buf, "none", sizeof(buf));
131
132 return evt_tag_str(tag, buf);
133 }
134
135 EVTTAG *
evt_tag_inaddr6(const char * tag,const struct in6_addr * addr)136 evt_tag_inaddr6(const char *tag, const struct in6_addr *addr)
137 {
138 char buf[128];
139
140 if (addr)
141 inet_ntop(AF_INET6, addr, buf, sizeof(buf));
142 else
143 strncpy(buf, "none", sizeof(buf));
144 return evt_tag_str(tag, buf);
145 }
146