Updates from James Coe.
[metze/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.3 1999/12/15 01:48:58 guy Exp $
10  *
11  * Ethereal - Network traffic analyzer
12  * By Gerald Combs <gerald@unicom.net>
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 <glib.h>
58 #include "packet.h"
59 #include "packet-ipv6.h"
60
61 int proto_srvloc = -1;
62 int hf_srvloc_version = -1;
63 int hf_srvloc_function = -1;
64 int hf_srvloc_flags = -1;
65 int hf_srvloc_error = -1;
66
67 static gint ett_srvloc = -1;
68 gint ett_srvloc_flags = -1;
69
70 /* Define function types */
71
72 #define SRVREQ          1
73 #define SRVRPLY         2
74 #define SRVREG          3
75 #define SRVDEREG        4
76 #define SRVACK          5
77 #define ATTRRQST        6
78 #define ATTRRPLY        7
79 #define DAADVERT        8
80 #define SRVTYPERQST     9
81 #define SRVTYPERPLY     10
82
83 /* Create protocol header structure */
84
85 struct srvloc_hdr {
86     guint8      version;
87     guint8      function;
88     guint16     length;
89     guint8      flags;
90     guint8      dialect;
91     u_char      language[2];
92     guint16     encoding;
93     guint16     xid;
94 };
95
96 /* List to resolve function numbers to names */
97
98 static const value_string srvloc_functions[] = {
99     { SRVREQ, "Service Request" }, 
100     { SRVRPLY, "Service Reply" }, 
101     { SRVREG, "Service Registration" }, 
102     { SRVDEREG, "Service Deregister" }, 
103     { SRVACK, "Service Acknowledge" }, 
104     { ATTRRQST, "Attribute Request" }, 
105     { ATTRRPLY, "Attribute Reply" }, 
106     { DAADVERT, "DA Advertisement" }, 
107     { SRVTYPERQST, "Service Type Request" }, 
108     { SRVTYPERPLY, "Service Type Reply" }, 
109 };
110
111 /* List to resolve flag values to names */
112
113
114 /* Define flag masks */
115
116 #define FLAG_O          0x80
117 #define FLAG_M          0x40
118 #define FLAG_U          0x20
119 #define FLAG_A          0x10
120 #define FLAG_F          0x08
121
122 /* Define Error Codes */
123
124 #define SUCCESS         0
125 #define LANG_NOT_SPTD   1
126 #define PROT_PARSE_ERR  2
127 #define INVLD_REG       3
128 #define SCOPE_NOT_SPTD  4
129 #define CHRSET_NOT_UND  5
130 #define AUTH_ABSENT     6
131 #define AUTH_FAILED     7
132
133 /* List to resolve error codes to names */
134
135 static const value_string srvloc_errs[] = {
136     { SUCCESS, "No Error" },
137     { LANG_NOT_SPTD, "Language not supported" },
138     { PROT_PARSE_ERR, "Protocol parse error" },
139     { INVLD_REG, "Invalid registration" },
140     { SCOPE_NOT_SPTD, "Scope not supported" },
141     { CHRSET_NOT_UND, "Character set not understood" },
142     { AUTH_ABSENT, "Authentication absent" },
143     { AUTH_FAILED, "Authentication failed" },
144 };
145
146 void
147 dissect_authblk(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
148 {
149     struct tm *stamp;
150     time_t seconds;
151     double floatsec;
152     guint16 length;
153     
154     seconds = pntohl(&pd[offset]) - 2208988800ul;
155     stamp = gmtime(&seconds);
156     floatsec = stamp->tm_sec + pntohl(&pd[offset + 4]) / 4294967296.0;
157     proto_tree_add_text(tree, offset, 8,
158                         "Timestamp: %04d-%02d-%02d %02d:%02d:%07.4f UTC",
159                         stamp->tm_year + 1900, stamp->tm_mon, stamp->tm_mday,
160                         stamp->tm_hour, stamp->tm_min, floatsec);
161     proto_tree_add_text(tree, offset + 8, 2, "Block Structure Desciptor: %u",
162                         pntohs(&pd[offset + 8]));
163     length = pntohs(&pd[offset + 10]);
164     proto_tree_add_text(tree, offset + 10, 2, "Authenticator length: %u",
165                         length);
166     offset += 12;
167     proto_tree_add_text(tree, offset, length, "Authentication block: %s",
168                         format_text(&pd[offset], length));
169     offset += length;
170 };
171
172 /* Packet dissection routine called by tcp & udp when port 427 detected */
173
174 void
175 dissect_srvloc(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
176 {
177     proto_item *ti, *tf;
178     proto_tree *srvloc_tree, *srvloc_flags;
179     struct srvloc_hdr srvloc_hdr;
180     int count;
181     int length;
182     
183     if (check_col(fd, COL_PROTOCOL))
184         col_add_str(fd, COL_PROTOCOL, "SRVLOC");
185     
186     if (check_col(fd, COL_INFO))
187         col_add_str(fd, COL_INFO, val_to_str(pd[offset + 1], srvloc_functions, "Unknown Function (%d)"));
188         
189     if (tree) {
190         ti = proto_tree_add_item(tree, proto_srvloc, offset, END_OF_FRAME, NULL);
191         srvloc_tree = proto_item_add_subtree(ti, ett_srvloc);
192     
193         if ( END_OF_FRAME > sizeof(srvloc_hdr) ) {
194             memcpy( &srvloc_hdr, &pd[offset], sizeof(srvloc_hdr) );
195             srvloc_hdr.length = pntohs(&srvloc_hdr.length);
196             srvloc_hdr.encoding = pntohs(&srvloc_hdr.encoding);
197             srvloc_hdr.xid = pntohs(&srvloc_hdr.xid);
198             proto_tree_add_item(srvloc_tree, hf_srvloc_version, offset, 1, srvloc_hdr.version);
199             proto_tree_add_item(srvloc_tree, hf_srvloc_function, offset + 1, 1, srvloc_hdr.function);
200             proto_tree_add_text(srvloc_tree, offset + 2, 2, "Length: %d",srvloc_hdr.length);
201             tf = proto_tree_add_item(srvloc_tree, hf_srvloc_flags, offset + 4, 1, srvloc_hdr.flags);
202             srvloc_flags = proto_item_add_subtree(tf, ett_srvloc_flags);
203             proto_tree_add_text(srvloc_flags, offset + 4, 0, "Overflow                          %d... .xxx", (srvloc_hdr.flags & FLAG_O) >> 7 );
204             proto_tree_add_text(srvloc_flags, offset + 4, 0, "Monolingual                       .%d.. .xxx", (srvloc_hdr.flags & FLAG_M) >> 6 ); 
205             proto_tree_add_text(srvloc_flags, offset + 4, 0, "URL Authentication Present        ..%d. .xxx", (srvloc_hdr.flags & FLAG_U) >> 5 );
206             proto_tree_add_text(srvloc_flags, offset + 4, 0, "Attribute Authentication Present  ...%d .xxx", (srvloc_hdr.flags & FLAG_A) >> 4 );
207             proto_tree_add_text(srvloc_flags, offset + 4, 0, "Fresh Service Entry               .... %dxxx", (srvloc_hdr.flags & FLAG_F) >> 3 );
208             proto_tree_add_text(srvloc_tree, offset + 5, 1, "Dialect: %d",srvloc_hdr.dialect); 
209             proto_tree_add_text(srvloc_tree, offset + 6, 2, "Language: %s", format_text(srvloc_hdr.language,2));
210             proto_tree_add_text(srvloc_tree, offset + 8, 2, "Encoding: %d", srvloc_hdr.encoding);
211             proto_tree_add_text(srvloc_tree, offset + 10, 2, "Transaction ID: %d", srvloc_hdr.xid);
212             offset += 12;
213         } else {
214         proto_tree_add_text(srvloc_tree, offset, END_OF_FRAME, "Invalid Packet: Length less than header.");
215         };
216         
217         if (( srvloc_hdr.length - 12 ) == END_OF_FRAME ) {
218             switch (srvloc_hdr.function) {
219                 case SRVREQ:
220                     proto_tree_add_text(srvloc_tree, offset, 0, "Service Request");
221                     length = pntohs(&pd[offset]);
222                     proto_tree_add_text(srvloc_tree, offset, 2, "Previous Response List Length: %d", length);
223                     offset += 2;
224                     proto_tree_add_text(srvloc_tree, offset, length, "Previous Response List: %s", format_text(&pd[offset], length)); 
225                     offset += length;
226                     length = pntohs(&pd[offset]);
227                     proto_tree_add_text(srvloc_tree, offset, 2, "Predicate length: %d", length);
228                     offset += 2;
229                     proto_tree_add_text(srvloc_tree, offset, length, "Predicate: %s", format_text(&pd[offset], length));
230                     offset += length;
231                 break;
232             
233                 case SRVRPLY:
234                     proto_tree_add_text(srvloc_tree, offset, 0, "Service Reply");
235                     proto_tree_add_item(srvloc_tree, hf_srvloc_error, offset, 2, pd[offset]);;
236                     offset += 2;
237                     proto_tree_add_text(srvloc_tree, offset, 2, "URL Count: %d", pntohs(&pd[offset]));
238                     offset += 2;
239                     for (count = pntohs(&pd[offset]) + 1; count > 0; count--, offset++) {
240                         proto_tree_add_text(srvloc_tree, offset, 2, "URL lifetime: %d", pntohs(&pd[offset]));
241                         offset += 2;
242                         length = pntohs(&pd[offset]);
243                         proto_tree_add_text(srvloc_tree, offset, 2, "URL length: %d", length);
244                         offset += 2;
245                         proto_tree_add_text(srvloc_tree, offset, length, "Service URL: %s", format_text(&pd[offset], length));
246                         offset += length;
247                         if ( (srvloc_hdr.flags & FLAG_U) == FLAG_U ) 
248                             dissect_authblk(pd, offset, fd, srvloc_tree);
249                     };
250                 break;
251
252                 case SRVREG:
253                     proto_tree_add_text(srvloc_tree, offset, 0, "Service Registration");
254                     proto_tree_add_text(srvloc_tree, offset, 2, "URL lifetime: %d", pntohs(&pd[offset]));
255                     offset += 2;
256                     length = pntohs(&pd[offset]);
257                     proto_tree_add_text(srvloc_tree, offset, 2, "URL length: %d", length);
258                     offset += 2;
259                     proto_tree_add_text(srvloc_tree, offset, length, "Service URL: %s", format_text(&pd[offset], length));
260                     offset += length;
261                     if ( (srvloc_hdr.flags & FLAG_U) == FLAG_U ) 
262                         dissect_authblk(pd, offset, fd, srvloc_tree);
263                     length = pntohs(&pd[offset]);
264                     proto_tree_add_text(srvloc_tree, offset, 2, "Attribute List length: %d", length);
265                     offset += 2;
266                     proto_tree_add_text(srvloc_tree, offset, length, "Attribute List: %s", format_text(&pd[offset], length));
267                     offset += length;
268                     if ( (srvloc_hdr.flags & FLAG_A) == FLAG_A ) 
269                         dissect_authblk(pd, offset, fd, srvloc_tree);
270                 break;
271
272                 case SRVDEREG:
273                     proto_tree_add_text(srvloc_tree, offset, 0, "Service Deregister");
274                     length = pntohs(&pd[offset]);
275                     proto_tree_add_text(srvloc_tree, offset, 2, "URL length: %d", length);
276                     offset += 2;
277                     proto_tree_add_text(srvloc_tree, offset, length, "Service URL: %s", format_text(&pd[offset], length));
278                     offset += length;
279                     if ( (srvloc_hdr.flags & FLAG_U) == FLAG_U ) 
280                         dissect_authblk(pd, offset, fd, srvloc_tree);
281                     length = pntohs(&pd[offset]);
282                     proto_tree_add_text(srvloc_tree, offset, 2, "Attribute List length: %d", length);
283                     offset += 2;
284                     proto_tree_add_text(srvloc_tree, offset, length, "Attribute List: %s", format_text(&pd[offset], length));
285                     offset += length;
286                     if ( (srvloc_hdr.flags & FLAG_A) == FLAG_A ) 
287                         dissect_authblk(pd, offset, fd, srvloc_tree);
288                 break;
289             
290                 case SRVACK:
291                     proto_tree_add_text(srvloc_tree, offset, 0, "Service Acknowledge");
292                     proto_tree_add_item(srvloc_tree, hf_srvloc_error, offset, 2, pd[offset]);;
293                     offset += 2;
294                 break;
295
296                 case ATTRRQST:
297                     proto_tree_add_text(srvloc_tree, offset, 0, "Attribute Request");
298                     length = pntohs(&pd[offset]);
299                     proto_tree_add_text(srvloc_tree, offset, 2, "Previous Response List Length: %d", length);
300                     offset += 2;
301                     proto_tree_add_text(srvloc_tree, offset, length, "Previous Response List: %s", format_text(&pd[offset], length)); 
302                     offset += length;
303                     length = pntohs(&pd[offset]);
304                     proto_tree_add_text(srvloc_tree, offset, 2, "URL length: %d", length);
305                     offset += 2;
306                     proto_tree_add_text(srvloc_tree, offset, length, "Service URL: %s", format_text(&pd[offset], length));
307                     offset += length;
308                     length = pntohs(&pd[offset]);
309                     proto_tree_add_text(srvloc_tree, offset, 2, "Scope List Length: %d", length);
310                     offset += 2;
311                     proto_tree_add_text(srvloc_tree, offset, length, "Scope Response List: %s", format_text(&pd[offset], length)); 
312                     offset += length;
313                     length = pntohs(&pd[offset]);
314                     proto_tree_add_text(srvloc_tree, offset, 2, "Attribute List length: %d", length);
315                     offset += 2;
316                     proto_tree_add_text(srvloc_tree, offset, length, "Attribute List: %s", format_text(&pd[offset], length));
317                     offset += length;
318                 break;
319             
320                 case ATTRRPLY:
321                     proto_tree_add_text(srvloc_tree, offset, 0, "Attribute Reply");
322                     proto_tree_add_item(srvloc_tree, hf_srvloc_error, offset, 2, pd[offset]);;
323                     offset += 2;
324                     length = pntohs(&pd[offset]);
325                     proto_tree_add_text(srvloc_tree, offset, 2, "Attribute List length: %d", length);
326                     offset += 2;
327                     proto_tree_add_text(srvloc_tree, offset, length, "Attribute List: %s", format_text(&pd[offset], length));
328                     offset += length;
329                     if ( (srvloc_hdr.flags & FLAG_A) == FLAG_A ) 
330                         dissect_authblk(pd, offset, fd, srvloc_tree);
331                 break;
332             
333                 case DAADVERT:
334                     proto_tree_add_text(srvloc_tree, offset, 0, "DA Advertisement");
335                     proto_tree_add_item(srvloc_tree, hf_srvloc_error, offset, 2, pd[offset]);;
336                     offset += 2;
337                     length = pntohs(&pd[offset]);
338                     proto_tree_add_text(srvloc_tree, offset, 2, "URL length: %d", length);
339                     offset += 2;
340                     proto_tree_add_text(srvloc_tree, offset, length, "Service URL: %s", format_text(&pd[offset], length));
341                     offset += length;
342                     length = pntohs(&pd[offset]);
343                     proto_tree_add_text(srvloc_tree, offset, 2, "Scope List Length: %d", length);
344                     offset += 2;
345                     proto_tree_add_text(srvloc_tree, offset, length, "Scope Response List: %s", format_text(&pd[offset], length)); 
346                     offset += length;
347                 break;
348
349                 case SRVTYPERQST:
350                     proto_tree_add_text(srvloc_tree, offset, 0, "Service Type Request");
351                     length = pntohs(&pd[offset]);
352                     proto_tree_add_text(srvloc_tree, offset, 2, "Previous Response List Length: %d", length);
353                     offset += 2;
354                     proto_tree_add_text(srvloc_tree, offset, length, "Previous Response List: %s", format_text(&pd[offset], length)); 
355                     offset += length;
356                     length = pntohs(&pd[offset]);
357                     proto_tree_add_text(srvloc_tree, offset, 2, "Naming Authority List length: %d", length);
358                     offset += 2;
359                     proto_tree_add_text(srvloc_tree, offset, length, "Naming Authority List: %s", format_text(&pd[offset], length)); 
360                     offset += length;
361                     length = pntohs(&pd[offset]);
362                     proto_tree_add_text(srvloc_tree, offset, 2, "Scope List Length: %d", length);
363                     offset += 2;
364                     proto_tree_add_text(srvloc_tree, offset, length, "Scope Response List: %s", format_text(&pd[offset], length)); 
365                     offset += length;
366                 break;
367
368                 case SRVTYPERPLY:
369                     proto_tree_add_text(srvloc_tree, offset, 0, "Service Type Reply");
370                     proto_tree_add_item(srvloc_tree, hf_srvloc_error, offset, 2, pd[offset]);;
371                     offset += 2;
372                     proto_tree_add_text(srvloc_tree, offset, 2, "Service Type Count: %d", pntohs(&pd[offset]));
373                     offset += 2;
374                     for (count = pntohs(&pd[offset]) + 1; count > 0; count--, offset++) {
375                         length = pntohs(&pd[offset]);
376                         proto_tree_add_text(srvloc_tree, offset, 2, "Service Type List length: %d", length);
377                         offset += 2;
378                         proto_tree_add_text(srvloc_tree, offset, length, "Service Type List: %s", format_text(&pd[offset], length));
379                         offset += length;
380                     };
381                 break;
382
383                 default:
384                     proto_tree_add_text(srvloc_tree, offset, END_OF_FRAME, "Unknown Function Type");
385             };
386         } else { proto_tree_add_text(srvloc_tree, offset, END_OF_FRAME,"Invalid packet: Bad length value");
387         };        
388     };
389 };
390
391 /* Register protocol with Ethereal. */
392
393 void
394 proto_register_srvloc(void)
395 {
396     static hf_register_info hf[] = {
397         { &hf_srvloc_version,
398             { "Version",           "srvloc.version",
399             FT_UINT8, BASE_DEC, NULL, 0x0,
400             "" }
401         },
402       
403         {&hf_srvloc_function,
404             {"Function", "srvloc.function", 
405             FT_UINT8, BASE_DEC, VALS(srvloc_functions), 0x0, 
406             ""}
407         },
408
409         {&hf_srvloc_flags,
410             {"Flags", "srvloc.flags", 
411             FT_UINT8, BASE_HEX, NULL, 0x0, 
412             ""}
413         },
414         
415         {&hf_srvloc_error,
416             {"Error Code", "srvloc.err",
417             FT_UINT8, BASE_DEC, VALS(srvloc_errs), 0x0,
418             ""}
419         },
420    };
421                   
422    static gint *ett[] = {
423       &ett_srvloc,
424       &ett_srvloc_flags,
425    };
426
427     proto_srvloc = proto_register_protocol("Service Location Protocol", "srvloc");
428     proto_register_field_array(proto_srvloc, hf, array_length(hf));
429     proto_register_subtree_array(ett, array_length(ett));
430 };