Thu Apr 23 00:23:31 2020 UTC ()
Avoid global scope for variables only used locally


(joerg)
diff -r1.17 -r1.18 src/usr.bin/netstat/atalk.c

cvs diff -r1.17 -r1.18 src/usr.bin/netstat/atalk.c (switch to unified diff)

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