NSH library: Update addroute and delroute command. The would support either IPv4 or IPv6, but not both.

This commit is contained in:
Gregory Nutt 2017-08-10 09:15:44 -06:00
parent 4ee084ab24
commit f58768db3d

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* apps/nshlib/nsh_routecmds.c * apps/nshlib/nsh_routecmds.c
* *
* Copyright (C) 2013 Gregory Nutt. All rights reserved. * Copyright (C) 2013, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -53,30 +53,6 @@
#if defined(CONFIG_NET) && defined(CONFIG_NET_ROUTE) #if defined(CONFIG_NET) && defined(CONFIG_NET_ROUTE)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
@ -91,17 +67,47 @@
#ifndef CONFIG_NSH_DISABLE_ADDROUTE #ifndef CONFIG_NSH_DISABLE_ADDROUTE
int cmd_addroute(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) int cmd_addroute(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{ {
#ifdef CONFIG_NET_IPv6 union
struct sockaddr_in6 target; {
struct sockaddr_in6 netmask; #ifdef CONFIG_NET_IPv4
struct sockaddr_in6 router; struct sockaddr_in ipv4;
struct in6_addr inaddr;
#else
struct sockaddr_in target;
struct sockaddr_in netmask;
struct sockaddr_in router;
struct in_addr inaddr;
#endif #endif
#ifdef CONFIG_NET_IPv6
struct sockaddr_in6 ipv6;
#endif
} target;
union
{
#ifdef CONFIG_NET_IPv4
struct sockaddr_in ipv4;
#endif
#ifdef CONFIG_NET_IPv6
struct sockaddr_in6 ipv6;
#endif
} netmask;
union
{
#ifdef CONFIG_NET_IPv4
struct sockaddr_in ipv4;
#endif
#ifdef CONFIG_NET_IPv6
struct sockaddr_in6 ipv6;
#endif
} router;
union
{
#ifdef CONFIG_NET_IPv4
struct in_addr ipv4;
#endif
#ifdef CONFIG_NET_IPv6
struct in6_addr ipv6;
#endif
} inaddr;
sa_family_t family;
int sockfd; int sockfd;
int ret; int ret;
@ -109,85 +115,127 @@ int cmd_addroute(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
* we don't have to look at the argument list. * we don't have to look at the argument list.
*/ */
/* Convert the target IP address string into its binary form */
#ifdef CONFIG_NET_IPv4
family = PF_INET;
ret = inet_pton(AF_INET, argv[1], &inaddr.ipv4);
if (ret != 1)
#endif
{
#ifdef CONFIG_NET_IPv6
family = PF_INET6;
ret = inet_pton(AF_INET6, argv[1], &inaddr.ipv6);
if (ret != 1)
#endif
{
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
goto errout;
}
}
/* We need to have a socket (any socket) in order to perform the ioctl */ /* We need to have a socket (any socket) in order to perform the ioctl */
sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 0); sockfd = socket(family, NETLIB_SOCK_IOCTL, 0);
if (sockfd < 0) if (sockfd < 0)
{ {
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "socket", NSH_ERRNO); nsh_output(vtbl, g_fmtcmdfailed, argv[0], "socket", NSH_ERRNO);
return ERROR;
}
/* Convert the target IP address string into its binary form */
#ifdef CONFIG_NET_IPv6
ret = inet_pton(AF_INET6, argv[1], &inaddr);
#else
ret = inet_pton(AF_INET, argv[1], &inaddr);
#endif
if (ret != 1)
{
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
goto errout; goto errout;
} }
/* Format the target sockaddr instance */ /* Format the target sockaddr instance */
memset(&target, 0, sizeof(target)); memset(&target, 0, sizeof(target));
#ifdef CONFIG_NET_IPv4
#ifdef CONFIG_NET_IPv6 #ifdef CONFIG_NET_IPv6
target.sin6_family = AF_INET6; if (family == PF_INET)
memcpy(&target.sin6_addr, &inaddr, sizeof(struct in6_addr)); #endif
#else {
target.sin_family = AF_INET; target.ipv4.sin_family = AF_INET;
target.sin_addr = inaddr; target.ipv4.sin_addr = inaddr.ipv4;
}
#endif
#ifdef CONFIG_NET_IPv6
#ifdef CONFIG_NET_IPv4
else
#endif
{
target.ipv6.sin6_family = AF_INET6;
memcpy(&target.ipv6.sin6_addr, &inaddr.ipv6,
sizeof(struct in6_addr));
}
#endif #endif
/* Convert the netmask IP address string into its binary form */ /* Convert the netmask IP address string into its binary form */
#ifdef CONFIG_NET_IPv6 ret = inet_pton(family, argv[2], &inaddr);
ret = inet_pton(AF_INET6, argv[2], &inaddr);
#else
ret = inet_pton(AF_INET, argv[2], &inaddr);
#endif
if (ret != 1) if (ret != 1)
{ {
nsh_output(vtbl, g_fmtarginvalid, argv[0]); nsh_output(vtbl, g_fmtarginvalid, argv[0]);
goto errout; goto errout_with_sockfd;
} }
/* Format the netmask sockaddr instance */ /* Format the netmask sockaddr instance */
memset(&netmask, 0, sizeof(netmask)); memset(&netmask, 0, sizeof(netmask));
#ifdef CONFIG_NET_IPv4
#ifdef CONFIG_NET_IPv6 #ifdef CONFIG_NET_IPv6
netmask.sin6_family = AF_INET6; if (family == PF_INET)
memcpy(&netmask.sin6_addr, &inaddr, sizeof(struct in6_addr)); #endif
#else {
netmask.sin_family = AF_INET; netmask.ipv4.sin_family = AF_INET;
netmask.sin_addr = inaddr; netmask.ipv4.sin_addr = inaddr.ipv4;
}
#endif
#ifdef CONFIG_NET_IPv6
#ifdef CONFIG_NET_IPv4
else
#endif
{
netmask.ipv6.sin6_family = AF_INET6;
memcpy(&netmask.ipv6.sin6_addr, &inaddr.ipv6,
sizeof(struct in6_addr));
}
#endif #endif
/* Convert the router IP address string into its binary form */ /* Convert the router IP address string into its binary form */
#ifdef CONFIG_NET_IPv6 ret = inet_pton(family, argv[3], &inaddr);
ret = inet_pton(AF_INET6, argv[3], &inaddr);
#else
ret = inet_pton(AF_INET, argv[3], &inaddr);
#endif
if (ret != 1) if (ret != 1)
{ {
nsh_output(vtbl, g_fmtarginvalid, argv[0]); nsh_output(vtbl, g_fmtarginvalid, argv[0]);
goto errout; goto errout_with_sockfd;
} }
/* Format the router sockaddr instance */ /* Format the router sockaddr instance */
memset(&router, 0, sizeof(router)); memset(&router, 0, sizeof(router));
#ifdef CONFIG_NET_IPv4
#ifdef CONFIG_NET_IPv6 #ifdef CONFIG_NET_IPv6
router.sin6_family = AF_INET6; if (family == PF_INET)
memcpy(&router.sin6_addr, &inaddr, sizeof(struct in6_addr)); #endif
#else {
router.sin_family = AF_INET; router.ipv4.sin_family = AF_INET;
router.sin_addr = inaddr; router.ipv4.sin_addr = inaddr.ipv4;
}
#endif
#ifdef CONFIG_NET_IPv6
#ifdef CONFIG_NET_IPv4
else
#endif
{
router.ipv6.sin6_family = AF_INET6;
memcpy(&router.ipv6.sin6_addr, &inaddr.ipv6,
sizeof(struct in6_addr));
}
#endif #endif
/* Then add the route */ /* Then add the route */
@ -199,14 +247,15 @@ int cmd_addroute(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
if (ret < 0) if (ret < 0)
{ {
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "addroute", NSH_ERRNO); nsh_output(vtbl, g_fmtcmdfailed, argv[0], "addroute", NSH_ERRNO);
goto errout; goto errout_with_sockfd;
} }
close(sockfd); close(sockfd);
return OK; return OK;
errout: errout_with_sockfd:
close(sockfd); close(sockfd);
errout:
return ERROR; return ERROR;
} }
#endif /* !CONFIG_NSH_DISABLE_ADDROUTE */ #endif /* !CONFIG_NSH_DISABLE_ADDROUTE */
@ -221,15 +270,37 @@ errout:
#ifndef CONFIG_NSH_DISABLE_DELROUTE #ifndef CONFIG_NSH_DISABLE_DELROUTE
int cmd_delroute(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) int cmd_delroute(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{ {
#ifdef CONFIG_NET_IPv6 union
struct sockaddr_in6 target; {
struct sockaddr_in6 netmask; #ifdef CONFIG_NET_IPv4
struct in6_addr inaddr; struct sockaddr_in ipv4;
#else
struct sockaddr_in target;
struct sockaddr_in netmask;
struct in_addr inaddr;
#endif #endif
#ifdef CONFIG_NET_IPv6
struct sockaddr_in6 ipv6;
#endif
} target;
union
{
#ifdef CONFIG_NET_IPv4
struct sockaddr_in ipv4;
#endif
#ifdef CONFIG_NET_IPv6
struct sockaddr_in6 ipv6;
#endif
} netmask;
union
{
#ifdef CONFIG_NET_IPv4
struct in_addr ipv4;
#endif
#ifdef CONFIG_NET_IPv6
struct in6_addr ipv6;
#endif
} inaddr;
sa_family_t family;
int sockfd; int sockfd;
int ret; int ret;
@ -237,61 +308,93 @@ int cmd_delroute(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
* we don't have to look at the argument list. * we don't have to look at the argument list.
*/ */
/* Convert the target IP address string into its binary form */
#ifdef CONFIG_NET_IPv4
family = PF_INET;
ret = inet_pton(AF_INET, argv[1], &inaddr.ipv4);
if (ret != 1)
#endif
{
#ifdef CONFIG_NET_IPv6
family = PF_INET6;
ret = inet_pton(AF_INET6, argv[1], &inaddr.ipv6);
if (ret != 1)
#endif
{
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
goto errout;
}
}
/* We need to have a socket (any socket) in order to perform the ioctl */ /* We need to have a socket (any socket) in order to perform the ioctl */
sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 0); sockfd = socket(family, NETLIB_SOCK_IOCTL, 0);
if (sockfd < 0) if (sockfd < 0)
{ {
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "socket", NSH_ERRNO); nsh_output(vtbl, g_fmtcmdfailed, argv[0], "socket", NSH_ERRNO);
return ERROR;
}
/* Convert the target IP address string into its binary form */
#ifdef CONFIG_NET_IPv6
ret = inet_pton(AF_INET6, argv[1], &inaddr);
#else
ret = inet_pton(AF_INET, argv[1], &inaddr);
#endif
if (ret != 1)
{
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
goto errout; goto errout;
} }
/* Format the target sockaddr instance */ /* Format the target sockaddr instance */
memset(&target, 0, sizeof(target)); memset(&target, 0, sizeof(target));
#ifdef CONFIG_NET_IPv4
#ifdef CONFIG_NET_IPv6 #ifdef CONFIG_NET_IPv6
target.sin6_family = AF_INET6; if (family == PF_INET)
memcpy(&target.sin6_addr, &inaddr, sizeof(struct in6_addr)); #endif
#else {
target.sin_family = AF_INET; target.ipv4.sin_family = AF_INET;
target.sin_addr = inaddr; target.ipv4.sin_addr = inaddr.ipv4;
}
#endif
#ifdef CONFIG_NET_IPv6
#ifdef CONFIG_NET_IPv4
else
#endif
{
target.ipv6.sin6_family = AF_INET6;
memcpy(&target.ipv6.sin6_addr, &inaddr.ipv6,
sizeof(struct in6_addr));
}
#endif #endif
/* Convert the netmask IP address string into its binary form */ /* Convert the netmask IP address string into its binary form */
#ifdef CONFIG_NET_IPv6 ret = inet_pton(family, argv[2], &inaddr);
ret = inet_pton(AF_INET6, argv[2], &inaddr);
#else
ret = inet_pton(AF_INET, argv[2], &inaddr);
#endif
if (ret != 1) if (ret != 1)
{ {
nsh_output(vtbl, g_fmtarginvalid, argv[0]); nsh_output(vtbl, g_fmtarginvalid, argv[0]);
goto errout; goto errout_with_sockfd;
} }
/* Format the netmask sockaddr instance */ /* Format the netmask sockaddr instance */
memset(&netmask, 0, sizeof(netmask)); memset(&netmask, 0, sizeof(netmask));
#ifdef CONFIG_NET_IPv4
#ifdef CONFIG_NET_IPv6 #ifdef CONFIG_NET_IPv6
netmask.sin6_family = AF_INET6; if (family == PF_INET)
memcpy(&netmask.sin6_addr, &inaddr, sizeof(struct in6_addr)); #endif
#else {
netmask.sin_family = AF_INET; netmask.ipv4.sin_family = AF_INET;
netmask.sin_addr = inaddr; netmask.ipv4.sin_addr = inaddr.ipv4;
}
#endif
#ifdef CONFIG_NET_IPv6
#ifdef CONFIG_NET_IPv4
else
#endif
{
netmask.ipv6.sin6_family = AF_INET6;
memcpy(&netmask.ipv6.sin6_addr, &inaddr.ipv6,
sizeof(struct in6_addr));
}
#endif #endif
/* Then delete the route */ /* Then delete the route */
@ -302,14 +405,15 @@ int cmd_delroute(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
if (ret < 0) if (ret < 0)
{ {
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "addroute", NSH_ERRNO); nsh_output(vtbl, g_fmtcmdfailed, argv[0], "addroute", NSH_ERRNO);
goto errout; goto errout_with_sockfd;
} }
close(sockfd); close(sockfd);
return OK; return OK;
errout: errout_with_sockfd:
close(sockfd); close(sockfd);
errout:
return ERROR; return ERROR;
} }
#endif /* !CONFIG_NSH_DISABLE_DELROUTE */ #endif /* !CONFIG_NSH_DISABLE_DELROUTE */