1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
/***********************************************************
Copyright 1989 by Carnegie Mellon University
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of CMU not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
******************************************************************/
/*
* Copyright (c) 1983,1988 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that this notice is preserved and that due credit is given
* to the University of California at Berkeley. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission. This software
* is provided ``as is'' without express or implied warranty.
*/
#include <strings.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "main.h"
#include "asn1.h"
#include "snmp.h"
#include "snmp_impl.h"
#include "snmp_api.h"
#include "snmp_client.h"
#include "mib.h"
extern int aflag;
extern int nflag;
extern char *plural();
extern char *malloc();
extern struct snmp_session *Session;
extern struct variable_list *getvarbyname();
char *inetname();
static void inetprint();
struct stat_table {
int entry; /* entry number in table */
/* format string to printf(description, value, plural(value)); */
/* warning: the %d must be before the %s */
char description[80];
};
static oid oid_ipstats[] = {1, 3, 6, 1, 2, 1, 4, 0, 0};
struct stat_table ip_stattab[] = {
{3, "%d total datagram%s received"},
{4, "%d datagram%s with header errors"},
{5, "%d datagram%s with an invalid destination address"},
{6, "%d datagram%s forwarded"},
{7, "%d datagram%s with unknown protocol"},
{8, "%d datagram%s discarded"},
{9, "%d datagram%s delivered"},
{10, "%d output datagram request%s"},
{11, "%d output datagram%s discarded"},
{12, "%d datagram%s with no route"},
{14, "%d fragment%s received"},
{15, "%d datagram%s reassembled"},
{16, "%d reassembly failure%s"},
{17, "%d datagram%s fragmented"},
{18, "%d fragmentation failure%s"},
{19, "%d fragment%s created"}
};
static oid oid_udpstats[] = {1, 3, 6, 1, 2, 1, 7, 0, 0};
struct stat_table udp_stattab[] = {
{1, "%d total datagram%s received"},
{2, "%d datagram%s to invalid port"},
{3, "%d datagram%s dropped due to errors"},
{4, "%d output datagram request%s"}
};
static oid oid_tcpstats[] = {1, 3, 6, 1, 2, 1, 6, 0, 0};
struct stat_table tcp_stattab[] = {
{5, "%d active open%s"},
{6, "%d passive open%s"},
{7, "%d failed attempt%s"},
{8, "%d reset%s of established connections"},
{9, "%d current established connection%s"},
{10, "%d segment%s received"},
{11, "%d segment%s sent"},
{12, "%d segment%s retransmitted"}
};
static oid oid_icmpstats[] = {1, 3, 6, 1, 2, 1, 5, 0, 0};
struct stat_table icmp_stattab[] = {
{1, "%d total message%s received"},
{2, "%d message%s dropped due to errors"},
{14, "%d ouput message request%s"},
{15, "%d output message%s discarded"}
};
struct stat_table icmp_inhistogram[] = {
{3, "Destination unreachable: %d"},
{4, "Time Exceeded: %d"},
{5, "Parameter Problem: %d"},
{6, "Source Quench: %d"},
{7, "Redirect: %d"},
{8, "Echo Request: %d"},
{9, "Echo Reply: %d"},
{10, "Timestamp Request: %d"},
{11, "Timestamp Reply: %d"},
{12, "Address Mask Request: %d"},
{13, "Addrss Mask Reply:%d"},
};
struct stat_table icmp_outhistogram[] = {
{16, "Destination unreachable: %d"},
{17, "Time Exceeded: %d"},
{18, "Parameter Problem: %d"},
{19, "Source Quench: %d"},
{20, "Redirect: %d"},
{21, "Echo Request: %d"},
{22, "Echo Reply: %d"},
{23, "Timestamp Request: %d"},
{24, "Timestamp Reply: %d"},
{25, "Address Mask Request: %d"},
{26, "Addrss Mask Reply:%d"},
};
struct tcpconn_entry {
oid instance[10];
struct in_addr localAddress;
int locAddrSet;
u_short localPort;
int locPortSet;
struct in_addr remoteAddress;
int remAddrSet;
u_short remotePort;
int remPortSet;
int state;
int stateSet;
struct tcpconn_entry *next;
};
#define TCPCONN_STATE 1
#define TCPCONN_LOCADDR 2
#define TCPCONN_LOCPORT 3
#define TCPCONN_REMADDR 4
#define TCPCONN_REMPORT 5
static oid oid_tcpconntable[] = {1, 3, 6, 1, 2, 1, 6, 13, 1};
#define ENTRY 9
char *tcpstates[] = {
"", "CLOSED", "LISTEN", "SYNSENT",
"SYNRECEIVED", "ESTABLISHED", "FINWAIT1", "FINWAIT2",
"CLOSEWAIT", "LASTACK", "CLOSING", "TIMEWAIT"
};
#define TCP_NSTATES 11
/*
* Print a summary of connections related to an Internet
* protocol (currently only TCP). For TCP, also give state of connection.
*/
void
protopr(){
struct tcpconn_entry *tcpconn = NULL, *tp, *newp;
struct snmp_pdu *request, *response;
struct variable_list *vp;
oid *instance;
int first, status;
request = snmp_pdu_create(GETNEXT_REQ_MSG);
snmp_add_null_var(request, oid_tcpconntable, sizeof(oid_tcpconntable)/sizeof(oid));
while(1){
status = snmp_synch_response(Session, request, &response);
if (status != STAT_SUCCESS || response->errstat != SNMP_ERR_NOERROR){
fprintf(stderr, "SNMP request failed\n");
break;
}
vp = response->variables;
if (vp->name_length != 20 ||
bcmp((char *)vp->name, (char *)oid_tcpconntable, sizeof(oid_tcpconntable))){
break;
}
request = snmp_pdu_create(GETNEXT_REQ_MSG);
snmp_add_null_var(request, vp->name, vp->name_length);
instance = vp->name + 10;
for(tp = tcpconn; tp != NULL; tp = tp->next){
if (!bcmp((char *)instance, (char *)tp->instance,
sizeof(tp->instance)))
break;
}
if (tp == NULL){
newp = (struct tcpconn_entry *)malloc(sizeof(struct tcpconn_entry));
if (tcpconn == NULL){
tcpconn = newp;
} else {
for(tp = tcpconn; tp->next != NULL; tp = tp->next)
;
tp->next = newp;
}
tp = newp;
bzero((char *)tp, sizeof(*tp));
tp->next = NULL;
bcopy((char *)instance, (char *)tp->instance, sizeof(tp->instance));
}
if (vp->name[ENTRY] == TCPCONN_STATE){
tp->state = *vp->val.integer;
tp->stateSet = 1;
}
if (vp->name[ENTRY] == TCPCONN_LOCADDR){
bcopy((char *)vp->val.string, (char *)&tp->localAddress, sizeof(u_long));
tp->locAddrSet = 1;
}
if (vp->name[ENTRY] == TCPCONN_LOCPORT){
tp->localPort = *vp->val.integer;
tp->locPortSet = 1;
}
if (vp->name[ENTRY] == TCPCONN_REMADDR){
bcopy((char *)vp->val.string, (char *)&tp->remoteAddress, sizeof(u_long));
tp->remAddrSet = 1;
}
if (vp->name[ENTRY] == TCPCONN_REMPORT){
tp->remotePort = *vp->val.integer;
tp->remPortSet = 1;
}
}
for(first = 1, tp = tcpconn; tp != NULL; tp = tp->next){
if (!(tp->stateSet && tp->locAddrSet
&& tp->locPortSet && tp->remAddrSet && tp->remPortSet)){
printf("incomplete entry\n");
continue;
}
if (!aflag && tp->state == MIB_TCPCONNSTATE_LISTEN)
continue;
if (first){
printf("Active Internet Connections");
if (aflag)
printf(" (including servers)");
putchar('\n');
printf("%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %s\n",
"Proto", "Recv-Q", "Send-Q",
"Local Address", "Foreign Address", "(state)");
first = 0;
}
printf("%-5.5s %6d %6d ", "tcp", 0, 0);
inetprint(&tp->localAddress, tp->localPort, "tcp");
inetprint(&tp->remoteAddress, tp->remotePort, "tcp");
if (tp->state < 1 || tp->state > TCP_NSTATES)
printf(" %d", tp->state);
else
printf(" %s", tcpstates[tp->state]);
putchar('\n');
}
}
/*
* Dump UDP statistics structure.
*/
void
udp_stats()
{
oid varname[MAX_NAME_LEN], *udpentry;
int varname_len;
struct variable_list *var;
int count;
struct stat_table *sp = udp_stattab;
bcopy((char *)oid_udpstats, (char *)varname, sizeof(oid_udpstats));
varname_len = sizeof(oid_udpstats) / sizeof(oid);
udpentry = varname + 7;
printf("udp:\n");
count = sizeof(udp_stattab) / sizeof (struct stat_table);
while (count--){
*udpentry = sp->entry;
var = getvarbyname(Session, varname, varname_len);
if (var){
putchar('\t');
printf(sp->description, *var->val.integer, plural((int)*var->val.integer));
putchar('\n');
}
sp++;
}
}
/*
* Dump TCP statistics structure.
*/
void
tcp_stats()
{
oid varname[MAX_NAME_LEN], *tcpentry;
int varname_len;
struct variable_list *var;
int count;
struct stat_table *sp = tcp_stattab;
bcopy((char *)oid_tcpstats, (char *)varname, sizeof(oid_tcpstats));
varname_len = sizeof(oid_tcpstats) / sizeof(oid);
tcpentry = varname + 7;
printf("tcp:\n");
count = sizeof(tcp_stattab) / sizeof (struct stat_table);
while (count--){
*tcpentry = sp->entry;
var = getvarbyname(Session, varname, varname_len);
if (var){
putchar('\t');
printf(sp->description, *var->val.integer, plural((int)*var->val.integer));
putchar('\n');
}
sp++;
}
}
/*
* Dump IP statistics structure.
*/
void
ip_stats()
{
oid varname[MAX_NAME_LEN], *ipentry;
int varname_len;
struct variable_list *var;
int count;
struct stat_table *sp = ip_stattab;
bcopy((char *)oid_ipstats, (char *)varname, sizeof(oid_ipstats));
varname_len = sizeof(oid_ipstats) / sizeof(oid);
ipentry = varname + 7;
printf("ip:\n");
count = sizeof(ip_stattab) / sizeof (struct stat_table);
while (count--){
*ipentry = sp->entry;
var = getvarbyname(Session, varname, varname_len);
if (var){
putchar('\t');
printf(sp->description, *var->val.integer, plural((int)*var->val.integer));
putchar('\n');
}
sp++;
}
}
/*
* Dump ICMP statistics.
*/
void
icmp_stats()
{
oid varname[MAX_NAME_LEN], *icmpentry;
int varname_len;
struct variable_list *var;
int count, first;
struct stat_table *sp;
bcopy((char *)oid_icmpstats, (char *)varname, sizeof(oid_icmpstats));
varname_len = sizeof(oid_icmpstats) / sizeof(oid);
icmpentry = varname + 7;
printf("icmp:\n");
sp = icmp_stattab;
count = sizeof(icmp_stattab) / sizeof (struct stat_table);
while (count--){
*icmpentry = sp->entry;
var = getvarbyname(Session, varname, varname_len);
if (var){
putchar('\t');
printf(sp->description, *var->val.integer, plural((int)*var->val.integer));
putchar('\n');
}
sp++;
}
sp = icmp_outhistogram;
first = 1;
count = sizeof(icmp_outhistogram) / sizeof (struct stat_table);
while (count--){
*icmpentry = sp->entry;
var = getvarbyname(Session, varname, varname_len);
if (var && *var->val.integer != 0){
if (first){
printf("\tOutput Histogram:\n");
first = 0;
}
printf("\t\t");
printf(sp->description, *var->val.integer, plural((int)*var->val.integer));
putchar('\n');
}
sp++;
}
sp = icmp_inhistogram;
first = 1;
count = sizeof(icmp_inhistogram) / sizeof (struct stat_table);
while (count--){
*icmpentry = sp->entry;
var = getvarbyname(Session, varname, varname_len);
if (var && *var->val.integer != 0){
if (first){
printf("\tInput Histogram:\n");
first = 0;
}
printf("\t\t");
printf(sp->description, *var->val.integer, plural((int)*var->val.integer));
putchar('\n');
}
sp++;
}
}
/*
* Pretty print an Internet address (net address + port).
* If the nflag was specified, use numbers instead of names.
*/
static void
inetprint(in, port, proto)
struct in_addr *in;
u_short port;
char *proto;
{
struct servent *sp = 0;
char line[80], *cp, *index();
int width;
sprintf(line, "%.*s.", 16, inetname(*in));
cp = index(line, '\0');
if (!nflag && port)
sp = getservbyport((int)port, proto);
if (sp || port == 0)
sprintf(cp, "%.8s", sp ? sp->s_name : "*");
else
sprintf(cp, "%d", port);
width = 22;
printf(" %-*.*s", width, width, line);
}
/*
* Construct an Internet address representation.
* If the nflag has been supplied, give
* numeric value, otherwise try for symbolic name.
*/
char *
inetname(in)
struct in_addr in;
{
char *cp;
static char line[50];
struct hostent *hp;
struct netent *np;
static char domain[MAXHOSTNAMELEN + 1];
static int first = 1;
if (first && !nflag) {
first = 0;
if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
(cp = index(domain, '.')))
(void) strcpy(domain, cp + 1);
else
domain[0] = 0;
}
cp = 0;
if (!nflag && in.s_addr != INADDR_ANY) {
u_long net = inet_netof(in);
u_long lna = inet_lnaof(in);
if (lna == INADDR_ANY) {
np = getnetbyaddr(net, AF_INET);
if (np)
cp = np->n_name;
}
if (cp == 0) {
hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
if (hp) {
if ((cp = index(hp->h_name, '.')) &&
!strcmp(cp + 1, domain))
*cp = 0;
cp = (char *) hp->h_name;
}
}
}
if (in.s_addr == INADDR_ANY)
strcpy(line, "*");
else if (cp)
strcpy(line, cp);
else {
in.s_addr = ntohl(in.s_addr);
#define C(x) ((x) & 0xff)
sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
}
return (line);
}
|