snmp_util.c
资源名称:snmp.src.rar [点击查看]
上传用户:cxs890
上传日期:2021-05-22
资源大小:347k
文件大小:3k
源码类别:
SNMP编程
开发平台:
C/C++
- #ifdef OS_VXWORKS
- #include <global.h>
- #include <libsys/vos/vos_rtos.h>
- #endif
- #include <ip/socket.h>
- #include <libsys/memory.h>
- #include <libdev/interface.h>
- #include <ipacl/ipacl.h>
- #include "config.h"
- #include "types.h"
- #include "asn1.h"
- #include "config_struct.h"
- #include "snmp_debug.h"
- /*#include "snmp_util.h"*/
- static int32 snmp_get32(cp)
- register char *cp;
- {
- int32 rval;
- rval = (unsigned char)(*cp++);
- rval <<= 8;
- rval |= (unsigned char)(*cp++);
- rval <<= 8;
- rval |= (unsigned char)(*cp++);
- rval <<= 8;
- rval |= (unsigned char)(*cp);
- return rval;
- }
- int check_community(unsigned char *community, int community_len, unsigned long src_ip)
- {
- int count;
- struct acl_info acl_info;
- SNMP_DEBUG_ROUTINES("check_community()");
- for (count = 0; count < SNMP_MAX_COMMUNITY_NUM; count++) {
- if (community_len == strlen(snmp_para.community[count].name)
- && !memcmp(snmp_para.community[count].name, community, community_len))
- break;
- }
- if (count >= SNMP_MAX_COMMUNITY_NUM)
- return -1;
- if (snmp_para.community[count].access_list[0]) {
- memset(&acl_info, 0, sizeof(struct acl_info));
- acl_info.src_ip = src_ip;
- if (ipacl_check(&acl_info, snmp_para.community[count].access_list) != IP_PERMIT)
- return -1;
- }
- return count;
- }
- int community_write(int community_id)
- {
- return (snmp_para.community[community_id].privilege == READWRITE);
- }
- #include <ip/target.h>
- #include <ip/ip_externs.h>
- void get_trapsrc_ip(unsigned long *ipaddr)
- {
- DEVICE_ETERNAL_ID trapsource;
- DEVICE_ID diID;
- ADDR_LINK *addr_list, *addr;
- *ipaddr = 0L;
- trapsource = snmp_para.trapsource;
- if (trapsource == DEVICE_INVALID_ETERNAL_ID) {
- return ;
- }
- /* 将eternalID转换为diID */
- if (INTERFACE_GLOBAL_SUCCESS != interface_omnivorous_callback_makemomentary(&diID, trapsource)) {
- return ;
- }
- /*
- 返回的是一个地址结构链表,其中端口的主地址在链表开始处。
- 注意:使用完毕后,必须释放所占用的内存空间!
- */
- addr_list = getipbyid(diID);
- if (addr_list) {
- addr = addr_list;
- *ipaddr = (unsigned long) snmp_get32(addr->addr.addr);
- while (addr_list) {
- addr = addr_list;
- addr_list = addr->next;
- sys_mem_free(addr);
- }
- }
- }
- long isquotation(char *ch)
- {
- register char *cp;
- int quotation_exist = FALSE;
- if (NULLCHAR == ch)
- return FALSE;
- cp = ch;
- while (*cp) {
- if (0x20 == *cp) {
- quotation_exist = TRUE;
- break;
- }
- cp++;
- }
- return quotation_exist;
- }
- int snmp_check_ipacl(char *ipacl, int ipaddr)
- {
- struct acl_info acl_info;
- int rc;
- if (ipacl == NULL || ipacl[0] == 0)
- return 0;
- acl_info.src_ip = ipaddr;
- rc = ipacl_check(&acl_info, ipacl);
- if ((rc & IP_PERMIT) == 0)
- return -1;
- return 0;
- }