Add the "Edit:Protocols..." feature which currently only implements
[obnox/wireshark/wip.git] / packet-who.c
1 /* packet-who.c
2  * Routines for who protocol (see man rwhod)
3  * Gilbert Ramirez <gram@xiexie.org>
4  *
5  * $Id: packet-who.c,v 1.9 2000/08/13 14:09:09 deniel Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <time.h>
36 #include <glib.h>
37 #include "packet.h"
38
39
40 /*
41  *
42 RWHOD(8)                 UNIX System Manager's Manual                 RWHOD(8)
43
44
45      The messages sent and received, are of the form:
46
47            struct  outmp {
48 0                   char    out_line[8];             tty name 
49 8                   char    out_name[8];             user id 
50 16                   long    out_time;               time on 
51            };
52
53            struct  whod {
54  0                   char    wd_vers;
55  1                   char    wd_type;
56  2                   char    wd_fill[2];
57  4                   int     wd_sendtime;
58  8                   int     wd_recvtime;
59 12                   char    wd_hostname[32];
60 44                   int     wd_loadav[3];
61 56                   int     wd_boottime;
62 60                   struct  whoent {
63                            struct  outmp we_utmp;
64 (20 each)                  int     we_idle;
65                    } wd_we[1024 / sizeof (struct whoent)];
66            };
67
68  Linux 2.0                       May 13, 1997                                2
69
70  *
71  */
72
73 static int proto_who = -1;
74 static int hf_who_vers = -1;
75 static int hf_who_type = -1;
76 static int hf_who_sendtime = -1;
77 static int hf_who_recvtime = -1;
78 static int hf_who_hostname = -1;
79 static int hf_who_loadav_5 = -1;
80 static int hf_who_loadav_10 = -1;
81 static int hf_who_loadav_15 = -1;
82 static int hf_who_boottime = -1;
83 static int hf_who_whoent = -1;
84 static int hf_who_tty = -1;
85 static int hf_who_uid = -1;
86 static int hf_who_timeon = -1;
87 static int hf_who_idle = -1;
88
89 static gint ett_who = -1;
90 static gint ett_whoent = -1;
91
92 #define UDP_PORT_WHO    513
93
94 static void dissect_whoent(const u_char *pd, int offset, frame_data *fd, proto_tree *tree);
95
96 static void
97 dissect_who(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
98 {
99
100         proto_tree      *who_tree = NULL;
101         proto_item      *who_ti = NULL;
102         gchar           server_name[33];
103         double          loadav_5 = 0.0, loadav_10 = 0.0, loadav_15 = 0.0;
104
105         OLD_CHECK_DISPLAY_AS_DATA(proto_who, pd, offset, fd, tree);
106
107         /* Summary information */
108         if (check_col(fd, COL_PROTOCOL))
109                 col_add_str(fd, COL_PROTOCOL, "WHO");
110
111         /* Figure out if we have enough bytes in the packet
112          * to retrieve the data that we want to put into the summary
113          * line: hostname and load average
114          */
115         if ( BYTES_ARE_IN_FRAME(offset, 60) ) {
116
117                 memcpy(server_name, &pd[offset + 12], 32);
118                 server_name[32] = '\0';
119
120                 loadav_5  = (double) pntohl(&pd[offset+44]) / 100.0;
121                 loadav_10 = (double) pntohl(&pd[offset+48]) / 100.0;
122                 loadav_15 = (double) pntohl(&pd[offset+52]) / 100.0;
123
124                 /* Summary information */
125                 if (check_col(fd, COL_INFO))
126                         col_add_fstr(fd, COL_INFO, "%s: %.02f %.02f %.02f",
127                                         server_name, loadav_5, loadav_10, loadav_15);
128         }
129         else {
130                 return;
131         }
132
133
134         if (tree) {
135                 struct timeval tv;
136
137                 tv.tv_usec = 0;
138
139                 /* We already know that the packet has enough data to fill in
140                  * the summary info. Retrieve that data */
141
142                 who_ti = proto_tree_add_item(tree, proto_who, NullTVB, offset, END_OF_FRAME, FALSE);
143                 who_tree = proto_item_add_subtree(who_ti, ett_who);
144
145                 proto_tree_add_uint(who_tree, hf_who_vers, NullTVB, offset, 1, pd[offset]);
146                 offset += 1;
147
148
149                 proto_tree_add_uint(who_tree, hf_who_type, NullTVB, offset, 1, pd[offset]);
150                 offset += 1;
151
152                 /* 2 filler bytes */
153                 offset += 2;
154
155                 tv.tv_sec = pntohl(&pd[offset]);
156                 proto_tree_add_time(who_tree, hf_who_sendtime, NullTVB, offset, 4, &tv);
157                 offset += 4;
158
159                 tv.tv_sec = pntohl(&pd[offset]);
160                 proto_tree_add_time(who_tree, hf_who_recvtime, NullTVB, offset, 4, &tv);
161                 offset += 4;
162
163                 proto_tree_add_string(who_tree, hf_who_hostname, NullTVB, offset, 32, server_name);
164                 offset += 32;
165
166                 proto_tree_add_double(who_tree, hf_who_loadav_5, NullTVB, offset, 4, loadav_5);
167                 offset += 4;
168
169                 proto_tree_add_double(who_tree, hf_who_loadav_10, NullTVB, offset, 4, loadav_10);
170                 offset += 4;
171
172                 proto_tree_add_double(who_tree, hf_who_loadav_15, NullTVB, offset, 4, loadav_15);
173                 offset += 4;
174
175                 tv.tv_sec = pntohl(&pd[offset]);
176                 proto_tree_add_time(who_tree, hf_who_boottime, NullTVB, offset, 4, &tv);
177                 offset += 4;
178
179                 dissect_whoent(pd, offset, fd, who_tree);
180         }
181 }
182
183 /* The man page says that (1024 / sizeof(struct whoent)) is the maximum number
184  * of whoent structures in the packet. */
185 #define SIZE_OF_WHOENT  24
186 #define MAX_NUM_WHOENTS (1024 / SIZE_OF_WHOENT)
187
188 static void
189 dissect_whoent(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
190 {
191         proto_tree      *whoent_tree = NULL;
192         proto_item      *whoent_ti = NULL;
193         int             line_offset = offset;
194         gchar           out_line[9];    
195         gchar           out_name[9];    
196         struct timeval  tv;
197         int             whoent_num = 0;
198         guint32         idle_secs; /* say that out loud... */
199
200         tv.tv_usec = 0;
201         out_line[8] = '\0';
202         out_name[8] = '\0';
203
204         while (BYTES_ARE_IN_FRAME(line_offset, SIZE_OF_WHOENT) && whoent_num < MAX_NUM_WHOENTS) {
205                 memcpy(out_line, &pd[line_offset], 8);
206                 memcpy(out_name, &pd[line_offset+8], 8);
207
208                 whoent_ti = proto_tree_add_item(tree, hf_who_whoent, NullTVB, line_offset, SIZE_OF_WHOENT, FALSE);
209                 whoent_tree = proto_item_add_subtree(whoent_ti, ett_whoent);
210
211                 proto_tree_add_string(whoent_tree, hf_who_tty, NullTVB, line_offset, 8, out_line);
212                 line_offset += 8;
213
214                 proto_tree_add_string(whoent_tree, hf_who_uid, NullTVB, line_offset, 8, out_name);
215                 line_offset += 8;
216
217                 tv.tv_sec = pntohl(&pd[line_offset]);
218                 proto_tree_add_time(whoent_tree, hf_who_timeon, NullTVB, line_offset, 4, &tv);
219                 line_offset += 4;
220
221                 idle_secs = pntohl(&pd[line_offset]);
222                 proto_tree_add_uint_format(whoent_tree, hf_who_idle, NullTVB, line_offset, 4, idle_secs,
223                                 "Idle: %s", time_secs_to_str(idle_secs));
224                 line_offset += 4;
225
226                 whoent_num++;
227         }
228 }
229
230 void
231 proto_register_who(void)
232 {
233         static hf_register_info hf[] = {
234                 { &hf_who_vers,
235                 { "Version",    "who.vers", FT_UINT8, BASE_DEC, NULL, 0x0,
236                         "" }},
237
238                 { &hf_who_type,
239                 { "Type",       "who.type", FT_UINT8, BASE_DEC, NULL, 0x0,
240                         "" }},
241
242                 { &hf_who_sendtime,
243                 { "Send Time",  "who.sendtime", FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
244                         "" }},
245
246                 { &hf_who_recvtime,
247                 { "Receive Time", "who.recvtime", FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
248                         "" }},
249
250                 { &hf_who_hostname,
251                 { "Hostname", "who.hostname", FT_STRING, BASE_NONE, NULL, 0x0,
252                         "" }},
253
254                 { &hf_who_loadav_5,
255                 { "Load Average Over Past  5 Minutes", "who.loadav_5", FT_DOUBLE, BASE_NONE, NULL, 0x0,
256                         "" }},
257
258                 { &hf_who_loadav_10,
259                 { "Load Average Over Past 10 Minutes", "who.loadav_10", FT_DOUBLE, BASE_NONE, NULL, 0x0,
260                         "" }},
261
262                 { &hf_who_loadav_15,
263                 { "Load Average Over Past 15 Minutes", "who.loadav_15", FT_DOUBLE, BASE_NONE, NULL, 0x0,
264                         "" }},
265
266                 { &hf_who_boottime,
267                 { "Boot Time", "who.boottime", FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
268                         "" }},
269
270                 { &hf_who_whoent,
271                 { "Who utmp Entry", "who.whoent", FT_NONE, BASE_NONE, NULL, 0x0,
272                         "" }},
273
274                 { &hf_who_tty,
275                 { "TTY Name", "who.tty", FT_STRING, BASE_NONE, NULL, 0x0,
276                         "" }},
277
278                 { &hf_who_uid,
279                 { "User ID", "who.uid", FT_STRING, BASE_NONE, NULL, 0x0,
280                         "" }},
281
282                 { &hf_who_timeon,
283                 { "Time On", "who.timeon", FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
284                         "" }},
285
286                 { &hf_who_idle,
287                 { "Time Idle", "who.idle", FT_UINT32, BASE_NONE, NULL, 0x0,
288                         "" }},
289         };
290
291         static gint *ett[] = {
292                 &ett_who,
293                 &ett_whoent,
294         };
295
296         proto_who = proto_register_protocol("Who", "who");
297         proto_register_field_array(proto_who, hf, array_length(hf));
298         proto_register_subtree_array(ett, array_length(ett));
299 }
300
301 void
302 proto_reg_handoff_who(void)
303 {
304         old_dissector_add("udp.port", UDP_PORT_WHO, dissect_who);
305 }