x_snmp.c
来自「TCP-IP红宝书源代码」· C语言 代码 · 共 654 行 · 第 1/2 页
C
654 行
if (buf[*ct - 1] == ' ')
erase1(--(*ct), stdout);
for (--(*ct); *ct >= 0 && buf[*ct] != ' '; (*ct)--)
erase1(*ct, stdout);
(*ct)++;
}
/*------------------------------------------------------------------------
* strnequ - if s1 and s2 have same length and same value, return TRUE
*------------------------------------------------------------------------
*/
LOCAL int
strnequ(
char *s1,
char *s2,
int n)
{
return ! strncmp(s1, s2, n);
}
/*------------------------------------------------------------------------
* snusage - print usage message
*------------------------------------------------------------------------
*/
LOCAL void snusage(
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 void initgetword(
char *buf)
{
nextchar = buf; /* nextchar at beginnning of buffer */
}
/*------------------------------------------------------------------------
* getword - get the next word from the input line
*------------------------------------------------------------------------
*/
LOCAL void getword(
char **word)
{
while (*nextchar == ' ') /* skip blanks */
nextchar++;
*word = nextchar;
while (*nextchar != '\0' && *nextchar != ' ')
nextchar++;
if (*nextchar != '\0')
*nextchar++ = '\0';
}
/*------------------------------------------------------------------------
* objd2oid - take a object string and parse it into an object id
*------------------------------------------------------------------------
*/
LOCAL int
objd2oid(
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 (((int)strlen(tmp) == len) &&
(memcmp(tmp, str, len)==0))
break;
}
if (i >= mib_entries)
return SYSERR;
idlen = oip->len = onp->mi_objid.len;
memcpy(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
*------------------------------------------------------------------------
*/
LOCAL int dot2oid(
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
*------------------------------------------------------------------------
*/
LOCAL int
parseoidlist(
struct req_desc *rqdp,
char **word)
{
struct snbentry *bl;
while (**word != NULLCH) {
if ((bl = parseoid(word)) == (struct snbentry *) SYSERR) {
snfreebl(&rqdp->bindlf); /* Canberra */
return SYSERR;
}
if (rqdp->bindlf == NULLCH)
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
*------------------------------------------------------------------------
*/
LOCAL struct snbentry *parseoid(
char **word)
{
struct snbentry *bl;
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]+
*------------------------------------------------------------------------
*/
LOCAL int
sendquery(
int stdout,
char *server)
{
struct req_desc rqd;
struct snbentry *bl;
char *word;
int repl;
initgetword(buf);
rqd.reqtype = PDU_GET; /* by default */
rqd.bindle = rqd.bindlf = NULLCH;
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) == (void *)NULLPTR) {
if (lastoidset) { /* no oids so use last one */
rqd.bindlf = rqd.bindle = bl = getnewbl();
bl->sb_oid.len = lastobjid.len;
memcpy(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;
memcpy(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
*------------------------------------------------------------------------
*/
LOCAL struct snbentry *getnewbl(void)
{
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
*------------------------------------------------------------------------
*/
LOCAL int
parseset(
struct req_desc *rqdp,
int stdout)
{
struct snbentry *bl;
char *word;
for (getword(&word); *word != NULLCH; 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 */
int
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 + -
显示快捷键?