From 5741e70eb97931cc86da795761041e550bf96f7f Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 18 Jan 2015 15:56:14 -0600 Subject: [PATCH] apps/netutils/netlib: Add new library functions to manipulate IPv6 addresses --- netutils/netlib/netlib_getifstatus.c | 4 +- netutils/netlib/netlib_getipv4addr.c | 24 ++---- netutils/netlib/netlib_getipv6addr.c | 106 ++++++++++++++++++++++++ netutils/netlib/netlib_getmacaddr.c | 4 +- netutils/netlib/netlib_ipmsfilter.c | 4 +- netutils/netlib/netlib_setdripv4addr.c | 37 +++------ netutils/netlib/netlib_setdripv6addr.c | 106 ++++++++++++++++++++++++ netutils/netlib/netlib_setifstatus.c | 6 +- netutils/netlib/netlib_setipv4addr.c | 37 +++------ netutils/netlib/netlib_setipv4netmask.c | 38 ++++----- netutils/netlib/netlib_setipv6addr.c | 106 ++++++++++++++++++++++++ netutils/netlib/netlib_setipv6netmask.c | 105 +++++++++++++++++++++++ netutils/netlib/netlib_setmacaddr.c | 4 +- 13 files changed, 482 insertions(+), 99 deletions(-) create mode 100644 netutils/netlib/netlib_getipv6addr.c create mode 100644 netutils/netlib/netlib_setdripv6addr.c create mode 100644 netutils/netlib/netlib_setipv6addr.c create mode 100644 netutils/netlib/netlib_setipv6netmask.c diff --git a/netutils/netlib/netlib_getifstatus.c b/netutils/netlib/netlib_getifstatus.c index 8521b781b..0d718209d 100644 --- a/netutils/netlib/netlib_getifstatus.c +++ b/netutils/netlib/netlib_getifstatus.c @@ -54,7 +54,7 @@ #include /**************************************************************************** - * Global Functions + * Public Functions ****************************************************************************/ /**************************************************************************** @@ -68,7 +68,7 @@ * flags The interface flags returned by SIOCGIFFLAGS * * Return: - * 0 on sucess; -1 on failure + * 0 on success; -1 on failure * ****************************************************************************/ diff --git a/netutils/netlib/netlib_getipv4addr.c b/netutils/netlib/netlib_getipv4addr.c index bfdad5d98..f19f8b337 100644 --- a/netutils/netlib/netlib_getipv4addr.c +++ b/netutils/netlib/netlib_getipv4addr.c @@ -1,7 +1,7 @@ /**************************************************************************** * netutils/netlib/netlib_getipv4addr.c * - * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2011, 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -38,7 +38,7 @@ ****************************************************************************/ #include -#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 +#if defined(CONFIG_NET_IPv4) && CONFIG_NSOCKET_DESCRIPTORS > 0 #include #include @@ -57,14 +57,14 @@ ****************************************************************************/ /**************************************************************************** - * Global Functions + * Public Functions ****************************************************************************/ /**************************************************************************** * Name: netlib_get_ipv4addr * * Description: - * Get the network driver IP address + * Get the network driver IPv4 address * * Parameters: * ifname The name of the interface to use @@ -75,13 +75,10 @@ * ****************************************************************************/ -#ifdef CONFIG_NET_IPv6 -int netlib_get_ipv4addr(const char *ifname, struct in6_addr *addr) -#else -int netlib_get_ipv4addr(const char *ifname, struct in_addr *addr) -#endif +int netlib_get_ipv4addr(FAR const char *ifname, FAR struct in_addr *addr) { int ret = ERROR; + if (ifname && addr) { int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 0); @@ -92,15 +89,10 @@ int netlib_get_ipv4addr(const char *ifname, struct in_addr *addr) ret = ioctl(sockfd, SIOCGIFADDR, (unsigned long)&req); if (!ret) { -#ifdef CONFIG_NET_IPv6 - FAR struct sockaddr_in6 *req_addr; - req_addr = (FAR struct sockaddr_in6 *)&req.ifr_addr; - memcpy(addr, &req_addr->sin6_addr, sizeof(struct in6_addr)); -#else FAR struct sockaddr_in *req_addr; + req_addr = (FAR struct sockaddr_in*)&req.ifr_addr; memcpy(addr, &req_addr->sin_addr, sizeof(struct in_addr)); -#endif } close(sockfd); @@ -110,4 +102,4 @@ int netlib_get_ipv4addr(const char *ifname, struct in_addr *addr) return ret; } -#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */ +#endif /* CONFIG_NET_IPv4 && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/netutils/netlib/netlib_getipv6addr.c b/netutils/netlib/netlib_getipv6addr.c new file mode 100644 index 000000000..3cd811d07 --- /dev/null +++ b/netutils/netlib/netlib_getipv6addr.c @@ -0,0 +1,106 @@ +/**************************************************************************** + * netutils/netlib/netlib_getipv6addr.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 +#if defined(CONFIG_NET_IPv6) && CONFIG_NSOCKET_DESCRIPTORS > 0 + +#include +#include + +#include +#include +#include + +#include +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_get_ipv6addr + * + * Description: + * Get the network driver IPv6 address + * + * Parameters: + * ifname The name of the interface to use + * ipaddr The location to return the IP address + * + * Return: + * 0 on success; -1 on failure + * + ****************************************************************************/ + +int netlib_get_ipv6addr(FAR const char *ifname, FAR struct in6_addr *addr) +{ + int ret = ERROR; + + if (ifname && addr) + { + int sockfd = socket(PF_INET6, NETLIB_SOCK_IOCTL, 0); + if (sockfd >= 0) + { + struct lifreq req; + + strncpy(req.lifr_name, ifname, IFNAMSIZ); + ret = ioctl(sockfd, SIOCGLIFADDR, (unsigned long)&req); + if (!ret) + { + FAR struct sockaddr_in6 *req_addr; + + req_addr = (FAR struct sockaddr_in6 *)&req.lifr_addr; + memcpy(addr, &req_addr->sin6_addr, sizeof(struct in6_addr)); + } + + close(sockfd); + } + } + + return ret; +} + +#endif /* CONFIG_NET_IPv6 && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/netutils/netlib/netlib_getmacaddr.c b/netutils/netlib/netlib_getmacaddr.c index da0e64e6d..1a404dd39 100644 --- a/netutils/netlib/netlib_getmacaddr.c +++ b/netutils/netlib/netlib_getmacaddr.c @@ -53,7 +53,7 @@ #include /**************************************************************************** - * Global Functions + * Public Functions ****************************************************************************/ /**************************************************************************** @@ -67,7 +67,7 @@ * macaddr The location to return the MAC address * * Return: - * 0 on sucess; -1 on failure + * 0 on success; -1 on failure * ****************************************************************************/ diff --git a/netutils/netlib/netlib_ipmsfilter.c b/netutils/netlib/netlib_ipmsfilter.c index a5fc96398..47e2688ab 100644 --- a/netutils/netlib/netlib_ipmsfilter.c +++ b/netutils/netlib/netlib_ipmsfilter.c @@ -60,7 +60,7 @@ ****************************************************************************/ /**************************************************************************** - * Global Functions + * Public Functions ****************************************************************************/ /**************************************************************************** @@ -76,7 +76,7 @@ * MCAST_EXCLUDE: Remove multicast address * * Return: - * 0 on sucess; Negated errno on failure + * 0 on success; Negated errno on failure * ****************************************************************************/ diff --git a/netutils/netlib/netlib_setdripv4addr.c b/netutils/netlib/netlib_setdripv4addr.c index b6b54f6db..04f10ce30 100644 --- a/netutils/netlib/netlib_setdripv4addr.c +++ b/netutils/netlib/netlib_setdripv4addr.c @@ -1,7 +1,7 @@ /**************************************************************************** * netutils/netlib/netlib_setdripv4addr.c * - * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2011, 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -38,7 +38,7 @@ ****************************************************************************/ #include -#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 +#if defined(CONFIG_NET_IPv4) && CONFIG_NSOCKET_DESCRIPTORS > 0 #include #include @@ -53,59 +53,48 @@ #include /**************************************************************************** - * Global Functions + * Public Functions ****************************************************************************/ /**************************************************************************** * Name: netlib_set_dripv4addr * * Description: - * Set the default router IP address + * Set the default router IPv4 address * * Parameters: * ifname The name of the interface to use * ipaddr The address to set * * Return: - * 0 on sucess; -1 on failure + * 0 on success; -1 on failure * ****************************************************************************/ -#ifdef CONFIG_NET_IPv6 -int netlib_set_dripv4addr(const char *ifname, const struct in6_addr *addr) -#else -int netlib_set_dripv4addr(const char *ifname, const struct in_addr *addr) -#endif +int netlib_set_dripv4addr(FAR const char *ifname, + FAR const struct in_addr *addr) { int ret = ERROR; + if (ifname && addr) { int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 0); if (sockfd >= 0) { + FAR struct sockaddr_in *inaddr; struct ifreq req; -#ifdef CONFIG_NET_IPv6 - struct sockaddr_in6 *inaddr; -#else - struct sockaddr_in *inaddr; -#endif + /* Add the device name to the request */ strncpy(req.ifr_name, ifname, IFNAMSIZ); /* Add the INET address to the request */ -#ifdef CONFIG_NET_IPv6 - inaddr = (struct sockaddr_in6 *)&req.ifr_addr; - inaddr->sin_family = AF_INET6; - inaddr->sin_port = 0; - memcpy(&inaddr->sin6_addr, addr, sizeof(struct in6_addr)); -#else - inaddr = (struct sockaddr_in *)&req.ifr_addr; + inaddr = (FAR struct sockaddr_in *)&req.ifr_addr; inaddr->sin_family = AF_INET; inaddr->sin_port = 0; memcpy(&inaddr->sin_addr, addr, sizeof(struct in_addr)); -#endif + ret = ioctl(sockfd, SIOCSIFDSTADDR, (unsigned long)&req); close(sockfd); } @@ -114,4 +103,4 @@ int netlib_set_dripv4addr(const char *ifname, const struct in_addr *addr) return ret; } -#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */ +#endif /* CONFIG_NET_IPv4 && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/netutils/netlib/netlib_setdripv6addr.c b/netutils/netlib/netlib_setdripv6addr.c new file mode 100644 index 000000000..d339e6b87 --- /dev/null +++ b/netutils/netlib/netlib_setdripv6addr.c @@ -0,0 +1,106 @@ +/**************************************************************************** + * netutils/netlib/netlib_setdripv6addr.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 +#if defined(CONFIG_NET_IPv6) && CONFIG_NSOCKET_DESCRIPTORS > 0 + +#include +#include + +#include +#include +#include + +#include +#include + +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_set_dripv6addr + * + * Description: + * Set the default router IP address + * + * Parameters: + * ifname The name of the interface to use + * ipaddr The address to set + * + * Return: + * 0 on success; -1 on failure + * + ****************************************************************************/ + +int netlib_set_dripv6addr(FAR const char *ifname, + FAR const struct in6_addr *addr) +{ + int ret = ERROR; + + if (ifname && addr) + { + int sockfd = socket(PF_INET6, NETLIB_SOCK_IOCTL, 0); + if (sockfd >= 0) + { + FAR struct sockaddr_in6 *inaddr; + struct lifreq req; + + /* Add the device name to the request */ + + strncpy(req.lifr_name, ifname, IFNAMSIZ); + + /* Add the INET address to the request */ + + inaddr = (FAR struct sockaddr_in6 *)&req.lifr_addr; + inaddr->sin_family = AF_INET6; + inaddr->sin_port = 0; + memcpy(&inaddr->sin6_addr, addr, sizeof(struct in6_addr)); + + ret = ioctl(sockfd, SIOCSLIFDSTADDR, (unsigned long)((uintptr_t)&req)); + close(sockfd); + } + } + + return ret; +} + +#endif /* CONFIG_NET_IPv6 && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/netutils/netlib/netlib_setifstatus.c b/netutils/netlib/netlib_setifstatus.c index e20f2240b..f754c17be 100644 --- a/netutils/netlib/netlib_setifstatus.c +++ b/netutils/netlib/netlib_setifstatus.c @@ -53,7 +53,7 @@ #include /**************************************************************************** - * Global Functions + * Public Functions ****************************************************************************/ /**************************************************************************** @@ -66,7 +66,7 @@ * ifname The name of the interface to use * * Return: - * 0 on sucess; -1 on failure + * 0 on success; -1 on failure * ****************************************************************************/ @@ -109,7 +109,7 @@ int netlib_ifup(const char *ifname) * ifname The name of the interface to use * * Return: - * 0 on sucess; -1 on failure + * 0 on success; -1 on failure * ****************************************************************************/ diff --git a/netutils/netlib/netlib_setipv4addr.c b/netutils/netlib/netlib_setipv4addr.c index 6da53447c..b3d08408f 100644 --- a/netutils/netlib/netlib_setipv4addr.c +++ b/netutils/netlib/netlib_setipv4addr.c @@ -1,7 +1,7 @@ /**************************************************************************** * netutils/netlib/netlib_setipv4addr.c * - * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2011, 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -38,7 +38,7 @@ ****************************************************************************/ #include -#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 +#if defined(CONFIG_NET_IPv4) && CONFIG_NSOCKET_DESCRIPTORS > 0 #include #include @@ -53,64 +53,53 @@ #include /**************************************************************************** - * Global Functions + * Public Functions ****************************************************************************/ /**************************************************************************** * Name: netlib_set_ipv4addr * * Description: - * Set the network driver IP address + * Set the network driver IPv4 address * * Parameters: * ifname The name of the interface to use * ipaddr The address to set * * Return: - * 0 on sucess; -1 on failure + * 0 on success; -1 on failure * ****************************************************************************/ -#ifdef CONFIG_NET_IPv6 -int netlib_set_ipv4addr(const char *ifname, const struct in6_addr *addr) -#else -int netlib_set_ipv4addr(const char *ifname, const struct in_addr *addr) -#endif +int netlib_set_ipv4addr(FAR const char *ifname, FAR const struct in_addr *addr) { int ret = ERROR; + if (ifname && addr) { int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 0); if (sockfd >= 0) { + FAR struct sockaddr_in *inaddr; struct ifreq req; -#ifdef CONFIG_NET_IPv6 - struct sockaddr_in6 *inaddr; -#else - struct sockaddr_in *inaddr; -#endif + /* Add the device name to the request */ strncpy(req.ifr_name, ifname, IFNAMSIZ); /* Add the INET address to the request */ -#ifdef CONFIG_NET_IPv6 - inaddr = (struct sockaddr_in6 *)&req.ifr_addr; - inaddr->sin_family = AF_INET6; - inaddr->sin_port = 0; - memcpy(&inaddr->sin6_addr, addr, sizeof(struct in6_addr)); -#else - inaddr = (struct sockaddr_in *)&req.ifr_addr; + inaddr = (FAR struct sockaddr_in *)&req.ifr_addr; inaddr->sin_family = AF_INET; inaddr->sin_port = 0; memcpy(&inaddr->sin_addr, addr, sizeof(struct in_addr)); -#endif + ret = ioctl(sockfd, SIOCSIFADDR, (unsigned long)&req); close(sockfd); } } + return ret; } -#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */ +#endif /* CONFIG_NET_IPv4 && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/netutils/netlib/netlib_setipv4netmask.c b/netutils/netlib/netlib_setipv4netmask.c index 972c8e233..e01c677a8 100644 --- a/netutils/netlib/netlib_setipv4netmask.c +++ b/netutils/netlib/netlib_setipv4netmask.c @@ -1,7 +1,7 @@ /**************************************************************************** * netutils/netlib/netlib_setipv4netmask.c * - * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2011, 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -38,7 +38,7 @@ ****************************************************************************/ #include -#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 +#if defined(CONFIG_NET_IPv4) && CONFIG_NSOCKET_DESCRIPTORS > 0 #include #include @@ -52,64 +52,54 @@ #include /**************************************************************************** - * Global Functions + * Public Functions ****************************************************************************/ /**************************************************************************** * Name: netlib_set_ipv4netmask * * Description: - * Set the netmask + * Set the PIv4 netmask * * Parameters: * ifname The name of the interface to use * ipaddr The address to set * * Return: - * 0 on sucess; -1 on failure + * 0 on success; -1 on failure * ****************************************************************************/ -#ifdef CONFIG_NET_IPv6 -int netlib_set_ipv4netmask(const char *ifname, const struct in6_addr *addr) -#else -int netlib_set_ipv4netmask(const char *ifname, const struct in_addr *addr) -#endif +int netlib_set_ipv4netmask(FAR const char *ifname, + FAR const struct in_addr *addr) { int ret = ERROR; + if (ifname && addr) { int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 0); if (sockfd >= 0) { + FAR struct sockaddr_in *inaddr; struct ifreq req; -#ifdef CONFIG_NET_IPv6 - struct sockaddr_in6 *inaddr; -#else - struct sockaddr_in *inaddr; -#endif + /* Add the device name to the request */ strncpy(req.ifr_name, ifname, IFNAMSIZ); /* Add the INET address to the request */ -#ifdef CONFIG_NET_IPv6 - inaddr = (struct sockaddr_in6 *)&req.ifr_addr; - inaddr->sin_family = AF_INET6; - inaddr->sin_port = 0; - memcpy(&inaddr->sin6_addr, addr, sizeof(struct in6_addr)); -#else - inaddr = (struct sockaddr_in *)&req.ifr_addr; + inaddr = (FAR struct sockaddr_in *)&req.ifr_addr; inaddr->sin_family = AF_INET; inaddr->sin_port = 0; memcpy(&inaddr->sin_addr, addr, sizeof(struct in_addr)); -#endif + ret = ioctl(sockfd, SIOCSIFNETMASK, (unsigned long)&req); close(sockfd); } } + return ret; } -#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */ +#endif /* CONFIG_NET_IPv4 && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/netutils/netlib/netlib_setipv6addr.c b/netutils/netlib/netlib_setipv6addr.c new file mode 100644 index 000000000..8a5d8be80 --- /dev/null +++ b/netutils/netlib/netlib_setipv6addr.c @@ -0,0 +1,106 @@ +/**************************************************************************** + * netutils/netlib/netlib_setipv6addr.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 +#if defined(CONFIG_NET_IPv6) && CONFIG_NSOCKET_DESCRIPTORS > 0 + +#include +#include + +#include +#include +#include + +#include +#include + +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_set_ipv6addr + * + * Description: + * Set the network driver IPv6 address + * + * Parameters: + * ifname The name of the interface to use + * ipaddr The address to set + * + * Return: + * 0 on success; -1 on failure + * + ****************************************************************************/ + +int netlib_set_ipv6addr(FAR const char *ifname, + FAR const struct in6_addr *addr) +{ + int ret = ERROR; + + if (ifname && addr) + { + int sockfd = socket(PF_INET6, NETLIB_SOCK_IOCTL, 0); + if (sockfd >= 0) + { + FAR struct sockaddr_in6 *inaddr; + struct lifreq req; + + /* Add the device name to the request */ + + strncpy(req.lifr_name, ifname, IFNAMSIZ); + + /* Add the INET address to the request */ + + inaddr = (FAR struct sockaddr_in6 *)&req.lifr_addr; + inaddr->sin_family = AF_INET6; + inaddr->sin_port = 0; + memcpy(&inaddr->sin6_addr, addr, sizeof(struct in6_addr)); + + ret = ioctl(sockfd, SIOCSLIFADDR, ((unsigned long)(uintptr_t)&req)); + close(sockfd); + } + } + + return ret; +} + +#endif /* CONFIG_NET_IPv6 && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/netutils/netlib/netlib_setipv6netmask.c b/netutils/netlib/netlib_setipv6netmask.c new file mode 100644 index 000000000..f092790a2 --- /dev/null +++ b/netutils/netlib/netlib_setipv6netmask.c @@ -0,0 +1,105 @@ +/**************************************************************************** + * netutils/netlib/netlib_setipv6netmask.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 +#if defined(CONFIG_NET_IPv6) && CONFIG_NSOCKET_DESCRIPTORS > 0 + +#include +#include +#include +#include +#include + +#include +#include + +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_set_ipv6netmask + * + * Description: + * Set the netmask + * + * Parameters: + * ifname The name of the interface to use + * ipaddr The address to set + * + * Return: + * 0 on success; -1 on failure + * + ****************************************************************************/ + +int netlib_set_ipv6netmask(FAR const char *ifname, + FAR const struct in6_addr *addr) +{ + int ret = ERROR; + + if (ifname && addr) + { + int sockfd = socket(PF_INET6, NETLIB_SOCK_IOCTL, 0); + if (sockfd >= 0) + { + FAR struct sockaddr_in6 *inaddr; + struct lifreq req; + + /* Add the device name to the request */ + + strncpy(req.lifr_name, ifname, IFNAMSIZ); + + /* Add the INET address to the request */ + + inaddr = (FAR struct sockaddr_in6 *)&req.lifr_addr; + inaddr->sin_family = AF_INET6; + inaddr->sin_port = 0; + memcpy(&inaddr->sin6_addr, addr, sizeof(struct in6_addr)); + + ret = ioctl(sockfd, SIOCSLIFNETMASK, (unsigned long)((uintptr_t)&req)); + close(sockfd); + } + } + + return ret; +} + +#endif /* CONFIG_NET_IPv6 && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/netutils/netlib/netlib_setmacaddr.c b/netutils/netlib/netlib_setmacaddr.c index 47fb463a3..733690a18 100644 --- a/netutils/netlib/netlib_setmacaddr.c +++ b/netutils/netlib/netlib_setmacaddr.c @@ -63,7 +63,7 @@ #endif /**************************************************************************** - * Global Functions + * Public Functions ****************************************************************************/ /**************************************************************************** @@ -77,7 +77,7 @@ * macaddr MAC address to set, size must be IFHWADDRLEN * * Return: - * 0 on sucess; -1 on failure + * 0 on success; -1 on failure * ****************************************************************************/