summaryrefslogtreecommitdiff
path: root/nuttx/netutils
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2007-11-30 20:46:29 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2007-11-30 20:46:29 +0000
commit563ced5a52a1260c2165435a103c204c39a658f0 (patch)
tree1690f3ad402aa77528c4cefe28e399d87ed06fb6 /nuttx/netutils
parent31e64b14e31c58b25a8ebe6eb2672ac4d1847c1b (diff)
downloadpx4-nuttx-563ced5a52a1260c2165435a103c204c39a658f0.tar.gz
px4-nuttx-563ced5a52a1260c2165435a103c204c39a658f0.tar.bz2
px4-nuttx-563ced5a52a1260c2165435a103c204c39a658f0.zip
Add examples/dhcpd
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@413 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/netutils')
-rw-r--r--nuttx/netutils/Makefile20
-rw-r--r--nuttx/netutils/dhcpc/dhcpc.c119
-rw-r--r--nuttx/netutils/uiplib/uip-gethostaddr.c3
-rw-r--r--nuttx/netutils/uiplib/uip-getmacaddr.c3
-rw-r--r--nuttx/netutils/uiplib/uip-setdraddr.c3
-rw-r--r--nuttx/netutils/uiplib/uip-sethostaddr.c3
-rw-r--r--nuttx/netutils/uiplib/uip-setmacaddr.c3
-rw-r--r--nuttx/netutils/uiplib/uip-setnetmask.c3
8 files changed, 88 insertions, 69 deletions
diff --git a/nuttx/netutils/Makefile b/nuttx/netutils/Makefile
index 752a6d0b1..0c14a1094 100644
--- a/nuttx/netutils/Makefile
+++ b/nuttx/netutils/Makefile
@@ -47,16 +47,17 @@ include webserver/Make.defs
endif
ifeq ($(CONFIG_NET_UDP),y)
include dhcpc/Make.defs
+include dhcpd/Make.defs
include resolv/Make.defs
endif
endif
-ASRCS = $(UIPLIB_ASRCS) $(DHCPC_ASRCS) $(RESOLV_ASRCS) $(SMTP_ASRCS) \
- $(TELNETD_ASRCS) $(WEBCLIENT_ASRCS) $(WEBSERVER_ASRCS) $(STRNG_ASRCS)
+ASRCS = $(UIPLIB_ASRCS) $(DHCPC_ASRCS) $(DHCPD_ASRCS) $(RESOLV_ASRCS) \
+ $(SMTP_ASRCS) $(TELNETD_ASRCS) $(WEBCLIENT_ASRCS) $(WEBSERVER_ASRCS)
AOBJS = $(ASRCS:.S=$(OBJEXT))
-CSRCS = $(UIPLIB_CSRCS) $(DHCPC_CSRCS) $(RESOLV_CSRCS) $(SMTP_CSRCS) \
- $(TELNETD_CSRCS) $(WEBCLIENT_CSRCS) $(WEBSERVER_CSRCS) $(STRNG_CSRCS)
+CSRCS = $(UIPLIB_CSRCS) $(DHCPC_CSRCS) $(DHCPD_CSRCS) $(RESOLV_CSRCS) \
+ $(SMTP_CSRCS) $(TELNETD_CSRCS) $(WEBCLIENT_CSRCS) $(WEBSERVER_CSRCS)
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
@@ -64,7 +65,7 @@ OBJS = $(AOBJS) $(COBJS)
BIN = libnetutils$(LIBEXT)
-VPATH = uiplib:dhcpc:resolv:smtp:telnetd:webclient:webserver
+VPATH = uiplib:dhcpc:dhcpd:resolv:smtp:telnetd:webclient:webserver
all: $(BIN)
@@ -82,8 +83,9 @@ $(BIN): $(OBJS)
.depend: Makefile $(SRCS)
ifeq ($(CONFIG_NET),y)
- @$(MKDEP) --dep-path . --dep-path uiplib --dep-path dhcpc --dep-path smtp --dep-path webclient \
- --dep-path resolv --dep-path telnetd --dep-path webserver \
+ @$(MKDEP) --dep-path . --dep-path uiplib --dep-path dhcpc --dep-path dhcpd \
+ --dep-path smtp --dep-path webclient --dep-path resolv \
+ --dep-path telnetd --dep-path webserver \
$(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
endif
@touch $@
@@ -92,11 +94,11 @@ depend: .depend
clean:
@rm -f $(BIN) *.o *.rel *.asm *.lst *.sym *.adb *~
- @rm -f uiplib/*~ dhcpc/*~ resolv/*~ smtp/*~ telnetd/*~ webclient/*~ webserver/*~
+ @rm -f uiplib/*~ dhcpc/*~ dhcpd/*~ resolv/*~ smtp/*~ telnetd/*~ webclient/*~ webserver/*~
@if [ ! -z "$(OBJEXT)" ]; then rm -f *$(OBJEXT); fi
+ @$(MAKE) -C dhcpd -f Makefile.host clean
distclean: clean
@rm -f Make.dep .depend
- @rm -f $(STRNG_CSRCS) $(STRNG_ASRCS)
-include Make.dep
diff --git a/nuttx/netutils/dhcpc/dhcpc.c b/nuttx/netutils/dhcpc/dhcpc.c
index bdb885ccc..6b7bc9535 100644
--- a/nuttx/netutils/dhcpc/dhcpc.c
+++ b/nuttx/netutils/dhcpc/dhcpc.c
@@ -100,15 +100,6 @@
* Private Types
****************************************************************************/
-struct dhcpc_state_internal
-{
- struct uip_udp_conn *conn;
- const void *mac_addr;
- int mac_len;
- int sockfd;
- char buffer[256];
-};
-
struct dhcp_msg
{
uint8 op;
@@ -130,6 +121,15 @@ struct dhcp_msg
uint8 options[312];
};
+struct dhcpc_state_s
+{
+ struct uip_udp_conn *ds_conn;
+ const void *ds_macaddr;
+ int ds_maclen;
+ int sockfd;
+ struct dhcp_msg packet;
+};
+
/****************************************************************************
* Private Data
****************************************************************************/
@@ -181,74 +181,66 @@ static uint8 *add_end(uint8 *optptr)
return optptr;
}
-static void create_msg(struct dhcpc_state_internal *pdhcpc, struct dhcp_msg *pmsg)
+static void create_msg(struct dhcpc_state_s *pdhcpc)
{
struct in_addr addr;
- pmsg->op = DHCP_REQUEST;
- pmsg->htype = DHCP_HTYPE_ETHERNET;
- pmsg->hlen = pdhcpc->mac_len;
- pmsg->hops = 0;
- memcpy(pmsg->xid, xid, sizeof(pmsg->xid));
- pmsg->secs = 0;
- pmsg->flags = HTONS(BOOTP_BROADCAST); /* Broadcast bit. */
+ memset(&pdhcpc->packet, 0, sizeof(struct dhcp_msg));
+ pdhcpc->packet.op = DHCP_REQUEST;
+ pdhcpc->packet.htype = DHCP_HTYPE_ETHERNET;
+ pdhcpc->packet.hlen = pdhcpc->ds_maclen;
+ memcpy(pdhcpc->packet.xid, xid, 4);
+ pdhcpc->packet.flags = HTONS(BOOTP_BROADCAST); /* Broadcast bit. */
uip_gethostaddr("eth0", &addr);
- memcpy(&pmsg->ciaddr, &addr.s_addr, sizeof(pmsg->ciaddr));
- memset(pmsg->yiaddr, 0, sizeof(pmsg->yiaddr));
- memset(pmsg->siaddr, 0, sizeof(pmsg->siaddr));
- memset(pmsg->giaddr, 0, sizeof(pmsg->giaddr));
-
- memcpy(pmsg->chaddr, pdhcpc->mac_addr, pdhcpc->mac_len);
- memset(&pmsg->chaddr[pdhcpc->mac_len], 0, sizeof(pmsg->chaddr) - pdhcpc->mac_len);
-#ifndef CONFIG_NET_DHCP_LIGHT
- memset(pmsg->sname, 0, sizeof(pmsg->sname));
- memset(pmsg->file, 0, sizeof(pmsg->file));
-#endif
+ memcpy(&pdhcpc->packet.ciaddr, &addr.s_addr, 4);
- memcpy(pmsg->options, magic_cookie, sizeof(magic_cookie));
+ memcpy(pdhcpc->packet.chaddr, pdhcpc->ds_macaddr, pdhcpc->ds_maclen);
+ memset(&pdhcpc->packet.chaddr[pdhcpc->ds_maclen], 0, 16 - pdhcpc->ds_maclen);
+ memcpy(pdhcpc->packet.options, magic_cookie, sizeof(magic_cookie));
}
-static int send_discover(struct dhcpc_state_internal *pdhcpc)
+static int send_discover(struct dhcpc_state_s *pdhcpc)
{
- struct dhcp_msg msg;
struct sockaddr_in addr;
uint8 *pend;
int len;
- create_msg(pdhcpc, &msg);
- pend = add_msg_type(&msg.options[4], DHCPDISCOVER);
+dbg("Calling create_msg\n");
+ create_msg(pdhcpc);
+ pend = &pdhcpc->packet.options[4];
+ pend = add_msg_type(pend, DHCPDISCOVER);
pend = add_req_options(pend);
pend = add_end(pend);
- len = pend - (uint8*)&msg;
+ len = pend - (uint8*)&pdhcpc->packet;
addr.sin_family = AF_INET;
addr.sin_port = HTONS(DHCPC_SERVER_PORT);
addr.sin_addr.s_addr = INADDR_BROADCAST;
- return sendto(pdhcpc->sockfd, &msg, len, 0,
+dbg("Calling sendto, len=%d\n", len);
+ return sendto(pdhcpc->sockfd, &pdhcpc->packet, len, 0,
(struct sockaddr*)&addr, sizeof(struct sockaddr_in));
}
-
-static int send_request(struct dhcpc_state_internal *pdhcpc, struct dhcpc_state *presult)
+static int send_request(struct dhcpc_state_s *pdhcpc, struct dhcpc_state *presult)
{
- struct dhcp_msg msg;
struct sockaddr_in addr;
uint8 *pend;
int len;
- create_msg(pdhcpc, &msg);
- pend = add_msg_type(&msg.options[4], DHCPREQUEST);
+ create_msg(pdhcpc);
+ pend = &pdhcpc->packet.options[4];
+ pend = add_msg_type(pend, DHCPREQUEST);
pend = add_server_id(presult, pend);
pend = add_req_ipaddr(presult, pend);
pend = add_end(pend);
- len = pend - (uint8*)&msg;
+ len = pend - (uint8*)&pdhcpc->packet;
addr.sin_family = AF_INET;
addr.sin_port = HTONS(DHCPC_SERVER_PORT);
addr.sin_addr.s_addr = INADDR_BROADCAST;
- return sendto(pdhcpc->sockfd, &msg, len, 0,
+ return sendto(pdhcpc->sockfd, &pdhcpc->packet, len, 0,
(struct sockaddr*)&addr, sizeof(struct sockaddr_in));
}
@@ -288,16 +280,15 @@ static uint8 parse_options(struct dhcpc_state *presult, uint8 *optptr, int len)
return type;
}
-static uint8 parse_msg(struct dhcpc_state_internal *pdhcpc, int buflen, struct dhcpc_state *presult)
+static uint8 parse_msg(struct dhcpc_state_s *pdhcpc, int buflen,
+ struct dhcpc_state *presult)
{
- struct dhcp_msg *pbuffer = (struct dhcp_msg *)pdhcpc->buffer;
-
- if (pbuffer->op == DHCP_REPLY &&
- memcmp(pbuffer->xid, xid, sizeof(xid)) == 0 &&
- memcmp(pbuffer->chaddr, pdhcpc->mac_addr, pdhcpc->mac_len) == 0)
+ if (pdhcpc->packet.op == DHCP_REPLY &&
+ memcmp(pdhcpc->packet.xid, xid, sizeof(xid)) == 0 &&
+ memcmp(pdhcpc->packet.chaddr, pdhcpc->ds_macaddr, pdhcpc->ds_maclen) == 0)
{
- memcpy(&presult->ipaddr.s_addr, pbuffer->yiaddr, 4);
- return parse_options(presult, &pbuffer->options[4], buflen);
+ memcpy(&presult->ipaddr.s_addr, pdhcpc->packet.yiaddr, 4);
+ return parse_options(presult, &pdhcpc->packet.options[4], buflen);
}
return 0;
}
@@ -306,22 +297,22 @@ static uint8 parse_msg(struct dhcpc_state_internal *pdhcpc, int buflen, struct d
* Global Functions
****************************************************************************/
-void *dhcpc_open(const void *mac_addr, int mac_len)
+void *dhcpc_open(const void *macaddr, int maclen)
{
- struct dhcpc_state_internal *pdhcpc;
+ struct dhcpc_state_s *pdhcpc;
struct sockaddr_in addr;
struct timeval tv;
/* Allocate an internal DHCP structure */
- pdhcpc = (struct dhcpc_state_internal *)malloc(sizeof(struct dhcpc_state_internal));
+ pdhcpc = (struct dhcpc_state_s *)malloc(sizeof(struct dhcpc_state_s));
if (pdhcpc)
{
/* Initialize the allocated structure */
- memset(pdhcpc, 0, sizeof(struct dhcpc_state_internal));
- pdhcpc->mac_addr = mac_addr;
- pdhcpc->mac_len = mac_len;
+ memset(pdhcpc, 0, sizeof(struct dhcpc_state_s));
+ pdhcpc->ds_macaddr = macaddr;
+ pdhcpc->ds_maclen = maclen;
/* Create a UDP socket */
@@ -357,6 +348,7 @@ void *dhcpc_open(const void *mac_addr, int mac_len)
}
}
+ dbg("Return %p\n", pdhcpc);
return (void*)pdhcpc;
}
@@ -371,12 +363,13 @@ void dhcpc_close(void *handle)
int dhcpc_request(void *handle, struct dhcpc_state *presult)
{
- struct dhcpc_state_internal *pdhcpc = (struct dhcpc_state_internal *)handle;
+ struct dhcpc_state_s *pdhcpc = (struct dhcpc_state_s *)handle;
ssize_t result;
int state;
/* Loop until we receive the offer */
+ dbg("Handle %p\n", handle);
do
{
state = STATE_SENDING;
@@ -385,18 +378,22 @@ int dhcpc_request(void *handle, struct dhcpc_state *presult)
{
/* Send the command */
+ dbg("Send DHCPDISCOVER, @4=%08x\n", *(uint32*)4);
if (send_discover(pdhcpc) < 0)
{
return ERROR;
}
/* Get the response */
-
- result = recv(pdhcpc->sockfd, pdhcpc->buffer, BUFFER_SIZE, 0);
+dbg("Sent DHCPDISCOVER\n");
+ result = recv(pdhcpc->sockfd, &pdhcpc->packet, sizeof(struct dhcp_msg), 0);
+dbg("recv returned %d\n");
if (result >= 0)
{
+dbg("Calling parse_msg\n");
if (parse_msg(pdhcpc, result, presult) == DHCPOFFER)
{
+ dbg("Received DHCPOFFER\n");
state = STATE_OFFER_RECEIVED;
}
}
@@ -413,6 +410,7 @@ int dhcpc_request(void *handle, struct dhcpc_state *presult)
{
/* Send the request */
+ dbg("Send DHCPREQUEST\n");
if (send_request(pdhcpc, presult) < 0)
{
return ERROR;
@@ -420,11 +418,12 @@ int dhcpc_request(void *handle, struct dhcpc_state *presult)
/* Get the response */
- result = recv(pdhcpc->sockfd, pdhcpc->buffer, BUFFER_SIZE, 0);
+ result = recv(pdhcpc->sockfd, &pdhcpc->packet, sizeof(struct dhcp_msg), 0);
if (result >= 0)
{
if (parse_msg(pdhcpc, result, presult) == DHCPACK)
{
+ dbg("Received ACK\n");
state = STATE_CONFIG_RECEIVED;
}
}
diff --git a/nuttx/netutils/uiplib/uip-gethostaddr.c b/nuttx/netutils/uiplib/uip-gethostaddr.c
index 732721b10..d20344d8c 100644
--- a/nuttx/netutils/uiplib/uip-gethostaddr.c
+++ b/nuttx/netutils/uiplib/uip-gethostaddr.c
@@ -43,9 +43,12 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
+
#include <string.h>
#include <errno.h>
+
#include <netinet/in.h>
+#include <net/if.h>
#include <net/uip/uip-lib.h>
diff --git a/nuttx/netutils/uiplib/uip-getmacaddr.c b/nuttx/netutils/uiplib/uip-getmacaddr.c
index 9e7a368ab..c45072c86 100644
--- a/nuttx/netutils/uiplib/uip-getmacaddr.c
+++ b/nuttx/netutils/uiplib/uip-getmacaddr.c
@@ -43,9 +43,12 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
+
#include <string.h>
#include <errno.h>
+
#include <netinet/in.h>
+#include <net/if.h>
#include <net/uip/uip-lib.h>
diff --git a/nuttx/netutils/uiplib/uip-setdraddr.c b/nuttx/netutils/uiplib/uip-setdraddr.c
index e8ab142ec..6d61f2d5d 100644
--- a/nuttx/netutils/uiplib/uip-setdraddr.c
+++ b/nuttx/netutils/uiplib/uip-setdraddr.c
@@ -43,10 +43,13 @@
#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/if.h>
#include <net/uip/uip-lib.h>
diff --git a/nuttx/netutils/uiplib/uip-sethostaddr.c b/nuttx/netutils/uiplib/uip-sethostaddr.c
index 6247184ba..3f3cd7a6d 100644
--- a/nuttx/netutils/uiplib/uip-sethostaddr.c
+++ b/nuttx/netutils/uiplib/uip-sethostaddr.c
@@ -43,10 +43,13 @@
#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/if.h>
#include <net/uip/uip-lib.h>
diff --git a/nuttx/netutils/uiplib/uip-setmacaddr.c b/nuttx/netutils/uiplib/uip-setmacaddr.c
index 4b1e67530..969238362 100644
--- a/nuttx/netutils/uiplib/uip-setmacaddr.c
+++ b/nuttx/netutils/uiplib/uip-setmacaddr.c
@@ -43,10 +43,13 @@
#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/if.h>
#include <net/uip/uip-lib.h>
diff --git a/nuttx/netutils/uiplib/uip-setnetmask.c b/nuttx/netutils/uiplib/uip-setnetmask.c
index ff70e5a37..619a5e8fa 100644
--- a/nuttx/netutils/uiplib/uip-setnetmask.c
+++ b/nuttx/netutils/uiplib/uip-setnetmask.c
@@ -43,10 +43,13 @@
#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/if.h>
#include <net/uip/uip-lib.h>