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 46 /** SNMP netconn API worker thread */ 47 static void 48 snmp_netconn_thread(void *arg) 49 { 50 struct netconn *conn; 51 struct netbuf *buf; 52 err_t err; 53 LWIP_UNUSED_ARG(arg); 54 55 /* Bind to SNMP port with default IP address */ 56 #if LWIP_IPV6 57 conn = netconn_new(NETCONN_UDP_IPV6); 58 netconn_bind(conn, IP6_ADDR_ANY, SNMP_IN_PORT); 59 #else /* LWIP_IPV6 */ 60 conn = netconn_new(NETCONN_UDP); 61 netconn_bind(conn, IP4_ADDR_ANY, SNMP_IN_PORT); 62 #endif /* LWIP_IPV6 */ 63 LWIP_ERROR("snmp_netconn: invalid conn", (conn != NULL), return;); 64 65 snmp_traps_handle = conn; 66 67 do { 68 err = netconn_recv(conn, &buf); 69 70 if (err == ERR_OK) { 71 snmp_receive(conn, buf->p, &buf->addr, buf->port); 72 } 73 74 if (buf != NULL) { 75 netbuf_delete(buf); 76 } 77 } while(1); 78 } 79 80 err_t 81 snmp_sendto(void *handle, struct pbuf *p, const ip_addr_t *dst, u16_t port) 82 { 83 err_t result; 84 struct netbuf buf; 85 86 memset(&buf, 0, sizeof(buf)); 87 buf.p = p; 88 result = netconn_sendto((struct netconn*)handle, &buf, dst, port); 89 90 return result; 91 } 92 93 u8_t 94 snmp_get_local_ip_for_dst(void* handle, const ip_addr_t *dst, ip_addr_t *result) 95 { 96 struct netconn* conn = (struct netconn*)handle; 97 struct netif *dst_if; 98 const ip_addr_t* dst_ip; 99 100 LWIP_UNUSED_ARG(conn); /* unused in case of IPV4 only configuration */ 101 102 ip_route_get_local_ip(&conn->pcb.udp->local_ip, dst, dst_if, dst_ip); 103 104 if ((dst_if != NULL) && (dst_ip != NULL)) { 105 ip_addr_copy(*result, *dst_ip); 106 return 1; 107 } else { 108 return 0; 109 } 110 } 111 112 /** 113 * Starts SNMP Agent. 114 */ 115 void 116 snmp_init(void) 117 { 118 sys_thread_new("snmp_netconn", snmp_netconn_thread, NULL, SNMP_STACK_SIZE, SNMP_THREAD_PRIO); 119 } 120 121 #endif /* LWIP_SNMP && SNMP_USE_NETCONN */ 122