typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32;
typedef signed char sint8; typedef signed short sint16; typedef signed int sint32;
#define MAXOID 20 #define MAXKEY 4 #define MAXKLEN 22 #define MAXVAR 60
#define scompare(a, b) strcmp((const char *)a, (const char *)b) #define sncompare(a, b, c) strncmp((const char *)a, (const char *)b, c) #define slength(a) strlen((const char *)a) #define scopy(a, b) strcpy((char *)a, (const char *)b) #define sncopy(a, b, c) strncpy((char *)a, (const char *)b, c)
#define Integer 0x02ul #define String 0x04ul #define Null 0x05ul #define Identifier 0x06ul #define OctetString 0x14ul #define Sequence 0x30ul #define IpAddress 0x40ul #define Counter 0x41ul #define Counter32 0x41ul #define Gauge 0x42ul #define Ticks 0x43ul
#define GetRequest 0xa0ul #define GetNextRequest 0xa1ul #define GetResponse 0xa2ul #define TrapV1 0xa4ul #define SetRequest 0xa3ul #define GetBulkRequest 0xa5ul #define Trap 0xa7ul #define Report 0xa8ul
/* Version 2 error values */ #define tooBig 1 #define noSuchName 2 #define badValue 3 #define readOnly 4 #define genErr 5 #define noAccess 6 #define wrongType 7 #define wrongLength 8 #define wrongEncoding 9 #define wrongValue 10 #define noCreation 11 #define inconsistentValue 12 #define resourceUnavailable 13 #define commitFailed 14 #define undoFailed 15 #define authorizationError 16 #define notWritable 17 #define inconsistentName 18
#define IMMED 0x01 /* Immediate value in mvp->len */ #define IMMED2 0x02 /* Immediate value in mvp->type + len */ #define BASE1 0x03 /* Base 0 in data space, base 1 in MIB */ #define SCALAR 0x04 /* Table not indexed (no offset) */ #define W 0x08 /* Write allowed */ #define SX 0x10 /* Sequential table index inferred */ #define NWORDER 0x20 /* Network byte ordering for basic type */ #define CAR 0x40 /* Call application after read */ #define CAW 0x80 /* Call application before write */
/* VACM return values */ #define accessAllowed 0 #define noSuchContext 1
struct COUNTER64 { uint32 hi, lo; };
typedef struct { uint8 nlen, name[MAXOID]; } OID;
typedef struct { OID oid; /* Object ID name and length */ uint16 ix; /* Index values (offsets) */ uint16 len; /* Length of table */ void *empty; /* empty flag */ } MIBTAB;
typedef struct { OID oid; /* Object ID name and length */ uint16 opt; /* Options (with choice) */ uint8 type; /* Type of variable */ sint16 len; /* Length of pointer field */ void *ptr; /* Pointer to possible variable data */ } MIBVAR;
/* Define SNMP access to a particular MIB */ typedef struct { const MIBVAR *mvp; /* MIB variables */ sint16 (*numvars)(void); /* Number of variables */ const MIBTAB *mtp; /* MIB tables */ sint16 (*numtabs)(void); /* Number of tables */ void (*get)(sint16 varix, sint16 tabix, uint8 **vvptr); sint16 (*set)(sint16 varix, sint16 tabix); sint16 (*index)(sint16 varix, sint16 index); void (*init)(uint16 type); /* Initialize the MIB */ sint16 (*check)(sint16 varix, sint16 tabix, const uint8 *inp); } MIB;
typedef struct { const MIB **mibs; /* Array of pointers to host MIBs */ uint16 nummibs; /* Number of host MIBs */ uint16 trapv; /* Trap version and startup trap type */ } AGENT_CONTEXT;
/* Function prototypes */ void snmpInit(void); void SnmpRefresh(void); sint16 inReqOutRep(uint8 **obp, uint16 olen, const uint8 *ibp, uint16 ilen);
/* Mid-level API */ sint16 ussSNMPAgentInit(const AGENT_CONTEXT *ac); sint16 ussSNMPAgentTrap(uint8 type, uint8 spec, unsigned char*contextName, const uint8 *vbs, uint16 len);
/* Utility */ void snmpEncodeIndex(uint8 **pp, const MIB *mibp, const MIBTAB *mtp, sint16 tabix); sint32 snmpVCompare(const uint8 *op1, sint16 len1, const uint8 *op2, sint16 len2);
sint32 snmpFindOID(const uint8 **retp, const uint8 *base, sint16 osize, sint16 onum, const uint8 *valp, sint16 vlen); sint32 snmpFindIndex(sint16 *tabixp, const MIBTAB *mtp, const MIB *mibp, const MIBVAR *mvp, const uint8 *reqixname, uint8 reqixlen, uint8 nflag); void snmpEncodeID(uint8 **pp, uint8 olen, uint32 val);
sint16 snmpReadLength(const uint8 **pp, uint16 type); sint16 snmpReadInt(uint32 *outp, uint8 olen, const uint8 **inp, uint16 type); sint16 snmpReadVal(uint8 *outp, uint8 olen, const uint8 **inp, uint16 type);
void snmpRWriteLength(uint8 **pp, uint16 type, sint16 len); void snmpRWriteInt(uint8 **pp, uint32 val, uint16 type, sint16 len); void snmpRWriteVal(uint8 **pp, const uint8 *vp, uint16 type, sint16 len);
/* VACM module */ sint16 isAccessAllowed(const uint8 *contextName);
// snmpFindIndex() Find a table entry index
sint32 snmpFindIndex(sint16 *tabixp, const MIBTAB *mtp, const MIB *mibp, const MIBVAR *mvp, const uint8 *reqixname, uint8 reqixlen, uint8 nflag) { uint8 *cp; sint32 sl1, sl2; sint16 i1, i3; uint8 ixname[MAXKLEN];
/* Default return values */ *tabixp = -1; sl2 = 0;
/* Search through each table entry's index for the element */ for (i1 = 0; ; i1++) { i3 = mibp->index(mvp - mibp->mvp, i1); if (i3 < 0) break; /* -1 -- End of table reached */ if (i3 == 0) continue; /* 0 -- Skip table entry */
/* Encode the table entry at the current index */ cp = ixname + MAXKLEN; snmpEncodeIndex(&cp, mibp, mtp, i1);
/* ** Compare agent encoding with requested encoding. ** Save the smallest positive difference. */ sl1 = snmpVCompare(cp, (const uint8 *)&ixname[MAXKLEN] - cp, reqixname, reqixlen); if (nflag) { if (sl1 >= 255) { sl2 = 1; break; } } else { if (sl1 == 0) { sl2 = 1; break; } } } *tabixp = i1;
return sl2; } |
|