3
A brief file description
5
@section license License
7
Licensed to the Apache Software Foundation (ASF) under one
8
or more contributor license agreements. See the NOTICE file
9
distributed with this work for additional information
10
regarding copyright ownership. The ASF licenses this file
11
to you under the Apache License, Version 2.0 (the
12
"License"); you may not use this file except in compliance
13
with the License. You may obtain a copy of the License at
15
http://www.apache.org/licenses/LICENSE-2.0
17
Unless required by applicable law or agreed to in writing, software
18
distributed under the License is distributed on an "AS IS" BASIS,
19
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
See the License for the specific language governing permissions and
21
limitations under the License.
24
/***************************************************************************
28
***************************************************************************/
30
#ifndef LOG_FIELD_ALIAS_MAP_H
31
#define LOG_FIELD_ALIAS_MAP_H
39
#include "ink_string.h"
41
/*****************************************************************************
43
The LogFieldAliasMap class is an abstract class used to provide an
44
interface to map between numbers of type IntType and strings. The
45
purpose is to obtain one representation from the other so that easy to
46
remember names can be used to refer to log fields of integer type.
48
The methods that subclasses should implement are:
50
1) asInt(char *key, IntType *val)
52
This method takes a string and sets the IntType argument to the
53
corresponding value, (unless the string is invalid). It returns an
56
2) asString(IntType key, char *buf, size_t bufLen, size_t *numChars=0)
58
This method takes an IntType key and writes its equivalent string to a
59
bufer buf of length bufLen. It sets the number of written characters
60
numChars (if numChars is not NULL), and returns an error status.
62
The IntType to string conversion is used when unmarshaling data prior to
63
writing to a log file, and the string to IntType conversion is used when
64
building filters (so that the filter value can be specified as a string,
65
but the actual field comparison is done between IntTypes).
67
Note that LogFieldAliasMap is derived from RefCountObj, so once a map
68
is constructed a pointer to it can be passed to other objects (e.g.,
69
to a LogField object) without the object having to worry about freeing
70
any memory the map may have allocated.
72
*****************************************************************************/
75
class LogFieldAliasMap:public RefCountObj
78
// the logging system assumes log entries of type sINT are
79
// unsigned integers (int64_t type) so we define IntType to be unsigned
80
// TODO/XXX: B0rken, need to fix this to int64_t
81
typedef unsigned int IntType;
83
{ ALL_OK = 0, INVALID_INT, INVALID_STRING, BUFFER_TOO_SMALL };
85
virtual int asInt(char *key, IntType * val, bool case_sensitive = 0) const = 0;
86
virtual int asString(IntType key, char *buf, size_t bufLen, size_t * numChars = 0) const = 0;
89
/*****************************************************************************
91
A LogFieldAliasTable implements a LogFieldAliasMap through a
92
straightforward table. The entries in the table are input with the
93
init(numPairs, ...) method. Arguments to this method are the number
94
numPairs of table entries, followed by the entries themselves in the
95
form integer, string. For example:
97
table->init(3, 1, "one", 2, "two", 7, "seven")
99
*****************************************************************************/
101
struct LogFieldAliasTableEntry
103
bool valid; // entry in table is valid
104
char *name; // the string equivalent
105
size_t length; // the length of the string
106
LogFieldAliasTableEntry():valid(false), name(NULL), length(0)
111
class LogFieldAliasTable:public LogFieldAliasMap
115
size_t m_min; // minimum numeric value
116
size_t m_max; // maximum numeric value
117
size_t m_entries; // number of entries in table
118
LogFieldAliasTableEntry *m_table; // array of table entries
122
LogFieldAliasTable():m_min(0), m_max(0), m_entries(0), m_table(0)
125
~LogFieldAliasTable() {
129
void init(size_t numPairs, ...);
131
int asInt(char *key, IntType * val, bool case_sensitive_search = 0) const
133
int retVal = INVALID_STRING;
135
for (size_t i = 0; i < m_entries; i++)
138
if (m_table[i].valid)
140
if (case_sensitive_search) {
141
found = (strcmp(key, m_table[i].name) == 0);
144
found = (strcasecmp(key, m_table[i].name) == 0);
150
*val = (unsigned int) (i + m_min);
159
int asString(IntType key, char *buf, size_t bufLen, size_t * numCharsPtr = 0) const
164
size_t i = key - m_min;
165
if (m_entries && key >= m_min && key <= m_max && m_table[i].valid)
167
register size_t l = m_table[i].length;
170
ink_strncpy(buf, m_table[key - m_min].name, bufLen);
176
retVal = BUFFER_TOO_SMALL;
180
retVal = INVALID_INT;
183
*numCharsPtr = numChars;
190
/*****************************************************************************
192
The LogFieldAliasIP class implements a LogFieldAliasMap that converts IP
193
addresses from their integer value to the "dot" notation and back.
195
*****************************************************************************/
197
class LogFieldAliasIP:public LogFieldAliasMap
200
int asInt(char *str, IntType * ip, bool case_sensitive = 0) const
202
NOWARN_UNUSED(case_sensitive);
204
// coverity[secure_coding]
205
if (sscanf(str, "%u.%u.%u.%u", &a, &b, &c, &d) == 4) {
206
*ip = d | (c << 8) | (b << 16) | (a << 24);
210
return INVALID_STRING;
214
int asString(IntType ip, char *buf, size_t bufLen, size_t * numCharsPtr = 0) const
216
return (LogUtils::ip_to_str(ip, buf, bufLen, numCharsPtr) ? BUFFER_TOO_SMALL : ALL_OK);
220
size_t n = snprintf (buf, bufLen, "%u.%u.%u.%u",
229
numChars = bufLen - 1;
230
retVal = BUFFER_TOO_SMALL;
233
*numCharsPtr = numChars;
240
/*****************************************************************************
242
The LogFieldAliasIPhex class implements a LogFieldAliasMap that converts IP
243
addresses from their integer value to the "hex" notation and back.
245
*****************************************************************************/
247
class LogFieldAliasIPhex:public LogFieldAliasMap
250
int asInt(char *str, IntType * ip, bool case_sensitive = 0) const
252
NOWARN_UNUSED(case_sensitive);
254
// coverity[secure_coding]
255
if (sscanf(str, "%2x%2x%2x%2x", &a, &b, &c, &d) == 4) {
256
*ip = d | (c << 8) | (b << 16) | (a << 24);
260
return INVALID_STRING;
264
int asString(IntType ip, char *buf, size_t bufLen, size_t * numCharsPtr = 0) const
267
return (LogUtils::timestamp_to_hex_str(ip, buf, bufLen, numCharsPtr) ? BUFFER_TOO_SMALL : ALL_OK);
271
/*****************************************************************************
273
The LogFieldAliasTimehex class implements a LogFieldAliasMap that converts time
274
from their integer value to the "hex" notation and back.
276
*****************************************************************************/
278
class LogFieldAliasTimeHex:public LogFieldAliasMap
281
int asInt(char *str, IntType * time, bool case_sensitive = 0) const
283
NOWARN_UNUSED(case_sensitive);
285
// coverity[secure_coding]
286
if (sscanf(str, "%lx", (unsigned long *) &a) == 1) {
291
return INVALID_STRING;
295
int asString(IntType time, char *buf, size_t bufLen, size_t * numCharsPtr = 0) const
297
return (LogUtils::timestamp_to_hex_str(time, buf, bufLen, numCharsPtr) ? BUFFER_TOO_SMALL : ALL_OK);
303
//LOG_FIELD_ALIAS_MAP_H