diff --git a/Makefile b/Makefile index 7f95c5cd3..d81606a3f 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ APPDIR = ${shell pwd} # SUBDIRS is the list of all directories containing Makefiles. It is used # only for cleaning. -SUBDIRS = nshlib vsn +SUBDIRS = nshlib netutils vsn # we use a non-existing .built_always to guarantee that Makefile # always walks into the sub-directories and asks for build diff --git a/README.txt b/README.txt index 86758dddf..b80393a03 100644 --- a/README.txt +++ b/README.txt @@ -52,6 +52,7 @@ will call: Application skeleton can be found under the hello sub-directory, which shows how an application can be added to the project. One must define: + 1. create sub-directory as: appname 2. provide entry point: appname_main() 3. set the requirements in the file: Makefile, specially the lines: @@ -62,3 +63,6 @@ define: CSRCS = C source file list as foo1.c foo2.c .. 4. add application in the apps/Makefile + + + diff --git a/netutils/Makefile b/netutils/Makefile new file mode 100644 index 000000000..3c3f11c43 --- /dev/null +++ b/netutils/Makefile @@ -0,0 +1,62 @@ +############################################################################ +# apps/netutils/Makefile +# +# Copyright (C) 2011 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +-include $(TOPDIR)/.config # Current configuration + +# Sub-directories + +ifeq ($(CONFIG_NET),y) +SUBDIRS = uiplib dhcpc dhcpd resolv smtp telnetd webclient webserver tftpc thttpd +endif + +all: nothing +.PHONY: nothing depend clean distclean + +nothing: + +define DOMAKE + @(MAKE) -C $1 $2 TOPDIR="$(TOPDIR) APPDIR=$(APPDIR)" +endef + +depend: + $(foreach DIR, $(SUBDIRS), $(eval $(call DOMAKE,$(DIR),depend))) + +clean: + $(foreach DIR, $(SUBDIRS), $(eval $(call DOMAKE,$(DIR),clean))) + +distclean: clean + $(foreach DIR, $(SUBDIRS), $(eval $(call DOMAKE,$(DIR),distclean))) + +-include Make.dep diff --git a/netutils/README.txt b/netutils/README.txt new file mode 100644 index 000000000..e4b05eb21 --- /dev/null +++ b/netutils/README.txt @@ -0,0 +1,39 @@ +netutils +^^^^^^^^ + +This directory contains most of the network applications contained +under the uIP-1.0 apps directory. As the uIP apps/README says, +these applications "are not all heavily tested." These uIP apps +include: + + dhcpc - Dynamic Host Configuration Protocol (DHCP) client + resolv - uIP DNS resolver + smtp - Simple Mail Transfer Protocol (SMTP) client + telnetd - TELNET server + webclient - HTTP web client + webserver - HTTP web server + +You may find additional information on these apps in the uIP forum +accessible through: http://www.sics.se/~adam/uip/index.php/Main_Page + +Additional applications that were not part of uIP (but which are +highly influenced by uIP) include: + + dhcpd - Dynamic Host Configuration Protocol (DHCP) server + tftpc - TFTP client + thttpd - This is a port of Jef Poskanzer's THTTPD HTPPD server. + See http://acme.com/software/thttpd/. + +If you use DHCPC/D, then some special configuration network options are +required. These include: + + CONFIG_NET=y Of course + CONFIG_NSOCKET_DESCRIPTORS And, of course, you must allocate some + socket descriptors. + CONFIG_NET_UDP=y UDP support is required for DHCP + (as well as various other UDP-related + configuration settings). + CONFIG_NET_BROADCAST=y UDP broadcast support is needed. + CONFIG_NET_BUFSIZE=650 The client must be prepared to receive + (or larger) DHCP messages of up to 576 bytes (excluding + Ethernet, IP, or UDP headers and FCS). diff --git a/netutils/dhcpc/Makefile b/netutils/dhcpc/Makefile new file mode 100644 index 000000000..7209907de --- /dev/null +++ b/netutils/dhcpc/Makefile @@ -0,0 +1,94 @@ +############################################################################ +# apps/netutils/dhcpc/Makefile +# +# Copyright (C) 2011 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +-include $(TOPDIR)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +# DHCP Client Library + +ASRCS = +CSRCS = + +ifeq ($(CONFIG_NET_UDP),y) +CSRCS += dhcpc.c +endif + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) +OBJS = $(AOBJS) $(COBJS) + +BIN = ../../libapps$(LIBEXT) + +ROOTDEPPATH = --dep-path . + +# Common build + +VPATH = + +all: .built + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +$(BIN): $(OBJS) + @( for obj in $(OBJS) ; do \ + $(call ARCHIVE, $@, $${obj}); \ + done ; ) + @touch .built + +.built: $(BIN) + +.depend: Makefile $(SRCS) + @$(MKDEP) $(ROOTDEPPATH) \ + $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep + @touch $@ + +# Register application +depend: .depend + +clean: + @rm -f $(BIN) *.o *~ .*.swp .built + $(call CLEAN) + +distclean: clean + @rm -f Make.dep .depend + +-include Make.dep diff --git a/netutils/dhcpc/dhcpc.c b/netutils/dhcpc/dhcpc.c new file mode 100644 index 000000000..dc80988bb --- /dev/null +++ b/netutils/dhcpc/dhcpc.c @@ -0,0 +1,607 @@ +/**************************************************************************** + * netutils/dhcpc/dhcpc.c + * + * Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Based heavily on portions of uIP: + * + * Author: Adam Dunkels + * Copyright (c) 2005, 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. Neither the name of the Institute 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 INSTITUTE 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 INSTITUTE 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 +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +#define STATE_INITIAL 0 +#define STATE_HAVE_OFFER 1 +#define STATE_HAVE_LEASE 2 + +#define BOOTP_BROADCAST 0x8000 + +#define DHCP_REQUEST 1 +#define DHCP_REPLY 2 +#define DHCP_HTYPE_ETHERNET 1 +#define DHCP_HLEN_ETHERNET 6 +#define DHCP_MSG_LEN 236 + +#define DHCPC_SERVER_PORT 67 +#define DHCPC_CLIENT_PORT 68 + +#define DHCPDISCOVER 1 +#define DHCPOFFER 2 +#define DHCPREQUEST 3 +#define DHCPDECLINE 4 +#define DHCPACK 5 +#define DHCPNAK 6 +#define DHCPRELEASE 7 + +#define DHCP_OPTION_SUBNET_MASK 1 +#define DHCP_OPTION_ROUTER 3 +#define DHCP_OPTION_DNS_SERVER 6 +#define DHCP_OPTION_REQ_IPADDR 50 +#define DHCP_OPTION_LEASE_TIME 51 +#define DHCP_OPTION_MSG_TYPE 53 +#define DHCP_OPTION_SERVER_ID 54 +#define DHCP_OPTION_REQ_LIST 55 +#define DHCP_OPTION_END 255 + +#define BUFFER_SIZE 256 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct dhcp_msg +{ + uint8_t op; + uint8_t htype; + uint8_t hlen; + uint8_t hops; + uint8_t xid[4]; + uint16_t secs; + uint16_t flags; + uint8_t ciaddr[4]; + uint8_t yiaddr[4]; + uint8_t siaddr[4]; + uint8_t giaddr[4]; + uint8_t chaddr[16]; +#ifndef CONFIG_NET_DHCP_LIGHT + uint8_t sname[64]; + uint8_t file[128]; +#endif + uint8_t options[312]; +}; + +struct dhcpc_state_s +{ + struct uip_udp_conn *ds_conn; + const void *ds_macaddr; + int ds_maclen; + int sockfd; + struct in_addr ipaddr; + struct in_addr serverid; + struct dhcp_msg packet; +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const uint8_t xid[4] = {0xad, 0xde, 0x12, 0x23}; +static const uint8_t magic_cookie[4] = {99, 130, 83, 99}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: dhcpc_add