2017-09-20 18:35:56 +02:00
|
|
|
/****************************************************************************
|
2021-06-11 12:06:42 +02:00
|
|
|
* apps/netutils/netlib/netlib_ipv6route.c
|
2017-09-20 18:35:56 +02:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2017-09-20 18:35:56 +02:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-09-20 18:35:56 +02:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2017-09-20 18:35:56 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2022-11-17 21:34:08 +01:00
|
|
|
#include <ctype.h>
|
2017-09-20 18:35:56 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include "netutils/netlib.h"
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_IPv6) && defined(HAVE_ROUTE_PROCFS)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Determines the size of an intermediate buffer that must be large enough
|
|
|
|
* to handle the longest line generated by this logic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define PROCFS_LINELEN 58
|
|
|
|
|
|
|
|
/* The form of the entry from the routing table file:
|
|
|
|
*
|
|
|
|
* 11111111112222222222333333333344444444445555
|
|
|
|
* 12345678901234567890123456789012345678901234567890123
|
|
|
|
* nnnn. target: xxxx:xxxx:xxxx:xxxxxxxx:xxxx:xxxx:xxxx
|
|
|
|
* netmask: xxxx:xxxx:xxxx:xxxxxxxx:xxxx:xxxx:xxxx
|
|
|
|
* router: xxxx:xxxx:xxxx:xxxxxxxx:xxxx:xxxx:xxxx
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ADDR_OFFSET 15
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: set_nul_terminator
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Make sure that the string is NUL terminated.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void set_nul_terminator(FAR char *str)
|
|
|
|
{
|
2021-06-11 12:06:42 +02:00
|
|
|
/* The first non-hex character that is not ':' terminates the address */
|
2017-09-20 18:59:00 +02:00
|
|
|
|
2021-06-11 12:06:42 +02:00
|
|
|
while ((*str >= '0' && *str <= '9') ||
|
|
|
|
(*str >= 'a' && *str <= 'f') ||
|
|
|
|
(*str >= 'A' && *str <= 'F') ||
|
|
|
|
*str == ':')
|
|
|
|
{
|
2017-09-20 18:35:56 +02:00
|
|
|
str++;
|
2021-06-11 12:06:42 +02:00
|
|
|
}
|
2017-09-20 18:35:56 +02:00
|
|
|
|
2021-06-11 12:06:42 +02:00
|
|
|
*str = '\0';
|
2017-09-20 18:35:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlib_read_ipv6route
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the next entry from the IPv6 routing table.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fd - The open file descriptor to the procfs' IPv6 routing table.
|
|
|
|
* route - The location to return that the next routing table entry.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* sizeof(struct netlib_ipv6_route_s) is returned on success. Zero is
|
2020-02-23 05:51:44 +01:00
|
|
|
* returned if the end of file is encountered. A negated errno value is
|
2017-09-20 18:35:56 +02:00
|
|
|
* returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t netlib_read_ipv6route(FILE *stream,
|
|
|
|
FAR struct netlib_ipv6_route_s *route)
|
|
|
|
{
|
|
|
|
char line[PROCFS_LINELEN];
|
|
|
|
FAR char *addr;
|
|
|
|
int ret;
|
2022-11-17 21:34:08 +01:00
|
|
|
int idx = 0;
|
2017-09-20 18:35:56 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(stream != NULL && route != NULL);
|
|
|
|
|
|
|
|
/* The form of the entries read from the routing table:
|
|
|
|
*
|
|
|
|
* 1111111111222222222233333333334444444444555
|
|
|
|
* 1234567890123456789012345678901234567890123456789012
|
|
|
|
* nnnn. target: xxxx:xxxx:xxxx:xxxxxxxx:xxxx:xxxx:xxxx
|
|
|
|
* netmask: xxxx:xxxx:xxxx:xxxxxxxx:xxxx:xxxx:xxxx
|
|
|
|
* router: xxxx:xxxx:xxxx:xxxxxxxx:xxxx:xxxx:xxxx
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Read the first line from the file */
|
|
|
|
|
|
|
|
if (fgets(line, PROCFS_LINELEN, stream) == NULL)
|
|
|
|
{
|
|
|
|
/* End of file (or possibly a read error?) */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-17 21:34:08 +01:00
|
|
|
/* First non-space char of 1st line should be a number index */
|
|
|
|
|
|
|
|
while (isspace(line[idx]))
|
|
|
|
{
|
|
|
|
idx++;
|
|
|
|
}
|
2017-09-20 18:35:56 +02:00
|
|
|
|
2022-11-17 21:34:08 +01:00
|
|
|
if (line[idx] < '0' || line[idx] > '9')
|
2017-09-20 18:35:56 +02:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make certain that there is a NUL terminator */
|
|
|
|
|
|
|
|
line[PROCFS_LINELEN - 1] = '\0';
|
|
|
|
|
|
|
|
/* Convert the prefix address to binary */
|
|
|
|
|
|
|
|
addr = &line[ADDR_OFFSET];
|
|
|
|
set_nul_terminator(addr);
|
|
|
|
|
|
|
|
ret = inet_pton(AF_INET6, addr, &route->prefix);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the second line from the file */
|
|
|
|
|
|
|
|
if (fgets(line, PROCFS_LINELEN, stream) == NULL)
|
|
|
|
{
|
|
|
|
/* End of file (or possibly a read error?) */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The second line of the group should not include an index */
|
|
|
|
|
|
|
|
if (line[0] != ' ')
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make certain that there is a NUL terminator */
|
|
|
|
|
|
|
|
line[PROCFS_LINELEN - 1] = '\0';
|
|
|
|
|
|
|
|
/* Convert the prefix address to binary */
|
|
|
|
|
|
|
|
addr = &line[ADDR_OFFSET];
|
|
|
|
set_nul_terminator(addr);
|
|
|
|
|
|
|
|
ret = inet_pton(AF_INET6, addr, &route->netmask);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the third line from the file */
|
|
|
|
|
|
|
|
if (fgets(line, PROCFS_LINELEN, stream) == NULL)
|
|
|
|
{
|
|
|
|
/* End of file (or possibly a read error?) */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The third line of the group should not include an index */
|
|
|
|
|
|
|
|
if (line[0] != ' ')
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make certain that there is a NUL terminator */
|
|
|
|
|
|
|
|
line[PROCFS_LINELEN - 1] = '\0';
|
|
|
|
|
|
|
|
/* Convert the prefix address to binary */
|
|
|
|
|
|
|
|
addr = &line[ADDR_OFFSET];
|
|
|
|
set_nul_terminator(addr);
|
|
|
|
|
|
|
|
ret = inet_pton(AF_INET6, addr, &route->router);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sizeof(struct netlib_ipv6_route_s);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_IPv6 && HAVE_ROUTE_PROCFS */
|