]> The Tcpdump Group git mirrors - tcpdump/blob - print-syslog.c
Merge branch 'master' of git+ssh://bpf.tcpdump.org/tcpdump/master/git/tcpdump
[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 #ifndef lint
18 static const char rcsid[] _U_ =
19 "@(#) $Header: /tcpdump/master/tcpdump/print-syslog.c,v 1.1 2004-10-29 11:42:53 hannes Exp $";
20 #endif
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <tcpdump-stdinc.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "interface.h"
32 #include "extract.h"
33
34 static const char tstr[] = "[|syslog]";
35
36 /*
37 * tokenlists and #defines taken from Ethereal - Network traffic analyzer
38 * by Gerald Combs <gerald@ethereal.com>
39 */
40
41 #define SYSLOG_SEVERITY_MASK 0x0007 /* 0000 0000 0000 0111 */
42 #define SYSLOG_FACILITY_MASK 0x03f8 /* 0000 0011 1111 1000 */
43 #define SYSLOG_MAX_DIGITS 3 /* The maximum number if priority digits to read in. */
44
45 static const struct tok syslog_severity_values[] = {
46 { 0, "emergency" },
47 { 1, "alert" },
48 { 2, "critical" },
49 { 3, "error" },
50 { 4, "warning" },
51 { 5, "notice" },
52 { 6, "info" },
53 { 7, "debug" },
54 { 0, NULL },
55 };
56
57 static const struct tok syslog_facility_values[] = {
58 { 0, "kernel" },
59 { 1, "user" },
60 { 2, "mail" },
61 { 3, "daemon" },
62 { 4, "auth" },
63 { 5, "syslog" },
64 { 6, "lpr" },
65 { 7, "news" },
66 { 8, "uucp" },
67 { 9, "cron" },
68 { 10, "authpriv" },
69 { 11, "ftp" },
70 { 12, "ntp" },
71 { 13, "security" },
72 { 14, "console" },
73 { 15, "cron" },
74 { 16, "local0" },
75 { 17, "local1" },
76 { 18, "local2" },
77 { 19, "local3" },
78 { 20, "local4" },
79 { 21, "local5" },
80 { 22, "local6" },
81 { 23, "local7" },
82 { 0, NULL },
83 };
84
85 void
86 syslog_print(register const u_char *pptr, register u_int len)
87 {
88 u_int16_t msg_off = 0;
89 u_int16_t pri = 0;
90 u_int16_t facility,severity;
91
92 /* extract decimal figures that are
93 * encapsulated within < > tags
94 * based on this decimal figure extract the
95 * severity and facility values
96 */
97
98 TCHECK2(*pptr, 1);
99 if (*(pptr+msg_off) == '<') {
100 msg_off++;
101 TCHECK2(*(pptr+msg_off), 1);
102 while ( *(pptr+msg_off) >= '0' &&
103 *(pptr+msg_off) <= '9' &&
104 msg_off <= SYSLOG_MAX_DIGITS) {
105 pri = pri * 10 + (*(pptr+msg_off) - '0');
106 msg_off++;
107 TCHECK2(*(pptr+msg_off), 1);
108 }
109 if (*(pptr+msg_off) != '>') {
110 printf("%s", tstr);
111 return;
112 }
113 msg_off++;
114 } else {
115 printf("%s", tstr);
116 return;
117 }
118
119 facility = (pri & SYSLOG_FACILITY_MASK) >> 3;
120 severity = pri & SYSLOG_SEVERITY_MASK;
121
122 if (vflag < 1 )
123 {
124 printf("SYSLOG %s.%s, length: %u",
125 tok2str(syslog_facility_values, "unknown (%u)", facility),
126 tok2str(syslog_severity_values, "unknown (%u)", severity),
127 len);
128 return;
129 }
130
131 printf("SYSLOG, length: %u\n\tFacility %s (%u), Severity %s (%u)\n\tMsg: ",
132 len,
133 tok2str(syslog_facility_values, "unknown (%u)", facility),
134 facility,
135 tok2str(syslog_severity_values, "unknown (%u)", severity),
136 severity);
137
138 /* print the syslog text in verbose mode */
139 for (; msg_off < len; msg_off++) {
140 TCHECK2(*(pptr+msg_off), 1);
141 safeputchar(*(pptr+msg_off));
142 }
143
144 if (vflag > 1)
145 print_unknown_data(gndo,pptr,"\n\t",len);
146
147 return;
148
149 trunc:
150 printf("%s", tstr);
151 }