snmp_raw.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @file
  3. * SNMP RAW API frontend.
  4. */
  5. /*
  6. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * Author: Dirk Ziegelmeier <dziegel@gmx.de>
  32. */
  33. #include "lwip/apps/snmp_opts.h"
  34. #include "lwip/ip_addr.h"
  35. #if LWIP_SNMP && SNMP_USE_RAW
  36. #include "lwip/udp.h"
  37. #include "lwip/ip.h"
  38. #include "snmp_msg.h"
  39. /* lwIP UDP receive callback function */
  40. static void
  41. snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
  42. {
  43. LWIP_UNUSED_ARG(arg);
  44. snmp_receive(pcb, p, addr, port);
  45. pbuf_free(p);
  46. }
  47. err_t
  48. snmp_sendto(void *handle, struct pbuf *p, const ip_addr_t *dst, u16_t port)
  49. {
  50. return udp_sendto((struct udp_pcb*)handle, p, dst, port);
  51. }
  52. u8_t
  53. snmp_get_local_ip_for_dst(void* handle, const ip_addr_t *dst, ip_addr_t *result)
  54. {
  55. struct udp_pcb* udp_pcb = (struct udp_pcb*)handle;
  56. struct netif *dst_if;
  57. const ip_addr_t* dst_ip;
  58. LWIP_UNUSED_ARG(udp_pcb); /* unused in case of IPV4 only configuration */
  59. ip_route_get_local_ip(&udp_pcb->local_ip, dst, dst_if, dst_ip);
  60. if ((dst_if != NULL) && (dst_ip != NULL)) {
  61. ip_addr_copy(*result, *dst_ip);
  62. return 1;
  63. } else {
  64. return 0;
  65. }
  66. }
  67. /**
  68. * @ingroup snmp_core
  69. * Starts SNMP Agent.
  70. * Allocates UDP pcb and binds it to IP_ANY_TYPE port 161.
  71. */
  72. void
  73. snmp_init(void)
  74. {
  75. err_t err;
  76. struct udp_pcb *snmp_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
  77. LWIP_ERROR("snmp_raw: no PCB", (snmp_pcb != NULL), return;);
  78. snmp_traps_handle = snmp_pcb;
  79. udp_recv(snmp_pcb, snmp_recv, (void *)SNMP_IN_PORT);
  80. err = udp_bind(snmp_pcb, IP_ANY_TYPE, SNMP_IN_PORT);
  81. LWIP_ERROR("snmp_raw: Unable to bind PCB", (err == ERR_OK), return;);
  82. }
  83. #endif /* LWIP_SNMP && SNMP_USE_RAW */