9
0
Fork 0

Correct some issues with IP/MAC address handling

git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@355 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
patacongo 2007-10-26 21:21:34 +00:00
parent a9021c3b7d
commit 27cb55eb5b
16 changed files with 407 additions and 65 deletions

View File

@ -43,12 +43,14 @@
* Included Files * Included Files
****************************************************************************/ ****************************************************************************/
#include <sys/ioctl.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <net/uip/uip.h> #include <net/uip/uip.h>
#include <net/uip/uip-arp.h> #include <net/uip/uip-arp.h>
#include <net/uip/uip-lib.h>
/* Here we include the header file for the application(s) we use in /* Here we include the header file for the application(s) we use in
* our project as defined in the config/<board-name>/defconfig file * our project as defined in the config/<board-name>/defconfig file
@ -90,21 +92,44 @@ void user_initialize(void)
int user_start(int argc, char *argv[]) int user_start(int argc, char *argv[])
{ {
struct in_addr addr; struct in_addr addr;
uip_ipaddr_t ipaddr; #if defined(CONFIG_EXAMPLE_UIP_DHCPC) || defined(CONFIG_ARCH_SIM)
#if defined(CONFIG_EXAMPLE_UIP_DHCPC) uint8 mac[IFHWADDRLEN];
uint16 mac[6] = {1, 2, 3, 4, 5, 6};
#endif #endif
#if defined(CONFIG_EXAMPLE_UIP_DHCPC) || defined(CONFIG_EXAMPLE_UIP_SMTP) #if defined(CONFIG_EXAMPLE_UIP_DHCPC) || defined(CONFIG_EXAMPLE_UIP_SMTP)
void *handle; void *handle;
#endif #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_sethostaddr("eth0", &addr);
uip_ipaddr(ipaddr, 192, 168, 0, 1); /* Set up the default router address */
uip_setdraddr("eth0", &ipaddr);
uip_ipaddr(ipaddr, 255, 255, 255, 0); uip_ipaddr(addr.s_addr, 192, 168, 0, 1);
uip_setnetmask("eth0", &ipaddr); 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) #if defined(CONFIG_EXAMPLE_UIP_WEBSERVER)
httpd_init(); httpd_init();
@ -113,7 +138,7 @@ void user_initialize(void)
telnetd_init(); telnetd_init();
#elif defined(CONFIG_EXAMPLE_UIP_DHCPC) #elif defined(CONFIG_EXAMPLE_UIP_DHCPC)
resolv_init(); resolv_init();
handle = dhcpc_open(&mac, 6); handle = dhcpc_open(&mac, IFHWADDRLEN);
if (handle) if (handle)
{ {
struct dhcpc_state ds; struct dhcpc_state ds;
@ -125,11 +150,11 @@ void user_initialize(void)
dhcpc_close(handle); dhcpc_close(handle);
} }
#elif defined(CONFIG_EXAMPLE_UIP_SMTP) #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(); handle = smtp_open();
if (handle) if (handle)
{ {
smtp_configure("localhost", ipaddr); smtp_configure("localhost", addr.s_addr);
smtp_send("adam@sics.se", NULL, "uip-testing@example.com", smtp_send("adam@sics.se", NULL, "uip-testing@example.com",
"Testing SMTP from uIP", "Test message sent by uIP\r\n"); "Testing SMTP from uIP", "Test message sent by uIP\r\n");
smtp_close(handle); smtp_close(handle);
@ -137,8 +162,8 @@ void user_initialize(void)
#elif defined(CONFIG_EXAMPLE_UIP_WEBCLIENT) #elif defined(CONFIG_EXAMPLE_UIP_WEBCLIENT)
webclient_init(); webclient_init();
resolv_init(); resolv_init();
uip_ipaddr(ipaddr, 195, 54, 122, 204); uip_ipaddr(addr.s_addr, 195, 54, 122, 204);
resolv_conf(ipaddr); resolv_conf(addr.s_addr);
resolv_query("www.sics.se"); resolv_query("www.sics.se");
#endif #endif

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* netutils/uiplib/uiplib.h * net/uip/uiplib.h
* Various uIP library functions. * Various uIP library functions.
* *
* Copyright (C) 2007 Gregory Nutt. All rights reserved. * Copyright (C) 2007 Gregory Nutt. All rights reserved.
@ -42,6 +42,7 @@
#ifndef __UIPLIB_H__ #ifndef __UIPLIB_H__
#define __UIPLIB_H__ #define __UIPLIB_H__
#include <nuttx/config.h>
#include <netinet/in.h> #include <netinet/in.h>
/* Convert a textual representation of an IP address to a numerical representation. /* Convert a textual representation of an IP address to a numerical representation.
@ -62,11 +63,21 @@
extern unsigned char uiplib_ipaddrconv(char *addrstr, unsigned char *addr); extern unsigned char uiplib_ipaddrconv(char *addrstr, unsigned char *addr);
/* Get and set IP addresses */ /* 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_gethostaddr(const char *ifname, struct in6_addr *addr);
extern int uip_sethostaddr(const char *ifname, const 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_setdraddr(const char *ifname, const struct in6_addr *addr);
extern int uip_setnetmask(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__ */ #endif /* __UIPLIB_H__ */

View File

@ -889,10 +889,9 @@ extern void uip_udpdisable(struct uip_udp_conn *conn);
*/ */
#ifndef CONFIG_NET_IPv6 #ifndef CONFIG_NET_IPv6
#define uip_ipaddr_cmp(addr1, addr2) (((uint16 *)addr1)[0] == ((uint16 *)addr2)[0] && \ # define uip_ipaddr_cmp(addr1, addr2) (addr1 == addr2)
((uint16 *)addr1)[1] == ((uint16 *)addr2)[1])
#else /* !CONFIG_NET_IPv6 */ #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 */ #endif /* !CONFIG_NET_IPv6 */
/* Compare two IP addresses with netmasks /* 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) #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 */ #endif /* __NET_UIP_UIP_H */

View File

@ -271,19 +271,6 @@
# define UIP_STATISTICS CONFIG_NET_STATISTICS # define UIP_STATISTICS CONFIG_NET_STATISTICS
#endif /* 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. /* Broadcast support.
* *
* This flag configures IP broadcast support. This is useful only * This flag configures IP broadcast support. This is useful only
@ -345,16 +332,4 @@
typedef uint16 uip_stats_t; 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__ */ #endif /* __UIPOPT_H__ */

View File

@ -56,6 +56,12 @@
* Definitions * Definitions
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_NET_IPv6
# define AF_INETX AF_INET6
#else
# define AF_INETX AF_INET
#endif
/**************************************************************************** /****************************************************************************
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
@ -71,10 +77,15 @@
static void _get_ipaddr(struct sockaddr *outaddr, uip_ipaddr_t *inaddr) static void _get_ipaddr(struct sockaddr *outaddr, uip_ipaddr_t *inaddr)
{ {
#ifdef CONFIG_NET_IPv6 #ifdef CONFIG_NET_IPv6
#error " big enough for IPv6 address"
struct sockaddr_in6 *dest = (struct sockaddr_in6 *)outaddr; 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 #else
struct sockaddr_in *dest = (struct sockaddr_in *)outaddr; struct sockaddr_in *dest = (struct sockaddr_in *)outaddr;
dest->sin_family = AF_INET;
dest->sin_port = 0;
dest->sin_addr.s_addr = *inaddr; dest->sin_addr.s_addr = *inaddr;
#endif #endif
} }
@ -83,7 +94,7 @@ static void _set_ipaddr(uip_ipaddr_t *outaddr, struct sockaddr *inaddr)
{ {
#ifdef CONFIG_NET_IPv6 #ifdef CONFIG_NET_IPv6
struct sockaddr_in6 *src = (struct sockaddr_in6 *)inaddr; 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 #else
struct sockaddr_in *src = (struct sockaddr_in *)inaddr; struct sockaddr_in *src = (struct sockaddr_in *)inaddr;
*outaddr = src->sin_addr.s_addr; *outaddr = src->sin_addr.s_addr;
@ -187,10 +198,12 @@ int netdev_ioctl(int sockfd, int cmd, struct ifreq *req)
break; break;
case SIOCGIFHWADDR: /* Get hardware address */ case SIOCGIFHWADDR: /* Get hardware address */
req->ifr_hwaddr.sa_family = AF_INETX;
memcpy(req->ifr_hwaddr.sa_data, dev->d_mac.addr, IFHWADDRLEN); memcpy(req->ifr_hwaddr.sa_data, dev->d_mac.addr, IFHWADDRLEN);
break; break;
case SIOCSIFHWADDR: /* Set hardware address */ case SIOCSIFHWADDR: /* Set hardware address */
req->ifr_hwaddr.sa_family = AF_INETX;
memcpy(dev->d_mac.addr, req->ifr_hwaddr.sa_data, IFHWADDRLEN); memcpy(dev->d_mac.addr, req->ifr_hwaddr.sa_data, IFHWADDRLEN);
break; break;

View File

@ -87,13 +87,20 @@
#include <string.h> #include <string.h>
#if UIP_LOGGING == 1 /* Check if logging of network events should be compiled in.
#include <stdio.h> *
* 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 <stdio.h>
extern void uip_log(char *msg); extern void uip_log(char *msg);
# define UIP_LOG(m) uip_log(m) # define UIP_LOG(m) uip_log(m)
#else #else
# define UIP_LOG(m) # define UIP_LOG(m)
#endif /* UIP_LOGGING == 1 */ #endif
#include "uip-internal.h" #include "uip-internal.h"

View File

@ -54,6 +54,7 @@
#include <net/uip/uip.h> #include <net/uip/uip.h>
#include <net/uip/dhcpc.h> #include <net/uip/dhcpc.h>
#include <net/uip/uip-lib.h>
/**************************************************************************** /****************************************************************************
* Definitions * Definitions
@ -192,7 +193,7 @@ static void create_msg(struct dhcpc_state_internal *pdhcpc, struct dhcp_msg *pms
pmsg->secs = 0; pmsg->secs = 0;
pmsg->flags = HTONS(BOOTP_BROADCAST); /* Broadcast bit. */ pmsg->flags = HTONS(BOOTP_BROADCAST); /* Broadcast bit. */
uip_gethostaddr( "eth0", &addr); uip_gethostaddr("eth0", &addr);
memcpy(&pmsg->ciaddr, &addr.s_addr, sizeof(pmsg->ciaddr)); memcpy(&pmsg->ciaddr, &addr.s_addr, sizeof(pmsg->ciaddr));
memset(pmsg->yiaddr, 0, sizeof(pmsg->yiaddr)); memset(pmsg->yiaddr, 0, sizeof(pmsg->yiaddr));
memset(pmsg->siaddr, 0, sizeof(pmsg->siaddr)); memset(pmsg->siaddr, 0, sizeof(pmsg->siaddr));

View File

@ -34,4 +34,5 @@
############################################################################ ############################################################################
UIPLIB_ASRCS = 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

View File

@ -47,19 +47,25 @@
#include <errno.h> #include <errno.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <net/uip/uip-lib.h>
/****************************************************************************
* Definitions
****************************************************************************/
/**************************************************************************** /****************************************************************************
* Global Functions * Global Functions
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
* Name: uip_sethostaddr * Name: uip_gethostaddr
* *
* Description: * Description:
* Get the network driver IP address * Get the network driver IP address
* *
* Parameters: * Parameters:
* ifname The name of the interface to use * ifname The name of the interface to use
* ipaddr The address to set * ipaddr The location to return the IP address
* *
* Return: * Return:
* 0 on sucess; -1 on failure * 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); ret = ioctl(sockfd, SIOCSIFADDR, (unsigned long)&req);
if (!ret) 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
} }
} }
} }

View File

@ -0,0 +1,102 @@
/****************************************************************************
* netutils/uiplib/uip-getmacaddr.c
*
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <nuttx/config.h>
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <net/uip/uip-lib.h>
/****************************************************************************
* 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 */

View File

@ -43,10 +43,13 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <net/uip/uip-lib.h>
/**************************************************************************** /****************************************************************************
* Global Functions * 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) int uip_setdraddr(const char *ifname, const struct in_addr *addr)
#endif #endif
{ {
int ret = ERROR;
if (ifname && addr) if (ifname && addr)
{ {
int sockfd = socket(PF_INET, SOCK_DGRAM, 0); int sockfd = socket(PF_INET, SOCK_DGRAM, 0);
if (sockfd >= 0) if (sockfd >= 0)
{ {
struct ifreq req; 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); 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 */ #endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */

View File

@ -43,10 +43,13 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <net/uip/uip-lib.h>
/**************************************************************************** /****************************************************************************
* Global Functions * 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) int uip_sethostaddr(const char *ifname, const struct in_addr *addr)
#endif #endif
{ {
int ret = ERROR;
if (ifname && addr) if (ifname && addr)
{ {
int sockfd = socket(PF_INET, SOCK_DGRAM, 0); int sockfd = socket(PF_INET, SOCK_DGRAM, 0);
if (sockfd >= 0) if (sockfd >= 0)
{ {
struct ifreq req; 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); 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 */ #endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */

View File

@ -0,0 +1,112 @@
/****************************************************************************
* netutils/uiplib/uip-setmacaddr.c
*
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <nuttx/config.h>
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <net/uip/uip-lib.h>
/****************************************************************************
* 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 */

View File

@ -43,10 +43,13 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <net/uip/uip-lib.h>
/**************************************************************************** /****************************************************************************
* Global Functions * 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) int uip_setnetmask(const char *ifname, const struct in_addr *addr)
#endif #endif
{ {
int ret = ERROR;
if (ifname && addr) if (ifname && addr)
{ {
int sockfd = socket(PF_INET, SOCK_DGRAM, 0); int sockfd = socket(PF_INET, SOCK_DGRAM, 0);
if (sockfd >= 0) if (sockfd >= 0)
{ {
struct ifreq req; 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); 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 */ #endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */

View File

@ -44,8 +44,7 @@
****************************************************************************/ ****************************************************************************/
#include <net/uip/uip.h> #include <net/uip/uip.h>
#include <net/uip/uip-lib.h>
#include "uiplib.h"
unsigned char uiplib_ipaddrconv(char *addrstr, unsigned char *ipaddr) unsigned char uiplib_ipaddrconv(char *addrstr, unsigned char *ipaddr)
{ {

View File

@ -56,8 +56,8 @@
#include <net/uip/uip.h> #include <net/uip/uip.h>
#include <net/uip/resolv.h> #include <net/uip/resolv.h>
#include "uiplib/uiplib.h"
#include <net/uip/uip-arch.h> #include <net/uip/uip-arch.h>
#include <net/uip/uip-lib.h>
#include "webclient.h" #include "webclient.h"