1 /** 2 * @file 3 * SNMP RAW API 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 <[email protected]> 33 */ 34 35 #include "lwip/apps/snmp_opts.h" 36 #include "lwip/ip_addr.h" 37 38 #if LWIP_SNMP && SNMP_USE_RAW 39 40 #include "lwip/udp.h" 41 #include "lwip/ip.h" 42 #include "lwip/prot/iana.h" 43 #include "snmp_msg.h" 44 45 /* lwIP UDP receive callback function */ 46 static void 47 snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) 48 { 49 LWIP_UNUSED_ARG(arg); 50 51 snmp_receive(pcb, p, addr, port); 52 53 pbuf_free(p); 54 } 55 56 err_t 57 snmp_sendto(void *handle, struct pbuf *p, const ip_addr_t *dst, u16_t port) 58 { 59 return udp_sendto((struct udp_pcb *)handle, p, dst, port); 60 } 61 62 u8_t 63 snmp_get_local_ip_for_dst(void *handle, const ip_addr_t *dst, ip_addr_t *result) 64 { 65 struct udp_pcb *udp_pcb = (struct udp_pcb *)handle; 66 struct netif *dst_if; 67 const ip_addr_t *dst_ip; 68 69 LWIP_UNUSED_ARG(udp_pcb); /* unused in case of IPV4 only configuration */ 70 71 ip_route_get_local_ip(&udp_pcb->local_ip, dst, dst_if, dst_ip); 72 73 if ((dst_if != NULL) && (dst_ip != NULL)) { 74 ip_addr_copy(*result, *dst_ip); 75 return 1; 76 } else { 77 return 0; 78 } 79 } 80 81 /** 82 * @ingroup snmp_core 83 * Starts SNMP Agent. 84 * Allocates UDP pcb and binds it to IP_ANY_TYPE port 161. 85 */ 86 void 87 snmp_init(void) 88 { 89 err_t err; 90 91 struct udp_pcb *snmp_pcb = udp_new_ip_type(IPADDR_TYPE_ANY); 92 LWIP_ERROR("snmp_raw: no PCB", (snmp_pcb != NULL), return;); 93 94 LWIP_ASSERT_CORE_LOCKED(); 95 96 snmp_traps_handle = snmp_pcb; 97 98 udp_recv(snmp_pcb, snmp_recv, NULL); 99 err = udp_bind(snmp_pcb, IP_ANY_TYPE, LWIP_IANA_PORT_SNMP); 100 LWIP_ERROR("snmp_raw: Unable to bind PCB", (err == ERR_OK), return;); 101 } 102 103 #endif /* LWIP_SNMP && SNMP_USE_RAW */ 104