Removed trailing whitespaces from .h and .c files using the
[obnox/wireshark/wip.git] / packet-udp.c
index 2b941a7c55acad87442b72f9f2905e896d7c4ea5..e695bddc25ab357f82f367e5586abd829b46cf7f 100644 (file)
@@ -1,7 +1,7 @@
 /* packet-udp.c
  * Routines for UDP packet disassembly
  *
- * $Id: packet-udp.c,v 1.97 2001/12/03 03:59:40 guy Exp $
+ * $Id: packet-udp.c,v 1.104 2002/08/02 23:36:04 jmayer Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
 # include "config.h"
 #endif
 
-#ifdef HAVE_SYS_TYPES_H
-# include <sys/types.h>
-#endif
-
-#ifdef HAVE_NETINET_IN_H
-# include <netinet/in.h>
-#endif
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include <glib.h>
-#include "packet.h"
-#include "resolv.h"
+#include <epan/packet.h>
+#include <epan/resolv.h>
 #include "ipproto.h"
 #include "in_cksum.h"
 #include "prefs.h"
@@ -51,7 +43,7 @@
 #include "packet-udp.h"
 
 #include "packet-ip.h"
-#include "conversation.h"
+#include <epan/conversation.h>
 
 static int proto_udp = -1;             
 static int hf_udp_srcport = -1;
@@ -77,7 +69,6 @@ typedef struct _e_udphdr {
 
 static dissector_table_t udp_dissector_table;
 static heur_dissector_list_t heur_subdissector_list;
-static conv_dissector_list_t conv_subdissector_list;
 static dissector_handle_t data_handle;
 
 /* Determine if there is a sub-dissector and call it.  This has been */
@@ -89,6 +80,7 @@ decode_udp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
        proto_tree *tree, int uh_sport, int uh_dport)
 {
   tvbuff_t *next_tvb;
+  int low_port, high_port;
 
   next_tvb = tvb_new_subset(tvb, offset, -1, -1);
 
@@ -99,9 +91,34 @@ decode_udp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
                uh_sport, uh_dport, next_tvb, pinfo, tree))
     return;
 
-  /* 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))
+  /* Do lookups with the subdissector table.
+     We try the port number with the lower value first, followed by the
+     port number with the higher value.  This means that, for packets
+     where a dissector is registered for *both* port numbers:
+
+       1) we pick the same dissector for traffic going in both directions;
+
+       2) we prefer the port number that's more likely to be the right
+          one (as that prefers well-known ports to reserved ports);
+
+     although there is, of course, no guarantee that any such strategy
+     will always pick the right port number.
+
+     XXX - we ignore port numbers of 0, as some dissectors use a port
+     number of 0 to disable the port, and as RFC 768 says that the source
+     port in UDP datagrams is optional and is 0 if not used. */
+  if (uh_sport > uh_dport) {
+    low_port = uh_dport;
+    high_port = uh_sport;
+  } else {
+    low_port = uh_sport;
+    high_port = uh_dport;
+  }
+  if (low_port != 0 &&
+      dissector_try_port(udp_dissector_table, low_port, next_tvb, pinfo, tree))
+    return;
+  if (high_port != 0 &&
+      dissector_try_port(udp_dissector_table, high_port, next_tvb, pinfo, tree))
     return;
 
   /* do lookup with the heuristic subdissector table */
@@ -126,20 +143,20 @@ dissect_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
   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);
+  if (check_col(pinfo->cinfo, COL_PROTOCOL))
+    col_set_str(pinfo->cinfo, COL_PROTOCOL, "UDP");
+  if (check_col(pinfo->cinfo, COL_INFO))
+    col_clear(pinfo->cinfo, COL_INFO);
 
   /* Avoids alignment problems on many architectures. */
   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);
+  uh_sport = g_ntohs(uh.uh_sport);
+  uh_dport = g_ntohs(uh.uh_dport);
+  uh_ulen  = g_ntohs(uh.uh_ulen);
+  uh_sum   = g_ntohs(uh.uh_sum);
   
-  if (check_col(pinfo->fd, COL_INFO))
-    col_add_fstr(pinfo->fd, COL_INFO, "Source port: %s  Destination port: %s",
+  if (check_col(pinfo->cinfo, COL_INFO))
+    col_add_fstr(pinfo->cinfo, COL_INFO, "Source port: %s  Destination port: %s",
            get_udp_port(uh_sport), get_udp_port(uh_dport));
     
   if (tree) {
@@ -182,13 +199,13 @@ dissect_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
       switch (pinfo->src.type) {
 
       case AT_IPv4:
-       phdr[0] = htonl((IP_PROTO_UDP<<16) + reported_len);
+       phdr[0] = g_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);
+        phdr[0] = g_htonl(reported_len);
+        phdr[1] = g_htonl(IP_PROTO_UDP);
         cksum_vec[2].len = 8;
         break;
 
@@ -268,14 +285,14 @@ proto_register_udp(void)
        proto_register_subtree_array(ett, array_length(ett));
 
 /* subdissector code */
-       udp_dissector_table = register_dissector_table("udp.port");
+       udp_dissector_table = register_dissector_table("udp.port",
+           "UDP port", FT_UINT16, BASE_DEC);
        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", 
+       "Show UDP summary in protocol tree", 
        "Whether the UDP summary line should be shown in the protocol tree",
        &udp_summary_in_tree);
 }