Webserver+3d print
cyclone_tcp/snmp/snmp_agent.c@0:8918a71cdbe9, 2017-02-04 (annotated)
- Committer:
- Sergunb
- Date:
- Sat Feb 04 18:15:49 2017 +0000
- Revision:
- 0:8918a71cdbe9
nothing else
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergunb | 0:8918a71cdbe9 | 1 | /** |
Sergunb | 0:8918a71cdbe9 | 2 | * @file snmp_agent.c |
Sergunb | 0:8918a71cdbe9 | 3 | * @brief SNMP agent (Simple Network Management Protocol) |
Sergunb | 0:8918a71cdbe9 | 4 | * |
Sergunb | 0:8918a71cdbe9 | 5 | * @section License |
Sergunb | 0:8918a71cdbe9 | 6 | * |
Sergunb | 0:8918a71cdbe9 | 7 | * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. |
Sergunb | 0:8918a71cdbe9 | 8 | * |
Sergunb | 0:8918a71cdbe9 | 9 | * This file is part of CycloneTCP Open. |
Sergunb | 0:8918a71cdbe9 | 10 | * |
Sergunb | 0:8918a71cdbe9 | 11 | * This program is free software; you can redistribute it and/or |
Sergunb | 0:8918a71cdbe9 | 12 | * modify it under the terms of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 13 | * as published by the Free Software Foundation; either version 2 |
Sergunb | 0:8918a71cdbe9 | 14 | * of the License, or (at your option) any later version. |
Sergunb | 0:8918a71cdbe9 | 15 | * |
Sergunb | 0:8918a71cdbe9 | 16 | * This program is distributed in the hope that it will be useful, |
Sergunb | 0:8918a71cdbe9 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Sergunb | 0:8918a71cdbe9 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Sergunb | 0:8918a71cdbe9 | 19 | * GNU General Public License for more details. |
Sergunb | 0:8918a71cdbe9 | 20 | * |
Sergunb | 0:8918a71cdbe9 | 21 | * You should have received a copy of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 22 | * along with this program; if not, write to the Free Software Foundation, |
Sergunb | 0:8918a71cdbe9 | 23 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Sergunb | 0:8918a71cdbe9 | 24 | * |
Sergunb | 0:8918a71cdbe9 | 25 | * @section Description |
Sergunb | 0:8918a71cdbe9 | 26 | * |
Sergunb | 0:8918a71cdbe9 | 27 | * SNMP is a simple protocol by which management information for a network |
Sergunb | 0:8918a71cdbe9 | 28 | * element may be inspected or altered by logically remote users. Refer |
Sergunb | 0:8918a71cdbe9 | 29 | * to the following RFCs for complete details: |
Sergunb | 0:8918a71cdbe9 | 30 | * - RFC 1157: A Simple Network Management Protocol (SNMP) |
Sergunb | 0:8918a71cdbe9 | 31 | * - RFC 1905: Protocol Operations for Version 2 of the Simple Network |
Sergunb | 0:8918a71cdbe9 | 32 | * Management Protocol (SNMPv2) |
Sergunb | 0:8918a71cdbe9 | 33 | * - RFC 3410: Introduction and Applicability Statements for Internet |
Sergunb | 0:8918a71cdbe9 | 34 | * Standard Management Framework |
Sergunb | 0:8918a71cdbe9 | 35 | * - RFC 3411: An Architecture for Describing SNMP Management Frameworks |
Sergunb | 0:8918a71cdbe9 | 36 | * - RFC 3412: Message Processing and Dispatching for the SNMP |
Sergunb | 0:8918a71cdbe9 | 37 | * - RFC 3413: Simple Network Management Protocol (SNMP) Applications |
Sergunb | 0:8918a71cdbe9 | 38 | * - RFC 3584: Coexistence between Version 1, Version 2, and Version 3 of |
Sergunb | 0:8918a71cdbe9 | 39 | * SNMP Framework |
Sergunb | 0:8918a71cdbe9 | 40 | * |
Sergunb | 0:8918a71cdbe9 | 41 | * @author Oryx Embedded SARL (www.oryx-embedded.com) |
Sergunb | 0:8918a71cdbe9 | 42 | * @version 1.7.6 |
Sergunb | 0:8918a71cdbe9 | 43 | **/ |
Sergunb | 0:8918a71cdbe9 | 44 | |
Sergunb | 0:8918a71cdbe9 | 45 | //Switch to the appropriate trace level |
Sergunb | 0:8918a71cdbe9 | 46 | #define TRACE_LEVEL SNMP_TRACE_LEVEL |
Sergunb | 0:8918a71cdbe9 | 47 | |
Sergunb | 0:8918a71cdbe9 | 48 | //Dependencies |
Sergunb | 0:8918a71cdbe9 | 49 | #include "core/net.h" |
Sergunb | 0:8918a71cdbe9 | 50 | #include "snmp/snmp_agent.h" |
Sergunb | 0:8918a71cdbe9 | 51 | #include "snmp/snmp_agent_dispatch.h" |
Sergunb | 0:8918a71cdbe9 | 52 | #include "snmp/snmp_agent_pdu.h" |
Sergunb | 0:8918a71cdbe9 | 53 | #include "snmp/snmp_agent_misc.h" |
Sergunb | 0:8918a71cdbe9 | 54 | #include "mibs/mib2_module.h" |
Sergunb | 0:8918a71cdbe9 | 55 | #include "crypto.h" |
Sergunb | 0:8918a71cdbe9 | 56 | #include "asn1.h" |
Sergunb | 0:8918a71cdbe9 | 57 | #include "oid.h" |
Sergunb | 0:8918a71cdbe9 | 58 | #include "debug.h" |
Sergunb | 0:8918a71cdbe9 | 59 | |
Sergunb | 0:8918a71cdbe9 | 60 | //Check TCP/IP stack configuration |
Sergunb | 0:8918a71cdbe9 | 61 | #if (SNMP_AGENT_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 62 | |
Sergunb | 0:8918a71cdbe9 | 63 | |
Sergunb | 0:8918a71cdbe9 | 64 | /** |
Sergunb | 0:8918a71cdbe9 | 65 | * @brief Initialize settings with default values |
Sergunb | 0:8918a71cdbe9 | 66 | * @param[out] settings Structure that contains SNMP agent settings |
Sergunb | 0:8918a71cdbe9 | 67 | **/ |
Sergunb | 0:8918a71cdbe9 | 68 | |
Sergunb | 0:8918a71cdbe9 | 69 | void snmpAgentGetDefaultSettings(SnmpAgentSettings *settings) |
Sergunb | 0:8918a71cdbe9 | 70 | { |
Sergunb | 0:8918a71cdbe9 | 71 | //The SNMP agent is not bound to any interface |
Sergunb | 0:8918a71cdbe9 | 72 | settings->interface = NULL; |
Sergunb | 0:8918a71cdbe9 | 73 | |
Sergunb | 0:8918a71cdbe9 | 74 | //Minimum version accepted by the SNMP agent |
Sergunb | 0:8918a71cdbe9 | 75 | settings->versionMin = SNMP_VERSION_1; |
Sergunb | 0:8918a71cdbe9 | 76 | //Maximum version accepted by the SNMP agent |
Sergunb | 0:8918a71cdbe9 | 77 | settings->versionMax = SNMP_VERSION_3; |
Sergunb | 0:8918a71cdbe9 | 78 | |
Sergunb | 0:8918a71cdbe9 | 79 | //SNMP port number |
Sergunb | 0:8918a71cdbe9 | 80 | settings->port = SNMP_PORT; |
Sergunb | 0:8918a71cdbe9 | 81 | //SNMP trap port number |
Sergunb | 0:8918a71cdbe9 | 82 | settings->trapPort = SNMP_TRAP_PORT; |
Sergunb | 0:8918a71cdbe9 | 83 | |
Sergunb | 0:8918a71cdbe9 | 84 | //Random data generation callback function |
Sergunb | 0:8918a71cdbe9 | 85 | settings->randCallback = NULL; |
Sergunb | 0:8918a71cdbe9 | 86 | } |
Sergunb | 0:8918a71cdbe9 | 87 | |
Sergunb | 0:8918a71cdbe9 | 88 | |
Sergunb | 0:8918a71cdbe9 | 89 | /** |
Sergunb | 0:8918a71cdbe9 | 90 | * @brief SNMP agent initialization |
Sergunb | 0:8918a71cdbe9 | 91 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 92 | * @param[in] settings SNMP agent specific settings |
Sergunb | 0:8918a71cdbe9 | 93 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 94 | **/ |
Sergunb | 0:8918a71cdbe9 | 95 | |
Sergunb | 0:8918a71cdbe9 | 96 | error_t snmpAgentInit(SnmpAgentContext *context, const SnmpAgentSettings *settings) |
Sergunb | 0:8918a71cdbe9 | 97 | { |
Sergunb | 0:8918a71cdbe9 | 98 | error_t error; |
Sergunb | 0:8918a71cdbe9 | 99 | |
Sergunb | 0:8918a71cdbe9 | 100 | //Debug message |
Sergunb | 0:8918a71cdbe9 | 101 | TRACE_INFO("Initializing SNMP agent...\r\n"); |
Sergunb | 0:8918a71cdbe9 | 102 | |
Sergunb | 0:8918a71cdbe9 | 103 | //Ensure the parameters are valid |
Sergunb | 0:8918a71cdbe9 | 104 | if(context == NULL || settings == NULL) |
Sergunb | 0:8918a71cdbe9 | 105 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 106 | |
Sergunb | 0:8918a71cdbe9 | 107 | //Check minimum and maximum SNMP versions |
Sergunb | 0:8918a71cdbe9 | 108 | if(settings->versionMin > settings->versionMax) |
Sergunb | 0:8918a71cdbe9 | 109 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 110 | |
Sergunb | 0:8918a71cdbe9 | 111 | //Clear the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 112 | memset(context, 0, sizeof(SnmpAgentContext)); |
Sergunb | 0:8918a71cdbe9 | 113 | |
Sergunb | 0:8918a71cdbe9 | 114 | //Save user settings |
Sergunb | 0:8918a71cdbe9 | 115 | context->settings = *settings; |
Sergunb | 0:8918a71cdbe9 | 116 | |
Sergunb | 0:8918a71cdbe9 | 117 | #if (SNMP_V3_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 118 | //Get current time |
Sergunb | 0:8918a71cdbe9 | 119 | context->systemTime = osGetSystemTime(); |
Sergunb | 0:8918a71cdbe9 | 120 | |
Sergunb | 0:8918a71cdbe9 | 121 | //Each SNMP engine maintains two values, snmpEngineBoots and snmpEngineTime, |
Sergunb | 0:8918a71cdbe9 | 122 | //which taken together provide an indication of time at that SNMP engine |
Sergunb | 0:8918a71cdbe9 | 123 | context->engineBoots = 1; |
Sergunb | 0:8918a71cdbe9 | 124 | context->engineTime = 0; |
Sergunb | 0:8918a71cdbe9 | 125 | |
Sergunb | 0:8918a71cdbe9 | 126 | //Check whether SNMPv3 is supported |
Sergunb | 0:8918a71cdbe9 | 127 | if(settings->versionMin <= SNMP_VERSION_3 && |
Sergunb | 0:8918a71cdbe9 | 128 | settings->versionMax >= SNMP_VERSION_3) |
Sergunb | 0:8918a71cdbe9 | 129 | { |
Sergunb | 0:8918a71cdbe9 | 130 | //Make sure a random number generator has been registered |
Sergunb | 0:8918a71cdbe9 | 131 | if(settings->randCallback == NULL) |
Sergunb | 0:8918a71cdbe9 | 132 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 133 | |
Sergunb | 0:8918a71cdbe9 | 134 | //The salt integer is initialized to an arbitrary value at boot time |
Sergunb | 0:8918a71cdbe9 | 135 | error = settings->randCallback((uint8_t *) &context->salt, sizeof(context->salt)); |
Sergunb | 0:8918a71cdbe9 | 136 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 137 | if(error) |
Sergunb | 0:8918a71cdbe9 | 138 | return error; |
Sergunb | 0:8918a71cdbe9 | 139 | } |
Sergunb | 0:8918a71cdbe9 | 140 | #endif |
Sergunb | 0:8918a71cdbe9 | 141 | |
Sergunb | 0:8918a71cdbe9 | 142 | //Create a mutex to prevent simultaneous access to SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 143 | if(!osCreateMutex(&context->mutex)) |
Sergunb | 0:8918a71cdbe9 | 144 | { |
Sergunb | 0:8918a71cdbe9 | 145 | //Failed to create mutex |
Sergunb | 0:8918a71cdbe9 | 146 | return ERROR_OUT_OF_RESOURCES; |
Sergunb | 0:8918a71cdbe9 | 147 | } |
Sergunb | 0:8918a71cdbe9 | 148 | |
Sergunb | 0:8918a71cdbe9 | 149 | //Open a UDP socket |
Sergunb | 0:8918a71cdbe9 | 150 | context->socket = socketOpen(SOCKET_TYPE_DGRAM, SOCKET_IP_PROTO_UDP); |
Sergunb | 0:8918a71cdbe9 | 151 | |
Sergunb | 0:8918a71cdbe9 | 152 | //Failed to open socket? |
Sergunb | 0:8918a71cdbe9 | 153 | if(!context->socket) |
Sergunb | 0:8918a71cdbe9 | 154 | { |
Sergunb | 0:8918a71cdbe9 | 155 | //Clean up side effects |
Sergunb | 0:8918a71cdbe9 | 156 | osDeleteMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 157 | //Report an error |
Sergunb | 0:8918a71cdbe9 | 158 | return ERROR_OPEN_FAILED; |
Sergunb | 0:8918a71cdbe9 | 159 | } |
Sergunb | 0:8918a71cdbe9 | 160 | |
Sergunb | 0:8918a71cdbe9 | 161 | //Start of exception handling block |
Sergunb | 0:8918a71cdbe9 | 162 | do |
Sergunb | 0:8918a71cdbe9 | 163 | { |
Sergunb | 0:8918a71cdbe9 | 164 | //Explicitly associate the socket with the relevant interface |
Sergunb | 0:8918a71cdbe9 | 165 | error = socketBindToInterface(context->socket, settings->interface); |
Sergunb | 0:8918a71cdbe9 | 166 | //Unable to bind the socket to the desired interface? |
Sergunb | 0:8918a71cdbe9 | 167 | if(error) |
Sergunb | 0:8918a71cdbe9 | 168 | break; |
Sergunb | 0:8918a71cdbe9 | 169 | |
Sergunb | 0:8918a71cdbe9 | 170 | //The SNMP agent listens for messages on port 161 |
Sergunb | 0:8918a71cdbe9 | 171 | error = socketBind(context->socket, &IP_ADDR_ANY, settings->port); |
Sergunb | 0:8918a71cdbe9 | 172 | //Unable to bind the socket to the desired port? |
Sergunb | 0:8918a71cdbe9 | 173 | if(error) |
Sergunb | 0:8918a71cdbe9 | 174 | break; |
Sergunb | 0:8918a71cdbe9 | 175 | |
Sergunb | 0:8918a71cdbe9 | 176 | //End of exception handling block |
Sergunb | 0:8918a71cdbe9 | 177 | } while(0); |
Sergunb | 0:8918a71cdbe9 | 178 | |
Sergunb | 0:8918a71cdbe9 | 179 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 180 | if(error) |
Sergunb | 0:8918a71cdbe9 | 181 | { |
Sergunb | 0:8918a71cdbe9 | 182 | //Clean up side effects |
Sergunb | 0:8918a71cdbe9 | 183 | osDeleteMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 184 | //Close underlying socket |
Sergunb | 0:8918a71cdbe9 | 185 | socketClose(context->socket); |
Sergunb | 0:8918a71cdbe9 | 186 | } |
Sergunb | 0:8918a71cdbe9 | 187 | |
Sergunb | 0:8918a71cdbe9 | 188 | //Return status code |
Sergunb | 0:8918a71cdbe9 | 189 | return error; |
Sergunb | 0:8918a71cdbe9 | 190 | } |
Sergunb | 0:8918a71cdbe9 | 191 | |
Sergunb | 0:8918a71cdbe9 | 192 | |
Sergunb | 0:8918a71cdbe9 | 193 | /** |
Sergunb | 0:8918a71cdbe9 | 194 | * @brief Start SNMP agent |
Sergunb | 0:8918a71cdbe9 | 195 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 196 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 197 | **/ |
Sergunb | 0:8918a71cdbe9 | 198 | |
Sergunb | 0:8918a71cdbe9 | 199 | error_t snmpAgentStart(SnmpAgentContext *context) |
Sergunb | 0:8918a71cdbe9 | 200 | { |
Sergunb | 0:8918a71cdbe9 | 201 | OsTask *task; |
Sergunb | 0:8918a71cdbe9 | 202 | |
Sergunb | 0:8918a71cdbe9 | 203 | //Debug message |
Sergunb | 0:8918a71cdbe9 | 204 | TRACE_INFO("Starting SNMP agent...\r\n"); |
Sergunb | 0:8918a71cdbe9 | 205 | |
Sergunb | 0:8918a71cdbe9 | 206 | //Make sure the SNMP agent context is valid |
Sergunb | 0:8918a71cdbe9 | 207 | if(context == NULL) |
Sergunb | 0:8918a71cdbe9 | 208 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 209 | |
Sergunb | 0:8918a71cdbe9 | 210 | //Start the SNMP agent service |
Sergunb | 0:8918a71cdbe9 | 211 | task = osCreateTask("SNMP Agent", (OsTaskCode) snmpAgentTask, |
Sergunb | 0:8918a71cdbe9 | 212 | context, SNMP_AGENT_STACK_SIZE, SNMP_AGENT_PRIORITY); |
Sergunb | 0:8918a71cdbe9 | 213 | |
Sergunb | 0:8918a71cdbe9 | 214 | //Unable to create the task? |
Sergunb | 0:8918a71cdbe9 | 215 | if(task == OS_INVALID_HANDLE) |
Sergunb | 0:8918a71cdbe9 | 216 | return ERROR_OUT_OF_RESOURCES; |
Sergunb | 0:8918a71cdbe9 | 217 | |
Sergunb | 0:8918a71cdbe9 | 218 | //The SNMP agent has successfully started |
Sergunb | 0:8918a71cdbe9 | 219 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 220 | } |
Sergunb | 0:8918a71cdbe9 | 221 | |
Sergunb | 0:8918a71cdbe9 | 222 | |
Sergunb | 0:8918a71cdbe9 | 223 | /** |
Sergunb | 0:8918a71cdbe9 | 224 | * @brief Load a MIB module |
Sergunb | 0:8918a71cdbe9 | 225 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 226 | * @param[in] module Pointer the MIB module to be loaded |
Sergunb | 0:8918a71cdbe9 | 227 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 228 | **/ |
Sergunb | 0:8918a71cdbe9 | 229 | |
Sergunb | 0:8918a71cdbe9 | 230 | error_t snmpAgentLoadMib(SnmpAgentContext *context, const MibModule *module) |
Sergunb | 0:8918a71cdbe9 | 231 | { |
Sergunb | 0:8918a71cdbe9 | 232 | error_t error; |
Sergunb | 0:8918a71cdbe9 | 233 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 234 | uint_t j; |
Sergunb | 0:8918a71cdbe9 | 235 | |
Sergunb | 0:8918a71cdbe9 | 236 | //Check parameters |
Sergunb | 0:8918a71cdbe9 | 237 | if(context == NULL || module == NULL) |
Sergunb | 0:8918a71cdbe9 | 238 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 239 | if(module->numObjects < 1) |
Sergunb | 0:8918a71cdbe9 | 240 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 241 | |
Sergunb | 0:8918a71cdbe9 | 242 | //Acquire exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 243 | osAcquireMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 244 | |
Sergunb | 0:8918a71cdbe9 | 245 | //Loop through existing MIBs |
Sergunb | 0:8918a71cdbe9 | 246 | for(i = 0; i < context->mibModuleCount; i++) |
Sergunb | 0:8918a71cdbe9 | 247 | { |
Sergunb | 0:8918a71cdbe9 | 248 | //Check whether the specified MIB module is already loaded |
Sergunb | 0:8918a71cdbe9 | 249 | if(context->mibModule[i] == module) |
Sergunb | 0:8918a71cdbe9 | 250 | break; |
Sergunb | 0:8918a71cdbe9 | 251 | } |
Sergunb | 0:8918a71cdbe9 | 252 | |
Sergunb | 0:8918a71cdbe9 | 253 | //MIB module found? |
Sergunb | 0:8918a71cdbe9 | 254 | if(i < context->mibModuleCount) |
Sergunb | 0:8918a71cdbe9 | 255 | { |
Sergunb | 0:8918a71cdbe9 | 256 | //Prevent the SNMP agent from loading the specified MIB multiple times |
Sergunb | 0:8918a71cdbe9 | 257 | error = NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 258 | } |
Sergunb | 0:8918a71cdbe9 | 259 | else |
Sergunb | 0:8918a71cdbe9 | 260 | { |
Sergunb | 0:8918a71cdbe9 | 261 | //Make sure there is enough room to add the specified MIB |
Sergunb | 0:8918a71cdbe9 | 262 | if(context->mibModuleCount < SNMP_AGENT_MAX_MIB_COUNT) |
Sergunb | 0:8918a71cdbe9 | 263 | { |
Sergunb | 0:8918a71cdbe9 | 264 | //Loop through existing MIBs |
Sergunb | 0:8918a71cdbe9 | 265 | for(i = 0; i < context->mibModuleCount; i++) |
Sergunb | 0:8918a71cdbe9 | 266 | { |
Sergunb | 0:8918a71cdbe9 | 267 | //Compare object identifiers |
Sergunb | 0:8918a71cdbe9 | 268 | if(oidComp(module->objects[0].oid, module->objects[0].oidLen, |
Sergunb | 0:8918a71cdbe9 | 269 | context->mibModule[i]->objects[0].oid, context->mibModule[i]->objects[0].oidLen) < 0) |
Sergunb | 0:8918a71cdbe9 | 270 | { |
Sergunb | 0:8918a71cdbe9 | 271 | //Make room for the new MIB |
Sergunb | 0:8918a71cdbe9 | 272 | for(j = context->mibModuleCount; j > i; j--) |
Sergunb | 0:8918a71cdbe9 | 273 | context->mibModule[j] = context->mibModule[j - 1]; |
Sergunb | 0:8918a71cdbe9 | 274 | |
Sergunb | 0:8918a71cdbe9 | 275 | //We are done |
Sergunb | 0:8918a71cdbe9 | 276 | break; |
Sergunb | 0:8918a71cdbe9 | 277 | } |
Sergunb | 0:8918a71cdbe9 | 278 | } |
Sergunb | 0:8918a71cdbe9 | 279 | |
Sergunb | 0:8918a71cdbe9 | 280 | //Insert the new MIB to the list |
Sergunb | 0:8918a71cdbe9 | 281 | context->mibModule[i] = module; |
Sergunb | 0:8918a71cdbe9 | 282 | //Update the number of MIBs |
Sergunb | 0:8918a71cdbe9 | 283 | context->mibModuleCount++; |
Sergunb | 0:8918a71cdbe9 | 284 | |
Sergunb | 0:8918a71cdbe9 | 285 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 286 | error = NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 287 | } |
Sergunb | 0:8918a71cdbe9 | 288 | else |
Sergunb | 0:8918a71cdbe9 | 289 | { |
Sergunb | 0:8918a71cdbe9 | 290 | //Failed to load the specified MIB |
Sergunb | 0:8918a71cdbe9 | 291 | error = ERROR_OUT_OF_RESOURCES; |
Sergunb | 0:8918a71cdbe9 | 292 | } |
Sergunb | 0:8918a71cdbe9 | 293 | } |
Sergunb | 0:8918a71cdbe9 | 294 | |
Sergunb | 0:8918a71cdbe9 | 295 | //Release exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 296 | osReleaseMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 297 | |
Sergunb | 0:8918a71cdbe9 | 298 | //Return status code |
Sergunb | 0:8918a71cdbe9 | 299 | return error; |
Sergunb | 0:8918a71cdbe9 | 300 | } |
Sergunb | 0:8918a71cdbe9 | 301 | |
Sergunb | 0:8918a71cdbe9 | 302 | |
Sergunb | 0:8918a71cdbe9 | 303 | /** |
Sergunb | 0:8918a71cdbe9 | 304 | * @brief Unload a MIB module |
Sergunb | 0:8918a71cdbe9 | 305 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 306 | * @param[in] module Pointer the MIB module to be unloaded |
Sergunb | 0:8918a71cdbe9 | 307 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 308 | **/ |
Sergunb | 0:8918a71cdbe9 | 309 | |
Sergunb | 0:8918a71cdbe9 | 310 | error_t snmpAgentUnloadMib(SnmpAgentContext *context, const MibModule *module) |
Sergunb | 0:8918a71cdbe9 | 311 | { |
Sergunb | 0:8918a71cdbe9 | 312 | error_t error; |
Sergunb | 0:8918a71cdbe9 | 313 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 314 | uint_t j; |
Sergunb | 0:8918a71cdbe9 | 315 | |
Sergunb | 0:8918a71cdbe9 | 316 | //Check parameters |
Sergunb | 0:8918a71cdbe9 | 317 | if(context == NULL || module == NULL) |
Sergunb | 0:8918a71cdbe9 | 318 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 319 | |
Sergunb | 0:8918a71cdbe9 | 320 | //Acquire exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 321 | osAcquireMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 322 | |
Sergunb | 0:8918a71cdbe9 | 323 | //Loop through existing MIBs |
Sergunb | 0:8918a71cdbe9 | 324 | for(i = 0; i < context->mibModuleCount; i++) |
Sergunb | 0:8918a71cdbe9 | 325 | { |
Sergunb | 0:8918a71cdbe9 | 326 | //Check whether the specified MIB module is already loaded |
Sergunb | 0:8918a71cdbe9 | 327 | if(context->mibModule[i] == module) |
Sergunb | 0:8918a71cdbe9 | 328 | break; |
Sergunb | 0:8918a71cdbe9 | 329 | } |
Sergunb | 0:8918a71cdbe9 | 330 | |
Sergunb | 0:8918a71cdbe9 | 331 | //MIB module found? |
Sergunb | 0:8918a71cdbe9 | 332 | if(i < context->mibModuleCount) |
Sergunb | 0:8918a71cdbe9 | 333 | { |
Sergunb | 0:8918a71cdbe9 | 334 | //Update the number of MIBs |
Sergunb | 0:8918a71cdbe9 | 335 | context->mibModuleCount--; |
Sergunb | 0:8918a71cdbe9 | 336 | |
Sergunb | 0:8918a71cdbe9 | 337 | //Remove the specified MIB from the list |
Sergunb | 0:8918a71cdbe9 | 338 | for(j = i; j < context->mibModuleCount; j++) |
Sergunb | 0:8918a71cdbe9 | 339 | context->mibModule[j] = context->mibModule[j + 1]; |
Sergunb | 0:8918a71cdbe9 | 340 | |
Sergunb | 0:8918a71cdbe9 | 341 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 342 | error = NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 343 | } |
Sergunb | 0:8918a71cdbe9 | 344 | else |
Sergunb | 0:8918a71cdbe9 | 345 | { |
Sergunb | 0:8918a71cdbe9 | 346 | //Failed to unload the specified MIB |
Sergunb | 0:8918a71cdbe9 | 347 | error = ERROR_NOT_FOUND; |
Sergunb | 0:8918a71cdbe9 | 348 | } |
Sergunb | 0:8918a71cdbe9 | 349 | |
Sergunb | 0:8918a71cdbe9 | 350 | //Release exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 351 | osReleaseMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 352 | |
Sergunb | 0:8918a71cdbe9 | 353 | //Return status code |
Sergunb | 0:8918a71cdbe9 | 354 | return error; |
Sergunb | 0:8918a71cdbe9 | 355 | } |
Sergunb | 0:8918a71cdbe9 | 356 | |
Sergunb | 0:8918a71cdbe9 | 357 | |
Sergunb | 0:8918a71cdbe9 | 358 | /** |
Sergunb | 0:8918a71cdbe9 | 359 | * @brief Set the value of the snmpEngineBoots variable |
Sergunb | 0:8918a71cdbe9 | 360 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 361 | * @param[in] engineBoots Number of times the SNMP engine has re-booted |
Sergunb | 0:8918a71cdbe9 | 362 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 363 | **/ |
Sergunb | 0:8918a71cdbe9 | 364 | |
Sergunb | 0:8918a71cdbe9 | 365 | error_t snmpAgentSetEngineBoots(SnmpAgentContext *context, int32_t engineBoots) |
Sergunb | 0:8918a71cdbe9 | 366 | { |
Sergunb | 0:8918a71cdbe9 | 367 | #if (SNMP_V3_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 368 | //Check parameters |
Sergunb | 0:8918a71cdbe9 | 369 | if(context == NULL) |
Sergunb | 0:8918a71cdbe9 | 370 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 371 | if(engineBoots < 0) |
Sergunb | 0:8918a71cdbe9 | 372 | return ERROR_OUT_OF_RANGE; |
Sergunb | 0:8918a71cdbe9 | 373 | |
Sergunb | 0:8918a71cdbe9 | 374 | //Acquire exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 375 | osAcquireMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 376 | |
Sergunb | 0:8918a71cdbe9 | 377 | //Get current time |
Sergunb | 0:8918a71cdbe9 | 378 | context->systemTime = osGetSystemTime(); |
Sergunb | 0:8918a71cdbe9 | 379 | |
Sergunb | 0:8918a71cdbe9 | 380 | //Set the value of the snmpEngineBoots |
Sergunb | 0:8918a71cdbe9 | 381 | context->engineBoots = engineBoots; |
Sergunb | 0:8918a71cdbe9 | 382 | //The snmpEngineTime is reset to zero |
Sergunb | 0:8918a71cdbe9 | 383 | context->engineTime = 0; |
Sergunb | 0:8918a71cdbe9 | 384 | |
Sergunb | 0:8918a71cdbe9 | 385 | //Release exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 386 | osReleaseMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 387 | |
Sergunb | 0:8918a71cdbe9 | 388 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 389 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 390 | #else |
Sergunb | 0:8918a71cdbe9 | 391 | //Not implemented |
Sergunb | 0:8918a71cdbe9 | 392 | return ERROR_NOT_IMPLEMENTED; |
Sergunb | 0:8918a71cdbe9 | 393 | #endif |
Sergunb | 0:8918a71cdbe9 | 394 | } |
Sergunb | 0:8918a71cdbe9 | 395 | |
Sergunb | 0:8918a71cdbe9 | 396 | |
Sergunb | 0:8918a71cdbe9 | 397 | /** |
Sergunb | 0:8918a71cdbe9 | 398 | * @brief Get the value of the snmpEngineBoots variable |
Sergunb | 0:8918a71cdbe9 | 399 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 400 | * @param[out] engineBoots Number of times the SNMP engine has re-booted |
Sergunb | 0:8918a71cdbe9 | 401 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 402 | **/ |
Sergunb | 0:8918a71cdbe9 | 403 | |
Sergunb | 0:8918a71cdbe9 | 404 | error_t snmpAgentGetEngineBoots(SnmpAgentContext *context, int32_t *engineBoots) |
Sergunb | 0:8918a71cdbe9 | 405 | { |
Sergunb | 0:8918a71cdbe9 | 406 | #if (SNMP_V3_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 407 | //Check parameters |
Sergunb | 0:8918a71cdbe9 | 408 | if(context == NULL || engineBoots == NULL) |
Sergunb | 0:8918a71cdbe9 | 409 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 410 | |
Sergunb | 0:8918a71cdbe9 | 411 | //Acquire exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 412 | osAcquireMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 413 | //Get the current value of the snmpEngineBoots |
Sergunb | 0:8918a71cdbe9 | 414 | *engineBoots = context->engineBoots; |
Sergunb | 0:8918a71cdbe9 | 415 | //Release exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 416 | osReleaseMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 417 | |
Sergunb | 0:8918a71cdbe9 | 418 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 419 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 420 | #else |
Sergunb | 0:8918a71cdbe9 | 421 | //Not implemented |
Sergunb | 0:8918a71cdbe9 | 422 | return ERROR_NOT_IMPLEMENTED; |
Sergunb | 0:8918a71cdbe9 | 423 | #endif |
Sergunb | 0:8918a71cdbe9 | 424 | } |
Sergunb | 0:8918a71cdbe9 | 425 | |
Sergunb | 0:8918a71cdbe9 | 426 | |
Sergunb | 0:8918a71cdbe9 | 427 | /** |
Sergunb | 0:8918a71cdbe9 | 428 | * @brief Set enterprise OID |
Sergunb | 0:8918a71cdbe9 | 429 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 430 | * @param[in] enterpriseOid Pointer to the enterprise OID |
Sergunb | 0:8918a71cdbe9 | 431 | * @param[in] enterpriseOidLen Length of the enterprise OID |
Sergunb | 0:8918a71cdbe9 | 432 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 433 | **/ |
Sergunb | 0:8918a71cdbe9 | 434 | |
Sergunb | 0:8918a71cdbe9 | 435 | error_t snmpAgentSetEnterpriseOid(SnmpAgentContext *context, |
Sergunb | 0:8918a71cdbe9 | 436 | const uint8_t *enterpriseOid, size_t enterpriseOidLen) |
Sergunb | 0:8918a71cdbe9 | 437 | { |
Sergunb | 0:8918a71cdbe9 | 438 | //Check parameters |
Sergunb | 0:8918a71cdbe9 | 439 | if(context == NULL || enterpriseOid == NULL) |
Sergunb | 0:8918a71cdbe9 | 440 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 441 | if(enterpriseOidLen > SNMP_MAX_OID_SIZE) |
Sergunb | 0:8918a71cdbe9 | 442 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 443 | |
Sergunb | 0:8918a71cdbe9 | 444 | //Acquire exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 445 | osAcquireMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 446 | |
Sergunb | 0:8918a71cdbe9 | 447 | //Set enterprise OID |
Sergunb | 0:8918a71cdbe9 | 448 | memcpy(context->enterpriseOid, enterpriseOid, enterpriseOidLen); |
Sergunb | 0:8918a71cdbe9 | 449 | //Save the length of the enterprise OID |
Sergunb | 0:8918a71cdbe9 | 450 | context->enterpriseOidLen = enterpriseOidLen; |
Sergunb | 0:8918a71cdbe9 | 451 | |
Sergunb | 0:8918a71cdbe9 | 452 | //Release exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 453 | osReleaseMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 454 | |
Sergunb | 0:8918a71cdbe9 | 455 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 456 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 457 | } |
Sergunb | 0:8918a71cdbe9 | 458 | |
Sergunb | 0:8918a71cdbe9 | 459 | |
Sergunb | 0:8918a71cdbe9 | 460 | /** |
Sergunb | 0:8918a71cdbe9 | 461 | * @brief Set context engine identifier |
Sergunb | 0:8918a71cdbe9 | 462 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 463 | * @param[in] contextEngine Pointer to the context engine identifier |
Sergunb | 0:8918a71cdbe9 | 464 | * @param[in] contextEngineLen Length of the context engine identifier |
Sergunb | 0:8918a71cdbe9 | 465 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 466 | **/ |
Sergunb | 0:8918a71cdbe9 | 467 | |
Sergunb | 0:8918a71cdbe9 | 468 | error_t snmpAgentSetContextEngine(SnmpAgentContext *context, |
Sergunb | 0:8918a71cdbe9 | 469 | const void *contextEngine, size_t contextEngineLen) |
Sergunb | 0:8918a71cdbe9 | 470 | { |
Sergunb | 0:8918a71cdbe9 | 471 | #if (SNMP_V3_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 472 | //Check parameters |
Sergunb | 0:8918a71cdbe9 | 473 | if(context == NULL || contextEngine == NULL) |
Sergunb | 0:8918a71cdbe9 | 474 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 475 | if(contextEngineLen > SNMP_MAX_CONTEXT_ENGINE_SIZE) |
Sergunb | 0:8918a71cdbe9 | 476 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 477 | |
Sergunb | 0:8918a71cdbe9 | 478 | //Acquire exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 479 | osAcquireMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 480 | |
Sergunb | 0:8918a71cdbe9 | 481 | //Set context engine identifier |
Sergunb | 0:8918a71cdbe9 | 482 | memcpy(context->contextEngine, contextEngine, contextEngineLen); |
Sergunb | 0:8918a71cdbe9 | 483 | //Save the length of the context engine identifier |
Sergunb | 0:8918a71cdbe9 | 484 | context->contextEngineLen = contextEngineLen; |
Sergunb | 0:8918a71cdbe9 | 485 | |
Sergunb | 0:8918a71cdbe9 | 486 | //Release exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 487 | osReleaseMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 488 | |
Sergunb | 0:8918a71cdbe9 | 489 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 490 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 491 | #else |
Sergunb | 0:8918a71cdbe9 | 492 | //Not implemented |
Sergunb | 0:8918a71cdbe9 | 493 | return ERROR_NOT_IMPLEMENTED; |
Sergunb | 0:8918a71cdbe9 | 494 | #endif |
Sergunb | 0:8918a71cdbe9 | 495 | } |
Sergunb | 0:8918a71cdbe9 | 496 | |
Sergunb | 0:8918a71cdbe9 | 497 | |
Sergunb | 0:8918a71cdbe9 | 498 | /** |
Sergunb | 0:8918a71cdbe9 | 499 | * @brief Set context name |
Sergunb | 0:8918a71cdbe9 | 500 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 501 | * @param[in] contextName NULL-terminated string that contains the context name |
Sergunb | 0:8918a71cdbe9 | 502 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 503 | **/ |
Sergunb | 0:8918a71cdbe9 | 504 | |
Sergunb | 0:8918a71cdbe9 | 505 | error_t snmpAgentSetContextName(SnmpAgentContext *context, |
Sergunb | 0:8918a71cdbe9 | 506 | const char_t *contextName) |
Sergunb | 0:8918a71cdbe9 | 507 | { |
Sergunb | 0:8918a71cdbe9 | 508 | #if (SNMP_V3_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 509 | size_t n; |
Sergunb | 0:8918a71cdbe9 | 510 | |
Sergunb | 0:8918a71cdbe9 | 511 | //Check parameters |
Sergunb | 0:8918a71cdbe9 | 512 | if(context == NULL || contextName == NULL) |
Sergunb | 0:8918a71cdbe9 | 513 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 514 | |
Sergunb | 0:8918a71cdbe9 | 515 | //Retrieve the length of the context name |
Sergunb | 0:8918a71cdbe9 | 516 | n = strlen(contextName); |
Sergunb | 0:8918a71cdbe9 | 517 | |
Sergunb | 0:8918a71cdbe9 | 518 | //Make sure the context name is valid |
Sergunb | 0:8918a71cdbe9 | 519 | if(n > SNMP_MAX_CONTEXT_NAME_LEN) |
Sergunb | 0:8918a71cdbe9 | 520 | return ERROR_INVALID_LENGTH; |
Sergunb | 0:8918a71cdbe9 | 521 | |
Sergunb | 0:8918a71cdbe9 | 522 | //Acquire exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 523 | osAcquireMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 524 | //Set context name |
Sergunb | 0:8918a71cdbe9 | 525 | strcpy(context->contextName, contextName); |
Sergunb | 0:8918a71cdbe9 | 526 | //Release exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 527 | osReleaseMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 528 | |
Sergunb | 0:8918a71cdbe9 | 529 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 530 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 531 | #else |
Sergunb | 0:8918a71cdbe9 | 532 | //Not implemented |
Sergunb | 0:8918a71cdbe9 | 533 | return ERROR_NOT_IMPLEMENTED; |
Sergunb | 0:8918a71cdbe9 | 534 | #endif |
Sergunb | 0:8918a71cdbe9 | 535 | } |
Sergunb | 0:8918a71cdbe9 | 536 | |
Sergunb | 0:8918a71cdbe9 | 537 | |
Sergunb | 0:8918a71cdbe9 | 538 | /** |
Sergunb | 0:8918a71cdbe9 | 539 | * @brief Create a new community string |
Sergunb | 0:8918a71cdbe9 | 540 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 541 | * @param[in] community NULL-terminated string that contains the community name |
Sergunb | 0:8918a71cdbe9 | 542 | * @param[in] mode Access rights |
Sergunb | 0:8918a71cdbe9 | 543 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 544 | **/ |
Sergunb | 0:8918a71cdbe9 | 545 | |
Sergunb | 0:8918a71cdbe9 | 546 | error_t snmpAgentCreateCommunity(SnmpAgentContext *context, |
Sergunb | 0:8918a71cdbe9 | 547 | const char_t *community, SnmpAccess mode) |
Sergunb | 0:8918a71cdbe9 | 548 | { |
Sergunb | 0:8918a71cdbe9 | 549 | #if (SNMP_V1_SUPPORT == ENABLED || SNMP_V2C_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 550 | //Add the community string to the local configuration datastore |
Sergunb | 0:8918a71cdbe9 | 551 | return snmpAgentCreateUser(context, community, mode, SNMP_KEY_FORMAT_NONE, |
Sergunb | 0:8918a71cdbe9 | 552 | SNMP_AUTH_PROTOCOL_NONE, NULL, SNMP_PRIV_PROTOCOL_NONE, NULL); |
Sergunb | 0:8918a71cdbe9 | 553 | #else |
Sergunb | 0:8918a71cdbe9 | 554 | //Not implemented |
Sergunb | 0:8918a71cdbe9 | 555 | return ERROR_NOT_IMPLEMENTED; |
Sergunb | 0:8918a71cdbe9 | 556 | #endif |
Sergunb | 0:8918a71cdbe9 | 557 | } |
Sergunb | 0:8918a71cdbe9 | 558 | |
Sergunb | 0:8918a71cdbe9 | 559 | |
Sergunb | 0:8918a71cdbe9 | 560 | /** |
Sergunb | 0:8918a71cdbe9 | 561 | * @brief Remove a community string |
Sergunb | 0:8918a71cdbe9 | 562 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 563 | * @param[in] community NULL-terminated string that contains the community name |
Sergunb | 0:8918a71cdbe9 | 564 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 565 | **/ |
Sergunb | 0:8918a71cdbe9 | 566 | |
Sergunb | 0:8918a71cdbe9 | 567 | error_t snmpAgentDeleteCommunity(SnmpAgentContext *context, const char_t *community) |
Sergunb | 0:8918a71cdbe9 | 568 | { |
Sergunb | 0:8918a71cdbe9 | 569 | #if (SNMP_V1_SUPPORT == ENABLED || SNMP_V2C_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 570 | //Remove the community string from the local configuration datastore |
Sergunb | 0:8918a71cdbe9 | 571 | return snmpAgentDeleteUser(context, community); |
Sergunb | 0:8918a71cdbe9 | 572 | #else |
Sergunb | 0:8918a71cdbe9 | 573 | //Not implemented |
Sergunb | 0:8918a71cdbe9 | 574 | return ERROR_NOT_IMPLEMENTED; |
Sergunb | 0:8918a71cdbe9 | 575 | #endif |
Sergunb | 0:8918a71cdbe9 | 576 | } |
Sergunb | 0:8918a71cdbe9 | 577 | |
Sergunb | 0:8918a71cdbe9 | 578 | |
Sergunb | 0:8918a71cdbe9 | 579 | /** |
Sergunb | 0:8918a71cdbe9 | 580 | * @brief Create a new user |
Sergunb | 0:8918a71cdbe9 | 581 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 582 | * @param[in] username NULL-terminated string that contains the user name |
Sergunb | 0:8918a71cdbe9 | 583 | * @param[in] mode Access rights |
Sergunb | 0:8918a71cdbe9 | 584 | * @param[in] keyFormat Key format (ASCII password or raw key) |
Sergunb | 0:8918a71cdbe9 | 585 | * @param[in] authProtocol Authentication type |
Sergunb | 0:8918a71cdbe9 | 586 | * @param[in] authKey Key to be used for data authentication |
Sergunb | 0:8918a71cdbe9 | 587 | * @param[in] privProtocol Privacy type |
Sergunb | 0:8918a71cdbe9 | 588 | * @param[in] privKey Key to be used for data encryption |
Sergunb | 0:8918a71cdbe9 | 589 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 590 | **/ |
Sergunb | 0:8918a71cdbe9 | 591 | |
Sergunb | 0:8918a71cdbe9 | 592 | error_t snmpAgentCreateUser(SnmpAgentContext *context, |
Sergunb | 0:8918a71cdbe9 | 593 | const char_t *username, SnmpAccess mode, SnmpKeyFormat keyFormat, |
Sergunb | 0:8918a71cdbe9 | 594 | SnmpAuthProtocol authProtocol, const void *authKey, |
Sergunb | 0:8918a71cdbe9 | 595 | SnmpPrivProtocol privProtocol, const void *privKey) |
Sergunb | 0:8918a71cdbe9 | 596 | { |
Sergunb | 0:8918a71cdbe9 | 597 | error_t error; |
Sergunb | 0:8918a71cdbe9 | 598 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 599 | size_t n; |
Sergunb | 0:8918a71cdbe9 | 600 | SnmpUserInfo *entry; |
Sergunb | 0:8918a71cdbe9 | 601 | SnmpUserInfo *firstFreeEntry; |
Sergunb | 0:8918a71cdbe9 | 602 | |
Sergunb | 0:8918a71cdbe9 | 603 | //Check parameters |
Sergunb | 0:8918a71cdbe9 | 604 | if(context == NULL || username == NULL) |
Sergunb | 0:8918a71cdbe9 | 605 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 606 | |
Sergunb | 0:8918a71cdbe9 | 607 | //Data authentication? |
Sergunb | 0:8918a71cdbe9 | 608 | if(authProtocol != SNMP_AUTH_PROTOCOL_NONE) |
Sergunb | 0:8918a71cdbe9 | 609 | { |
Sergunb | 0:8918a71cdbe9 | 610 | //Check key format |
Sergunb | 0:8918a71cdbe9 | 611 | if(keyFormat != SNMP_KEY_FORMAT_TEXT && keyFormat != SNMP_KEY_FORMAT_RAW) |
Sergunb | 0:8918a71cdbe9 | 612 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 613 | |
Sergunb | 0:8918a71cdbe9 | 614 | //Data authentication requires a key |
Sergunb | 0:8918a71cdbe9 | 615 | if(authKey == NULL) |
Sergunb | 0:8918a71cdbe9 | 616 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 617 | } |
Sergunb | 0:8918a71cdbe9 | 618 | |
Sergunb | 0:8918a71cdbe9 | 619 | //Data confidentiality? |
Sergunb | 0:8918a71cdbe9 | 620 | if(privProtocol != SNMP_PRIV_PROTOCOL_NONE) |
Sergunb | 0:8918a71cdbe9 | 621 | { |
Sergunb | 0:8918a71cdbe9 | 622 | //Check key format |
Sergunb | 0:8918a71cdbe9 | 623 | if(keyFormat != SNMP_KEY_FORMAT_TEXT && keyFormat != SNMP_KEY_FORMAT_RAW) |
Sergunb | 0:8918a71cdbe9 | 624 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 625 | |
Sergunb | 0:8918a71cdbe9 | 626 | //Data confidentiality requires a key |
Sergunb | 0:8918a71cdbe9 | 627 | if(privKey == NULL) |
Sergunb | 0:8918a71cdbe9 | 628 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 629 | |
Sergunb | 0:8918a71cdbe9 | 630 | //There is no provision for data confidentiality without data authentication |
Sergunb | 0:8918a71cdbe9 | 631 | if(authProtocol == SNMP_AUTH_PROTOCOL_NONE) |
Sergunb | 0:8918a71cdbe9 | 632 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 633 | } |
Sergunb | 0:8918a71cdbe9 | 634 | |
Sergunb | 0:8918a71cdbe9 | 635 | //Retrieve the length of the user name |
Sergunb | 0:8918a71cdbe9 | 636 | n = strlen(username); |
Sergunb | 0:8918a71cdbe9 | 637 | |
Sergunb | 0:8918a71cdbe9 | 638 | //Make sure the user name is valid |
Sergunb | 0:8918a71cdbe9 | 639 | if(n == 0 || n > SNMP_MAX_USER_NAME_LEN) |
Sergunb | 0:8918a71cdbe9 | 640 | return ERROR_INVALID_LENGTH; |
Sergunb | 0:8918a71cdbe9 | 641 | |
Sergunb | 0:8918a71cdbe9 | 642 | //Acquire exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 643 | osAcquireMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 644 | |
Sergunb | 0:8918a71cdbe9 | 645 | //Keep track of the first free entry |
Sergunb | 0:8918a71cdbe9 | 646 | firstFreeEntry = NULL; |
Sergunb | 0:8918a71cdbe9 | 647 | |
Sergunb | 0:8918a71cdbe9 | 648 | //Loop through the list of users |
Sergunb | 0:8918a71cdbe9 | 649 | for(i = 0; i < SNMP_AGENT_MAX_USER_COUNT; i++) |
Sergunb | 0:8918a71cdbe9 | 650 | { |
Sergunb | 0:8918a71cdbe9 | 651 | //Point to the current entry |
Sergunb | 0:8918a71cdbe9 | 652 | entry = &context->userTable[i]; |
Sergunb | 0:8918a71cdbe9 | 653 | |
Sergunb | 0:8918a71cdbe9 | 654 | //Check if the entry is currently in use |
Sergunb | 0:8918a71cdbe9 | 655 | if(entry->name[0] != '\0') |
Sergunb | 0:8918a71cdbe9 | 656 | { |
Sergunb | 0:8918a71cdbe9 | 657 | //Check whether the user name already exists |
Sergunb | 0:8918a71cdbe9 | 658 | if(!strcmp(entry->name, username)) |
Sergunb | 0:8918a71cdbe9 | 659 | break; |
Sergunb | 0:8918a71cdbe9 | 660 | } |
Sergunb | 0:8918a71cdbe9 | 661 | else |
Sergunb | 0:8918a71cdbe9 | 662 | { |
Sergunb | 0:8918a71cdbe9 | 663 | //Keep track of the first free entry |
Sergunb | 0:8918a71cdbe9 | 664 | if(firstFreeEntry == NULL) |
Sergunb | 0:8918a71cdbe9 | 665 | firstFreeEntry = entry; |
Sergunb | 0:8918a71cdbe9 | 666 | } |
Sergunb | 0:8918a71cdbe9 | 667 | } |
Sergunb | 0:8918a71cdbe9 | 668 | |
Sergunb | 0:8918a71cdbe9 | 669 | //If the specified user name does not exist, then a new |
Sergunb | 0:8918a71cdbe9 | 670 | //entry should be created |
Sergunb | 0:8918a71cdbe9 | 671 | if(i >= SNMP_AGENT_MAX_USER_COUNT) |
Sergunb | 0:8918a71cdbe9 | 672 | entry = firstFreeEntry; |
Sergunb | 0:8918a71cdbe9 | 673 | |
Sergunb | 0:8918a71cdbe9 | 674 | //Check whether the service list runs out of space |
Sergunb | 0:8918a71cdbe9 | 675 | if(entry != NULL) |
Sergunb | 0:8918a71cdbe9 | 676 | { |
Sergunb | 0:8918a71cdbe9 | 677 | //Save user name |
Sergunb | 0:8918a71cdbe9 | 678 | strcpy(entry->name, username); |
Sergunb | 0:8918a71cdbe9 | 679 | //Access rights |
Sergunb | 0:8918a71cdbe9 | 680 | entry->mode = mode; |
Sergunb | 0:8918a71cdbe9 | 681 | |
Sergunb | 0:8918a71cdbe9 | 682 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 683 | error = NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 684 | |
Sergunb | 0:8918a71cdbe9 | 685 | #if (SNMP_V3_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 686 | //Authentication protocol |
Sergunb | 0:8918a71cdbe9 | 687 | entry->authProtocol = authProtocol; |
Sergunb | 0:8918a71cdbe9 | 688 | //Privacy protocol |
Sergunb | 0:8918a71cdbe9 | 689 | entry->privProtocol = privProtocol; |
Sergunb | 0:8918a71cdbe9 | 690 | |
Sergunb | 0:8918a71cdbe9 | 691 | //Data authentication? |
Sergunb | 0:8918a71cdbe9 | 692 | if(authProtocol != SNMP_AUTH_PROTOCOL_NONE) |
Sergunb | 0:8918a71cdbe9 | 693 | { |
Sergunb | 0:8918a71cdbe9 | 694 | //ASCII password or raw key? |
Sergunb | 0:8918a71cdbe9 | 695 | if(keyFormat == SNMP_KEY_FORMAT_TEXT) |
Sergunb | 0:8918a71cdbe9 | 696 | { |
Sergunb | 0:8918a71cdbe9 | 697 | //Generate the authentication key from the provided password |
Sergunb | 0:8918a71cdbe9 | 698 | error = snmpGenerateKey(authProtocol, authKey, context->contextEngine, |
Sergunb | 0:8918a71cdbe9 | 699 | context->contextEngineLen, &entry->authKey); |
Sergunb | 0:8918a71cdbe9 | 700 | } |
Sergunb | 0:8918a71cdbe9 | 701 | else |
Sergunb | 0:8918a71cdbe9 | 702 | { |
Sergunb | 0:8918a71cdbe9 | 703 | //Save the authentication key |
Sergunb | 0:8918a71cdbe9 | 704 | memcpy(&entry->authKey, authKey, sizeof(SnmpKey)); |
Sergunb | 0:8918a71cdbe9 | 705 | } |
Sergunb | 0:8918a71cdbe9 | 706 | } |
Sergunb | 0:8918a71cdbe9 | 707 | |
Sergunb | 0:8918a71cdbe9 | 708 | //Check status code |
Sergunb | 0:8918a71cdbe9 | 709 | if(!error) |
Sergunb | 0:8918a71cdbe9 | 710 | { |
Sergunb | 0:8918a71cdbe9 | 711 | //Data confidentiality? |
Sergunb | 0:8918a71cdbe9 | 712 | if(privProtocol != SNMP_PRIV_PROTOCOL_NONE) |
Sergunb | 0:8918a71cdbe9 | 713 | { |
Sergunb | 0:8918a71cdbe9 | 714 | //ASCII password or raw key? |
Sergunb | 0:8918a71cdbe9 | 715 | if(keyFormat == SNMP_KEY_FORMAT_TEXT) |
Sergunb | 0:8918a71cdbe9 | 716 | { |
Sergunb | 0:8918a71cdbe9 | 717 | //Generate the privacy key from the provided password |
Sergunb | 0:8918a71cdbe9 | 718 | error = snmpGenerateKey(authProtocol, privKey, context->contextEngine, |
Sergunb | 0:8918a71cdbe9 | 719 | context->contextEngineLen, &entry->privKey); |
Sergunb | 0:8918a71cdbe9 | 720 | } |
Sergunb | 0:8918a71cdbe9 | 721 | else |
Sergunb | 0:8918a71cdbe9 | 722 | { |
Sergunb | 0:8918a71cdbe9 | 723 | //Save the privacy key |
Sergunb | 0:8918a71cdbe9 | 724 | memcpy(&entry->privKey, privKey, sizeof(SnmpKey)); |
Sergunb | 0:8918a71cdbe9 | 725 | } |
Sergunb | 0:8918a71cdbe9 | 726 | } |
Sergunb | 0:8918a71cdbe9 | 727 | } |
Sergunb | 0:8918a71cdbe9 | 728 | |
Sergunb | 0:8918a71cdbe9 | 729 | //Check status code |
Sergunb | 0:8918a71cdbe9 | 730 | if(error) |
Sergunb | 0:8918a71cdbe9 | 731 | { |
Sergunb | 0:8918a71cdbe9 | 732 | //Clean up side effects |
Sergunb | 0:8918a71cdbe9 | 733 | memset(entry, 0, sizeof(SnmpUserInfo)); |
Sergunb | 0:8918a71cdbe9 | 734 | } |
Sergunb | 0:8918a71cdbe9 | 735 | #endif |
Sergunb | 0:8918a71cdbe9 | 736 | } |
Sergunb | 0:8918a71cdbe9 | 737 | else |
Sergunb | 0:8918a71cdbe9 | 738 | { |
Sergunb | 0:8918a71cdbe9 | 739 | //Unable to add new user |
Sergunb | 0:8918a71cdbe9 | 740 | error = ERROR_OUT_OF_RESOURCES; |
Sergunb | 0:8918a71cdbe9 | 741 | } |
Sergunb | 0:8918a71cdbe9 | 742 | |
Sergunb | 0:8918a71cdbe9 | 743 | //Release exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 744 | osReleaseMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 745 | |
Sergunb | 0:8918a71cdbe9 | 746 | //Return error code |
Sergunb | 0:8918a71cdbe9 | 747 | return error; |
Sergunb | 0:8918a71cdbe9 | 748 | } |
Sergunb | 0:8918a71cdbe9 | 749 | |
Sergunb | 0:8918a71cdbe9 | 750 | |
Sergunb | 0:8918a71cdbe9 | 751 | /** |
Sergunb | 0:8918a71cdbe9 | 752 | * @brief Remove existing user |
Sergunb | 0:8918a71cdbe9 | 753 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 754 | * @param[in] username NULL-terminated string that contains the user name |
Sergunb | 0:8918a71cdbe9 | 755 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 756 | **/ |
Sergunb | 0:8918a71cdbe9 | 757 | |
Sergunb | 0:8918a71cdbe9 | 758 | error_t snmpAgentDeleteUser(SnmpAgentContext *context, const char_t *username) |
Sergunb | 0:8918a71cdbe9 | 759 | { |
Sergunb | 0:8918a71cdbe9 | 760 | error_t error; |
Sergunb | 0:8918a71cdbe9 | 761 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 762 | SnmpUserInfo *entry; |
Sergunb | 0:8918a71cdbe9 | 763 | |
Sergunb | 0:8918a71cdbe9 | 764 | //Acquire exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 765 | osAcquireMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 766 | |
Sergunb | 0:8918a71cdbe9 | 767 | //Loop through the list of users |
Sergunb | 0:8918a71cdbe9 | 768 | for(i = 0; i < SNMP_AGENT_MAX_USER_COUNT; i++) |
Sergunb | 0:8918a71cdbe9 | 769 | { |
Sergunb | 0:8918a71cdbe9 | 770 | //Point to the current entry |
Sergunb | 0:8918a71cdbe9 | 771 | entry = &context->userTable[i]; |
Sergunb | 0:8918a71cdbe9 | 772 | |
Sergunb | 0:8918a71cdbe9 | 773 | //Compare user names |
Sergunb | 0:8918a71cdbe9 | 774 | if(!strcmp(entry->name, username)) |
Sergunb | 0:8918a71cdbe9 | 775 | break; |
Sergunb | 0:8918a71cdbe9 | 776 | } |
Sergunb | 0:8918a71cdbe9 | 777 | |
Sergunb | 0:8918a71cdbe9 | 778 | //User name found? |
Sergunb | 0:8918a71cdbe9 | 779 | if(i < SNMP_AGENT_MAX_USER_COUNT) |
Sergunb | 0:8918a71cdbe9 | 780 | { |
Sergunb | 0:8918a71cdbe9 | 781 | //Clear the security profile of the user |
Sergunb | 0:8918a71cdbe9 | 782 | memset(entry, 0, sizeof(SnmpUserInfo)); |
Sergunb | 0:8918a71cdbe9 | 783 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 784 | error = NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 785 | } |
Sergunb | 0:8918a71cdbe9 | 786 | else |
Sergunb | 0:8918a71cdbe9 | 787 | { |
Sergunb | 0:8918a71cdbe9 | 788 | //The specified user name does not exist |
Sergunb | 0:8918a71cdbe9 | 789 | error = ERROR_NOT_FOUND; |
Sergunb | 0:8918a71cdbe9 | 790 | } |
Sergunb | 0:8918a71cdbe9 | 791 | |
Sergunb | 0:8918a71cdbe9 | 792 | //Release exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 793 | osReleaseMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 794 | |
Sergunb | 0:8918a71cdbe9 | 795 | //Return status code |
Sergunb | 0:8918a71cdbe9 | 796 | return error; |
Sergunb | 0:8918a71cdbe9 | 797 | } |
Sergunb | 0:8918a71cdbe9 | 798 | |
Sergunb | 0:8918a71cdbe9 | 799 | |
Sergunb | 0:8918a71cdbe9 | 800 | /** |
Sergunb | 0:8918a71cdbe9 | 801 | * @brief Send SNMP trap message |
Sergunb | 0:8918a71cdbe9 | 802 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 803 | * @param[in] destIpAddr Destination IP address |
Sergunb | 0:8918a71cdbe9 | 804 | * @param[in] version SNMP version identifier |
Sergunb | 0:8918a71cdbe9 | 805 | * @param[in] username User name or community name |
Sergunb | 0:8918a71cdbe9 | 806 | * @param[in] genericTrapType Generic trap type |
Sergunb | 0:8918a71cdbe9 | 807 | * @param[in] specificTrapCode Specific code |
Sergunb | 0:8918a71cdbe9 | 808 | * @param[in] objectList List of object names |
Sergunb | 0:8918a71cdbe9 | 809 | * @param[in] objectListSize Number of entries in the list |
Sergunb | 0:8918a71cdbe9 | 810 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 811 | **/ |
Sergunb | 0:8918a71cdbe9 | 812 | |
Sergunb | 0:8918a71cdbe9 | 813 | error_t snmpAgentSendTrap(SnmpAgentContext *context, const IpAddr *destIpAddr, |
Sergunb | 0:8918a71cdbe9 | 814 | SnmpVersion version, const char_t *username, uint_t genericTrapType, |
Sergunb | 0:8918a71cdbe9 | 815 | uint_t specificTrapCode, const SnmpTrapObject *objectList, uint_t objectListSize) |
Sergunb | 0:8918a71cdbe9 | 816 | { |
Sergunb | 0:8918a71cdbe9 | 817 | error_t error; |
Sergunb | 0:8918a71cdbe9 | 818 | |
Sergunb | 0:8918a71cdbe9 | 819 | //Check parameters |
Sergunb | 0:8918a71cdbe9 | 820 | if(context == NULL || destIpAddr == NULL || username == NULL) |
Sergunb | 0:8918a71cdbe9 | 821 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 822 | |
Sergunb | 0:8918a71cdbe9 | 823 | //Make sure the list of objects is valid |
Sergunb | 0:8918a71cdbe9 | 824 | if(objectListSize > 0 && objectList == NULL) |
Sergunb | 0:8918a71cdbe9 | 825 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 826 | |
Sergunb | 0:8918a71cdbe9 | 827 | //Acquire exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 828 | osAcquireMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 829 | |
Sergunb | 0:8918a71cdbe9 | 830 | //Refresh SNMP engine time |
Sergunb | 0:8918a71cdbe9 | 831 | snmpRefreshEngineTime(context); |
Sergunb | 0:8918a71cdbe9 | 832 | |
Sergunb | 0:8918a71cdbe9 | 833 | //Start of exception handling block |
Sergunb | 0:8918a71cdbe9 | 834 | do |
Sergunb | 0:8918a71cdbe9 | 835 | { |
Sergunb | 0:8918a71cdbe9 | 836 | #if (SNMP_V1_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 837 | //SNMPv1 version? |
Sergunb | 0:8918a71cdbe9 | 838 | if(version == SNMP_VERSION_1) |
Sergunb | 0:8918a71cdbe9 | 839 | { |
Sergunb | 0:8918a71cdbe9 | 840 | //Format Trap-PDU |
Sergunb | 0:8918a71cdbe9 | 841 | error = snmpFormatTrapPdu(context, version, username, |
Sergunb | 0:8918a71cdbe9 | 842 | genericTrapType, specificTrapCode, objectList, objectListSize); |
Sergunb | 0:8918a71cdbe9 | 843 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 844 | if(error) |
Sergunb | 0:8918a71cdbe9 | 845 | break; |
Sergunb | 0:8918a71cdbe9 | 846 | |
Sergunb | 0:8918a71cdbe9 | 847 | //Format SMNP message header |
Sergunb | 0:8918a71cdbe9 | 848 | error = snmpWriteMessageHeader(&context->response); |
Sergunb | 0:8918a71cdbe9 | 849 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 850 | if(error) |
Sergunb | 0:8918a71cdbe9 | 851 | break; |
Sergunb | 0:8918a71cdbe9 | 852 | } |
Sergunb | 0:8918a71cdbe9 | 853 | else |
Sergunb | 0:8918a71cdbe9 | 854 | #endif |
Sergunb | 0:8918a71cdbe9 | 855 | #if (SNMP_V2C_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 856 | //SNMPv2c version? |
Sergunb | 0:8918a71cdbe9 | 857 | if(version == SNMP_VERSION_2C) |
Sergunb | 0:8918a71cdbe9 | 858 | { |
Sergunb | 0:8918a71cdbe9 | 859 | //Format SNMPv2-Trap-PDU |
Sergunb | 0:8918a71cdbe9 | 860 | error = snmpFormatTrapPdu(context, version, username, |
Sergunb | 0:8918a71cdbe9 | 861 | genericTrapType, specificTrapCode, objectList, objectListSize); |
Sergunb | 0:8918a71cdbe9 | 862 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 863 | if(error) |
Sergunb | 0:8918a71cdbe9 | 864 | break; |
Sergunb | 0:8918a71cdbe9 | 865 | |
Sergunb | 0:8918a71cdbe9 | 866 | //Format SMNP message header |
Sergunb | 0:8918a71cdbe9 | 867 | error = snmpWriteMessageHeader(&context->response); |
Sergunb | 0:8918a71cdbe9 | 868 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 869 | if(error) |
Sergunb | 0:8918a71cdbe9 | 870 | break; |
Sergunb | 0:8918a71cdbe9 | 871 | } |
Sergunb | 0:8918a71cdbe9 | 872 | else |
Sergunb | 0:8918a71cdbe9 | 873 | #endif |
Sergunb | 0:8918a71cdbe9 | 874 | #if (SNMP_V3_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 875 | //SNMPv3 version? |
Sergunb | 0:8918a71cdbe9 | 876 | if(version == SNMP_VERSION_3) |
Sergunb | 0:8918a71cdbe9 | 877 | { |
Sergunb | 0:8918a71cdbe9 | 878 | //Information about the user name is extracted from the local |
Sergunb | 0:8918a71cdbe9 | 879 | //configuration datastore |
Sergunb | 0:8918a71cdbe9 | 880 | context->user = snmpFindUser(context, username, strlen(username)); |
Sergunb | 0:8918a71cdbe9 | 881 | |
Sergunb | 0:8918a71cdbe9 | 882 | //Invalid user name? |
Sergunb | 0:8918a71cdbe9 | 883 | if(context->user == NULL) |
Sergunb | 0:8918a71cdbe9 | 884 | { |
Sergunb | 0:8918a71cdbe9 | 885 | //Report an error |
Sergunb | 0:8918a71cdbe9 | 886 | error = ERROR_UNKNOWN_USER_NAME; |
Sergunb | 0:8918a71cdbe9 | 887 | //Exit immediately |
Sergunb | 0:8918a71cdbe9 | 888 | break; |
Sergunb | 0:8918a71cdbe9 | 889 | } |
Sergunb | 0:8918a71cdbe9 | 890 | |
Sergunb | 0:8918a71cdbe9 | 891 | //Format SNMPv2-Trap-PDU |
Sergunb | 0:8918a71cdbe9 | 892 | error = snmpFormatTrapPdu(context, version, username, |
Sergunb | 0:8918a71cdbe9 | 893 | genericTrapType, specificTrapCode, objectList, objectListSize); |
Sergunb | 0:8918a71cdbe9 | 894 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 895 | if(error) |
Sergunb | 0:8918a71cdbe9 | 896 | break; |
Sergunb | 0:8918a71cdbe9 | 897 | |
Sergunb | 0:8918a71cdbe9 | 898 | //Format scopedPDU |
Sergunb | 0:8918a71cdbe9 | 899 | error = snmpWriteScopedPdu(&context->response); |
Sergunb | 0:8918a71cdbe9 | 900 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 901 | if(error) |
Sergunb | 0:8918a71cdbe9 | 902 | break; |
Sergunb | 0:8918a71cdbe9 | 903 | |
Sergunb | 0:8918a71cdbe9 | 904 | //Check whether the privFlag is set |
Sergunb | 0:8918a71cdbe9 | 905 | if(context->response.msgFlags & SNMP_MSG_FLAG_PRIV) |
Sergunb | 0:8918a71cdbe9 | 906 | { |
Sergunb | 0:8918a71cdbe9 | 907 | //Encrypt data |
Sergunb | 0:8918a71cdbe9 | 908 | error = snmpEncryptData(context->user, &context->response, &context->salt); |
Sergunb | 0:8918a71cdbe9 | 909 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 910 | if(error) |
Sergunb | 0:8918a71cdbe9 | 911 | break; |
Sergunb | 0:8918a71cdbe9 | 912 | } |
Sergunb | 0:8918a71cdbe9 | 913 | |
Sergunb | 0:8918a71cdbe9 | 914 | //Format SMNP message header |
Sergunb | 0:8918a71cdbe9 | 915 | error = snmpWriteMessageHeader(&context->response); |
Sergunb | 0:8918a71cdbe9 | 916 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 917 | if(error) |
Sergunb | 0:8918a71cdbe9 | 918 | break; |
Sergunb | 0:8918a71cdbe9 | 919 | |
Sergunb | 0:8918a71cdbe9 | 920 | //Check whether the authFlag is set |
Sergunb | 0:8918a71cdbe9 | 921 | if(context->response.msgFlags & SNMP_MSG_FLAG_AUTH) |
Sergunb | 0:8918a71cdbe9 | 922 | { |
Sergunb | 0:8918a71cdbe9 | 923 | //Authenticate outgoing SNMP message |
Sergunb | 0:8918a71cdbe9 | 924 | error = snmpAuthOutgoingMessage(context->user, &context->response); |
Sergunb | 0:8918a71cdbe9 | 925 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 926 | if(error) |
Sergunb | 0:8918a71cdbe9 | 927 | break; |
Sergunb | 0:8918a71cdbe9 | 928 | } |
Sergunb | 0:8918a71cdbe9 | 929 | } |
Sergunb | 0:8918a71cdbe9 | 930 | else |
Sergunb | 0:8918a71cdbe9 | 931 | #endif |
Sergunb | 0:8918a71cdbe9 | 932 | //Invalid SNMP version? |
Sergunb | 0:8918a71cdbe9 | 933 | { |
Sergunb | 0:8918a71cdbe9 | 934 | //Debug message |
Sergunb | 0:8918a71cdbe9 | 935 | TRACE_WARNING(" Invalid SNMP version!\r\n"); |
Sergunb | 0:8918a71cdbe9 | 936 | //Report an error |
Sergunb | 0:8918a71cdbe9 | 937 | error = ERROR_INVALID_VERSION; |
Sergunb | 0:8918a71cdbe9 | 938 | //Exit immediately |
Sergunb | 0:8918a71cdbe9 | 939 | break; |
Sergunb | 0:8918a71cdbe9 | 940 | } |
Sergunb | 0:8918a71cdbe9 | 941 | |
Sergunb | 0:8918a71cdbe9 | 942 | //Total number of messages which were passed from the SNMP protocol |
Sergunb | 0:8918a71cdbe9 | 943 | //entity to the transport service |
Sergunb | 0:8918a71cdbe9 | 944 | MIB2_INC_COUNTER32(mib2Base.snmpGroup.snmpOutPkts, 1); |
Sergunb | 0:8918a71cdbe9 | 945 | |
Sergunb | 0:8918a71cdbe9 | 946 | //Debug message |
Sergunb | 0:8918a71cdbe9 | 947 | TRACE_INFO("Sending SNMP message to %s port %" PRIu16 |
Sergunb | 0:8918a71cdbe9 | 948 | " (%" PRIuSIZE " bytes)...\r\n", |
Sergunb | 0:8918a71cdbe9 | 949 | ipAddrToString(destIpAddr, NULL), |
Sergunb | 0:8918a71cdbe9 | 950 | context->settings.trapPort, context->response.length); |
Sergunb | 0:8918a71cdbe9 | 951 | |
Sergunb | 0:8918a71cdbe9 | 952 | //Display the contents of the SNMP message |
Sergunb | 0:8918a71cdbe9 | 953 | TRACE_DEBUG_ARRAY(" ", context->response.pos, context->response.length); |
Sergunb | 0:8918a71cdbe9 | 954 | //Display ASN.1 structure |
Sergunb | 0:8918a71cdbe9 | 955 | asn1DumpObject(context->response.pos, context->response.length, 0); |
Sergunb | 0:8918a71cdbe9 | 956 | |
Sergunb | 0:8918a71cdbe9 | 957 | //Send SNMP trap message |
Sergunb | 0:8918a71cdbe9 | 958 | error = socketSendTo(context->socket, destIpAddr, context->settings.trapPort, |
Sergunb | 0:8918a71cdbe9 | 959 | context->response.pos, context->response.length, NULL, 0); |
Sergunb | 0:8918a71cdbe9 | 960 | |
Sergunb | 0:8918a71cdbe9 | 961 | //End of exception handling block |
Sergunb | 0:8918a71cdbe9 | 962 | } while(0); |
Sergunb | 0:8918a71cdbe9 | 963 | |
Sergunb | 0:8918a71cdbe9 | 964 | //Release exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 965 | osReleaseMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 966 | |
Sergunb | 0:8918a71cdbe9 | 967 | //Return status code |
Sergunb | 0:8918a71cdbe9 | 968 | return error; |
Sergunb | 0:8918a71cdbe9 | 969 | } |
Sergunb | 0:8918a71cdbe9 | 970 | |
Sergunb | 0:8918a71cdbe9 | 971 | |
Sergunb | 0:8918a71cdbe9 | 972 | /** |
Sergunb | 0:8918a71cdbe9 | 973 | * @brief SNMP agent task |
Sergunb | 0:8918a71cdbe9 | 974 | * @param[in] context Pointer to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 975 | **/ |
Sergunb | 0:8918a71cdbe9 | 976 | |
Sergunb | 0:8918a71cdbe9 | 977 | void snmpAgentTask(SnmpAgentContext *context) |
Sergunb | 0:8918a71cdbe9 | 978 | { |
Sergunb | 0:8918a71cdbe9 | 979 | error_t error; |
Sergunb | 0:8918a71cdbe9 | 980 | |
Sergunb | 0:8918a71cdbe9 | 981 | #if (NET_RTOS_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 982 | //Main loop |
Sergunb | 0:8918a71cdbe9 | 983 | while(1) |
Sergunb | 0:8918a71cdbe9 | 984 | { |
Sergunb | 0:8918a71cdbe9 | 985 | #endif |
Sergunb | 0:8918a71cdbe9 | 986 | //Wait for an incoming datagram |
Sergunb | 0:8918a71cdbe9 | 987 | error = socketReceiveFrom(context->socket, &context->remoteIpAddr, |
Sergunb | 0:8918a71cdbe9 | 988 | &context->remotePort, context->request.buffer, |
Sergunb | 0:8918a71cdbe9 | 989 | SNMP_MAX_MSG_SIZE, &context->request.bufferLen, 0); |
Sergunb | 0:8918a71cdbe9 | 990 | |
Sergunb | 0:8918a71cdbe9 | 991 | //Any datagram received? |
Sergunb | 0:8918a71cdbe9 | 992 | if(!error) |
Sergunb | 0:8918a71cdbe9 | 993 | { |
Sergunb | 0:8918a71cdbe9 | 994 | //Acquire exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 995 | osAcquireMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 996 | |
Sergunb | 0:8918a71cdbe9 | 997 | //Debug message |
Sergunb | 0:8918a71cdbe9 | 998 | TRACE_INFO("\r\nSNMP message received from %s port %" PRIu16 |
Sergunb | 0:8918a71cdbe9 | 999 | " (%" PRIuSIZE " bytes)...\r\n", |
Sergunb | 0:8918a71cdbe9 | 1000 | ipAddrToString(&context->remoteIpAddr, NULL), |
Sergunb | 0:8918a71cdbe9 | 1001 | context->remotePort, context->request.bufferLen); |
Sergunb | 0:8918a71cdbe9 | 1002 | |
Sergunb | 0:8918a71cdbe9 | 1003 | //Display the contents of the SNMP message |
Sergunb | 0:8918a71cdbe9 | 1004 | TRACE_DEBUG_ARRAY(" ", context->request.buffer, context->request.bufferLen); |
Sergunb | 0:8918a71cdbe9 | 1005 | //Dump ASN.1 structure |
Sergunb | 0:8918a71cdbe9 | 1006 | asn1DumpObject(context->request.buffer, context->request.bufferLen, 0); |
Sergunb | 0:8918a71cdbe9 | 1007 | |
Sergunb | 0:8918a71cdbe9 | 1008 | //Process incoming SNMP message |
Sergunb | 0:8918a71cdbe9 | 1009 | error = snmpProcessMessage(context); |
Sergunb | 0:8918a71cdbe9 | 1010 | |
Sergunb | 0:8918a71cdbe9 | 1011 | //Check status code |
Sergunb | 0:8918a71cdbe9 | 1012 | if(!error) |
Sergunb | 0:8918a71cdbe9 | 1013 | { |
Sergunb | 0:8918a71cdbe9 | 1014 | //Debug message |
Sergunb | 0:8918a71cdbe9 | 1015 | TRACE_INFO("Sending SNMP message to %s port %" PRIu16 |
Sergunb | 0:8918a71cdbe9 | 1016 | " (%" PRIuSIZE " bytes)...\r\n", |
Sergunb | 0:8918a71cdbe9 | 1017 | ipAddrToString(&context->remoteIpAddr, NULL), |
Sergunb | 0:8918a71cdbe9 | 1018 | context->remotePort, context->response.length); |
Sergunb | 0:8918a71cdbe9 | 1019 | |
Sergunb | 0:8918a71cdbe9 | 1020 | //Display the contents of the SNMP message |
Sergunb | 0:8918a71cdbe9 | 1021 | TRACE_DEBUG_ARRAY(" ", context->response.pos, context->response.length); |
Sergunb | 0:8918a71cdbe9 | 1022 | //Display ASN.1 structure |
Sergunb | 0:8918a71cdbe9 | 1023 | asn1DumpObject(context->response.pos, context->response.length, 0); |
Sergunb | 0:8918a71cdbe9 | 1024 | |
Sergunb | 0:8918a71cdbe9 | 1025 | //Send SNMP response message |
Sergunb | 0:8918a71cdbe9 | 1026 | socketSendTo(context->socket, &context->remoteIpAddr, context->remotePort, |
Sergunb | 0:8918a71cdbe9 | 1027 | context->response.pos, context->response.length, NULL, 0); |
Sergunb | 0:8918a71cdbe9 | 1028 | } |
Sergunb | 0:8918a71cdbe9 | 1029 | |
Sergunb | 0:8918a71cdbe9 | 1030 | //Release exclusive access to the SNMP agent context |
Sergunb | 0:8918a71cdbe9 | 1031 | osReleaseMutex(&context->mutex); |
Sergunb | 0:8918a71cdbe9 | 1032 | } |
Sergunb | 0:8918a71cdbe9 | 1033 | #if (NET_RTOS_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 1034 | } |
Sergunb | 0:8918a71cdbe9 | 1035 | #endif |
Sergunb | 0:8918a71cdbe9 | 1036 | } |
Sergunb | 0:8918a71cdbe9 | 1037 | |
Sergunb | 0:8918a71cdbe9 | 1038 | #endif |
Sergunb | 0:8918a71cdbe9 | 1039 |