Removed trailing whitespaces from .h and .c files using the
[obnox/wireshark/wip.git] / packet-null.c
index 18bc02cce79437410f6960f37512150d1feeb034..851e5e79d22a263cc5c87413d3da7e7e2cdcbdf2 100644 (file)
@@ -1,12 +1,12 @@
 /* packet-null.c
  * Routines for null packet disassembly
  *
- * $Id: packet-null.c,v 1.22 2000/05/19 04:54:34 gram Exp $
+ * $Id: packet-null.c,v 1.55 2002/08/03 19:49:30 guy Exp $
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  *
- * This file created and by Mike Hall <mlh@io.com>
+ * This file created by Mike Hall <mlh@io.com>
  * Copyright 1998
  * 
  * This program is free software; you can redistribute it and/or
 # include "config.h"
 #endif
 
-#ifdef HAVE_SYS_TYPES_H
-#include <sys/types.h>
-#endif
-
 #include <glib.h>
 
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
-
-#include "packet.h"
-#include "packet-atalk.h"
+#include <string.h>
+#include <epan/packet.h>
+#include "packet-null.h"
+#include <epan/atalk-utils.h>
 #include "packet-ip.h"
-#include "packet-ipv6.h"
 #include "packet-ipx.h"
 #include "packet-osi.h"
 #include "packet-ppp.h"
 #include "etypes.h"
+#include "aftypes.h"
 
-extern const value_string etype_vals[];
+static dissector_table_t null_dissector_table;
 
 /* protocols and header fields */
 static int proto_null = -1;
@@ -58,21 +52,6 @@ static gint ett_null = -1;
 
 /* Null/loopback structs and definitions */
 
