2 * Routines for syslog message dissection
4 * Copyright 2000, Gerald Combs <gerald@ethereal.com>
6 * $Id: packet-syslog.c,v 1.13 2001/12/10 00:25:39 guy 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.
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_len = -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 msg_len = tvb_length(tvb);
154 if (tvb_get_guint8(tvb, 0) == '<' && isdigit(tvb_get_guint8(tvb, 1))) {
157 while (isdigit(tvb_get_guint8(tvb, msg_off)) && msg_off <= MAX_DIGITS &&
158 tvb_length_remaining(tvb, msg_off)) {
159 pri = pri * 10 + (tvb_get_guint8(tvb, msg_off) - '0');
162 if (tvb_get_guint8(tvb, msg_off) == '>')
164 fac = (pri & FACILITY_MASK) >> 3;
165 lev = pri & PRIORITY_MASK;
168 /* Copy the message into a string buffer, with a trailing ellipsis if needed. */
169 msg_len = tvb_length_remaining(tvb, msg_off);
170 if (msg_len >= COL_INFO_LEN) {
171 tvb_memcpy(tvb, msg_str, msg_off, ellipsis_len);
172 strcpy (msg_str + ellipsis_len, ELLIPSIS);
174 tvb_memcpy(tvb, msg_str, msg_off, msg_len);
175 msg_str[msg_len] = '\0';
178 if (check_col(pinfo->cinfo, COL_INFO)) {
180 col_add_fstr(pinfo->cinfo, COL_INFO, "%s.%s: %s",
181 val_to_str(fac, short_fac, "UNKNOWN"),
182 val_to_str(lev, short_lev, "UNKNOWN"), msg_str);
184 col_add_fstr(pinfo->cinfo, COL_INFO, "%s", msg_str);
190 ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0,
191 tvb_length(tvb), "Syslog message: %s.%s: %s",
192 val_to_str(fac, short_fac, "UNKNOWN"),
193 val_to_str(lev, short_lev, "UNKNOWN"), msg_str);
195 ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0,
196 tvb_length(tvb), "Syslog message: (unknown): %s", msg_str);
198 syslog_tree = proto_item_add_subtree(ti, ett_syslog);
200 ti = proto_tree_add_uint(syslog_tree, hf_syslog_facility, tvb, 0,
202 ti = proto_tree_add_uint(syslog_tree, hf_syslog_level, tvb, 0,
205 proto_tree_add_uint_format(syslog_tree, hf_syslog_msg_len, tvb, msg_off,
206 msg_len, msg_len, "Message (%d byte%s)", msg_len, plurality(msg_len, "", "s"));
211 /* Register the protocol with Ethereal */
212 void proto_register_syslog(void)
215 /* Setup list of header fields */
216 static hf_register_info hf[] = {
217 { &hf_syslog_facility,
218 { "Facility", "syslog.facility",
219 FT_UINT8, BASE_DEC, VALS(long_fac), FACILITY_MASK,
220 "Message facility", HFILL }
223 { "Level", "syslog.level",
224 FT_UINT8, BASE_DEC, VALS(long_lev), PRIORITY_MASK,
225 "Message level", HFILL }
227 { &hf_syslog_msg_len,
228 { "Message length", "syslog.msg_len",
229 FT_UINT32, BASE_DEC, NULL, 0x0,
230 "Message length, excluding priority descriptor", HFILL }
234 /* Setup protocol subtree array */
235 static gint *ett[] = {
239 /* Register the protocol name and description */
240 proto_syslog = proto_register_protocol("Syslog message", "Syslog", "syslog");
242 /* Required function calls to register the header fields and subtrees used */
243 proto_register_field_array(proto_syslog, hf, array_length(hf));
244 proto_register_subtree_array(ett, array_length(ett));
248 proto_reg_handoff_syslog(void)
250 dissector_handle_t syslog_handle;
252 syslog_handle = create_dissector_handle(dissect_syslog, proto_syslog);
253 dissector_add("udp.port", UDP_PORT_SYSLOG, syslog_handle);