📄 snmpfunc.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/lib/snmpfunc.c,v 1.3 2003/01/15 14:04:35 josh Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: snmpfunc.c,v $ * Revision 1.3 2003/01/15 14:04:35 josh * directory structure shifting * * Revision 1.2 2001/11/08 15:56:27 tneale * Updated for newest file layout * * Revision 1.1.1.1 2001/11/05 17:48:43 tneale * Tornado shuffle * * Revision 1.9 2001/05/07 14:39:22 markadam * Changed the default view for "public" access to be the same as for "private" * access. Note, this only affects GETs. * * Revision 1.8 2001/01/19 22:23:52 paul * Update copyright. * * Revision 1.7 2000/03/17 00:12:45 meister * Update copyright message * * Revision 1.6 1998/09/28 21:12:28 josh * just to make RFC2275 views alittle more useful if you're not using * SNMPv3... * * Revision 1.5 1998/07/06 01:04:55 sar * Add some casts and change atoi to strtol to make compilers happy * * Revision 1.4 1998/06/16 05:36:37 sar * clean up some type info, cast some strings * * Revision 1.3 1998/06/09 22:02:11 sar * Only define proxy_release_private if proxy is installed * * Revision 1.2 1998/06/08 19:47:44 sar * Added rmon start call if liaison is installed * * Revision 1.1 1998/06/07 02:40:06 sar * Move the validate and timing routines to the snark library * so they can be used by multiple demos * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include <wrn/wm/common/install.h>#include <wrn/wm/common/config.h>#include <wrn/wm/common/types.h>#include <snark.h>#include <wrn/wm/demo/snmpfunc.h>#include <wrn/wm/snmp/engine/asn1conf.h>#include <wrn/wm/snmp/engine/snmpdefs.h>#include <wrn/wm/snmp/engine/buffer.h>#include <wrn/wm/snmp/engine/proxy_tr.h>#include <wrn/wm/common/glue.h>#if INSTALL_ENVOY_SNMP_RFC2275_VIEWS static bits8_t *one = (bits8_t *)("one");static bits8_t *two = (bits8_t *)("two");#endifchar snmp_get_request_community[SNMP_COMM_MAX] = "public";char snmp_set_request_community[SNMP_COMM_MAX] = "private";char snmp_trap_community[SNMP_COMM_MAX] = "SNMP_trap";#if INSTALL_LIAISONextern void rmon_new_snmp_command(void);#endif#if INSTALL_ENVOY_SNMP_PROXYextern sbits32_t proxy_send_rtn(EBUFFER_T *, ptr_t);#endif#if INSTALL_ENVOY_SNMP_PROXY/****************************************************************************NAME: proxy_release_privatePURPOSE: Routine to release any resources that the validate routine attached to the user_private pointer for proxies.PARAMETERS: SNMP_PKT_T * Packet we're working onRETURNS: void ****************************************************************************/void proxy_release_private(SNMP_PKT_T *rp){if (rp->proxy_routine && rp->user_private) { SNMP_memory_free(rp->user_private); }}#endif/********************************************************************** validate_SNMP_community -- Check an operation against the community name. Get class operations can use either the set or get community. Set operations may use only the set community. The parameters to this procedure are: SNMP_PKT_T * The received packet (decoded format) SNMPADDR_T * Source of the packet SNMPADDR_T * Destination of the packet (most likely the address of the machine on which this code is running.) This procedure should return 0 if happy with the sommunity string and 1 if not. This routine may hang additional data onto the "private" field of the packet structure. The user will be given the opportinity to release that memory via release_private(). **********************************************************************//*ARGSUSED*//*lint -e715 */int validate_SNMP_community(SNMP_PKT_T *rp, SNMPADDR_T *pktsrc, SNMPADDR_T *pktdst){static bits16_t lcl_ident_source = 0;bits8_t *com_str;ALENGTH_T com_len;int match_get, match_set;#if INSTALL_LIAISONrmon_new_snmp_command();#endifMEMCPY(&(rp->pkt_src), pktsrc, sizeof(SNMPADDR_T));MEMCPY(&(rp->pkt_dst), pktdst, sizeof(SNMPADDR_T));rp->lcl_ident = lcl_ident_source++;rp->user_private = 0;com_str = EBufferStart(&(rp->community));com_len = EBufferUsed(&(rp->community));#if (INSTALL_ENVOY_SNMP_PROXY)/* Test to see if this is a proxy request, the community string will be of the form "proxy@vx@cstring@addr@port" with vx beign v1 or v2 to select the next hop version, port is optional (default = 161) so we require a minimum of 10 characters */if ((com_len >= 10) && (MEMCMP("proxy@", com_str, 6) == 0)) { bits8_t *vstr, *cstr, *astr, *pstr, addrstr[128]; ALENGTH_T vlen, clen, alen; TRANS_STUFF_T *trans_stuff; int port = 161; /* dig out the version string */ vstr = com_str + 6; com_len -= 6; /* figure out how long the version string is */ for (cstr = vstr, vlen = 0; *cstr != '@'; cstr++, vlen++, com_len--) { /* a properly formatted string must have at least 5 characters left (the current character 2 '@' and one character each for the community string and the addr */ if (com_len <= 5) return(1); } /* we must have only two characters for the version string and the first must be 'v' */ if ((vlen != 2) || (*vstr != 'v')) return(1); /* dig out the proxy community string, remove the '@' */ cstr++; com_len--; /* figure out how long the community string is */ for (astr = cstr, clen = 0; *astr != '@'; astr++, clen++, com_len--) { /* a properly formatted cstring must have at least 3 characters left (the current character the '@' and the first character of the addr */ if (com_len <= 3) return(1); } /* remove the '@' */ astr++; com_len--; /* figure out how long the addr string is */ for (pstr = astr, alen = 0; (com_len) && (*pstr != '@'); pstr++, alen++, com_len--) /* empty for loop */ ; /* if we don't have a name we bail out */ if (alen == 0) return(1); /* remove the '@' */ pstr++; com_len--; /* if we have characters left after the '@' they belong to the port string, we reject any ports that would be too long more than 5 characters (16 bit numbes) otherwise we copy 0 terminate the string then convert to an integer */ if (com_len > 5) return(1); if (com_len > 0) { MEMCPY(addrstr, pstr, com_len); addrstr[com_len] = 0; port = (int)STRTOL((char *)addrstr, 0, 0); if (port == 0) return(1); } /* having made it here we have a community string, address and port for use with the proxy stuff we allocate space for the transport block (which includes the proxy block) and the name, then fill them in */ trans_stuff = (TRANS_STUFF_T *)SNMP_memory_alloc(sizeof(TRANS_STUFF_T) + alen + 1); if (trans_stuff == 0) return(1); trans_stuff->addr_str = ((bits8_t *)trans_stuff) + sizeof(TRANS_STUFF_T); /* save the address string */ MEMCPY(trans_stuff->addr_str, astr, alen); /* add the 0 */ trans_stuff->addr_str[alen] = 0; /* and the rest of the information */ EBufferPreLoad(BFL_IS_STATIC, &(trans_stuff->pblockp.community), cstr, clen); trans_stuff->port = port; trans_stuff->pkt = 0; trans_stuff->pblockp.transport_routine = proxy_send_rtn; trans_stuff->pblockp.transport_block = (ptr_t)trans_stuff; rp->user_private = (ptr_t)trans_stuff; switch(*(vstr+1)) {#if (INSTALL_ENVOY_SNMP_VERSION_1) case '1': /* rp->proxy_routine = SNMP_Proxy_V1_Translation;*/ rp->proxy_routine = 0; break;#endif#if (INSTALL_ENVOY_SNMP_VERSION_2) case '2': /* rp->proxy_routine = SNMP_Proxy_V2_Translation;*/ rp->proxy_routine = 0; break;#endif default: return(1); } return(0); }#endif /* (INSTALL_ENVOY_SNMP_PROXY) */match_get = (STRLEN(snmp_get_request_community) == com_len && (MEMCMP(snmp_get_request_community, com_str, com_len) == 0));match_set = (STRLEN(snmp_set_request_community) == com_len && (MEMCMP(snmp_set_request_community, com_str, com_len) == 0));switch(rp->pdu_type) { case SET_REQUEST_PDU: if (match_set) { /* good set community name */ rp->mib_view = 0xffffffffL;#if (INSTALL_ENVOY_SNMP_RFC1445_VIEWS) rp->view_index = 2;#elif (INSTALL_ENVOY_SNMP_RFC2275_VIEWS) EBufferPreLoad(BFL_IS_STATIC, &rp->view_name, two, 3);#endif return(0); } if (match_get) return(3); return(2); case GET_REQUEST_PDU: case GET_NEXT_REQUEST_PDU: case GET_BULK_REQUEST_PDU: if (match_get || match_set) { /* allow access if either get or set name matched */ rp->mib_view = 0xffffffffL;#if (INSTALL_ENVOY_SNMP_RFC1445_VIEWS) rp->view_index = 2;#elif (INSTALL_ENVOY_SNMP_RFC2275_VIEWS) EBufferPreLoad(BFL_IS_STATIC, &rp->view_name, two, 3);#endif return(0); } return(2); default: /* allow other pdu types through without checking */ return(0); }}/*lint -e715 *//**********************************************************************This is a placeholder routine**********************************************************************/int validate_SNMP_address(SNMP_PKT_T *rp, SNMPADDR_T *src, SNMPADDR_T *dst){MEMCPY(&rp->pkt_src, src, sizeof(SNMPADDR_T));MEMCPY(&rp->pkt_dst, dst, sizeof(SNMPADDR_T));return(0);}#if INSTALL_ENVOY_SNMP_VERSION_3/********************************************************************** validate_SNMP_V3_address -- Check an operation against the address. Primarily this function is for copying the addressing info into the packet structure, but the user may also choose to deny access based on address or use a different mib tree based on address or context information. The parameters to this procedure are: SNMP_PKT_T * The received packet (decoded format) SNMPADDR_T * Source of the packet SNMPADDR_T * Destination of the packet (most likely the address of the machine on which this code is running.) This procedure should return 0 if happy and 1 if not This routine may hang additional data onto the "private" field of the packet structure. The user will be given the opportinity to release that memory via release_private(). **********************************************************************/int validate_SNMP_V3_address(SNMP_PKT_T *rp, SNMPADDR_T *pktsrc, SNMPADDR_T *pktdst){rp->user_private = 0;MEMCPY(&(rp->pkt_src), pktsrc, sizeof(SNMPADDR_T));MEMCPY(&(rp->pkt_dst), pktdst, sizeof(SNMPADDR_T));return(0);}#endif /* #if INSTALL_ENVOY_SNMP_VERSION_3 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -