2007-11-07 00:38:14 +01:00
|
|
|
/****************************************************************************
|
2014-06-18 18:50:09 +02:00
|
|
|
* net/udp/udp_callback.c
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
2015-01-30 15:54:40 +01:00
|
|
|
* Copyright (C) 2007-2009, 2015 Gregory Nutt. All rights reserved.
|
2012-09-13 20:32:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP)
|
|
|
|
|
2009-12-15 15:53:45 +01:00
|
|
|
#include <stdint.h>
|
2015-02-02 18:37:22 +01:00
|
|
|
#include <string.h>
|
2007-11-07 00:38:14 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2014-06-24 16:53:28 +02:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2015-01-30 15:54:40 +01:00
|
|
|
#include <nuttx/net/netstats.h>
|
2014-06-26 17:32:39 +02:00
|
|
|
#include <nuttx/net/udp.h>
|
2007-11-07 00:38:14 +01:00
|
|
|
|
2014-06-29 02:07:02 +02:00
|
|
|
#include "devif/devif.h"
|
2014-06-25 02:55:01 +02:00
|
|
|
#include "udp/udp.h"
|
2007-11-07 00:38:14 +01:00
|
|
|
|
2015-01-30 13:08:26 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define IPv4BUF ((FAR struct ipv4_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
|
|
|
|
#define IPv6BUF ((FAR struct ipv6_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
|
|
|
|
|
|
|
|
#define UDPIPv4BUF ((FAR struct udp_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv4_HDRLEN])
|
|
|
|
#define UDPIPv6BUF ((FAR struct udp_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv6_HDRLEN])
|
|
|
|
|
2007-11-07 00:38:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-30 13:08:26 +01:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: udp_datahandler
|
2015-01-30 13:08:26 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the receipt of UDP data by adding the newly received packet to
|
|
|
|
* the UDP read-ahead buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_UDP_READAHEAD
|
|
|
|
static uint16_t udp_datahandler(FAR struct net_driver_s *dev, FAR struct udp_conn_s *conn,
|
|
|
|
FAR uint8_t *buffer, uint16_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct iob_s *iob;
|
|
|
|
int ret;
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
FAR struct sockaddr_in6 src_addr6;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
FAR struct sockaddr_in src_addr4;
|
|
|
|
#endif
|
|
|
|
FAR void *src_addr;
|
|
|
|
uint8_t src_addr_size;
|
|
|
|
|
|
|
|
/* Allocate on I/O buffer to start the chain (throttling as necessary).
|
|
|
|
* We will not wait for an I/O buffer to become available in this context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
iob = iob_tryalloc(true);
|
|
|
|
if (iob == NULL)
|
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to create new I/O buffer chain\n");
|
2015-01-30 13:08:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (IFF_IS_IPv6(dev->d_flags))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FAR struct udp_hdr_s *udp = UDPIPv6BUF;
|
|
|
|
FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;
|
|
|
|
|
|
|
|
src_addr6.sin6_family = AF_INET6;
|
|
|
|
src_addr6.sin6_port = udp->srcport;
|
|
|
|
|
|
|
|
net_ipv6addr_copy(src_addr6.sin6_addr.s6_addr, ipv6->srcipaddr);
|
|
|
|
|
|
|
|
src_addr_size = sizeof(src_addr6);
|
|
|
|
src_addr = &src_addr6;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2015-02-16 21:33:58 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2015-02-17 14:37:44 +01:00
|
|
|
/* Hybrid dual-stack IPv6/IPv4 implementations recognize a special
|
|
|
|
* class of addresses, the IPv4-mapped IPv6 addresses.
|
|
|
|
*/
|
|
|
|
|
2016-02-26 13:45:37 +01:00
|
|
|
if (conn->domain == PF_INET6)
|
2015-02-16 21:33:58 +01:00
|
|
|
{
|
2015-05-27 19:39:44 +02:00
|
|
|
FAR struct udp_hdr_s *udp = UDPIPv6BUF;
|
|
|
|
FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;
|
2015-02-17 14:37:44 +01:00
|
|
|
in_addr_t ipv4addr;
|
2015-02-16 21:33:58 +01:00
|
|
|
|
2015-02-17 14:37:44 +01:00
|
|
|
/* Encode the IPv4 address as an IPv-mapped IPv6 address */
|
2015-02-16 21:33:58 +01:00
|
|
|
|
2015-02-17 14:37:44 +01:00
|
|
|
src_addr6.sin6_family = AF_INET6;
|
|
|
|
src_addr6.sin6_port = udp->srcport;
|
2015-02-16 21:33:58 +01:00
|
|
|
|
2015-05-27 19:39:44 +02:00
|
|
|
ipv4addr = net_ip4addr_conv32(ipv6->srcipaddr);
|
|
|
|
ip6_map_ipv4addr(ipv4addr, src_addr6.sin6_addr.s6_addr16);
|
2015-02-16 21:33:58 +01:00
|
|
|
|
|
|
|
src_addr_size = sizeof(src_addr6);
|
|
|
|
src_addr = &src_addr6;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2015-05-27 19:39:44 +02:00
|
|
|
FAR struct udp_hdr_s *udp = UDPIPv4BUF;
|
|
|
|
FAR struct ipv4_hdr_s *ipv4 = IPv4BUF;
|
|
|
|
|
2015-02-16 21:33:58 +01:00
|
|
|
src_addr4.sin_family = AF_INET;
|
|
|
|
src_addr4.sin_port = udp->srcport;
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2015-02-16 21:33:58 +01:00
|
|
|
net_ipv4addr_copy(src_addr4.sin_addr.s_addr,
|
2015-01-30 13:08:26 +01:00
|
|
|
net_ip4addr_conv32(ipv4->srcipaddr));
|
|
|
|
|
2015-02-16 21:33:58 +01:00
|
|
|
src_addr_size = sizeof(src_addr4);
|
|
|
|
src_addr = &src_addr4;
|
|
|
|
}
|
2015-01-30 13:08:26 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
/* Copy the src address info into the I/O buffer chain. We will not wait
|
|
|
|
* for an I/O buffer to become available in this context. It there is
|
|
|
|
* any failure to allocated, the entire I/O buffer chain will be discarded.
|
|
|
|
*/
|
|
|
|
|
2015-10-08 23:10:04 +02:00
|
|
|
ret = iob_trycopyin(iob, (FAR const uint8_t *)&src_addr_size,
|
2015-01-30 13:08:26 +01:00
|
|
|
sizeof(uint8_t), 0, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* On a failure, iob_trycopyin return a negated error value but does
|
|
|
|
* not free any I/O buffers.
|
|
|
|
*/
|
|
|
|
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to add data to the I/O buffer chain: %d\n", ret);
|
2015-01-30 13:08:26 +01:00
|
|
|
(void)iob_free_chain(iob);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:10:04 +02:00
|
|
|
ret = iob_trycopyin(iob, (FAR const uint8_t *)src_addr, src_addr_size,
|
2015-01-30 13:08:26 +01:00
|
|
|
sizeof(uint8_t), true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* On a failure, iob_trycopyin return a negated error value but does
|
|
|
|
* not free any I/O buffers.
|
|
|
|
*/
|
|
|
|
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to add data to the I/O buffer chain: %d\n", ret);
|
2015-01-30 13:08:26 +01:00
|
|
|
(void)iob_free_chain(iob);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-11 15:55:40 +02:00
|
|
|
if (buflen > 0)
|
2015-01-30 13:08:26 +01:00
|
|
|
{
|
2015-08-11 15:55:40 +02:00
|
|
|
/* Copy the new appdata into the I/O buffer chain */
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2015-08-11 15:55:40 +02:00
|
|
|
ret = iob_trycopyin(iob, buffer, buflen,
|
|
|
|
src_addr_size + sizeof(uint8_t), true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* On a failure, iob_trycopyin return a negated error value but
|
|
|
|
* does not free any I/O buffers.
|
|
|
|
*/
|
|
|
|
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to add data to the I/O buffer chain: %d\n",
|
|
|
|
ret);
|
2015-08-11 15:55:40 +02:00
|
|
|
(void)iob_free_chain(iob);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-01-30 13:08:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the new I/O buffer chain to the tail of the read-ahead queue */
|
|
|
|
|
|
|
|
ret = iob_tryadd_queue(iob, &conn->readahead);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to queue the I/O buffer chain: %d\n", ret);
|
2015-01-30 13:08:26 +01:00
|
|
|
(void)iob_free_chain(iob);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Buffered %d bytes\n", buflen);
|
2015-01-30 13:08:26 +01:00
|
|
|
return buflen;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_UDP_READAHEAD */
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: net_dataevent
|
2015-01-30 13:08:26 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handling the network UDP_NEWDATA event.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline uint16_t
|
|
|
|
net_dataevent(FAR struct net_driver_s *dev, FAR struct udp_conn_s *conn,
|
|
|
|
uint16_t flags)
|
|
|
|
{
|
|
|
|
uint16_t ret;
|
2015-08-11 15:55:40 +02:00
|
|
|
#ifdef CONFIG_NET_UDP_READAHEAD
|
|
|
|
uint8_t *buffer = dev->d_appdata;
|
|
|
|
int buflen = dev->d_len;
|
|
|
|
uint16_t recvlen;
|
|
|
|
#endif
|
2015-01-30 13:08:26 +01:00
|
|
|
|
|
|
|
ret = (flags & ~UDP_NEWDATA);
|
|
|
|
|
|
|
|
/* Is there new data? With non-zero length? (Certain connection events
|
|
|
|
* can have zero-length with UDP_NEWDATA set just to cause an ACK).
|
|
|
|
*/
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("No receive on connection\n");
|
2015-01-30 13:08:26 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_UDP_READAHEAD
|
2015-08-11 15:55:40 +02:00
|
|
|
/* Save as the packet data as in the read-ahead buffer. NOTE that
|
|
|
|
* partial packets will not be buffered.
|
|
|
|
*/
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2015-08-11 15:55:40 +02:00
|
|
|
recvlen = udp_datahandler(dev, conn, buffer, buflen);
|
|
|
|
if (recvlen < buflen)
|
2015-01-30 13:08:26 +01:00
|
|
|
#endif
|
2015-08-11 15:55:40 +02:00
|
|
|
{
|
|
|
|
/* There is no handler to receive new data and there are no free
|
|
|
|
* read-ahead buffers to retain the data -- drop the packet.
|
|
|
|
*/
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Dropped %d bytes\n", dev->d_len);
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2015-08-11 15:55:40 +02:00
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
|
|
|
g_netstats.udp.drop++;
|
2015-01-30 13:08:26 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In any event, the new data has now been handled */
|
|
|
|
|
|
|
|
dev->d_len = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-11-07 00:38:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: udp_callback
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Inform the application holding the UDP socket of a change in state.
|
|
|
|
*
|
2013-09-06 17:18:07 +02:00
|
|
|
* Returned Value:
|
|
|
|
* OK if packet has been processed, otherwise ERROR.
|
|
|
|
*
|
2007-11-07 00:38:14 +01:00
|
|
|
* Assumptions:
|
|
|
|
* This function is called at the interrupt level with interrupts disabled.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-28 00:48:12 +02:00
|
|
|
uint16_t udp_callback(FAR struct net_driver_s *dev,
|
2014-06-25 02:55:01 +02:00
|
|
|
FAR struct udp_conn_s *conn, uint16_t flags)
|
2007-11-07 00:38:14 +01:00
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("flags: %04x\n", flags);
|
2007-11-07 00:38:14 +01:00
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
|
2008-09-01 17:22:54 +02:00
|
|
|
if (conn)
|
2007-11-07 00:38:14 +01:00
|
|
|
{
|
|
|
|
/* Perform the callback */
|
|
|
|
|
2015-05-28 20:01:38 +02:00
|
|
|
flags = devif_conn_event(dev, conn, flags, conn->list);
|
2015-01-30 13:08:26 +01:00
|
|
|
|
|
|
|
if ((flags & UDP_NEWDATA) != 0)
|
|
|
|
{
|
|
|
|
/* Data was not handled.. dispose of it appropriately */
|
|
|
|
|
|
|
|
flags = net_dataevent(dev, conn, flags);
|
|
|
|
}
|
2007-11-07 00:38:14 +01:00
|
|
|
}
|
2013-09-06 17:18:07 +02:00
|
|
|
|
2013-10-11 18:48:00 +02:00
|
|
|
return flags;
|
2007-11-07 00:38:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_UDP */
|