Squelch some compiler warnings.
[obnox/wireshark/wip.git] / packet-udp.c
index a62915bcf5cb7f13120d68592cc137b25572d5ff..de0db83c55c8499c52b4119c7d41d1c4b6f758f0 100644 (file)
@@ -1,10 +1,10 @@
 /* packet-udp.c
  * Routines for UDP packet disassembly
  *
- * $Id: packet-udp.c,v 1.53 2000/04/03 09:24:08 guy Exp $
+ * $Id: packet-udp.c,v 1.94 2001/06/18 02:17:53 guy Exp $
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
  *
  * Richard Sharpe, 13-Feb-1999, added dispatch table support and 
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include <glib.h>
-#include "globals.h"
+#include "packet.h"
 #include "resolv.h"
+#include "ipproto.h"
+#include "in_cksum.h"
+#include "prefs.h"
 
-#include "plugins.h"
-
-#include "packet-auto_rp.h"
-#include "packet-bootp.h"
-#include "packet-dhis.h"
-#include "packet-dns.h"
-#include "packet-hsrp.h"
-#include "packet-icp.h"
-#include "packet-icq.h"
-#include "packet-ipx.h"
-#include "packet-isakmp.h"
-#include "packet-l2tp.h"
-#include "packet-nbns.h"
-#include "packet-ncp.h"
-#include "packet-ntp.h"
-#include "packet-radius.h"
-#include "packet-rip.h"
-#include "packet-ripng.h"
-#include "packet-rpc.h"
-#include "packet-rx.h"
-#include "packet-sap.h"
-#include "packet-snmp.h"
-#include "packet-srvloc.h"
-#include "packet-tacacs.h"
-#include "packet-tftp.h"
-#include "packet-time.h"
-#include "packet-vines.h"
-#include "packet-wccp.h"
-#include "packet-who.h"
+#include "packet-udp.h"
+
+#include "packet-ip.h"
+#include "conversation.h"
 
 static int proto_udp = -1;             
 static int hf_udp_srcport = -1;
@@ -80,9 +59,13 @@ static int hf_udp_dstport = -1;
 static int hf_udp_port = -1;
 static int hf_udp_length = -1;
 static int hf_udp_checksum = -1;
+static int hf_udp_checksum_bad = -1;
 
 static gint ett_udp = -1;
 
+/* Place UDP summary in proto tree */
+static gboolean udp_summary_in_tree = TRUE;
+
 /* UDP structs and definitions */
 
 typedef struct _e_udphdr {
@@ -92,246 +75,212 @@ typedef struct _e_udphdr {
   guint16 uh_sum;
 } e_udphdr;
 
-/* UDP Ports -> should go in packet-udp.h */
-
-#define UDP_PORT_TIME    37
-#define UDP_PORT_TACACS  49
-#define UDP_PORT_DNS     53
-#define UDP_PORT_BOOTPS  67
-#define UDP_PORT_TFTP    69
-#define UDP_PORT_IPX    213
-#define UDP_PORT_NTP   123
-#define UDP_PORT_NBNS  137
-#define UDP_PORT_NBDGM 138
-#define UDP_PORT_SNMP   161
-#define UDP_PORT_SNMP_TRAP 162
-#define UDP_PORT_SRVLOC 427
-#define UDP_PORT_PIM_RP_DISC 496
-#define UDP_PORT_ISAKMP        500
-#define UDP_PORT_WHO    513
-#define UDP_PORT_RIP    520
-#define UDP_PORT_RIPNG  521
-#define UDP_PORT_NCP    524
-#define UDP_PORT_VINES 573
-#define UDP_PORT_RADIUS 1645
-#define UDP_PORT_L2TP   1701
-#define UDP_PORT_RADIUS_NEW 1812
-#define UDP_PORT_RADACCT 1646
-#define UDP_PORT_RADACCT_NEW 1813
-#define UDP_PORT_HSRP   1985
-#define UDP_PORT_ICP    3130
-#define UDP_PORT_ICQ   4000
-#define UDP_PORT_SAP   9875
-#define UDP_PORT_RX_LOW 7000
-#define UDP_PORT_RX_HIGH 7009
-#define UDP_PORT_RX_AFS_BACKUPS 7021
-#define UDP_PORT_WCCP  2048
-#define UDP_PORT_DHIS1 58800
-#define UDP_PORT_DHIS2 58801
-
 static dissector_table_t udp_dissector_table;
+static heur_dissector_list_t heur_subdissector_list;
+static conv_dissector_list_t conv_subdissector_list;
+
+/* Determine if there is a sub-dissector and call it.  This has been */
+/* separated into a stand alone routine to other protocol dissectors */
+/* can call to it, ie. socks   */
+
+void
+decode_udp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
+       proto_tree *tree, int uh_sport, int uh_dport)
+{
+  tvbuff_t *next_tvb;
+
+  next_tvb = tvb_new_subset(tvb, offset, -1, -1);
 
-void init_dissect_udp(void) {
+/* determine if this packet is part of a conversation and call dissector */
+/* for the conversation if available */
+
+  if (try_conversation_dissector(&pinfo->src, &pinfo->dst, PT_UDP,
+               uh_sport, uh_dport, next_tvb, pinfo, tree))
+    return;
 
-  udp_dissector_table = register_dissector_table(hf_udp_port);
+  /* do lookup with the subdissector table */
+  if (dissector_try_port(udp_dissector_table, uh_sport, next_tvb, pinfo, tree) ||
+      dissector_try_port(udp_dissector_table, uh_dport, next_tvb, pinfo, tree))
+    return;
 
-  /* Now add the protocols we know about */
+  /* do lookup with the heuristic subdissector table */
+  if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
+    return;
 
-  dissector_add("udp.port", UDP_PORT_BOOTPS, dissect_bootp);
-  dissector_add("udp.port", UDP_PORT_TFTP, dissect_tftp);
-  dissector_add("udp.port", UDP_PORT_SAP, dissect_sap);
-  dissector_add("udp.port", UDP_PORT_HSRP, dissect_hsrp);
-  dissector_add("udp.port", UDP_PORT_PIM_RP_DISC, dissect_auto_rp);
-  dissector_add("udp.port", UDP_PORT_TACACS, dissect_tacacs);
-  dissector_add("udp.port", UDP_PORT_DHIS1, dissect_dhis);
-  dissector_add("udp.port", UDP_PORT_DHIS2, dissect_dhis);
+  dissect_data(next_tvb, 0, pinfo, tree);
 }
 
-void
-dissect_udp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
+
+static void
+dissect_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
+{
   e_udphdr  uh;
   guint16    uh_sport, uh_dport, uh_ulen, uh_sum;
-  dissector_t dissect_routine;
   proto_tree *udp_tree;
   proto_item *ti;
-
-  if (!BYTES_ARE_IN_FRAME(offset, sizeof(e_udphdr))) {
-    dissect_data(pd, offset, fd, tree);
-    return;
-  }
+  guint      len;
+  guint      reported_len;
+  vec_t      cksum_vec[4];
+  guint32    phdr[2];
+  guint16    computed_cksum;
+  int        offset = 0;
+
+  if (check_col(pinfo->fd, COL_PROTOCOL))
+    col_set_str(pinfo->fd, COL_PROTOCOL, "UDP");
+  if (check_col(pinfo->fd, COL_INFO))
+    col_clear(pinfo->fd, COL_INFO);
 
   /* Avoids alignment problems on many architectures. */
-  memcpy(&uh, &pd[offset], sizeof(e_udphdr));
+  tvb_memcpy(tvb, (guint8 *)&uh, offset, sizeof(e_udphdr));
   uh_sport = ntohs(uh.uh_sport);
   uh_dport = ntohs(uh.uh_dport);
   uh_ulen  = ntohs(uh.uh_ulen);
   uh_sum   = ntohs(uh.uh_sum);
   
-  if (check_col(fd, COL_PROTOCOL))
-    col_add_str(fd, COL_PROTOCOL, "UDP");
-  if (check_col(fd, COL_INFO))
-    col_add_fstr(fd, COL_INFO, "Source port: %s  Destination port: %s",
+  if (check_col(pinfo->fd, COL_INFO))
+    col_add_fstr(pinfo->fd, COL_INFO, "Source port: %s  Destination port: %s",
            get_udp_port(uh_sport), get_udp_port(uh_dport));
     
   if (tree) {
-    ti = proto_tree_add_item(tree, proto_udp, offset, 8);
+    if (udp_summary_in_tree) {
+      ti = proto_tree_add_protocol_format(tree, proto_udp, tvb, offset, 8,
+      "User Datagram Protocol, Src Port: %s (%u), Dst Port: %s (%u)",
+      get_udp_port(uh_sport), uh_sport, get_udp_port(uh_dport), uh_dport);
+    } else {
+      ti = proto_tree_add_item(tree, proto_udp, tvb, offset, 8, FALSE);
+    }
     udp_tree = proto_item_add_subtree(ti, ett_udp);
 
-    proto_tree_add_uint_format(udp_tree, hf_udp_srcport, offset, 2, uh_sport,
+    proto_tree_add_uint_format(udp_tree, hf_udp_srcport, tvb, offset, 2, uh_sport,
        "Source port: %s (%u)", get_udp_port(uh_sport), uh_sport);
-    proto_tree_add_uint_format(udp_tree, hf_udp_dstport, offset + 2, 2, uh_dport,
+    proto_tree_add_uint_format(udp_tree, hf_udp_dstport, tvb, offset + 2, 2, uh_dport,
        "Destination port: %s (%u)", get_udp_port(uh_dport), uh_dport);
 
-    proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset, 2, uh_sport);
-    proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset+2, 2, uh_dport);
-
-    proto_tree_add_item(udp_tree, hf_udp_length, offset + 4, 2,  uh_ulen);
-    proto_tree_add_uint_format(udp_tree, hf_udp_checksum, offset + 6, 2, uh_sum,
-       "Checksum: 0x%04x", uh_sum);
+    proto_tree_add_uint_hidden(udp_tree, hf_udp_port, tvb, offset, 2, uh_sport);
+    proto_tree_add_uint_hidden(udp_tree, hf_udp_port, tvb, offset+2, 2, uh_dport);
+
+    proto_tree_add_uint(udp_tree, hf_udp_length, tvb, offset + 4, 2,  uh_ulen);
+    reported_len = tvb_reported_length(tvb);
+    len = tvb_length(tvb);
+    if (uh_sum == 0) {
+      /* No checksum supplied in the packet. */
+      proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
+        offset + 6, 2, uh_sum, "Checksum: 0x%04x (none)", uh_sum);
+    } else if (!pinfo->fragmented && len >= reported_len) {
+      /* The packet isn't part of a fragmented datagram and isn't
+         truncated, so we can checksum it.
+        XXX - make a bigger scatter-gather list once we do fragment
+        reassembly? */
+
+      /* Set up the fields of the pseudo-header. */
+      cksum_vec[0].ptr = pinfo->src.data;
+      cksum_vec[0].len = pinfo->src.len;
+      cksum_vec[1].ptr = pinfo->dst.data;
+      cksum_vec[1].len = pinfo->dst.len;
+      cksum_vec[2].ptr = (const guint8 *)&phdr;
+      switch (pinfo->src.type) {
+
+      case AT_IPv4:
+       phdr[0] = htonl((IP_PROTO_UDP<<16) + reported_len);
+       cksum_vec[2].len = 4;
+       break;
+
+      case AT_IPv6:
+        phdr[0] = htonl(reported_len);
+        phdr[1] = htonl(IP_PROTO_UDP);
+        cksum_vec[2].len = 8;
+        break;
+
+      default:
+        /* TCP runs only atop IPv4 and IPv6.... */
+        g_assert_not_reached();
+        break;
+      }
+      cksum_vec[3].ptr = tvb_get_ptr(tvb, offset, len);
+      cksum_vec[3].len = reported_len;
+      computed_cksum = in_cksum(&cksum_vec[0], 4);
+      if (computed_cksum == 0) {
+        proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
+          offset + 6, 2, uh_sum, "Checksum: 0x%04x (correct)", uh_sum);
+      } else {
+       proto_tree_add_boolean_hidden(udp_tree, hf_udp_checksum_bad, tvb,
+          offset + 6, 2, TRUE);
+        proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
+          offset + 6, 2, uh_sum,
+         "Checksum: 0x%04x (incorrect, should be 0x%04x)", uh_sum,
+          in_cksum_shouldbe(uh_sum, computed_cksum));
+      }
+    } else {
+      proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
+        offset + 6, 2, uh_sum, "Checksum: 0x%04x", uh_sum);
+    }
   }
 
   /* Skip over header */
   offset += 8;
 
