2007-09-16 19:46:25 +02:00
|
|
|
/****************************************************************************
|
2008-12-12 19:33:36 +01:00
|
|
|
* net/netdev_findbyaddr.c
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
2014-03-21 00:27:59 +01:00
|
|
|
* Copyright (C) 2007-2009, 2014 Gregory Nutt. All rights reserved.
|
2012-09-13 20:32:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-09-16 19:46:25 +02: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) && CONFIG_NSOCKET_DESCRIPTORS > 0
|
|
|
|
|
2009-12-15 16:57:25 +01:00
|
|
|
#include <stdbool.h>
|
2007-09-17 00:12:04 +02:00
|
|
|
#include <string.h>
|
2007-09-16 19:46:25 +02:00
|
|
|
#include <errno.h>
|
2007-11-22 15:42:52 +01:00
|
|
|
#include <debug.h>
|
2007-09-16 19:46:25 +02:00
|
|
|
|
2012-03-04 00:18:34 +01:00
|
|
|
#include <nuttx/net/uip/uip-arch.h>
|
2007-09-16 19:46:25 +02:00
|
|
|
|
2013-10-05 21:16:18 +02:00
|
|
|
#include "net_route.h"
|
2008-12-12 19:33:36 +01:00
|
|
|
#include "net_internal.h"
|
2007-09-16 19:46:25 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2009-12-15 16:57:25 +01:00
|
|
|
* Pre-processor Definitions
|
2007-09-16 19:46:25 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-03-21 00:27:59 +01:00
|
|
|
* Private Types
|
2007-09-16 19:46:25 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-11-22 15:42:52 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Function: netdev_maskcmp
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-05 21:16:18 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Function: netdev_finddevice
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a previously registered network device by matching a local address
|
2014-03-21 00:27:59 +01:00
|
|
|
* with the subnet served by the device. Only "up" devices are considered
|
|
|
|
* (since a "down" device has no meaningful address).
|
2013-10-05 21:16:18 +02:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* addr - Pointer to the remote address of a connection
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Pointer to driver on success; null on failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called from normal user mode
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct uip_driver_s *netdev_finddevice(const uip_ipaddr_t addr)
|
2007-11-22 15:42:52 +01:00
|
|
|
{
|
2013-10-05 21:16:18 +02:00
|
|
|
struct uip_driver_s *dev;
|
2014-03-21 00:27:59 +01:00
|
|
|
|
|
|
|
/* Examine each registered network device */
|
2013-10-05 21:16:18 +02:00
|
|
|
|
|
|
|
netdev_semtake();
|
|
|
|
for (dev = g_netdevices; dev; dev = dev->flink)
|
|
|
|
{
|
2014-03-22 16:24:25 +01:00
|
|
|
/* Is the interface in the "up" state? */
|
2014-03-21 00:27:59 +01:00
|
|
|
|
2014-03-22 16:24:25 +01:00
|
|
|
if ((dev->d_flags & IFF_UP) != 0)
|
2013-10-05 21:16:18 +02:00
|
|
|
{
|
2014-03-22 16:24:25 +01:00
|
|
|
/* Yes.. check for an address match (under the netmask) */
|
2014-03-21 00:27:59 +01:00
|
|
|
|
2014-03-22 16:24:25 +01:00
|
|
|
if (uip_ipaddr_maskcmp(dev->d_ipaddr, addr, dev->d_netmask))
|
2014-03-21 00:27:59 +01:00
|
|
|
{
|
2014-03-22 16:24:25 +01:00
|
|
|
/* Its a match */
|
2014-03-21 00:27:59 +01:00
|
|
|
|
2014-03-22 16:24:25 +01:00
|
|
|
netdev_semgive();
|
|
|
|
return dev;
|
2014-03-21 00:27:59 +01:00
|
|
|
}
|
2013-10-05 21:16:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-21 00:27:59 +01:00
|
|
|
/* No device with the matching address found */
|
|
|
|
|
2013-10-05 21:16:18 +02:00
|
|
|
netdev_semgive();
|
|
|
|
return NULL;
|
2007-11-22 15:42:52 +01:00
|
|
|
}
|
|
|
|
|
2007-09-16 19:46:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Global Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2007-11-22 15:42:52 +01:00
|
|
|
* Function: netdev_findbyaddr
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2013-10-05 21:16:18 +02:00
|
|
|
* Find a previously registered network device by matching an arbitrary
|
|
|
|
* IP address.
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
|
|
|
* Parameters:
|
2013-10-05 21:16:18 +02:00
|
|
|
* addr - Pointer to the remote address of a connection
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Pointer to driver on success; null on failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called from normal user mode
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-05 21:16:18 +02:00
|
|
|
FAR struct uip_driver_s *netdev_findbyaddr(const uip_ipaddr_t addr)
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
|
|
|
struct uip_driver_s *dev;
|
2013-10-05 21:16:18 +02:00
|
|
|
#ifdef CONFIG_NET_ROUTE
|
|
|
|
uip_ipaddr_t router;
|
|
|
|
int ret;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* First, see if the address maps to the a local network */
|
|
|
|
|
|
|
|
dev = netdev_finddevice(addr);
|
|
|
|
if (dev)
|
|
|
|
{
|
|
|
|
return dev;
|
|
|
|
}
|
2007-11-22 15:42:52 +01:00
|
|
|
|
2013-10-05 21:16:18 +02:00
|
|
|
/* No.. The address lies on an external network */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ROUTE
|
|
|
|
/* If we have a routing table, then perhaps we can find the the local
|
|
|
|
* address of a router that can forward packets to the external network.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
ret = net_router(addr, router);
|
|
|
|
#else
|
|
|
|
ret = net_router(addr, &router);
|
|
|
|
#endif
|
|
|
|
if (ret >= 0)
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
2013-10-05 21:16:18 +02:00
|
|
|
/* Success... try to find the network device associated with the local
|
|
|
|
* router address
|
|
|
|
*/
|
|
|
|
|
|
|
|
dev = netdev_finddevice(router);
|
|
|
|
if (dev)
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
2013-10-05 21:16:18 +02:00
|
|
|
return dev;
|
2007-09-16 19:46:25 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-05 21:16:18 +02:00
|
|
|
#endif /* CONFIG_NET_ROUTE */
|
|
|
|
|
|
|
|
/* The above lookup will fail if the packet is being sent out of our
|
|
|
|
* out subnet to a router and there is no routing information.
|
|
|
|
*
|
|
|
|
* However, if there is only a single, registered network interface, then
|
|
|
|
* the decision is pretty easy. Use that device and its default router
|
|
|
|
* address.
|
|
|
|
*/
|
|
|
|
|
|
|
|
netdev_semtake();
|
|
|
|
if (g_netdevices && !g_netdevices->flink)
|
|
|
|
{
|
|
|
|
dev = g_netdevices;
|
|
|
|
}
|
|
|
|
netdev_semgive();
|
|
|
|
|
|
|
|
/* If we will did not find the network device, then we might as well fail
|
|
|
|
* because we are not configured properly to determine the route to the
|
|
|
|
* target.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return dev;
|
2007-09-16 19:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */
|