]> The Tcpdump Group git mirrors - tcpdump/blob - print-syslog.c
We have to set the filter on every new file.
[tcpdump] / print-syslog.c
1 /*
2 * Copyright (c) 1998-2004 Hannes Gredler <hannes@tcpdump.org>
3 * The TCPDUMP project
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code
7 * distributions retain the above copyright notice and this paragraph
8 * in its entirety, and (2) distributions including binary code include
9 * the above copyright notice and this paragraph in its entirety in
10 * the documentation or other materials provided with the distribution.
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
12 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
13 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 * FOR A PARTICULAR PURPOSE.
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <netdissect-stdinc.h>
22
23 #include "netdissect.h"
24 #include "extract.h"
25
26 static const char tstr[] = "[|syslog]";
27
28 /*
29 * tokenlists and #defines taken from Ethereal - Network traffic analyzer
30 * by Gerald Combs <gerald@ethereal.com>
31 */
32
33 #define SYSLOG_SEVERITY_MASK 0x0007 /* 0000 0000 0000 0111 */
34 #define SYSLOG_FACILITY_MASK 0x03f8 /* 0000 0011 1111 1000 */
35 #define SYSLOG_MAX_DIGITS 3 /* The maximum number if priority digits to read in. */
36
37 static const struct tok syslog_severity_values[] = {
38 { 0, "emergency" },
39 { 1, "alert" },
40 { 2, "critical" },
41 { 3, "error" },
42 { 4, "warning" },
43 { 5, "notice" },
44 { 6, "info" },
45 { 7, "debug" },
46 { 0, NULL },
47 };
48
49 static const struct tok syslog_facility_values[] = {
50 { 0, "kernel" },
51 { 1, "user" },
52 { 2, "mail" },
53 { 3, "daemon" },
54 { 4, "auth" },
55 { 5, "syslog" },
56 { 6, "lpr" },
57 { 7, "news" },
58 { 8, "uucp" },
59 { 9, "cron" },
60 { 10, "authpriv" },
61 { 11, "ftp" },
62 { 12, "ntp" },
63 { 13, "security" },
64 { 14, "console" },
65 { 15, "cron" },
66 { 16, "local0" },
67 { 17, "local1" },
68 { 18, "local2" },
69 { 19, "local3" },
70 { 20, "local4" },
71 { 21, "local5" },
72 { 22, "local6" },
73 { 23, "local7" },
74 { 0, NULL },
75 };
76
77 void
78 syslog_print(netdissect_options *ndo,
79 register const u_char *pptr, register u_int len)
80 {
81 uint16_t msg_off = 0;
82 uint16_t pri = 0;
83 uint16_t facility,severity;
84
85 /* extract decimal figures that are
86 * encapsulated within < > tags
87 * based on this decimal figure extract the
88 * severity and facility values
89 */
90
91 ND_TCHECK2(*pptr, 1);
92 if (*(pptr+msg_off) == '<') {
93 msg_off++;
94 ND_TCHECK2(*(pptr + msg_off), 1);
95 while ( *(pptr+msg_off) >= '0' &&
96 *(pptr+msg_off) <= '9' &&
97 msg_off <= SYSLOG_MAX_DIGITS) {
98 pri = pri * 10 + (*(pptr+msg_off) - '0');
99 msg_off++;
100 ND_TCHECK2(*(pptr + msg_off), 1);
101 }
102 if (*(pptr+msg_off) != '>') {
103 ND_PRINT((ndo, "%s", tstr));
104 return;
105 }
106 msg_off++;
107 } else {
108 ND_PRINT((ndo, "%s", tstr));
109 return;
110 }
111
112 facility = (pri & SYSLOG_FACILITY_MASK) >> 3;
113 severity = pri & SYSLOG_SEVERITY_MASK;
114
115 if (ndo->ndo_vflag < 1 )
116 {
117 ND_PRINT((ndo, "SYSLOG %s.%s, length: %u",
118 tok2str(syslog_facility_values, "unknown (%u)", facility),
119 tok2str(syslog_severity_values, "unknown (%u)", severity),
120 len));
121 return;
122 }
123
124 ND_PRINT((ndo, "SYSLOG, length: %u\n\tFacility %s (%u), Severity %s (%u)\n\tMsg: ",
125 len,
126 tok2str(syslog_facility_values, "unknown (%u)", facility),
127 facility,
128 tok2str(syslog_severity_values, "unknown (%u)", severity),
129 severity));
130
131 /* print the syslog text in verbose mode */
132 for (; msg_off < len; msg_off++) {
133 ND_TCHECK2(*(pptr + msg_off), 1);
134 safeputchar(ndo, *(pptr + msg_off));
135 }
136
137 if (ndo->ndo_vflag > 1)
138 print_unknown_data(ndo, pptr, "\n\t", len);
139
140 return;
141
142 trunc:
143 ND_PRINT((ndo, "%s", tstr));
144 }