📄 linux-user.c
字号:
/* -------------------------------------------------------------------------- *//* *//* Filename: userexit.c *//* *//* Author: Icon Laboratories, Inc. *//* (888) 235-3443 *//* http://www.icon-labs.com *//* *//* Notice: Copyright 2001, Icon Laboratories, Inc. All rights reserved. *//* *//* Description: This file contains user exits from Envoy code as defined in *//* the envoy.h file. There are #ifdefs with conditional code *//* affected by the install.h file. Both envoy.h and install.h *//* are included indirectly via envoy/h/snmp.h. This "template" *//* file contains stub versions of these functions (either doing *//* nothing or "the right thing"). The implementor can copy this *//* file as userexit.c and modify the functions as needed. *//* *//* Modification History: *//* *//* Jim Jones v1.2 05-30-2001 Added USER_free/malloc & Comments *//* Pete Flugstad v1.1 11-07-2000 Updated for v3 & VACM using NV_Op *//* Pete Flugstad v1.0 09-18-2000 Pete's Original for v1/v2c *//* *//* -------------------------------------------------------------------------- */#include <stdio.h>#include <stdlib.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <wrn/wm/snmp/engine/asn1conf.h>#include <wrn/wm/snmp/engine/asn1.h>#include <wrn/wm/snmp/engine/buffer.h>#include <wrn/wm/snmp/engine/mib.h>#include <wrn/wm/snmp/engine/localio.h>#include <wrn/wm/snmp/engine/snmpdefs.h>#include <wrn/wm/snmp/engine/snmp.h>#include <wrn/wm/snmp/engine/auxfuncs.h>#define ICON_PROXY_AGENT 0#define ICON_AGENTX_AGENT 0#define ICON_VALIDATE_COMMUNITY 1/**************************************************************** SNMP_malloc To allocate space and return a pointer to it. Parameters: size_t size The number of bytes requested Returns: Generic pointer to allocated space or NULL (failed).*****************************************************************/void *USER_malloc(size_t size){ return (void *)malloc(size);}/**************************************************************** SNMP_free To free allocated space (presumably allocated by SNMP_malloc) Parameters: void *ptr A pointer to alloc'd space Returns: Nothing*****************************************************************/void USER_free(void *ptr){ free(ptr);}/**************************************************************** SNMP_release_private Get rid of a any private data attached to the packet structure. May be left undefined in which case nothing is done with the private field when the SNMP packet structure is freed. Parameters: SNMP_PKT_T *pkt The packet itself Returns: Nothing*****************************************************************/void USER_release_private ( SNMP_PKT_T *pkt ){ free( pkt->user_private );}/***************************************************************** SNMP_user_get_encode_buffer Allows the user the opportunity to get the buffer that the SNMP packet will be encoded into. If not used then leave undefined. Parameters: SNMP_PKT_T *pkt The packet being encoded. unsigned int need The amount of storage needed. EBUFFER_T *ebuffp The ebuffer to fill in with the actual buffer. Returns: 0 successful 1 not successful****************************************************************/int USER_user_get_encode_buffer( SNMP_PKT_T *pkt, unsigned int need, EBUFFER_T *ebuffp ){ /* just malloc the space */ unsigned char *ptr = (unsigned char *) malloc( need ); if ( !ptr ) return 1; EBufferSetup( BFL_IS_DYNAMIC, ebuffp, ptr, need ); return 0;}/***************************************************************** SNMP_trace_input SNMP_trace_output for implementing packet tracing through the SNMP code. May not be left undefined but may instead be defined as nothing. Parameters: SNMP_PKT_T *pkt The packet being traced Returns: Nothing****************************************************************/void USER_Trace_Input( SNMP_PKT_T *pkt ){}void USER_Trace_Output( SNMP_PKT_T *pkt ){}/***************************************************************** The next series of macros are hooks into the processing of SET PDUs. For most uses you should be able to leave these undefined. SNMP_validate_set_pdu -- Perform a global validation of a SET PDU. SNMP_user_pre_set -- Perform a global validation of a SET PDU after all of the test procedures have been called and given the "go ahead". SNMP_user_post_set -- Perform any final activities after all the set procedures have been called. Parameters: SNMP_PKT_T *pkt The packet itself Returns for SNMP_Validate_set_pdu and SNMP_user_pre_set: -1 If the PDU is bad and should be rejected with a GEN_ERR. 0 If the PDU is good and normal handling should proceed. +1 If the PDU is good and this routine has performed all of the set operations internally. Returns for SNMP_user_post_set: nothing*****************************************************************/int USER_validate_set_pdu(SNMP_PKT_T *pkt){ return 0;}int USER_user_pre_set(SNMP_PKT_T *pkt){ return 0;}int USER_user_post_set(SNMP_PKT_T *pkt){ return 0;}/***************************************************************** Timer routines. These connect the Envoy timer routines to the system. These are required for some of the options (notably for agentx, v3 inform-notification, and proxy support). ENVOY_CALL_TIMER - this routine should cause the system to call the handler routine in the specified number of milliseconds. ENVOY_NOW - this routine returns a clock with millisecond granularity. *****************************************************************/#include <sys/time.h>#include <unistd.h>/* returns the relative time in milliseconds */bits32_t USER_now(void){ struct timeval tp; gettimeofday(&tp, 0); return((tp.tv_sec * 1000) + ((tp.tv_usec/1000) % 1000));}static bits32_t calltime;static void (*callee)(void);void USER_Call_Timer(bits32_t when, void (*what)(void)){ if ( callee ) { /* should not happen?? print out an error message?? */ } /* when is in milliseconds in the future */ calltime = when + USER_now(); callee = what;}/* * the agent main loop should call this to setup the struct * TV for the next timeout, if needed, or to call the * timeout function if we've timed out. */int USER_Process_Timeout( struct timeval *tv ){ if ( callee ) { sbits32_t rtime; void (*to_call)(void); rtime = calltime - USER_now(); if (rtime <= 0 ) { /* timeout has passed */ to_call = callee; callee = NULL; (*to_call)(); return 0; /* no timeout */ } /* rtime > 0, so timeout is in the future, setup timeval */ tv->tv_sec = rtime / 1000; tv->tv_usec = (rtime % 1000) * 1000; return 1; /* timeout set */ } return 0; /* no timeout */}#if ICON_VALIDATE_COMMUNITY/****************************************************************** SNMP_validate_community Check an operation against the community name. If the community is accepted this routine must set the view mask, number or name (depending on the view scheme in use) and attach the source and destination addresses to the packet. It may attach proxy information to the packet or choose a non-default mib tree for use with the give community. 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 SNMP_release_private().Parameters: SNMP_PKT_T *pkt The received packet (decoded format) SNMPADDR_T *src Source of the packet SNMPADDR_T *dst Destination of the packet (most likely the address of the machine on which this code is running.)Returns: 0 - community is ok and processing should continue 1 - community isn't okay, drop the packet, any stats have been updated 2 - community isn't okay, drop the packet, update BadCommunityNames 3 - community isn't okay, drop the packet, update BadCommunityUses*****************************************************************/int USER_validate_community(SNMP_PKT_T *pkt, SNMPADDR_T *src, SNMPADDR_T *dst){ pkt->user_private = 0; MEMCPY(&(pkt->pkt_src), src, sizeof(SNMPADDR_T)); MEMCPY(&(pkt->pkt_dst), dst, sizeof(SNMPADDR_T)); return 0;}#endif#if (INSTALL_ENVOY_SNMP_RFC2275_VIEWS)#include "wrn/wm/demo/nvutils.h"/* View management function - this function should validate the request * and/or update the NV Store as indicated */sbits32_t USER_View_2275( int op, int stage, SNMP_PKT_T *pkt, VB_T *vbp, VIEWLEAF_T *cur, VIEWLEAF_T *new ){ switch ( stage ) { case STAGE_TEST: switch ( op ) { case OP_CREATE: return SNMP_NV_View_2275_Add_Mod(cur, new, 0); case OP_UPDATE: return SNMP_NV_View_2275_Add_Mod(cur, new, 0); case OP_DESTROY: return SNMP_NV_View_2275_Add_Mod(cur, new, NV_MOD_DESTROY); } break; case STAGE_BACKOUT: SNMP_NV_Clean(); return 0; case STAGE_SET: return 0; case STAGE_UNDO: SNMP_NV_Clean(); return 0; case STAGE_FINISHED: SNMP_NV_Doit(); return 0; } /* switch(stage) */ return 0;}#endif#if INSTALL_ENVOY_SNMP_VERSION_3/***************************************************************** If version 3 is installed we may need the following macros. ENVOY_TIME - returns a clock with a granularity of seconds PARAMETERS: none returns: bits32_t SNMP_validate_address This routine gives the user a chance to examine the address information as well as most of the packet itself before a v3 packet is processed. The routine must attach the address information to the packet. It may attach a non-default mib tree if desired. Note: in some error cases the packet will not be decoded when this routine is called. Parameters: SNMP_PKT_T *pkt The received packet (decoded format) SNMPADDR_T *src Source of the packet SNMPADDR_T *dst Destination of the packet (most likely the address of the machine on which this code is running.) returns: 0 - ok 1 - failure, drop the packet Lastly are are a series of macros that will connect the method routines we supplied for the v3 tables to your non-volatile storage areas. You should consult the manuals for more information about these macros.*****************************************************************/int USER_validate_address (SNMP_PKT_T *pkt, SNMPADDR_T *src, SNMPADDR_T *dst){ pkt->user_private = 0; MEMCPY(&(pkt->pkt_src), src, sizeof(SNMPADDR_T)); MEMCPY(&(pkt->pkt_dst), dst, sizeof(SNMPADDR_T)); return 0;}/* V3 table management functions - these functions should validate the * request and/or update the NV Store as indicated *//* include all the v3 header files we need */#include <wrn/wm/snmp/engine/v3_acc.h>#include <wrn/wm/snmp/engine/v3_ntfy.h>#include <wrn/wm/snmp/engine/v3_trgt.h>#include <wrn/wm/snmp/engine/v3_user.h>sbits32_t NV_Op( int table, int op, int stage, SNMP_PKT_T *pkt, VB_T *vbp, void *cur, void *new ){ switch ( stage ) { case STAGE_TEST: { int mod = ( op == OP_CREATE || op == OP_UPDATE ) ? 0 : NV_MOD_DESTROY; switch ( table ) { case TABLE_VIEW: return SNMP_NV_View_2275_Add_Mod((struct VIEWLEAF_S *)cur, (struct VIEWLEAF_S *)new, mod);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -