Olivier Abad's patches to add:
[obnox/wireshark/wip.git] / packet-ntp.c
1 /* packet-ntp.c
2  * Routines for NTP packet dissection
3  * Copyright 1999, Nathan Neulinger <nneul@umr.edu>
4  *
5  * $Id: packet-ntp.c,v 1.1 1999/10/14 05:10:30 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@unicom.net>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from packet-tftp.c
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include <string.h>
43 #include <glib.h>
44 #include "packet.h"
45 #include "packet-ntp.h"
46
47 static int proto_ntp = -1;
48 static int hf_ntp_flags = -1;
49 static int hf_ntp_stratum = -1;
50 static int hf_ntp_ppoll = -1;
51 static int hf_ntp_precision = -1;
52 static int hf_ntp_rootdelay = -1;
53 static int hf_ntp_rootdispersion = -1;
54 static int hf_ntp_refid = -1;
55 static int hf_ntp_reftime = -1;
56 static int hf_ntp_org = -1;
57 static int hf_ntp_rec = -1;
58 static int hf_ntp_xmt = -1;
59 static int hf_ntp_keyid = -1;
60 static int hf_ntp_mac = -1;
61
62 void
63 dissect_ntp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
64 {
65         proto_tree      *ntp_tree, *ti;
66         struct ntp_packet *pkt;
67
68         /* get at least a full packet structure */
69         if ( !BYTES_ARE_IN_FRAME(offset, 48) ) /* 48 without keyid or mac */
70                 return;
71
72         pkt = (struct ntp_packet *) &pd[offset];
73         
74         if (check_col(fd, COL_PROTOCOL))
75                 col_add_str(fd, COL_PROTOCOL, "NTP");
76
77         if (check_col(fd, COL_INFO))
78                 col_add_str(fd, COL_INFO, "NTP");
79
80         if (tree) {
81                 ti = proto_tree_add_item(tree, proto_ntp, offset, END_OF_FRAME, NULL);
82                 ntp_tree = proto_item_add_subtree(ti, ETT_NTP);
83
84                 proto_tree_add_item(ntp_tree, hf_ntp_flags, offset, 1, pkt->flags);
85                 proto_tree_add_item(ntp_tree, hf_ntp_stratum, offset+1, 1, pkt->stratum);
86                 proto_tree_add_item(ntp_tree, hf_ntp_ppoll, offset+2, 1, pkt->ppoll);
87                 proto_tree_add_item(ntp_tree, hf_ntp_precision, offset+3, 1, pkt->precision);
88                 proto_tree_add_item(ntp_tree, hf_ntp_rootdelay, offset+4, 4, pkt->rootdelay);
89                 proto_tree_add_item(ntp_tree, hf_ntp_rootdispersion, offset+8, 4, pkt->rootdispersion);
90                 proto_tree_add_item(ntp_tree, hf_ntp_refid, offset+12, 4, pkt->refid);
91                 proto_tree_add_item(ntp_tree, hf_ntp_reftime, offset+16, 8, pkt->reftime);
92                 proto_tree_add_item(ntp_tree, hf_ntp_org, offset+24, 8, pkt->org);
93                 proto_tree_add_item(ntp_tree, hf_ntp_rec, offset+32, 8, pkt->rec);
94                 proto_tree_add_item(ntp_tree, hf_ntp_xmt, offset+40, 8, pkt->xmt);
95
96                 if ( BYTES_ARE_IN_FRAME(offset, 50) )
97                         proto_tree_add_item(ntp_tree, hf_ntp_keyid, offset+48, 4, pkt->keyid);
98                 if ( BYTES_ARE_IN_FRAME(offset, 53) )
99                         proto_tree_add_item(ntp_tree, hf_ntp_mac, offset+52, END_OF_FRAME, pkt->mac);
100         }
101 }
102
103 void
104 proto_register_ntp(void)
105 {
106         static hf_register_info hf[] = {
107                         { &hf_ntp_flags, {      
108                                 "Flags", "ntp.flags", FT_BYTES, BASE_HEX, 
109                                 NULL, 0, "Flags (Leap/Version/Mode)" }},
110                         { &hf_ntp_stratum, {    
111                                 "Peer Clock Stratum", "ntp.stratum", FT_BYTES, BASE_HEX, 
112                                 NULL, 0, "Peer Clock Stratum" }},
113                         { &hf_ntp_ppoll, {      
114                                 "Peer Polling Interval", "ntp.ppoll", FT_BYTES, BASE_HEX, 
115                                 NULL, 0, "Peer Polling Interval" }},
116                         { &hf_ntp_precision, {  
117                                 "Peer Clock Precision", "ntp.precision", FT_BYTES, BASE_HEX, 
118                                 NULL, 0, "Peer Clock Precision" }},
119                         { &hf_ntp_rootdelay, {  
120                                 "Distance to Primary", "ntp.rootdelay", FT_BYTES, BASE_HEX, 
121                                 NULL, 0, "Distance to Primary" }},
122                         { &hf_ntp_rootdispersion, {     
123                                 "Clock Dispersion", "ntp.rootdispersion", FT_BYTES, BASE_HEX, 
124                                 NULL, 0, "Clock Dispersion" }},
125                         { &hf_ntp_refid, {      
126                                 "Reference Clock ID", "ntp.refid", FT_BYTES, BASE_HEX, 
127                                 NULL, 0, "Reference Clock ID" }},
128                         { &hf_ntp_reftime, {    
129                                 "Reference Clock Update Time", "ntp.reftime", FT_BYTES, BASE_HEX, 
130                                 NULL, 0, "Reference Clock Update Time" }},
131                         { &hf_ntp_org, {        
132                                 "Originate Time Stamp", "ntp.org", FT_BYTES, BASE_HEX, 
133                                 NULL, 0, "Originate Time Stamp" }},
134                         { &hf_ntp_rec, {        
135                                 "Receive Time Stamp", "ntp.rec", FT_BYTES, BASE_HEX, 
136                                 NULL, 0, "Receive Time Stamp" }},
137                         { &hf_ntp_xmt, {        
138                                 "Transmit Time Stamp", "ntp.xmt", FT_BYTES, BASE_HEX, 
139                                 NULL, 0, "Transmit Time Stamp" }},
140                         { &hf_ntp_keyid, {      
141                                 "Key ID", "ntp.keyid", FT_BYTES, BASE_HEX, 
142                                 NULL, 0, "Key ID" }},
143                         { &hf_ntp_mac, {        
144                                 "Message Authentication Code", "ntp.mac", FT_BYTES, BASE_HEX, 
145                                 NULL, 0, "Message Authentication Code" }},
146         };
147
148         proto_ntp = proto_register_protocol("Network Time Protocol", "ntp");
149         proto_register_field_array(proto_ntp, hf, array_length(hf));
150 }