xref: /third_party/lwip/src/apps/snmp/snmp_netconn.c (revision 195972f6)
1195972f6Sopenharmony_ci/**
2195972f6Sopenharmony_ci * @file
3195972f6Sopenharmony_ci * SNMP netconn frontend.
4195972f6Sopenharmony_ci */
5195972f6Sopenharmony_ci
6195972f6Sopenharmony_ci/*
7195972f6Sopenharmony_ci * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8195972f6Sopenharmony_ci * All rights reserved.
9195972f6Sopenharmony_ci *
10195972f6Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
11195972f6Sopenharmony_ci * are permitted provided that the following conditions are met:
12195972f6Sopenharmony_ci *
13195972f6Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice,
14195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer.
15195972f6Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice,
16195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer in the documentation
17195972f6Sopenharmony_ci *    and/or other materials provided with the distribution.
18195972f6Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote products
19195972f6Sopenharmony_ci *    derived from this software without specific prior written permission.
20195972f6Sopenharmony_ci *
21195972f6Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22195972f6Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23195972f6Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24195972f6Sopenharmony_ci * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25195972f6Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26195972f6Sopenharmony_ci * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27195972f6Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28195972f6Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29195972f6Sopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30195972f6Sopenharmony_ci * OF SUCH DAMAGE.
31195972f6Sopenharmony_ci *
32195972f6Sopenharmony_ci * Author: Dirk Ziegelmeier <dziegel@gmx.de>
33195972f6Sopenharmony_ci */
34195972f6Sopenharmony_ci
35195972f6Sopenharmony_ci#include "lwip/apps/snmp_opts.h"
36195972f6Sopenharmony_ci
37195972f6Sopenharmony_ci#if LWIP_SNMP && SNMP_USE_NETCONN
38195972f6Sopenharmony_ci
39195972f6Sopenharmony_ci#include <string.h>
40195972f6Sopenharmony_ci#include "lwip/api.h"
41195972f6Sopenharmony_ci#include "lwip/ip.h"
42195972f6Sopenharmony_ci#include "lwip/udp.h"
43195972f6Sopenharmony_ci#include "snmp_msg.h"
44195972f6Sopenharmony_ci#include "lwip/sys.h"
45195972f6Sopenharmony_ci#include "lwip/prot/iana.h"
46195972f6Sopenharmony_ci
47195972f6Sopenharmony_ci/** SNMP netconn API worker thread */
48195972f6Sopenharmony_cistatic void
49195972f6Sopenharmony_cisnmp_netconn_thread(void *arg)
50195972f6Sopenharmony_ci{
51195972f6Sopenharmony_ci  struct netconn *conn;
52195972f6Sopenharmony_ci  struct netbuf *buf;
53195972f6Sopenharmony_ci  err_t err;
54195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(arg);
55195972f6Sopenharmony_ci
56195972f6Sopenharmony_ci  /* Bind to SNMP port with default IP address */
57195972f6Sopenharmony_ci#if LWIP_IPV6
58195972f6Sopenharmony_ci  conn = netconn_new(NETCONN_UDP_IPV6);
59195972f6Sopenharmony_ci  netconn_bind(conn, IP6_ADDR_ANY, LWIP_IANA_PORT_SNMP);
60195972f6Sopenharmony_ci#else /* LWIP_IPV6 */
61195972f6Sopenharmony_ci  conn = netconn_new(NETCONN_UDP);
62195972f6Sopenharmony_ci  netconn_bind(conn, IP4_ADDR_ANY, LWIP_IANA_PORT_SNMP);
63195972f6Sopenharmony_ci#endif /* LWIP_IPV6 */
64195972f6Sopenharmony_ci  LWIP_ERROR("snmp_netconn: invalid conn", (conn != NULL), return;);
65195972f6Sopenharmony_ci
66195972f6Sopenharmony_ci  snmp_traps_handle = conn;
67195972f6Sopenharmony_ci
68195972f6Sopenharmony_ci  do {
69195972f6Sopenharmony_ci    err = netconn_recv(conn, &buf);
70195972f6Sopenharmony_ci
71195972f6Sopenharmony_ci    if (err == ERR_OK) {
72195972f6Sopenharmony_ci      snmp_receive(conn, buf->p, &buf->addr, buf->port);
73195972f6Sopenharmony_ci    }
74195972f6Sopenharmony_ci
75195972f6Sopenharmony_ci    if (buf != NULL) {
76195972f6Sopenharmony_ci      netbuf_delete(buf);
77195972f6Sopenharmony_ci    }
78195972f6Sopenharmony_ci  } while (1);
79195972f6Sopenharmony_ci}
80195972f6Sopenharmony_ci
81195972f6Sopenharmony_cierr_t
82195972f6Sopenharmony_cisnmp_sendto(void *handle, struct pbuf *p, const ip_addr_t *dst, u16_t port)
83195972f6Sopenharmony_ci{
84195972f6Sopenharmony_ci  err_t result;
85195972f6Sopenharmony_ci  struct netbuf buf;
86195972f6Sopenharmony_ci
87195972f6Sopenharmony_ci  memset(&buf, 0, sizeof(buf));
88195972f6Sopenharmony_ci  buf.p = p;
89195972f6Sopenharmony_ci  result = netconn_sendto((struct netconn *)handle, &buf, dst, port);
90195972f6Sopenharmony_ci
91195972f6Sopenharmony_ci  return result;
92195972f6Sopenharmony_ci}
93195972f6Sopenharmony_ci
94195972f6Sopenharmony_ciu8_t
95195972f6Sopenharmony_cisnmp_get_local_ip_for_dst(void *handle, const ip_addr_t *dst, ip_addr_t *result)
96195972f6Sopenharmony_ci{
97195972f6Sopenharmony_ci  struct netconn *conn = (struct netconn *)handle;
98195972f6Sopenharmony_ci  struct netif *dst_if;
99195972f6Sopenharmony_ci  const ip_addr_t *dst_ip;
100195972f6Sopenharmony_ci
101195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(conn); /* unused in case of IPV4 only configuration */
102195972f6Sopenharmony_ci
103195972f6Sopenharmony_ci  ip_route_get_local_ip(&conn->pcb.udp->local_ip, dst, dst_if, dst_ip);
104195972f6Sopenharmony_ci
105195972f6Sopenharmony_ci  if ((dst_if != NULL) && (dst_ip != NULL)) {
106195972f6Sopenharmony_ci    ip_addr_copy(*result, *dst_ip);
107195972f6Sopenharmony_ci    return 1;
108195972f6Sopenharmony_ci  } else {
109195972f6Sopenharmony_ci    return 0;
110195972f6Sopenharmony_ci  }
111195972f6Sopenharmony_ci}
112195972f6Sopenharmony_ci
113195972f6Sopenharmony_ci/**
114195972f6Sopenharmony_ci * Starts SNMP Agent.
115195972f6Sopenharmony_ci */
116195972f6Sopenharmony_civoid
117195972f6Sopenharmony_cisnmp_init(void)
118195972f6Sopenharmony_ci{
119195972f6Sopenharmony_ci  LWIP_ASSERT_CORE_LOCKED();
120195972f6Sopenharmony_ci  sys_thread_new("snmp_netconn", snmp_netconn_thread, NULL, SNMP_STACK_SIZE, SNMP_THREAD_PRIO);
121195972f6Sopenharmony_ci}
122195972f6Sopenharmony_ci
123195972f6Sopenharmony_ci#endif /* LWIP_SNMP && SNMP_USE_NETCONN */
124