x_snmp.c,v
来自「TCP-IP红宝书源代码」· C,V 代码 · 共 650 行 · 第 1/2 页
C,V
650 行
*/
LOCAL eraseword(ttyp, ct, stdout)
struct tty *ttyp;
int *ct;
int stdout;
{
/* if first character behind cursor is a blank, eat it up */
if (buf[*ct - 1] == BLANK)
erase1(ttyp, --(*ct), stdout);
for (--(*ct); *ct >= 0 && buf[*ct] != BLANK; (*ct)--)
erase1(ttyp, *ct, stdout);
(*ct)++;
}
/*------------------------------------------------------------------------
* strnequ - if s1 and s2 have same length and same value, return TRUE
*------------------------------------------------------------------------
*/
strnequ(s1, s2, n)
char *s1, *s2;
int n;
{
return ! strncmp(s1, s2, n);
}
/*------------------------------------------------------------------------
* snusage - print usage message
*------------------------------------------------------------------------
*/
LOCAL snusage(stdout)
int stdout;
{
char sb[BUFSIZE];
sprintf(sb, "usage: snmp agent-mach-name\n");
write(stdout, sb, strlen(sb));
}
static char *nextchar; /* points to char that begins next word */
/*------------------------------------------------------------------------
* initgetword - initialize the routines to get words off the input
*------------------------------------------------------------------------
*/
LOCAL initgetword(buf)
char *buf;
{
nextchar = buf; /* nextchar at beginnning of buffer */
}
/*------------------------------------------------------------------------
* getword - get the next word from the input line
*------------------------------------------------------------------------
*/
LOCAL getword(word)
char **word;
{
while (*nextchar == BLANK) /* skip blanks */
nextchar++;
*word = nextchar;
while (*nextchar != '\0' && *nextchar != BLANK)
nextchar++;
if (*nextchar != '\0')
*nextchar++ = '\0';
}
/*------------------------------------------------------------------------
* objd2oid - take a object string and parse it into an object id
*------------------------------------------------------------------------
*/
objd2oid(str, oip)
char *str;
struct oid *oip;
{
register char *cp;
struct mib_info *onp;
int idlen, len, i;
char tmp[BUFSIZE];
idlen = 0;
cp = str;
if (isalpha(*str)) {
/*
* The string at least starts out with object descriptors,
* and may have object ids at the end. So, first, find the
* end of the descriptors part.
*/
for (; *cp != '\0' && (isalpha(*cp) || *cp == '.'); cp++)
/* empty */;
len = cp - str;
if (*cp != '\0')
len--;
for (i = 0; i < mib_entries; i++) {
onp = &mib[i];
strcpy(tmp, onp->mi_prefix);
strcat(tmp, onp->mi_name);
if (strlen(tmp) == len && blkequ(tmp, str, len))
break;
}
if (i >= mib_entries)
return SYSERR;
idlen = oip->len = onp->mi_objid.len;
blkcopy(oip->id, onp->mi_objid.id, idlen*2);
/* remove possible trailing ".0" */
if (strequ(cp, "0"))
return OK;
}
/* rest of variable name is object sub-id's */
if (isdigit(*cp))
oip->len = idlen + dot2oid(&oip->id[idlen], cp);
return OK;
}
/*------------------------------------------------------------------------
* dot2oid - convert a character representation of an object id into
* an array of sub-identifiers
*------------------------------------------------------------------------
*/
int dot2oid(id, str)
u_short id[]; /* array of id's */
char *str;
{
register int totval;
int numids = 0;
while (TRUE) {
totval = 0;
while (isdigit(*str))
totval = totval * 10 + (*str++ - '0');
id[numids++] = (u_short) totval;
if (*str == '.')
str++;
else
break;
}
return numids;
}
/*------------------------------------------------------------------------
* parseoidlist - read a list of oid's from the input line
*------------------------------------------------------------------------
*/
parseoidlist(rqdp, word)
struct req_desc *rqdp;
char **word;
{
struct snbentry *parseoid(), *bl;
while (**word != NULL) {
if ((bl = parseoid(word)) == (struct snbentry *) SYSERR)
return SYSERR;
if (rqdp->bindlf == NULL)
rqdp->bindlf = rqdp->bindle = bl;
else {
bl->sb_prev = rqdp->bindle;
rqdp->bindle = rqdp->bindle->sb_next = bl;
}
}
return OK;
}
/*------------------------------------------------------------------------
* parseoid - read an oid from the input line
*------------------------------------------------------------------------
*/
struct snbentry *parseoid(word)
char **word;
{
struct snbentry *bl, *getnewbl();
bl = getnewbl();
if (objd2oid(*word, &bl->sb_oid) == SYSERR) {
freemem(bl, sizeof(struct snbentry));
return (struct snbentry *) SYSERR;
} else
SVTYPE(bl) = ASN1_NULL;
getword(word);
return bl;
}
static struct oid lastobjid; /* holds last objid that was returned */
static int lastoidset = FALSE; /* flags if there is a last objid */
/*------------------------------------------------------------------------
* sendquery - parse the input line and send the query. Input has
* one of the following forms:
* [object-name]+
* next [object-name]+
* set [object-name type value]+
*------------------------------------------------------------------------
*/
sendquery(stdout, server)
int stdout;
char *server;
{
struct req_desc rqd;
struct snbentry *bl, *getnewbl();
char *word;
int repl;
initgetword(buf);
rqd.reqtype = PDU_GET; /* by default */
rqd.bindle = rqd.bindlf = NULL;
getword(&word);
if (*word == '\0')
return OK;
if (strequ(word, "next")) {
rqd.reqtype = PDU_GETN;
getword(&word);
if (parseoidlist(&rqd, &word) == SYSERR) {
fprintf(stdout, "unknown variable\n");
return SYSERR;
}
if ((bl = rqd.bindlf) == NULL) {
if (lastoidset) { /* no oids so use last one */
rqd.bindlf = rqd.bindle = bl = getnewbl();
bl->sb_oid.len = lastobjid.len;
blkcopy(bl->sb_oid.id, lastobjid.id,
lastobjid.len*2);
SVTYPE(bl) = ASN1_NULL;
} else {
fprintf(stdout, "bad syntax\n");
return SYSERR;
}
}
} else if (strequ(word, "set")) {
rqd.reqtype = PDU_SET;
if (parseset(&rqd, stdout) == SYSERR)
return SYSERR;
} else if (parseoidlist(&rqd, &word) == SYSERR) {
fprintf(stdout, "unknown variable\n");
return SYSERR;
}
repl = snclient(&rqd, server, stdout);
switch (repl) {
case SCL_OK:
if (rqd.err_stat == SNMP_OK)
snmpprint(stdout, rqd.bindlf);
else
snerr(stdout, &rqd);
break;
case SCL_OPENF:
fprintf(stdout, "snmp: open failed\n");
break;
case SCL_WRITEF:
fprintf(stdout, "snmp: write failed\n");
break;
case SCL_NORESP:
fprintf(stdout, "snmp: No response from server %s\n", server);
break;
case SCL_READF:
fprintf(stdout, "snmp: read failed\n");
break;
case SCL_BADRESP:
fprintf(stdout, "snmp: received bad response\n");
break;
}
/* save this object for use with the "next" operation */
lastobjid.len = rqd.bindlf->sb_oid.len;
blkcopy(lastobjid.id, rqd.bindlf->sb_oid.id,
rqd.bindlf->sb_oid.len*2);
lastoidset = TRUE;
snfreebl(rqd.bindlf);
return OK;
}
/*------------------------------------------------------------------------
* getnewbl - get a new bindlist node from memory and initialize it
*------------------------------------------------------------------------
*/
struct snbentry *getnewbl()
{
struct snbentry *blp;
blp = (struct snbentry *) getmem(sizeof(struct snbentry));
blp->sb_next = blp->sb_prev = 0;
blp->sb_a1slen = 0;
return blp;
}
/*------------------------------------------------------------------------
* parseset - parse the words from the input line for a set operation
*------------------------------------------------------------------------
*/
parseset(rqdp, stdout)
struct req_desc *rqdp;
int stdout;
{
struct snbentry *bl, *parseoid();
char *word;
for (getword(&word); *word != NULL; getword(&word)) {
if ((bl = parseoid(&word)) == (struct snbentry *) SYSERR)
return SYSERR;
if (rqdp->bindlf) {
bl->sb_prev = rqdp->bindle;
rqdp->bindle = rqdp->bindle->sb_next = bl;
} else
rqdp->bindlf = rqdp->bindle = bl;
if (parsevalue(&word, bl) == SYSERR) {
fprintf(stdout, "bad syntax\n");
return SYSERR;
}
}
return OK;
}
#else /* SNMP */
x_snmp(stdin, stdout, stderr, nargs, args)
int stdin, stdout, stderr, nargs;
char *args[];
{
write(stderr, "no SNMP support included\n", 25);
return OK;
}
#endif /* SNMP */
@
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?