File: snmptrapd-header-parser.c

package info (click to toggle)
syslog-ng 3.19.1-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,176 kB
  • sloc: ansic: 114,472; makefile: 4,697; sh: 4,391; python: 4,282; java: 4,047; xml: 2,435; yacc: 1,108; lex: 426; perl: 193; awk: 184
file content (320 lines) | stat: -rw-r--r-- 8,182 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright (c) 2017 Balabit
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 */

#include "snmptrapd-header-parser.h"

#include "str-format.h"
#include "timeutils.h"

#include <string.h>
#include <ctype.h>

typedef struct
{
  SnmpTrapdNVContext *nv_context;
  const gchar **input;
  gsize *input_len;
} SnmpTrapdHeaderParser;

typedef gboolean (*SnmpTrapdHeaderParserStep)(SnmpTrapdHeaderParser *self);


static inline void
_skip_spaces(SnmpTrapdHeaderParser *self)
{
  const gchar *current_char = *self->input;

  while (*self->input_len > 0 && *current_char == ' ')
    {
      ++current_char;
      --(*self->input_len);
    }

  *self->input = current_char;
}


static gboolean
_run_header_parser(SnmpTrapdHeaderParser *self,
                   SnmpTrapdHeaderParserStep *parser_steps, gsize parser_steps_size)
{
  SnmpTrapdHeaderParserStep parser_step;

  for (gsize step_index = 0; step_index < parser_steps_size; ++step_index)
    {
      _skip_spaces(self);

      parser_step = parser_steps[step_index];
      if (!parser_step(self))
        return FALSE;
    }

  return TRUE;
}


static inline gboolean
_expect_char(SnmpTrapdHeaderParser *self, gchar c)
{
  return scan_expect_char(self->input, (gint *) self->input_len, c);
}

static inline gboolean
_expect_newline(SnmpTrapdHeaderParser *self)
{
  return _expect_char(self, '\n');
}

static inline gboolean
_expect_newline_or_eom(SnmpTrapdHeaderParser *self)
{
  return _expect_newline(self) || *self->input_len == 0;
}

static inline gboolean
_expect_colon(SnmpTrapdHeaderParser *self)
{
  return _expect_char(self, ':');
}

static inline gboolean
_expect_tab(SnmpTrapdHeaderParser *self)
{
  return _expect_char(self, '\t');
}

static inline void
_scroll_to_eom(SnmpTrapdHeaderParser *self)
{
  while (*self->input_len > 0 || **self->input)
    {
      ++(*self->input);
      --(*self->input_len);
    }
}

static gboolean
_parse_v1_uptime(SnmpTrapdHeaderParser *self)
{
  if (!scan_expect_str(self->input, (gint *) self->input_len, "Uptime:"))
    return FALSE;

  _skip_spaces(self);

  const gchar *uptime_start = *self->input;

  const gchar *uptime_end = strchr(uptime_start, '\n');
  if (!uptime_end)
    {
      _scroll_to_eom(self);
      snmptrapd_nv_context_add_name_value(self->nv_context, "uptime", uptime_start, *self->input - uptime_start);
      return TRUE;
    }

  snmptrapd_nv_context_add_name_value(self->nv_context, "uptime", uptime_start, uptime_end - uptime_start);

  *self->input_len -= uptime_end - *self->input;
  *self->input = uptime_end;
  return TRUE;
}

static gboolean
_parse_v1_trap_type_and_subtype(SnmpTrapdHeaderParser *self)
{
  const gchar *type_start = *self->input;

  const gchar *type_end = strpbrk(type_start, "(\n");
  gboolean type_exists = type_end && *type_end == '(';

  if (!type_exists)
    return FALSE;

  const gchar *subtype_start = type_end + 1;

  if (*(type_end - 1) == ' ')
    --type_end;

  snmptrapd_nv_context_add_name_value(self->nv_context, "type", type_start, type_end - type_start);

  const gchar *subtype_end = strpbrk(subtype_start, ")\n");
  gboolean subtype_exists = subtype_end && *subtype_end == ')';

  if (!subtype_exists)
    return FALSE;

  snmptrapd_nv_context_add_name_value(self->nv_context, "subtype", subtype_start, subtype_end - subtype_start);

  *self->input_len -= (subtype_end + 1) - *self->input;
  *self->input = subtype_end + 1;
  return TRUE;
}

