Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed
snmp_msg.h
00001 /** 00002 * @file 00003 * SNMP Agent message handling structures. 00004 */ 00005 00006 /* 00007 * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands. 00008 * All rights reserved. 00009 * 00010 * Redistribution and use in source and binary forms, with or without modification, 00011 * are permitted provided that the following conditions are met: 00012 * 00013 * 1. Redistributions of source code must retain the above copyright notice, 00014 * this list of conditions and the following disclaimer. 00015 * 2. Redistributions in binary form must reproduce the above copyright notice, 00016 * this list of conditions and the following disclaimer in the documentation 00017 * and/or other materials provided with the distribution. 00018 * 3. The name of the author may not be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00022 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00024 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00026 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00029 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00030 * OF SUCH DAMAGE. 00031 * 00032 * Author: Christiaan Simons <christiaan.simons@axon.tv> 00033 */ 00034 00035 #ifndef __LWIP_SNMP_MSG_H__ 00036 #define __LWIP_SNMP_MSG_H__ 00037 00038 #include "lwip/opt.h" 00039 #include "lwip/snmp.h" 00040 #include "lwip/snmp_structs.h" 00041 00042 #if LWIP_SNMP 00043 00044 #if SNMP_PRIVATE_MIB 00045 #include "private_mib.h" 00046 #endif 00047 00048 #ifdef __cplusplus 00049 extern "C" { 00050 #endif 00051 00052 /* The listen port of the SNMP agent. Clients have to make their requests to 00053 this port. Most standard clients won't work if you change this! */ 00054 #ifndef SNMP_IN_PORT 00055 #define SNMP_IN_PORT 161 00056 #endif 00057 /* The remote port the SNMP agent sends traps to. Most standard trap sinks won't 00058 work if you change this! */ 00059 #ifndef SNMP_TRAP_PORT 00060 #define SNMP_TRAP_PORT 162 00061 #endif 00062 00063 #define SNMP_ES_NOERROR 0 00064 #define SNMP_ES_TOOBIG 1 00065 #define SNMP_ES_NOSUCHNAME 2 00066 #define SNMP_ES_BADVALUE 3 00067 #define SNMP_ES_READONLY 4 00068 #define SNMP_ES_GENERROR 5 00069 00070 #define SNMP_GENTRAP_COLDSTART 0 00071 #define SNMP_GENTRAP_WARMSTART 1 00072 #define SNMP_GENTRAP_AUTHFAIL 4 00073 #define SNMP_GENTRAP_ENTERPRISESPC 6 00074 00075 struct snmp_varbind 00076 { 00077 /* next pointer, NULL for last in list */ 00078 struct snmp_varbind *next; 00079 /* previous pointer, NULL for first in list */ 00080 struct snmp_varbind *prev; 00081 00082 /* object identifier length (in s32_t) */ 00083 u8_t ident_len; 00084 /* object identifier array */ 00085 s32_t *ident; 00086 00087 /* object value ASN1 type */ 00088 u8_t value_type; 00089 /* object value length (in u8_t) */ 00090 u8_t value_len; 00091 /* object value */ 00092 void *value; 00093 00094 /* encoding varbind seq length length */ 00095 u8_t seqlenlen; 00096 /* encoding object identifier length length */ 00097 u8_t olenlen; 00098 /* encoding object value length length */ 00099 u8_t vlenlen; 00100 /* encoding varbind seq length */ 00101 u16_t seqlen; 00102 /* encoding object identifier length */ 00103 u16_t olen; 00104 /* encoding object value length */ 00105 u16_t vlen; 00106 }; 00107 00108 struct snmp_varbind_root 00109 { 00110 struct snmp_varbind *head; 00111 struct snmp_varbind *tail; 00112 /* number of variable bindings in list */ 00113 u8_t count; 00114 /* encoding varbind-list seq length length */ 00115 u8_t seqlenlen; 00116 /* encoding varbind-list seq length */ 00117 u16_t seqlen; 00118 }; 00119 00120 /** output response message header length fields */ 00121 struct snmp_resp_header_lengths 00122 { 00123 /* encoding error-index length length */ 00124 u8_t erridxlenlen; 00125 /* encoding error-status length length */ 00126 u8_t errstatlenlen; 00127 /* encoding request id length length */ 00128 u8_t ridlenlen; 00129 /* encoding pdu length length */ 00130 u8_t pdulenlen; 00131 /* encoding community length length */ 00132 u8_t comlenlen; 00133 /* encoding version length length */ 00134 u8_t verlenlen; 00135 /* encoding sequence length length */ 00136 u8_t seqlenlen; 00137 00138 /* encoding error-index length */ 00139 u16_t erridxlen; 00140 /* encoding error-status length */ 00141 u16_t errstatlen; 00142 /* encoding request id length */ 00143 u16_t ridlen; 00144 /* encoding pdu length */ 00145 u16_t pdulen; 00146 /* encoding community length */ 00147 u16_t comlen; 00148 /* encoding version length */ 00149 u16_t verlen; 00150 /* encoding sequence length */ 00151 u16_t seqlen; 00152 }; 00153 00154 /** output response message header length fields */ 00155 struct snmp_trap_header_lengths 00156 { 00157 /* encoding timestamp length length */ 00158 u8_t tslenlen; 00159 /* encoding specific-trap length length */ 00160 u8_t strplenlen; 00161 /* encoding generic-trap length length */ 00162 u8_t gtrplenlen; 00163 /* encoding agent-addr length length */ 00164 u8_t aaddrlenlen; 00165 /* encoding enterprise-id length length */ 00166 u8_t eidlenlen; 00167 /* encoding pdu length length */ 00168 u8_t pdulenlen; 00169 /* encoding community length length */ 00170 u8_t comlenlen; 00171 /* encoding version length length */ 00172 u8_t verlenlen; 00173 /* encoding sequence length length */ 00174 u8_t seqlenlen; 00175 00176 /* encoding timestamp length */ 00177 u16_t tslen; 00178 /* encoding specific-trap length */ 00179 u16_t strplen; 00180 /* encoding generic-trap length */ 00181 u16_t gtrplen; 00182 /* encoding agent-addr length */ 00183 u16_t aaddrlen; 00184 /* encoding enterprise-id length */ 00185 u16_t eidlen; 00186 /* encoding pdu length */ 00187 u16_t pdulen; 00188 /* encoding community length */ 00189 u16_t comlen; 00190 /* encoding version length */ 00191 u16_t verlen; 00192 /* encoding sequence length */ 00193 u16_t seqlen; 00194 }; 00195 00196 /* Accepting new SNMP messages. */ 00197 #define SNMP_MSG_EMPTY 0 00198 /* Search for matching object for variable binding. */ 00199 #define SNMP_MSG_SEARCH_OBJ 1 00200 /* Perform SNMP operation on in-memory object. 00201 Pass-through states, for symmetry only. */ 00202 #define SNMP_MSG_INTERNAL_GET_OBJDEF 2 00203 #define SNMP_MSG_INTERNAL_GET_VALUE 3 00204 #define SNMP_MSG_INTERNAL_SET_TEST 4 00205 #define SNMP_MSG_INTERNAL_GET_OBJDEF_S 5 00206 #define SNMP_MSG_INTERNAL_SET_VALUE 6 00207 /* Perform SNMP operation on object located externally. 00208 In theory this could be used for building a proxy agent. 00209 Practical use is for an enterprise spc. app. gateway. */ 00210 #define SNMP_MSG_EXTERNAL_GET_OBJDEF 7 00211 #define SNMP_MSG_EXTERNAL_GET_VALUE 8 00212 #define SNMP_MSG_EXTERNAL_SET_TEST 9 00213 #define SNMP_MSG_EXTERNAL_GET_OBJDEF_S 10 00214 #define SNMP_MSG_EXTERNAL_SET_VALUE 11 00215 00216 #define SNMP_COMMUNITY_STR_LEN 64 00217 struct snmp_msg_pstat 00218 { 00219 /* lwIP local port (161) binding */ 00220 struct udp_pcb *pcb; 00221 /* source IP address */ 00222 struct ip_addr sip; 00223 /* source UDP port */ 00224 u16_t sp; 00225 /* request type */ 00226 u8_t rt; 00227 /* request ID */ 00228 s32_t rid; 00229 /* error status */ 00230 s32_t error_status; 00231 /* error index */ 00232 s32_t error_index; 00233 /* community name (zero terminated) */ 00234 u8_t community[SNMP_COMMUNITY_STR_LEN + 1]; 00235 /* community string length (exclusive zero term) */ 00236 u8_t com_strlen; 00237 /* one out of MSG_EMPTY, MSG_DEMUX, MSG_INTERNAL, MSG_EXTERNAL_x */ 00238 u8_t state; 00239 /* saved arguments for MSG_EXTERNAL_x */ 00240 struct mib_external_node *ext_mib_node; 00241 struct snmp_name_ptr ext_name_ptr; 00242 struct obj_def ext_object_def; 00243 struct snmp_obj_id ext_oid; 00244 /* index into input variable binding list */ 00245 u8_t vb_idx; 00246 /* ptr into input variable binding list */ 00247 struct snmp_varbind *vb_ptr; 00248 /* list of variable bindings from input */ 00249 struct snmp_varbind_root invb; 00250 /* list of variable bindings to output */ 00251 struct snmp_varbind_root outvb; 00252 /* output response lengths used in ASN encoding */ 00253 struct snmp_resp_header_lengths rhl; 00254 }; 00255 00256 struct snmp_msg_trap 00257 { 00258 /* lwIP local port (161) binding */ 00259 struct udp_pcb *pcb; 00260 /* destination IP address in network order */ 00261 struct ip_addr dip; 00262 00263 /* source enterprise ID (sysObjectID) */ 00264 struct snmp_obj_id *enterprise; 00265 /* source IP address, raw network order format */ 00266 u8_t sip_raw[4]; 00267 /* generic trap code */ 00268 u32_t gen_trap; 00269 /* specific trap code */ 00270 u32_t spc_trap; 00271 /* timestamp */ 00272 u32_t ts; 00273 /* list of variable bindings to output */ 00274 struct snmp_varbind_root outvb; 00275 /* output trap lengths used in ASN encoding */ 00276 struct snmp_trap_header_lengths thl; 00277 }; 00278 00279 /** Agent Version constant, 0 = v1 oddity */ 00280 extern const s32_t snmp_version; 00281 /** Agent default "public" community string */ 00282 extern const char snmp_publiccommunity[7]; 00283 00284 extern struct snmp_msg_trap trap_msg; 00285 00286 /** Agent setup, start listening to port 161. */ 00287 void snmp_init(void); 00288 void snmp_trap_dst_enable(u8_t dst_idx, u8_t enable); 00289 void snmp_trap_dst_ip_set(u8_t dst_idx, struct ip_addr *dst); 00290 00291 /** Varbind-list functions. */ 00292 struct snmp_varbind* snmp_varbind_alloc(struct snmp_obj_id *oid, u8_t type, u8_t len); 00293 void snmp_varbind_free(struct snmp_varbind *vb); 00294 void snmp_varbind_list_free(struct snmp_varbind_root *root); 00295 void snmp_varbind_tail_add(struct snmp_varbind_root *root, struct snmp_varbind *vb); 00296 struct snmp_varbind* snmp_varbind_tail_remove(struct snmp_varbind_root *root); 00297 00298 /** Handle an internal (recv) or external (private response) event. */ 00299 void snmp_msg_event(u8_t request_id); 00300 err_t snmp_send_response(struct snmp_msg_pstat *m_stat); 00301 err_t snmp_send_trap(s8_t generic_trap, struct snmp_obj_id *eoid, s32_t specific_trap); 00302 void snmp_coldstart_trap(void); 00303 void snmp_authfail_trap(void); 00304 00305 #ifdef __cplusplus 00306 } 00307 #endif 00308 00309 #endif /* LWIP_SNMP */ 00310 00311 #endif /* __LWIP_SNMP_MSG_H__ */
Generated on Tue Jul 12 2022 19:17:24 by 1.7.2