⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pass_persist.c

📁 snmp的源代码,已经在我的ubuntu下编译通过
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <net-snmp/net-snmp-config.h>#if HAVE_IO_H#include <io.h>#endif#if HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#if HAVE_UNISTD_H#include <unistd.h>#endif#include <ctype.h>#include <sys/types.h>#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if HAVE_SYS_WAIT_H# include <sys/wait.h>#endif#if HAVE_WINSOCK_H#include <winsock.h>#endif#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include <signal.h>#include <errno.h>#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include "struct.h"#include "pass_persist.h"#include "extensible.h"#include "util_funcs.h"struct extensible *persistpassthrus = NULL;int             numpersistpassthrus = 0;struct persist_pipe_type {    FILE           *fIn, *fOut;    int             fdIn, fdOut;    int             pid;}              *persist_pipes = (struct persist_pipe_type *) NULL;static int      init_persist_pipes(void);static void     close_persist_pipe(int iindex);static int      open_persist_pipe(int iindex, char *command);static void     destruct_persist_pipes(void);static int      write_persist_pipe(int iindex, const char *data);/* * These are defined in pass.c  */extern int      asc2bin(char *p);extern int      bin2asc(char *p, size_t n);extern int      snmp_oid_min_compare(const oid *, size_t, const oid *,                                     size_t);/* * the relocatable extensible commands variables  */struct variable2 extensible_persist_passthru_variables[] = {    /*     * bogus entry.  Only some of it is actually used.      */    {MIBINDEX, ASN_INTEGER, RWRITE, var_extensible_pass_persist, 0,     {MIBINDEX}},};voidinit_pass_persist(void){    snmpd_register_config_handler("pass_persist",                                  pass_persist_parse_config,                                  pass_persist_free_config,                                  "miboid program");}voidpass_persist_parse_config(const char *token, char *cptr){    struct extensible **ppass = &persistpassthrus, **etmp, *ptmp;    char           *tcptr;    int             i;    if (*cptr == '.')        cptr++;    if (!isdigit(*cptr)) {        config_perror("second token is not a OID");        return;    }    numpersistpassthrus++;    while (*ppass != NULL)        ppass = &((*ppass)->next);    (*ppass) = (struct extensible *) malloc(sizeof(struct extensible));    if (*ppass == NULL)        return;    (*ppass)->type = PASSTHRU_PERSIST;    (*ppass)->miblen = parse_miboid(cptr, (*ppass)->miboid);    while (isdigit(*cptr) || *cptr == '.')        cptr++;    /*     * name      */    cptr = skip_white(cptr);    if (cptr == NULL) {        config_perror("No command specified on pass_persist line");        (*ppass)->command[0] = 0;    } else {        for (tcptr = cptr; *tcptr != 0 && *tcptr != '#' && *tcptr != ';';             tcptr++);        strncpy((*ppass)->command, cptr, tcptr - cptr);        (*ppass)->command[tcptr - cptr] = 0;    }    strncpy((*ppass)->name, (*ppass)->command, sizeof((*ppass)->name));    (*ppass)->name[ sizeof((*ppass)->name)-1 ] = 0;    (*ppass)->next = NULL;    register_mib("pass_persist",                 (struct variable *) extensible_persist_passthru_variables,                 sizeof(struct variable2), 1, (*ppass)->miboid,                 (*ppass)->miblen);    /*     * argggg -- pasthrus must be sorted      */    if (numpersistpassthrus > 1) {        etmp = (struct extensible **)            malloc(((sizeof(struct extensible *)) * numpersistpassthrus));        if (etmp == NULL)            return;        for (i = 0, ptmp = (struct extensible *) persistpassthrus;             i < numpersistpassthrus && ptmp != 0; i++, ptmp = ptmp->next)            etmp[i] = ptmp;        qsort(etmp, numpersistpassthrus, sizeof(struct extensible *),              pass_persist_compare);        persistpassthrus = (struct extensible *) etmp[0];        ptmp = (struct extensible *) etmp[0];        for (i = 0; i < numpersistpassthrus - 1; i++) {            ptmp->next = etmp[i + 1];            ptmp = ptmp->next;        }        ptmp->next = NULL;        free(etmp);    }}voidpass_persist_free_config(void){    struct extensible *etmp, *etmp2;    /*     * Close any open pipes to any programs      */    destruct_persist_pipes();    for (etmp = persistpassthrus; etmp != NULL;) {        etmp2 = etmp;        etmp = etmp->next;        unregister_mib(etmp2->miboid, etmp2->miblen);        free(etmp2);    }    persistpassthrus = NULL;    numpersistpassthrus = 0;}u_char         *var_extensible_pass_persist(struct variable *vp,                            oid * name,                            size_t * length,                            int exact,                            size_t * var_len, WriteMethod ** write_method){    oid             newname[MAX_OID_LEN];    int             i, rtest, newlen;    static long     long_ret;    char            buf[SNMP_MAXBUF];    static char     buf2[SNMP_MAXBUF];    static oid      objid[MAX_OID_LEN];    struct extensible *persistpassthru;    FILE           *file;    /*     * Make sure that our basic pipe structure is malloced      */    init_persist_pipes();    long_ret = *length;    for (i = 1; i <= numpersistpassthrus; i++) {        persistpassthru = get_exten_instance(persistpassthrus, i);        rtest = snmp_oid_min_compare(name, *length,                                     persistpassthru->miboid,                                     persistpassthru->miblen);        if ((exact && rtest == 0) || (!exact && rtest <= 0)) {            /*             * setup args              */            if (persistpassthru->miblen >= *length || rtest < 0)                sprint_mib_oid(buf, persistpassthru->miboid,                               persistpassthru->miblen);            else                sprint_mib_oid(buf, name, *length);            /*             * Open our pipe if necessary              */            if (!open_persist_pipe(i, persistpassthru->name)) {                return (NULL);            }            if (exact)                snprintf(persistpassthru->command,                  sizeof(persistpassthru->command), "get\n%s\n", buf);            else                snprintf(persistpassthru->command,                  sizeof(persistpassthru->command), "getnext\n%s\n", buf);            persistpassthru->command[ sizeof(persistpassthru->command)-1 ] = 0;            DEBUGMSGTL(("ucd-snmp/pass_persist",                        "persistpass-sending:\n%s",                        persistpassthru->command));            if (!write_persist_pipe(i, persistpassthru->command)) {                *var_len = 0;                /*                 * close_persist_pipes is called in write_persist_pipe                  */                return (NULL);            }            /*             * valid call.  Exec and get output              */            if ((file = persist_pipes[i].fIn)) {                if (fgets(buf, sizeof(buf), file) == NULL) {                    *var_len = 0;                    close_persist_pipe(i);                    return (NULL);                }                /*                 * persistant scripts return "NONE\n" on invalid items                  */                if (!strncmp(buf, "NONE", 4)) {                    if (exact) {                        *var_len = 0;                        return (NULL);                    }                    continue;                }                newlen = parse_miboid(buf, newname);                /*                 * its good, so copy onto name/length                  */                memcpy((char *) name, (char *) newname,                       (int) newlen * sizeof(oid));                *length = newlen;                /*                 * set up return pointer for setable stuff                  */                *write_method = setPassPersist;                if (newlen == 0 || fgets(buf, sizeof(buf), file) == NULL                    || fgets(buf2, sizeof(buf2), file) == NULL) {                    *var_len = 0;                    close_persist_pipe(i);                    return (NULL);                }                /*                 * buf contains the return type, and buf2 contains the data                  */                if (!strncasecmp(buf, "string", 6)) {                    buf2[strlen(buf2) - 1] = 0; /* zap the linefeed */                    *var_len = strlen(buf2);                    vp->type = ASN_OCTET_STR;                    return ((unsigned char *) buf2);                } else if (!strncasecmp(buf, "integer", 7)) {                    *var_len = sizeof(long_ret);                    long_ret = strtol(buf2, NULL, 10);                    vp->type = ASN_INTEGER;                    return ((unsigned char *) &long_ret);                } else if (!strncasecmp(buf, "unsigned", 8)) {                    *var_len = sizeof(long_ret);                    long_ret = strtoul(buf2, NULL, 10);                    vp->type = ASN_UNSIGNED;                    return ((unsigned char *) &long_ret);                } else if (!strncasecmp(buf, "counter", 7)) {                    *var_len = sizeof(long_ret);                    long_ret = strtoul(buf2, NULL, 10);                    vp->type = ASN_COUNTER;                    return ((unsigned char *) &long_ret);                } else if (!strncasecmp(buf, "octet", 5)) {                    *var_len = asc2bin(buf2);                    vp->type = ASN_OCTET_STR;                    return ((unsigned char *) buf2);                } else if (!strncasecmp(buf, "opaque", 6)) {                    *var_len = asc2bin(buf2);                    vp->type = ASN_OPAQUE;                    return ((unsigned char *) buf2);                } else if (!strncasecmp(buf, "gauge", 5)) {                    *var_len = sizeof(long_ret);                    long_ret = strtoul(buf2, NULL, 10);                    vp->type = ASN_GAUGE;                    return ((unsigned char *) &long_ret);                } else if (!strncasecmp(buf, "objectid", 8)) {                    newlen = parse_miboid(buf2, objid);                    *var_len = newlen * sizeof(oid);                    vp->type = ASN_OBJECT_ID;                    return ((unsigned char *) objid);                } else if (!strncasecmp(buf, "timetick", 8)) {                    *var_len = sizeof(long_ret);                    long_ret = strtoul(buf2, NULL, 10);                    vp->type = ASN_TIMETICKS;                    return ((unsigned char *) &long_ret);                } else if (!strncasecmp(buf, "ipaddress", 9)) {                    newlen = parse_miboid(buf2, objid);                    if (newlen != 4) {                        snmp_log(LOG_ERR,                                 "invalid ipaddress returned:  %s\n",                                 buf2);                        *var_len = 0;                        return (NULL);                    }                    long_ret =                        (objid[0] << (8 * 3)) + (objid[1] << (8 * 2)) +                        (objid[2] << 8) + objid[3];                    long_ret = htonl(long_ret);                    *var_len = sizeof(long_ret);                    vp->type = ASN_IPADDRESS;                    return ((unsigned char *) &long_ret);                }            }            *var_len = 0;            return (NULL);        }    }    if (var_len)        *var_len = 0;    *write_method = NULL;    return (NULL);}intsetPassPersist(int action,               u_char * var_val,               u_char var_val_type,               size_t var_val_len,               u_char * statP, oid * name, size_t name_len){    int             i, rtest;    struct extensible *persistpassthru;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -