Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_snmp_raw.c Source File

lwip_snmp_raw.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * SNMP RAW API 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 #include "lwip/ip_addr.h"
00037 
00038 #if LWIP_SNMP && SNMP_USE_RAW
00039 
00040 #include "lwip/udp.h"
00041 #include "lwip/ip.h"
00042 #include "snmp_msg.h"
00043 
00044 /* lwIP UDP receive callback function */
00045 static void
00046 snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
00047 {
00048   LWIP_UNUSED_ARG(arg);
00049 
00050   snmp_receive(pcb, p, addr, port);
00051 
00052   pbuf_free(p);
00053 }
00054 
00055 err_t 
00056 snmp_sendto(void *handle, struct pbuf *p, const ip_addr_t *dst, u16_t port)
00057 {
00058   return udp_sendto((struct udp_pcb*)handle, p, dst, port);
00059 }
00060 
00061 u8_t
00062 snmp_get_local_ip_for_dst(void* handle, const ip_addr_t *dst, ip_addr_t *result)
00063 {
00064   struct udp_pcb* udp_pcb = (struct udp_pcb*)handle;
00065   struct netif *dst_if;
00066   const ip_addr_t* dst_ip;
00067 
00068   LWIP_UNUSED_ARG(udp_pcb); /* unused in case of IPV4 only configuration */
00069 
00070   ip_route_get_local_ip(&udp_pcb->local_ip, dst, dst_if, dst_ip);
00071 
00072   if ((dst_if != NULL) && (dst_ip != NULL)) {
00073     ip_addr_copy(*result, *dst_ip);
00074     return 1;
00075   } else {
00076     return 0;
00077   }
00078 }
00079 
00080 /**
00081  * @ingroup snmp_core
00082  * Starts SNMP Agent.
00083  * Allocates UDP pcb and binds it to IP_ANY_TYPE port 161.
00084  */
00085 void
00086 snmp_init(void)
00087 {
00088   err_t err;
00089   
00090   struct udp_pcb *snmp_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
00091   LWIP_ERROR("snmp_raw: no PCB", (snmp_pcb != NULL), return;);
00092 
00093   snmp_traps_handle = snmp_pcb;
00094 
00095   udp_recv(snmp_pcb, snmp_recv, (void *)SNMP_IN_PORT);
00096   err = udp_bind(snmp_pcb, IP_ANY_TYPE, SNMP_IN_PORT);
00097   LWIP_ERROR("snmp_raw: Unable to bind PCB", (err == ERR_OK), return;);
00098 }
00099 
00100 #endif /* LWIP_SNMP && SNMP_USE_RAW */