06330c3b0f4fccd800ccc0f14352169c4f7eeee7
[obnox/wireshark/wip.git] / packet-ipx.c
1 /* packet-ipx.c
2  * Routines for NetWare's IPX
3  * Gilbert Ramirez <gram@xiexie.org>
4  *
5  * $Id: packet-ipx.c,v 1.59 2000/05/22 18:09:36 guy 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 <stdio.h>
36 #include <glib.h>
37 #include "etypes.h"
38 #include "ppptypes.h"
39 #include "llcsaps.h"
40 #include "packet.h"
41 #include "packet-ipx.h"
42 #include "packet-nbipx.h"
43 #include "packet-ncp.h"
44 #include "packet-eigrp.h"
45 #include "resolv.h"
46
47 #include "packet-snmp.h"
48
49 /* The information in this module (IPX, SPX, NCP) comes from:
50         NetWare LAN Analysis, Second Edition
51         Laura A. Chappell and Dan E. Hakes
52         (c) 1994 Novell, Inc.
53         Novell Press, San Jose.
54         ISBN: 0-7821-1362-1
55
56   And from the ncpfs source code by Volker Lendecke
57
58 */
59         
60 static int proto_ipx = -1;
61 static int hf_ipx_checksum = -1;
62 static int hf_ipx_len = -1;
63 static int hf_ipx_hops = -1;
64 static int hf_ipx_packet_type = -1;
65 static int hf_ipx_dnet = -1;
66 static int hf_ipx_dnode = -1;
67 static int hf_ipx_dsocket = -1;
68 static int hf_ipx_snet = -1;
69 static int hf_ipx_snode = -1;
70 static int hf_ipx_ssocket = -1;
71
72 static gint ett_ipx = -1;
73
74 static int proto_spx = -1;
75 static int hf_spx_connection_control = -1;
76 static int hf_spx_datastream_type = -1;
77 static int hf_spx_src_id = -1;
78 static int hf_spx_dst_id = -1;
79 static int hf_spx_seq_nr = -1;
80 static int hf_spx_ack_nr = -1;
81 static int hf_spx_all_nr = -1;
82
83 static gint ett_spx = -1;
84
85 static int proto_ipxrip = -1;
86 static int hf_ipxrip_request = -1;
87 static int hf_ipxrip_response = -1;
88
89 static gint ett_ipxrip = -1;
90
91 static int proto_sap = -1;
92 static int hf_sap_request = -1;
93 static int hf_sap_response = -1;
94
95 static gint ett_ipxsap = -1;
96 static gint ett_ipxsap_server = -1;
97
98 static gint ett_ipxmsg = -1;
99 static int proto_ipxmsg = -1;
100 static int hf_msg_conn = -1;
101 static int hf_msg_sigchar = -1;
102
103 static void
104 dissect_spx(const u_char *pd, int offset, frame_data *fd, proto_tree *tree);
105
106 static void
107 dissect_ipxrip(const u_char *pd, int offset, frame_data *fd, proto_tree *tree);
108
109 static void
110 dissect_ipxsap(const u_char *pd, int offset, frame_data *fd, proto_tree *tree);
111
112 static void
113 dissect_ipxmsg(const u_char *pd, int offset, frame_data *fd, proto_tree *tree);
114
115 static void
116 dissect_eigrp_ipx(const u_char *pd, int offset, frame_data *fd, proto_tree *tree);
117
118 typedef void    (dissect_func_t)(const u_char *, int, frame_data *, proto_tree *);
119
120 #define UDP_PORT_IPX    213             /* RFC 1234 */
121
122 struct port_info {
123         guint16 port;
124         dissect_func_t *func;
125         char    *text;
126 };
127
128 struct conn_info {
129         guint8  ctrl;
130         char    *text;
131 };
132
133 struct server_info {
134         guint16 type;
135         char    *text;
136 };
137
138 /* ================================================================= */
139 /* IPX                                                               */
140 /* ================================================================= */
141
142 /* info on these sockets can be found in this listing from Novell:
143
144         http://developer.novell.com/engsup/sample/tids/dsoc1b/dsoc1b.htm
145 */
146
147 #define IPX_SOCKET_PING_CISCO           0x0002 /* In cisco this is set with: ipx ping-default cisco */
148 #define IPX_SOCKET_NCP                  0x0451
149 #define IPX_SOCKET_SAP                  0x0452
150 #define IPX_SOCKET_IPXRIP               0x0453
151 #define IPX_SOCKET_NETBIOS              0x0455
152 #define IPX_SOCKET_DIAGNOSTIC           0x0456
153 #define IPX_SOCKET_SERIALIZATION        0x0457
154 #define IPX_SOCKET_NWLINK_SMB_NAMEQUERY 0x0551
155 #define IPX_SOCKET_NWLINK_SMB_DGRAM     0x0553
156 #define IPX_SOCKET_NWLINK_SMB_BROWSE    0x0555 /* ? not sure on this
157         but I guessed based on the content of the packet I saw */
158 #define IPX_SOCKET_ATTACHMATE_GW        0x055d
159 #define IPX_SOCKET_IPX_MESSAGE          0x4001
160 #define IPX_SOCKET_ADSM                 0x8522 /* www.tivoli.com */
161 #define IPX_SOCKET_EIGRP                0x85be /* cisco ipx eigrp */
162 #define IPX_SOCKET_WIDE_AREA_ROUTER     0x9001
163 #define IPX_SOCKET_SNMP_AGENT           0x900F /* RFC 1906 */
164 #define IPX_SOCKET_SNMP_SINK            0x9010 /* RFC 1906 */
165 #define IPX_SOCKET_PING_NOVELL          0x9086 /* In cisco this is set with: ipx ping-default novell */
166 #define IPX_SOCKET_TCP_TUNNEL           0x9091 /* RFC 1791 */
167 #define IPX_SOCKET_UDP_TUNNEL           0x9092 /* RFC 1791 */
168
169 static struct port_info ports[] = {
170
171         { IPX_SOCKET_PING_CISCO, NULL, "CISCO PING" },
172         { IPX_SOCKET_NCP,                       dissect_ncp,
173                                 "NCP" },
174         { IPX_SOCKET_SAP,                       dissect_ipxsap,
175                                 "SAP" },
176         { IPX_SOCKET_IPXRIP,                    dissect_ipxrip,
177                                 "RIP" },
178         { IPX_SOCKET_NETBIOS,                   NULL,
179                                 "NetBIOS" },
180         { IPX_SOCKET_DIAGNOSTIC,                NULL,
181                                 "Diagnostic" },
182         { IPX_SOCKET_SERIALIZATION,             NULL,
183                                 "Serialization" },
184         { IPX_SOCKET_NWLINK_SMB_NAMEQUERY,      NULL,
185                                 "NWLink SMB Name Query" },
186         { IPX_SOCKET_NWLINK_SMB_DGRAM,          dissect_nwlink_dg,
187                 "NWLink SMB Datagram" },
188         { IPX_SOCKET_NWLINK_SMB_BROWSE, NULL, "NWLink SMB Browse" },
189         { IPX_SOCKET_ATTACHMATE_GW,             NULL, "Attachmate Gateway" },
190         { IPX_SOCKET_IPX_MESSAGE, dissect_ipxmsg, "IPX Message" },
191         { IPX_SOCKET_SNMP_AGENT, dissect_snmp, "SNMP Agent" },
192         { IPX_SOCKET_SNMP_SINK, dissect_snmp, "SNMP Sink" },
193         { IPX_SOCKET_PING_NOVELL, NULL, "NOVELL PING" },
194         { IPX_SOCKET_UDP_TUNNEL, NULL, "UDP Tunnel" },
195         { IPX_SOCKET_TCP_TUNNEL, NULL, "TCP Tunnel" },
196         { IPX_SOCKET_TCP_TUNNEL, NULL, "TCP Tunnel" },
197         { IPX_SOCKET_ADSM, NULL, "ADSM" },
198         { IPX_SOCKET_EIGRP, dissect_eigrp_ipx, "Cisco EIGRP for IPX" },
199         { IPX_SOCKET_WIDE_AREA_ROUTER, NULL, "Wide Area Router" },
200         { 0x0000,                               NULL,
201                                 NULL }
202 };
203
204 static char*
205 port_text(guint16 port) {
206         int i=0;
207
208         while (ports[i].text != NULL) {
209                 if (ports[i].port == port) {
210                         return ports[i].text;
211                 }
212                 i++;
213         }
214         return "Unknown";
215 }
216
217 static dissect_func_t*
218 port_func(guint16 port) {
219         int i=0;
220
221         while (ports[i].text != NULL) {
222                 if (ports[i].port == port) {
223                         return ports[i].func;
224                 }
225                 i++;
226         }
227         return NULL;
228 }
229
230 /*
231  * From:
232  *
233  *      http://alr.base2co.com:457/netguide/dipxD.ipx_packet_struct.html
234  *
235  * which is part of SCO's "Network Programmer's Guide and Reference".
236  *
237  * It calls type 20 "NetBIOS name packet".  Microsoft Network Monitor
238  * calls it "WAN Broadcast"; it's also used for SMB browser announcements,
239  * i.e. NetBIOS (broadcast) datagrams.
240  */
241 #define IPX_PACKET_TYPE_IPX             0
242 #define IPX_PACKET_TYPE_RIP             1
243 #define IPX_PACKET_TYPE_ECHO            2
244 #define IPX_PACKET_TYPE_ERROR           3
245 #define IPX_PACKET_TYPE_PEP             4
246 #define IPX_PACKET_TYPE_SPX             5
247 #define IPX_PACKET_TYPE_NCP             17
248 #define IPX_PACKET_TYPE_WANBCAST        20      /* propagated NetBIOS packet? */
249
250 static const value_string ipx_packet_type_vals[] = {
251         { IPX_PACKET_TYPE_IPX,          "IPX" },
252         { IPX_PACKET_TYPE_RIP,          "RIP" },
253         { IPX_PACKET_TYPE_ECHO,         "Echo" },
254         { IPX_PACKET_TYPE_ERROR,        "Error" },
255         { IPX_PACKET_TYPE_PEP,          "PEP" }, /* Packet Exchange Packet */
256         { IPX_PACKET_TYPE_SPX,          "SPX" },
257         { 16,                           "Experimental Protocol" },
258         { IPX_PACKET_TYPE_NCP,          "NCP" },
259         { 18,                           "Experimental Protocol" },
260         { 19,                           "Experimental Protocol" },
261         { IPX_PACKET_TYPE_WANBCAST,     "NetBIOS Broadcast" },
262         { 21,                           "Experimental Protocol" },
263         { 22,                           "Experimental Protocol" },
264         { 23,                           "Experimental Protocol" },
265         { 24,                           "Experimental Protocol" },
266         { 25,                           "Experimental Protocol" },
267         { 26,                           "Experimental Protocol" },
268         { 27,                           "Experimental Protocol" },
269         { 28,                           "Experimental Protocol" },
270         { 29,                           "Experimental Protocol" },
271         { 30,                           "Experimental Protocol" },
272         { 31,                           "Experimental Protocol" },
273         { 0,                            NULL }
274 };
275
276 static const value_string ipxmsg_sigchar_vals[] = {
277         { '?', "Poll inactive station" },
278         { 0, NULL }
279 };
280
281 gchar*
282 ipxnet_to_string(const guint8 *ad)
283 {
284         guint32 addr = pntohl(ad);
285         return ipxnet_to_str_punct(addr, ' ');
286 }
287
288 /* We use a different representation of hardware addresses
289  * than ether_to_str(); we don't put punctuation between the hex
290  * digits.
291  */
292
293 gchar*
294 ipx_addr_to_str(guint32 net, const guint8 *ad)
295 {
296         static gchar    str[3][8+1+MAXNAMELEN+1]; /* 8 digits, 1 period, NAME, 1 null */
297         static gchar    *cur;
298         char            *name;
299
300         if (cur == &str[0][0]) {
301                 cur = &str[1][0];
302         } else if (cur == &str[1][0]) {
303                 cur = &str[2][0];
304         } else {
305                 cur = &str[0][0];
306         }
307
308         name = get_ether_name_if_known(ad);
309
310         if (name) {
311                 sprintf(cur, "%s.%s", get_ipxnet_name(net), name);
312         }
313         else {
314                 sprintf(cur, "%s.%s", get_ipxnet_name(net), ether_to_str_punct(ad, '\0'));
315         }
316         return cur;
317 }
318
319 gchar *
320 ipxnet_to_str_punct(const guint32 ad, char punct) {
321   static gchar  str[3][12];
322   static gchar *cur;
323   gchar        *p;
324   int          i;
325   guint32      octet;
326   static const gchar hex_digits[16] = "0123456789ABCDEF";
327   static const guint32  octet_mask[4] =
328           { 0xff000000 , 0x00ff0000, 0x0000ff00, 0x000000ff };
329
330   if (cur == &str[0][0]) {
331     cur = &str[1][0];
332   } else if (cur == &str[1][0]) {  
333     cur = &str[2][0];
334   } else {  
335     cur = &str[0][0];
336   }
337   p = &cur[12];
338   *--p = '\0';
339   i = 3;
340   for (;;) {
341     octet = (ad & octet_mask[i]) >> ((3 - i) * 8);
342     *--p = hex_digits[octet&0xF];
343     octet >>= 4;
344     *--p = hex_digits[octet&0xF];
345     if (i == 0)
346       break;
347     if (punct)
348       *--p = punct;
349     i--;
350   }
351   return p;
352 }
353
354 void
355 capture_ipx(const u_char *pd, int offset, packet_counts *ld)
356 {
357         ld->ipx++;
358 }
359
360 void
361 dissect_ipx(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
362
363         proto_tree      *ipx_tree;
364         proto_item      *ti;
365         guint8          ipx_type, ipx_hops;
366         guint16         ipx_checksum, ipx_length;
367         int             len;
368         guint8          *ipx_snode, *ipx_dnode, *ipx_snet, *ipx_dnet;
369
370         guint16         ipx_dsocket, ipx_ssocket;
371         dissect_func_t  *dissect;
372         guint32         ipx_dnet_val, ipx_snet_val;
373
374         /* Calculate here for use in pinfo and in tree */
375         ipx_dnet = (guint8*)&pd[offset+6];
376         ipx_snet = (guint8*)&pd[offset+18];
377         ipx_dnet_val = pntohl(ipx_dnet);
378         ipx_snet_val = pntohl(ipx_snet);
379         ipx_dsocket = pntohs(&pd[offset+16]);
380         ipx_ssocket = pntohs(&pd[offset+28]);
381         ipx_dnode = (guint8*)&pd[offset+10];
382         ipx_snode = (guint8*)&pd[offset+22];
383         ipx_type = pd[offset+5];
384         ipx_length = pntohs(&pd[offset+2]);
385
386         /* Length of IPX datagram plus headers above it. */
387         len = ipx_length + offset;
388
389         /* Set the payload and captured-payload lengths to the minima of
390            (the IPX length plus the length of the headers above it) and
391            the frame lengths. */
392         if (pi.len > len)
393                 pi.len = len;
394         if (pi.captured_len > len)
395                 pi.captured_len = len;
396
397         SET_ADDRESS(&pi.net_src, AT_IPX, 10, &pd[offset+18]);
398         SET_ADDRESS(&pi.src, AT_IPX, 10, &pd[offset+18]);
399         SET_ADDRESS(&pi.net_dst, AT_IPX, 10, &pd[offset+6]);
400         SET_ADDRESS(&pi.dst, AT_IPX, 10, &pd[offset+6]);
401
402         if (check_col(fd, COL_PROTOCOL))
403                 col_add_str(fd, COL_PROTOCOL, "IPX");
404         if (check_col(fd, COL_INFO))
405                 col_add_fstr(fd, COL_INFO, "%s (0x%04X)", port_text(ipx_dsocket),
406                                 ipx_dsocket);
407
408         if (tree) {
409                 ipx_checksum = pntohs(&pd[offset]);
410                 ipx_hops = pd[offset+4];
411
412                 ti = proto_tree_add_item(tree, proto_ipx, NullTVB, offset, 30, NULL);
413                 ipx_tree = proto_item_add_subtree(ti, ett_ipx);
414                 proto_tree_add_item(ipx_tree, hf_ipx_checksum, NullTVB, offset, 2, ipx_checksum);
415                 proto_tree_add_uint_format(ipx_tree, hf_ipx_len, NullTVB, offset+2, 2, ipx_length,
416                         "Length: %d bytes", ipx_length);
417                 proto_tree_add_uint_format(ipx_tree, hf_ipx_hops, NullTVB, offset+4, 1, ipx_hops,
418                         "Transport Control: %d hops", ipx_hops);
419                 proto_tree_add_item(ipx_tree, hf_ipx_packet_type, NullTVB, offset+5, 1, ipx_type);
420                 proto_tree_add_item(ipx_tree, hf_ipx_dnet, NullTVB, offset+6, 4, ipx_dnet_val);
421                 proto_tree_add_item(ipx_tree, hf_ipx_dnode, NullTVB, offset+10, 6, ipx_dnode);
422                 proto_tree_add_uint_format(ipx_tree, hf_ipx_dsocket, NullTVB, offset+16, 2,
423                         ipx_dsocket, "Destination Socket: %s (0x%04X)",
424                         port_text(ipx_dsocket), ipx_dsocket);
425                 proto_tree_add_item(ipx_tree, hf_ipx_snet, NullTVB, offset+18, 4, ipx_snet_val);
426                 proto_tree_add_item(ipx_tree, hf_ipx_snode, NullTVB, offset+22, 6, ipx_snode);
427                 proto_tree_add_uint_format(ipx_tree, hf_ipx_ssocket, NullTVB, offset+28, 2,
428                         ipx_ssocket, "Source Socket: %s (0x%04X)", port_text(ipx_ssocket),
429                         ipx_ssocket);
430         }
431         offset += 30;
432
433         switch (ipx_type) {
434                 case IPX_PACKET_TYPE_SPX:
435                         dissect_spx(pd, offset, fd, tree);
436                         break;
437
438                 case IPX_PACKET_TYPE_NCP:
439                         dissect_ncp(pd, offset, fd, tree);
440                         break;
441
442                 case IPX_PACKET_TYPE_WANBCAST:
443                 case IPX_PACKET_TYPE_PEP:
444                         if (ipx_dsocket == IPX_SOCKET_NETBIOS) {
445                                 dissect_nbipx(pd, offset, fd, tree);
446                                 break;
447                         }
448                         /* else fall through */
449
450                 case 0: /* IPX, fall through to default */
451                         /* XXX - should type 0's be dissected as NBIPX
452                            if they're aimed at the NetBIOS socket? */
453                 default:
454                         dissect = port_func(ipx_dsocket);
455                         if (dissect) {
456                                 dissect(pd, offset, fd, tree);
457                         }
458                         else {
459                                 dissect = port_func(ipx_ssocket);
460                                 if (dissect) {
461                                         dissect(pd, offset, fd, tree);
462                                 }
463                                 else {
464                                         dissect_data(pd, offset, fd, tree);
465                                 }
466                         }
467                         break;
468         }
469 }
470
471
472 /* ================================================================= */
473 /* SPX                                                               */
474 /* ================================================================= */
475 static char*
476 spx_conn_ctrl(u_char ctrl)
477 {
478         int i=0;
479
480         static struct conn_info conns[] = {
481                 { 0x10, "End-of-Message" },
482                 { 0x20, "Attention" },
483                 { 0x40, "Acknowledgment Required"},
484                 { 0x80, "System Packet"},
485                 { 0x00, NULL }
486         };
487
488         while (conns[i].text != NULL) {
489                 if (conns[i].ctrl == ctrl) {
490                         return conns[i].text;
491                 }
492                 i++;
493         }
494         return "Unknown";
495 }
496
497 static char*
498 spx_datastream(u_char type)
499 {
500         switch (type) {
501                 case 0xfe:
502                         return "End-of-Connection";
503                 case 0xff:
504                         return "End-of-Connection Acknowledgment";
505                 default:
506                         return "Client-Defined";
507         }
508 }
509
510 static void
511 dissect_spx(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
512         proto_tree      *spx_tree;
513         proto_item      *ti;
514
515         if (check_col(fd, COL_PROTOCOL))
516                 col_add_str(fd, COL_PROTOCOL, "SPX");
517         if (check_col(fd, COL_INFO))
518                 col_add_str(fd, COL_INFO, "SPX");
519
520         if (tree) {
521                 ti = proto_tree_add_item(tree, proto_spx, NullTVB, offset, 12, NULL);
522                 spx_tree = proto_item_add_subtree(ti, ett_spx);
523
524                 proto_tree_add_uint_format(spx_tree, hf_spx_connection_control, NullTVB,
525                                            offset,      1,
526                                            pd[offset],
527                                            "Connection Control: %s (0x%02X)",
528                                            spx_conn_ctrl(pd[offset]), 
529                                            pd[offset]);
530
531                 proto_tree_add_uint_format(spx_tree, hf_spx_datastream_type, NullTVB,
532                                            offset+1,     1,
533                                            pd[offset+1],
534                                            "Datastream Type: %s (0x%02X)",
535                                            spx_datastream(pd[offset+1]), 
536                                            pd[offset+1]);
537
538                 proto_tree_add_item(spx_tree, hf_spx_src_id, NullTVB, 
539                                     offset+2,     2,
540                                     pntohs( &pd[offset+2] ));
541
542                 proto_tree_add_item(spx_tree, hf_spx_dst_id, NullTVB,
543                                     offset+4,     2,
544                                     pntohs( &pd[offset+4] ));
545
546                 proto_tree_add_item(spx_tree, hf_spx_seq_nr, NullTVB, 
547                                     offset+6,     2,
548                                     pntohs( &pd[offset+6] ) );
549
550                 proto_tree_add_item(spx_tree, hf_spx_ack_nr, NullTVB,
551                                     offset+8,     2,
552                                     pntohs( &pd[offset+8] ) );
553
554                 proto_tree_add_item(spx_tree, hf_spx_all_nr, NullTVB,
555                                     offset+10,     2,
556                                     pntohs( &pd[offset+10] ) );
557
558                 offset += 12;
559                 dissect_data(pd, offset, fd, tree);
560         }
561 }
562
563 /* ================================================================= */
564 /* IPX Message                                                       */
565 /* ================================================================= */
566 static void
567 dissect_ipxmsg(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
568         proto_tree      *msg_tree;
569         proto_item      *ti;
570         u_char conn_number, sig_char;
571
572         if ( ! BYTES_ARE_IN_FRAME(offset,2) )
573         {
574                 return;
575         }
576
577         conn_number = pd[offset];
578         sig_char = pd[offset+1];
579
580         if (check_col(fd, COL_PROTOCOL))
581          col_add_str(fd, COL_PROTOCOL, "IPX MSG");
582         if (check_col(fd, COL_PROTOCOL)) {
583                 col_add_fstr(fd, COL_INFO, 
584                         "%s, Connection %d", 
585                         val_to_str(sig_char, ipxmsg_sigchar_vals, "Unknown Signature Char"), conn_number);
586         }
587
588         if (tree) {
589                 ti = proto_tree_add_item(tree, proto_ipxmsg, NullTVB, offset, END_OF_FRAME, NULL);
590                 msg_tree = proto_item_add_subtree(ti, ett_ipxmsg);
591
592                 proto_tree_add_item(msg_tree, hf_msg_conn, NullTVB, offset, 1, conn_number);
593                 proto_tree_add_item(msg_tree, hf_msg_sigchar, NullTVB, offset+1, 1, sig_char);
594         }
595 }
596
597
598 /* ================================================================= */
599 /* IPX RIP                                                           */
600 /* ================================================================= */
601 static void
602 dissect_ipxrip(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
603         proto_tree      *rip_tree;
604         proto_item      *ti;
605         guint16         operation;
606         struct ipx_rt_def route;
607         int             cursor;
608
609         char            *rip_type[2] = { "Request", "Response" };
610
611         operation = pntohs(&pd[offset]) - 1;
612
613         if (check_col(fd, COL_PROTOCOL))
614          col_add_str(fd, COL_PROTOCOL, "IPX RIP");
615         if (check_col(fd, COL_PROTOCOL)) {
616          if (operation < 2) {
617                  col_add_str(fd, COL_INFO, rip_type[operation]);
618          }
619          else {
620                  col_add_str(fd, COL_INFO, "Unknown Packet Type");
621          }
622         }
623
624         if (tree) {
625                 ti = proto_tree_add_item(tree, proto_ipxrip, NullTVB, offset, END_OF_FRAME, NULL);
626                 rip_tree = proto_item_add_subtree(ti, ett_ipxrip);
627
628                 if (operation < 2) {
629                         proto_tree_add_text(rip_tree, NullTVB, offset, 2,
630                         "RIP packet type: %s", rip_type[operation]);
631
632                         if (operation == 0) {
633                           proto_tree_add_item_hidden(rip_tree, 
634                                                      hf_ipxrip_request, 
635                                                      NullTVB, offset, 2, 1);
636                         } else {
637                           proto_tree_add_item_hidden(rip_tree, 
638                                                      hf_ipxrip_response, 
639                                                      NullTVB, offset, 2, 1);
640                         }
641
642                 }
643                 else {
644                         proto_tree_add_text(rip_tree, NullTVB, offset, 2, "Unknown RIP packet type");
645                 }
646
647                 for (cursor = offset + 2; cursor < pi.captured_len; cursor += 8) {
648                         memcpy(&route.network, &pd[cursor], 4);
649                         route.hops = pntohs(&pd[cursor+4]);
650                         route.ticks = pntohs(&pd[cursor+6]);
651
652                         if (operation == IPX_RIP_REQUEST - 1) {
653                                 proto_tree_add_text(rip_tree, NullTVB, cursor,      8,
654                                         "Route Vector: %s, %d hop%s, %d tick%s",
655                                         ipxnet_to_string((guint8*)&route.network),
656                                         route.hops,  route.hops  == 1 ? "" : "s",
657                                         route.ticks, route.ticks == 1 ? "" : "s");
658                         }
659                         else {
660                                 proto_tree_add_text(rip_tree, NullTVB, cursor,      8,
661                                         "Route Vector: %s, %d hop%s, %d tick%s (%d ms)",
662                                         ipxnet_to_string((guint8*)&route.network),
663                                         route.hops,  route.hops  == 1 ? "" : "s",
664                                         route.ticks, route.ticks == 1 ? "" : "s",
665                                         route.ticks * 1000 / 18);
666                         }
667                 }
668         }
669 }
670
671
672
673 /* ================================================================= */
674 /* SAP                                                                   */
675 /* ================================================================= */
676 static char*
677 server_type(guint16 type)
678 {
679         int i=0;
680
681         /* some of these are from ncpfs, others are from the book */
682         static struct server_info       servers[] = {
683                 { 0x0001,       "User" },
684                 { 0x0002,       "User Group" },
685                 { 0x0003,       "Print Queue" },
686                 { 0x0004,       "File server" },
687                 { 0x0005,       "Job server" },
688                 { 0x0007,       "Print server" },
689                 { 0x0008,       "Archive server" },
690                 { 0x0009,       "Archive server" },
691                 { 0x000a,       "Job queue" },
692                 { 0x000b,       "Administration" },
693                 { 0x0021,       "NAS SNA gateway" },
694                 { 0x0024,       "Remote bridge" },
695                 { 0x0026,       "Bridge server" },
696                 { 0x0027,       "TCP/IP gateway" },
697                 { 0x002d,       "Time Synchronization VAP" },
698                 { 0x002e,       "Archive Server Dynamic SAP" },
699                 { 0x0047,       "Advertising print server" },
700                 { 0x004b,       "Btrieve VAP 5.0" },
701                 { 0x004c,       "SQL VAP" },
702                 { 0x0050,       "Btrieve VAP" },
703                 { 0x0053,       "Print Queue VAP" },
704                 { 0x007a,       "TES NetWare for VMS" },
705                 { 0x0098,       "NetWare access server" },
706                 { 0x009a,       "Named Pipes server" },
707                 { 0x009e,       "Portable NetWare Unix" },
708                 { 0x0107,       "NetWare 386" },
709                 { 0x0111,       "Test server" },
710                 { 0x0133,       "NetWare Name Service" },
711                 { 0x0166,       "NetWare management" },
712                 { 0x023f,       "SMS Testing and Development" },
713                 { 0x026a,       "NetWare management" },
714                 { 0x026b,       "Time synchronization" },
715                 { 0x027b,       "NetWare Management Agent" },
716                 { 0x0278,       "NetWare Directory server" },
717                 { 0x030c,       "HP LaserJet / Quick Silver" },
718                 { 0x0355,       "Arcada Software" },
719                 { 0x0361,       "NETINELO" },
720                 { 0x037e,       "Powerchute UPS Monitoring" },
721                 { 0x03e1,       "UnixWare Application Server" },
722                 { 0x044c,       "Archive" },
723                 { 0x055d,       "Attachmate SNA gateway" },
724                 { 0x0610,       "Adaptec SCSI Management" },
725                 { 0x0640,       "NT Server-RPC/GW for NW/Win95 User Level Sec" },
726                 { 0x064e,       "NT Server-IIS" },
727                 { 0x0810,       "ELAN License Server Demo" },
728                 { 0x8002,       "Intel NetPort Print Server" },
729
730 /* For any unidentified ones, I found a really big list of them at: */
731 /*    http://www.inpnet.org/cnpweb/saplist.txt */
732 /*    http://www.isi.edu/in-notes/iana/assignments/novell-sap-numbers */
733
734                 { 0x0000,       NULL }
735         };
736
737         while (servers[i].text != NULL) {
738                 if (servers[i].type == type) {
739                         return servers[i].text;
740                 }
741                 i++;
742         }
743         return "Unknown";
744 }
745
746 static void
747 dissect_ipxsap(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
748
749         proto_tree      *sap_tree, *s_tree;
750         proto_item      *ti;
751         int             cursor;
752         struct sap_query query;
753         struct sap_server_ident server;
754
755         char            *sap_type[4] = { "General Query", "General Response",
756                 "Nearest Query", "Nearest Response" };
757
758         query.query_type = pntohs(&pd[offset]);
759         query.server_type = pntohs(&pd[offset+2]);
760
761         if (check_col(fd, COL_PROTOCOL))
762                 col_add_str(fd, COL_PROTOCOL, "SAP");
763         if (check_col(fd, COL_INFO)) {
764                 if (query.query_type >= 1 && query.query_type <= 4) {
765                         col_add_str(fd, COL_INFO, sap_type[query.query_type - 1]);
766                 }
767                 else {
768                         col_add_str(fd, COL_INFO, "Unknown Packet Type");
769                 }
770         }
771
772         if (tree) {
773                 ti = proto_tree_add_item(tree, proto_sap, NullTVB, offset, END_OF_FRAME, NULL);
774                 sap_tree = proto_item_add_subtree(ti, ett_ipxsap);
775
776                 if (query.query_type >= 1 && query.query_type <= 4) {
777                         proto_tree_add_text(sap_tree, NullTVB, offset, 2, sap_type[query.query_type - 1]);
778                         if ((query.query_type - 1) % 2) {
779                           proto_tree_add_item_hidden(sap_tree, 
780                                                      hf_sap_response, 
781                                                      NullTVB, offset, 2, 1);
782                         } else {
783                           proto_tree_add_item_hidden(sap_tree, 
784                                                      hf_sap_request, 
785                                                      NullTVB, offset, 2, 1);
786                         }
787                 }
788                 else {
789                         proto_tree_add_text(sap_tree, NullTVB, offset, 2,
790                                         "Unknown SAP Packet Type %d", query.query_type);
791                 }
792
793                 if (query.query_type == IPX_SAP_GENERAL_RESPONSE ||
794                                 query.query_type == IPX_SAP_NEAREST_RESPONSE) { /* responses */
795
796                         for (cursor = offset + 2; (cursor + 64) <= pi.captured_len; cursor += 64) {
797                                 server.server_type = pntohs(&pd[cursor]);
798                                 memcpy(server.server_name, &pd[cursor+2], 48);
799                                 memcpy(&server.server_network, &pd[cursor+50], 4);
800                                 memcpy(&server.server_node, &pd[cursor+54], 6);
801                                 server.server_port = pntohs(&pd[cursor+60]);
802                                 server.intermediate_network = pntohs(&pd[cursor+62]);
803
804                                 ti = proto_tree_add_text(sap_tree, NullTVB, cursor+2, 48,
805                                         "Server Name: %s", server.server_name);
806                                 s_tree = proto_item_add_subtree(ti, ett_ipxsap_server);
807
808                                 proto_tree_add_text(s_tree, NullTVB, cursor, 2, "Server Type: %s (0x%04X)",
809                                                 server_type(server.server_type), server.server_type);
810                                 proto_tree_add_text(s_tree, NullTVB, cursor+50, 4, "Network: %s",
811                                                 ipxnet_to_string((guint8*)&pd[cursor+50]));
812                                 proto_tree_add_text(s_tree, NullTVB, cursor+54, 6, "Node: %s",
813                                                 ether_to_str((guint8*)&pd[cursor+54]));
814                                 proto_tree_add_text(s_tree, NullTVB, cursor+60, 2, "Socket: %s (0x%04X)",
815                                                 port_text(server.server_port), server.server_port);
816                                 proto_tree_add_text(s_tree, NullTVB, cursor+62, 2,
817                                                 "Intermediate Networks: %d",
818                                                 server.intermediate_network);
819                         }
820                 }
821                 else {  /* queries */
822                         proto_tree_add_text(sap_tree, NullTVB, offset+2, 2, "Server Type: %s (0x%04X)",
823                                         server_type(query.server_type), query.server_type);
824                 }
825         }
826 }
827
828 static void
829 dissect_eigrp_ipx(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
830 {                                                                                                 
831         dissect_eigrp(pd,offset,fd,tree);                                                                 
832 }                                                                                                 
833
834 void
835 proto_register_ipx(void)
836 {
837         static hf_register_info hf_ipx[] = {
838                 { &hf_ipx_checksum,
839                 { "Checksum",           "ipx.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
840                         "" }},
841
842                 { &hf_ipx_len,
843                 { "Length",             "ipx.len", FT_UINT16, BASE_DEC, NULL, 0x0,
844                         "" }},
845
846                 { &hf_ipx_hops,
847                 { "Transport Control (Hops)", "ipx.hops", FT_UINT8, BASE_DEC, NULL, 0x0,
848                         "" }},
849
850                 { &hf_ipx_packet_type,
851                 { "Packet Type",        "ipx.packet_type", FT_UINT8, BASE_HEX, VALS(ipx_packet_type_vals),
852                         0x0,
853                         "" }},
854
855                 { &hf_ipx_dnet,
856                 { "Destination Network","ipx.dst.net", FT_IPXNET, BASE_NONE, NULL, 0x0,
857                         "" }},
858
859                 { &hf_ipx_dnode,
860                 { "Destination Node",   "ipx.dst.node", FT_ETHER, BASE_NONE, NULL, 0x0,
861                         "" }},
862
863                 { &hf_ipx_dsocket,
864                 { "Destination Socket", "ipx.dst.socket", FT_UINT16, BASE_HEX, NULL, 0x0,
865                         "" }},
866
867                 { &hf_ipx_snet,
868                 { "Source Network","ipx.src.net", FT_IPXNET, BASE_NONE, NULL, 0x0,
869                         "" }},
870
871                 { &hf_ipx_snode,
872                 { "Source Node",        "ipx.src.node", FT_ETHER, BASE_NONE, NULL, 0x0,
873                         "" }},
874
875                 { &hf_ipx_ssocket,
876                 { "Source Socket",      "ipx.src.socket", FT_UINT16, BASE_HEX, NULL, 0x0,
877                         "" }},
878         };
879
880         static hf_register_info hf_spx[] = {
881                 { &hf_spx_connection_control,
882                 { "Connection Control",         "spx.ctl", 
883                   FT_UINT8,     BASE_HEX,       NULL,   0x0,
884                   "" }},
885
886                 { &hf_spx_datastream_type,
887                 { "Datastream type",            "spx.type", 
888                   FT_UINT8,     BASE_HEX,       NULL,   0x0,
889                   "" }},
890
891                 { &hf_spx_src_id,
892                 { "Source Connection ID",       "spx.src", 
893                   FT_UINT16,    BASE_DEC,       NULL,   0x0,
894                   "" }},
895
896                 { &hf_spx_dst_id,
897                 { "Destination Connection ID",  "spx.dst", 
898                   FT_UINT16,    BASE_DEC,       NULL,   0x0,
899                   "" }},
900
901                 { &hf_spx_seq_nr,
902                 { "Sequence Number",            "spx.seq", 
903                   FT_UINT16,    BASE_DEC,       NULL,   0x0,
904                   "" }},
905
906                 { &hf_spx_ack_nr,
907                 { "Acknowledgment Number",      "spx.ack", 
908                   FT_UINT16,    BASE_DEC,       NULL,   0x0,
909                   "" }},
910
911                 { &hf_spx_all_nr,
912                 { "Allocation Number",          "spx.alloc", 
913                   FT_UINT16,    BASE_DEC,       NULL,   0x0,
914                   "" }}
915         };
916
917         static hf_register_info hf_ipxrip[] = {
918                 { &hf_ipxrip_request,
919                 { "Request",                    "ipxrip.request", 
920                   FT_BOOLEAN,   BASE_NONE,      NULL,   0x0,
921                   "TRUE if IPX RIP request" }},
922
923                 { &hf_ipxrip_response,
924                 { "Response",                   "ipxrip.response", 
925                   FT_BOOLEAN,   BASE_NONE,      NULL,   0x0,
926                   "TRUE if IPX RIP response" }}
927         };
928
929         static hf_register_info hf_sap[] = {
930                 { &hf_sap_request,
931                 { "Request",                    "ipxsap.request", 
932                   FT_BOOLEAN,   BASE_NONE,      NULL,   0x0,
933                   "TRUE if SAP request" }},
934
935                 { &hf_sap_response,
936                 { "Response",                   "ipxsap.response", 
937                   FT_BOOLEAN,   BASE_NONE,      NULL,   0x0,
938                   "TRUE if SAP response" }}
939         };
940
941         static hf_register_info hf_ipxmsg[] = {
942                 { &hf_msg_conn,
943                 { "Connection Number",                  "ipxmsg.conn", 
944                   FT_UINT8,     BASE_NONE,      NULL,   0x0,
945                   "Connection Number" }},
946
947                 { &hf_msg_sigchar,
948                 { "Signature Char",                     "ipxmsg.sigchar", 
949                   FT_UINT8,     BASE_NONE,      VALS(ipxmsg_sigchar_vals),      0x0,
950                   "Signature Char" }}
951         };
952
953         static gint *ett[] = {
954                 &ett_ipx,
955                 &ett_spx,
956                 &ett_ipxmsg,
957                 &ett_ipxrip,
958                 &ett_ipxsap,
959                 &ett_ipxsap_server,
960         };
961
962         proto_ipx = proto_register_protocol ("Internetwork Packet eXchange", "ipx");
963         proto_register_field_array(proto_ipx, hf_ipx, array_length(hf_ipx));
964
965         proto_spx = proto_register_protocol ("Sequenced Packet eXchange", "spx");
966         proto_register_field_array(proto_spx, hf_spx, array_length(hf_spx));
967
968         proto_ipxrip = proto_register_protocol ("IPX Routing Information Protocol", "ipxrip");
969         proto_register_field_array(proto_ipxrip, hf_ipxrip, array_length(hf_ipxrip));
970
971         proto_ipxmsg = proto_register_protocol ("IPX Message", "ipxmsg");
972         proto_register_field_array(proto_ipxmsg, hf_ipxmsg, array_length(hf_ipxmsg));
973
974         proto_sap = proto_register_protocol ("Service Advertisement Protocol", "ipxsap");
975         proto_register_field_array(proto_sap, hf_sap, array_length(hf_sap));
976
977         proto_register_subtree_array(ett, array_length(ett));
978 }
979
980 void
981 proto_reg_handoff_ipx(void)
982 {
983         dissector_add("udp.port", UDP_PORT_IPX, dissect_ipx);
984         dissector_add("ethertype", ETHERTYPE_IPX, dissect_ipx);
985         dissector_add("ppp.protocol", PPP_IPX, dissect_ipx);
986         dissector_add("llc.dsap", SAP_NETWARE, dissect_ipx);
987 }