2 * Routines for syslog message dissection
4 * Copyright 2000, Gerald Combs <gerald@ethereal.com>
6 * $Id: packet-syslog.c,v 1.19 2002/08/28 21:00:35 jmayer Exp $
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@ethereal.com>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 #include <epan/packet.h>
39 #define UDP_PORT_SYSLOG 514
41 #define PRIORITY_MASK 0x0007 /* 0000 0111 */
42 #define FACILITY_MASK 0x03f8 /* 1111 1000 */
44 /* The maximum number if priority digits to read in. */
47 #define COL_INFO_LEN 32
48 #define ELLIPSIS "..." /* ISO 8859-1 doesn't appear to have a real ellipsis. */
50 static const value_string short_lev[] = {
62 static const value_string short_fac[] = {
72 { 9, "CRON" }, /* The BSDs, Linux, and others */
75 { 15, "CRON" }, /* Solaris */
87 static const value_string long_lev[] = {
88 { 0, "EMERG - system is unusable" },
89 { 1, "ALERT - action must be taken immediately" },
90 { 2, "CRIT - critical conditions" },
91 { 3, "ERR - error conditions" },
92 { 4, "WARNING - warning conditions" },
93 { 5, "NOTICE - normal but significant condition" },
94 { 6, "INFO - informational" },
95 { 7, "DEBUG - debug-level messages" },
99 static const value_string long_fac[] = {
100 { 0, "KERN - kernel messages" },
101 { 1, "USER - random user-level messages" },
102 { 2, "MAIL - mail system" },
103 { 3, "DAEMON - system daemons" },
104 { 4, "AUTH - security/authorization messages" },
105 { 5, "SYSLOG - messages generated internally by syslogd" },
106 { 6, "LPR - line printer subsystem" },
107 { 7, "NEWS - network news subsystem" },
108 { 8, "UUCP - UUCP subsystem" },
109 { 9, "CRON - clock daemon (BSD, Linux)" },
110 { 10, "AUTHPRIV - security/authorization messages (private)" },
111 { 11, "FTP - ftp daemon" },
112 { 15, "CRON - clock daemon (Solaris)" },
113 { 16, "LOCAL0 - reserved for local use" },
114 { 17, "LOCAL1 - reserved for local use" },
115 { 18, "LOCAL2 - reserved for local use" },
116 { 19, "LOCAL3 - reserved for local use" },
117 { 20, "LOCAL4 - reserved for local use" },
118 { 21, "LOCAL5 - reserved for local use" },
119 { 22, "LOCAL6 - reserved for local use" },
120 { 23, "LOCAL7 - reserved for local use" },
124 static gint proto_syslog = -1;
125 static gint hf_syslog_level = -1;
126 static gint hf_syslog_facility = -1;
127 static gint hf_syslog_msg = -1;
129 static gint ett_syslog = -1;
131 /* I couldn't find any documentation for the syslog message format.
132 According to the BSD sources, the message format is '<', P, '>', and
133 T. P is a decimal value, which should be treated as an 8 bit
134 unsigned integer. The lower three bits comprise the level, and the
135 upper five bits are the facility. T is the message text.
138 static void dissect_syslog(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
140 gint pri = -1, lev = -1, fac = -1;
141 gint msg_off = 0, msg_len;
142 gint ellipsis_len = (COL_INFO_LEN - strlen(ELLIPSIS)) - 1;
144 proto_tree *syslog_tree;
145 gchar msg_str[COL_INFO_LEN];
147 if (check_col(pinfo->cinfo, COL_PROTOCOL))
148 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Syslog");
149 if (check_col(pinfo->cinfo, COL_INFO))
150 col_clear(pinfo->cinfo, COL_INFO);
152 if (tvb_get_guint8(tvb, msg_off) == '<') {
153 /* A facility and level follow. */
156 while (tvb_bytes_exist(tvb, msg_off, 1) &&
157 isdigit(tvb_get_guint8(tvb, msg_off)) && msg_off <= MAX_DIGITS) {
158 pri = pri * 10 + (tvb_get_guint8(tvb, msg_off) - '0');
161 if (tvb_get_guint8(tvb, msg_off) == '>')
163 fac = (pri & FACILITY_MASK) >> 3;
164 lev = pri & PRIORITY_MASK;
167 /* Copy the message into a string buffer, with a trailing ellipsis if needed. */
168 msg_len = tvb_ensure_length_remaining(tvb, msg_off);
169 if (msg_len >= COL_INFO_LEN) {
170 tvb_memcpy(tvb, msg_str, msg_off, ellipsis_len);
171 strcpy (msg_str + ellipsis_len, ELLIPSIS);
173 tvb_memcpy(tvb, msg_str, msg_off, msg_len);
174 msg_str[msg_len] = '\0';
177 if (check_col(pinfo->cinfo, COL_INFO)) {
179 col_add_fstr(pinfo->cinfo, COL_INFO, "%s.%s: %s",
180 val_to_str(fac, short_fac, "UNKNOWN"),
181 val_to_str(lev, short_lev, "UNKNOWN"), msg_str);
183 col_add_fstr(pinfo->cinfo, COL_INFO, "%s", msg_str);
189 ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0, -1,
190 "Syslog message: %s.%s: %s",
191 val_to_str(fac, short_fac, "UNKNOWN"),
192 val_to_str(lev, short_lev, "UNKNOWN"), msg_str);
194 ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0, -1,
195 "Syslog message: (unknown): %s", msg_str);
197 syslog_tree = proto_item_add_subtree(ti, ett_syslog);
199 ti = proto_tree_add_uint(syslog_tree, hf_syslog_facility, tvb, 0,
201 ti = proto_tree_add_uint(syslog_tree, hf_syslog_level, tvb, 0,
204 proto_tree_add_item(syslog_tree, hf_syslog_msg, tvb, msg_off,
210 /* Register the protocol with Ethereal */
211 void proto_register_syslog(void)
214 /* Setup list of header fields */
215 static hf_register_info hf[] = {
216 { &hf_syslog_facility,
217 { "Facility", "syslog.facility",
218 FT_UINT8, BASE_DEC, VALS(long_fac), FACILITY_MASK,
219 "Message facility", HFILL }
222 { "Level", "syslog.level",
223 FT_UINT8, BASE_DEC, VALS(long_lev), PRIORITY_MASK,
224 "Message level", HFILL }
227 { "Message", "syslog.msg",
228 FT_STRING, BASE_NONE, NULL, 0x0,
229 "Message Text", HFILL }
233 /* Setup protocol subtree array */
234 static gint *ett[] = {
238 /* Register the protocol name and description */
239 proto_syslog = proto_register_protocol("Syslog message", "Syslog", "syslog");
241 /* Required function calls to register the header fields and subtrees used */
242 proto_register_field_array(proto_syslog, hf, array_length(hf));
243 proto_register_subtree_array(ett, array_length(ett));
247 proto_reg_handoff_syslog(void)
249 dissector_handle_t syslog_handle;
251 syslog_handle = create_dissector_handle(dissect_syslog, proto_syslog);
252 dissector_add("udp.port", UDP_PORT_SYSLOG, syslog_handle);