1195972f6Sopenharmony_ci/**
2195972f6Sopenharmony_ci * @file
3195972f6Sopenharmony_ci * SNMP RAW API 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#include "lwip/ip_addr.h"
37195972f6Sopenharmony_ci
38195972f6Sopenharmony_ci#if LWIP_SNMP && SNMP_USE_RAW
39195972f6Sopenharmony_ci
40195972f6Sopenharmony_ci#include "lwip/udp.h"
41195972f6Sopenharmony_ci#include "lwip/ip.h"
42195972f6Sopenharmony_ci#include "lwip/prot/iana.h"
43195972f6Sopenharmony_ci#include "snmp_msg.h"
44195972f6Sopenharmony_ci
45195972f6Sopenharmony_ci/* lwIP UDP receive callback function */
46195972f6Sopenharmony_cistatic void
47195972f6Sopenharmony_cisnmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
48195972f6Sopenharmony_ci{
49195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(arg);
50195972f6Sopenharmony_ci
51195972f6Sopenharmony_ci  snmp_receive(pcb, p, addr, port);
52195972f6Sopenharmony_ci
53195972f6Sopenharmony_ci  pbuf_free(p);
54195972f6Sopenharmony_ci}
55195972f6Sopenharmony_ci
56195972f6Sopenharmony_cierr_t
57195972f6Sopenharmony_cisnmp_sendto(void *handle, struct pbuf *p, const ip_addr_t *dst, u16_t port)
58195972f6Sopenharmony_ci{
59195972f6Sopenharmony_ci  return udp_sendto((struct udp_pcb *)handle, p, dst, port);
60195972f6Sopenharmony_ci}
61195972f6Sopenharmony_ci
62195972f6Sopenharmony_ciu8_t
63195972f6Sopenharmony_cisnmp_get_local_ip_for_dst(void *handle, const ip_addr_t *dst, ip_addr_t *result)
64195972f6Sopenharmony_ci{
65195972f6Sopenharmony_ci  struct udp_pcb *udp_pcb = (struct udp_pcb *)handle;
66195972f6Sopenharmony_ci  struct netif *dst_if;
67195972f6Sopenharmony_ci  const ip_addr_t *dst_ip;
68195972f6Sopenharmony_ci
69195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(udp_pcb); /* unused in case of IPV4 only configuration */
70195972f6Sopenharmony_ci
71195972f6Sopenharmony_ci  ip_route_get_local_ip(&udp_pcb->local_ip, dst, dst_if, dst_ip);
72195972f6Sopenharmony_ci
73195972f6Sopenharmony_ci  if ((dst_if != NULL) && (dst_ip != NULL)) {
74195972f6Sopenharmony_ci    ip_addr_copy(*result, *dst_ip);
75195972f6Sopenharmony_ci    return 1;
76195972f6Sopenharmony_ci  } else {
77195972f6Sopenharmony_ci    return 0;
78195972f6Sopenharmony_ci  }
79195972f6Sopenharmony_ci}
80195972f6Sopenharmony_ci
81195972f6Sopenharmony_ci/**
82195972f6Sopenharmony_ci * @ingroup snmp_core
83195972f6Sopenharmony_ci * Starts SNMP Agent.
84195972f6Sopenharmony_ci * Allocates UDP pcb and binds it to IP_ANY_TYPE port 161.
85195972f6Sopenharmony_ci */
86195972f6Sopenharmony_civoid
87195972f6Sopenharmony_cisnmp_init(void)
88195972f6Sopenharmony_ci{
89195972f6Sopenharmony_ci  err_t err;
90195972f6Sopenharmony_ci
91195972f6Sopenharmony_ci  struct udp_pcb *snmp_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
92195972f6Sopenharmony_ci  LWIP_ERROR("snmp_raw: no PCB", (snmp_pcb != NULL), return;);
93195972f6Sopenharmony_ci
94195972f6Sopenharmony_ci  LWIP_ASSERT_CORE_LOCKED();
95195972f6Sopenharmony_ci
96195972f6Sopenharmony_ci  snmp_traps_handle = snmp_pcb;
97195972f6Sopenharmony_ci
98195972f6Sopenharmony_ci  udp_recv(snmp_pcb, snmp_recv, NULL);
99195972f6Sopenharmony_ci  err = udp_bind(snmp_pcb, IP_ANY_TYPE, LWIP_IANA_PORT_SNMP);
100195972f6Sopenharmony_ci  LWIP_ERROR("snmp_raw: Unable to bind PCB", (err == ERR_OK), return;);
101195972f6Sopenharmony_ci}
102195972f6Sopenharmony_ci
103195972f6Sopenharmony_ci#endif /* LWIP_SNMP && SNMP_USE_RAW */
104