2017-09-29 16:33:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/route/net_fileroute.c
|
|
|
|
*
|
2020-01-11 16:40:32 +01:00
|
|
|
* Copyright (C) 2017, 2020 Gregory Nutt. All rights reserved.
|
2017-09-29 16:33:36 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2018-01-05 15:57:22 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2017-09-29 16:33:36 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
|
|
|
#include "route/fileroute.h"
|
|
|
|
#include "route/route.h"
|
|
|
|
|
|
|
|
#if defined(CONFIG_ROUTE_IPv4_FILEROUTE) || defined(CONFIG_ROUTE_IPv6_FILEROUTE)
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-01-13 21:26:04 +01:00
|
|
|
* Pre-processor Definitions
|
2017-09-29 16:33:36 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Special "impossible" PID value used to indicate that there is no holder
|
|
|
|
* of the lock.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define NO_HOLDER ((pid_t)-1)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv4_FILEROUTE
|
|
|
|
/* Semaphore used to lock a routing table for exclusive write-only access */
|
|
|
|
|
|
|
|
static sem_t g_ipv4_exclsem;
|
|
|
|
static pid_t g_ipv4_holder = NO_HOLDER;
|
|
|
|
static int g_ipv4_count;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv6_FILEROUTE
|
|
|
|
/* Semaphore used to lock a routing table for exclusive write-only access */
|
|
|
|
|
|
|
|
static sem_t g_ipv6_exclsem;
|
|
|
|
static pid_t g_ipv6_holder = NO_HOLDER;
|
|
|
|
static int g_ipv6_count;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_openroute_detached
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Open and detach the routing table.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-09-29 16:33:36 +02:00
|
|
|
* pathname - The full path to the routing table entry
|
|
|
|
* oflags - Open flags
|
|
|
|
* filep - Location in which to return the detached file instance.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* A non-negative file descriptor is returned on success. A negated errno
|
|
|
|
* value is returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int net_openroute_detached(FAR const char *pathname, int oflags,
|
|
|
|
FAR struct file *filep)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-09-15 18:49:41 +02:00
|
|
|
/* Open the file for read/write access. */
|
2017-09-29 16:33:36 +02:00
|
|
|
|
2018-09-15 18:49:41 +02:00
|
|
|
ret = file_open(filep, pathname, oflags, 0644);
|
2017-09-29 16:33:36 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-09-15 18:49:41 +02:00
|
|
|
nerr("ERROR: Failed to open %s: %d\n", pathname, ret);
|
2017-09-29 16:33:36 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_routesize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the size of a routing table in terms of the number of entries in
|
|
|
|
* the routing table.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-09-29 16:33:36 +02:00
|
|
|
* path - The path to the routing table
|
|
|
|
* entrysize - The size of one entry in the routing table
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* A non-negative count of the number of entries in the routing table is
|
|
|
|
* returned on success; a negated errno value is returned on and failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int net_routesize(FAR const char *path, size_t entrysize)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get information about the file */
|
|
|
|
|
|
|
|
ret = stat(path, &buf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode;
|
|
|
|
|
|
|
|
/* stat() failed, but is that because the routing table has not been
|
|
|
|
* created yet?
|
|
|
|
*/
|
|
|
|
|
2018-01-31 00:57:36 +01:00
|
|
|
errcode = get_errno();
|
2017-09-29 16:33:36 +02:00
|
|
|
if (errcode == ENOENT)
|
|
|
|
{
|
|
|
|
/* The routing table file has not been created. Return size zero. */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some other error */
|
|
|
|
|
|
|
|
return -errcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The directory entry at this path must be a regular file */
|
|
|
|
|
|
|
|
if (S_ISREG(buf.st_mode))
|
|
|
|
{
|
|
|
|
unsigned int nentries = buf.st_size / entrysize;
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_NET_WARN
|
|
|
|
if (nentries * entrysize != buf.st_size)
|
|
|
|
{
|
2020-01-11 16:40:32 +01:00
|
|
|
nwarn("WARNING: Size of routing table is not an even multiple of "
|
|
|
|
"entries\n");
|
2017-09-29 16:33:36 +02:00
|
|
|
nwarn(" %lu != %lu / %lu\n",
|
|
|
|
(unsigned long)nentries,
|
|
|
|
(unsigned long)buf.st_size,
|
|
|
|
(unsigned long)entrysize);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return nentries;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It is probably a directory (should check for sure) */
|
|
|
|
|
|
|
|
return -EISDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_init_fileroute
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the in-memory, RAM routing table
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-09-29 16:33:36 +02:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called early in initialization so that no special protection is needed.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void net_init_fileroute(void)
|
|
|
|
{
|
|
|
|
/* Initialize semaphores */
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv4_FILEROUTE
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&g_ipv4_exclsem, 0, 1);
|
2017-09-29 16:33:36 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv6_FILEROUTE
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&g_ipv6_exclsem, 0, 1);
|
2017-09-29 16:33:36 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_openroute_ipv4/net_openroute_ipv6
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Open the IPv4/IPv6 routing table with the specified access privileges.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-09-29 16:33:36 +02:00
|
|
|
* oflags - Open flags
|
|
|
|
* filep - Location in which to return the detached file instance.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* A non-negative file descriptor is returned on success. A negated errno
|
|
|
|
* value is returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv4_FILEROUTE
|
|
|
|
int net_openroute_ipv4(int oflags, FAR struct file *filep)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Lock the route.. we don't want to open it while it is subject to
|
|
|
|
* modification.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = net_lockroute_ipv4();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: net_lockroute_ipv4() failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Open the file for read/write access. */
|
|
|
|
|
|
|
|
ret = net_openroute_detached(IPv4_ROUTE_PATH, oflags, filep);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: net_openroute_detached() failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
net_unlockroute_ipv4();
|
2017-09-29 16:33:36 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv6_FILEROUTE
|
|
|
|
int net_openroute_ipv6(int oflags, FAR struct file *filep)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Lock the route.. we don't want to open it while it is subject to
|
|
|
|
* modification.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = net_lockroute_ipv6();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: net_lockroute_ipv6() failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Open the file for read/write access. */
|
|
|
|
|
|
|
|
ret = net_openroute_detached(IPv6_ROUTE_PATH, oflags, filep);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: net_openroute_detached() failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
net_unlockroute_ipv6();
|
2017-09-29 16:33:36 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_readroute_ipv4/net_readroute_ipv6
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read one route entry from the IPv4/IPv6 routing table.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-09-29 16:33:36 +02:00
|
|
|
* filep - Detached file instance obtained by net_openroute_ipv{4|6}[_rdonly]
|
|
|
|
* route - Location to return the next route read from the file
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The number of bytes read on success. The special return valud of zero
|
2020-02-23 09:50:23 +01:00
|
|
|
* indicates that the endof of file was encountered (and nothing was read).
|
2017-09-29 16:33:36 +02:00
|
|
|
* A negated errno value is returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv4_FILEROUTE
|
|
|
|
ssize_t net_readroute_ipv4(FAR struct file *filep,
|
|
|
|
FAR struct net_route_ipv4_s *route)
|
|
|
|
{
|
2017-09-29 20:46:38 +02:00
|
|
|
FAR uint8_t *dest;
|
|
|
|
ssize_t ntotal;
|
2017-09-29 16:33:36 +02:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* Lock the route.. we don't want to read from it while it is subject to
|
|
|
|
* modification.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = (ssize_t)net_lockroute_ipv4();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: net_lockroute_ipv4() failed: %d\n", ret);
|
2017-09-29 20:46:38 +02:00
|
|
|
return ret;
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
2017-09-29 20:46:38 +02:00
|
|
|
|
|
|
|
/* Read one record from the current position in the routing table */
|
|
|
|
|
|
|
|
dest = (FAR uint8_t *)route;
|
|
|
|
ntotal = 0;
|
|
|
|
|
|
|
|
do
|
2017-09-29 16:33:36 +02:00
|
|
|
{
|
2017-09-29 20:46:38 +02:00
|
|
|
size_t nbytes = sizeof(struct net_route_ipv4_s) - ntotal;
|
|
|
|
ssize_t nread;
|
2017-09-29 16:33:36 +02:00
|
|
|
|
2017-09-29 20:46:38 +02:00
|
|
|
nread = file_read(filep, dest, nbytes);
|
|
|
|
if (nread <= 0)
|
|
|
|
{
|
|
|
|
/* Read error or end of file */
|
|
|
|
|
|
|
|
ntotal = nread;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ntotal += nread;
|
|
|
|
dest += nread;
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
2017-09-29 20:46:38 +02:00
|
|
|
while (ntotal < sizeof(struct net_route_ipv4_s));
|
2017-09-29 16:33:36 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
net_unlockroute_ipv4();
|
2017-09-29 20:46:38 +02:00
|
|
|
return ntotal;
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv6_FILEROUTE
|
|
|
|
ssize_t net_readroute_ipv6(FAR struct file *filep,
|
|
|
|
FAR struct net_route_ipv6_s *route)
|
|
|
|
{
|
2017-09-29 20:46:38 +02:00
|
|
|
FAR uint8_t *dest;
|
|
|
|
ssize_t ntotal;
|
2017-09-29 16:33:36 +02:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* Lock the route.. we don't want to read from it while it is subject to
|
|
|
|
* modification.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = (ssize_t)net_lockroute_ipv6();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: net_lockroute_ipv6() failed: %d\n", ret);
|
2017-09-29 20:46:38 +02:00
|
|
|
return ret;
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
2017-09-29 20:46:38 +02:00
|
|
|
|
|
|
|
/* Read one record from the current position in the routing table */
|
|
|
|
|
|
|
|
dest = (FAR uint8_t *)route;
|
|
|
|
ntotal = 0;
|
|
|
|
|
|
|
|
do
|
2017-09-29 16:33:36 +02:00
|
|
|
{
|
2017-09-29 20:46:38 +02:00
|
|
|
size_t nbytes = sizeof(struct net_route_ipv6_s) - ntotal;
|
|
|
|
ssize_t nread;
|
|
|
|
|
|
|
|
nread = file_read(filep, dest, nbytes);
|
|
|
|
if (nread <= 0)
|
|
|
|
{
|
|
|
|
/* Read error or end of file */
|
|
|
|
|
|
|
|
ret = nread;
|
|
|
|
break;
|
|
|
|
}
|
2017-09-29 16:33:36 +02:00
|
|
|
|
2017-09-29 20:46:38 +02:00
|
|
|
ntotal += nread;
|
|
|
|
dest += nread;
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
2017-09-29 20:46:38 +02:00
|
|
|
while (ntotal < sizeof(struct net_route_ipv6_s));
|
2017-09-29 16:33:36 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
net_unlockroute_ipv6();
|
2017-09-29 20:46:38 +02:00
|
|
|
return ntotal;
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_writeroute_ipv4/net_writeroute_ipv6
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write one route entry to the IPv4/IPv6 routing table.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-09-29 16:33:36 +02:00
|
|
|
* filep - Detached file instance obtained by net_openroute_ipv{4|6}[_rdonly]
|
|
|
|
* route - Location to return the next route read from the file
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The number of bytes written on success. A negated errno value is
|
|
|
|
* returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv4_FILEROUTE
|
|
|
|
ssize_t net_writeroute_ipv4(FAR struct file *filep,
|
|
|
|
FAR const struct net_route_ipv4_s *route)
|
|
|
|
{
|
2017-09-29 20:46:38 +02:00
|
|
|
FAR const uint8_t *src;
|
|
|
|
ssize_t ntotal;
|
2017-09-29 16:33:36 +02:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* Lock the route.. we don't want to write to it while it is subject to
|
|
|
|
* modification.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = (ssize_t)net_lockroute_ipv4();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: net_lockroute_ipv4() failed: %d\n", ret);
|
2017-09-29 20:46:38 +02:00
|
|
|
return ret;
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
2017-09-29 20:46:38 +02:00
|
|
|
|
|
|
|
/* Write one record at the current position in the routing table */
|
|
|
|
|
|
|
|
src = (FAR const uint8_t *)route;
|
|
|
|
ntotal = 0;
|
|
|
|
|
|
|
|
do
|
2017-09-29 16:33:36 +02:00
|
|
|
{
|
2017-09-29 20:46:38 +02:00
|
|
|
size_t nbytes = sizeof(struct net_route_ipv4_s) - ntotal;
|
|
|
|
ssize_t nwritten;
|
|
|
|
|
|
|
|
nwritten = file_write(filep, src, nbytes);
|
|
|
|
if (nwritten <= 0)
|
|
|
|
{
|
|
|
|
/* Write error (zero is not a valid return value for write) */
|
|
|
|
|
|
|
|
ret = nwritten;
|
|
|
|
break;
|
|
|
|
}
|
2017-09-29 16:33:36 +02:00
|
|
|
|
2017-09-29 20:46:38 +02:00
|
|
|
ntotal += nwritten;
|
|
|
|
src += nwritten;
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
2017-09-29 20:46:38 +02:00
|
|
|
while (ntotal < sizeof(struct net_route_ipv4_s));
|
2017-09-29 16:33:36 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
net_unlockroute_ipv4();
|
2017-09-29 20:46:38 +02:00
|
|
|
return ntotal;
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv6_FILEROUTE
|
|
|
|
ssize_t net_writeroute_ipv6(FAR struct file *filep,
|
|
|
|
FAR const struct net_route_ipv6_s *route)
|
|
|
|
{
|
2017-09-29 20:46:38 +02:00
|
|
|
FAR const uint8_t *src;
|
|
|
|
ssize_t ntotal;
|
2017-09-29 16:33:36 +02:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* Lock the route.. we don't want to write to it while it is subject to
|
|
|
|
* modification.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = (ssize_t)net_lockroute_ipv6();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: net_lockroute_ipv6() failed: %d\n", ret);
|
2017-09-29 20:46:38 +02:00
|
|
|
return ret;
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
2017-09-29 20:46:38 +02:00
|
|
|
|
|
|
|
/* Write one record at the current position in the routing table */
|
|
|
|
|
|
|
|
src = (FAR const uint8_t *)route;
|
|
|
|
ntotal = 0;
|
|
|
|
|
|
|
|
do
|
2017-09-29 16:33:36 +02:00
|
|
|
{
|
2017-09-29 20:46:38 +02:00
|
|
|
size_t nbytes = sizeof(struct net_route_ipv6_s) - ntotal;
|
|
|
|
ssize_t nwritten;
|
|
|
|
|
|
|
|
nwritten = file_write(filep, src, nbytes);
|
|
|
|
if (nwritten <= 0)
|
|
|
|
{
|
|
|
|
/* Write error (zero is not a valid return value for write) */
|
|
|
|
|
|
|
|
ntotal = nwritten;
|
|
|
|
break;
|
2019-07-03 02:02:23 +02:00
|
|
|
}
|
2017-09-29 16:33:36 +02:00
|
|
|
|
2017-09-29 20:46:38 +02:00
|
|
|
ntotal += nwritten;
|
|
|
|
src += nwritten;
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
2017-09-29 20:46:38 +02:00
|
|
|
while (ntotal < sizeof(struct net_route_ipv6_s));
|
2017-09-29 16:33:36 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
net_unlockroute_ipv6();
|
2017-09-29 16:33:36 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_seekroute_ipv4/net_seekroute_ipv6
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Seek to a specific entry entry to the IPv4/IPv6 routing table.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-09-29 16:33:36 +02:00
|
|
|
* filep - Detached file instance obtained by net_openroute_ipv{4|6}[_rdonly]
|
|
|
|
* index - The index of the routing table entry to seek to.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv4_FILEROUTE
|
|
|
|
off_t net_seekroute_ipv4(FAR struct file *filep, unsigned int index)
|
|
|
|
{
|
|
|
|
off_t offset;
|
|
|
|
off_t ret;
|
|
|
|
|
|
|
|
/* Convert the index to a file offset */
|
|
|
|
|
|
|
|
offset = (off_t)index * sizeof(struct net_route_ipv4_s);
|
|
|
|
|
|
|
|
/* Then seek to that position */
|
|
|
|
|
|
|
|
ret = file_seek(filep, offset, SEEK_SET);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: file_seek() failed: %ld\n", (long)ret);
|
|
|
|
return (int)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv6_FILEROUTE
|
|
|
|
off_t net_seekroute_ipv6(FAR struct file *filep, unsigned int index)
|
|
|
|
{
|
|
|
|
off_t offset;
|
|
|
|
off_t ret;
|
|
|
|
|
|
|
|
/* Convert the index to a file offset */
|
|
|
|
|
|
|
|
offset = (off_t)index * sizeof(struct net_route_ipv6_s);
|
|
|
|
|
|
|
|
/* Then seek to that position */
|
|
|
|
|
|
|
|
ret = file_seek(filep, offset, SEEK_SET);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: file_seek() failed: %ld\n", (long)ret);
|
|
|
|
return (int)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_routesize_ipv4/net_routesize_ipv6
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the size of a routing table in terms of the number of entries in
|
|
|
|
* the routing table.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-09-29 16:33:36 +02:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* A non-negative count of the number of entries in the routing table is
|
|
|
|
* returned on success; a negated errno value is returned on and failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The size of the routing table may change after this size is returned
|
|
|
|
* unless the routing table is locked to prevent any modification to the
|
|
|
|
* routing table.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv4_FILEROUTE
|
|
|
|
int net_routesize_ipv4(void)
|
|
|
|
{
|
|
|
|
return net_routesize(IPv4_ROUTE_PATH, sizeof(struct net_route_ipv4_s));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv6_FILEROUTE
|
|
|
|
int net_routesize_ipv6(void)
|
|
|
|
{
|
|
|
|
return net_routesize(IPv6_ROUTE_PATH, sizeof(struct net_route_ipv6_s));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_lockroute_ipv4/net_lockroute_ipv6
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Lock access to the routing table. Necessary when a routing table is
|
|
|
|
* being reorganized due to deletion of a route.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-09-29 16:33:36 +02:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv4_FILEROUTE
|
|
|
|
int net_lockroute_ipv4(void)
|
|
|
|
{
|
|
|
|
pid_t me = getpid();
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Are we already the holder of the lock? */
|
|
|
|
|
|
|
|
if (g_ipv4_holder == me)
|
|
|
|
{
|
|
|
|
/* Yes.. just increment the count of locks held */
|
|
|
|
|
|
|
|
g_ipv4_count++;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. wait to get the lock */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&g_ipv4_exclsem);
|
2017-09-29 16:33:36 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
nerr("ERROR: nxsem_wait() failed: %d\n", ret);
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGASSERT(g_ipv4_holder == NO_HOLDER && g_ipv4_count == 0);
|
|
|
|
|
|
|
|
/* We are now the holder with one count */
|
|
|
|
|
|
|
|
g_ipv4_holder = me;
|
|
|
|
g_ipv4_count = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv6_FILEROUTE
|
|
|
|
int net_lockroute_ipv6(void)
|
|
|
|
{
|
|
|
|
pid_t me = getpid();
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Are we already the holder of the lock? */
|
|
|
|
|
|
|
|
if (g_ipv6_holder == me)
|
|
|
|
{
|
|
|
|
/* Yes.. just increment the count of locks held */
|
|
|
|
|
|
|
|
g_ipv6_count++;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. wait to get the lock */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&g_ipv6_exclsem);
|
2017-09-29 16:33:36 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-01-05 15:57:22 +01:00
|
|
|
nerr("ERROR: nxsem_wait() failed: %d\n", ret);
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGASSERT(g_ipv6_holder == NO_HOLDER && g_ipv6_count == 0);
|
|
|
|
|
|
|
|
/* We are now the holder with one count */
|
|
|
|
|
|
|
|
g_ipv6_holder = me;
|
|
|
|
g_ipv6_count = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_unlockroute_ipv4/net_unlockroute_ipv6
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release the read lock.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-09-29 16:33:36 +02:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv4_FILEROUTE
|
|
|
|
int net_unlockroute_ipv4(void)
|
|
|
|
{
|
|
|
|
pid_t me = getpid();
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* If would be an error if we are called with on a thread that does not
|
|
|
|
* hold the lock.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(me == g_ipv4_holder && g_ipv4_count > 0);
|
|
|
|
|
|
|
|
/* Release the count on the lock. If this is the last count, then release
|
|
|
|
* the lock.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (g_ipv4_count > 1)
|
|
|
|
{
|
|
|
|
/* Not the last count... just decrement the count and return success */
|
|
|
|
|
|
|
|
g_ipv4_count--;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This is the last count. Release the lock */
|
|
|
|
|
|
|
|
g_ipv4_holder = NO_HOLDER;
|
|
|
|
g_ipv4_count = 0;
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
ret = nxsem_post(&g_ipv4_exclsem);
|
2017-09-29 16:33:36 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nerr("ERROR: nxsem_post() failed: %d\n", ret);
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv6_FILEROUTE
|
|
|
|
int net_unlockroute_ipv6(void)
|
|
|
|
{
|
|
|
|
pid_t me = getpid();
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* If would be an error if we are called with on a thread that does not
|
|
|
|
* hold the lock.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(me == g_ipv6_holder && g_ipv6_count > 0);
|
|
|
|
|
|
|
|
/* Release the count on the lock. If this is the last count, then release
|
|
|
|
* the lock.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (g_ipv6_count > 1)
|
|
|
|
{
|
|
|
|
/* Not the last count... just decrement the count and return success */
|
|
|
|
|
|
|
|
g_ipv6_count--;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This is the last count. Release the lock */
|
|
|
|
|
|
|
|
g_ipv6_holder = NO_HOLDER;
|
|
|
|
g_ipv6_count = 0;
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
ret = nxsem_post(&g_ipv6_exclsem);
|
2017-09-29 16:33:36 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nerr("ERROR: nxsem_post() failed: %d\n", ret);
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_closeroute_ipv4/net_closeroute_ipv6
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Close the IPv4/IPv6 routing table.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-09-29 16:33:36 +02:00
|
|
|
* filep - Detached file instance obtained by net_openroute_ipv{4|6}[_rdonly]
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success. A negated errno value is returned on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv4_FILEROUTE
|
|
|
|
int net_closeroute_ipv4(FAR struct file *filep)
|
|
|
|
{
|
2018-09-15 21:04:56 +02:00
|
|
|
return file_close(filep);
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_IPv6_FILEROUTE
|
|
|
|
int net_closeroute_ipv6(FAR struct file *filep)
|
|
|
|
{
|
2018-09-15 21:04:56 +02:00
|
|
|
return file_close(filep);
|
2017-09-29 16:33:36 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* CONFIG_ROUTE_IPv4_FILEROUTE || CONFIG_ROUTE_IPv6_FILEROUTE */
|