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