snmp.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef _SNMP_H_
  2. #define _SNMP_H_
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. // SNMP Debug Message (dump) Enable
  7. #define _SNMP_DEBUG_
  8. #define PORT_SNMP_AGENT 161
  9. #define PORT_SNMP_TRAP 162
  10. #define SNMP_V1 0
  11. #define MAX_OID 12
  12. #define MAX_STRING 64
  13. #define MAX_SNMPMSG_LEN 512
  14. #define MAX_TRAPMSG_LEN 512
  15. // SNMP Error code
  16. #define SNMP_SUCCESS 0
  17. #define OID_NOT_FOUND -1
  18. #define TABLE_FULL -2
  19. #define ILLEGAL_LENGTH -3
  20. #define INVALID_ENTRY_ID -4
  21. #define INVALID_DATA_TYPE -5
  22. #define NO_SUCH_NAME 2
  23. #define BAD_VALUE 3
  24. // SNMPv1 Commands
  25. #define GET_REQUEST 0xa0
  26. #define GET_NEXT_REQUEST 0xa1
  27. #define GET_RESPONSE 0xa2
  28. #define SET_REQUEST 0xa3
  29. // Macros: SNMPv1 request validation checker
  30. #define VALID_REQUEST(x) ((x == GET_REQUEST) || (x == GET_NEXT_REQUEST) || (x == SET_REQUEST))
  31. // SNMPv1 Return Types
  32. #define SNMPDTYPE_INTEGER 0x02
  33. #define SNMPDTYPE_OCTET_STRING 0x04
  34. #define SNMPDTYPE_NULL_ITEM 0x05
  35. #define SNMPDTYPE_OBJ_ID 0x06
  36. #define SNMPDTYPE_SEQUENCE 0x30
  37. #define SNMPDTYPE_SEQUENCE_OF SNMPDTYPE_SEQUENCE
  38. #define SNMPDTYPE_COUNTER 0x41
  39. #define SNMPDTYPE_GAUGE 0x42
  40. #define SNMPDTYPE_TIME_TICKS 0x43
  41. #define SNMPDTYPE_OPAQUE 0x44
  42. // SNMP Trap: Standard Trap Types (Generic)
  43. #define SNMPTRAP_COLDSTART 0x00 // Generic trap-type 0: Cold Start
  44. #define SNMPTRAP_WARMSTART 0x01 // Generic trap-type 1: Warm Start
  45. #define SNMPTRAP_LINKDOWN 0x02 // Generic trap-type 2: Link Down
  46. #define SNMPTRAP_LINKUP 0x03 // Generic trap-type 3: Link Up
  47. #define SNMPTRAP_AUTHENTICATION 0x04 // Generic trap-type 4: Authentication Failure
  48. #define SNMPTRAP_EGPNEIGHBORLOSS 0x05 // Generic trap-type 5: EGP Neighbor Loss
  49. // Macros
  50. #define COPY_SEGMENT(x) \
  51. { \
  52. request_msg.index += seglen; \
  53. memcpy(&response_msg.buffer[response_msg.index], &request_msg.buffer[x.start], seglen ); \
  54. response_msg.index += seglen; \
  55. }
  56. #ifndef HTONL
  57. #define HTONL(x) \
  58. ((((x) >> 24) & 0x000000ff) | \
  59. (((x) >> 8) & 0x0000ff00) | \
  60. (((x) << 8) & 0x00ff0000) | \
  61. (((x) << 24) & 0xff000000))
  62. #endif
  63. typedef struct {
  64. uint8_t oidlen;
  65. uint8_t oid[MAX_OID];
  66. uint8_t dataType;
  67. uint8_t dataLen;
  68. union {
  69. uint8_t octetstring[MAX_STRING];
  70. uint32_t intval;
  71. } u;
  72. void (*getfunction)(void *, uint8_t *);
  73. void (*setfunction)(int32_t);
  74. } dataEntryType;
  75. struct messageStruct {
  76. uint8_t buffer[MAX_SNMPMSG_LEN];
  77. int32_t len;
  78. int32_t index;
  79. };
  80. typedef struct {
  81. int32_t start; /* Absolute Index of the TLV */
  82. int32_t len; /* The L value of the TLV */
  83. int32_t vstart; /* Absolute Index of this TLV's Value */
  84. int32_t nstart; /* Absolute Index of the next TLV */
  85. } tlvStructType;
  86. /********************************************************************************************/
  87. /* SNMP : Functions */
  88. /********************************************************************************************/
  89. // SNMP Main functions
  90. void snmpd_init(uint8_t * managerIP, uint8_t * agentIP, uint8_t sn_agent, uint8_t sn_trap);
  91. int32_t snmpd_run(void);
  92. int32_t snmp_sendTrap(uint8_t * managerIP, uint8_t * agentIP, int8_t* community, dataEntryType enterprise_oid, uint32_t genericTrap, uint32_t specificTrap, uint32_t va_count, ...);
  93. // SNMP Time handler functions
  94. void SNMP_time_handler(void);
  95. uint32_t getSNMPTimeTick(void);
  96. void currentUptime(void *ptr, uint8_t *len);
  97. #ifdef __cplusplus
  98. }
  99. #endif
  100. #endif