From a7e644a852025eeb8af8648f73aac7c9ffc315bd Mon Sep 17 00:00:00 2001 From: patacongo Date: Fri, 26 Oct 2007 21:21:34 +0000 Subject: Correct some issues with IP/MAC address handling git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@355 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/netutils/dhcpc/dhcpc.c | 3 +- nuttx/netutils/uiplib/Make.defs | 3 +- nuttx/netutils/uiplib/uip-gethostaddr.c | 17 ++++- nuttx/netutils/uiplib/uip-getmacaddr.c | 102 +++++++++++++++++++++++++++++ nuttx/netutils/uiplib/uip-setdraddr.c | 32 ++++++++- nuttx/netutils/uiplib/uip-sethostaddr.c | 32 ++++++++- nuttx/netutils/uiplib/uip-setmacaddr.c | 112 ++++++++++++++++++++++++++++++++ nuttx/netutils/uiplib/uip-setnetmask.c | 32 ++++++++- nuttx/netutils/uiplib/uiplib.c | 3 +- nuttx/netutils/uiplib/uiplib.h | 72 -------------------- nuttx/netutils/webclient/webclient.c | 2 +- 11 files changed, 321 insertions(+), 89 deletions(-) create mode 100644 nuttx/netutils/uiplib/uip-getmacaddr.c create mode 100644 nuttx/netutils/uiplib/uip-setmacaddr.c delete mode 100644 nuttx/netutils/uiplib/uiplib.h (limited to 'nuttx/netutils') diff --git a/nuttx/netutils/dhcpc/dhcpc.c b/nuttx/netutils/dhcpc/dhcpc.c index 23a107fc3..cff741794 100644 --- a/nuttx/netutils/dhcpc/dhcpc.c +++ b/nuttx/netutils/dhcpc/dhcpc.c @@ -54,6 +54,7 @@ #include #include +#include /**************************************************************************** * Definitions @@ -192,7 +193,7 @@ static void create_msg(struct dhcpc_state_internal *pdhcpc, struct dhcp_msg *pms pmsg->secs = 0; pmsg->flags = HTONS(BOOTP_BROADCAST); /* Broadcast bit. */ - uip_gethostaddr( "eth0", &addr); + uip_gethostaddr("eth0", &addr); memcpy(&pmsg->ciaddr, &addr.s_addr, sizeof(pmsg->ciaddr)); memset(pmsg->yiaddr, 0, sizeof(pmsg->yiaddr)); memset(pmsg->siaddr, 0, sizeof(pmsg->siaddr)); diff --git a/nuttx/netutils/uiplib/Make.defs b/nuttx/netutils/uiplib/Make.defs index 246f022b5..d8b8ac600 100644 --- a/nuttx/netutils/uiplib/Make.defs +++ b/nuttx/netutils/uiplib/Make.defs @@ -34,4 +34,5 @@ ############################################################################ UIPLIB_ASRCS = -UIPLIB_CSRCS = uiplib.c uip-sethostaddr.c uip-gethostaddr.c uip-setdraddr.c uip-setnetmask.c +UIPLIB_CSRCS = uiplib.c uip-setmacaddr.c uip-getmacaddr.c uip-sethostaddr.c \ + uip-gethostaddr.c uip-setdraddr.c uip-setnetmask.c diff --git a/nuttx/netutils/uiplib/uip-gethostaddr.c b/nuttx/netutils/uiplib/uip-gethostaddr.c index e99aa580a..64c5bb2ff 100644 --- a/nuttx/netutils/uiplib/uip-gethostaddr.c +++ b/nuttx/netutils/uiplib/uip-gethostaddr.c @@ -47,19 +47,25 @@ #include #include +#include + +/**************************************************************************** + * Definitions + ****************************************************************************/ + /**************************************************************************** * Global Functions ****************************************************************************/ /**************************************************************************** - * Name: uip_sethostaddr + * Name: uip_gethostaddr * * Description: * Get the network driver IP address * * Parameters: * ifname The name of the interface to use - * ipaddr The address to set + * ipaddr The location to return the IP address * * Return: * 0 on sucess; -1 on failure @@ -83,7 +89,12 @@ int uip_gethostaddr(const char *ifname, struct in_addr *addr) ret = ioctl(sockfd, SIOCSIFADDR, (unsigned long)&req); if (!ret) { - memcpy(addr, &req.ifr_addr, sizeof(addr)); +#ifdef CONFIG_NET_IPv6 +#error "req.ifr_addr.s_addr not big enough for IPv6 address" + memcpy(addr, &req.ifr_addr, sizeof(struct in6_addr)); +#else + memcpy(addr, &req.ifr_addr, sizeof(struct in_addr)); +#endif } } } diff --git a/nuttx/netutils/uiplib/uip-getmacaddr.c b/nuttx/netutils/uiplib/uip-getmacaddr.c new file mode 100644 index 000000000..a8f936113 --- /dev/null +++ b/nuttx/netutils/uiplib/uip-getmacaddr.c @@ -0,0 +1,102 @@ +/**************************************************************************** + * netutils/uiplib/uip-getmacaddr.c + * + * Copyright (C) 2007 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) && CONFIG_NSOCKET_DESCRIPTORS > 0 + +#include +#include +#include +#include +#include +#include + +#include + +/**************************************************************************** + * Global Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: uip_getmacaddr + * + * Description: + * Get the network driver IP address + * + * Parameters: + * ifname The name of the interface to use + * macaddr The location to return the MAC address + * + * Return: + * 0 on sucess; -1 on failure + * + ****************************************************************************/ + +int uip_getmacaddr(const char *ifname, uint8 *macaddr) +{ + int ret = ERROR; + if (ifname && macaddr) + { + /* Get a socket (only so that we get access to the INET subsystem) */ + + int sockfd = socket(PF_INET, SOCK_DGRAM, 0); + if (sockfd >= 0) + { + struct ifreq req; + memset (&req, 0, sizeof(struct ifreq)); + + /* Put the driver name into the request */ + + strncpy(req.ifr_name, ifname, IFNAMSIZ); + + /* Perform the ioctl to get the MAC address */ + + ret = ioctl(sockfd, SIOCGIFHWADDR, (unsigned long)&req); + if (!ret) + { + /* Return the MAC address */ + + memcpy(macaddr, &req.ifr_hwaddr.sa_data, IFHWADDRLEN); + } + } + } + return ret; +} + +#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/nuttx/netutils/uiplib/uip-setdraddr.c b/nuttx/netutils/uiplib/uip-setdraddr.c index 29ae97efd..3b15a9ca9 100644 --- a/nuttx/netutils/uiplib/uip-setdraddr.c +++ b/nuttx/netutils/uiplib/uip-setdraddr.c @@ -43,10 +43,13 @@ #include #include #include +#include #include #include #include +#include + /**************************************************************************** * Global Functions ****************************************************************************/ @@ -72,18 +75,41 @@ int uip_setdraddr(const char *ifname, const struct in6_addr *addr) int uip_setdraddr(const char *ifname, const struct in_addr *addr) #endif { + int ret = ERROR; if (ifname && addr) { int sockfd = socket(PF_INET, SOCK_DGRAM, 0); if (sockfd >= 0) { 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); - memcpy(&req.ifr_addr, addr, sizeof(addr)); - return ioctl(sockfd, SIOCSIFDSTADDR, (unsigned long)&req); + + /* Add the INET address to the request */ + +#ifdef CONFIG_NET_IPv6 +#error "req.ifr_addr.s_addr not big enough for IPv6 address" + 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->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); } } - return ERROR; + return ret; } #endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/nuttx/netutils/uiplib/uip-sethostaddr.c b/nuttx/netutils/uiplib/uip-sethostaddr.c index 13deaa0d1..4a0b979bc 100644 --- a/nuttx/netutils/uiplib/uip-sethostaddr.c +++ b/nuttx/netutils/uiplib/uip-sethostaddr.c @@ -43,10 +43,13 @@ #include #include #include +#include #include #include #include +#include + /**************************************************************************** * Global Functions ****************************************************************************/ @@ -72,18 +75,41 @@ int uip_sethostaddr(const char *ifname, const struct in6_addr *addr) int uip_sethostaddr(const char *ifname, const struct in_addr *addr) #endif { + int ret = ERROR; if (ifname && addr) { int sockfd = socket(PF_INET, SOCK_DGRAM, 0); if (sockfd >= 0) { 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); - memcpy(&req.ifr_addr, addr, sizeof(addr)); - return ioctl(sockfd, SIOCSIFADDR, (unsigned long)&req); + + /* Add the INET address to the request */ + +#ifdef CONFIG_NET_IPv6 +#error "req.ifr_addr.s_addr not big enough for IPv6 address" + 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->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 ERROR; + return ret; } #endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/nuttx/netutils/uiplib/uip-setmacaddr.c b/nuttx/netutils/uiplib/uip-setmacaddr.c new file mode 100644 index 000000000..b02e204ce --- /dev/null +++ b/nuttx/netutils/uiplib/uip-setmacaddr.c @@ -0,0 +1,112 @@ +/**************************************************************************** + * netutils/uiplib/uip-setmacaddr.c + * + * Copyright (C) 2007 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) && CONFIG_NSOCKET_DESCRIPTORS > 0 + +#include +#include +#include +#include +#include +#include +#include + +#include + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +#ifdef CONFIG_NET_IPv6 +# define AF_INETX AF_INET6 +#else +# define AF_INETX AF_INET +#endif + +/**************************************************************************** + * Global Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: uip_setmacaddr + * + * Description: + * Set the network driver MAC address + * + * Parameters: + * ifname The name of the interface to use + * macaddr MAC address to set, size must be IFHWADDRLEN + * + * Return: + * 0 on sucess; -1 on failure + * + ****************************************************************************/ + +int uip_setmacaddr(const char *ifname, const uint8 *macaddr) +{ + int ret = ERROR; + if (ifname && macaddr) + { + /* Get a socket (only so that we get access to the INET subsystem) */ + + int sockfd = socket(PF_INET, SOCK_DGRAM, 0); + if (sockfd >= 0) + { + struct ifreq req; + + /* Put the driver name into the request */ + + strncpy(req.ifr_name, ifname, IFNAMSIZ); + + /* Put the new MAC address into the request */ + + req.ifr_hwaddr.sa_family = AF_INETX; + memcpy(&req.ifr_hwaddr.sa_data, macaddr, IFHWADDRLEN); + + /* Perforom the ioctl to set the MAC address */ + + ret = ioctl(sockfd, SIOCSIFHWADDR, (unsigned long)&req); + close(sockfd); + } + } + return ret; +} + +#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/nuttx/netutils/uiplib/uip-setnetmask.c b/nuttx/netutils/uiplib/uip-setnetmask.c index d3acf867e..56e579ef3 100644 --- a/nuttx/netutils/uiplib/uip-setnetmask.c +++ b/nuttx/netutils/uiplib/uip-setnetmask.c @@ -43,10 +43,13 @@ #include #include #include +#include #include #include #include +#include + /**************************************************************************** * Global Functions ****************************************************************************/ @@ -72,18 +75,41 @@ int uip_setnetmask(const char *ifname, const struct in6_addr *addr) int uip_setnetmask(const char *ifname, const struct in_addr *addr) #endif { + int ret = ERROR; if (ifname && addr) { int sockfd = socket(PF_INET, SOCK_DGRAM, 0); if (sockfd >= 0) { 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); - memcpy(&req.ifr_addr, addr, sizeof(addr)); - return ioctl(sockfd, SIOCGIFNETMASK, (unsigned long)&req); + + /* Add the INET address to the request */ + +#ifdef CONFIG_NET_IPv6 +#error "req.ifr_addr.s_addr not big enough for IPv6 address" + 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->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 ERROR; + return ret; } #endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/nuttx/netutils/uiplib/uiplib.c b/nuttx/netutils/uiplib/uiplib.c index 569c59ae3..a60d5743b 100644 --- a/nuttx/netutils/uiplib/uiplib.c +++ b/nuttx/netutils/uiplib/uiplib.c @@ -44,8 +44,7 @@ ****************************************************************************/ #include - -#include "uiplib.h" +#include unsigned char uiplib_ipaddrconv(char *addrstr, unsigned char *ipaddr) { diff --git a/nuttx/netutils/uiplib/uiplib.h b/nuttx/netutils/uiplib/uiplib.h deleted file mode 100644 index 89e1b8c38..000000000 --- a/nuttx/netutils/uiplib/uiplib.h +++ /dev/null @@ -1,72 +0,0 @@ -/**************************************************************************** - * netutils/uiplib/uiplib.h - * Various uIP library functions. - * - * Copyright (C) 2007 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * Based on uIP which also has a BSD style license: - * - * Author: Adam Dunkels - * Copyright (c) 2002, Adam Dunkels. - * All rights reserved. - * - * 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. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. - * - ****************************************************************************/ - -#ifndef __UIPLIB_H__ -#define __UIPLIB_H__ - -#include - -/* Convert a textual representation of an IP address to a numerical representation. - * - * This function takes a textual representation of an IP address in - * the form a.b.c.d and converts it into a 4-byte array that can be - * used by other uIP functions. - * - * addrstr A pointer to a string containing the IP address in - * textual form. - * - * addr A pointer to a 4-byte array that will be filled in with - * the numerical representation of the address. - * - * Return: 0 If the IP address could not be parsed. - * Return: Non-zero If the IP address was parsed. - */ - -extern unsigned char uiplib_ipaddrconv(char *addrstr, unsigned char *addr); - -/* Get and set IP addresses */ - -extern int uip_gethostaddr(const char *ifname, struct in6_addr *addr); -extern int uip_sethostaddr(const char *ifname, const struct in6_addr *addr); -extern int uip_setdraddr(const char *ifname, const struct in6_addr *addr); -extern int uip_setnetmask(const char *ifname, const struct in6_addr *addr); - -#endif /* __UIPLIB_H__ */ diff --git a/nuttx/netutils/webclient/webclient.c b/nuttx/netutils/webclient/webclient.c index 9b900ef66..56c430881 100644 --- a/nuttx/netutils/webclient/webclient.c +++ b/nuttx/netutils/webclient/webclient.c @@ -56,8 +56,8 @@ #include #include -#include "uiplib/uiplib.h" #include +#include #include "webclient.h" -- cgit v1.2.3