static gboolean
_parse_v1_enterprise_oid(SnmpTrapdHeaderParser *self)
{
  const gchar *enterprise_string_start = *self->input;
  gsize input_left = *self->input_len;

  while (*self->input_len > 0 && !g_ascii_isspace(**self->input))
    {
      ++(*self->input);
      --(*self->input_len);
    }

  gsize enterprise_string_length = input_left - *self->input_len;

  /* enterprise_string is optional */
  if (enterprise_string_length == 0)
    return TRUE;

  snmptrapd_nv_context_add_name_value(self->nv_context, "enterprise_oid",
                                      enterprise_string_start, enterprise_string_length);

  return TRUE;
}


static gboolean
_parse_transport_info(SnmpTrapdHeaderParser *self)
{
  if(!scan_expect_char(self->input, (gint *) self->input_len, '['))
    return FALSE;

  _skip_spaces(self);

  const gchar *transport_info_start = *self->input;

  const gchar *transport_info_end = strchr(transport_info_start, '\n');
  if (!transport_info_end)
    return FALSE;

  while(*transport_info_end != ']')
    {
      --transport_info_end;
      if(transport_info_end == transport_info_start)
        return FALSE;
    }

  gsize transport_info_len = transport_info_end - transport_info_start;

  snmptrapd_nv_context_add_name_value(self->nv_context, "transport_info", transport_info_start, transport_info_len);

  *self->input_len -= (transport_info_end + 1) - *self->input;
  *self->input = transport_info_end + 1;
  return TRUE;
}

static gboolean
_parse_hostname(SnmpTrapdHeaderParser *self)
{
  const gchar *hostname_start = *self->input;
  gsize input_left = *self->input_len;

  while (*self->input_len > 0 && !g_ascii_isspace(**self->input))
    {
      ++(*self->input);
      --(*self->input_len);
    }

  gsize hostname_length = input_left - *self->input_len;
  if (hostname_length == 0)
    return FALSE;

  snmptrapd_nv_context_add_name_value(self->nv_context, "hostname", hostname_start, hostname_length);
  return TRUE;
}

static gboolean
_parse_timestamp(SnmpTrapdHeaderParser *self)
{
  GTimeVal now;
  cached_g_current_time(&now);
  time_t now_tv_sec = (time_t) now.tv_sec;

  LogStamp *stamp = &self->nv_context->msg->timestamps[LM_TS_STAMP];
  stamp->tv_usec = 0;
  stamp->zone_offset = -1;

  /* NOTE: we initialize various unportable fields in tm using a
   * localtime call, as the value of tm_gmtoff does matter but it does
   * not exist on all platforms and 0 initializing it causes trouble on
   * time-zone barriers */

  struct tm tm;
  cached_localtime(&now_tv_sec, &tm);
  if (!scan_std_timestamp(self->input, (gint *)self->input_len, &tm))
    return FALSE;

  tm.tm_isdst = -1;
  stamp->tv_sec = cached_mktime(&tm);
  stamp->zone_offset = get_local_timezone_ofs(stamp->tv_sec);

  return TRUE;
}

static gboolean
_try_parse_v1_info(SnmpTrapdHeaderParser *self)
{
  /* detect v1 format */
  const gchar *new_line = strchr(*self->input, '\n');
  if (new_line && new_line[1] != '\t')
    return TRUE;

  SnmpTrapdHeaderParserStep v1_info_parser_steps[] =
  {
    _parse_v1_enterprise_oid,
    _expect_newline,
    _expect_tab,
    _parse_v1_trap_type_and_subtype,
    _parse_v1_uptime
  };

  return _run_header_parser(self, v1_info_parser_steps,
                            sizeof(v1_info_parser_steps) / sizeof(SnmpTrapdHeaderParserStep));
}

gboolean
snmptrapd_header_parser_parse(SnmpTrapdNVContext *nv_context, const gchar **input, gsize *input_len)
{
  /*
   * DATE HOST [TRANSPORT_INFO]: V1_ENTERPRISE_OID
   * <TAB> V1_TRAP_TYPE (V1_TRAP_SUBTYPE) "Uptime:" UPTIME
   */

  SnmpTrapdHeaderParser self =
  {
    .nv_context = nv_context,
    .input = input,
    .input_len = input_len
  };

  SnmpTrapdHeaderParserStep parser_steps[] =
  {
    _parse_timestamp,
    _parse_hostname,
    _parse_transport_info,
    _expect_colon,
    _try_parse_v1_info,
    _expect_newline_or_eom
  };

  return _run_header_parser(&self, parser_steps, sizeof(parser_steps) / sizeof(SnmpTrapdHeaderParserStep));
}