[d48955b] | 1 | #include <machine/rtems-bsd-user-space.h> |
---|
| 2 | |
---|
[c40e45b] | 3 | #ifdef __rtems__ |
---|
| 4 | #include "rtems-bsd-netstat-namespace.h" |
---|
| 5 | #endif /* __rtems__ */ |
---|
| 6 | |
---|
[f1710b6] | 7 | /*- |
---|
| 8 | * Copyright (c) 1983, 1988, 1993 |
---|
| 9 | * Regents of the University of California. All rights reserved. |
---|
| 10 | * |
---|
| 11 | * Redistribution and use in source and binary forms, with or without |
---|
| 12 | * modification, are permitted provided that the following conditions |
---|
| 13 | * are met: |
---|
| 14 | * 1. Redistributions of source code must retain the above copyright |
---|
| 15 | * notice, this list of conditions and the following disclaimer. |
---|
| 16 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 17 | * notice, this list of conditions and the following disclaimer in the |
---|
| 18 | * documentation and/or other materials provided with the distribution. |
---|
| 19 | * 4. Neither the name of the University nor the names of its contributors |
---|
| 20 | * may be used to endorse or promote products derived from this software |
---|
| 21 | * without specific prior written permission. |
---|
| 22 | * |
---|
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
| 33 | * SUCH DAMAGE. |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | #ifndef lint |
---|
[c40e45b] | 37 | static char const copyright[] = |
---|
[f1710b6] | 38 | "@(#) Copyright (c) 1983, 1988, 1993\n\ |
---|
| 39 | Regents of the University of California. All rights reserved.\n"; |
---|
| 40 | #endif /* not lint */ |
---|
| 41 | |
---|
| 42 | #if 0 |
---|
| 43 | #ifndef lint |
---|
| 44 | static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94"; |
---|
| 45 | #endif /* not lint */ |
---|
| 46 | #endif |
---|
| 47 | |
---|
[c333bab] | 48 | #ifdef __rtems__ |
---|
| 49 | #define __need_getopt_newlib |
---|
| 50 | #include <getopt.h> |
---|
[a400e1d] | 51 | #include <machine/rtems-bsd-program.h> |
---|
| 52 | #include <machine/rtems-bsd-commands.h> |
---|
[c333bab] | 53 | #endif /* __rtems__ */ |
---|
[f1710b6] | 54 | #include <sys/cdefs.h> |
---|
| 55 | __FBSDID("$FreeBSD$"); |
---|
| 56 | |
---|
[e599318] | 57 | #include <rtems/bsd/sys/param.h> |
---|
[f1710b6] | 58 | #include <sys/file.h> |
---|
| 59 | #include <sys/protosw.h> |
---|
| 60 | #include <sys/socket.h> |
---|
| 61 | #include <sys/socketvar.h> |
---|
[c40e45b] | 62 | #include <sys/sysctl.h> |
---|
[f1710b6] | 63 | |
---|
| 64 | #include <netinet/in.h> |
---|
| 65 | |
---|
| 66 | #ifdef NETGRAPH |
---|
| 67 | #include <netgraph/ng_socket.h> |
---|
| 68 | #endif |
---|
| 69 | |
---|
| 70 | #include <ctype.h> |
---|
| 71 | #include <err.h> |
---|
| 72 | #include <errno.h> |
---|
| 73 | #include <kvm.h> |
---|
| 74 | #include <limits.h> |
---|
| 75 | #include <netdb.h> |
---|
| 76 | #include <nlist.h> |
---|
| 77 | #include <paths.h> |
---|
| 78 | #include <stdint.h> |
---|
| 79 | #include <stdio.h> |
---|
| 80 | #include <stdlib.h> |
---|
[c40e45b] | 81 | #include <stdbool.h> |
---|
[f1710b6] | 82 | #include <string.h> |
---|
| 83 | #include <unistd.h> |
---|
| 84 | #include "netstat.h" |
---|
[c40e45b] | 85 | #include "nl_defs.h" |
---|
| 86 | #include <libxo/xo.h> |
---|
| 87 | #ifdef __rtems__ |
---|
| 88 | #include "rtems-bsd-netstat-main-data.h" |
---|
| 89 | #endif /* __rtems__ */ |
---|
[f1710b6] | 90 | |
---|
[c40e45b] | 91 | #ifndef __rtems__ |
---|
| 92 | static struct protox { |
---|
| 93 | #else /* __rtems__ */ |
---|
| 94 | static const struct protox { |
---|
| 95 | #endif /* __rtems__ */ |
---|
[f1710b6] | 96 | int pr_index; /* index into nlist of cb head */ |
---|
| 97 | int pr_sindex; /* index into nlist of stat block */ |
---|
| 98 | u_char pr_wanted; /* 1 if wanted, 0 otherwise */ |
---|
| 99 | void (*pr_cblocks)(u_long, const char *, int, int); |
---|
| 100 | /* control blocks printing routine */ |
---|
| 101 | void (*pr_stats)(u_long, const char *, int, int); |
---|
| 102 | /* statistics printing routine */ |
---|
| 103 | void (*pr_istats)(char *); /* per/if statistics printing routine */ |
---|
| 104 | const char *pr_name; /* well-known name */ |
---|
| 105 | int pr_usesysctl; /* non-zero if we use sysctl, not kvm */ |
---|
| 106 | int pr_protocol; |
---|
[c40e45b] | 107 | } protox[] = { |
---|
[f1710b6] | 108 | { N_TCBINFO, N_TCPSTAT, 1, protopr, |
---|
| 109 | tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, |
---|
| 110 | { N_UDBINFO, N_UDPSTAT, 1, protopr, |
---|
| 111 | udp_stats, NULL, "udp", 1, IPPROTO_UDP }, |
---|
| 112 | #ifdef SCTP |
---|
| 113 | { -1, N_SCTPSTAT, 1, sctp_protopr, |
---|
| 114 | sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP }, |
---|
[66659ff] | 115 | #endif |
---|
| 116 | #ifdef SDP |
---|
| 117 | { -1, -1, 1, protopr, |
---|
| 118 | NULL, NULL, "sdp", 1, IPPROTO_TCP }, |
---|
[f1710b6] | 119 | #endif |
---|
| 120 | { N_DIVCBINFO, -1, 1, protopr, |
---|
| 121 | NULL, NULL, "divert", 1, IPPROTO_DIVERT }, |
---|
| 122 | { N_RIPCBINFO, N_IPSTAT, 1, protopr, |
---|
| 123 | ip_stats, NULL, "ip", 1, IPPROTO_RAW }, |
---|
| 124 | { N_RIPCBINFO, N_ICMPSTAT, 1, protopr, |
---|
| 125 | icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP }, |
---|
| 126 | { N_RIPCBINFO, N_IGMPSTAT, 1, protopr, |
---|
| 127 | igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP }, |
---|
| 128 | #ifdef IPSEC |
---|
[c40e45b] | 129 | { -1, N_IPSEC4STAT, 1, NULL, /* keep as compat */ |
---|
| 130 | ipsec_stats, NULL, "ipsec", 1, 0}, |
---|
[f1710b6] | 131 | { -1, N_AHSTAT, 1, NULL, |
---|
[c40e45b] | 132 | ah_stats, NULL, "ah", 1, 0}, |
---|
[f1710b6] | 133 | { -1, N_ESPSTAT, 1, NULL, |
---|
[c40e45b] | 134 | esp_stats, NULL, "esp", 1, 0}, |
---|
[f1710b6] | 135 | { -1, N_IPCOMPSTAT, 1, NULL, |
---|
[c40e45b] | 136 | ipcomp_stats, NULL, "ipcomp", 1, 0}, |
---|
[f1710b6] | 137 | #endif |
---|
| 138 | { N_RIPCBINFO, N_PIMSTAT, 1, protopr, |
---|
| 139 | pim_stats, NULL, "pim", 1, IPPROTO_PIM }, |
---|
[c40e45b] | 140 | { -1, N_CARPSTATS, 1, NULL, |
---|
[f1710b6] | 141 | carp_stats, NULL, "carp", 1, 0 }, |
---|
[c40e45b] | 142 | #ifdef PF |
---|
| 143 | { -1, N_PFSYNCSTATS, 1, NULL, |
---|
[f1710b6] | 144 | pfsync_stats, NULL, "pfsync", 1, 0 }, |
---|
[c40e45b] | 145 | #endif |
---|
[f1710b6] | 146 | { -1, N_ARPSTAT, 1, NULL, |
---|
| 147 | arp_stats, NULL, "arp", 1, 0 }, |
---|
| 148 | { -1, -1, 0, NULL, |
---|
| 149 | NULL, NULL, NULL, 0, 0 } |
---|
| 150 | }; |
---|
| 151 | |
---|
| 152 | #ifdef INET6 |
---|
[c40e45b] | 153 | #ifndef __rtems__ |
---|
| 154 | static struct protox ip6protox[] = { |
---|
| 155 | #else /* __rtems__ */ |
---|
[4db7af6] | 156 | static const struct protox ip6protox[] = { |
---|
[c40e45b] | 157 | #endif /* __rtems__ */ |
---|
[f1710b6] | 158 | { N_TCBINFO, N_TCPSTAT, 1, protopr, |
---|
| 159 | tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, |
---|
| 160 | { N_UDBINFO, N_UDPSTAT, 1, protopr, |
---|
| 161 | udp_stats, NULL, "udp", 1, IPPROTO_UDP }, |
---|
| 162 | { N_RIPCBINFO, N_IP6STAT, 1, protopr, |
---|
| 163 | ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW }, |
---|
| 164 | { N_RIPCBINFO, N_ICMP6STAT, 1, protopr, |
---|
| 165 | icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 }, |
---|
[66659ff] | 166 | #ifdef SDP |
---|
| 167 | { -1, -1, 1, protopr, |
---|
| 168 | NULL, NULL, "sdp", 1, IPPROTO_TCP }, |
---|
| 169 | #endif |
---|
[f1710b6] | 170 | #ifdef IPSEC |
---|
| 171 | { -1, N_IPSEC6STAT, 1, NULL, |
---|
[c40e45b] | 172 | ipsec_stats, NULL, "ipsec6", 1, 0 }, |
---|
[f1710b6] | 173 | #endif |
---|
| 174 | #ifdef notyet |
---|
| 175 | { -1, N_PIM6STAT, 1, NULL, |
---|
| 176 | pim6_stats, NULL, "pim6", 1, 0 }, |
---|
| 177 | #endif |
---|
| 178 | { -1, N_RIP6STAT, 1, NULL, |
---|
| 179 | rip6_stats, NULL, "rip6", 1, 0 }, |
---|
| 180 | { -1, -1, 0, NULL, |
---|
| 181 | NULL, NULL, NULL, 0, 0 } |
---|
| 182 | }; |
---|
| 183 | #endif /*INET6*/ |
---|
| 184 | |
---|
| 185 | #ifdef IPSEC |
---|
[c40e45b] | 186 | #ifndef __rtems__ |
---|
| 187 | static struct protox pfkeyprotox[] = { |
---|
| 188 | #else /* __rtems__ */ |
---|
[4db7af6] | 189 | static const struct protox pfkeyprotox[] = { |
---|
[c40e45b] | 190 | #endif /* __rtems__ */ |
---|
[f1710b6] | 191 | { -1, N_PFKEYSTAT, 1, NULL, |
---|
| 192 | pfkey_stats, NULL, "pfkey", 0, 0 }, |
---|
| 193 | { -1, -1, 0, NULL, |
---|
| 194 | NULL, NULL, NULL, 0, 0 } |
---|
| 195 | }; |
---|
| 196 | #endif |
---|
| 197 | |
---|
| 198 | #ifdef NETGRAPH |
---|
[c40e45b] | 199 | #ifndef __rtems__ |
---|
| 200 | static struct protox netgraphprotox[] = { |
---|
| 201 | #else /* __rtems__ */ |
---|
[4db7af6] | 202 | static const struct protox netgraphprotox[] = { |
---|
[c40e45b] | 203 | #endif /* __rtems__ */ |
---|
| 204 | { N_NGSOCKLIST, -1, 1, netgraphprotopr, |
---|
[f1710b6] | 205 | NULL, NULL, "ctrl", 0, 0 }, |
---|
[c40e45b] | 206 | { N_NGSOCKLIST, -1, 1, netgraphprotopr, |
---|
[f1710b6] | 207 | NULL, NULL, "data", 0, 0 }, |
---|
| 208 | { -1, -1, 0, NULL, |
---|
| 209 | NULL, NULL, NULL, 0, 0 } |
---|
| 210 | }; |
---|
| 211 | #endif |
---|
| 212 | |
---|
[c40e45b] | 213 | #ifndef __rtems__ |
---|
| 214 | static struct protox *protoprotox[] = { |
---|
| 215 | #else /* __rtems__ */ |
---|
| 216 | static const struct protox *const protoprotox[] = { |
---|
| 217 | #endif /* __rtems__ */ |
---|
[f1710b6] | 218 | protox, |
---|
| 219 | #ifdef INET6 |
---|
| 220 | ip6protox, |
---|
| 221 | #endif |
---|
| 222 | #ifdef IPSEC |
---|
| 223 | pfkeyprotox, |
---|
| 224 | #endif |
---|
[c40e45b] | 225 | NULL }; |
---|
[f1710b6] | 226 | |
---|
[c40e45b] | 227 | #ifndef __rtems__ |
---|
| 228 | static void printproto(struct protox *, const char *, bool *); |
---|
| 229 | #else /* __rtems__ */ |
---|
| 230 | static void printproto(const struct protox *, const char *, bool *); |
---|
| 231 | #endif /* __rtems__ */ |
---|
[f1710b6] | 232 | static void usage(void); |
---|
[c40e45b] | 233 | #ifndef __rtems__ |
---|
| 234 | static struct protox *name2protox(const char *); |
---|
| 235 | static struct protox *knownname(const char *); |
---|
| 236 | #else /* __rtems__ */ |
---|
[4db7af6] | 237 | static const struct protox *name2protox(const char *); |
---|
| 238 | static const struct protox *knownname(const char *); |
---|
[c40e45b] | 239 | #endif /* __rtems__ */ |
---|
| 240 | |
---|
| 241 | static int kresolve_list(struct nlist *_nl); |
---|
[f1710b6] | 242 | |
---|
| 243 | static kvm_t *kvmd; |
---|
| 244 | static char *nlistf = NULL, *memf = NULL; |
---|
| 245 | |
---|
| 246 | int Aflag; /* show addresses of protocol control block */ |
---|
| 247 | int aflag; /* show all sockets (including servers) */ |
---|
[c40e45b] | 248 | static int Bflag; /* show information about bpf consumers */ |
---|
[f1710b6] | 249 | int bflag; /* show i/f total bytes in/out */ |
---|
| 250 | int dflag; /* show i/f dropped packets */ |
---|
| 251 | int gflag; /* show group (multicast) routing or stats */ |
---|
| 252 | int hflag; /* show counters in human readable format */ |
---|
| 253 | int iflag; /* show interfaces */ |
---|
| 254 | int Lflag; /* show size of listen queues */ |
---|
| 255 | int mflag; /* show memory stats */ |
---|
| 256 | int noutputs = 0; /* how much outputs before we exit */ |
---|
| 257 | int numeric_addr; /* show addresses numerically */ |
---|
| 258 | int numeric_port; /* show ports numerically */ |
---|
| 259 | static int pflag; /* show given protocol */ |
---|
[af5333e] | 260 | #ifndef __rtems__ |
---|
[c40e45b] | 261 | static int Qflag; /* show netisr information */ |
---|
[af5333e] | 262 | #endif /* __rtems__ */ |
---|
[f1710b6] | 263 | int rflag; /* show routing tables (or routing stats) */ |
---|
[c40e45b] | 264 | int Rflag; /* show flow / RSS statistics */ |
---|
[f1710b6] | 265 | int sflag; /* show protocol statistics */ |
---|
| 266 | int Wflag; /* wide display */ |
---|
[af5333e] | 267 | int Tflag; /* TCP Information */ |
---|
[f1710b6] | 268 | int xflag; /* extra information, includes all socket buffer info */ |
---|
| 269 | int zflag; /* zero stats */ |
---|
| 270 | |
---|
| 271 | int interval; /* repeat interval for i/f stats */ |
---|
| 272 | |
---|
| 273 | char *interface; /* desired i/f for stats, or NULL for all i/fs */ |
---|
| 274 | int unit; /* unit number for above */ |
---|
| 275 | |
---|
[c40e45b] | 276 | static int af; /* address family */ |
---|
[f1710b6] | 277 | int live; /* true if we are examining a live system */ |
---|
| 278 | |
---|
[2df6b31] | 279 | #ifdef __rtems__ |
---|
[d01564c] | 280 | static int main(int argc, char *argv[]); |
---|
| 281 | |
---|
[c40e45b] | 282 | RTEMS_LINKER_RWSET(bsd_prog_netstat, char); |
---|
| 283 | |
---|
| 284 | int |
---|
| 285 | rtems_bsd_command_netstat(int argc, char *argv[]) |
---|
[2df6b31] | 286 | { |
---|
[6da9b23] | 287 | int exit_code; |
---|
[c40e45b] | 288 | void *data_begin; |
---|
| 289 | size_t data_size; |
---|
[6da9b23] | 290 | |
---|
[c40e45b] | 291 | data_begin = RTEMS_LINKER_SET_BEGIN(bsd_prog_netstat); |
---|
| 292 | data_size = RTEMS_LINKER_SET_SIZE(bsd_prog_netstat); |
---|
[6da9b23] | 293 | |
---|
[c40e45b] | 294 | rtems_bsd_program_lock(); |
---|
| 295 | exit_code = rtems_bsd_program_call_main_with_data_restore("netstat", |
---|
| 296 | main, argc, argv, data_begin, data_size); |
---|
[6da9b23] | 297 | rtems_bsd_program_unlock(); |
---|
[2df6b31] | 298 | |
---|
[6da9b23] | 299 | return exit_code; |
---|
[2df6b31] | 300 | } |
---|
[d01564c] | 301 | #endif /* __rtems__ */ |
---|
[f1710b6] | 302 | int |
---|
| 303 | main(int argc, char *argv[]) |
---|
| 304 | { |
---|
[c40e45b] | 305 | #ifndef __rtems__ |
---|
| 306 | struct protox *tp = NULL; /* for printing cblocks & stats */ |
---|
| 307 | #else /* __rtems__ */ |
---|
[4db7af6] | 308 | const struct protox *tp = NULL; /* for printing cblocks & stats */ |
---|
[c40e45b] | 309 | #endif /* __rtems__ */ |
---|
[f1710b6] | 310 | int ch; |
---|
[c40e45b] | 311 | int fib = -1; |
---|
| 312 | char *endptr; |
---|
| 313 | bool first = true; |
---|
[b6ac989] | 314 | #ifdef __rtems__ |
---|
[c333bab] | 315 | struct getopt_data getopt_data; |
---|
| 316 | memset(&getopt_data, 0, sizeof(getopt_data)); |
---|
| 317 | #define optind getopt_data.optind |
---|
| 318 | #define optarg getopt_data.optarg |
---|
| 319 | #define opterr getopt_data.opterr |
---|
| 320 | #define optopt getopt_data.optopt |
---|
[b4d6afd] | 321 | #define getopt(argc, argv, opt) getopt_r(argc, argv, "+" opt, &getopt_data) |
---|
[c333bab] | 322 | #endif /* __rtems__ */ |
---|
[f1710b6] | 323 | |
---|
| 324 | af = AF_UNSPEC; |
---|
| 325 | |
---|
[c40e45b] | 326 | argc = xo_parse_args(argc, argv); |
---|
| 327 | if (argc < 0) |
---|
| 328 | exit(EXIT_FAILURE); |
---|
| 329 | |
---|
| 330 | while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:RrSTsuWw:xz")) |
---|
[af5333e] | 331 | != -1) |
---|
[f1710b6] | 332 | switch(ch) { |
---|
[7eeb079] | 333 | case '4': |
---|
| 334 | #ifdef INET |
---|
| 335 | af = AF_INET; |
---|
| 336 | #else |
---|
| 337 | errx(1, "IPv4 support is not compiled in"); |
---|
| 338 | #endif |
---|
| 339 | break; |
---|
| 340 | case '6': |
---|
| 341 | #ifdef INET6 |
---|
| 342 | af = AF_INET6; |
---|
| 343 | #else |
---|
| 344 | errx(1, "IPv6 support is not compiled in"); |
---|
| 345 | #endif |
---|
| 346 | break; |
---|
[f1710b6] | 347 | case 'A': |
---|
| 348 | Aflag = 1; |
---|
| 349 | break; |
---|
| 350 | case 'a': |
---|
| 351 | aflag = 1; |
---|
| 352 | break; |
---|
| 353 | case 'B': |
---|
| 354 | Bflag = 1; |
---|
| 355 | break; |
---|
| 356 | case 'b': |
---|
| 357 | bflag = 1; |
---|
| 358 | break; |
---|
| 359 | case 'd': |
---|
| 360 | dflag = 1; |
---|
| 361 | break; |
---|
[c40e45b] | 362 | case 'F': |
---|
| 363 | fib = strtol(optarg, &endptr, 0); |
---|
| 364 | if (*endptr != '\0' || |
---|
| 365 | (fib == 0 && (errno == EINVAL || errno == ERANGE))) |
---|
| 366 | xo_errx(1, "%s: invalid fib", optarg); |
---|
| 367 | break; |
---|
[f1710b6] | 368 | case 'f': |
---|
[c40e45b] | 369 | if (strcmp(optarg, "inet") == 0) |
---|
[f1710b6] | 370 | af = AF_INET; |
---|
| 371 | #ifdef INET6 |
---|
| 372 | else if (strcmp(optarg, "inet6") == 0) |
---|
| 373 | af = AF_INET6; |
---|
| 374 | #endif |
---|
| 375 | #ifdef IPSEC |
---|
| 376 | else if (strcmp(optarg, "pfkey") == 0) |
---|
| 377 | af = PF_KEY; |
---|
| 378 | #endif |
---|
[c40e45b] | 379 | else if (strcmp(optarg, "unix") == 0 || |
---|
| 380 | strcmp(optarg, "local") == 0) |
---|
[f1710b6] | 381 | af = AF_UNIX; |
---|
| 382 | #ifdef NETGRAPH |
---|
| 383 | else if (strcmp(optarg, "ng") == 0 |
---|
| 384 | || strcmp(optarg, "netgraph") == 0) |
---|
| 385 | af = AF_NETGRAPH; |
---|
| 386 | #endif |
---|
| 387 | else if (strcmp(optarg, "link") == 0) |
---|
| 388 | af = AF_LINK; |
---|
| 389 | else { |
---|
[c40e45b] | 390 | xo_errx(1, "%s: unknown address family", |
---|
| 391 | optarg); |
---|
[f1710b6] | 392 | } |
---|
| 393 | break; |
---|
| 394 | case 'g': |
---|
| 395 | gflag = 1; |
---|
| 396 | break; |
---|
| 397 | case 'h': |
---|
| 398 | hflag = 1; |
---|
| 399 | break; |
---|
| 400 | case 'I': { |
---|
| 401 | char *cp; |
---|
| 402 | |
---|
| 403 | iflag = 1; |
---|
[c40e45b] | 404 | #ifndef __rtems__ |
---|
| 405 | for (cp = interface = optarg; isalpha(*cp); cp++) |
---|
| 406 | #else /* __rtems__ */ |
---|
| 407 | for (cp = interface = optarg; isalpha( |
---|
| 408 | (unsigned char) *cp); cp++) |
---|
| 409 | #endif /* __rtems__ */ |
---|
[f1710b6] | 410 | continue; |
---|
| 411 | unit = atoi(cp); |
---|
| 412 | break; |
---|
| 413 | } |
---|
| 414 | case 'i': |
---|
| 415 | iflag = 1; |
---|
| 416 | break; |
---|
| 417 | case 'L': |
---|
| 418 | Lflag = 1; |
---|
| 419 | break; |
---|
| 420 | case 'M': |
---|
| 421 | memf = optarg; |
---|
| 422 | break; |
---|
| 423 | case 'm': |
---|
| 424 | mflag = 1; |
---|
| 425 | break; |
---|
| 426 | case 'N': |
---|
| 427 | nlistf = optarg; |
---|
| 428 | break; |
---|
| 429 | case 'n': |
---|
| 430 | numeric_addr = numeric_port = 1; |
---|
| 431 | break; |
---|
| 432 | case 'p': |
---|
| 433 | if ((tp = name2protox(optarg)) == NULL) { |
---|
[c40e45b] | 434 | xo_errx(1, "%s: unknown or uninstrumented " |
---|
| 435 | "protocol", optarg); |
---|
[f1710b6] | 436 | } |
---|
| 437 | pflag = 1; |
---|
| 438 | break; |
---|
[af5333e] | 439 | #ifndef __rtems__ |
---|
| 440 | case 'Q': |
---|
| 441 | Qflag = 1; |
---|
| 442 | break; |
---|
| 443 | #endif /* __rtems__ */ |
---|
[f1710b6] | 444 | case 'q': |
---|
| 445 | noutputs = atoi(optarg); |
---|
| 446 | if (noutputs != 0) |
---|
| 447 | noutputs++; |
---|
| 448 | break; |
---|
| 449 | case 'r': |
---|
| 450 | rflag = 1; |
---|
| 451 | break; |
---|
[c40e45b] | 452 | case 'R': |
---|
| 453 | Rflag = 1; |
---|
| 454 | break; |
---|
[f1710b6] | 455 | case 's': |
---|
| 456 | ++sflag; |
---|
| 457 | break; |
---|
| 458 | case 'S': |
---|
| 459 | numeric_addr = 1; |
---|
| 460 | break; |
---|
| 461 | case 'u': |
---|
| 462 | af = AF_UNIX; |
---|
| 463 | break; |
---|
| 464 | case 'W': |
---|
| 465 | case 'l': |
---|
| 466 | Wflag = 1; |
---|
| 467 | break; |
---|
| 468 | case 'w': |
---|
| 469 | interval = atoi(optarg); |
---|
| 470 | iflag = 1; |
---|
| 471 | break; |
---|
[af5333e] | 472 | case 'T': |
---|
| 473 | Tflag = 1; |
---|
| 474 | break; |
---|
[f1710b6] | 475 | case 'x': |
---|
| 476 | xflag = 1; |
---|
| 477 | break; |
---|
| 478 | case 'z': |
---|
| 479 | zflag = 1; |
---|
| 480 | break; |
---|
| 481 | case '?': |
---|
| 482 | default: |
---|
| 483 | usage(); |
---|
| 484 | } |
---|
| 485 | argv += optind; |
---|
| 486 | argc -= optind; |
---|
| 487 | |
---|
| 488 | #define BACKWARD_COMPATIBILITY |
---|
| 489 | #ifdef BACKWARD_COMPATIBILITY |
---|
| 490 | if (*argv) { |
---|
[c40e45b] | 491 | #ifndef __rtems__ |
---|
| 492 | if (isdigit(**argv)) { |
---|
| 493 | #else /* __rtems__ */ |
---|
[5898fa1] | 494 | if (isdigit((unsigned char) **argv)) { |
---|
[c40e45b] | 495 | #endif /* __rtems__ */ |
---|
[f1710b6] | 496 | interval = atoi(*argv); |
---|
| 497 | if (interval <= 0) |
---|
| 498 | usage(); |
---|
| 499 | ++argv; |
---|
| 500 | iflag = 1; |
---|
| 501 | } |
---|
| 502 | if (*argv) { |
---|
| 503 | nlistf = *argv; |
---|
| 504 | if (*++argv) |
---|
| 505 | memf = *argv; |
---|
| 506 | } |
---|
| 507 | } |
---|
| 508 | #endif |
---|
| 509 | |
---|
| 510 | /* |
---|
| 511 | * Discard setgid privileges if not the running kernel so that bad |
---|
| 512 | * guys can't print interesting stuff from kernel memory. |
---|
| 513 | */ |
---|
| 514 | live = (nlistf == NULL && memf == NULL); |
---|
[c40e45b] | 515 | if (!live) { |
---|
| 516 | if (setgid(getgid()) != 0) |
---|
| 517 | xo_err(-1, "setgid"); |
---|
| 518 | } |
---|
[f1710b6] | 519 | |
---|
[c40e45b] | 520 | if (xflag && Tflag) |
---|
| 521 | xo_errx(1, "-x and -T are incompatible, pick one."); |
---|
[af5333e] | 522 | |
---|
[95a3ba8] | 523 | /* Load all necessary kvm symbols */ |
---|
| 524 | kresolve_list(nl); |
---|
| 525 | |
---|
[f1710b6] | 526 | if (Bflag) { |
---|
| 527 | if (!live) |
---|
| 528 | usage(); |
---|
| 529 | bpf_stats(interface); |
---|
[c40e45b] | 530 | xo_finish(); |
---|
[f1710b6] | 531 | exit(0); |
---|
| 532 | } |
---|
| 533 | if (mflag) { |
---|
| 534 | if (!live) { |
---|
| 535 | if (kread(0, NULL, 0) == 0) |
---|
[c40e45b] | 536 | mbpr(kvmd, nl[N_SFSTAT].n_value); |
---|
[f1710b6] | 537 | } else |
---|
| 538 | mbpr(NULL, 0); |
---|
[c40e45b] | 539 | xo_finish(); |
---|
[f1710b6] | 540 | exit(0); |
---|
| 541 | } |
---|
[af5333e] | 542 | #ifndef __rtems__ |
---|
| 543 | if (Qflag) { |
---|
| 544 | if (!live) { |
---|
| 545 | if (kread(0, NULL, 0) == 0) |
---|
[c40e45b] | 546 | netisr_stats(); |
---|
[af5333e] | 547 | } else |
---|
[c40e45b] | 548 | netisr_stats(); |
---|
| 549 | xo_finish(); |
---|
[af5333e] | 550 | exit(0); |
---|
| 551 | } |
---|
| 552 | #endif /* __rtems__ */ |
---|
[f1710b6] | 553 | #if 0 |
---|
| 554 | /* |
---|
| 555 | * Keep file descriptors open to avoid overhead |
---|
| 556 | * of open/close on each call to get* routines. |
---|
| 557 | */ |
---|
| 558 | sethostent(1); |
---|
| 559 | setnetent(1); |
---|
| 560 | #else |
---|
| 561 | /* |
---|
| 562 | * This does not make sense any more with DNS being default over |
---|
| 563 | * the files. Doing a setXXXXent(1) causes a tcp connection to be |
---|
| 564 | * used for the queries, which is slower. |
---|
| 565 | */ |
---|
| 566 | #endif |
---|
| 567 | if (iflag && !sflag) { |
---|
[c40e45b] | 568 | xo_open_container("statistics"); |
---|
| 569 | intpr(NULL, af); |
---|
| 570 | xo_close_container("statistics"); |
---|
| 571 | xo_finish(); |
---|
[f1710b6] | 572 | exit(0); |
---|
| 573 | } |
---|
| 574 | if (rflag) { |
---|
[c40e45b] | 575 | xo_open_container("statistics"); |
---|
| 576 | if (sflag) { |
---|
| 577 | rt_stats(); |
---|
| 578 | flowtable_stats(); |
---|
| 579 | } else |
---|
| 580 | routepr(fib, af); |
---|
| 581 | xo_close_container("statistics"); |
---|
| 582 | xo_finish(); |
---|
[f1710b6] | 583 | exit(0); |
---|
| 584 | } |
---|
[c40e45b] | 585 | |
---|
[f1710b6] | 586 | if (gflag) { |
---|
[c40e45b] | 587 | xo_open_container("statistics"); |
---|
[f1710b6] | 588 | if (sflag) { |
---|
| 589 | if (af == AF_INET || af == AF_UNSPEC) |
---|
[c40e45b] | 590 | mrt_stats(); |
---|
[f1710b6] | 591 | #ifdef INET6 |
---|
| 592 | if (af == AF_INET6 || af == AF_UNSPEC) |
---|
[c40e45b] | 593 | mrt6_stats(); |
---|
[f1710b6] | 594 | #endif |
---|
| 595 | } else { |
---|
| 596 | if (af == AF_INET || af == AF_UNSPEC) |
---|
[c40e45b] | 597 | mroutepr(); |
---|
[f1710b6] | 598 | #ifdef INET6 |
---|
| 599 | if (af == AF_INET6 || af == AF_UNSPEC) |
---|
[c40e45b] | 600 | mroute6pr(); |
---|
[f1710b6] | 601 | #endif |
---|
| 602 | } |
---|
[c40e45b] | 603 | xo_close_container("statistics"); |
---|
| 604 | xo_finish(); |
---|
[f1710b6] | 605 | exit(0); |
---|
| 606 | } |
---|
| 607 | |
---|
| 608 | if (tp) { |
---|
[c40e45b] | 609 | xo_open_container("statistics"); |
---|
| 610 | printproto(tp, tp->pr_name, &first); |
---|
| 611 | if (!first) |
---|
| 612 | xo_close_list("socket"); |
---|
| 613 | xo_close_container("statistics"); |
---|
| 614 | xo_finish(); |
---|
[f1710b6] | 615 | exit(0); |
---|
| 616 | } |
---|
[c40e45b] | 617 | |
---|
| 618 | xo_open_container("statistics"); |
---|
[f1710b6] | 619 | if (af == AF_INET || af == AF_UNSPEC) |
---|
| 620 | for (tp = protox; tp->pr_name; tp++) |
---|
[c40e45b] | 621 | printproto(tp, tp->pr_name, &first); |
---|
[f1710b6] | 622 | #ifdef INET6 |
---|
| 623 | if (af == AF_INET6 || af == AF_UNSPEC) |
---|
| 624 | for (tp = ip6protox; tp->pr_name; tp++) |
---|
[c40e45b] | 625 | printproto(tp, tp->pr_name, &first); |
---|
[f1710b6] | 626 | #endif /*INET6*/ |
---|
| 627 | #ifdef IPSEC |
---|
| 628 | if (af == PF_KEY || af == AF_UNSPEC) |
---|
| 629 | for (tp = pfkeyprotox; tp->pr_name; tp++) |
---|
[c40e45b] | 630 | printproto(tp, tp->pr_name, &first); |
---|
[f1710b6] | 631 | #endif /*IPSEC*/ |
---|
| 632 | #ifdef NETGRAPH |
---|
| 633 | if (af == AF_NETGRAPH || af == AF_UNSPEC) |
---|
| 634 | for (tp = netgraphprotox; tp->pr_name; tp++) |
---|
[c40e45b] | 635 | printproto(tp, tp->pr_name, &first); |
---|
[f1710b6] | 636 | #endif /* NETGRAPH */ |
---|
[0fa7dab] | 637 | #ifndef __rtems__ |
---|
[f1710b6] | 638 | if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) |
---|
| 639 | unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value, |
---|
[66659ff] | 640 | nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value, |
---|
[c40e45b] | 641 | nl[N_UNP_SPHEAD].n_value, &first); |
---|
| 642 | #endif /* __rtems__ */ |
---|
| 643 | |
---|
| 644 | if (!first) |
---|
| 645 | xo_close_list("socket"); |
---|
| 646 | xo_close_container("statistics"); |
---|
| 647 | xo_finish(); |
---|
[f1710b6] | 648 | exit(0); |
---|
| 649 | } |
---|
| 650 | |
---|
[c40e45b] | 651 | static int |
---|
| 652 | fetch_stats_internal(const char *sysctlname, u_long off, void *stats, |
---|
| 653 | size_t len, kreadfn_t kreadfn, int zero) |
---|
| 654 | { |
---|
| 655 | int error; |
---|
| 656 | |
---|
| 657 | if (live) { |
---|
| 658 | memset(stats, 0, len); |
---|
| 659 | if (zero) |
---|
| 660 | error = sysctlbyname(sysctlname, NULL, NULL, stats, |
---|
| 661 | len); |
---|
| 662 | else |
---|
| 663 | error = sysctlbyname(sysctlname, stats, &len, NULL, 0); |
---|
| 664 | if (error == -1 && errno != ENOENT) |
---|
| 665 | xo_warn("sysctl %s", sysctlname); |
---|
| 666 | } else { |
---|
| 667 | if (off == 0) |
---|
| 668 | return (1); |
---|
| 669 | error = kreadfn(off, stats, len); |
---|
| 670 | } |
---|
| 671 | return (error); |
---|
| 672 | } |
---|
| 673 | |
---|
| 674 | int |
---|
| 675 | fetch_stats(const char *sysctlname, u_long off, void *stats, |
---|
| 676 | size_t len, kreadfn_t kreadfn) |
---|
| 677 | { |
---|
| 678 | |
---|
| 679 | return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, |
---|
| 680 | zflag)); |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | int |
---|
| 684 | fetch_stats_ro(const char *sysctlname, u_long off, void *stats, |
---|
| 685 | size_t len, kreadfn_t kreadfn) |
---|
| 686 | { |
---|
| 687 | |
---|
| 688 | return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0)); |
---|
| 689 | } |
---|
| 690 | |
---|
[f1710b6] | 691 | /* |
---|
| 692 | * Print out protocol statistics or control blocks (per sflag). |
---|
| 693 | * If the interface was not specifically requested, and the symbol |
---|
| 694 | * is not in the namelist, ignore this one. |
---|
| 695 | */ |
---|
| 696 | static void |
---|
[c40e45b] | 697 | #ifndef __rtems__ |
---|
| 698 | printproto(struct protox *tp, const char *name, bool *first) |
---|
| 699 | #else /* __rtems__ */ |
---|
| 700 | printproto(const struct protox *tp, const char *name, bool *first) |
---|
| 701 | #endif /* __rtems__ */ |
---|
[f1710b6] | 702 | { |
---|
| 703 | void (*pr)(u_long, const char *, int, int); |
---|
| 704 | u_long off; |
---|
[c40e45b] | 705 | bool doingdblocks = false; |
---|
[f1710b6] | 706 | |
---|
| 707 | if (sflag) { |
---|
| 708 | if (iflag) { |
---|
| 709 | if (tp->pr_istats) |
---|
[c40e45b] | 710 | intpr(tp->pr_istats, af); |
---|
[f1710b6] | 711 | else if (pflag) |
---|
[c40e45b] | 712 | xo_message("%s: no per-interface stats routine", |
---|
[f1710b6] | 713 | tp->pr_name); |
---|
| 714 | return; |
---|
| 715 | } else { |
---|
| 716 | pr = tp->pr_stats; |
---|
| 717 | if (!pr) { |
---|
| 718 | if (pflag) |
---|
[c40e45b] | 719 | xo_message("%s: no stats routine", |
---|
[f1710b6] | 720 | tp->pr_name); |
---|
| 721 | return; |
---|
| 722 | } |
---|
| 723 | if (tp->pr_usesysctl && live) |
---|
| 724 | off = 0; |
---|
| 725 | else if (tp->pr_sindex < 0) { |
---|
| 726 | if (pflag) |
---|
[c40e45b] | 727 | xo_message("%s: stats routine doesn't " |
---|
| 728 | "work on cores", tp->pr_name); |
---|
[f1710b6] | 729 | return; |
---|
| 730 | } else |
---|
| 731 | off = nl[tp->pr_sindex].n_value; |
---|
| 732 | } |
---|
| 733 | } else { |
---|
[c40e45b] | 734 | doingdblocks = true; |
---|
[f1710b6] | 735 | pr = tp->pr_cblocks; |
---|
| 736 | if (!pr) { |
---|
| 737 | if (pflag) |
---|
[c40e45b] | 738 | xo_message("%s: no PCB routine", tp->pr_name); |
---|
[f1710b6] | 739 | return; |
---|
| 740 | } |
---|
| 741 | if (tp->pr_usesysctl && live) |
---|
| 742 | off = 0; |
---|
| 743 | else if (tp->pr_index < 0) { |
---|
| 744 | if (pflag) |
---|
[c40e45b] | 745 | xo_message("%s: PCB routine doesn't work on " |
---|
| 746 | "cores", tp->pr_name); |
---|
[f1710b6] | 747 | return; |
---|
| 748 | } else |
---|
| 749 | off = nl[tp->pr_index].n_value; |
---|
| 750 | } |
---|
| 751 | if (pr != NULL && (off || (live && tp->pr_usesysctl) || |
---|
[c40e45b] | 752 | af != AF_UNSPEC)) { |
---|
| 753 | if (doingdblocks && *first) { |
---|
| 754 | xo_open_list("socket"); |
---|
| 755 | *first = false; |
---|
| 756 | } |
---|
| 757 | |
---|
[f1710b6] | 758 | (*pr)(off, name, af, tp->pr_protocol); |
---|
[c40e45b] | 759 | } |
---|
| 760 | } |
---|
| 761 | |
---|
| 762 | static int |
---|
| 763 | kvmd_init(void) |
---|
| 764 | { |
---|
| 765 | char errbuf[_POSIX2_LINE_MAX]; |
---|
| 766 | |
---|
| 767 | if (kvmd != NULL) |
---|
| 768 | return (0); |
---|
| 769 | |
---|
| 770 | kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); |
---|
| 771 | if (setgid(getgid()) != 0) |
---|
| 772 | xo_err(-1, "setgid"); |
---|
| 773 | |
---|
| 774 | if (kvmd == NULL) { |
---|
| 775 | xo_warnx("kvm not available: %s", errbuf); |
---|
| 776 | return (-1); |
---|
| 777 | } |
---|
| 778 | |
---|
| 779 | return (0); |
---|
| 780 | } |
---|
| 781 | |
---|
| 782 | /* |
---|
| 783 | * Resolve symbol list, return 0 on success. |
---|
| 784 | */ |
---|
| 785 | static int |
---|
| 786 | kresolve_list(struct nlist *_nl) |
---|
| 787 | { |
---|
| 788 | |
---|
| 789 | if ((kvmd == NULL) && (kvmd_init() != 0)) |
---|
| 790 | return (-1); |
---|
| 791 | |
---|
| 792 | if (_nl[0].n_type != 0) |
---|
| 793 | return (0); |
---|
| 794 | |
---|
| 795 | if (kvm_nlist(kvmd, _nl) < 0) { |
---|
| 796 | if (nlistf) |
---|
| 797 | xo_errx(1, "%s: kvm_nlist: %s", nlistf, |
---|
| 798 | kvm_geterr(kvmd)); |
---|
| 799 | else |
---|
| 800 | xo_errx(1, "kvm_nlist: %s", kvm_geterr(kvmd)); |
---|
| 801 | } |
---|
| 802 | |
---|
| 803 | return (0); |
---|
| 804 | } |
---|
| 805 | |
---|
| 806 | /* |
---|
| 807 | * Wrapper of kvm_dpcpu_setcpu(). |
---|
| 808 | */ |
---|
| 809 | void |
---|
| 810 | kset_dpcpu(u_int cpuid) |
---|
| 811 | { |
---|
| 812 | |
---|
| 813 | if ((kvmd == NULL) && (kvmd_init() != 0)) |
---|
| 814 | xo_errx(-1, "%s: kvm is not available", __func__); |
---|
| 815 | |
---|
| 816 | if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0) |
---|
| 817 | xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__, |
---|
| 818 | cpuid, kvm_geterr(kvmd)); |
---|
| 819 | return; |
---|
[f1710b6] | 820 | } |
---|
| 821 | |
---|
| 822 | /* |
---|
| 823 | * Read kernel memory, return 0 on success. |
---|
| 824 | */ |
---|
| 825 | int |
---|
| 826 | kread(u_long addr, void *buf, size_t size) |
---|
| 827 | { |
---|
| 828 | |
---|
[c40e45b] | 829 | if (kvmd_init() < 0) |
---|
| 830 | return (-1); |
---|
[f1710b6] | 831 | |
---|
| 832 | if (!buf) |
---|
| 833 | return (0); |
---|
| 834 | if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) { |
---|
[c40e45b] | 835 | xo_warnx("%s", kvm_geterr(kvmd)); |
---|
[f1710b6] | 836 | return (-1); |
---|
| 837 | } |
---|
| 838 | return (0); |
---|
| 839 | } |
---|
| 840 | |
---|
[c40e45b] | 841 | /* |
---|
| 842 | * Read single counter(9). |
---|
| 843 | */ |
---|
| 844 | uint64_t |
---|
| 845 | kread_counter(u_long addr) |
---|
| 846 | { |
---|
| 847 | |
---|
| 848 | if (kvmd_init() < 0) |
---|
| 849 | return (-1); |
---|
| 850 | |
---|
| 851 | return (kvm_counter_u64_fetch(kvmd, addr)); |
---|
| 852 | } |
---|
| 853 | |
---|
| 854 | /* |
---|
| 855 | * Read an array of N counters in kernel memory into array of N uint64_t's. |
---|
| 856 | */ |
---|
| 857 | int |
---|
| 858 | kread_counters(u_long addr, void *buf, size_t size) |
---|
| 859 | { |
---|
| 860 | #ifndef __rtems__ |
---|
| 861 | uint64_t *c; |
---|
| 862 | u_long *counters; |
---|
| 863 | size_t i, n; |
---|
| 864 | |
---|
| 865 | if (kvmd_init() < 0) |
---|
| 866 | return (-1); |
---|
| 867 | |
---|
| 868 | if (size % sizeof(uint64_t) != 0) { |
---|
| 869 | xo_warnx("kread_counters: invalid counter set size"); |
---|
| 870 | return (-1); |
---|
| 871 | } |
---|
| 872 | |
---|
| 873 | n = size / sizeof(uint64_t); |
---|
| 874 | if ((counters = malloc(n * sizeof(u_long))) == NULL) |
---|
| 875 | xo_err(-1, "malloc"); |
---|
| 876 | if (kread(addr, counters, n * sizeof(u_long)) < 0) { |
---|
| 877 | free(counters); |
---|
| 878 | return (-1); |
---|
| 879 | } |
---|
| 880 | |
---|
| 881 | c = buf; |
---|
| 882 | for (i = 0; i < n; i++) |
---|
| 883 | c[i] = kvm_counter_u64_fetch(kvmd, counters[i]); |
---|
| 884 | |
---|
| 885 | free(counters); |
---|
| 886 | return (0); |
---|
| 887 | #else /* __rtems__ */ |
---|
| 888 | return (-1); |
---|
| 889 | #endif /* __rtems__ */ |
---|
| 890 | } |
---|
| 891 | |
---|
[f1710b6] | 892 | const char * |
---|
| 893 | plural(uintmax_t n) |
---|
| 894 | { |
---|
| 895 | return (n != 1 ? "s" : ""); |
---|
| 896 | } |
---|
| 897 | |
---|
| 898 | const char * |
---|
| 899 | plurales(uintmax_t n) |
---|
| 900 | { |
---|
| 901 | return (n != 1 ? "es" : ""); |
---|
| 902 | } |
---|
| 903 | |
---|
| 904 | const char * |
---|
| 905 | pluralies(uintmax_t n) |
---|
| 906 | { |
---|
| 907 | return (n != 1 ? "ies" : "y"); |
---|
| 908 | } |
---|
| 909 | |
---|
| 910 | /* |
---|
| 911 | * Find the protox for the given "well-known" name. |
---|
| 912 | */ |
---|
[c40e45b] | 913 | #ifndef __rtems__ |
---|
| 914 | static struct protox * |
---|
| 915 | #else /* __rtems__ */ |
---|
[4db7af6] | 916 | static const struct protox * |
---|
[c40e45b] | 917 | #endif /* __rtems__ */ |
---|
[f1710b6] | 918 | knownname(const char *name) |
---|
| 919 | { |
---|
[c40e45b] | 920 | #ifndef __rtems__ |
---|
| 921 | struct protox **tpp, *tp; |
---|
| 922 | #else /* __rtems__ */ |
---|
| 923 | const struct protox *const *tpp, *tp; |
---|
| 924 | #endif /* __rtems__ */ |
---|
[f1710b6] | 925 | |
---|
| 926 | for (tpp = protoprotox; *tpp; tpp++) |
---|
| 927 | for (tp = *tpp; tp->pr_name; tp++) |
---|
| 928 | if (strcmp(tp->pr_name, name) == 0) |
---|
| 929 | return (tp); |
---|
| 930 | return (NULL); |
---|
| 931 | } |
---|
| 932 | |
---|
| 933 | /* |
---|
| 934 | * Find the protox corresponding to name. |
---|
| 935 | */ |
---|
[c40e45b] | 936 | #ifndef __rtems__ |
---|
| 937 | static struct protox * |
---|
| 938 | #else /* __rtems__ */ |
---|
[4db7af6] | 939 | static const struct protox * |
---|
[c40e45b] | 940 | #endif /* __rtems__ */ |
---|
[f1710b6] | 941 | name2protox(const char *name) |
---|
| 942 | { |
---|
[c40e45b] | 943 | #ifndef __rtems__ |
---|
| 944 | struct protox *tp; |
---|
| 945 | #else /* __rtems__ */ |
---|
[4db7af6] | 946 | const struct protox *tp; |
---|
[c40e45b] | 947 | #endif /* __rtems__ */ |
---|
[f1710b6] | 948 | char **alias; /* alias from p->aliases */ |
---|
| 949 | struct protoent *p; |
---|
| 950 | |
---|
| 951 | /* |
---|
| 952 | * Try to find the name in the list of "well-known" names. If that |
---|
| 953 | * fails, check if name is an alias for an Internet protocol. |
---|
| 954 | */ |
---|
| 955 | if ((tp = knownname(name)) != NULL) |
---|
| 956 | return (tp); |
---|
| 957 | |
---|
| 958 | setprotoent(1); /* make protocol lookup cheaper */ |
---|
| 959 | while ((p = getprotoent()) != NULL) { |
---|
| 960 | /* assert: name not same as p->name */ |
---|
| 961 | for (alias = p->p_aliases; *alias; alias++) |
---|
| 962 | if (strcmp(name, *alias) == 0) { |
---|
| 963 | endprotoent(); |
---|
| 964 | return (knownname(p->p_name)); |
---|
| 965 | } |
---|
| 966 | } |
---|
| 967 | endprotoent(); |
---|
| 968 | return (NULL); |
---|
| 969 | } |
---|
| 970 | |
---|
| 971 | static void |
---|
| 972 | usage(void) |
---|
| 973 | { |
---|
[c40e45b] | 974 | (void)xo_error("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", |
---|
| 975 | "usage: netstat [-46AaLnRSTWx] [-f protocol_family | -p protocol]\n" |
---|
[f1710b6] | 976 | " [-M core] [-N system]", |
---|
[7eeb079] | 977 | " netstat -i | -I interface [-46abdhnW] [-f address_family]\n" |
---|
[f1710b6] | 978 | " [-M core] [-N system]", |
---|
[c40e45b] | 979 | " netstat -w wait [-I interface] [-46d] [-M core] [-N system]\n" |
---|
| 980 | " [-q howmany]", |
---|
| 981 | " netstat -s [-46sz] [-f protocol_family | -p protocol]\n" |
---|
[f1710b6] | 982 | " [-M core] [-N system]", |
---|
[c40e45b] | 983 | " netstat -i | -I interface -s [-46s]\n" |
---|
| 984 | " [-f protocol_family | -p protocol] [-M core] [-N system]", |
---|
[f1710b6] | 985 | " netstat -m [-M core] [-N system]", |
---|
[c40e45b] | 986 | " netstat -B [-z] [-I interface]", |
---|
| 987 | " netstat -r [-46AnW] [-F fibnum] [-f address_family]\n" |
---|
| 988 | " [-M core] [-N system]", |
---|
[f1710b6] | 989 | " netstat -rs [-s] [-M core] [-N system]", |
---|
[7eeb079] | 990 | " netstat -g [-46W] [-f address_family] [-M core] [-N system]", |
---|
| 991 | " netstat -gs [-46s] [-f address_family] [-M core] [-N system]", |
---|
[af5333e] | 992 | " netstat -Q"); |
---|
[c40e45b] | 993 | xo_finish(); |
---|
[f1710b6] | 994 | exit(1); |
---|
| 995 | } |
---|