Rizky Ardi Maulana / mbed-os
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_snmp_netconn.c Source File

lwip_snmp_netconn.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * SNMP netconn 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 
00037 #if LWIP_SNMP && SNMP_USE_NETCONN
00038 
00039 #include "lwip/api.h"
00040 #include "lwip/ip.h"
00041 #include "lwip/udp.h"
00042 #include "snmp_msg.h"
00043 #include "lwip/sys.h"
00044 
00045 /** SNMP netconn API worker thread */
00046 static void
00047 snmp_netconn_thread(void *arg)
00048 {
00049   struct netconn *conn;
00050   struct netbuf *buf;
00051   err_t err;
00052   LWIP_UNUSED_ARG(arg);
00053   
00054   /* Bind to SNMP port with default IP address */
00055  #if LWIP_IPV6
00056   conn = netconn_new(NETCONN_UDP_IPV6);
00057   netconn_bind(conn, IP6_ADDR_ANY, SNMP_IN_PORT);
00058 #else /* LWIP_IPV6 */
00059   conn = netconn_new(NETCONN_UDP);
00060   netconn_bind(conn, IP_ADDR_ANY, SNMP_IN_PORT);
00061 #endif /* LWIP_IPV6 */
00062   LWIP_ERROR("snmp_netconn: invalid conn", (conn != NULL), return;);
00063   
00064   snmp_traps_handle = conn;
00065 
00066   do {
00067     err = netconn_recv(conn, &buf);
00068 
00069     if (err == ERR_OK) {
00070       snmp_receive(conn, buf->p, &buf->addr, buf->port);
00071     }
00072 
00073     if (buf != NULL) {
00074       netbuf_delete(buf);
00075     }
00076   } while(1);
00077 }
00078 
00079 err_t 
00080 snmp_sendto(void *handle, struct pbuf *p, const ip_addr_t *dst, u16_t port)
00081 {
00082   err_t result;
00083   struct netbuf buf;
00084   
00085   memset(&buf, 0, sizeof(buf));
00086   buf.p = p;
00087   result = netconn_sendto((struct netconn*)handle, &buf, dst, port);
00088   
00089   return result;
00090 }
00091 
00092 u8_t
00093 snmp_get_local_ip_for_dst(void* handle, const ip_addr_t *dst, ip_addr_t *result)
00094 {
00095   struct netconn* conn = (struct netconn*)handle;
00096   struct netif *dst_if;
00097   const ip_addr_t* dst_ip;
00098 
00099   LWIP_UNUSED_ARG(conn); /* unused in case of IPV4 only configuration */
00100 
00101   ip_route_get_local_ip(&conn->pcb.udp->local_ip, dst, dst_if, dst_ip);
00102 
00103   if ((dst_if != NULL) && (dst_ip != NULL)) {
00104     ip_addr_copy(*result, *dst_ip);
00105     return 1;
00106   } else {
00107     return 0;
00108   }
00109 }
00110 
00111 /**
00112  * Starts SNMP Agent.
00113  */
00114 void
00115 snmp_init(void)
00116 {
00117   sys_thread_new("snmp_netconn", snmp_netconn_thread, NULL, SNMP_STACK_SIZE, SNMP_THREAD_PRIO);
00118 }
00119 
00120 #endif /* LWIP_SNMP && SNMP_USE_NETCONN */