Commit my changes to handle KRB5 in SPNEGO ...
[metze/wireshark/wip.git] / packet-ntp.c
index 387cc938cd81f3a8617ea021afd389ed1d1a81a0..6524f1f03f1261478f8bb174bf1fcc15736244b3 100644 (file)
@@ -2,24 +2,24 @@
  * Routines for NTP packet dissection
  * Copyright 1999, Nathan Neulinger <nneul@umr.edu>
  *
- * $Id: packet-ntp.c,v 1.25 2001/01/22 08:03:45 guy Exp $
+ * $Id: packet-ntp.c,v 1.37 2002/08/28 21:00:24 jmayer Exp $
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
  *
  * Copied from packet-tftp.c
- * 
+ *
  * 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.
 
 #include <stdio.h>
 
-#ifdef HAVE_SYS_TYPES_H
-# include <sys/types.h>
-#endif
-
-#ifdef HAVE_NETINET_IN_H
-# include <netinet/in.h>
-#endif
-
 #include <string.h>
 #include <time.h>
 #include <math.h>
@@ -48,8 +40,8 @@
 # include "snprintf.h"
 #endif
 
-#include "packet.h"
-#include "resolv.h"
+#include <epan/packet.h>
+#include <epan/resolv.h>
 #include "packet-ntp.h"
 
 /*
@@ -221,7 +213,7 @@ static gint ett_ntp_flags = -1;
  * returns pointer to filled buffer.
  */
 static char *
-ntp_fmt_ts(guint8 *reftime, char* buff)
+ntp_fmt_ts(const guint8 *reftime, char* buff)
 {
        guint32 tempstmp, tempfrac;
        time_t temptime;
@@ -236,14 +228,18 @@ ntp_fmt_ts(guint8 *reftime, char* buff)
        } else {
                temptime = tempstmp - (guint32) NTP_BASETIME;
                bd = gmtime(&temptime);
-               fractime = bd->tm_sec + tempfrac / 4294967296.0;
-               snprintf(buff, NTP_TS_SIZE, "%04d-%02d-%02d %02d:%02d:%07.4f UTC",
-                        bd->tm_year + 1900, bd->tm_mon + 1, bd->tm_mday,
-                        bd->tm_hour, bd->tm_min, fractime);
+               if (bd != NULL) {
+                       fractime = bd->tm_sec + tempfrac / 4294967296.0;
+                       snprintf(buff, NTP_TS_SIZE,
+                                "%04d-%02d-%02d %02d:%02d:%07.4f UTC",
+                                bd->tm_year + 1900, bd->tm_mon + 1, bd->tm_mday,
+                                bd->tm_hour, bd->tm_min, fractime);
+               } else
+                       strncpy(buff, "Not representable", NTP_TS_SIZE);
        }
        return buff;
 }
-               
+
 /* dissect_ntp - dissects NTP packet data
  * tvb - tvbuff for packet data (IN)
  * pinfo - packet info
@@ -260,24 +256,24 @@ dissect_ntp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
        gint8           precision;
        double          rootdelay;
        double          rootdispersion;
-       guint8          *refid;
-       guint8          *reftime;
-       guint8          *org;
-       guint8          *rec;
-       guint8          *xmt;
+       const guint8    *refid;
+       guint32         refid_addr;
+       const guint8    *reftime;
+       const guint8    *org;
+       const guint8    *rec;
+       const guint8    *xmt;
        gchar           buff[NTP_TS_SIZE];
        int             i;
 
-       if (check_col(pinfo->fd, COL_PROTOCOL))
-               col_set_str(pinfo->fd, COL_PROTOCOL, "NTP");
+       if (check_col(pinfo->cinfo, COL_PROTOCOL))
+               col_set_str(pinfo->cinfo, COL_PROTOCOL, "NTP");
 
-       if (check_col(pinfo->fd, COL_INFO))
-               col_set_str(pinfo->fd, COL_INFO, "NTP");
+       if (check_col(pinfo->cinfo, COL_INFO))
+               col_set_str(pinfo->cinfo, COL_INFO, "NTP");
 
        if (tree) {
                /* Adding NTP item and subtree */
-               ti = proto_tree_add_item(tree, proto_ntp, tvb, 0,
-                   tvb_length(tvb), FALSE);
+               ti = proto_tree_add_item(tree, proto_ntp, tvb, 0, -1, FALSE);
                ntp_tree = proto_item_add_subtree(ti, ett_ntp);
 
                flags = tvb_get_guint8(tvb, 0);
@@ -314,9 +310,9 @@ dissect_ntp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
                ppoll = tvb_get_guint8(tvb, 2);
                proto_tree_add_uint_format(ntp_tree, hf_ntp_ppoll, tvb, 2, 1,
                                           ppoll,
-                                          (((ppoll >= 4) && (ppoll <= 16)) ? 
-                                          "Peer Pooling Interval: %u (%u sec)" :
-                                          "Peer Pooling Interval: invalid (%u)"),
+                                          (((ppoll >= 4) && (ppoll <= 16)) ?
+                                          "Peer Polling Interval: %u (%u sec)" :
+                                          "Peer Polling Interval: invalid (%u)"),
                                           ppoll,
                                           1 << ppoll);
 
