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