1 /* $OpenBSD: print-ipx.c,v 1.12 2009/10/27 23:59:55 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1994, 1995, 1996 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that: (1) source code distributions 9 * retain the above copyright notice and this paragraph in its entirety, (2) 10 * distributions including binary code include the above copyright notice and 11 * this paragraph in its entirety in the documentation or other materials 12 * provided with the distribution, and (3) all advertising materials mentioning 13 * features or use of this software display the following acknowledgement: 14 * ``This product includes software developed by the University of California, 15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16 * the University nor the names of its contributors may be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 * 23 * Format and print Novell IPX packets. 24 * Contributed by Brad Parker (brad@fcr.com). 25 */ 26 27 #include <sys/param.h> 28 #include <sys/time.h> 29 #include <sys/socket.h> 30 31 #include <netinet/in.h> 32 #include <netinet/in_systm.h> 33 #include <netinet/ip.h> 34 #include <netinet/ip_var.h> 35 #include <netinet/udp.h> 36 #include <netinet/udp_var.h> 37 #include <netinet/tcp.h> 38 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 43 #include "interface.h" 44 #include "addrtoname.h" 45 #include "ipx.h" 46 #include "extract.h" 47 48 49 static const char *ipxaddr_string(u_int32_t, const u_char *); 50 void ipx_decode(const struct ipxHdr *, const u_char *, u_int); 51 void ipx_sap_print(const u_short *, u_int); 52 void ipx_rip_print(const u_short *, u_int); 53 54 /* 55 * Print IPX datagram packets. 56 */ 57 void 58 ipx_print(const u_char *p, u_int length) 59 { 60 const struct ipxHdr *ipx = (const struct ipxHdr *)p; 61 62 TCHECK(ipx->srcSkt); 63 (void)printf("%s.%x > ", 64 ipxaddr_string(EXTRACT_32BITS(ipx->srcNet), ipx->srcNode), 65 EXTRACT_16BITS(&ipx->srcSkt)); 66 67 (void)printf("%s.%x:", 68 ipxaddr_string(EXTRACT_32BITS(ipx->dstNet), ipx->dstNode), 69 EXTRACT_16BITS(&ipx->dstSkt)); 70 71 /* take length from ipx header */ 72 TCHECK(ipx->length); 73 length = EXTRACT_16BITS(&ipx->length); 74 75 ipx_decode(ipx, (u_char *)ipx + ipxSize, length - ipxSize); 76 return; 77 trunc: 78 printf("[|ipx %d]", length); 79 } 80 81 static const char * 82 ipxaddr_string(u_int32_t net, const u_char *node) 83 { 84 static char line[256]; 85 86 snprintf(line, sizeof(line), "%x.%02x:%02x:%02x:%02x:%02x:%02x", 87 net, node[0], node[1], node[2], node[3], node[4], node[5]); 88 89 return line; 90 } 91 92 void 93 ipx_decode(const struct ipxHdr *ipx, const u_char *datap, u_int length) 94 { 95 register u_short dstSkt; 96 97 dstSkt = EXTRACT_16BITS(&ipx->dstSkt); 98 switch (dstSkt) { 99 case IPX_SKT_NCP: 100 (void)printf(" ipx-ncp %d", length); 101 break; 102 case IPX_SKT_SAP: 103 ipx_sap_print((u_short *)datap, length); 104 break; 105 case IPX_SKT_RIP: 106 ipx_rip_print((u_short *)datap, length); 107 break; 108 case IPX_SKT_NETBIOS: 109 (void)printf(" ipx-netbios %d", length); 110 break; 111 case IPX_SKT_DIAGNOSTICS: 112 (void)printf(" ipx-diags %d", length); 113 break; 114 default: 115 (void)printf(" ipx-#%x %d", dstSkt, length); 116 break; 117 } 118 } 119 120 void 121 ipx_sap_print(const u_short *ipx, u_int length) 122 { 123 int command, i; 124 125 TCHECK(ipx[0]); 126 command = EXTRACT_16BITS(ipx); 127 ipx++; 128 length -= 2; 129 130 switch (command) { 131 case 1: 132 case 3: 133 if (command == 1) 134 (void)printf("ipx-sap-req"); 135 else 136 (void)printf("ipx-sap-nearest-req"); 137 138 if (length > 0) { 139 TCHECK(ipx[1]); 140 (void)printf(" %x '", EXTRACT_16BITS(&ipx[0])); 141 fn_print((u_char *)&ipx[1], min(snapend, (u_char *)&ipx[1] + 48)); 142 putchar('\''); 143 } 144 break; 145 146 case 2: 147 case 4: 148 if (command == 2) 149 (void)printf("ipx-sap-resp"); 150 else 151 (void)printf("ipx-sap-nearest-resp"); 152 153 for (i = 0; i < 8 && length > 0; i++) { 154 TCHECK2(ipx[27], 1); 155 (void)printf(" %x '", EXTRACT_16BITS(&ipx[0])); 156 fn_print((u_char *)&ipx[1], min(snapend, (u_char *)&ipx[1] + 48)); 157 printf("' addr %s", 158 ipxaddr_string(EXTRACT_32BITS(&ipx[25]), (u_char *)&ipx[27])); 159 ipx += 32; 160 length -= 64; 161 } 162 break; 163 default: 164 (void)printf("ipx-sap-?%x", command); 165 break; 166 } 167 return; 168 trunc: 169 printf("[|ipx %d]", length); 170 } 171 172 void 173 ipx_rip_print(const u_short *ipx, u_int length) 174 { 175 int command, i; 176 177 TCHECK(ipx[0]); 178 command = EXTRACT_16BITS(ipx); 179 ipx++; 180 length -= 2; 181 182 switch (command) { 183 case 1: 184 (void)printf("ipx-rip-req"); 185 if (length > 0) { 186 TCHECK(ipx[3]); 187 (void)printf(" %u/%d.%d", EXTRACT_32BITS(&ipx[0]), 188 EXTRACT_16BITS(&ipx[2]), EXTRACT_16BITS(&ipx[3])); 189 } 190 break; 191 case 2: 192 (void)printf("ipx-rip-resp"); 193 for (i = 0; i < 50 && length > 0; i++) { 194 TCHECK(ipx[3]); 195 (void)printf(" %u/%d.%d", EXTRACT_32BITS(&ipx[0]), 196 EXTRACT_16BITS(&ipx[2]), EXTRACT_16BITS(&ipx[3])); 197 198 ipx += 4; 199 length -= 8; 200 } 201 break; 202 default: 203 (void)printf("ipx-rip-?%x", command); 204 } 205 return; 206 trunc: 207 printf("[|ipx %d]", length); 208 } 209 210