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

📄 tunnel.c

📁 开发snmp的开发包有两个开放的SNMP开发库
💻 C
📖 第 1 页 / 共 2 页
字号:
    DEBUGMSG(("tunnel", "getNextTunnelByIfIndex(%d): ", index));    for (tunnel = tunnels; tunnel; tunnel = tunnel->next) {        if (tunnel->ifindex > index) {            if (!tunnel->active)                continue;            DEBUGMSG(("tunnel",                      "%s (index=%d)\n", tunnel->ifname, tunnel->ifindex));            return tunnel;        }    }    DEBUGMSG(("tunnel", "NONE\n"));    return NULL;}static voidfillConfigOid(oid * name, struct tunnel *tunnel){    name[0] = ((unsigned char *) &tunnel->local)[0];    name[1] = ((unsigned char *) &tunnel->local)[1];    name[2] = ((unsigned char *) &tunnel->local)[2];    name[3] = ((unsigned char *) &tunnel->local)[3];    name[4] = ((unsigned char *) &tunnel->remote)[0];    name[5] = ((unsigned char *) &tunnel->remote)[1];    name[6] = ((unsigned char *) &tunnel->remote)[2];    name[7] = ((unsigned char *) &tunnel->remote)[3];    name[8] = tunnel->encaps;    name[9] = tunnel->id;    DEBUGMSGOID(("tunnel", name, 10));}static struct tunnel *getTunnelByConfigOid(oid * name, size_t * length){    struct tunnel  *tunnel;    oid             tname[4 + 4 + 1 + 1];    DEBUGMSG(("tunnel", "getTunnelByConfigOid(): "));    for (tunnel = tunnels; tunnel; tunnel = tunnel->next) {        fillConfigOid(tname, tunnel);        if (!snmp_oid_compare(tname, 4 + 4 + 1 + 1,                              &name[tunnel_len + 3],                              (*length) - tunnel_len - 3)) {            if (!tunnel->active)                break;            DEBUGMSG(("tunnel",                      "%s (index=%d)\n", tunnel->ifname, tunnel->ifindex));            return tunnel;        }    }    DEBUGMSG(("tunnel", "NONE\n"));    return NULL;}static struct tunnel *getNextTunnelByConfigOid(oid * name, size_t * length){    struct tunnel  *tunnel, *last_tunnel;    oid             tname[10], last_tname[10];    DEBUGMSG(("tunnel", "getNextTunnelByConfigOid("));    DEBUGMSGOID(("tunnel", name, *length));    DEBUGMSG(("tunnel", "): "));    last_tunnel = NULL;    for (tunnel = tunnels; tunnel; tunnel = tunnel->next) {        if (!tunnel->active)            continue;        fillConfigOid(tname, tunnel);        if (snmp_oid_compare(tname, 10,                             &name[tunnel_len + 3],                             (*length) - tunnel_len - 3) > 0) {            if (!last_tunnel) {                last_tunnel = tunnel;                memcpy((char *) last_tname, (char *) tname,                       10 * sizeof(oid));            } else {                if (snmp_oid_compare(tname, 10, last_tname, 10) < 0) {                    last_tunnel = tunnel;                    memcpy((char *) last_tname, (char *) tname,                           10 * sizeof(oid));                }            }        }    }    if (last_tunnel) {        DEBUGMSG(("tunnel",                  "%s (index=%d)\n",                  last_tunnel->ifname, last_tunnel->ifindex));    } else {        DEBUGMSG(("tunnel", "NONE\n"));    }    return last_tunnel;}static intwriteLocalAddress(int action, unsigned char *var_val,                  unsigned char var_val_type, size_t var_val_len,                  unsigned char *statP, oid * name, size_t name_len){    static struct tunnel *tunnel;    struct ip_tunnel_parm *parm;    switch (action) {    case RESERVE1:        if (var_val_type != ASN_IPADDRESS) {            return SNMP_ERR_WRONGTYPE;        }        if (var_val_len != 4) {            return SNMP_ERR_WRONGLENGTH;        }    case RESERVE2:        tunnel = getTunnelByIfIndex((int) name[name_len - 1]);        if (!tunnel) {            return SNMP_ERR_NOSUCHNAME;        }    case FREE:        break;    case ACTION:        break;    case UNDO:        break;    case COMMIT:        if (!tunnel) {            return SNMP_ERR_NOSUCHNAME;        }        parm = getTunnelParm(tunnel->ifname);        if (!parm) {            return SNMP_ERR_NOSUCHNAME;        }        parm->iph.saddr = *(unsigned long *) var_val;        setTunnelParm(tunnel->ifname, parm);        break;    }    return SNMP_ERR_NOERROR;}static intwriteRemoteAddress(int action, unsigned char *var_val,                   unsigned char var_val_type, size_t var_val_len,                   unsigned char *statP, oid * name, size_t name_len){    static struct tunnel *tunnel;    struct ip_tunnel_parm *parm;    switch (action) {    case RESERVE1:        if (var_val_type != ASN_IPADDRESS) {            return SNMP_ERR_WRONGTYPE;        }        if (var_val_len != 4) {            return SNMP_ERR_WRONGLENGTH;        }    case RESERVE2:        tunnel = getTunnelByIfIndex((int) name[name_len - 1]);        if (!tunnel) {            return SNMP_ERR_NOSUCHNAME;        }    case FREE:        break;    case ACTION:        break;    case UNDO:        break;    case COMMIT:        if (!tunnel) {            return SNMP_ERR_NOSUCHNAME;        }        parm = getTunnelParm(tunnel->ifname);        if (!parm) {            return SNMP_ERR_NOSUCHNAME;        }        parm->iph.daddr = *(unsigned long *) var_val;        setTunnelParm(tunnel->ifname, parm);        break;    }    return SNMP_ERR_NOERROR;}static intwriteHopLimit(int action, unsigned char *var_val,              unsigned char var_val_type, size_t var_val_len,              unsigned char *statP, oid * name, size_t name_len){    static struct tunnel *tunnel;    struct ip_tunnel_parm *parm;    switch (action) {    case RESERVE1:        if (var_val_type != ASN_INTEGER) {            return SNMP_ERR_WRONGTYPE;        }        if (var_val_len > sizeof(long)) {            return SNMP_ERR_WRONGLENGTH;        }    case RESERVE2:        tunnel = getTunnelByIfIndex((int) name[name_len - 1]);        if (!tunnel) {            return SNMP_ERR_NOSUCHNAME;        }    case FREE:        break;    case ACTION:        break;    case UNDO:        break;    case COMMIT:        if (!tunnel) {            return SNMP_ERR_NOSUCHNAME;        }        parm = getTunnelParm(tunnel->ifname);        if (!parm) {            return SNMP_ERR_NOSUCHNAME;        }        parm->iph.ttl = *(long *) var_val;        setTunnelParm(tunnel->ifname, parm);        break;    }    return SNMP_ERR_NOERROR;}static intwriteTOS(int action, unsigned char *var_val,         unsigned char var_val_type, size_t var_val_len,         unsigned char *statP, oid * name, size_t name_len){    static struct tunnel *tunnel;    struct ip_tunnel_parm *parm;    switch (action) {    case RESERVE1:        if (var_val_type != ASN_INTEGER) {            return SNMP_ERR_WRONGTYPE;        }        if (var_val_len > sizeof(long)) {            return SNMP_ERR_WRONGLENGTH;        }    case RESERVE2:        tunnel = getTunnelByIfIndex((int) name[name_len - 1]);        if (!tunnel) {            return SNMP_ERR_NOSUCHNAME;        }    case FREE:        break;    case ACTION:        break;    case UNDO:        break;    case COMMIT:        if (!tunnel) {            return SNMP_ERR_NOSUCHNAME;        }        parm = getTunnelParm(tunnel->ifname);        if (!parm) {            return SNMP_ERR_NOSUCHNAME;        }        /*         * this does not cover all meaningful values:          */        parm->iph.tos = (*(long *) var_val == -1) ? 1 : *(long *) var_val;        setTunnelParm(tunnel->ifname, parm);        break;    }    return SNMP_ERR_NOERROR;}unsigned char  *var_tunnelIfEntry(struct variable *vp,                  oid * name, size_t * length,                  int exact, size_t * var_len, WriteMethod ** write_method){    static unsigned long ret_addr;    static long     ret_int;    struct tunnel  *tunnel;    DEBUGMSGTL(("tunnel", "var_tunnelIfEntry: "));    DEBUGMSGOID(("tunnel", name, *length));    DEBUGMSG(("tunnel", " %d\n", exact));    updateTunnels();    if (exact) {        if (*length != tunnel_len + 3 + 1) {            return NULL;        }        tunnel = getTunnelByIfIndex((int) name[*length - 1]);    } else {        if ((*length) < tunnel_len) {            memcpy((char *) name, (char *) tunnel_variables_oid,                   tunnel_len * sizeof(oid));        }        if ((*length) < tunnel_len + 1) {            name[tunnel_len] = 1;        }        if ((*length) < tunnel_len + 2) {            name[tunnel_len + 1] = 1;        }        if ((*length) < tunnel_len + 3) {            name[tunnel_len + 2] = 1;        }        if ((*length) < tunnel_len + 4) {            name[tunnel_len + 3] = 0;        }        *length = tunnel_len + 4;        tunnel = getNextTunnelByIfIndex(name[*length - 1]);        if (!tunnel) {            /*             * end of column, continue with first row of next column              */            tunnel = tunnels;            name[tunnel_len + 2]++;            if (name[tunnel_len + 2] > 6) {                /*                 * there is no next column                  */                return NULL;            }            if (!tunnel) {                /*                 * there is no (next) row                  */                return NULL;            }        }    }    if (!tunnel) {        return NULL;    }    name[*length - 1] = tunnel->ifindex;    DEBUGMSGTL(("tunnel", "var_tunnelIfEntry: using"));    DEBUGMSGOID(("tunnel", name, *length));    DEBUGMSG(("tunnel", "\n"));    switch (name[tunnel_len + 2]) {    case 1:                    /* tunnelIfLocalAddress */        ret_addr = tunnel->local;        *var_len = 4;        vp->type = ASN_IPADDRESS;        *write_method = writeLocalAddress;        return (u_char *) & ret_addr;    case 2:                    /* tunnelIfRemoteAddress */        ret_addr = tunnel->remote;        *var_len = 4;        vp->type = ASN_IPADDRESS;        *write_method = writeRemoteAddress;        return (u_char *) & ret_addr;    case 3:                    /* tunnelIfEncapsMethod */        ret_int = tunnel->encaps;        *var_len = sizeof(ret_int);        vp->type = ASN_INTEGER;        return (u_char *) & ret_int;    case 4:                    /* tunnelIfHopLimit */        ret_int = tunnel->hoplimit;        *var_len = sizeof(ret_int);        vp->type = ASN_INTEGER;        *write_method = writeHopLimit;        return (u_char *) & ret_int;    case 5:                    /* tunnelIfSecurity */        ret_int = tunnel->security;        *var_len = sizeof(ret_int);        vp->type = ASN_INTEGER;        return (u_char *) & ret_int;    case 6:                    /* tunnelIfTOS */        ret_int = tunnel->tos;        *var_len = sizeof(ret_int);        vp->type = ASN_INTEGER;        *write_method = writeTOS;        return (u_char *) & ret_int;    default:        return 0;    }    return NULL;}unsigned char  *var_tunnelConfigEntry(struct variable *vp,                      oid * name, size_t * length,                      int exact, size_t * var_len,                      WriteMethod ** write_method){    static long     ret_int;    struct tunnel  *tunnel;    int             i;    DEBUGMSGTL(("tunnel", "var_tunnelConfigEntry: "));    DEBUGMSGOID(("tunnel", name, *length));    DEBUGMSG(("tunnel", " %d\n", exact));    updateTunnels();    if (exact) {        if (*length != tunnel_len + 3 + 4 + 4 + 1 + 1) {            return NULL;        }        tunnel = getTunnelByConfigOid(name, length);    } else {        if (snmp_oid_compare(name, *length,                             tunnel_configEntry_oid,                             tunnel_configEntry_len) < 0) {            *length = 0;        }        if ((*length) < tunnel_len) {            memcpy((char *) name, (char *) tunnel_variables_oid,                   tunnel_len * sizeof(oid));        }        if ((*length) < tunnel_len + 1) {            name[tunnel_len] = 2;        }        if ((*length) < tunnel_len + 2) {            name[tunnel_len + 1] = 1;        }        if ((*length) < tunnel_len + 3) {            name[tunnel_len + 2] = 5;        }        for (i = MAX(*length, tunnel_len + 3);             i < tunnel_len + 3 + 4 + 4 + 1 + 1; i++) {            name[i] = 0;        }        *length = tunnel_len + 3 + 4 + 4 + 1 + 1;        tunnel = getNextTunnelByConfigOid(name, length);        if (!tunnel) {            /*             * end of column, continue with first row of next column              */            tunnel = tunnels;            name[tunnel_len + 2]++;            if (name[tunnel_len + 2] > 6) {                /*                 * there is no next column                  */                return NULL;            }            if (!tunnel) {                /*                 * there is no (next) row                  */                return NULL;            }        }    }    if (!tunnel) {        return NULL;    }    fillConfigOid(&name[tunnel_len + 3], tunnel);    DEBUGMSGTL(("tunnel", "var_tunnelConfigEntry: using "));    DEBUGMSGOID(("tunnel", name, *length));    DEBUGMSG(("tunnel", "\n"));    switch (name[tunnel_len + 2]) {    case 5:                    /* tunnelConfigIfIndex */        ret_int = tunnel->ifindex;        *var_len = sizeof(ret_int);        vp->type = ASN_INTEGER;        return (u_char *) & ret_int;    case 6:                    /* tunnelConfigStatus */        ret_int = 1;            /* active */        *var_len = sizeof(ret_int);        vp->type = ASN_INTEGER;        return (u_char *) & ret_int;    default:        return 0;    }    return NULL;}

⌨️ 快捷键说明

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