mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-09-13 22:20:31 +00:00
NET: Rename uiplib/UIPLIB to netlib/NETLIB
This commit is contained in:
parent
79aa55183c
commit
2ee5041477
53 changed files with 109 additions and 109 deletions
6
netutils/netlib/.gitignore
vendored
Normal file
6
netutils/netlib/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
/.built
|
||||
/.depend
|
||||
/Make.dep
|
||||
/*.src
|
||||
/*.obj
|
||||
/*.lst
|
||||
13
netutils/netlib/Kconfig
Normal file
13
netutils/netlib/Kconfig
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see misc/tools/kconfig-language.txt.
|
||||
#
|
||||
|
||||
config NETUTILS_NETLIB
|
||||
bool "Network support library"
|
||||
default n
|
||||
---help---
|
||||
Enable support for the network support library.
|
||||
|
||||
if NETUTILS_NETLIB
|
||||
endif
|
||||
116
netutils/netlib/Makefile
Normal file
116
netutils/netlib/Makefile
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
############################################################################
|
||||
# apps/netutils/netlib/Makefile
|
||||
#
|
||||
# Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
-include $(TOPDIR)/.config
|
||||
-include $(TOPDIR)/Make.defs
|
||||
include $(APPDIR)/Make.defs
|
||||
|
||||
# UIP Library
|
||||
|
||||
ASRCS =
|
||||
CSRCS = netlib.c uip_sethostaddr.c uip_gethostaddr.c uip_setdraddr.c \
|
||||
uip_setnetmask.c uip_parsehttpurl.c uip_setifflag.c \
|
||||
uip_getifflag.c
|
||||
|
||||
# These require TCP support
|
||||
|
||||
ifeq ($(CONFIG_NET_TCP),y)
|
||||
CSRCS += uip_server.c uip_listenon.c
|
||||
endif
|
||||
|
||||
# No MAC address support for SLIP (Ethernet only)
|
||||
|
||||
ifneq ($(CONFIG_NET_SLIP),y)
|
||||
CSRCS += uip_setmacaddr.c uip_getmacaddr.c
|
||||
endif
|
||||
|
||||
# IGMP support
|
||||
|
||||
ifeq ($(CONFIG_NET_IGMP),y)
|
||||
CSRCS += uip_ipmsfilter.c
|
||||
endif
|
||||
|
||||
AOBJS = $(ASRCS:.S=$(OBJEXT))
|
||||
COBJS = $(CSRCS:.c=$(OBJEXT))
|
||||
|
||||
SRCS = $(ASRCS) $(CSRCS)
|
||||
OBJS = $(AOBJS) $(COBJS)
|
||||
|
||||
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
|
||||
BIN = ..\..\libapps$(LIBEXT)
|
||||
else
|
||||
ifeq ($(WINTOOL),y)
|
||||
BIN = ..\\..\\libapps$(LIBEXT)
|
||||
else
|
||||
BIN = ../../libapps$(LIBEXT)
|
||||
endif
|
||||
endif
|
||||
|
||||
ROOTDEPPATH = --dep-path .
|
||||
|
||||
# Common build
|
||||
|
||||
VPATH =
|
||||
|
||||
all: .built
|
||||
.PHONY: context depend clean distclean
|
||||
|
||||
$(AOBJS): %$(OBJEXT): %.S
|
||||
$(call ASSEMBLE, $<, $@)
|
||||
|
||||
$(COBJS): %$(OBJEXT): %.c
|
||||
$(call COMPILE, $<, $@)
|
||||
|
||||
.built: $(OBJS)
|
||||
$(call ARCHIVE, $(BIN), $(OBJS))
|
||||
$(Q) touch .built
|
||||
|
||||
context:
|
||||
|
||||
.depend: Makefile $(SRCS)
|
||||
$(Q) $(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep
|
||||
$(Q) touch $@
|
||||
|
||||
depend: .depend
|
||||
|
||||
clean:
|
||||
$(call DELFILE, .built)
|
||||
$(call CLEAN)
|
||||
|
||||
distclean: clean
|
||||
$(call DELFILE, Make.dep)
|
||||
$(call DELFILE, .depend)
|
||||
|
||||
-include Make.dep
|
||||
157
netutils/netlib/netlib.c
Normal file
157
netutils/netlib/netlib.c
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
/****************************************************************************
|
||||
* netutils/netlib/netlib.c
|
||||
* Various uIP library functions.
|
||||
*
|
||||
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Based on uIP which also has a BSD style license:
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
* Copyright (c) 2004, Adam Dunkels and the Swedish Institute of
|
||||
* Computer Science.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/net/uip.h>
|
||||
#include <apps/netutils/netlib.h>
|
||||
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
bool netlib_ipaddrconv(const char *addrstr, uint8_t *ipaddr)
|
||||
{
|
||||
unsigned char tmp;
|
||||
char c;
|
||||
unsigned char i;
|
||||
unsigned char j;
|
||||
|
||||
tmp = 0;
|
||||
|
||||
for (i = 0; i < 4; ++i)
|
||||
{
|
||||
j = 0;
|
||||
do
|
||||
{
|
||||
c = *addrstr;
|
||||
++j;
|
||||
if (j > 4)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (c == '.' || c == 0)
|
||||
{
|
||||
*ipaddr = tmp;
|
||||
++ipaddr;
|
||||
tmp = 0;
|
||||
}
|
||||
else if (c >= '0' && c <= '9')
|
||||
{
|
||||
tmp = (tmp * 10) + (c - '0');
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
++addrstr;
|
||||
}
|
||||
while (c != '.' && c != 0);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool netlib_hwmacconv(const char *hwstr, uint8_t *hw)
|
||||
{
|
||||
unsigned char tmp;
|
||||
char c;
|
||||
unsigned char i;
|
||||
unsigned char j;
|
||||
|
||||
if (strlen(hwstr) != 17)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
tmp = 0;
|
||||
|
||||
for (i = 0; i < 6; ++i)
|
||||
{
|
||||
j = 0;
|
||||
do
|
||||
{
|
||||
c = *hwstr;
|
||||
++j;
|
||||
if (j > 3)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (c == ':' || c == 0)
|
||||
{
|
||||
*hw = tmp;
|
||||
nvdbg("HWMAC[%d]%0.2X\n",i,tmp);
|
||||
++hw;
|
||||
tmp = 0;
|
||||
}
|
||||
else if (c >= '0' && c <= '9')
|
||||
{
|
||||
tmp = (tmp << 4) + (c - '0');
|
||||
}
|
||||
else if (c >= 'a' && c <= 'f')
|
||||
{
|
||||
tmp = (tmp << 4) + (c - 'a' + 10);
|
||||
}
|
||||
else if (c >= 'A' && c <= 'F')
|
||||
{
|
||||
tmp = (tmp << 4) + (c - 'A' + 10);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
++hwstr;
|
||||
}
|
||||
while (c != ':' && c != 0);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
107
netutils/netlib/uip_gethostaddr.c
Normal file
107
netutils/netlib/uip_gethostaddr.c
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
/****************************************************************************
|
||||
* netutils/netlib/uip_gethostaddr.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
|
||||
#include <apps/netutils/netlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_gethostaddr
|
||||
*
|
||||
* Description:
|
||||
* Get the network driver IP address
|
||||
*
|
||||
* Parameters:
|
||||
* ifname The name of the interface to use
|
||||
* ipaddr The location to return the IP address
|
||||
*
|
||||
* Return:
|
||||
* 0 on sucess; -1 on failure
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
int uip_gethostaddr(const char *ifname, struct in6_addr *addr)
|
||||
#else
|
||||
int uip_gethostaddr(const char *ifname, struct in_addr *addr)
|
||||
#endif
|
||||
{
|
||||
int ret = ERROR;
|
||||
if (ifname && addr)
|
||||
{
|
||||
int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 0);
|
||||
if (sockfd >= 0)
|
||||
{
|
||||
struct ifreq req;
|
||||
strncpy(req.ifr_name, ifname, IFNAMSIZ);
|
||||
ret = ioctl(sockfd, SIOCGIFADDR, (unsigned long)&req);
|
||||
if (!ret)
|
||||
{
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
memcpy(addr, &req.ifr_addr, sizeof(struct in6_addr));
|
||||
#else
|
||||
memcpy(addr, &req.ifr_addr, sizeof(struct in_addr));
|
||||
#endif
|
||||
}
|
||||
close(sockfd);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */
|
||||
107
netutils/netlib/uip_getifflag.c
Normal file
107
netutils/netlib/uip_getifflag.c
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
/****************************************************************************
|
||||
* netutils/netlib/uip_getifflag.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011, 2014 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
|
||||
#include <apps/netutils/netlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_getifstatus
|
||||
*
|
||||
* Description:
|
||||
* Get the network driver ifup/ifdown status
|
||||
*
|
||||
* Parameters:
|
||||
* ifname The name of the interface to use
|
||||
* flags The interface flags returned by SIOCGIFFLAGS
|
||||
*
|
||||
* Return:
|
||||
* 0 on sucess; -1 on failure
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int uip_getifstatus(FAR const char *ifname, FAR uint8_t *flags)
|
||||
{
|
||||
int ret = ERROR;
|
||||
if (ifname)
|
||||
{
|
||||
/* Get a socket (only so that we get access to the INET subsystem) */
|
||||
|
||||
int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 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 ifup or ifdown status */
|
||||
|
||||
ret = ioctl(sockfd, SIOCGIFFLAGS, (unsigned long)&req);
|
||||
if (!ret)
|
||||
{
|
||||
*flags = req.ifr_flags;
|
||||
}
|
||||
|
||||
close(sockfd);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */
|
||||
106
netutils/netlib/uip_getmacaddr.c
Normal file
106
netutils/netlib/uip_getmacaddr.c
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
/****************************************************************************
|
||||
* netutils/netlib/uip_getmacaddr.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
|
||||
#include <apps/netutils/netlib.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_t *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, NETLIB_SOCK_IOCTL, 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);
|
||||
}
|
||||
close(sockfd);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */
|
||||
116
netutils/netlib/uip_ipmsfilter.c
Normal file
116
netutils/netlib/uip_ipmsfilter.c
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
/****************************************************************************
|
||||
* netutils/netlib/uip_setmultiaddr.c
|
||||
*
|
||||
* Copyright (C) 2010-2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <sys/sockio.h>
|
||||
|
||||
#include <apps/netutils/netlib.h>
|
||||
#include <apps/netutils/ipmsfilter.h>
|
||||
|
||||
#ifdef CONFIG_NET_IGMP
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ipmsfilter
|
||||
*
|
||||
* Description:
|
||||
* Add or remove an IP address from a multicast filter set.
|
||||
*
|
||||
* Parameters:
|
||||
* ifname The name of the interface to use, size must less than IMSFNAMSIZ
|
||||
* multiaddr Multicast group address to add/remove (network byte order)
|
||||
* fmode MCAST_INCLUDE: Add multicast address
|
||||
* MCAST_EXCLUDE: Remove multicast address
|
||||
*
|
||||
* Return:
|
||||
* 0 on sucess; Negated errno on failure
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int ipmsfilter(FAR const char *ifname, FAR const struct in_addr *multiaddr,
|
||||
uint32_t fmode)
|
||||
{
|
||||
int ret = ERROR;
|
||||
|
||||
nvdbg("ifname: %s muliaddr: %08x fmode: %ld\n", ifname, *multiaddr, fmode);
|
||||
if (ifname && multiaddr)
|
||||
{
|
||||
/* Get a socket (only so that we get access to the INET subsystem) */
|
||||
|
||||
int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 0);
|
||||
if (sockfd >= 0)
|
||||
{
|
||||
struct ip_msfilter imsf;
|
||||
|
||||
/* Put the driver name into the request */
|
||||
|
||||
strncpy(imsf.imsf_name, ifname, IMSFNAMSIZ);
|
||||
|
||||
/* Put the new address into the request */
|
||||
|
||||
imsf.imsf_multiaddr.s_addr = multiaddr->s_addr;
|
||||
|
||||
/* Perforom the ioctl to set the MAC address */
|
||||
|
||||
imsf.imsf_fmode = fmode;
|
||||
ret = ioctl(sockfd, SIOCSIPMSFILTER, (unsigned long)&imsf);
|
||||
close(sockfd);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET_IGM */
|
||||
131
netutils/netlib/uip_listenon.c
Normal file
131
netutils/netlib/uip_listenon.c
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
/****************************************************************************
|
||||
* netutils/netlib/uip_listenon.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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 Gregory Nutt 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>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <apps/netutils/netlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_listenon
|
||||
*
|
||||
* Description:
|
||||
* Implement basic server listening
|
||||
*
|
||||
* Parameters:
|
||||
* portno The port to listen on (in network byte order)
|
||||
*
|
||||
* Return:
|
||||
* A valid listening socket or -1 on error.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int uip_listenon(uint16_t portno)
|
||||
{
|
||||
struct sockaddr_in myaddr;
|
||||
int listensd;
|
||||
#ifdef CONFIG_NET_HAVE_REUSEADDR
|
||||
int optval;
|
||||
#endif
|
||||
|
||||
/* Create a new TCP socket to use to listen for connections */
|
||||
|
||||
listensd = socket(PF_INET, SOCK_STREAM, 0);
|
||||
if (listensd < 0)
|
||||
{
|
||||
ndbg("socket failure: %d\n", errno);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* Set socket to reuse address */
|
||||
|
||||
#ifdef CONFIG_NET_HAVE_REUSEADDR
|
||||
optval = 1;
|
||||
if (setsockopt(listensd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
|
||||
{
|
||||
ndbg("setsockopt SO_REUSEADDR failure: %d\n", errno);
|
||||
goto errout_with_socket;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Bind the socket to a local address */
|
||||
|
||||
myaddr.sin_family = AF_INET;
|
||||
myaddr.sin_port = portno;
|
||||
myaddr.sin_addr.s_addr = INADDR_ANY;
|
||||
|
||||
if (bind(listensd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr_in)) < 0)
|
||||
{
|
||||
ndbg("bind failure: %d\n", errno);
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
/* Listen for connections on the bound TCP socket */
|
||||
|
||||
if (listen(listensd, 5) < 0)
|
||||
{
|
||||
ndbg("listen failure %d\n", errno);
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
/* Begin accepting connections */
|
||||
|
||||
nvdbg("Accepting connections on port %d\n", ntohs(portno));
|
||||
return listensd;
|
||||
|
||||
errout_with_socket:
|
||||
close(listensd);
|
||||
return ERROR;
|
||||
}
|
||||
149
netutils/netlib/uip_parsehttpurl.c
Normal file
149
netutils/netlib/uip_parsehttpurl.c
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
/****************************************************************************
|
||||
* netutils/netlib/uip_parsehttpurl.c
|
||||
*
|
||||
* Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <apps/netutils/netlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
*****************************************************************************/
|
||||
|
||||
const char g_http[] = "http://";
|
||||
#define HTTPLEN 7
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
*****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_parsehttpurl
|
||||
****************************************************************************/
|
||||
|
||||
int uip_parsehttpurl(const char *url, uint16_t *port,
|
||||
char *hostname, int hostlen,
|
||||
char *filename, int namelen)
|
||||
{
|
||||
const char *src = url;
|
||||
char *dest;
|
||||
int bytesleft;
|
||||
int ret = OK;
|
||||
|
||||
/* A valid HTTP URL must begin with http:// if it does not, we will assume
|
||||
* that it is a file name only, but still return an error. wget() depends
|
||||
* on this strange behavior.
|
||||
*/
|
||||
|
||||
if (strncmp(src, g_http, HTTPLEN) != 0)
|
||||
{
|
||||
ret = -EINVAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Skip over the http:// */
|
||||
|
||||
src += HTTPLEN;
|
||||
|
||||
/* Concatenate the hostname following http:// and up to the termnator */
|
||||
|
||||
dest = hostname;
|
||||
bytesleft = hostlen;
|
||||
while (*src != '\0' && *src != '/' && *src != ' ' && *src != ':')
|
||||
{
|
||||
/* Make sure that there is space for another character in the hostname.
|
||||
* (reserving space for the null terminator)
|
||||
*/
|
||||
|
||||
if (bytesleft > 1)
|
||||
{
|
||||
*dest++ = *src++;
|
||||
bytesleft--;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = -E2BIG;
|
||||
}
|
||||
}
|
||||
*dest = '\0';
|
||||
|
||||
/* Check if the hostname is following by a port number */
|
||||
|
||||
if (*src == ':')
|
||||
{
|
||||
uint16_t accum = 0;
|
||||
src++; /* Skip over the colon */
|
||||
|
||||
while (*src >= '0' && *src <= '9')
|
||||
{
|
||||
accum = 10*accum + *src - '0';
|
||||
src++;
|
||||
}
|
||||
*port = accum;
|
||||
}
|
||||
}
|
||||
|
||||
/* The rest of the line is the file name */
|
||||
|
||||
if (*src == '\0' || *src == ' ')
|
||||
{
|
||||
ret = -ENOENT;
|
||||
}
|
||||
|
||||
/* Make sure the file name starts with exactly one '/' */
|
||||
|
||||
dest = filename;
|
||||
bytesleft = namelen;
|
||||
while (*src == '/')
|
||||
{
|
||||
src++;
|
||||
}
|
||||
*dest++ = '/';
|
||||
bytesleft--;
|
||||
|
||||
/* The copy the rest of the file name to the user buffer */
|
||||
|
||||
strncpy(dest, src, namelen);
|
||||
filename[namelen-1] = '\0';
|
||||
return ret;
|
||||
}
|
||||
|
||||
175
netutils/netlib/uip_server.c
Normal file
175
netutils/netlib/uip_server.c
Normal file
|
|
@ -0,0 +1,175 @@
|
|||
/****************************************************************************
|
||||
* netutils/netlib/uip_server.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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 Gregory Nutt 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>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <apps/netutils/netlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_server
|
||||
*
|
||||
* Description:
|
||||
* Implement basic server logic
|
||||
*
|
||||
* Parameters:
|
||||
* portno The port to listen on (in network byte order)
|
||||
* handler The entrypoint of the task to spawn when a new connection is
|
||||
* accepted.
|
||||
* stacksize The stack size needed by the spawned task
|
||||
*
|
||||
* Return:
|
||||
* Does not return unless an error occurs.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_server(uint16_t portno, pthread_startroutine_t handler, int stacksize)
|
||||
{
|
||||
struct sockaddr_in myaddr;
|
||||
#ifdef CONFIG_NET_HAVE_SOLINGER
|
||||
struct linger ling;
|
||||
#endif
|
||||
pthread_t child;
|
||||
pthread_attr_t attr;
|
||||
socklen_t addrlen;
|
||||
int listensd;
|
||||
int acceptsd;
|
||||
int ret;
|
||||
|
||||
/* Create a new TCP socket to use to listen for connections */
|
||||
|
||||
listensd = uip_listenon(portno);
|
||||
if (listensd < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/* Begin serving connections */
|
||||
|
||||
for (;;)
|
||||
{
|
||||
/* Accept the next connectin */
|
||||
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
acceptsd = accept(listensd, (struct sockaddr*)&myaddr, &addrlen);
|
||||
if (acceptsd < 0)
|
||||
{
|
||||
ndbg("accept failure: %d\n", errno);
|
||||
break;
|
||||
}
|
||||
|
||||
nvdbg("Connection accepted -- spawning sd=%d\n", acceptsd);
|
||||
|
||||
/* Configure to "linger" until all data is sent when the socket is
|
||||
* closed.
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_NET_HAVE_SOLINGER
|
||||
ling.l_onoff = 1;
|
||||
ling.l_linger = 30; /* timeout is seconds */
|
||||
|
||||
ret = setsockopt(acceptsd, SOL_SOCKET, SO_LINGER, &ling, sizeof(struct linger));
|
||||
if (ret < 0)
|
||||
{
|
||||
close(acceptsd);
|
||||
ndbg("setsockopt SO_LINGER failure: %d\n", errno);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Create a thread to handle the connection. The socket descriptor is
|
||||
* provided in as the single argument to the new thread.
|
||||
*/
|
||||
|
||||
(void)pthread_attr_init(&attr);
|
||||
(void)pthread_attr_setstacksize(&attr, stacksize);
|
||||
|
||||
ret = pthread_create(&child, &attr, handler, (void*)acceptsd);
|
||||
if (ret != 0)
|
||||
{
|
||||
/* Close the connection */
|
||||
|
||||
close(acceptsd);
|
||||
ndbg("pthread_create failed\n");
|
||||
|
||||
if (ret == EAGAIN)
|
||||
{
|
||||
/* Lacked resources to create a new thread. This is a temporary
|
||||
* condition, so we close this peer, but keep serving for
|
||||
* other connections.
|
||||
*/
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Something is very wrong... Break out and stop serving */
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
/* We don't care when/how the child thread exits so detach from it now
|
||||
* in order to avoid memory leaks.
|
||||
*/
|
||||
|
||||
(void)pthread_detach(child);
|
||||
}
|
||||
|
||||
/* Close the listerner socket */
|
||||
|
||||
close(listensd);
|
||||
}
|
||||
116
netutils/netlib/uip_setdraddr.c
Normal file
116
netutils/netlib/uip_setdraddr.c
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
/****************************************************************************
|
||||
* netutils/netlib/uip_setdraddr.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
|
||||
#include <apps/netutils/netlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_setdraddr
|
||||
*
|
||||
* Description:
|
||||
* Set the default router IP address
|
||||
*
|
||||
* Parameters:
|
||||
* ifname The name of the interface to use
|
||||
* ipaddr The address to set
|
||||
*
|
||||
* Return:
|
||||
* 0 on sucess; -1 on failure
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
int uip_setdraddr(const char *ifname, const struct in6_addr *addr)
|
||||
#else
|
||||
int uip_setdraddr(const char *ifname, const struct in_addr *addr)
|
||||
#endif
|
||||
{
|
||||
int ret = ERROR;
|
||||
if (ifname && addr)
|
||||
{
|
||||
int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 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);
|
||||
|
||||
/* 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->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 ret;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */
|
||||
116
netutils/netlib/uip_sethostaddr.c
Normal file
116
netutils/netlib/uip_sethostaddr.c
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
/****************************************************************************
|
||||
* netutils/netlib/uip_sethostaddr.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
|
||||
#include <apps/netutils/netlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_sethostaddr
|
||||
*
|
||||
* Description:
|
||||
* Set the network driver IP address
|
||||
*
|
||||
* Parameters:
|
||||
* ifname The name of the interface to use
|
||||
* ipaddr The address to set
|
||||
*
|
||||
* Return:
|
||||
* 0 on sucess; -1 on failure
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
int uip_sethostaddr(const char *ifname, const struct in6_addr *addr)
|
||||
#else
|
||||
int uip_sethostaddr(const char *ifname, const struct in_addr *addr)
|
||||
#endif
|
||||
{
|
||||
int ret = ERROR;
|
||||
if (ifname && addr)
|
||||
{
|
||||
int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 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);
|
||||
|
||||
/* 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->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 */
|
||||
145
netutils/netlib/uip_setifflag.c
Normal file
145
netutils/netlib/uip_setifflag.c
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
/****************************************************************************
|
||||
* netutils/netlib/uip_setifflag.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011, 2014 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
|
||||
#include <apps/netutils/netlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_ifup
|
||||
*
|
||||
* Description:
|
||||
* Set the network interface UP
|
||||
*
|
||||
* Parameters:
|
||||
* ifname The name of the interface to use
|
||||
*
|
||||
* Return:
|
||||
* 0 on sucess; -1 on failure
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int uip_ifup(const char *ifname)
|
||||
{
|
||||
int ret = ERROR;
|
||||
if (ifname)
|
||||
{
|
||||
/* Get a socket (only so that we get access to the INET subsystem) */
|
||||
|
||||
int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 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 ifup flag */
|
||||
|
||||
req.ifr_flags |= IFF_UP;
|
||||
|
||||
ret = ioctl(sockfd, SIOCSIFFLAGS, (unsigned long)&req);
|
||||
close(sockfd);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_ifdown
|
||||
*
|
||||
* Description:
|
||||
* Set the network interface DOWN
|
||||
*
|
||||
* Parameters:
|
||||
* ifname The name of the interface to use
|
||||
*
|
||||
* Return:
|
||||
* 0 on sucess; -1 on failure
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int uip_ifdown(const char *ifname)
|
||||
{
|
||||
int ret = ERROR;
|
||||
if (ifname)
|
||||
{
|
||||
/* Get a socket (only so that we get access to the INET subsystem) */
|
||||
|
||||
int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 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 ifup flag */
|
||||
|
||||
req.ifr_flags |= IFF_DOWN;
|
||||
|
||||
ret = ioctl(sockfd, SIOCSIFFLAGS, (unsigned long)&req);
|
||||
close(sockfd);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */
|
||||
116
netutils/netlib/uip_setmacaddr.c
Normal file
116
netutils/netlib/uip_setmacaddr.c
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
/****************************************************************************
|
||||
* netutils/netlib/uip_setmacaddr.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
|
||||
#include <apps/netutils/netlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor 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_t *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, NETLIB_SOCK_IOCTL, 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);
|
||||
|
||||
/* Perform the ioctl to set the MAC address */
|
||||
|
||||
ret = ioctl(sockfd, SIOCSIFHWADDR, (unsigned long)&req);
|
||||
close(sockfd);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */
|
||||
115
netutils/netlib/uip_setnetmask.c
Normal file
115
netutils/netlib/uip_setnetmask.c
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
/****************************************************************************
|
||||
* netutils/netlib/uip_setnetmask.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
|
||||
#include <apps/netutils/netlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Global Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_setnetmask
|
||||
*
|
||||
* Description:
|
||||
* Set the netmask
|
||||
*
|
||||
* Parameters:
|
||||
* ifname The name of the interface to use
|
||||
* ipaddr The address to set
|
||||
*
|
||||
* Return:
|
||||
* 0 on sucess; -1 on failure
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
int uip_setnetmask(const char *ifname, const struct in6_addr *addr)
|
||||
#else
|
||||
int uip_setnetmask(const char *ifname, const struct in_addr *addr)
|
||||
#endif
|
||||
{
|
||||
int ret = ERROR;
|
||||
if (ifname && addr)
|
||||
{
|
||||
int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 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);
|
||||
|
||||
/* 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->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 */
|
||||
Loading…
Add table
Add a link
Reference in a new issue