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/examples/uip/main.c | 51 +++++++++++---- nuttx/include/net/uip/uip-lib.h | 83 +++++++++++++++++++++++ nuttx/include/net/uip/uip.h | 13 +++- nuttx/include/net/uip/uipopt.h | 25 ------- nuttx/net/netdev-ioctl.c | 17 ++++- nuttx/net/uip/uip.c | 13 +++- 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 +- 17 files changed, 477 insertions(+), 135 deletions(-) create mode 100644 nuttx/include/net/uip/uip-lib.h 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 diff --git a/nuttx/examples/uip/main.c b/nuttx/examples/uip/main.c index 6bc6418bf..3024de873 100644 --- a/nuttx/examples/uip/main.c +++ b/nuttx/examples/uip/main.c @@ -43,12 +43,14 @@ * Included Files ****************************************************************************/ +#include #include #include #include #include #include +#include /* Here we include the header file for the application(s) we use in * our project as defined in the config//defconfig file @@ -90,21 +92,44 @@ void user_initialize(void) int user_start(int argc, char *argv[]) { struct in_addr addr; - uip_ipaddr_t ipaddr; -#if defined(CONFIG_EXAMPLE_UIP_DHCPC) - uint16 mac[6] = {1, 2, 3, 4, 5, 6}; +#if defined(CONFIG_EXAMPLE_UIP_DHCPC) || defined(CONFIG_ARCH_SIM) + uint8 mac[IFHWADDRLEN]; #endif #if defined(CONFIG_EXAMPLE_UIP_DHCPC) || defined(CONFIG_EXAMPLE_UIP_SMTP) void *handle; #endif - addr.s_addr = htonl( 192 << 24 | 168 << 16 | 0 << 8 | 2 ); +#if defined(CONFIG_ARCH_SIM) + /* Give the simulated dirver a MAC address */ + + mac[0] = 0x00; + mac[1] = 0xe0; + mac[2] = 0xb0; + mac[3] = 0x0b; + mac[4] = 0xba; + mac[5] = 0xbe; + uip_setmacaddr("eth0", mac); + +#elif defined(CONFIG_EXAMPLE_UIP_DHCPC) + /* Get the MAC address of the NIC */ + + uip_getmacaddr("eth0", mac); +#endif + + /* Set up our host address */ + + uip_ipaddr(addr.s_addr, 192, 168, 0, 128 ); uip_sethostaddr("eth0", &addr); - uip_ipaddr(ipaddr, 192, 168, 0, 1); - uip_setdraddr("eth0", &ipaddr); - uip_ipaddr(ipaddr, 255, 255, 255, 0); - uip_setnetmask("eth0", &ipaddr); + /* Set up the default router address */ + + uip_ipaddr(addr.s_addr, 192, 168, 0, 1); + uip_setdraddr("eth0", &addr); + + /* Setup the subnet mask */ + + uip_ipaddr(addr.s_addr, 255, 255, 255, 0); + uip_setnetmask("eth0", &addr); #if defined(CONFIG_EXAMPLE_UIP_WEBSERVER) httpd_init(); @@ -113,7 +138,7 @@ void user_initialize(void) telnetd_init(); #elif defined(CONFIG_EXAMPLE_UIP_DHCPC) resolv_init(); - handle = dhcpc_open(&mac, 6); + handle = dhcpc_open(&mac, IFHWADDRLEN); if (handle) { struct dhcpc_state ds; @@ -125,11 +150,11 @@ void user_initialize(void) dhcpc_close(handle); } #elif defined(CONFIG_EXAMPLE_UIP_SMTP) - uip_ipaddr(ipaddr, 127, 0, 0, 1); + uip_ipaddr(addr.s_addr, 127, 0, 0, 1); handle = smtp_open(); if (handle) { - smtp_configure("localhost", ipaddr); + smtp_configure("localhost", addr.s_addr); smtp_send("adam@sics.se", NULL, "uip-testing@example.com", "Testing SMTP from uIP", "Test message sent by uIP\r\n"); smtp_close(handle); @@ -137,8 +162,8 @@ void user_initialize(void) #elif defined(CONFIG_EXAMPLE_UIP_WEBCLIENT) webclient_init(); resolv_init(); - uip_ipaddr(ipaddr, 195, 54, 122, 204); - resolv_conf(ipaddr); + uip_ipaddr(addr.s_addr, 195, 54, 122, 204); + resolv_conf(addr.s_addr); resolv_query("www.sics.se"); #endif diff --git a/nuttx/include/net/uip/uip-lib.h b/nuttx/include/net/uip/uip-lib.h new file mode 100644 index 000000000..3f6c9860d --- /dev/null +++ b/nuttx/include/net/uip/uip-lib.h @@ -0,0 +1,83 @@ +/**************************************************************************** + * net/uip/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 +#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/MAC addresses */ + +extern int uip_setmacaddr(const char *ifname, const uint8 *macaddr); +extern int uip_getmacaddr(const char *ifname, uint8 *macaddr); + +#ifdef CONFIG_NET_IPv6 +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); +#else +extern int uip_gethostaddr(const char *ifname, struct in_addr *addr); +extern int uip_sethostaddr(const char *ifname, const struct in_addr *addr); +extern int uip_setdraddr(const char *ifname, const struct in_addr *addr); +extern int uip_setnetmask(const char *ifname, const struct in_addr *addr); +#endif + +#endif /* __UIPLIB_H__ */ diff --git a/nuttx/include/net/uip/uip.h b/nuttx/include/net/uip/uip.h index fae05836c..dfc0bf337 100644 --- a/nuttx/include/net/uip/uip.h +++ b/nuttx/include/net/uip/uip.h @@ -889,10 +889,9 @@ extern void uip_udpdisable(struct uip_udp_conn *conn); */ #ifndef CONFIG_NET_IPv6 -#define uip_ipaddr_cmp(addr1, addr2) (((uint16 *)addr1)[0] == ((uint16 *)addr2)[0] && \ - ((uint16 *)addr1)[1] == ((uint16 *)addr2)[1]) +# define uip_ipaddr_cmp(addr1, addr2) (addr1 == addr2) #else /* !CONFIG_NET_IPv6 */ -#define uip_ipaddr_cmp(addr1, addr2) (memcmp(addr1, addr2, sizeof(uip_ip6addr_t)) == 0) +# define uip_ipaddr_cmp(addr1, addr2) (memcmp(&addr1, &addr2, sizeof(uip_ip6addr_t)) == 0) #endif /* !CONFIG_NET_IPv6 */ /* Compare two IP addresses with netmasks @@ -1013,4 +1012,12 @@ extern void uip_udpdisable(struct uip_udp_conn *conn); #define uip_ipaddr4(addr) (htons(((uint16 *)(addr))[1]) & 0xff) +/* Print out a uIP log message. + * + * This function must be implemented by the module that uses uIP, and + * is called by uIP whenever a log message is generated. + */ + +extern void uip_log(char *msg); + #endif /* __NET_UIP_UIP_H */ diff --git a/nuttx/include/net/uip/uipopt.h b/nuttx/include/net/uip/uipopt.h index 3123a7135..085ce6439 100644 --- a/nuttx/include/net/uip/uipopt.h +++ b/nuttx/include/net/uip/uipopt.h @@ -271,19 +271,6 @@ # define UIP_STATISTICS CONFIG_NET_STATISTICS #endif /* CONFIG_NET_STATISTICS */ -/* Determines if logging of certain events should be compiled in. - * - * This is useful mostly for debugging. The function uip_log() - * must be implemented to suit the architecture of the project, if - * logging is turned on. - */ - -#ifndef CONFIG_NET_LOGGING -# define UIP_LOGGING 0 -#else /* CONFIG_NET_LOGGING */ -# define UIP_LOGGING CONFIG_NET_LOGGING -#endif /* CONFIG_NET_LOGGING */ - /* Broadcast support. * * This flag configures IP broadcast support. This is useful only @@ -345,16 +332,4 @@ typedef uint16 uip_stats_t; -/**************************************************************************** - * Public Function Prototypes - ****************************************************************************/ - -/* Print out a uIP log message. - * - * This function must be implemented by the module that uses uIP, and - * is called by uIP whenever a log message is generated. - */ - -void uip_log(char *msg); - #endif /* __UIPOPT_H__ */ diff --git a/nuttx/net/netdev-ioctl.c b/nuttx/net/netdev-ioctl.c index cdc7c80b8..1abc84105 100644 --- a/nuttx/net/netdev-ioctl.c +++ b/nuttx/net/netdev-ioctl.c @@ -56,6 +56,12 @@ * Definitions ****************************************************************************/ +#ifdef CONFIG_NET_IPv6 +# define AF_INETX AF_INET6 +#else +# define AF_INETX AF_INET +#endif + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -71,10 +77,15 @@ static void _get_ipaddr(struct sockaddr *outaddr, uip_ipaddr_t *inaddr) { #ifdef CONFIG_NET_IPv6 +#error " big enough for IPv6 address" struct sockaddr_in6 *dest = (struct sockaddr_in6 *)outaddr; - memcpy(&dest->sin6_addr.in6_u, inaddr, IFHWADDRLEN); + dest->sin_family = AF_INET6; + dest->sin_port = 0; + memcpy(dest->sin6_addr.in6_u.u6_addr8, inaddr, 16); #else struct sockaddr_in *dest = (struct sockaddr_in *)outaddr; + dest->sin_family = AF_INET; + dest->sin_port = 0; dest->sin_addr.s_addr = *inaddr; #endif } @@ -83,7 +94,7 @@ static void _set_ipaddr(uip_ipaddr_t *outaddr, struct sockaddr *inaddr) { #ifdef CONFIG_NET_IPv6 struct sockaddr_in6 *src = (struct sockaddr_in6 *)inaddr; - memcpy(outaddr, &src->sin6_addr.in6_u, IFHWADDRLEN); + memcpy(outaddr, src->sin6_addr.in6_u.u6_addr8, 16); #else struct sockaddr_in *src = (struct sockaddr_in *)inaddr; *outaddr = src->sin_addr.s_addr; @@ -187,10 +198,12 @@ int netdev_ioctl(int sockfd, int cmd, struct ifreq *req) break; case SIOCGIFHWADDR: /* Get hardware address */ + req->ifr_hwaddr.sa_family = AF_INETX; memcpy(req->ifr_hwaddr.sa_data, dev->d_mac.addr, IFHWADDRLEN); break; case SIOCSIFHWADDR: /* Set hardware address */ + req->ifr_hwaddr.sa_family = AF_INETX; memcpy(dev->d_mac.addr, req->ifr_hwaddr.sa_data, IFHWADDRLEN); break; diff --git a/nuttx/net/uip/uip.c b/nuttx/net/uip/uip.c index 3cfb37b2a..3c8e8001e 100644 --- a/nuttx/net/uip/uip.c +++ b/nuttx/net/uip/uip.c @@ -87,13 +87,20 @@ #include -#if UIP_LOGGING == 1 -#include +/* Check if logging of network events should be compiled in. + * + * This is useful mostly for debugging. The function uip_log() + * must be implemented to suit the architecture of the project, if + * logging is turned on. + */ + +#ifdef CONFIG_NET_LOGGING +# include extern void uip_log(char *msg); # define UIP_LOG(m) uip_log(m) #else # define UIP_LOG(m) -#endif /* UIP_LOGGING == 1 */ +#endif #include "uip-internal.h" 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