Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_snmp_raw.c Source File

lwip_snmp_raw.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * SNMP RAW API frontend.
00004  */
00005 
00006 /*
00007  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
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: Dirk Ziegelmeier <dziegel@gmx.de>
00033  */
00034 
00035 #include "lwip/apps/snmp_opts.h"
00036 #include "lwip/ip_addr.h"
00037 
00038 #if LWIP_SNMP && SNMP_USE_RAW
00039 
00040 #include "lwip/udp.h"
00041 #include "lwip/ip.h"
00042 #include "lwip/prot/iana.h"
00043 #include "snmp_msg.h"
00044 
00045 /* lwIP UDP receive callback function */
00046 static void
00047 snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
00048 {
00049   LWIP_UNUSED_ARG(arg);
00050 
00051   snmp_receive(pcb, p, addr, port);
00052 
00053   pbuf_free(p);
00054 }
00055 
00056 err_t
00057 snmp_sendto(void *handle, struct pbuf *p, const ip_addr_t *dst, u16_t port)
00058 {
00059   return udp_sendto((struct udp_pcb *)handle, p, dst, port);
00060 }
00061 
00062 u8_t
00063 snmp_get_local_ip_for_dst(void *handle, const ip_addr_t *dst, ip_addr_t *result)
00064 {
00065   struct udp_pcb *udp_pcb = (struct udp_pcb *)handle;
00066   struct netif *dst_if;
00067   const ip_addr_t *dst_ip;
00068 
00069   LWIP_UNUSED_ARG(udp_pcb); /* unused in case of IPV4 only configuration */
00070 
00071   ip_route_get_local_ip(&udp_pcb->local_ip, dst, dst_if, dst_ip);
00072 
00073   if ((dst_if != NULL) && (dst_ip != NULL)) {
00074     ip_addr_copy(*result, *dst_ip);
00075     return 1;
00076   } else {
00077     return 0;
00078   }
00079 }
00080 
00081 /**
00082  * @ingroup snmp_core
00083  * Starts SNMP Agent.
00084  * Allocates UDP pcb and binds it to IP_ANY_TYPE port 161.
00085  */
00086 void
00087 snmp_init(void)
00088 {
00089   err_t err;
00090 
00091   struct udp_pcb *snmp_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
00092   LWIP_ERROR("snmp_raw: no PCB", (snmp_pcb != NULL), return;);
00093 
00094   LWIP_ASSERT_CORE_LOCKED();
00095 
00096   snmp_traps_handle = snmp_pcb;
00097 
00098   udp_recv(snmp_pcb, snmp_recv, NULL);
00099   err = udp_bind(snmp_pcb, IP_ANY_TYPE, LWIP_IANA_PORT_SNMP);
00100   LWIP_ERROR("snmp_raw: Unable to bind PCB", (err == ERR_OK), return;);
00101 }
00102 
00103 #endif /* LWIP_SNMP && SNMP_USE_RAW */