Give the IPX dissector dissector hash tables for the IPX type and socket
[obnox/wireshark/wip.git] / packet-atalk.c
1 /* packet-atalk.c
2  * Routines for Appletalk packet disassembly (DDP, currently).
3  *
4  * $Id: packet-atalk.c,v 1.37 2000/05/30 03:35:51 guy Exp $
5  *
6  * Simon Wilkinson <sxw@dcs.ed.ac.uk>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #ifdef HAVE_SYS_TYPES_H
28 # include <sys/types.h>
29 #endif
30
31 #ifdef HAVE_NETINET_IN_H
32 # include <netinet/in.h>
33 #endif
34
35 #include <glib.h>
36 #include "packet.h"
37 #include "packet-atalk.h"
38 #include "etypes.h"
39 #include "ppptypes.h"
40
41 static int proto_ddp = -1;
42 static int hf_ddp_hopcount = -1;
43 static int hf_ddp_len = -1;
44 static int hf_ddp_checksum = -1;
45 static int hf_ddp_dst_net = -1;
46 static int hf_ddp_src_net = -1;
47 static int hf_ddp_dst_node = -1;
48 static int hf_ddp_src_node = -1;
49 static int hf_ddp_dst_socket = -1;
50 static int hf_ddp_src_socket = -1;
51 static int hf_ddp_type = -1;
52
53 static int proto_nbp = -1;
54 static int hf_nbp_op = -1;
55 static int hf_nbp_info = -1;
56 static int hf_nbp_count = -1;
57 static int hf_nbp_tid = -1;
58
59 static int hf_nbp_node_net = -1;
60 static int hf_nbp_node_port = -1;
61 static int hf_nbp_node_node = -1;
62 static int hf_nbp_node_enum = -1;
63 static int hf_nbp_node_object = -1;
64 static int hf_nbp_node_type = -1;
65 static int hf_nbp_node_zone = -1;
66
67 static int proto_rtmp = -1;
68 static int hf_rtmp_tuple_net = -1;
69 static int hf_rtmp_tuple_dist = -1;
70 static int hf_rtmp_net = -1;
71 static int hf_rtmp_node_len = -1;
72 static int hf_rtmp_node = -1;
73
74 static gint ett_nbp = -1;
75 static gint ett_nbp_info = -1;
76 static gint ett_nbp_node = -1;
77 static gint ett_rtmp = -1;
78 static gint ett_rtmp_tuple = -1;
79 static gint ett_ddp = -1;
80 static gint ett_pstring = -1;
81
82 static dissector_table_t ddp_dissector_table;
83
84 /* P = Padding, H = Hops, L = Len */
85 #if BYTE_ORDER == BIG_ENDIAN
86  /* PPHHHHLL LLLLLLLL */
87 # define ddp_hops(x)    ( ( x >> 10) & 0x3C )
88 # define ddp_len(x)             ( x & 0x03ff )
89 #else
90  /* LLLLLLLL PPHHHHLL*/
91 # define ddp_hops(x)    ( x & 0x3C )
92 # define ddp_len(x)             ( ntohs(x) & 0x03ff )
93 #endif
94 typedef struct _e_ddp {
95   guint16       hops_len; /* combines pad, hops, and len */
96   guint16       sum,dnet,snet;
97   guint8        dnode,snode;
98   guint8        dport,sport;
99   guint8        type;
100 } e_ddp;
101
102 #define DDP_HEADER_SIZE 13
103
104 gchar *
105 atalk_addr_to_str(const struct atalk_ddp_addr *addrp)
106 {
107   static gchar  str[3][14];
108   static gchar  *cur;
109
110   if (cur == &str[0][0]) {
111     cur = &str[1][0];
112   } else if (cur == &str[1][0]) {
113     cur = &str[2][0];
114   } else {
115     cur = &str[0][0];
116   }
117
118   sprintf(cur, "%u.%u:%u", addrp->net, addrp->node, addrp->port);
119   return cur;
120 }
121
122 static const value_string op_vals[] = {
123   {DDP_RTMPDATA, "AppleTalk Routing Table response or data" },
124   {DDP_NBP, "AppleTalk Name Binding Protocol packet"},
125   {DDP_ATP, "AppleTalk Transaction Protocol packet"},
126   {DDP_AEP, "AppleTalk Echo Protocol packet"},
127   {DDP_RTMPREQ, "AppleTalk Routing Table request"},
128   {DDP_ZIP, "AppleTalk Zone Information Protocol packet"},
129   {DDP_ADSP, "AppleTalk Data Stream Protocol"},
130   {DDP_EIGRP, "Cisco EIGRP for AppleTalk"},
131   {0, NULL}
132 };
133
134 #define NBP_LOOKUP 2
135 #define NBP_FORWARD 4
136 #define NBP_REPLY 3
137
138 static const value_string nbp_op_vals[] = {
139   {NBP_LOOKUP, "lookup"},
140   {NBP_FORWARD, "forward request"},
141   {NBP_REPLY, "reply"},
142   {0, NULL}
143 };
144
145 int dissect_pascal_string(const u_char *pd, int offset, frame_data *fd, 
146         proto_tree *tree, int hf_index)
147 {
148         int len;
149         
150         if ( ! BYTES_ARE_IN_FRAME(offset,1) ) {
151                 dissect_data(pd,offset,fd,tree);
152                 return END_OF_FRAME;
153         }
154                 
155         len = pd[offset];
156         if ( ! BYTES_ARE_IN_FRAME(offset,len) ) {
157                 dissect_data(pd,offset,fd,tree);
158                 return END_OF_FRAME;
159         }
160         offset++;
161
162         if ( tree )
163         {
164                 char *tmp;
165                 proto_tree *item;
166                 proto_tree *subtree;
167                 
168                 tmp = g_malloc( len+1 );
169                 memcpy(tmp, &pd[offset], len);
170                 tmp[len] = 0;
171                 item = proto_tree_add_item(tree, hf_index, NullTVB, offset-1, len+1, tmp);
172
173                 subtree = proto_item_add_subtree(item, ett_pstring);
174                 proto_tree_add_text(subtree, NullTVB, offset-1, 1, "Length: %d", len);
175                 proto_tree_add_text(subtree, NullTVB, offset, len, "Data: %s", tmp);
176                 
177                 g_free(tmp);
178         }
179         offset += len;
180         
181         return offset;  
182 }
183
184 static void
185 dissect_rtmp_request(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
186   dissect_data(pd, offset, fd, tree);
187   return;
188 }
189
190 static void
191 dissect_rtmp_data(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
192   proto_tree *rtmp_tree;
193   proto_item *ti;
194   guint16 net;
195   guint8 nodelen,nodelen_bits;
196   guint16 node; /* might be more than 8 bits */
197   int i;
198
199   if (!BYTES_ARE_IN_FRAME(offset, 3)) {
200     dissect_data(pd, offset, fd, tree);
201     return;
202   }
203
204   net = pntohs(&pd[offset]);
205   nodelen_bits = pd[offset+2];
206   if ( nodelen_bits <= 8 ) {
207         node = pd[offset]+1;
208         nodelen = 1;
209   } else {
210     node = pntohs(&pd[offset]);
211         nodelen = 2;
212   }
213   
214   if (check_col(fd, COL_PROTOCOL))
215     col_add_str(fd, COL_PROTOCOL, "RTMP");
216
217   if (check_col(fd, COL_INFO))
218     col_add_fstr(fd, COL_INFO, "Net: %d  Node Len: %d  Node: %d",
219                 net, nodelen_bits, node);
220   
221   if (tree) {
222     ti = proto_tree_add_item(tree, proto_rtmp, NullTVB, offset, END_OF_FRAME, NULL);
223     rtmp_tree = proto_item_add_subtree(ti, ett_rtmp);
224
225         proto_tree_add_item(rtmp_tree, hf_rtmp_net, NullTVB, offset, 2, net);
226         proto_tree_add_item(rtmp_tree, hf_rtmp_node_len, NullTVB, offset+2, 1, nodelen_bits);
227         proto_tree_add_item(rtmp_tree, hf_rtmp_node, NullTVB, offset+3, nodelen, nodelen);
228     offset += 3 + nodelen;
229
230     i = 1;
231         while ( BYTES_ARE_IN_FRAME(offset, 1) )
232         {
233                 proto_tree *tuple_item, *tuple_tree;
234                 guint16 tuple_net, tuple_net2;
235                 guint8 tuple_dist, tuple_dist2;
236
237                 if ( ! BYTES_ARE_IN_FRAME(offset, 3) )
238                 {
239                         dissect_data(pd,offset,fd,rtmp_tree);
240                         return;
241                 }
242
243                 tuple_net = pntohs(&pd[offset]);
244                 tuple_dist = pd[offset+2];
245
246                 tuple_item = proto_tree_add_text(rtmp_tree, NullTVB, offset, 3, 
247                         "Tuple %d:  Net: %d  Dist: %d",
248                         i, tuple_net, tuple_dist);
249                 tuple_tree = proto_item_add_subtree(tuple_item, ett_rtmp_tuple);
250
251                 proto_tree_add_item(tuple_tree, hf_rtmp_tuple_net, NullTVB, offset, 2, 
252                         tuple_net);
253                 proto_tree_add_item(tuple_tree, hf_rtmp_tuple_dist, NullTVB, offset+2, 1,
254                         tuple_dist);
255
256                 if ( tuple_dist == 0 || tuple_dist & 0x80 ) /* phase 1/2 */
257                 {
258                         if ( ! BYTES_ARE_IN_FRAME(offset+3, 3) )
259                         {
260                                 dissect_data(pd,offset,fd,rtmp_tree);
261                                 return;
262                         }
263
264                         tuple_net2 = pntohs(&pd[offset+3]);
265                         tuple_dist2 = pd[offset+5];
266
267                         proto_tree_add_item(tuple_tree, hf_rtmp_tuple_net, NullTVB, offset, 2, 
268                                 tuple_net2);
269                         proto_tree_add_item(tuple_tree, hf_rtmp_tuple_dist, NullTVB, offset+2, 1,
270                                 tuple_dist2);
271                                 
272                         proto_item_set_len(tuple_item, 6);
273                         offset += 6;
274                 }
275                 else /* screwy gatorbox/etc. */
276                 {
277                         offset += 3;
278                 }
279
280                 i++;
281         }
282   }
283
284   return;
285 }
286
287 static void
288 dissect_nbp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
289   proto_tree *nbp_tree;
290   proto_tree *nbp_info_tree;
291   proto_item *ti, *info_item;
292   guint op, count;
293   int i;
294
295   if (!BYTES_ARE_IN_FRAME(offset, 2)) {
296     dissect_data(pd, offset, fd, tree);
297     return;
298   }
299
300   op = pd[offset] >> 4;
301   count = pd[offset] & 0x0F;
302
303   if (check_col(fd, COL_PROTOCOL))
304     col_add_str(fd, COL_PROTOCOL, "NBP");
305
306   if (check_col(fd, COL_INFO))
307     col_add_fstr(fd, COL_INFO, "Op: %s  Count: %d",
308       val_to_str(op, nbp_op_vals, "unknown (%1x)"), count);
309   
310   if (tree) {
311     ti = proto_tree_add_item(tree, proto_nbp, NullTVB, offset, END_OF_FRAME, NULL);
312     nbp_tree = proto_item_add_subtree(ti, ett_nbp);
313
314     info_item = proto_tree_add_uint_format(nbp_tree, hf_nbp_info, NullTVB, offset, 1,
315                 pd[offset], 
316                 "Info: 0x%01X  Operation: %s  Count: %d", pd[offset],
317                 val_to_str(op, nbp_op_vals, "unknown"),
318                 count);
319         nbp_info_tree = proto_item_add_subtree(info_item, ett_nbp_info);
320     proto_tree_add_item(nbp_info_tree, hf_nbp_op, NullTVB, offset, 1, pd[offset]);
321     proto_tree_add_item(nbp_info_tree, hf_nbp_count, NullTVB, offset, 1, pd[offset]);
322     proto_tree_add_item(nbp_tree, hf_nbp_tid, NullTVB, offset+1, 1, pd[offset+1]);
323         offset += 2;
324
325     for (i=0; i<count; i++) {
326                 struct atalk_ddp_addr addr;
327                 proto_tree *node_item,*node_tree;
328                 int soffset = offset;
329
330                 if ( !BYTES_ARE_IN_FRAME(offset, 6) ) {
331                         dissect_data(pd,offset,fd,nbp_tree);
332                         return;
333                 }
334
335                 node_item = proto_tree_add_text(nbp_tree, NullTVB, offset, 4, 
336                         "Node %d", i+1);
337                 node_tree = proto_item_add_subtree(node_item, ett_nbp_node);
338
339                 addr.net = pntohs(&pd[offset]);
340                 addr.node = pd[offset+2];
341                 addr.port = pd[offset+3];
342
343                 /* note, this is probably wrong, I need to look at my info at work
344                         tomorrow to straighten it out */
345
346                 proto_tree_add_item(node_tree, hf_nbp_node_net, NullTVB, offset, 2, addr.net);
347                 offset += 2;
348                 proto_tree_add_item(node_tree, hf_nbp_node_node, NullTVB, offset, 1, addr.node);
349                 offset++;
350                 proto_tree_add_item(node_tree, hf_nbp_node_port, NullTVB, offset, 1, addr.port);
351                 offset++;
352                 proto_tree_add_item(node_tree, hf_nbp_node_enum, NullTVB, offset, 1, pd[offset]);
353                 offset++;
354
355                 offset = dissect_pascal_string(pd,offset,fd,node_tree,hf_nbp_node_object);
356                 offset = dissect_pascal_string(pd,offset,fd,node_tree,hf_nbp_node_type);
357                 offset = dissect_pascal_string(pd,offset,fd,node_tree,hf_nbp_node_zone);
358
359                 proto_item_set_len(node_item, offset-soffset);
360         }
361   }
362
363   return;
364 }
365
366 void
367 dissect_ddp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
368   e_ddp       ddp;
369   proto_tree *ddp_tree;
370   proto_item *ti;
371   static struct atalk_ddp_addr src, dst;
372
373   if (!BYTES_ARE_IN_FRAME(offset, DDP_HEADER_SIZE)) {
374     dissect_data(pd, offset, fd, tree);
375     return;
376   }
377
378   memcpy(&ddp, &pd[offset], sizeof(e_ddp));
379   ddp.dnet=ntohs(ddp.dnet);
380   ddp.snet=ntohs(ddp.snet);
381   ddp.sum=ntohs(ddp.sum);
382   
383   src.net = ddp.snet;
384   src.node = ddp.snode;
385   src.port = ddp.sport;
386   dst.net = ddp.dnet;
387   dst.node = ddp.dnode;
388   dst.port = ddp.dport;
389   SET_ADDRESS(&pi.net_src, AT_ATALK, sizeof src, (guint8 *)&src);
390   SET_ADDRESS(&pi.src, AT_ATALK, sizeof src, (guint8 *)&src);
391   SET_ADDRESS(&pi.net_dst, AT_ATALK, sizeof dst, (guint8 *)&dst);
392   SET_ADDRESS(&pi.dst, AT_ATALK, sizeof dst, (guint8 *)&dst);
393
394   if (check_col(fd, COL_PROTOCOL))
395     col_add_str(fd, COL_PROTOCOL, "DDP");
396   if (check_col(fd, COL_INFO))
397     col_add_str(fd, COL_INFO,
398       val_to_str(ddp.type, op_vals, "Unknown DDP protocol (%02x)"));
399   
400   if (tree) {
401     ti = proto_tree_add_item(tree, proto_ddp, NullTVB, offset, DDP_HEADER_SIZE, NULL);
402     ddp_tree = proto_item_add_subtree(ti, ett_ddp);
403     proto_tree_add_item(ddp_tree, hf_ddp_hopcount, NullTVB, offset,      1, 
404                         ddp_hops(ddp.hops_len));
405     proto_tree_add_item(ddp_tree, hf_ddp_len, NullTVB, offset,      2, 
406                         ddp_len(ddp.hops_len));
407     proto_tree_add_item(ddp_tree, hf_ddp_checksum, NullTVB, offset + 2,  2, ddp.sum);
408     proto_tree_add_item(ddp_tree, hf_ddp_dst_net, NullTVB, offset + 4,  2, ddp.dnet);
409     proto_tree_add_item(ddp_tree, hf_ddp_src_net, NullTVB,  offset + 6,  2, ddp.snet);
410     proto_tree_add_item(ddp_tree, hf_ddp_dst_node, NullTVB, offset + 8,  1, ddp.dnode);
411     proto_tree_add_item(ddp_tree, hf_ddp_src_node, NullTVB, offset + 9,  1, ddp.snode);
412     proto_tree_add_item(ddp_tree, hf_ddp_dst_socket, NullTVB, offset + 10, 1, ddp.dport);
413     proto_tree_add_item(ddp_tree, hf_ddp_src_socket, NullTVB, offset + 11, 1, ddp.sport);
414     proto_tree_add_item(ddp_tree, hf_ddp_type, NullTVB, offset + 12, 1, ddp.type);  
415   }
416
417   offset += DDP_HEADER_SIZE;
418
419   if (!dissector_try_port(ddp_dissector_table, ddp.type, pd, offset, fd, tree))
420     dissect_data(pd, offset, fd, tree);
421 }
422
423 void
424 proto_register_atalk(void)
425 {
426   static hf_register_info hf_ddp[] = {
427     { &hf_ddp_hopcount,
428       { "Hop count",            "ddp.hopcount", FT_UINT8,  BASE_DEC, NULL, 0x0,
429         "" }},
430
431     { &hf_ddp_len,
432       { "Datagram length",      "ddp.len",      FT_UINT16, BASE_DEC, NULL, 0x0,
433         "" }},
434
435     { &hf_ddp_checksum,
436       { "Checksum",             "ddp.checksum", FT_UINT16, BASE_DEC, NULL, 0x0,
437         "" }},
438
439     { &hf_ddp_dst_net,
440       { "Destination Net",      "ddp.dst.net",  FT_UINT16, BASE_DEC, NULL, 0x0,
441         "" }},
442
443     { &hf_ddp_src_net,
444       { "Source Net",           "ddp.src.net",  FT_UINT16, BASE_DEC, NULL, 0x0,
445         "" }},
446
447     { &hf_ddp_dst_node,
448       { "Destination Node",     "ddp.dst.node", FT_UINT8,  BASE_DEC, NULL, 0x0,
449         "" }},
450
451     { &hf_ddp_src_node,
452       { "Source Node",          "ddp.src.node", FT_UINT8,  BASE_DEC, NULL, 0x0,
453         "" }},
454
455     { &hf_ddp_dst_socket,
456       { "Destination Socket",   "ddp.dst.socket", FT_UINT8,  BASE_DEC, NULL, 0x0,
457         "" }},
458
459     { &hf_ddp_src_socket,
460       { "Source Socket",        "ddp.src.socket", FT_UINT8,  BASE_DEC, NULL, 0x0,
461         "" }},
462
463     { &hf_ddp_type,
464       { "Protocol type",        "ddp.type",     FT_UINT8,  BASE_DEC, VALS(op_vals), 0x0,
465         "" }},
466   };
467
468   static hf_register_info hf_nbp[] = {
469     { &hf_nbp_op,
470       { "Operation",            "nbp.op",       FT_UINT8,  BASE_DEC, 
471                 VALS(nbp_op_vals), 0xF0, "Operation" }},
472     { &hf_nbp_info,
473       { "Info",         "nbp.info",     FT_UINT8,  BASE_HEX, 
474                 NULL, 0x0, "Info" }},
475     { &hf_nbp_count,
476       { "Count",                "nbp.count",    FT_UINT8,  BASE_DEC, 
477                 NULL, 0x0F, "Count" }},
478     { &hf_nbp_node_net,
479       { "Network",              "nbp.net",      FT_UINT16,  BASE_DEC, 
480                 NULL, 0x0, "Network" }},
481     { &hf_nbp_node_node,
482       { "Node",         "nbp.node",     FT_UINT8,  BASE_DEC, 
483                 NULL, 0x0, "Node" }},
484     { &hf_nbp_node_port,
485       { "Port",         "nbp.port",     FT_UINT8,  BASE_DEC, 
486                 NULL, 0x0, "Port" }},
487     { &hf_nbp_node_enum,
488       { "Enumerator",           "nbp.enum",     FT_UINT8,  BASE_DEC, 
489                 NULL, 0x0, "Enumerator" }},
490     { &hf_nbp_node_object,
491       { "Object",               "nbp.object",   FT_STRING,  BASE_DEC, 
492                 NULL, 0x0, "Object" }},
493     { &hf_nbp_node_type,
494       { "Type",         "nbp.type",     FT_STRING,  BASE_DEC, 
495                 NULL, 0x0, "Type" }},
496     { &hf_nbp_node_zone,
497       { "Zone",         "nbp.zone",     FT_STRING,  BASE_DEC, 
498                 NULL, 0x0, "Zone" }},
499     { &hf_nbp_tid,
500       { "Transaction ID",               "nbp.tid",      FT_UINT8,  BASE_DEC, 
501                 NULL, 0x0, "Transaction ID" }}
502   };
503
504   static hf_register_info hf_rtmp[] = {
505     { &hf_rtmp_net,
506       { "Net",          "rtmp.net",     FT_UINT16,  BASE_DEC, 
507                 NULL, 0x0, "Net" }},
508     { &hf_rtmp_node,
509       { "Node",         "nbp.nodeid",   FT_UINT8,  BASE_DEC, 
510                 NULL, 0x0, "Node" }},
511     { &hf_rtmp_node_len,
512       { "Node Length",          "nbp.nodeid.length",    FT_UINT8,  BASE_DEC, 
513                 NULL, 0x0, "Node Length" }},
514     { &hf_rtmp_tuple_net,
515       { "Net",          "rtmp.tuple.net",       FT_UINT16,  BASE_DEC, 
516                 NULL, 0x0, "Net" }},
517     { &hf_rtmp_tuple_dist,
518       { "Distance",             "rtmp.tuple.dist",      FT_UINT16,  BASE_DEC, 
519                 NULL, 0x0, "Distance" }}
520   };
521
522
523   static gint *ett[] = {
524     &ett_ddp,
525         &ett_nbp,
526         &ett_nbp_info,
527         &ett_nbp_node,
528         &ett_pstring,
529         &ett_rtmp,
530         &ett_rtmp_tuple
531   };
532
533   proto_ddp = proto_register_protocol("Datagram Delivery Protocol", "ddp");
534   proto_register_field_array(proto_ddp, hf_ddp, array_length(hf_ddp));
535
536   proto_nbp = proto_register_protocol("Name Binding Protocol", "nbp");
537   proto_register_field_array(proto_nbp, hf_nbp, array_length(hf_nbp));
538
539   proto_rtmp = proto_register_protocol("Routing Table", "rtmp");
540   proto_register_field_array(proto_rtmp, hf_rtmp, array_length(hf_rtmp));
541
542   proto_register_subtree_array(ett, array_length(ett));
543
544   /* subdissector code */
545   ddp_dissector_table = register_dissector_table("ddp.type");
546 }
547
548 void
549 proto_reg_handoff_atalk(void)
550 {
551   dissector_add("ethertype", ETHERTYPE_ATALK, dissect_ddp);
552   dissector_add("ppp.protocol", PPP_AT, dissect_ddp);
553   dissector_add("ddp.type", DDP_NBP, dissect_nbp);
554   dissector_add("ddp.type", DDP_RTMPREQ, dissect_rtmp_request);
555   dissector_add("ddp.type", DDP_RTMPDATA, dissect_rtmp_data);
556 }