~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to proxy/logging/LogFieldAliasMap.h

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 
 
3
  A brief file description
 
4
 
 
5
  @section license License
 
6
 
 
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
 
14
 
 
15
      http://www.apache.org/licenses/LICENSE-2.0
 
16
 
 
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.
 
22
 */
 
23
 
 
24
/***************************************************************************
 
25
 LogFieldAliasMap.h
 
26
 
 
27
 
 
28
 ***************************************************************************/
 
29
 
 
30
#ifndef LOG_FIELD_ALIAS_MAP_H
 
31
#define LOG_FIELD_ALIAS_MAP_H
 
32
 
 
33
#include <stdarg.h>
 
34
#include <string.h>
 
35
 
 
36
#include "libts.h"
 
37
#include "Ptr.h"
 
38
#include "LogUtils.h"
 
39
#include "ink_string.h"
 
40
 
 
41
/*****************************************************************************
 
42
 
 
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.
 
47
 
 
48
The methods that subclasses should implement are:
 
49
 
 
50
1) asInt(char *key, IntType *val)
 
51
 
 
52
This method takes a string and sets the IntType argument to the
 
53
corresponding value, (unless the string is invalid). It returns an
 
54
error status.
 
55
 
 
56
2) asString(IntType key, char *buf, size_t bufLen, size_t *numChars=0)
 
57
 
 
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.
 
61
 
 
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).
 
66
 
 
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.
 
71
 
 
72
 *****************************************************************************/
 
73
 
 
74
 
 
75
class LogFieldAliasMap:public RefCountObj
 
76
{
 
77
public:
 
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;
 
82
  enum
 
83
  { ALL_OK = 0, INVALID_INT, INVALID_STRING, BUFFER_TOO_SMALL };
 
84
 
 
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;
 
87
};
 
88
 
 
89
/*****************************************************************************
 
90
 
 
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:
 
96
 
 
97
table->init(3, 1, "one", 2, "two", 7, "seven")
 
98
 
 
99
 *****************************************************************************/
 
100
 
 
101
struct LogFieldAliasTableEntry
 
102
{
 
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)
 
107
  {
 
108
  };
 
109
};
 
110
 
 
111
class LogFieldAliasTable:public LogFieldAliasMap
 
112
{
 
113
private:
 
114
 
 
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
 
119
 
 
120
public:
 
121
 
 
122
    LogFieldAliasTable():m_min(0), m_max(0), m_entries(0), m_table(0)
 
123
  {
 
124
  };
 
125
  ~LogFieldAliasTable() {
 
126
    delete[]m_table;
 
127
  };
 
128
 
 
129
  void init(size_t numPairs, ...);
 
130
 
 
131
  int asInt(char *key, IntType * val, bool case_sensitive_search = 0) const
 
132
  {
 
133
    int retVal = INVALID_STRING;
 
134
 
 
135
    for (size_t i = 0; i < m_entries; i++)
 
136
    {
 
137
      bool found;
 
138
      if (m_table[i].valid)
 
139
      {
 
140
        if (case_sensitive_search) {
 
141
          found = (strcmp(key, m_table[i].name) == 0);
 
142
        } else
 
143
        {
 
144
          found = (strcasecmp(key, m_table[i].name) == 0);
 
145
        }
 
146
      } else {
 
147
        found = false;
 
148
      }
 
149
      if (found) {
 
150
        *val = (unsigned int) (i + m_min);
 
151
        retVal = ALL_OK;
 
152
        break;
 
153
      }
 
154
    }
 
155
 
 
156
    return retVal;
 
157
  };
 
158
 
 
159
  int asString(IntType key, char *buf, size_t bufLen, size_t * numCharsPtr = 0) const
 
160
  {
 
161
    int retVal;
 
162
    size_t numChars;
 
163
 
 
164
    size_t i = key - m_min;
 
165
    if (m_entries && key >= m_min && key <= m_max && m_table[i].valid)
 
166
    {
 
167
      register size_t l = m_table[i].length;
 
168
      if (l < bufLen)
 
169
      {
 
170
        ink_strncpy(buf, m_table[key - m_min].name, bufLen);
 
171
        numChars = l;
 
172
        retVal = ALL_OK;
 
173
      } else
 
174
      {
 
175
        numChars = 0;
 
176
        retVal = BUFFER_TOO_SMALL;
 
177
      }
 
178
    } else {
 
179
      numChars = 0;
 
180
      retVal = INVALID_INT;
 
181
    }
 
182
    if (numCharsPtr) {
 
183
      *numCharsPtr = numChars;
 
184
    }
 
185
    return retVal;
 
186
  };
 
187
};
 
