1/** 2 * @file 3 * SNMP netconn frontend. 4 */ 5 6/* 7 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without modification, 11 * are permitted provided that the following conditions are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright notice, 14 * this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 * OF SUCH DAMAGE. 31 * 32 * Author: Dirk Ziegelmeier <dziegel@gmx.de> 33 */ 34 35#include "lwip/apps/snmp_opts.h" 36 37#if LWIP_SNMP && SNMP_USE_NETCONN 38 39#include <string.h> 40#include "lwip/api.h" 41#include "lwip/ip.h" 42#include "lwip/udp.h" 43#include "snmp_msg.h" 44#include "lwip/sys.h" 45#include "lwip/prot/iana.h" 46 47/** SNMP netconn API worker thread */ 48static void 49snmp_netconn_thread(void *arg) 50{ 51 struct netconn *conn; 52 struct netbuf *buf; 53 err_t err; 54 LWIP_UNUSED_ARG(arg); 55 56 /* Bind to SNMP port with default IP address */ 57#if LWIP_IPV6 58 conn = netconn_new(NETCONN_UDP_IPV6); 59 netconn_bind(conn, IP6_ADDR_ANY, LWIP_IANA_PORT_SNMP); 60#else /* LWIP_IPV6 */ 61 conn = netconn_new(NETCONN_UDP); 62 netconn_bind(conn, IP4_ADDR_ANY, LWIP_IANA_PORT_SNMP); 63#endif /* LWIP_IPV6 */ 64 LWIP_ERROR("snmp_netconn: invalid conn", (conn != NULL), return;); 65 66 snmp_traps_handle = conn; 67 68 do { 69 err = netconn_recv(conn, &buf); 70 71 if (err == ERR_OK) { 72 snmp_receive(conn, buf->p, &buf->addr, buf->port); 73 } 74 75 if (buf != NULL) { 76 netbuf_delete(buf); 77 } 78 } while (1); 79} 80 81err_t 82snmp_sendto(void *handle, struct pbuf *p, const ip_addr_t *dst, u16_t port) 83{ 84 err_t result; 85 struct netbuf buf; 86 87 memset(&buf, 0, sizeof(buf)); 88 buf.p = p; 89 result = netconn_sendto((struct netconn *)handle, &buf, dst, port); 90 91 return result; 92} 93 94u8_t 95snmp_get_local_ip_for_dst(void *handle, const ip_addr_t *dst, ip_addr_t *result) 96{ 97 struct netconn *conn = (struct netconn *)handle; 98 struct netif *dst_if; 99 const ip_addr_t *dst_ip; 100 101 LWIP_UNUSED_ARG(conn); /* unused in case of IPV4 only configuration */ 102 103 ip_route_get_local_ip(&conn->pcb.udp->local_ip, dst, dst_if, dst_ip); 104 105 if ((dst_if != NULL) && (dst_ip != NULL)) { 106 ip_addr_copy(*result, *dst_ip); 107 return 1; 108 } else { 109 return 0; 110 } 111} 112 113/** 114 * Starts SNMP Agent. 115 */ 116void 117snmp_init(void) 118{ 119 LWIP_ASSERT_CORE_LOCKED(); 120 sys_thread_new("snmp_netconn", snmp_netconn_thread, NULL, SNMP_STACK_SIZE, SNMP_THREAD_PRIO); 121} 122 123#endif /* LWIP_SNMP && SNMP_USE_NETCONN */ 124