| 1 |
/* |
| 2 |
* $OpenBSD: inet.c,v 1.92 2005/02/10 14:25:08 itojun Exp $ |
| 3 |
*/ |
| 4 |
/* |
| 5 |
* $NetBSD: inet.c,v 1.14 1995/10/03 21:42:37 thorpej Exp $ |
| 6 |
*/ |
| 7 |
|
| 8 |
/* |
| 9 |
* Copyright (c) 1983, 1988, 1993 |
| 10 |
* The Regents of the University of California. All rights reserved. |
| 11 |
* |
| 12 |
* Redistribution and use in source and binary forms, with or without |
| 13 |
* modification, are permitted provided that the following conditions |
| 14 |
* are met: |
| 15 |
* 1. Redistributions of source code must retain the above copyright |
| 16 |
* notice, this list of conditions and the following disclaimer. |
| 17 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 18 |
* notice, this list of conditions and the following disclaimer in the |
| 19 |
* documentation and/or other materials provided with the distribution. |
| 20 |
* 3. Neither the name of the University nor the names of its contributors |
| 21 |
* may be used to endorse or promote products derived from this software |
| 22 |
* without specific prior written permission. |
| 23 |
* |
| 24 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 25 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 30 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 33 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 |
* SUCH DAMAGE. |
| 35 |
*/ |
| 36 |
|
| 37 |
#include <net-snmp/net-snmp-config.h> |
| 38 |
|
| 39 |
#include <stdint.h> |
| 40 |
#ifdef HAVE_UNISTD_H |
| 41 |
#include <unistd.h> |
| 42 |
#endif |
| 43 |
#ifdef HAVE_WINSOCK_H |
| 44 |
#include "winstub.h" |
| 45 |
#endif |
| 46 |
#ifdef HAVE_SYS_SOCKET_H |
| 47 |
#include <sys/socket.h> |
| 48 |
#endif |
| 49 |
#ifdef HAVE_NETDB_H |
| 50 |
#include <netdb.h> |
| 51 |
#endif |
| 52 |
#ifdef HAVE_NETINET_IN_H |
| 53 |
#include <netinet/in.h> |
| 54 |
#endif |
| 55 |
#ifdef HAVE_ARPA_INET_H |
| 56 |
#include <arpa/inet.h> |
| 57 |
#endif |
| 58 |
|
| 59 |
#include <net-snmp/net-snmp-includes.h> |
| 60 |
|
| 61 |
#include "main.h" |
| 62 |
#include "netstat.h" |
| 63 |
|
| 64 |
struct stat_table { |
| 65 |
unsigned int entry; /* entry number in table */ |
| 66 |
/* |
| 67 |
* format string to printf(description, value) |
| 68 |
* warning: the %d must be before the %s |
| 69 |
*/ |
| 70 |
char description[80]; |
| 71 |
}; |
| 72 |
|
| 73 |
static char *inetname(const struct in_addr *); |
| 74 |
|
| 75 |
/* |
| 76 |
* Print a summary of connections related to |
| 77 |
* an Internet protocol (kread-based) - Omitted |
| 78 |
*/ |
| 79 |
|
| 80 |
/* |
| 81 |
* Print a summary of TCP connections |
| 82 |
* Listening processes are suppressed unless the |
| 83 |
* -a (all) flag is specified. |
| 84 |
*/ |
| 85 |
const char *tcpstates[] = { |
| 86 |
"", |
| 87 |
"CLOSED", |
| 88 |
"LISTEN", |
| 89 |
"SYNSENT", |
| 90 |
"SYNRECEIVED", |
| 91 |
"ESTABLISHED", |
| 92 |
"FINWAIT1", |
| 93 |
"FINWAIT2", |
| 94 |
"CLOSEWAIT", |
| 95 |
"LASTACK", |
| 96 |
"CLOSING", |
| 97 |
"TIMEWAIT" |
| 98 |
}; |
| 99 |
|
| 100 |
#define TCP_NSTATES 11 |
| 101 |
|
| 102 |
static void |
| 103 |
tcpprotoprint_line(const char *name, netsnmp_variable_list * vp, |
| 104 |
int *first) |
| 105 |
{ |
| 106 |
int state, width; |
| 107 |
char *cp; |
| 108 |
union { |
| 109 |
struct in_addr addr; |
| 110 |
char data[4]; |
| 111 |
} tmpAddr; |
| 112 |
oid localPort, remotePort; |
| 113 |
struct in_addr localAddr, remoteAddr; |
| 114 |
|
| 115 |
state = *vp->val.integer; |
| 116 |
if (!aflag && state == MIB_TCPCONNSTATE_LISTEN) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
if (*first) { |
| 121 |
printf("Active Internet (%s) Connections", name); |
| 122 |
if (aflag) |
| 123 |
printf(" (including servers)"); |
| 124 |
putchar('\n'); |
| 125 |
width = 27; |
| 126 |
printf("%-5.5s %*.*s %*.*s %s\n", |
| 127 |
"Proto", -width, width, "Local Address", |
| 128 |
-width, width, "Remote Address", "(state)"); |
| 129 |
*first = 0; |
| 130 |
} |
| 131 |
|
| 132 |
/* |
| 133 |
* Extract the local/remote information from the index values |
| 134 |
*/ |
| 135 |
cp = tmpAddr.data; |
| 136 |
cp[0] = (uint8_t) vp->name[10]; |
| 137 |
cp[1] = (uint8_t) vp->name[11]; |
| 138 |
cp[2] = (uint8_t) vp->name[12]; |
| 139 |
cp[3] = (uint8_t) vp->name[13]; |
| 140 |
localAddr.s_addr = tmpAddr.addr.s_addr; |
| 141 |
localPort = vp->name[14]; |
| 142 |
cp = tmpAddr.data; |
| 143 |
cp[0] = (uint8_t) vp->name[15]; |
| 144 |
cp[1] = (uint8_t) vp->name[16]; |
| 145 |
cp[2] = (uint8_t) vp->name[17]; |
| 146 |
cp[3] = (uint8_t) vp->name[18]; |
| 147 |
remoteAddr.s_addr = tmpAddr.addr.s_addr; |
| 148 |
remotePort = vp->name[19]; |
| 149 |
|
| 150 |
printf("%-5.5s", name); |
| 151 |
inetprint(&localAddr, localPort, name, 1); |
| 152 |
inetprint(&remoteAddr, remotePort, name, 0); |
| 153 |
if (state < 1 || state > TCP_NSTATES) { |
| 154 |
printf(" %d\n", state); |
| 155 |
} else { |
| 156 |
printf(" %s\n", tcpstates[state]); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
static void |
| 161 |
tcpprotopr_get(const char *name, oid * root, size_t root_len) |
| 162 |
{ |
| 163 |
netsnmp_variable_list *var, *vp; |
| 164 |
int first = 1; |
| 165 |
|
| 166 |
/* |
| 167 |
* Walking the tcpConnState column will provide all |
| 168 |
* the necessary information. |
| 169 |
*/ |
| 170 |
var = NULL; |
| 171 |
snmp_varlist_add_variable(&var, root, root_len, ASN_NULL, NULL, 0); |
| 172 |
if (!var) |
| 173 |
return; |
| 174 |
if (netsnmp_query_walk(var, ss) != SNMP_ERR_NOERROR) |
| 175 |
return; |
| 176 |
if ((var->type & 0xF0) == 0x80) /* Exception */ |
| 177 |
return; |
| 178 |
|
| 179 |
for (vp = var; vp; vp = vp->next_variable) { |
| 180 |
tcpprotoprint_line(name, vp, &first); |
| 181 |
} |
| 182 |
snmp_free_varbind(var); |
| 183 |
} |
| 184 |
|
| 185 |
/* |
| 186 |
* Print a summary of UDP "connections" |
| 187 |
* XXX - what about "listening" services ?? |
| 188 |
*/ |
| 189 |
void |
| 190 |
udpprotopr(const char *name) |
| 191 |
{ |
| 192 |
netsnmp_variable_list *var, *vp; |
| 193 |
oid udpLocalAddress_oid[] = |
| 194 |
{ 1, 3, 6, 1, 2, 1, 7, 5, 1, 1 }; |
| 195 |
size_t udpLocalAddress_len = OID_LENGTH(udpLocalAddress_oid); |
| 196 |
union { |
| 197 |
struct in_addr addr; |
| 198 |
char data[4]; |
| 199 |
} tmpAddr; |
| 200 |
struct in_addr localAddr; |
| 201 |
oid localPort; |
| 202 |
char *cp; |
| 203 |
|
| 204 |
/* |
| 205 |
* Walking a single column of the udpTable will provide |
| 206 |
* all the necessary information from the index values. |
| 207 |
*/ |
| 208 |
var = NULL; |
| 209 |
snmp_varlist_add_variable(&var, udpLocalAddress_oid, |
| 210 |
udpLocalAddress_len, ASN_NULL, NULL, 0); |
| 211 |
if (!var) |
| 212 |
return; |
| 213 |
if (netsnmp_query_walk(var, ss) != SNMP_ERR_NOERROR) { |
| 214 |
snmp_free_varbind(var); |
| 215 |
return; |
| 216 |
} |
| 217 |
if ((var->type & 0xF0) == 0x80) { /* Exception */ |
| 218 |
snmp_free_varbind(var); |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
printf("Active Internet (%s) Connections\n", name); |
| 223 |
printf("%-5.5s %-27.27s\n", "Proto", "Local Address"); |
| 224 |
for (vp = var; vp; vp = vp->next_variable) { |
| 225 |
printf("%-5.5s", name); |
| 226 |
/* |
| 227 |
* Extract the local port from the index values, but take |
| 228 |
* the IP address from the varbind value, (which is why |
| 229 |
* we walked udpLocalAddress rather than udpLocalPort) |
| 230 |
*/ |
| 231 |
cp = tmpAddr.data; |
| 232 |
cp[0] = (uint8_t) vp->name[10]; |
| 233 |
cp[1] = (uint8_t) vp->name[11]; |
| 234 |
cp[2] = (uint8_t) vp->name[12]; |
| 235 |
cp[3] = (uint8_t) vp->name[13]; |
| 236 |
localAddr.s_addr = tmpAddr.addr.s_addr; |
| 237 |
localPort = vp->name[14]; |
| 238 |
inetprint(&localAddr, localPort, name, 1); |
| 239 |
putchar('\n'); |
| 240 |
} |
| 241 |
snmp_free_varbind(var); |
| 242 |
} |
| 243 |
|
| 244 |
void |
| 245 |
tcpprotopr_bulkget(const char *name, oid * root, size_t root_len) |
| 246 |
{ |
| 247 |
netsnmp_variable_list *vp; |
| 248 |
netsnmp_pdu *pdu, *response; |
| 249 |
oid tcpConnState_oid[MAX_OID_LEN]; |
| 250 |
size_t tcpConnState_len; |
| 251 |
int first = 1; |
| 252 |
int running = 1; |
| 253 |
int status; |
| 254 |
|
| 255 |
/* |
| 256 |
* setup initial object name |
| 257 |
*/ |
| 258 |
memmove(tcpConnState_oid, root, sizeof(oid) * root_len); |
| 259 |
tcpConnState_len = root_len; |
| 260 |
|
| 261 |
/* |
| 262 |
* Walking the tcpConnState column will provide all |
| 263 |
* the necessary information. |
| 264 |
*/ |
| 265 |
while (running) { |
| 266 |
/* |
| 267 |
* create PDU for GETBULK request and add object name to request |
| 268 |
*/ |
| 269 |
pdu = snmp_pdu_create(SNMP_MSG_GETBULK); |
| 270 |
pdu->non_repeaters = 0; |
| 271 |
pdu->max_repetitions = max_getbulk; /* fill the packet */ |
| 272 |
snmp_add_null_var(pdu, tcpConnState_oid, tcpConnState_len); |
| 273 |
|
| 274 |
/* |
| 275 |
* do the request |
| 276 |
*/ |
| 277 |
status = snmp_synch_response(ss, pdu, &response); |
| 278 |
if (status == STAT_SUCCESS) { |
| 279 |
if (response->errstat == SNMP_ERR_NOERROR) { |
| 280 |
for (vp = response->variables; vp; vp = vp->next_variable) { |
| 281 |
if ((vp->name_length < root_len) || |
| 282 |
(memcmp(root, vp->name, sizeof(oid) * root_len) != |
| 283 |
0)) { |
| 284 |
/* |
| 285 |
* not part of this subtree |
| 286 |
*/ |
| 287 |
running = 0; |
| 288 |
continue; |
| 289 |
} |
| 290 |
|
| 291 |
if ((vp->type != SNMP_ENDOFMIBVIEW) && |
| 292 |
(vp->type != SNMP_NOSUCHOBJECT) && |
| 293 |
(vp->type != SNMP_NOSUCHINSTANCE)) { |
| 294 |
|
| 295 |
tcpprotoprint_line(name, vp, &first); |
| 296 |
|
| 297 |
/* |
| 298 |
* Check if last variable, and if so, save for next request. |
| 299 |
*/ |
| 300 |
if (vp->next_variable == NULL) { |
| 301 |
memmove(tcpConnState_oid, vp->name, |
| 302 |
vp->name_length * sizeof(oid)); |
| 303 |
tcpConnState_len = vp->name_length; |
| 304 |
} |
| 305 |
} else { |
| 306 |
/* |
| 307 |
* an exception value, so stop |
| 308 |
*/ |
| 309 |
running = 0; |
| 310 |
} |
| 311 |
} |
| 312 |
} else { |
| 313 |
/* |
| 314 |
* error in response, print it |
| 315 |
*/ |
| 316 |
running = 0; |
| 317 |
} |
| 318 |
} else if (status == STAT_TIMEOUT) { |
| 319 |
running = 0; |
| 320 |
} else { /* status == STAT_ERROR */ |
| 321 |
running = 0; |
| 322 |
} |
| 323 |
|
| 324 |
if (response) { |
| 325 |
snmp_free_pdu(response); |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
void |
| 331 |
tcpprotopr(const char *name) |
| 332 |
{ |
| 333 |
oid tcpConnState_oid[] = { 1, 3, 6, 1, 2, 1, 6, 13, 1, 1 }; |
| 334 |
size_t tcpConnState_len = OID_LENGTH(tcpConnState_oid); |
| 335 |
int use_getbulk = 1; |
| 336 |
|
| 337 |
#ifndef NETSNMP_DISABLE_SNMPV1 |
| 338 |
if (ss->version == SNMP_VERSION_1) { |
| 339 |
use_getbulk = 0; |
| 340 |
} |
| 341 |
#endif |
| 342 |
|
| 343 |
if (use_getbulk) { |
| 344 |
tcpprotopr_bulkget(name, tcpConnState_oid, tcpConnState_len); |
| 345 |
} else { |
| 346 |
tcpprotopr_get(name, tcpConnState_oid, tcpConnState_len); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
/********************* |
| 352 |
* |
| 353 |
* Internet-protocol statistics |
| 354 |
* |
| 355 |
*********************/ |
| 356 |
|
| 357 |
void |
| 358 |
_dump_stats(const char *name, oid * oid_buf, size_t buf_len, |
| 359 |
struct stat_table *stable) |
| 360 |
{ |
| 361 |
netsnmp_variable_list *var, *vp; |
| 362 |
struct stat_table *sp; |
| 363 |
oid stat; |
| 364 |
|
| 365 |
var = NULL; |
| 366 |
for (sp = stable; sp->entry; sp++) { |
| 367 |
oid_buf[buf_len - 2] = sp->entry; |
| 368 |
snmp_varlist_add_variable(&var, oid_buf, buf_len, |
| 369 |
ASN_NULL, NULL, 0); |
| 370 |
} |
| 371 |
|
| 372 |
if (netsnmp_query_get(var, ss) != SNMP_ERR_NOERROR) { |
| 373 |
/* |
| 374 |
* Need to fix and re-try SNMPv1 errors |
| 375 |
*/ |
| 376 |
snmp_free_varbind(var); |
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
printf("%s:\n", name); |
| 381 |
sp = stable; |
| 382 |
for (vp = var; vp; vp = vp->next_variable, sp++) { |
| 383 |
/* |
| 384 |
* Match the returned results against |
| 385 |
* the original stats table. |
| 386 |
*/ |
| 387 |
stat = vp->name[buf_len - 2]; |
| 388 |
while (sp->entry < stat) { |
| 389 |
sp++; |
| 390 |
if (sp->entry == 0) |
| 391 |
break; |
| 392 |
} |
| 393 |
if (sp->entry > stat) |
| 394 |
continue; |
| 395 |
|
| 396 |
/* |
| 397 |
* Skip exceptions or missing values |
| 398 |
*/ |
| 399 |
if (!vp->val.integer) |
| 400 |
continue; |
| 401 |
/* |
| 402 |
* If '-Cs' was specified twice, |
| 403 |
* then only display non-zero stats. |
| 404 |
*/ |
| 405 |
if (*vp->val.integer > 0 || sflag == 1) { |
| 406 |
printf(sp->description, *vp->val.integer, |
| 407 |
plural(*vp->val.integer)); |
| 408 |
putchar('\n'); |
| 409 |
} |
| 410 |
} |
| 411 |
snmp_free_varbind(var); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
/* |
| 416 |
* Dump IP statistics. |
| 417 |
*/ |
| 418 |
void |
| 419 |
ip_stats(const char *name) |
| 420 |
{ |
| 421 |
oid ipstats_oid[] = { 1, 3, 6, 1, 2, 1, 4, 0, 0 }; |
| 422 |
size_t ipstats_len = OID_LENGTH(ipstats_oid); |
| 423 |
struct stat_table ipstats_tbl[] = { |
| 424 |
{3, "%14lu total datagram%s received"}, |
| 425 |
{4, "%14lu datagram%s with header errors"}, |
| 426 |
{5, "%14lu datagram%s with an invalid destination address"}, |
| 427 |
{6, "%14lu datagram%s forwarded"}, |
| 428 |
{7, "%14lu datagram%s with unknown protocol"}, |
| 429 |
{8, "%14lu datagram%s discarded"}, |
| 430 |
{9, "%14lu datagram%s delivered"}, |
| 431 |
{10, "%14lu output datagram request%s"}, |
| 432 |
{11, "%14lu output datagram%s discarded"}, |
| 433 |
{12, "%14lu datagram%s with no route"}, |
| 434 |
{14, "%14lu fragment%s received"}, |
| 435 |
{15, "%14lu datagram%s reassembled"}, |
| 436 |
{16, "%14lu reassembly failure%s"}, |
| 437 |
{17, "%14lu datagram%s fragmented"}, |
| 438 |
{18, "%14lu fragmentation failure%s"}, |
| 439 |
{19, "%14lu fragment%s created"}, |
| 440 |
{23, "%14lu route%s discarded"}, |
| 441 |
{0, ""} |
| 442 |
}; |
| 443 |
|
| 444 |
_dump_stats(name, ipstats_oid, ipstats_len, ipstats_tbl); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
/* |
| 449 |
* Dump ICMP statistics. |
| 450 |
*/ |
| 451 |
void |
| 452 |
icmp_stats(const char *name) |
| 453 |
{ |
| 454 |
oid icmpstats_oid[] = { 1, 3, 6, 1, 2, 1, 5, 0, 0 }; |
| 455 |
size_t icmpstats_len = OID_LENGTH(icmpstats_oid); |
| 456 |
struct stat_table icmpstats_tbl[] = { |
| 457 |
{1, "%14lu total message%s received"}, |
| 458 |
{2, "%14lu message%s dropped due to errors"}, |
| 459 |
{14, "%14lu ouput message request%s"}, |
| 460 |
{15, "%14lu output message%s discarded"}, |
| 461 |
{0, ""} |
| 462 |
}; |
| 463 |
struct stat_table icmp_inhistogram[] = { |
| 464 |
{3, " Destination unreachable: %lu"}, |
| 465 |
{4, " Time Exceeded: %lu"}, |
| 466 |
{5, " Parameter Problem: %lu"}, |
| 467 |
{6, " Source Quench: %lu"}, |
| 468 |
{7, " Redirect: %lu"}, |
| 469 |
{8, " Echo Request: %lu"}, |
| 470 |
{9, " Echo Reply: %lu"}, |
| 471 |
{10, " Timestamp Request: %lu"}, |
| 472 |
{11, " Timestamp Reply: %lu"}, |
| 473 |
{12, " Address Mask Request: %lu"}, |
| 474 |
{13, " Address Mask Reply: %lu"}, |
| 475 |
{0, ""} |
| 476 |
}; |
| 477 |
struct stat_table icmp_outhistogram[] = { |
| 478 |
{16, " Destination unreachable: %lu"}, |
| 479 |
{17, " Time Exceeded: %lu"}, |
| 480 |
{18, " Parameter Problem: %lu"}, |
| 481 |
{19, " Source Quench: %lu"}, |
| 482 |
{20, " Redirect: %lu"}, |
| 483 |
{21, " Echo Request: %lu"}, |
| 484 |
{22, " Echo Reply: %lu"}, |
| 485 |
{23, " Timestamp Request: %lu"}, |
| 486 |
{24, " Timestamp Reply: %lu"}, |
| 487 |
{25, " Address Mask Request: %lu"}, |
| 488 |
{26, " Address Mask Reply: %lu"}, |
| 489 |
{0, ""} |
| 490 |
}; |
| 491 |
|
| 492 |
_dump_stats(name, icmpstats_oid, icmpstats_len, icmpstats_tbl); |
| 493 |
_dump_stats(" Input Histogram", |
| 494 |
icmpstats_oid, icmpstats_len, icmp_inhistogram); |
| 495 |
_dump_stats(" Output Histogram", |
| 496 |
icmpstats_oid, icmpstats_len, icmp_outhistogram); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
/* |
| 501 |
* Dump TCP statistics. |
| 502 |
*/ |
| 503 |
void |
| 504 |
tcp_stats(const char *name) |
| 505 |
{ |
| 506 |
oid tcpstats_oid[] = { 1, 3, 6, 1, 2, 1, 6, 0, 0 }; |
| 507 |
size_t tcpstats_len = OID_LENGTH(tcpstats_oid); |
| 508 |
struct stat_table tcpstats_tbl[] = { |
| 509 |
{5, "%14lu active open%s"}, |
| 510 |
{6, "%14lu passive open%s"}, |
| 511 |
{7, "%14lu failed attempt%s"}, |
| 512 |
{8, "%14lu reset%s of established connections"}, |
| 513 |
{9, "%14lu currently established connection%s"}, |
| 514 |
{10, "%14lu segment%s received"}, |
| 515 |
{11, "%14lu segment%s sent"}, |
| 516 |
{12, "%14lu segment%s retransmitted"}, |
| 517 |
{14, "%14lu invalid segment%s received"}, |
| 518 |
{15, "%14lu reset%s sent"}, |
| 519 |
{0, ""} |
| 520 |
}; |
| 521 |
_dump_stats("tcp", tcpstats_oid, tcpstats_len, tcpstats_tbl); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
/* |
| 526 |
* Dump UDP statistics. |
| 527 |
*/ |
| 528 |
void |
| 529 |
udp_stats(const char *name) |
| 530 |
{ |
| 531 |
oid udpstats_oid[] = { 1, 3, 6, 1, 2, 1, 7, 0, 0 }; |
| 532 |
size_t udpstats_len = OID_LENGTH(udpstats_oid); |
| 533 |
struct stat_table udpstats_tbl[] = { |
| 534 |
{1, "%14lu total datagram%s received"}, |
| 535 |
{2, "%14lu datagram%s to invalid port"}, |
| 536 |
{3, "%14lu datagram%s dropped due to errors"}, |
| 537 |
{4, "%14lu output datagram request%s"}, |
| 538 |
{0, ""} |
| 539 |
}; |
| 540 |
_dump_stats("udp", udpstats_oid, udpstats_len, udpstats_tbl); |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
/* |
| 545 |
* Omitted: |
| 546 |
* Dump IGMP statistics |
| 547 |
* Dump PIM statistics |
| 548 |
* Dump AH statistics |
| 549 |
* Dump etherip statistics |
| 550 |
* Dump ESP statistics |
| 551 |
* Dump IP-in-IP statistics |
| 552 |
* Dump CARP statistics |
| 553 |
* Dump pfsync statistics |
| 554 |
* Dump IPCOMP statistics |
| 555 |
*/ |
| 556 |
|
| 557 |
/* |
| 558 |
* Utility routines |
| 559 |
*/ |
| 560 |
|
| 561 |
/* |
| 562 |
* Translation of RPC service names - Omitted |
| 563 |
*/ |
| 564 |
|
| 565 |
/* |
| 566 |
* Pretty print an Internet address (net address + port). |
| 567 |
* If the nflag was specified, use numbers instead of names. |
| 568 |
*/ |
| 569 |
void |
| 570 |
inetprint(const struct in_addr *in, int port, const char *proto, int local) |
| 571 |
{ |
| 572 |
struct servent *sp = NULL; |
| 573 |
char line[80], *cp; |
| 574 |
int width = 27; |
| 575 |
|
| 576 |
if (vflag) |
| 577 |
snprintf(line, sizeof line, "%s.", inetname(in)); |
| 578 |
else |
| 579 |
snprintf(line, sizeof line, "%.*s.", width - 9, inetname(in)); |
| 580 |
cp = strchr(line, '\0'); |
| 581 |
if (!nflag && port) |
| 582 |
sp = getservbyport(htons((uint16_t) port), proto); |
| 583 |
if (sp || port == 0) |
| 584 |
snprintf(cp, line + sizeof line - cp, vflag ? "%s" : "%.8s", |
| 585 |
sp ? sp->s_name : "*"); |
| 586 |
/* |
| 587 |
* Translation of RPC service names - Omitted |
| 588 |
*/ |
| 589 |
else |
| 590 |
snprintf(cp, line + sizeof line - cp, "%d", port); |
| 591 |
if (vflag && width < strlen(line)) |
| 592 |
width = strlen(line); |
| 593 |
printf(" %-*.*s", width, width, line); |
| 594 |
} |
| 595 |
|
| 596 |
/* |
| 597 |
* Construct an Internet address representation. |
| 598 |
* If the nflag has been supplied, give |
| 599 |
* numeric value, otherwise try for symbolic name. |
| 600 |
*/ |
| 601 |
char * |
| 602 |
inetname(const struct in_addr *inp) |
| 603 |
{ |
| 604 |
char *cp; |
| 605 |
static char line[50]; |
| 606 |
struct netent *np; |
| 607 |
static char domain[MAXHOSTNAMELEN]; |
| 608 |
static int first = 1; |
| 609 |
|
| 610 |
if (first && !nflag) { |
| 611 |
char tmp[MAXHOSTNAMELEN]; |
| 612 |
first = 0; |
| 613 |
if (gethostname(tmp, sizeof(tmp)) == 0 && (cp = strchr(tmp, '.'))) |
| 614 |
(void) strlcpy(domain, cp + 1, sizeof domain); |
| 615 |
else |
| 616 |
domain[0] = '\0'; |
| 617 |
} |
| 618 |
if (inp->s_addr == INADDR_ANY) { |
| 619 |
strlcpy(line, "*", sizeof(line)); |
| 620 |
} else { |
| 621 |
char host[16]; |
| 622 |
|
| 623 |
cp = NULL; |
| 624 |
if (!nflag && inet_lnaof(*inp) == INADDR_ANY) { |
| 625 |
np = getnetbyaddr(inet_netof(*inp), AF_INET); |
| 626 |
if (np) |
| 627 |
cp = np->n_name; |
| 628 |
} |
| 629 |
if (cp == NULL) { |
| 630 |
struct sockaddr_in sin; |
| 631 |
|
| 632 |
memset(&sin, 0, sizeof(sin)); |
| 633 |
sin.sin_family = AF_INET; |
| 634 |
sin.sin_addr = *inp; |
| 635 |
if (getnameinfo((struct sockaddr *) &sin, sizeof(sin), |
| 636 |
host, sizeof(host), NULL, 0, |
| 637 |
nflag ? NI_NUMERICHOST : 0) < 0) |
| 638 |
strlcpy(host, "?", sizeof(host)); |
| 639 |
if ((cp = strchr(host, '.')) && !strcmp(cp + 1, domain)) |
| 640 |
*cp = '\0'; |
| 641 |
#if defined (WIN32) || defined (cygwin) |
| 642 |
/* |
| 643 |
* Windows insists on returning the computer name for |
| 644 |
* 127.0.0.1 even if the hosts file lists something |
| 645 |
* else such as 'localhost'. If we are trying to look |
| 646 |
* up 127.0.0.1, just return 'localhost'. |
| 647 |
*/ |
| 648 |
if (inp->s_addr == htonl(INADDR_LOOPBACK)) |
| 649 |
strlcpy(host, "localhost", sizeof(host)); |
| 650 |
#endif |
| 651 |
cp = host; |
| 652 |
} |
| 653 |
strlcpy(line, cp, sizeof(line)); |
| 654 |
} |
| 655 |
return line; |
| 656 |
} |