-/* Macro to byte-swap 32-bit quantities. */
-#define        BSWAP32(x) \
-       ((((x)&0xFF000000)>>24) | \
-        (((x)&0x00FF0000)>>8) | \
-        (((x)&0x0000FF00)<<8) | \
-        (((x)&0x000000FF)<<24))
-
-/* BSD AF_ values. */
-#define BSD_AF_INET            2
-#define BSD_AF_ISO             7
-#define BSD_AF_APPLETALK       16
-#define BSD_AF_IPX             23
-#define BSD_AF_INET6_BSD       24      /* OpenBSD (and probably NetBSD), BSD/OS */
-#define BSD_AF_INET6_FREEBSD   28
-
 /* Family values. */
 static const value_string family_vals[] = {
     {BSD_AF_INET,          "IP"             },
@@ -81,11 +60,14 @@ static const value_string family_vals[] = {
     {BSD_AF_IPX,           "Netware IPX/SPX"},
     {BSD_AF_INET6_BSD,     "IPv6"           },
     {BSD_AF_INET6_FREEBSD, "IPv6"           },
+    {BSD_AF_INET6_DARWIN,  "IPv6"           }, 
     {0,                    NULL             }
 };
 
+static dissector_handle_t ppp_hdlc_handle;
+static dissector_handle_t data_handle;
 void
-capture_null( const u_char *pd, packet_counts *ld )
+capture_null( const guchar *pd, int len, packet_counts *ld )
 {
   guint32 null_header;
 
@@ -174,15 +156,23 @@ capture_null( const u_char *pd, packet_counts *ld )
    * given that the effect of inserting the two 0 bytes depends only
    * on the byte order of the machine reading the file.)
    */
+  if (!BYTES_ARE_IN_FRAME(0, len, 2)) {
+    ld->other++;
+    return;
+  }
   if (pd[0] == 0xFF && pd[1] == 0x03) {
     /*
      * Hand it to PPP.
      */
-    capture_ppp(pd, ld);
+    capture_ppp_hdlc(pd, 0, len, ld);
   } else {
     /*
      * Treat it as a normal DLT_NULL header.
      */
+    if (!BYTES_ARE_IN_FRAME(0, len, (int)sizeof(null_header))) {
+      ld->other++;
+      return;
+    }
     memcpy((char *)&null_header, (char *)&pd[0], sizeof(null_header));
 
     if ((null_header & 0xFFFF0000) != 0) {
@@ -201,12 +191,12 @@ capture_null( const u_char *pd, packet_counts *ld )
      * BSD derivatives have different values?).
      */
     if (null_header > IEEE_802_3_MAX_LEN)
-      capture_ethertype(null_header, 4, pd, ld);
+      capture_ethertype(null_header, pd, 4, len, ld);
     else {
       switch (null_header) {
 
       case BSD_AF_INET:
-        capture_ip(pd, 4, ld);
+        capture_ip(pd, 4, len, ld);
         break;
 
       default:
@@ -217,15 +207,13 @@ capture_null( const u_char *pd, packet_counts *ld )
   }
 }
 
-void
+static void
 dissect_null(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 {
   guint32      null_header;
   proto_tree   *fh_tree;
   proto_item   *ti;
   tvbuff_t     *next_tvb;
-  const guint8 *next_pd;
-  int          next_offset;
 
   /*
    * See comment in "capture_null()" for an explanation of what we're
@@ -235,25 +223,24 @@ dissect_null(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
     /*
      * Hand it to PPP.
      */
-    tvb_compat(tvb, &next_pd, &next_offset);
-    dissect_ppp(next_pd, next_offset, pinfo->fd, tree);
+    call_dissector(ppp_hdlc_handle, tvb, pinfo, tree);
   } else {
 
     /* load the top pane info. This should be overwritten by
        the next protocol in the stack */
-    if(check_col(pinfo->fd, COL_RES_DL_SRC))
-      col_add_str(pinfo->fd, COL_RES_DL_SRC, "N/A" );
-    if(check_col(pinfo->fd, COL_RES_DL_DST))
-      col_add_str(pinfo->fd, COL_RES_DL_DST, "N/A" );
-    if(check_col(pinfo->fd, COL_PROTOCOL))
-      col_add_str(pinfo->fd, COL_PROTOCOL, "N/A" );
-    if(check_col(pinfo->fd, COL_INFO))
-      col_add_str(pinfo->fd, COL_INFO, "Null/Loopback" );
+    if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
+      col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "N/A" );
+    if(check_col(pinfo->cinfo, COL_RES_DL_DST))
+      col_set_str(pinfo->cinfo, COL_RES_DL_DST, "N/A" );
+    if(check_col(pinfo->cinfo, COL_PROTOCOL))
+      col_set_str(pinfo->cinfo, COL_PROTOCOL, "N/A" );
+    if(check_col(pinfo->cinfo, COL_INFO))
+      col_set_str(pinfo->cinfo, COL_INFO, "Null/Loopback" );
 
     /*
      * Treat it as a normal DLT_NULL header.
      */
-    memcpy((char *)&null_header, (char *)tvb_get_ptr(tvb, 0, sizeof(null_header)), sizeof(null_header));
+    tvb_memcpy(tvb, (guint8 *)&null_header, 0, sizeof(null_header));
 
     if ((null_header & 0xFFFF0000) != 0) {
       /* Byte-swap it. */
@@ -272,49 +259,25 @@ dissect_null(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
      */
     if (null_header > IEEE_802_3_MAX_LEN) {
       if (tree) {
-        ti = proto_tree_add_item(tree, proto_null, tvb, 0, 4, NULL);
+        ti = proto_tree_add_item(tree, proto_null, tvb, 0, 4, FALSE);
         fh_tree = proto_item_add_subtree(ti, ett_null);
       } else
        fh_tree = NULL;
-      ethertype(null_header, tvb, 4, pinfo, tree, fh_tree, hf_null_etype);
+      ethertype(null_header, tvb, 4, pinfo, tree, fh_tree, hf_null_etype, -1);
     } else {
       /* populate a tree in the second pane with the status of the link
          layer (ie none) */
       if (tree) {
-        ti = proto_tree_add_item(tree, proto_null, tvb, 0, 4, NULL);
+        ti = proto_tree_add_item(tree, proto_null, tvb, 0, 4, FALSE);
         fh_tree = proto_item_add_subtree(ti, ett_null);
-        proto_tree_add_item(fh_tree, hf_null_family, tvb, 0, 4, null_header);
+        proto_tree_add_uint(fh_tree, hf_null_family, tvb, 0, 4, null_header);
       }
 
       next_tvb = tvb_new_subset(tvb, 4, -1, -1);
-      tvb_compat(next_tvb, &next_pd, &next_offset);
-
-      switch (null_header) {
-
-      case BSD_AF_INET:
-        dissect_ip(next_pd, next_offset, pinfo->fd, tree);
-        break;
-
-      case BSD_AF_APPLETALK:
-        dissect_ddp(next_pd, next_offset, pinfo->fd, tree);
-        break;
-
-      case BSD_AF_IPX:
-        dissect_ipx(next_pd, next_offset, pinfo->fd, tree);
-        break;
-
-      case BSD_AF_ISO:
-        dissect_osi(next_pd, next_offset, pinfo->fd, tree);
-        break;
-
-      case BSD_AF_INET6_BSD:
-      case BSD_AF_INET6_FREEBSD:
-        dissect_ipv6(next_pd, next_offset, pinfo->fd, tree);
-        break;
-
-      default:
-        dissect_data_tvb(next_tvb, pinfo, tree);
-        break;
+      if (!dissector_try_port(null_dissector_table, null_header,
+           next_tvb, pinfo, tree)) {
+        /* No sub-dissector found.  Label rest of packet as "Data" */
+        call_dissector(data_handle,next_tvb, pinfo, tree);
       }
     }
   }
@@ -328,17 +291,35 @@ proto_register_null(void)
                /* registered here but handled in ethertype.c */
                { &hf_null_etype,
                { "Type",               "null.type", FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
-                       "" }},
+                       "", HFILL }},
 
                { &hf_null_family,
                { "Family",             "null.family",  FT_UINT32, BASE_HEX, VALS(family_vals), 0x0,
-                       "" }}
+                       "", HFILL }}
        };
        static gint *ett[] = {
                &ett_null,
        };
 
-       proto_null = proto_register_protocol ("Null/Loopback", "null" );
+       proto_null = proto_register_protocol("Null/Loopback", "Null", "null");
        proto_register_field_array(proto_null, hf, array_length(hf));
        proto_register_subtree_array(ett, array_length(ett));
+
+       /* subdissector code */
+       null_dissector_table = register_dissector_table("null.type",
+          "BSD AF_ type", FT_UINT32, BASE_DEC);
+}
+
+void
+proto_reg_handoff_null(void)
+{
+       dissector_handle_t null_handle;
+
+       /*
+        * Get a handle for the PPP-in-HDLC-like-framing dissector.
+        */
+       ppp_hdlc_handle = find_dissector("ppp_hdlc");
+       data_handle = find_dissector("data");
+       null_handle = create_dissector_handle(dissect_null, proto_null);
+       dissector_add("wtap_encap", WTAP_ENCAP_NULL, null_handle);
 }