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