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
|
/***********************************************************
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.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1983 Regents of the University of California.\n\
All rights reserved.\n";
#endif not lint
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <string.h>
#include "asn1.h"
#include "snmp.h"
#include "snmp_api.h"
/* internet protocols */
extern void protopr();
extern void tcp_stats(), udp_stats(), ip_stats(), icmp_stats();
extern void intpr();
extern void rt_stats();
extern void routepr();
#define NULLPROTOX ((struct protox *) 0)
struct protox {
u_char pr_wanted; /* 1 if wanted, 0 otherwise */
void (*pr_cblocks)(); /* control blocks printing routine */
void (*pr_stats)(); /* statistics printing routine */
char *pr_name; /* well-known name */
} protox[] = {
{ 1, protopr, tcp_stats, "tcp" },
{ 1, 0, udp_stats, "udp" },
{ 1, 0, ip_stats, "ip" },
{ 1, 0, icmp_stats, "icmp" },
{ 0, 0, 0, 0 }
};
int aflag;
int iflag;
int nflag;
int pflag;
int rflag;
int sflag;
int interval;
char *interface;
char usage[] = "host community [ -ainrs ] [-p proto] [-I interface] [ interval ]";
int debug = 0;
extern char *malloc();
struct snmp_session *Session;
int snmp_dump_packet = 0;
int print_errors = 0;
static void
pr_usage (name)
char *name;
{
printf("usage: %s %s\n", name, usage);
exit(1);
}
int
main(argc, argv)
int argc;
char *argv[];
{
char *cp, *name;
char *host = 0;
struct protoent *p;
struct protox *tp = 0; /* for printing cblocks & stats */
struct protox *name2protox(); /* for -p */
char *community = 0;
struct snmp_session session;
name = argv[0];
argc--, argv++;
while (argc > 0) {
if (**argv == '-') {
for (cp = &argv[0][1]; *cp; cp++) {
switch(*cp) {
case 'a':
aflag++;
break;
case 'i':
iflag++;
break;
case 'n':
nflag++;
break;
case 'r':
rflag++;
break;
case 's':
sflag++;
break;
case 'p':
argv++;
argc--;
if (argc == 0)
pr_usage();
if ((tp = name2protox(*argv)) == NULLPROTOX) {
fprintf(stderr, "%s: unknown or uninstrumented protocol\n",
*argv);
exit(10);
}
pflag++;
break;
case 'I':
iflag++;
if (*(interface = cp + 1) == 0) {
if ((interface = argv[1]) == 0)
break;
argv++;
argc--;
}
for (cp = interface; *cp; cp++)
;
cp--;
break;
default:
pr_usage(name);
}
}
} else {
if (isdigit(argv[0][0])) {
interval = atoi(argv[0]);
if (interval <= 0)
pr_usage(name);
argv++, argc--;
iflag++;
} else {
if (! host) {
host = *argv;
} else {
if (! community) {
community = *argv;
} else {
pr_usage(name);
}
}
}
}
argv++, argc--;
}
if (! host || ! community) {
pr_usage(name);
}
bzero((char *)&session, sizeof(struct snmp_session));
session.peername = host;
session.community = (u_char *)community;
session.community_len = strlen((char *)community);
session.retries = SNMP_DEFAULT_RETRIES;
session.timeout = SNMP_DEFAULT_TIMEOUT;
session.authenticator = NULL;
snmp_synch_setup(&session);
Session = snmp_open(&session);
if (Session == NULL){
printf("Couldn't open snmp\n");
exit(-1);
}
if (pflag) {
if (tp->pr_stats)
(*tp->pr_stats)();
else
printf("%s: no stats routine\n", tp->pr_name);
exit(0);
}
/*
* Keep file descriptors open to avoid overhead
* of open/close on each call to get* routines.
*/
sethostent(1);
setnetent(1);
if (iflag) {
intpr(interval);
exit(0);
}
if (rflag) {
if (sflag)
rt_stats();
else
routepr();
exit(0);
}
setprotoent(1);
setservent(1);
while ((p = getprotoent())) {
for (tp = protox; tp->pr_name; tp++)
if (strcmp(tp->pr_name, p->p_name) == 0)
break;
if (tp->pr_name == 0 || tp->pr_wanted == 0)
continue;
if (sflag) {
if (tp->pr_stats)
(*tp->pr_stats)();
} else
if (tp->pr_cblocks)
(*tp->pr_cblocks)();
}
endprotoent();
exit(0);
}
char *
plural(n)
int n;
{
return (n != 1 ? "s" : "");
}
/*
* Find the protox for the given "well-known" name.
*/
struct protox *
knownname(name)
char *name;
{
struct protox *tp;
for (tp = protox; tp->pr_name; tp++)
if (strcmp(tp->pr_name, name) == 0)
return(tp);
return(NULLPROTOX);
}
/*
* Find the protox corresponding to name.
*/
struct protox *
name2protox(name)
char *name;
{
struct protox *tp;
char **alias; /* alias from p->aliases */
struct protoent *p;
/*
* Try to find the name in the list of "well-known" names. If that
* fails, check if name is an alias for an Internet protocol.
*/
if ((tp = knownname(name)))
return(tp);
setprotoent(1); /* make protocol lookup cheaper */
while ((p = getprotoent())) {
/* assert: name not same as p->name */
for (alias = p->p_aliases; *alias; alias++)
if (strcmp(name, *alias) == 0) {
endprotoent();
return(knownname(p->p_name));
}
}
endprotoent();
return(NULLPROTOX);
}
|