-  pi.ptype = PT_UDP;
-  pi.srcport = uh_sport;
-  pi.destport = uh_dport;
-
-  /* ONC RPC.  We can't base this on anything in the UDP header; we have
-     to look at the payload.  If "dissect_rpc()" returns TRUE, it was
-     an RPC packet, otherwise it's some other type of packet. */
-  if (dissect_rpc(pd, offset, fd, tree))
-    return;
-
-  /* try to apply the plugins */
-#ifdef HAVE_PLUGINS
-  {
-      plugin *pt_plug = plugin_list;
-
-      if (pt_plug) {
-         while (pt_plug) {
-             if (pt_plug->enabled && !strcmp(pt_plug->protocol, "udp") &&
-                 tree && dfilter_apply(pt_plug->filter, tree, pd)) {
-                 pt_plug->dissector(pd, offset, fd, tree);
-                 return;
-             }
-             pt_plug = pt_plug->next;
-         }
-      }
-  }
-#endif
-
-  /* XXX - we should do all of this through the table of ports. */
-#define PORT_IS(port)  (uh_sport == port || uh_dport == port)
-  if (PORT_IS(UDP_PORT_BOOTPS))
-      dissect_bootp(pd, offset, fd, tree);
-  else if (PORT_IS(UDP_PORT_DNS))
-      dissect_dns(pd, offset, fd, tree);
-  else if (PORT_IS(UDP_PORT_SRVLOC))
-      dissect_srvloc(pd, offset, fd, tree);
-  else if (PORT_IS(UDP_PORT_ISAKMP))
-      dissect_isakmp(pd, offset, fd, tree);
-  else if (PORT_IS(UDP_PORT_RIP)) {
-      /* we should check the source port too (RIP: UDP src and dst port 520) */
-      dissect_rip(pd, offset, fd, tree);
-  } else if (PORT_IS(UDP_PORT_RIPNG))
-      dissect_ripng(pd, offset, fd, tree);
-  else if (PORT_IS(UDP_PORT_NCP))
-      dissect_ncp(pd, offset, fd, tree); /* XXX -- need to handle nw_server_address */
-  else if (PORT_IS(UDP_PORT_NBNS))
-      dissect_nbns(pd, offset, fd, tree);
-  else if (PORT_IS(UDP_PORT_NBDGM))
-      dissect_nbdgm(pd, offset, fd, tree);
-  else if (PORT_IS(UDP_PORT_NTP))
-      dissect_ntp(pd, offset, fd, tree);
-  else if (PORT_IS(UDP_PORT_WHO))
-      dissect_who(pd, offset, fd, tree);
-  else if (PORT_IS(UDP_PORT_IPX)) /* RFC 1234 */
-      dissect_ipx(pd, offset, fd, tree);
-  else if ((uh_sport >= UDP_PORT_RX_LOW && uh_sport <= UDP_PORT_RX_HIGH) ||
-       (uh_dport >= UDP_PORT_RX_LOW && uh_dport <= UDP_PORT_RX_HIGH) ||
-       PORT_IS(UDP_PORT_RX_AFS_BACKUPS)) 
-      dissect_rx(pd, offset, fd, tree); /* transarc AFS's RX protocol */
-  else if (PORT_IS(UDP_PORT_SNMP) || PORT_IS(UDP_PORT_SNMP_TRAP))
-      dissect_snmp(pd, offset, fd, tree);
-  else if (PORT_IS(UDP_PORT_VINES)) {
-      /* FIXME: AFAIK, src and dst port must be the same */
-      dissect_vines_frp(pd, offset, fd, tree);
-  } else if (PORT_IS(UDP_PORT_TFTP)) {
-      /* This is the first point of call, but it adds a dynamic call */
-      dissector_add("udp.port", MAX(uh_sport, uh_dport), dissect_tftp);  /* Add to table */
-      dissect_tftp(pd, offset, fd, tree);
-  } else if (PORT_IS(UDP_PORT_TIME)) {
-      dissect_time(pd, offset, fd, tree);
-  } else if (PORT_IS(UDP_PORT_RADIUS) ||
-               PORT_IS(UDP_PORT_RADACCT) ||
-               PORT_IS(UDP_PORT_RADIUS_NEW) ||
-               PORT_IS(UDP_PORT_RADACCT_NEW) ) {
-      dissect_radius(pd, offset, fd, tree);
-   } else if ( PORT_IS(UDP_PORT_L2TP)) {
-      dissect_l2tp(pd,offset,fd,tree);
-  } else if ( PORT_IS(UDP_PORT_ICP)) {
-       dissect_icp(pd,offset,fd,tree);
- } else if ( PORT_IS(UDP_PORT_ICQ)) {
-        dissect_icq(pd,offset,fd,tree);
- } else if (PORT_IS(UDP_PORT_WCCP) ) {
-       dissect_wccp(pd, offset, fd, tree);
- } else {
-      /* OK, find a routine in the table, else use the default */
-
-      if ((dissect_routine = dissector_lookup(udp_dissector_table, uh_sport))) {
-
-       dissector_t dr2 = dissector_lookup(udp_dissector_table, uh_dport);
-
-       if (dr2 == NULL) {  /* Not in the table, add */
-
-         dissector_add("udp.port", uh_dport, dissect_tftp);
-
-       }
-
-       (*dissect_routine)(pd, offset, fd, tree);
-      }
-      else if ((dissect_routine = dissector_lookup(udp_dissector_table, uh_dport))) {
+  pinfo->ptype = PT_UDP;
+  pinfo->srcport = uh_sport;
+  pinfo->destport = uh_dport;
 
-       (*dissect_routine)(pd, offset, fd, tree);
+/* call sub-dissectors */
+  decode_udp_ports( tvb, offset, pinfo, tree, uh_sport, uh_dport);
 
-      }
-      else {
-
-       dissect_data(pd, offset, fd, tree);
-      }
-  }
 }
 
 void
 proto_register_udp(void)
 {
+       module_t *udp_module;
        static hf_register_info hf[] = {
                { &hf_udp_srcport,
                { "Source Port",        "udp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
-                       "" }},
+                       "", HFILL }},
 
                { &hf_udp_dstport,
                { "Destination Port",   "udp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
-                       "" }},
+                       "", HFILL }},
 
                { &hf_udp_port,
                { "Source or Destination Port", "udp.port", FT_UINT16, BASE_DEC,  NULL, 0x0,
-                       "" }},
+                       "", HFILL }},
 
                { &hf_udp_length,
                { "Length",             "udp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
-                       "" }},
+                       "", HFILL }},
+
+               { &hf_udp_checksum_bad,
+               { "Bad Checksum",       "udp.checksum_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
+                       "", HFILL }},
 
                { &hf_udp_checksum,
                { "Checksum",           "udp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
-                       "" }},
+                       "", HFILL }},
        };
        static gint *ett[] = {
                &ett_udp,
        };
 
-       proto_udp = proto_register_protocol("User Datagram Protocol", "udp");
+       proto_udp = proto_register_protocol("User Datagram Protocol",
+           "UDP", "udp");
        proto_register_field_array(proto_udp, hf, array_length(hf));
        proto_register_subtree_array(ett, array_length(ett));
+
+/* subdissector code */
+       udp_dissector_table = register_dissector_table("udp.port");
+       register_heur_dissector_list("udp", &heur_subdissector_list);
+       register_conv_dissector_list("udp", &conv_subdissector_list);
+       
+       /* Register configuration preferences */
+       udp_module = prefs_register_protocol(proto_udp, NULL);
+       prefs_register_bool_preference(udp_module, "udp_summary_in_tree",
+       "Show UDP summary in protocol treee", 
+       "Whether the UDP summary line should be shown in the protocol tree",
+       &udp_summary_in_tree);
+}
+
+void
+proto_reg_handoff_udp(void)
+{
+       dissector_add("ip.proto", IP_PROTO_UDP, dissect_udp, proto_udp);
 }