188
 
 
189
 
 
190
/*****************************************************************************
 
191
 
 
192
The LogFieldAliasIP class implements a LogFieldAliasMap that converts IP
 
193
addresses from their integer value to the "dot" notation and back.
 
194
 
 
195
 *****************************************************************************/
 
196
 
 
197
class LogFieldAliasIP:public LogFieldAliasMap
 
198
{
 
199
public:
 
200
  int asInt(char *str, IntType * ip, bool case_sensitive = 0) const
 
201
  {
 
202
    NOWARN_UNUSED(case_sensitive);
 
203
    unsigned a, b, c, d;
 
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);
 
207
      return ALL_OK;
 
208
    } else
 
209
    {
 
210
      return INVALID_STRING;
 
211
    }
 
212
  };
 
213
 
 
214
  int asString(IntType ip, char *buf, size_t bufLen, size_t * numCharsPtr = 0) const
 
215
  {
 
216
    return (LogUtils::ip_to_str(ip, buf, bufLen, numCharsPtr) ? BUFFER_TOO_SMALL : ALL_OK);
 
217
/*
 
218
        int retVal;
 
219
        size_t numChars;
 
220
        size_t n = snprintf (buf, bufLen, "%u.%u.%u.%u",
 
221
                                 (ip >> 24) & 0xff,
 
222
                                 (ip >> 16) & 0xff,
 
223
                                 (ip >> 8)  & 0xff,
 
224
                                 ip         & 0xff);
 
225
        if (n < bufLen) {
 
226
            numChars = n;
 
227
            retVal = ALL_OK;
 
228
        } else {
 
229
            numChars = bufLen - 1;
 
230
            retVal = BUFFER_TOO_SMALL;
 
231
        }
 
232
        if (numCharsPtr) {
 
233
            *numCharsPtr = numChars;
 
234
        }
 
235
        return retVal;
 
236
*/
 
237
  };
 
238
};
 
239
 
 
240
/*****************************************************************************
 
241
 
 
242
The LogFieldAliasIPhex class implements a LogFieldAliasMap that converts IP
 
243
addresses from their integer value to the "hex" notation and back.
 
244
 
 
245
 *****************************************************************************/
 
246
 
 
247
class LogFieldAliasIPhex:public LogFieldAliasMap
 
248
{
 
249
public:
 
250
  int asInt(char *str, IntType * ip, bool case_sensitive = 0) const
 
251
  {
 
252
    NOWARN_UNUSED(case_sensitive);
 
253
    unsigned a, b, c, d;
 
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);
 
257
      return ALL_OK;
 
258
    } else
 
259
    {
 
260
      return INVALID_STRING;
 
261
    }
 
262
  };
 
263
 
 
264
  int asString(IntType ip, char *buf, size_t bufLen, size_t * numCharsPtr = 0) const
 
265
  {
 
266
 
 
267
    return (LogUtils::timestamp_to_hex_str(ip, buf, bufLen, numCharsPtr) ? BUFFER_TOO_SMALL : ALL_OK);
 
268
  };
 
269
};
 
270
 
 
271
/*****************************************************************************
 
272
 
 
273
The LogFieldAliasTimehex class implements a LogFieldAliasMap that converts time
 
274
from their integer value to the "hex" notation and back.
 
275
 
 
276
 *****************************************************************************/
 
277
 
 
278
class LogFieldAliasTimeHex:public LogFieldAliasMap
 
279
{
 
280
public:
 
281
  int asInt(char *str, IntType * time, bool case_sensitive = 0) const
 
282
  {
 
283
    NOWARN_UNUSED(case_sensitive);
 
284
    unsigned long a;
 
285
    // coverity[secure_coding]
 
286
    if (sscanf(str, "%lx", (unsigned long *) &a) == 1) {
 
287
      *time = (IntType) a;
 
288
      return ALL_OK;
 
289
    } else
 
290
    {
 
291
      return INVALID_STRING;
 
292
    }
 
293
  };
 
294
 
 
295
  int asString(IntType time, char *buf, size_t bufLen, size_t * numCharsPtr = 0) const
 
296
  {
 
297
    return (LogUtils::timestamp_to_hex_str(time, buf, bufLen, numCharsPtr) ? BUFFER_TOO_SMALL : ALL_OK);
 
298
  };
 
299
};
 
300
 
 
301
 
 
302
 
 
303
//LOG_FIELD_ALIAS_MAP_H
 
304
#endif