Frame numbers are unsigned.
[obnox/wireshark/wip.git] / packet-syslog.c
index c66eef70d9ef0715e5b8a8a90ca0c6310e102f15..45d66e9c98df8809023554a1d8ab5f3b9e0955a8 100644 (file)
@@ -1,25 +1,24 @@
 /* packet-syslog.c
  * Routines for syslog message dissection
  *
- * Copyright 2000, Gerald Combs <gerald@zing.org>
+ * Copyright 2000, Gerald Combs <gerald@ethereal.com>
  *
- * $Id: packet-syslog.c,v 1.2 2000/06/11 15:54:02 gerald Exp $
+ * $Id: packet-syslog.c,v 1.19 2002/08/28 21:00:35 jmayer Exp $
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
  *
- * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
@@ -35,7 +34,7 @@
 
 #include <string.h>
 #include <glib.h>
-#include "packet.h"
+#include <epan/packet.h>
 
 #define UDP_PORT_SYSLOG 514
 
@@ -45,9 +44,7 @@
 /* The maximum number if priority digits to read in. */
 #define MAX_DIGITS 3
 
-/* On my RH 6.2 box, memcpy overwrites nearby chunks of memory if this is
-   a multiple of four...  */
-#define COL_INFO_LEN 35
+#define COL_INFO_LEN 32
 #define ELLIPSIS "..." /* ISO 8859-1 doesn't appear to have a real ellipsis. */
 
 static const value_string short_lev[] = {
@@ -127,28 +124,18 @@ static const value_string long_fac[] = {
 static gint proto_syslog = -1;
 static gint hf_syslog_level = -1;
 static gint hf_syslog_facility = -1;
-static gint hf_syslog_msg_len = -1;
+static gint hf_syslog_msg = -1;
 
 static gint ett_syslog = -1;
 
-/* I couldn't find any documentation for the syslog message format.  
+/* I couldn't find any documentation for the syslog message format.
    According to the BSD sources, the message format is '<', P, '>', and
    T.  P is a decimal value, which should be treated as an 8 bit
    unsigned integer.  The lower three bits comprise the level, and the
    upper five bits are the facility.  T is the message text.
  */
 
-#if 0
 static void dissect_syslog(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
-{
-  gint pri = -1, lev, fac;
-  gint msg_off = 0, msg_len;
-  gint ellipsis_len = (COL_INFO_LEN - strlen(ELLIPSIS)) - 1;
-  proto_item *ti;
-  proto_tree *syslog_tree;
-  gchar msg_str[COL_INFO_LEN];
-#else
-static void dissect_syslog(const u_char *pd, int o, frame_data *fd, proto_tree *tree)
 {
   gint pri = -1, lev = -1, fac = -1;
   gint msg_off = 0, msg_len;
@@ -157,19 +144,17 @@ static void dissect_syslog(const u_char *pd, int o, frame_data *fd, proto_tree *
   proto_tree *syslog_tree;
   gchar msg_str[COL_INFO_LEN];
 
-  tvbuff_t *tvb;
-  packet_info *pinfo = &pi;
-  tvb = tvb_new_subset(pinfo->compat_top_tvb, o, -1, -1);
-#endif
+  if (check_col(pinfo->cinfo, COL_PROTOCOL))
+    col_set_str(pinfo->cinfo, COL_PROTOCOL, "Syslog");
+  if (check_col(pinfo->cinfo, COL_INFO))
+    col_clear(pinfo->cinfo, COL_INFO);
 
-  pinfo->current_proto = "Syslog";
-  msg_len = tvb_length(tvb);
-  
-  if (tvb_get_guint8(tvb, 0) == '<' && isdigit(tvb_get_guint8(tvb, 1))) {
+  if (tvb_get_guint8(tvb, msg_off) == '<') {
+    /* A facility and level follow. */
     msg_off++;
     pri = 0;
-    while (isdigit(tvb_get_guint8(tvb, msg_off)) && msg_off <= MAX_DIGITS &&
-           tvb_length_remaining(tvb, msg_off)) {
+    while (tvb_bytes_exist(tvb, msg_off, 1) &&
+           isdigit(tvb_get_guint8(tvb, msg_off)) && msg_off <= MAX_DIGITS) {
       pri = pri * 10 + (tvb_get_guint8(tvb, msg_off) - '0');
       msg_off++;
     }
@@ -180,38 +165,34 @@ static void dissect_syslog(const u_char *pd, int o, frame_data *fd, proto_tree *
   }
 
   /* Copy the message into a string buffer, with a trailing ellipsis if needed. */
-  msg_len = tvb_length_remaining(tvb, msg_off);
-  if (msg_len > ellipsis_len) {
+  msg_len = tvb_ensure_length_remaining(tvb, msg_off);
+  if (msg_len >= COL_INFO_LEN) {
     tvb_memcpy(tvb, msg_str, msg_off, ellipsis_len);
     strcpy (msg_str + ellipsis_len, ELLIPSIS);
-    msg_str[COL_INFO_LEN] = '\0';
   } else {
     tvb_memcpy(tvb, msg_str, msg_off, msg_len);
     msg_str[msg_len] = '\0';
   }
-    
-  if (check_col(pinfo->fd, COL_PROTOCOL)) 
-    col_add_str(pinfo->fd, COL_PROTOCOL, "Syslog");
-    
-  if (check_col(pinfo->fd, COL_INFO)) {
+
+  if (check_col(pinfo->cinfo, COL_INFO)) {
     if (pri >= 0) {
-      col_add_fstr(pinfo->fd, COL_INFO, "%s.%s: %s", 
+      col_add_fstr(pinfo->cinfo, COL_INFO, "%s.%s: %s",
         val_to_str(fac, short_fac, "UNKNOWN"),
         val_to_str(lev, short_lev, "UNKNOWN"), msg_str);
     } else {
-      col_add_fstr(pinfo->fd, COL_INFO, "%s", msg_str);
+      col_add_fstr(pinfo->cinfo, COL_INFO, "%s", msg_str);
     }
   }
-  
+
   if (tree) {
     if (pri >= 0) {
-      ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0,
-        tvb_length(tvb), "Syslog message: %s.%s: %s",
+      ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0, -1,
+        "Syslog message: %s.%s: %s",
         val_to_str(fac, short_fac, "UNKNOWN"),
         val_to_str(lev, short_lev, "UNKNOWN"), msg_str);
     } else {
-      ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0,
-        tvb_length(tvb), "Syslog message: (unknown): %s", msg_str);
+      ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0, -1,
+        "Syslog message: (unknown): %s", msg_str);
     }
     syslog_tree = proto_item_add_subtree(ti, ett_syslog);
     if (pri >= 0) {
@@ -220,32 +201,32 @@ static void dissect_syslog(const u_char *pd, int o, frame_data *fd, proto_tree *
       ti = proto_tree_add_uint(syslog_tree, hf_syslog_level, tvb, 0,
         msg_off, pri);
     }
-    proto_tree_add_uint_format(syslog_tree, hf_syslog_msg_len, tvb, msg_off,
-      msg_len, msg_len, "Message (%d byte%s)", msg_len, plurality(msg_len, "", "s"));
+    proto_tree_add_item(syslog_tree, hf_syslog_msg, tvb, msg_off,
+      msg_len, FALSE);
   }
   return;
 }
+
 /* Register the protocol with Ethereal */
 void proto_register_syslog(void)
-{                 
+{
 
   /* Setup list of header fields */
   static hf_register_info hf[] = {
     { &hf_syslog_facility,
       { "Facility",           "syslog.facility",
       FT_UINT8, BASE_DEC, VALS(long_fac), FACILITY_MASK,
-      "Message facility" }
+      "Message facility", HFILL }
     },
     { &hf_syslog_level,
       { "Level",              "syslog.level",
       FT_UINT8, BASE_DEC, VALS(long_lev), PRIORITY_MASK,
-      "Message level" }
+      "Message level", HFILL }
     },
-    { &hf_syslog_msg_len,
-      { "Message length",     "syslog.msg_len",
-      FT_UINT32, BASE_DEC, NULL, 0x0,
-      "Message length, excluding priority descriptor" }
+    { &hf_syslog_msg,
+      { "Message",            "syslog.msg",
+      FT_STRING, BASE_NONE, NULL, 0x0,
+      "Message Text", HFILL }
     },
   };
 
@@ -255,15 +236,18 @@ void proto_register_syslog(void)
   };
 
   /* Register the protocol name and description */
-  proto_syslog = proto_register_protocol("Syslog message", "syslog");
+  proto_syslog = proto_register_protocol("Syslog message", "Syslog", "syslog");
 
   /* Required function calls to register the header fields and subtrees used */
   proto_register_field_array(proto_syslog, hf, array_length(hf));
   proto_register_subtree_array(ett, array_length(ett));
-};
+}
 
 void
 proto_reg_handoff_syslog(void)
 {
-  dissector_add("udp.port", UDP_PORT_SYSLOG, dissect_syslog);
+  dissector_handle_t syslog_handle;
+
+  syslog_handle = create_dissector_handle(dissect_syslog, proto_syslog);
+  dissector_add("udp.port", UDP_PORT_SYSLOG, syslog_handle);
 }