1 /* $NetBSD: atalk.c,v 1.8 2003/08/07 11:15:18 agc Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1988, 1993 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 the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "from @(#)atalk.c 1.1 (Whistle) 6/6/96"; 36 #else 37 __RCSID("$NetBSD: atalk.c,v 1.8 2003/08/07 11:15:18 agc Exp $"); 38 #endif 39 #endif /* not lint */ 40 41 #include <sys/param.h> 42 #include <sys/queue.h> 43 #include <sys/socket.h> 44 #include <sys/socketvar.h> 45 #include <sys/mbuf.h> 46 #include <sys/protosw.h> 47 48 #include <net/route.h> 49 #include <net/if.h> 50 51 #include <netinet/tcp_fsm.h> 52 53 #include <netatalk/at.h> 54 #include <netatalk/ddp_var.h> 55 56 #include <nlist.h> 57 #include <errno.h> 58 #include <stdio.h> 59 #include <string.h> 60 #include "netstat.h" 61 62 struct ddpcb ddpcb; 63 struct socket sockb; 64 65 static int first = 1; 66 67 static char *at_pr_net __P((struct sockaddr_at *, int)); 68 static char *at_pr_host __P((struct sockaddr_at *, int)); 69 static char *at_pr_range __P((struct sockaddr_at *)); 70 static char *at_pr_port __P((struct sockaddr_at *)); 71 72 /* 73 * Print a summary of connections related to a Network Systems 74 * protocol. For XXX, also give state of connection. 75 * Listening processes (aflag) are suppressed unless the 76 * -a (all) flag is specified. 77 */ 78 79 static char * 80 at_pr_net(sat, numeric) 81 struct sockaddr_at *sat; 82 int numeric; 83 { 84 static char mybuf[50]; 85 86 if (!numeric) { 87 switch (sat->sat_addr.s_net) { 88 case 0xffff: 89 return "????"; 90 case ATADDR_ANYNET: 91 return ("*"); 92 } 93 } 94 (void)snprintf(mybuf, sizeof(mybuf), "%hu", ntohs(sat->sat_addr.s_net)); 95 return (mybuf); 96 } 97 98 static char * 99 at_pr_host(sat, numeric) 100 struct sockaddr_at *sat; 101 int numeric; 102 { 103 static char mybuf[50]; 104 105 if (!numeric) { 106 switch (sat->sat_addr.s_node) { 107 case ATADDR_BCAST: 108 return "bcast"; 109 case ATADDR_ANYNODE: 110 return ("*"); 111 } 112 } 113 (void)snprintf(mybuf, sizeof(mybuf), "%d", 114 (unsigned int)sat->sat_addr.s_node); 115 return (mybuf); 116 } 117 118 static char * 119 at_pr_port(sat) 120 struct sockaddr_at *sat; 121 { 122 static char mybuf[50]; 123 124 switch (sat->sat_port) { 125 case ATADDR_ANYPORT: 126 return ("*"); 127 case 0xff: 128 return "????"; 129 default: 130 (void)snprintf(mybuf, sizeof(mybuf), "%d", 131 (unsigned int)sat->sat_port); 132 return (mybuf); 133 } 134 } 135 136 static char * 137 at_pr_range(sat) 138 struct sockaddr_at *sat; 139 { 140 static char mybuf[50]; 141 142 if (sat->sat_range.r_netrange.nr_firstnet 143 != sat->sat_range.r_netrange.nr_lastnet) { 144 (void)snprintf(mybuf, sizeof(mybuf), "%d-%d", 145 ntohs(sat->sat_range.r_netrange.nr_firstnet), 146 ntohs(sat->sat_range.r_netrange.nr_lastnet)); 147 } else { 148 (void)snprintf(mybuf, sizeof(mybuf), "%d", 149 ntohs(sat->sat_range.r_netrange.nr_firstnet)); 150 } 151 return (mybuf); 152 } 153 154 155 /* what == 0 for addr only == 3 156 * 1 for net 157 * 2 for host 158 * 4 for port 159 * 8 for numeric only 160 */ 161 char * 162 atalk_print(sa, what) 163 const struct sockaddr *sa; 164 int what; 165 { 166 struct sockaddr_at *sat = (struct sockaddr_at *) sa; 167 static char mybuf[50]; 168 int numeric = (what & 0x08); 169 170 mybuf[0] = 0; 171 switch (what & 0x13) { 172 case 0: 173 mybuf[0] = 0; 174 break; 175 case 1: 176 (void)snprintf(mybuf, sizeof(mybuf), "%s", 177 at_pr_net(sat, numeric)); 178 break; 179 case 2: 180 (void)snprintf(mybuf, sizeof(mybuf), "%s", 181 at_pr_host(sat, numeric)); 182 break; 183 case 3: 184 (void)snprintf(mybuf, sizeof(mybuf), "%s.%s", 185 at_pr_net(sat, numeric), 186 at_pr_host(sat, numeric)); 187 break; 188 case 0x10: 189 (void)snprintf(mybuf, sizeof(mybuf), "%s", at_pr_range(sat)); 190 } 191 if (what & 4) { 192 (void)snprintf(mybuf + strlen(mybuf), 193 sizeof(mybuf) - strlen(mybuf), ".%s", at_pr_port(sat)); 194 } 195 return (mybuf); 196 } 197 198 char * 199 atalk_print2(sa, mask, what) 200 const struct sockaddr *sa; 201 const struct sockaddr *mask; 202 int what; 203 { 204 size_t n, l; 205 static char buf[100]; 206 struct sockaddr_at *sat1, *sat2; 207 struct sockaddr_at thesockaddr; 208 struct sockaddr *sa2; 209 210 sat1 = (struct sockaddr_at *) sa; 211 sat2 = (struct sockaddr_at *) mask; 212 sa2 = (struct sockaddr *) & thesockaddr; 213 214 thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net & 215 sat2->sat_addr.s_net; 216 n = snprintf(buf, sizeof(buf), "%s", atalk_print(sa2, 1 | (what & 8))); 217 if (n >= sizeof(buf)) 218 n = sizeof(buf) - 1; 219 else if (n == -1) 220 n = 0; /* What else can be done ? */ 221 if (sat2->sat_addr.s_net != 0xFFFF) { 222 thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net | 223 ~sat2->sat_addr.s_net; 224 l = snprintf(buf + n, sizeof(buf) - n, 225 "-%s", atalk_print(sa2, 1 | (what & 8))); 226 if (l >= sizeof(buf) - n) 227 l = sizeof(buf) - n - 1; 228 if (l > 0) 229 n += l; 230 } 231 if (what & 2) { 232 l = snprintf(buf + n, sizeof(buf) - n, ".%s", 233 atalk_print(sa, what & (~1))); 234 if (l >= sizeof(buf) - n) 235 l = sizeof(buf) - n - 1; 236 if (l > 0) 237 n += l; 238 } 239 return (buf); 240 } 241 242 void 243 atalkprotopr(off, name) 244 u_long off; 245 char *name; 246 { 247 struct ddpcb cb; 248 struct ddpcb *prev, *next; 249 struct ddpcb *initial; 250 int width = 22; 251 if (off == 0) 252 return; 253 if (kread(off, (char *)&initial, sizeof(struct ddpcb *)) < 0) 254 return; 255 ddpcb = cb; 256 prev = (struct ddpcb *)off; 257 for (next = initial; next != NULL; prev = next) { 258 u_long ppcb = (u_long)next; 259 260 if (kread((u_long)next, (char *)&ddpcb, sizeof(ddpcb)) < 0) 261 return; 262 next = ddpcb.ddp_next; 263 #if 0 264 if (!aflag && atalk_nullhost(ddpcb.ddp_lsat)) { 265 continue; 266 } 267 #endif 268 if (kread((u_long)ddpcb.ddp_socket, 269 (char *)&sockb, sizeof(sockb)) < 0) 270 return; 271 if (first) { 272 printf("Active ATALK connections"); 273 if (aflag) 274 printf(" (including servers)"); 275 putchar('\n'); 276 if (Aflag) { 277 width = 18; 278 printf("%-8.8s ", "PCB"); 279 } 280 printf("%-5.5s %-6.6s %-6.6s %*.*s %*.*s %s\n", 281 "Proto", "Recv-Q", "Send-Q", 282 -width, width, "Local Address", 283 -width, width, "Foreign Address", "(state)"); 284 first = 0; 285 } 286 if (Aflag) 287 printf("%8lx ", ppcb); 288 printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc, 289 sockb.so_snd.sb_cc); 290 printf(" %*.*s", -width, width, 291 atalk_print((struct sockaddr *)&ddpcb.ddp_lsat, 7)); 292 printf(" %*.*s", -width, width, 293 atalk_print((struct sockaddr *)&ddpcb.ddp_fsat, 7)); 294 putchar('\n'); 295 } 296 } 297 #define ANY(x,y,z) \ 298 ((sflag==1 || (x)) ? printf("\t%ld %s%s%s\n",x,y,plural(x),z) : 0) 299 300 /* 301 * Dump DDP statistics structure. 302 */ 303 void 304 ddp_stats(off, name) 305 u_long off; 306 char *name; 307 { 308 struct ddpstat ddpstat; 309 310 if (off == 0) 311 return; 312 if (kread(off, (char *)&ddpstat, sizeof(ddpstat)) < 0) 313 return; 314 printf("%s:\n", name); 315 ANY(ddpstat.ddps_short, "packet", " with short headers "); 316 ANY(ddpstat.ddps_long, "packet", " with long headers "); 317 ANY(ddpstat.ddps_nosum, "packet", " with no checksum "); 318 ANY(ddpstat.ddps_tooshort, "packet", " too short "); 319 ANY(ddpstat.ddps_badsum, "packet", " with bad checksum "); 320 ANY(ddpstat.ddps_toosmall, "packet", " with not enough data "); 321 ANY(ddpstat.ddps_forward, "packet", " forwarded "); 322 ANY(ddpstat.ddps_encap, "packet", " encapsulated "); 323 ANY(ddpstat.ddps_cantforward, "packet", " rcvd for unreachable dest "); 324 ANY(ddpstat.ddps_nosockspace, "packet", " dropped due to no socket space "); 325 } 326 #undef ANY 327