snmp_agent_message.h
Go to the documentation of this file.
1 /**
2  * @file snmp_message.h
3  * @brief SNMP message formatting and parsing
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _SNMP_AGENT_MESSAGE_H
30 #define _SNMP_AGENT_MESSAGE_H
31 
32 //Dependencies
33 #include "core/net.h"
34 #include "snmp/snmp_agent.h"
35 
36 //SNMPv1 message header overhead
37 #define SNMP_V1_MSG_HEADER_OVERHEAD 48
38 //SNMPv2c message header overhead
39 #define SNMP_V2C_MSG_HEADER_OVERHEAD 37
40 //SNMPv3 message header overhead
41 #define SNMP_V3_MSG_HEADER_OVERHEAD 105
42 
43 //C++ guard
44 #ifdef __cplusplus
45  extern "C" {
46 #endif
47 
48 
49 /**
50  * @brief SNMP message
51  **/
52 
53 typedef struct
54 {
55  uint8_t buffer[SNMP_MAX_MSG_SIZE]; ///<Buffer that holds the message
56  size_t bufferLen; ///<Original length of the message
57  uint8_t *pos; ///<Current position
58  size_t length; ///<Length of the message
59  int32_t version; ///<SNMP version identifier
60 #if (SNMP_V1_SUPPORT == ENABLED || SNMP_V2C_SUPPORT == ENABLED)
61  const char_t *community; ///<Community name
62  size_t communityLen; ///<Length of the community name
63 #endif
64 #if (SNMP_V3_SUPPORT == ENABLED)
65  int32_t msgId; ///<Message identifier
66  int32_t msgMaxSize; ///<Maximum message size supported by the sender
67  uint8_t msgFlags; ///<Bit fields which control processing of the message
68  int32_t msgSecurityModel; ///<Security model used by the sender
69  const uint8_t *msgAuthEngineId; ///<Authoritative engine identifier
70  size_t msgAuthEngineIdLen; ///<Length of the authoritative engine identifier
71  int32_t msgAuthEngineBoots; ///<Number of times the SNMP engine has rebooted
72  int32_t msgAuthEngineTime; ///<Number of seconds since last reboot
73  const char_t *msgUserName; ///<User name
74  size_t msgUserNameLen; ///<Length of the user name
75  uint8_t *msgAuthParameters; ///<Authentication parameters
76  size_t msgAuthParametersLen; ///<Length of the authentication parameters
77  const uint8_t *msgPrivParameters; ///<Privacy parameters
78  size_t msgPrivParametersLen; ///<Length of the privacy parameters
79  const uint8_t *contextEngineId; ///<Context engine identifier
80  size_t contextEngineIdLen; ///<Length of the context engine identifier
81  const char_t *contextName; ///<Context name
82  size_t contextNameLen; ///<Length of the context name
83 #endif
84  SnmpPduType pduType; ///<PDU type
85  int32_t requestId; ///<Request identifier
86  int32_t errorStatus; ///<Error status
87  int32_t errorIndex; ///<Error index
88 #if (SNMP_V1_SUPPORT == ENABLED)
89  const uint8_t *enterpriseOid; ///<Type of object generating trap
90  size_t enterpriseOidLen; ///<Length of the enterprise OID
91  Ipv4Addr agentAddr; ///<Address of object generating trap
92  int32_t genericTrapType; ///<Generic trap type
93  int32_t specificTrapCode; ///<Specific trap code
94  uint32_t timestamp; ///<Timestamp
95 #endif
96 #if (SNMP_V2C_SUPPORT == ENABLED || SNMP_V3_SUPPORT == ENABLED)
97  int32_t nonRepeaters; ///<GetBulkRequest-PDU specific parameter
98  int32_t maxRepetitions; ///<GetBulkRequest-PDU specific parameter
99 #endif
100  uint8_t *varBindList; ///<List of variable bindings
101  size_t varBindListLen; ///<Length of the list in bytes
102  size_t varBindListMaxLen; ///<Maximum length of the list in bytes
103  size_t oidLen; ///<Length of the object identifier
104 } SnmpMessage;
105 
106 
107 /**
108  * @brief Variable binding
109  **/
110 
111 typedef struct
112 {
113  const uint8_t *oid;
114  size_t oidLen;
117  const uint8_t *value;
118  size_t valueLen;
119 } SnmpVarBind;
120 
121 
122 //SNMP related functions
125 
127 
130 
133 
136 
139 
142 
145 
146 error_t snmpEncodeInt32(int32_t value, uint8_t *dest, size_t *length);
147 error_t snmpEncodeUnsignedInt32(uint32_t value, uint8_t *dest, size_t *length);
148 error_t snmpEncodeUnsignedInt64(uint64_t value, uint8_t *dest, size_t *length);
149 
150 error_t snmpDecodeInt32(const uint8_t *src, size_t length, int32_t *value);
151 error_t snmpDecodeUnsignedInt32(const uint8_t *src, size_t length, uint32_t *value);
152 error_t snmpDecodeUnsignedInt64(const uint8_t *src, size_t length, uint64_t *value);
153 
154 //C++ guard
155 #ifdef __cplusplus
156  }
157 #endif
158 
159 #endif
error_t snmpDecodeInt32(const uint8_t *src, size_t length, int32_t *value)
Decode a 32-bit signed integer.
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:232
size_t length
Length of the message.
error_t snmpParseMessageHeader(SnmpMessage *message)
Parse SNMP message header.
int32_t specificTrapCode
Specific trap code.
uint8_t * msgAuthParameters
Authentication parameters.
const uint8_t * contextEngineId
Context engine identifier.
int32_t msgId
Message identifier.
char char_t
Definition: compiler_port.h:41
size_t msgAuthParametersLen
Length of the authentication parameters.
error_t snmpDecodeUnsignedInt64(const uint8_t *src, size_t length, uint64_t *value)
Decode a 64-bit unsigned integer.
size_t enterpriseOidLen
Length of the enterprise OID.
TCP/IP stack core.
size_t communityLen
Length of the community name.
int32_t msgAuthEngineTime
Number of seconds since last reboot.
int32_t errorIndex
Error index.
Ipv4Addr agentAddr
Address of object generating trap.
const char_t * msgUserName
User name.
uint8_t message[]
Definition: chap.h:150
const uint8_t * msgPrivParameters
Privacy parameters.
int32_t errorStatus
Error status.
size_t msgUserNameLen
Length of the user name.
error_t snmpParsePduHeader(SnmpMessage *message)
Parse PDU header.
error_t snmpEncodeInt32(int32_t value, uint8_t *dest, size_t *length)
Encode a 32-bit signed integer.
int32_t msgAuthEngineBoots
Number of times the SNMP engine has rebooted.
const uint8_t * value
error_t snmpDecodeUnsignedInt32(const uint8_t *src, size_t length, uint32_t *value)
Decode a 32-bit unsigned integer.
error_t snmpParseScopedPdu(SnmpMessage *message)
Parse scopedPDU field.
error_t snmpWriteMessageHeader(SnmpMessage *message)
Format SNMP message header.
int32_t requestId
Request identifier.
const uint8_t * oid
Variable binding.
error_t snmpEncodeUnsignedInt64(uint64_t value, uint8_t *dest, size_t *length)
Encode a 64-bit unsigned integer.
int32_t msgSecurityModel
Security model used by the sender.
int32_t nonRepeaters
GetBulkRequest-PDU specific parameter.
size_t bufferLen
Original length of the message.
error_t snmpWritePduHeader(SnmpMessage *message)
Format PDU header.
SNMP agent (Simple Network Management Protocol)
int32_t genericTrapType
Generic trap type.
size_t varBindListMaxLen
Maximum length of the list in bytes.
error_t snmpParseCommunity(SnmpMessage *message)
Parse community name.
error_t snmpParseGlobalData(SnmpMessage *message)
Parse msgGlobalData field.
size_t msgPrivParametersLen
Length of the privacy parameters.
const uint8_t * enterpriseOid
Type of object generating trap.
error_t snmpWriteScopedPdu(SnmpMessage *message)
Format scopedPDU.
uint8_t * pos
Current position.
SnmpPduType
SNMP PDU types.
Definition: snmp_common.h:146
SnmpPduType pduType
PDU type.
error_t snmpEncodeUnsignedInt32(uint32_t value, uint8_t *dest, size_t *length)
Encode a 32-bit unsigned integer.
const char_t * contextName
Context name.
error_t snmpParseSecurityParameters(SnmpMessage *message)
Parse msgSecurityParameters field.
uint32_t timestamp
Timestamp.
error_t snmpWriteCommunity(SnmpMessage *message)
Format community name.
error_t snmpComputeMessageOverhead(SnmpMessage *message)
Compute SNMP message overhead.
const char_t * community
Community name.
size_t msgAuthEngineIdLen
Length of the authoritative engine identifier.
error_t snmpInitResponse(SnmpAgentContext *context)
Initialize a GetResponse-PDU.
error_t
Error codes.
Definition: error.h:40
SNMP message.
void snmpInitMessage(SnmpMessage *message)
Initialize a SNMP message.
unsigned int uint_t
Definition: compiler_port.h:43
size_t oidLen
Length of the object identifier.
uint8_t value[]
Definition: dtls_misc.h:141
const uint8_t * msgAuthEngineId
Authoritative engine identifier.
int32_t maxRepetitions
GetBulkRequest-PDU specific parameter.
error_t snmpWriteSecurityParameters(SnmpMessage *message)
Format msgSecurityParameters field.
size_t varBindListLen
Length of the list in bytes.
#define SNMP_MAX_MSG_SIZE
Definition: snmp_common.h:58
#define SnmpAgentContext
Definition: snmp_agent.h:34
size_t contextNameLen
Length of the context name.
uint8_t length
Definition: dtls_misc.h:140
uint8_t msgFlags
Bit fields which control processing of the message.
int32_t msgMaxSize
Maximum message size supported by the sender.
size_t contextEngineIdLen
Length of the context engine identifier.
error_t snmpWriteGlobalData(SnmpMessage *message)
Format msgGlobalData field.
int32_t version
SNMP version identifier.
uint8_t * varBindList
List of variable bindings.