@@ -372,7 +368,8 @@ dissect_ntp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
                        }
                } else {
                        buff[sizeof(buff) - 1] = '\0';
-                       strncpy (buff, get_hostname (tvb_get_ntohl(tvb, 12)),
+                       tvb_memcpy(tvb, (guint8 *)&refid_addr, 12, 4);
+                       strncpy (buff, get_hostname (refid_addr),
                            sizeof(buff));
                        if (buff[sizeof(buff) - 1] != '\0')
                                strcpy(&buff[sizeof(buff) - 4], "...");
@@ -387,7 +384,7 @@ dissect_ntp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
                reftime = tvb_get_ptr(tvb, 16, 8);
                proto_tree_add_bytes_format(ntp_tree, hf_ntp_reftime, tvb, 16, 8,
                                           reftime,
-                                          "Reference Clock Update Time: %s", 
+                                          "Reference Clock Update Time: %s",
                                           ntp_fmt_ts(reftime, buff));
 
                /* Originate Timestamp: This is the time at which the request departed
@@ -396,7 +393,7 @@ dissect_ntp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
                org = tvb_get_ptr(tvb, 24, 8);
                proto_tree_add_bytes_format(ntp_tree, hf_ntp_org, tvb, 24, 8,
                                           org,
-                                          "Originate Time Stamp: %s", 
+                                          "Originate Time Stamp: %s",
                                           ntp_fmt_ts(org, buff));
                /* Receive Timestamp: This is the time at which the request arrived at
                 * the server.
@@ -404,7 +401,7 @@ dissect_ntp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
                rec = tvb_get_ptr(tvb, 32, 8);
                proto_tree_add_bytes_format(ntp_tree, hf_ntp_rec, tvb, 32, 8,
                                           rec,
-                                          "Receive Time Stamp: %s", 
+                                          "Receive Time Stamp: %s",
                                           ntp_fmt_ts(rec, buff));
                /* Transmit Timestamp: This is the time at which the reply departed the
                 * server for the client.
@@ -412,7 +409,7 @@ dissect_ntp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
                xmt = tvb_get_ptr(tvb, 40, 8);
                proto_tree_add_bytes_format(ntp_tree, hf_ntp_xmt, tvb, 40, 8,
                                           xmt,
-                                          "Transmit Time Stamp: %s", 
+                                          "Transmit Time Stamp: %s",
                                           ntp_fmt_ts(xmt, buff));
 
                /* When the NTP authentication scheme is implemented, the
@@ -435,54 +432,54 @@ void
 proto_register_ntp(void)
 {
        static hf_register_info hf[] = {
-               { &hf_ntp_flags, {      
-                       "Flags", "ntp.flags", FT_UINT8, BASE_HEX, 
-                       NULL, 0, "Flags (Leap/Version/Mode)" }},
+               { &hf_ntp_flags, {
+                       "Flags", "ntp.flags", FT_UINT8, BASE_HEX,
+                       NULL, 0, "Flags (Leap/Version/Mode)", HFILL }},
                { &hf_ntp_flags_li, {
                        "Leap Indicator", "ntp.flags.li", FT_UINT8, BASE_DEC,
-                       VALS(li_types), NTP_LI_MASK, "Leap Indicator" }},
+                       VALS(li_types), NTP_LI_MASK, "Leap Indicator", HFILL }},
                { &hf_ntp_flags_vn, {
                        "Version number", "ntp.flags.vn", FT_UINT8, BASE_DEC,
-                       VALS(ver_nums), NTP_VN_MASK, "Version number" }},
+                       VALS(ver_nums), NTP_VN_MASK, "Version number", HFILL }},
                { &hf_ntp_flags_mode, {
                        "Mode", "ntp.flags.mode", FT_UINT8, BASE_DEC,
-                       VALS(mode_types), NTP_MODE_MASK, "Mode" }},
-               { &hf_ntp_stratum, {    
+                       VALS(mode_types), NTP_MODE_MASK, "Mode", HFILL }},
+               { &hf_ntp_stratum, {
                        "Peer Clock Stratum", "ntp.stratum", FT_UINT8, BASE_DEC,
-                       NULL, 0, "Peer Clock Stratum" }},
-               { &hf_ntp_ppoll, {      
-                       "Peer Polling Interval", "ntp.ppoll", FT_UINT8, BASE_DEC, 
-                       NULL, 0, "Peer Polling Interval" }},
-               { &hf_ntp_precision, {  
-                       "Peer Clock Precision", "ntp.precision", FT_UINT8, BASE_DEC, 
-                       NULL, 0, "Peer Clock Precision" }},
-               { &hf_ntp_rootdelay, {  
+                       NULL, 0, "Peer Clock Stratum", HFILL }},
+               { &hf_ntp_ppoll, {
+                       "Peer Polling Interval", "ntp.ppoll", FT_UINT8, BASE_DEC,
+                       NULL, 0, "Peer Polling Interval", HFILL }},
+               { &hf_ntp_precision, {
+                       "Peer Clock Precision", "ntp.precision", FT_UINT8, BASE_DEC,
+                       NULL, 0, "Peer Clock Precision", HFILL }},
+               { &hf_ntp_rootdelay, {
                        "Root Delay", "ntp.rootdelay", FT_DOUBLE, BASE_DEC,
-                       NULL, 0, "Root Delay" }},
-               { &hf_ntp_rootdispersion, {     
-                       "Clock Dispersion", "ntp.rootdispersion", FT_DOUBLE, BASE_DEC, 
-                       NULL, 0, "Clock Dispersion" }},
-               { &hf_ntp_refid, {      
-                       "Reference Clock ID", "ntp.refid", FT_BYTES, BASE_NONE, 
-                       NULL, 0, "Reference Clock ID" }},
-               { &hf_ntp_reftime, {    
-                       "Reference Clock Update Time", "ntp.reftime", FT_BYTES, BASE_NONE, 
-                       NULL, 0, "Reference Clock Update Time" }},
-               { &hf_ntp_org, {        
-                       "Originate Time Stamp", "ntp.org", FT_BYTES, BASE_NONE, 
-                       NULL, 0, "Originate Time Stamp" }},
-               { &hf_ntp_rec, {        
-                       "Receive Time Stamp", "ntp.rec", FT_BYTES, BASE_NONE, 
-                       NULL, 0, "Receive Time Stamp" }},
-               { &hf_ntp_xmt, {        
-                       "Transmit Time Stamp", "ntp.xmt", FT_BYTES, BASE_NONE, 
-                       NULL, 0, "Transmit Time Stamp" }},
-               { &hf_ntp_keyid, {      
-                       "Key ID", "ntp.keyid", FT_BYTES, BASE_HEX, 
-                       NULL, 0, "Key ID" }},
-               { &hf_ntp_mac, {        
-                       "Message Authentication Code", "ntp.mac", FT_BYTES, BASE_HEX, 
-                       NULL, 0, "Message Authentication Code" }},
+                       NULL, 0, "Root Delay", HFILL }},
+               { &hf_ntp_rootdispersion, {
+                       "Clock Dispersion", "ntp.rootdispersion", FT_DOUBLE, BASE_DEC,
+                       NULL, 0, "Clock Dispersion", HFILL }},
+               { &hf_ntp_refid, {
+                       "Reference Clock ID", "ntp.refid", FT_BYTES, BASE_NONE,
+                       NULL, 0, "Reference Clock ID", HFILL }},
+               { &hf_ntp_reftime, {
+                       "Reference Clock Update Time", "ntp.reftime", FT_BYTES, BASE_NONE,
+                       NULL, 0, "Reference Clock Update Time", HFILL }},
+               { &hf_ntp_org, {
+                       "Originate Time Stamp", "ntp.org", FT_BYTES, BASE_NONE,
+                       NULL, 0, "Originate Time Stamp", HFILL }},
+               { &hf_ntp_rec, {
+                       "Receive Time Stamp", "ntp.rec", FT_BYTES, BASE_NONE,
+                       NULL, 0, "Receive Time Stamp", HFILL }},
+               { &hf_ntp_xmt, {
+                       "Transmit Time Stamp", "ntp.xmt", FT_BYTES, BASE_NONE,
+                       NULL, 0, "Transmit Time Stamp", HFILL }},
+               { &hf_ntp_keyid, {
+                       "Key ID", "ntp.keyid", FT_BYTES, BASE_HEX,
+                       NULL, 0, "Key ID", HFILL }},
+               { &hf_ntp_mac, {
+                       "Message Authentication Code", "ntp.mac", FT_BYTES, BASE_HEX,
+                       NULL, 0, "Message Authentication Code", HFILL }},
         };
        static gint *ett[] = {
                &ett_ntp,
@@ -498,6 +495,9 @@ proto_register_ntp(void)
 void
 proto_reg_handoff_ntp(void)
 {
-       dissector_add("udp.port", UDP_PORT_NTP, dissect_ntp, proto_ntp);
-       dissector_add("tcp.port", TCP_PORT_NTP, dissect_ntp, proto_ntp);
+       dissector_handle_t ntp_handle;
+
+       ntp_handle = create_dissector_handle(dissect_ntp, proto_ntp);
+       dissector_add("udp.port", UDP_PORT_NTP, ntp_handle);
+       dissector_add("tcp.port", TCP_PORT_NTP, ntp_handle);
 }