Note that capture filters don't work on Linux loopback devices with the
[obnox/wireshark/wip.git] / packet-srvloc.c
1 /* packet-srvloc.c
2  * Routines for SRVLOC (Service Location Protocol) packet dissection
3  * Copyright 1999, James Coe <jammer@cin.net>
4  *
5  * NOTE: This is Alpha software not all features have been verified yet.
6  *       In particular I have not had an opportunity to see how it 
7  *       responds to SRVLOC over TCP.
8  *
9  * $Id: packet-srvloc.c,v 1.6 2000/01/22 02:00:24 guy Exp $
10  *
11  * Ethereal - Network traffic analyzer
12  * By Gerald Combs <gerald@zing.org>
13  * Copyright 1998 Gerald Combs
14  *
15  * Service Location Protocol is RFC 2165
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * as published by the Free Software Foundation; either version 2
20  * of the License, or (at your option) any later version.
21  * 
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * 
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
41 #endif
42
43 #ifdef HAVE_NETINET_IN_H
44 # include <netinet/in.h>
45 #endif
46
47 #ifdef NEED_SNPRINTF_H
48 # ifdef HAVE_STDARG_H
49 #  include <stdarg.h>
50 # else
51 #  include <varargs.h>
52 # endif
53 # include "snprintf.h"
54 #endif
55
56 #include <string.h>
57 #include <time.h>
58 #include <glib.h>
59 #include "packet.h"
60 #include "packet-ipv6.h"
61
62 int proto_srvloc = -1;
63 int hf_srvloc_version = -1;
64 int hf_srvloc_function = -1;
65 int hf_srvloc_flags = -1;
66 int hf_srvloc_error = -1;
67
68 static gint ett_srvloc = -1;
69 gint ett_srvloc_flags = -1;
70
71 /* Define function types */
72
73 #define SRVREQ          1
74 #define SRVRPLY         2
75 #define SRVREG          3
76 #define SRVDEREG        4
77 #define SRVACK          5
78 #define ATTRRQST        6
79 #define ATTRRPLY        7
80 #define DAADVERT        8
81 #define SRVTYPERQST     9
82 #define SRVTYPERPLY     10
83
84 /* Create protocol header structure */
85
86 struct srvloc_hdr {
87     guint8      version;
88     guint8      function;
89     guint16     length;
90     guint8      flags;
91     guint8      dialect;
92     u_char      language[2];
93     guint16     encoding;
94     guint16     xid;
95 };
96
97 /* List to resolve function numbers to names */
98
99 static const value_string srvloc_functions[] = {
100     { SRVREQ, "Service Request" }, 
101     { SRVRPLY, "Service Reply" }, 
102     { SRVREG, "Service Registration" }, 
103     { SRVDEREG, "Service Deregister" }, 
104     { SRVACK, "Service Acknowledge" }, 
105     { ATTRRQST, "Attribute Request" }, 
106     { ATTRRPLY, "Attribute Reply" }, 
107     { DAADVERT, "DA Advertisement" }, 
108     { SRVTYPERQST, "Service Type Request" }, 
109     { SRVTYPERPLY, "Service Type Reply" }, 
110 };
111
112 /* List to resolve flag values to names */
113
114
115 /* Define flag masks */
116
117 #define FLAG_O          0x80
118 #define FLAG_M          0x40
119 #define FLAG_U          0x20
120 #define FLAG_A          0x10
121 #define FLAG_F          0x08
122
123 /* Define Error Codes */
124
125 #define SUCCESS         0
126 #define LANG_NOT_SPTD   1
127 #define PROT_PARSE_ERR  2
128 #define INVLD_REG       3
129 #define SCOPE_NOT_SPTD  4
130 #define CHRSET_NOT_UND  5
131 #define AUTH_ABSENT     6
132 #define AUTH_FAILED     7
133
134 /* List to resolve error codes to names */
135
136 static const value_string srvloc_errs[] = {
137     { SUCCESS, "No Error" },
138     { LANG_NOT_SPTD, "Language not supported" },
139     { PROT_PARSE_ERR, "Protocol parse error" },
140     { INVLD_REG, "Invalid registration" },
141     { SCOPE_NOT_SPTD, "Scope not supported" },
142     { CHRSET_NOT_UND, "Character set not understood" },
143     { AUTH_ABSENT, "Authentication absent" },
144     { AUTH_FAILED, "Authentication failed" },
145 };
146
147 void
148 dissect_authblk(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
149 {
150     struct tm *stamp;
151     time_t seconds;
152     double floatsec;
153     guint16 length;
154     
155     seconds = pntohl(&pd[offset]) - 2208988800ul;
156     stamp = gmtime(&seconds);
157     floatsec = stamp->tm_sec + pntohl(&pd[offset + 4]) / 4294967296.0;
158     proto_tree_add_text(tree, offset, 8,
159                         "Timestamp: %04d-%02d-%02d %02d:%02d:%07.4f UTC",
160                         stamp->tm_year + 1900, stamp->tm_mon + 1,
161                         stamp->tm_mday, stamp->tm_hour, stamp->tm_min,
162                         floatsec);
163     proto_tree_add_text(tree, offset + 8, 2, "Block Structure Desciptor: %u",
164                         pntohs(&pd[offset + 8]));
165     length = pntohs(&pd[offset + 10]);
166     proto_tree_add_text(tree, offset + 10, 2, "Authenticator length: %u",
167                         length);
168     offset += 12;
169     proto_tree_add_text(tree, offset, length, "Authentication block: %s",
170                         format_text(&pd[offset], length));
171     offset += length;
172 };
173
174 /* Packet dissection routine called by tcp & udp when port 427 detected */
175
176 void
177 dissect_srvloc(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
178 {
179     proto_item *ti, *tf;
180     proto_tree *srvloc_tree, *srvloc_flags;
181     struct srvloc_hdr srvloc_hdr;
182     int count;
183     int length;
184     
185     if (check_col(fd, COL_PROTOCOL))
186         col_add_str(fd, COL_PROTOCOL, "SRVLOC");
187     
188     if (check_col(fd, COL_INFO))
189         col_add_str(fd, COL_INFO, val_to_str(pd[offset + 1], srvloc_functions, "Unknown Function (%d)"));
190         
191     if (tree) {
192         ti = proto_tree_add_item(tree, proto_srvloc, offset, END_OF_FRAME, NULL);
193         srvloc_tree = proto_item_add_subtree(ti, ett_srvloc);
194     
195         if ( END_OF_FRAME > sizeof(srvloc_hdr) ) {
196             memcpy( &srvloc_hdr, &pd[offset], sizeof(srvloc_hdr) );
197             srvloc_hdr.length = pntohs(&srvloc_hdr.length);
198             srvloc_hdr.encoding = pntohs(&srvloc_hdr.encoding);
199             srvloc_hdr.xid = pntohs(&srvloc_hdr.xid);
200             proto_tree_add_item(srvloc_tree, hf_srvloc_version, offset, 1, srvloc_hdr.version);
201             proto_tree_add_item(srvloc_tree, hf_srvloc_function, offset + 1, 1, srvloc_hdr.function);
202             proto_tree_add_text(srvloc_tree, offset + 2, 2, "Length: %d",srvloc_hdr.length);
203             tf = proto_tree_add_item(srvloc_tree, hf_srvloc_flags, offset + 4, 1, srvloc_hdr.flags);
204             srvloc_flags = proto_item_add_subtree(tf, ett_srvloc_flags);
205             proto_tree_add_text(srvloc_flags, offset + 4, 0, "Overflow                          %d... .xxx", (srvloc_hdr.flags & FLAG_O) >> 7 );
206             proto_tree_add_text(srvloc_flags, offset + 4, 0, "Monolingual                       .%d.. .xxx", (srvloc_hdr.flags & FLAG_M) >> 6 ); 
207             proto_tree_add_text(srvloc_flags, offset + 4, 0, "URL Authentication Present        ..%d. .xxx", (srvloc_hdr.flags & FLAG_U) >> 5 );
208             proto_tree_add_text(srvloc_flags, offset + 4, 0, "Attribute Authentication Present  ...%d .xxx", (srvloc_hdr.flags & FLAG_A) >> 4 );
209             proto_tree_add_text(srvloc_flags, offset + 4, 0, "Fresh Service Entry               .... %dxxx", (srvloc_hdr.flags & FLAG_F) >> 3 );
210             proto_tree_add_text(srvloc_tree, offset + 5, 1, "Dialect: %d",srvloc_hdr.dialect); 
211             proto_tree_add_text(srvloc_tree, offset + 6, 2, "Language: %s", format_text(srvloc_hdr.language,2));
212             proto_tree_add_text(srvloc_tree, offset + 8, 2, "Encoding: %d", srvloc_hdr.encoding);
213             proto_tree_add_text(srvloc_tree, offset + 10, 2, "Transaction ID: %d", srvloc_hdr.xid);
214             offset += 12;
215         } else {
216         proto_tree_add_text(srvloc_tree, offset, END_OF_FRAME, "Invalid Packet: Length less than header.");
217         };
218         
219         if (( srvloc_hdr.length - 12 ) == END_OF_FRAME ) {
220             switch (srvloc_hdr.function) {
221                 case SRVREQ:
222                     proto_tree_add_text(srvloc_tree, offset, 0, "Service Request");
223                     length = pntohs(&pd[offset]);
224                     proto_tree_add_text(srvloc_tree, offset, 2, "Previous Response List Length: %d", length);
225                     offset += 2;
226                     proto_tree_add_text(srvloc_tree, offset, length, "Previous Response List: %s", format_text(&pd[offset], length)); 
227                     offset += length;
228                     length = pntohs(&pd[offset]);
229                     proto_tree_add_text(srvloc_tree, offset, 2, "Predicate length: %d", length);
230                     offset += 2;
231                     proto_tree_add_text(srvloc_tree, offset, length, "Predicate: %s", format_text(&pd[offset], length));
232                     offset += length;
233                 break;
234             
235                 case SRVRPLY:
236                     proto_tree_add_text(srvloc_tree, offset, 0, "Service Reply");
237                     proto_tree_add_item(srvloc_tree, hf_srvloc_error, offset, 2, pd[offset]);;
238                     offset += 2;
239                     proto_tree_add_text(srvloc_tree, offset, 2, "URL Count: %d", pntohs(&pd[offset]));
240                     offset += 2;
241                     for (count = pntohs(&pd[offset]) + 1; count > 0; count--, offset++) {
242                         proto_tree_add_text(srvloc_tree, offset, 2, "URL lifetime: %d", pntohs(&pd[offset]));
243                         offset += 2;
244                         length = pntohs(&pd[offset]);
245                         proto_tree_add_text(srvloc_tree, offset, 2, "URL length: %d", length);
246                         offset += 2;
247                         proto_tree_add_text(srvloc_tree, offset, length, "Service URL: %s", format_text(&pd[offset], length));
248                         offset += length;
249                         if ( (srvloc_hdr.flags & FLAG_U) == FLAG_U ) 
250                             dissect_authblk(pd, offset, fd, srvloc_tree);
251                     };
252                 break;
253
254                 case SRVREG:
255                     proto_tree_add_text(srvloc_tree, offset, 0, "Service Registration");
256                     proto_tree_add_text(srvloc_tree, offset, 2, "URL lifetime: %d", pntohs(&pd[offset]));
257                     offset += 2;
258                     length = pntohs(&pd[offset]);
259                     proto_tree_add_text(srvloc_tree, offset, 2, "URL length: %d", length);
260                     offset += 2;
261                     proto_tree_add_text(srvloc_tree, offset, length, "Service URL: %s", format_text(&pd[offset], length));
262                     offset += length;
263                     if ( (srvloc_hdr.flags & FLAG_U) == FLAG_U ) 
264                         dissect_authblk(pd, offset, fd, srvloc_tree);
265                     length = pntohs(&pd[offset]);
266                     proto_tree_add_text(srvloc_tree, offset, 2, "Attribute List length: %d", length);
267                     offset += 2;
268                     proto_tree_add_text(srvloc_tree, offset, length, "Attribute List: %s", format_text(&pd[offset], length));
269                     offset += length;
270                     if ( (srvloc_hdr.flags & FLAG_A) == FLAG_A ) 
271                         dissect_authblk(pd, offset, fd, srvloc_tree);
272                 break;
273
274                 case SRVDEREG:
275                     proto_tree_add_text(srvloc_tree, offset, 0, "Service Deregister");
276                     length = pntohs(&pd[offset]);
277                     proto_tree_add_text(srvloc_tree, offset, 2, "URL length: %d", length);
278                     offset += 2;
279                     proto_tree_add_text(srvloc_tree, offset, length, "Service URL: %s", format_text(&pd[offset], length));
280                     offset += length;
281                     if ( (srvloc_hdr.flags & FLAG_U) == FLAG_U ) 
282                         dissect_authblk(pd, offset, fd, srvloc_tree);
283                     length = pntohs(&pd[offset]);
284                     proto_tree_add_text(srvloc_tree, offset, 2, "Attribute List length: %d", length);
285                     offset += 2;
286                     proto_tree_add_text(srvloc_tree, offset, length, "Attribute List: %s", format_text(&pd[offset], length));
287                     offset += length;
288                     if ( (srvloc_hdr.flags & FLAG_A) == FLAG_A ) 
289                         dissect_authblk(pd, offset, fd, srvloc_tree);
290                 break;
291             
292                 case SRVACK:
293                     proto_tree_add_text(srvloc_tree, offset, 0, "Service Acknowledge");
294                     proto_tree_add_item(srvloc_tree, hf_srvloc_error, offset, 2, pd[offset]);;
295                     offset += 2;
296                 break;
297
298                 case ATTRRQST:
299                     proto_tree_add_text(srvloc_tree, offset, 0, "Attribute Request");
300                     length = pntohs(&pd[offset]);
301                     proto_tree_add_text(srvloc_tree, offset, 2, "Previous Response List Length: %d", length);
302                     offset += 2;
303                     proto_tree_add_text(srvloc_tree, offset, length, "Previous Response List: %s", format_text(&pd[offset], length)); 
304                     offset += length;
305                     length = pntohs(&pd[offset]);
306                     proto_tree_add_text(srvloc_tree, offset, 2, "URL length: %d", length);
307                     offset += 2;
308                     proto_tree_add_text(srvloc_tree, offset, length, "Service URL: %s", format_text(&pd[offset], length));
309                     offset += length;
310                     length = pntohs(&pd[offset]);
311                     proto_tree_add_text(srvloc_tree, offset, 2, "Scope List Length: %d", length);
312                     offset += 2;
313                     proto_tree_add_text(srvloc_tree, offset, length, "Scope Response List: %s", format_text(&pd[offset], length)); 
314                     offset += length;
315                     length = pntohs(&pd[offset]);
316                     proto_tree_add_text(srvloc_tree, offset, 2, "Attribute List length: %d", length);
317                     offset += 2;
318                     proto_tree_add_text(srvloc_tree, offset, length, "Attribute List: %s", format_text(&pd[offset], length));
319                     offset += length;
320                 break;
321             
322                 case ATTRRPLY:
323                     proto_tree_add_text(srvloc_tree, offset, 0, "Attribute Reply");
324                     proto_tree_add_item(srvloc_tree, hf_srvloc_error, offset, 2, pd[offset]);;
325                     offset += 2;
326                     length = pntohs(&pd[offset]);
327                     proto_tree_add_text(srvloc_tree, offset, 2, "Attribute List length: %d", length);
328                     offset += 2;
329                     proto_tree_add_text(srvloc_tree, offset, length, "Attribute List: %s", format_text(&pd[offset], length));
330                     offset += length;
331                     if ( (srvloc_hdr.flags & FLAG_A) == FLAG_A ) 
332                         dissect_authblk(pd, offset, fd, srvloc_tree);
333                 break;
334             
335                 case DAADVERT:
336                     proto_tree_add_text(srvloc_tree, offset, 0, "DA Advertisement");
337                     proto_tree_add_item(srvloc_tree, hf_srvloc_error, offset, 2, pd[offset]);;
338                     offset += 2;
339                     length = pntohs(&pd[offset]);
340                     proto_tree_add_text(srvloc_tree, offset, 2, "URL length: %d", length);
341                     offset += 2;
342                     proto_tree_add_text(srvloc_tree, offset, length, "Service URL: %s", format_text(&pd[offset], length));
343                     offset += length;
344                     length = pntohs(&pd[offset]);
345                     proto_tree_add_text(srvloc_tree, offset, 2, "Scope List Length: %d", length);
346                     offset += 2;
347                     proto_tree_add_text(srvloc_tree, offset, length, "Scope Response List: %s", format_text(&pd[offset], length)); 
348                     offset += length;
349                 break;
350
351                 case SRVTYPERQST:
352                     proto_tree_add_text(srvloc_tree, offset, 0, "Service Type Request");
353                     length = pntohs(&pd[offset]);
354                     proto_tree_add_text(srvloc_tree, offset, 2, "Previous Response List Length: %d", length);
355                     offset += 2;
356                     proto_tree_add_text(srvloc_tree, offset, length, "Previous Response List: %s", format_text(&pd[offset], length)); 
357                     offset += length;
358                     length = pntohs(&pd[offset]);
359                     proto_tree_add_text(srvloc_tree, offset, 2, "Naming Authority List length: %d", length);
360                     offset += 2;
361                     proto_tree_add_text(srvloc_tree, offset, length, "Naming Authority List: %s", format_text(&pd[offset], length)); 
362                     offset += length;
363                     length = pntohs(&pd[offset]);
364                     proto_tree_add_text(srvloc_tree, offset, 2, "Scope List Length: %d", length);
365                     offset += 2;
366                     proto_tree_add_text(srvloc_tree, offset, length, "Scope Response List: %s", format_text(&pd[offset], length)); 
367                     offset += length;
368                 break;
369
370                 case SRVTYPERPLY:
371                     proto_tree_add_text(srvloc_tree, offset, 0, "Service Type Reply");
372                     proto_tree_add_item(srvloc_tree, hf_srvloc_error, offset, 2, pd[offset]);;
373                     offset += 2;
374                     proto_tree_add_text(srvloc_tree, offset, 2, "Service Type Count: %d", pntohs(&pd[offset]));
375                     offset += 2;
376                     for (count = pntohs(&pd[offset]) + 1; count > 0; count--, offset++) {
377                         length = pntohs(&pd[offset]);
378                         proto_tree_add_text(srvloc_tree, offset, 2, "Service Type List length: %d", length);
379                         offset += 2;
380                         proto_tree_add_text(srvloc_tree, offset, length, "Service Type List: %s", format_text(&pd[offset], length));
381                         offset += length;
382                     };
383                 break;
384
385                 default:
386                     proto_tree_add_text(srvloc_tree, offset, END_OF_FRAME, "Unknown Function Type");
387             };
388         } else { proto_tree_add_text(srvloc_tree, offset, END_OF_FRAME,"Invalid packet: Bad length value");
389         };        
390     };
391 };
392
393 /* Register protocol with Ethereal. */
394
395 void
396 proto_register_srvloc(void)
397 {
398     static hf_register_info hf[] = {
399         { &hf_srvloc_version,
400             { "Version",           "srvloc.version",
401             FT_UINT8, BASE_DEC, NULL, 0x0,
402             "" }
403         },
404       
405         {&hf_srvloc_function,
406             {"Function", "srvloc.function", 
407             FT_UINT8, BASE_DEC, VALS(srvloc_functions), 0x0, 
408             ""}
409         },
410
411         {&hf_srvloc_flags,
412             {"Flags", "srvloc.flags", 
413             FT_UINT8, BASE_HEX, NULL, 0x0, 
414             ""}
415         },
416         
417         {&hf_srvloc_error,
418             {"Error Code", "srvloc.err",
419             FT_UINT8, BASE_DEC, VALS(srvloc_errs), 0x0,
420             ""}
421         },
422    };
423                   
424    static gint *ett[] = {
425       &ett_srvloc,
426       &ett_srvloc_flags,
427    };
428
429     proto_srvloc = proto_register_protocol("Service Location Protocol", "srvloc");
430     proto_register_field_array(proto_srvloc, hf, array_length(hf));
431     proto_register_subtree_array(ett, array_length(ett));
432 };