mirror of
https://github.com/apache/nuttx.git
synced 2026-06-06 08:36:24 +08:00
Import of uIP 1.0
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@308 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
############################################################
|
||||
# Makefile
|
||||
#
|
||||
# Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions
|
||||
# are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in
|
||||
# the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# 3. Neither the name 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.
|
||||
#
|
||||
############################################################
|
||||
|
||||
-include $(TOPDIR)/Make.defs
|
||||
|
||||
MKDEP = $(TOPDIR)/tools/mkdeps.sh
|
||||
|
||||
ifeq ($(CONFIG_NET_UIP),y)
|
||||
include uip/Make.defs
|
||||
endif
|
||||
|
||||
ASRCS = $(UIP_ASRCS)
|
||||
AOBJS = $(ASRCS:.S=$(OBJEXT))
|
||||
|
||||
CSRCS = $(UIP_CSRCS)
|
||||
COBJS = $(CSRCS:.c=$(OBJEXT))
|
||||
|
||||
SRCS = $(ASRCS) $(CSRCS)
|
||||
OBJS = $(AOBJS) $(COBJS)
|
||||
|
||||
BIN = libnet$(LIBEXT)
|
||||
|
||||
VPATH = uip
|
||||
|
||||
all: $(BIN)
|
||||
|
||||
$(AOBJS): %$(OBJEXT): %.S
|
||||
$(CC) -c $(CFLAGS) -D__ASSEMBLY__ $< -o $@
|
||||
|
||||
$(COBJS): %$(OBJEXT): %.c
|
||||
$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
$(BIN): $(OBJS)
|
||||
( for obj in $(OBJS) ; do \
|
||||
$(AR) $@ $${obj} || \
|
||||
{ echo "$(AR) $@ $obj FAILED!" ; exit 1 ; } ; \
|
||||
done ; )
|
||||
|
||||
.depend: Makefile $(SRCS)
|
||||
ifeq ($(CONFIG_NET_UIP),y)
|
||||
$(MKDEP) --dep-path uip $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
|
||||
endif
|
||||
touch $@
|
||||
|
||||
depend: .depend
|
||||
|
||||
clean:
|
||||
rm -f $(BIN) *.o *.rel *.asm *.lst *.sym *.adb *~
|
||||
if [ ! -z "$(OBJEXT)" ]; then rm -f *$(OBJEXT); fi
|
||||
|
||||
distclean: clean
|
||||
rm -f Make.dep .depend
|
||||
|
||||
-include Make.dep
|
||||
Binary file not shown.
@@ -0,0 +1,38 @@
|
||||
############################################################################
|
||||
# Make.defs
|
||||
#
|
||||
# Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions
|
||||
# are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in
|
||||
# the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# 3. Neither the name 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
UIP_ASRCS =
|
||||
UIP_CSRCS = psock.c uip-arp.c uip.c uip-fw.c uip-neighbor.c uip-split.c uip-wait.c
|
||||
|
||||
+377
@@ -0,0 +1,377 @@
|
||||
/*
|
||||
* Copyright (c) 2004, 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.
|
||||
*
|
||||
* This file is part of the uIP TCP/IP stack
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
* $Id: psock.c,v 1.1.1.1 2007-08-26 23:04:11 patacongo Exp $
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <net/uip/uip.h>
|
||||
#include <net/uip/uipopt.h>
|
||||
#include <net/uip/psock.h>
|
||||
|
||||
#define STATE_NONE 0
|
||||
#define STATE_ACKED 1
|
||||
#define STATE_READ 2
|
||||
#define STATE_BLOCKED_NEWDATA 3
|
||||
#define STATE_BLOCKED_CLOSE 4
|
||||
#define STATE_BLOCKED_SEND 5
|
||||
#define STATE_DATA_SENT 6
|
||||
|
||||
/*
|
||||
* Return value of the buffering functions that indicates that a
|
||||
* buffer was not filled by incoming data.
|
||||
*
|
||||
*/
|
||||
#define BUF_NOT_FULL 0
|
||||
#define BUF_NOT_FOUND 0
|
||||
|
||||
/*
|
||||
* Return value of the buffering functions that indicates that a
|
||||
* buffer was completely filled by incoming data.
|
||||
*
|
||||
*/
|
||||
#define BUF_FULL 1
|
||||
|
||||
/*
|
||||
* Return value of the buffering functions that indicates that an
|
||||
* end-marker byte was found.
|
||||
*
|
||||
*/
|
||||
#define BUF_FOUND 2
|
||||
|
||||
static void buf_setup(struct psock_buf *buf, uint8 *bufptr, uint16 bufsize)
|
||||
{
|
||||
buf->ptr = bufptr;
|
||||
buf->left = bufsize;
|
||||
}
|
||||
|
||||
static uint8 buf_bufdata(struct psock_buf *buf, uint16 len, uint8 **dataptr, uint16 *datalen)
|
||||
{
|
||||
if (*datalen < buf->left)
|
||||
{
|
||||
memcpy(buf->ptr, *dataptr, *datalen);
|
||||
buf->ptr += *datalen;
|
||||
buf->left -= *datalen;
|
||||
*dataptr += *datalen;
|
||||
*datalen = 0;
|
||||
return BUF_NOT_FULL;
|
||||
}
|
||||
else if (*datalen == buf->left)
|
||||
{
|
||||
memcpy(buf->ptr, *dataptr, *datalen);
|
||||
buf->ptr += *datalen;
|
||||
buf->left = 0;
|
||||
*dataptr += *datalen;
|
||||
*datalen = 0;
|
||||
return BUF_FULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(buf->ptr, *dataptr, buf->left);
|
||||
buf->ptr += buf->left;
|
||||
*datalen -= buf->left;
|
||||
*dataptr += buf->left;
|
||||
buf->left = 0;
|
||||
return BUF_FULL;
|
||||
}
|
||||
}
|
||||
|
||||
static uint8 buf_bufto(struct psock_buf *buf, uint8 endmarker, uint8 **dataptr, uint16 *datalen)
|
||||
{
|
||||
uint8 c;
|
||||
while(buf->left > 0 && *datalen > 0)
|
||||
{
|
||||
c = *buf->ptr = **dataptr;
|
||||
++*dataptr;
|
||||
++buf->ptr;
|
||||
--*datalen;
|
||||
--buf->left;
|
||||
|
||||
if (c == endmarker)
|
||||
{
|
||||
return BUF_FOUND;
|
||||
}
|
||||
}
|
||||
|
||||
if (*datalen == 0)
|
||||
{
|
||||
return BUF_NOT_FOUND;
|
||||
}
|
||||
|
||||
while(*datalen > 0)
|
||||
{
|
||||
c = **dataptr;
|
||||
--*datalen;
|
||||
++*dataptr;
|
||||
|
||||
if (c == endmarker)
|
||||
{
|
||||
return BUF_FOUND | BUF_FULL;
|
||||
}
|
||||
}
|
||||
|
||||
return BUF_FULL;
|
||||
}
|
||||
|
||||
static boolean send_data(register struct psock *s)
|
||||
{
|
||||
/* Inidicate that we are blocked waiting for the send to complete */
|
||||
|
||||
s->state = STATE_BLOCKED_SEND;
|
||||
|
||||
/* Loop until we successfully send the data */
|
||||
|
||||
for (;;)
|
||||
{
|
||||
/* If the data has not been sent OR if it needs to be retransmitted,
|
||||
* then send it now.
|
||||
*/
|
||||
|
||||
if (s->state != STATE_DATA_SENT || uip_rexmit())
|
||||
{
|
||||
if (s->sendlen > uip_mss())
|
||||
{
|
||||
uip_send(s->sendptr, uip_mss());
|
||||
}
|
||||
else
|
||||
{
|
||||
uip_send(s->sendptr, s->sendlen);
|
||||
}
|
||||
|
||||
s->state = STATE_DATA_SENT;
|
||||
}
|
||||
|
||||
/* Check if all data has been sent and acknowledged */
|
||||
|
||||
if (s->state == STATE_DATA_SENT && uip_acked())
|
||||
{
|
||||
/* Yes.. the data has been sent AND acknowledge */
|
||||
|
||||
if (s->sendlen > uip_mss())
|
||||
{
|
||||
s->sendlen -= uip_mss();
|
||||
s->sendptr += uip_mss();
|
||||
}
|
||||
else
|
||||
{
|
||||
s->sendptr += s->sendlen;
|
||||
s->sendlen = 0;
|
||||
}
|
||||
|
||||
s->state = STATE_ACKED;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* No.. then wait on the retransmit or acked events */
|
||||
|
||||
(void)uip_event_wait(UIP_ACKDATA|UIP_REXMIT);
|
||||
}
|
||||
|
||||
return FALSE; /* We never get here */
|
||||
}
|
||||
|
||||
void psock_send(struct psock *s, const char *buf, unsigned int len)
|
||||
{
|
||||
/* If there is no data to send, we exit immediately. */
|
||||
|
||||
if (len > 0)
|
||||
{
|
||||
/* Save the length of and a pointer to the data that is to be sent. */
|
||||
|
||||
s->sendptr = (const uint8*)buf;
|
||||
s->sendlen = len;
|
||||
s->state = STATE_NONE;
|
||||
|
||||
/* Loop here until all data is sent. The s->sendlen variable is updated
|
||||
* by the data_sent() function.
|
||||
*/
|
||||
|
||||
while(s->sendlen > 0) {
|
||||
|
||||
/* Wait until the data has been sent and acknowledged */
|
||||
|
||||
send_data(s);
|
||||
}
|
||||
|
||||
/* Done */
|
||||
|
||||
s->state = STATE_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
void psock_generator_send(register struct psock *s, unsigned short (*generate)(void *), void *arg)
|
||||
{
|
||||
/* Ensure that there is a generator function to call. */
|
||||
|
||||
if (generate != NULL)
|
||||
{
|
||||
/* Call the generator function to generate the data in the uip_appdata
|
||||
* buffer.
|
||||
*/
|
||||
|
||||
s->sendlen = generate(arg);
|
||||
s->sendptr = uip_appdata;
|
||||
s->state = STATE_NONE;
|
||||
|
||||
do
|
||||
{
|
||||
/* Call the generator function again if we are called to perform a
|
||||
* retransmission.
|
||||
*/
|
||||
|
||||
if (uip_rexmit())
|
||||
{
|
||||
generate(arg);
|
||||
}
|
||||
|
||||
/* Wait until all data is sent and acknowledged. */
|
||||
|
||||
send_data(s);
|
||||
}
|
||||
while(s->sendlen > 0);
|
||||
|
||||
/* Done */
|
||||
|
||||
s->state = STATE_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
uint16 psock_datalen(struct psock *psock)
|
||||
{
|
||||
return psock->bufsize - psock->buf.left;
|
||||
}
|
||||
|
||||
boolean psock_checknewdata(struct psock *s)
|
||||
{
|
||||
if (s->readlen > 0)
|
||||
{
|
||||
/* There is data in the uip_appdata buffer that has not yet been read
|
||||
* with the PSOCK_READ functions.
|
||||
*/
|
||||
return TRUE;
|
||||
}
|
||||
else if (s->state == STATE_READ)
|
||||
{
|
||||
/* All data in uip_appdata buffer already consumed. */
|
||||
|
||||
s->state = STATE_BLOCKED_NEWDATA;
|
||||
return FALSE;
|
||||
}
|
||||
else if (uip_newdata())
|
||||
{
|
||||
/* There is new data that has not been consumed. */
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* There is no new data. */
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
void psock_waitnewdata(struct psock *s)
|
||||
{
|
||||
while (!psock_checknewdata(s))
|
||||
{
|
||||
uip_event_wait(UIP_NEWDATA);
|
||||
}
|
||||
}
|
||||
|
||||
void psock_readto(register struct psock *psock, unsigned char c)
|
||||
{
|
||||
restart:
|
||||
buf_setup(&psock->buf, psock->bufptr, psock->bufsize);
|
||||
|
||||
/* XXX: Should add buf_checkmarker() before do{} loop, if
|
||||
incoming data has been handled while waiting for a write. */
|
||||
|
||||
do
|
||||
{
|
||||
if (psock->readlen == 0)
|
||||
{
|
||||
psock_waitnewdata(psock);
|
||||
psock->state = STATE_READ;
|
||||
psock->readptr = (uint8 *)uip_appdata;
|
||||
psock->readlen = uip_datalen();
|
||||
}
|
||||
}
|
||||
while((buf_bufto(&psock->buf, c, &psock->readptr, &psock->readlen) & BUF_FOUND) == 0);
|
||||
|
||||
if (psock_datalen(psock) == 0)
|
||||
{
|
||||
psock->state = STATE_NONE;
|
||||
goto restart;
|
||||
}
|
||||
}
|
||||
|
||||
void psock_readbuf(register struct psock *psock)
|
||||
{
|
||||
restart:
|
||||
buf_setup(&psock->buf, psock->bufptr, psock->bufsize);
|
||||
|
||||
/* XXX: Should add buf_checkmarker() before do{} loop, if
|
||||
incoming data has been handled while waiting for a write. */
|
||||
|
||||
do
|
||||
{
|
||||
if (psock->readlen == 0)
|
||||
{
|
||||
psock_waitnewdata(psock);
|
||||
dbg("Waited for newdata\n");
|
||||
psock->state = STATE_READ;
|
||||
psock->readptr = (uint8 *)uip_appdata;
|
||||
psock->readlen = uip_datalen();
|
||||
}
|
||||
}
|
||||
while(buf_bufdata(&psock->buf, psock->bufsize, &psock->readptr, &psock->readlen) != BUF_FULL);
|
||||
|
||||
if (psock_datalen(psock) == 0)
|
||||
{
|
||||
psock->state = STATE_NONE;
|
||||
goto restart;
|
||||
}
|
||||
}
|
||||
|
||||
void psock_init(register struct psock *psock, char *buffer, unsigned int buffersize)
|
||||
{
|
||||
psock->state = STATE_NONE;
|
||||
psock->readlen = 0;
|
||||
psock->bufptr = (uint8*)buffer;
|
||||
psock->bufsize = buffersize;
|
||||
buf_setup(&psock->buf, (uint8*)buffer, buffersize);
|
||||
}
|
||||
@@ -0,0 +1,405 @@
|
||||
/* uip-arp.c
|
||||
* Implementation of the ARP Address Resolution Protocol.
|
||||
* Author: Adam Dunkels <adam@dunkels.com>
|
||||
*
|
||||
* The Address Resolution Protocol ARP is used for mapping between IP
|
||||
* addresses and link level addresses such as the Ethernet MAC
|
||||
* addresses. ARP uses broadcast queries to ask for the link level
|
||||
* address of a known IP address and the host which is configured with
|
||||
* the IP address for which the query was meant, will respond with its
|
||||
* link level address.
|
||||
*
|
||||
* Note: This ARP implementation only supports Ethernet.
|
||||
*
|
||||
* Copyright (c) 2001-2003, Adam Dunkels.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote
|
||||
* products derived from this software without specific prior
|
||||
* written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
#include <net/uip/uip-arp.h>
|
||||
|
||||
struct arp_hdr
|
||||
{
|
||||
struct uip_eth_hdr ethhdr;
|
||||
uint16 hwtype;
|
||||
uint16 protocol;
|
||||
uint8 hwlen;
|
||||
uint8 protolen;
|
||||
uint16 opcode;
|
||||
struct uip_eth_addr shwaddr;
|
||||
uint16 sipaddr[2];
|
||||
struct uip_eth_addr dhwaddr;
|
||||
uint16 dipaddr[2];
|
||||
};
|
||||
|
||||
struct ethip_hdr {
|
||||
struct uip_eth_hdr ethhdr;
|
||||
/* IP header. */
|
||||
uint8 vhl,
|
||||
tos,
|
||||
len[2],
|
||||
ipid[2],
|
||||
ipoffset[2],
|
||||
ttl,
|
||||
proto;
|
||||
uint16 ipchksum;
|
||||
uint16 srcipaddr[2],
|
||||
destipaddr[2];
|
||||
};
|
||||
|
||||
#define ARP_REQUEST 1
|
||||
#define ARP_REPLY 2
|
||||
|
||||
#define ARP_HWTYPE_ETH 1
|
||||
|
||||
struct arp_entry {
|
||||
uint16 ipaddr[2];
|
||||
struct uip_eth_addr ethaddr;
|
||||
uint8 time;
|
||||
};
|
||||
|
||||
static const struct uip_eth_addr broadcast_ethaddr =
|
||||
{{0xff,0xff,0xff,0xff,0xff,0xff}};
|
||||
static const uint16 broadcast_ipaddr[2] = {0xffff,0xffff};
|
||||
|
||||
static struct arp_entry arp_table[UIP_ARPTAB_SIZE];
|
||||
static uint16 ipaddr[2];
|
||||
static uint8 i, c;
|
||||
|
||||
static uint8 arptime;
|
||||
static uint8 tmpage;
|
||||
|
||||
#define BUF ((struct arp_hdr *)&uip_buf[0])
|
||||
#define IPBUF ((struct ethip_hdr *)&uip_buf[0])
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* Initialize the ARP module.
|
||||
*
|
||||
*/
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
void
|
||||
uip_arp_init(void)
|
||||
{
|
||||
for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
|
||||
memset(arp_table[i].ipaddr, 0, 4);
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* Periodic ARP processing function.
|
||||
*
|
||||
* This function performs periodic timer processing in the ARP module
|
||||
* and should be called at regular intervals. The recommended interval
|
||||
* is 10 seconds between the calls.
|
||||
*
|
||||
*/
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
void
|
||||
uip_arp_timer(void)
|
||||
{
|
||||
struct arp_entry *tabptr;
|
||||
|
||||
++arptime;
|
||||
for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
|
||||
tabptr = &arp_table[i];
|
||||
if((tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0 &&
|
||||
arptime - tabptr->time >= UIP_ARP_MAXAGE) {
|
||||
memset(tabptr->ipaddr, 0, 4);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
uip_arp_update(uint16 *pipaddr, struct uip_eth_addr *ethaddr)
|
||||
{
|
||||
register struct arp_entry *tabptr;
|
||||
/* Walk through the ARP mapping table and try to find an entry to
|
||||
update. If none is found, the IP -> MAC address mapping is
|
||||
inserted in the ARP table. */
|
||||
for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
|
||||
|
||||
tabptr = &arp_table[i];
|
||||
/* Only check those entries that are actually in use. */
|
||||
if(tabptr->ipaddr[0] != 0 &&
|
||||
tabptr->ipaddr[1] != 0) {
|
||||
|
||||
/* Check if the source IP address of the incoming packet matches
|
||||
the IP address in this ARP table entry. */
|
||||
if(pipaddr[0] == tabptr->ipaddr[0] &&
|
||||
pipaddr[1] == tabptr->ipaddr[1]) {
|
||||
|
||||
/* An old entry found, update this and return. */
|
||||
memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
|
||||
tabptr->time = arptime;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* If we get here, no existing ARP table entry was found, so we
|
||||
create one. */
|
||||
|
||||
/* First, we try to find an unused entry in the ARP table. */
|
||||
for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
|
||||
tabptr = &arp_table[i];
|
||||
if(tabptr->ipaddr[0] == 0 &&
|
||||
tabptr->ipaddr[1] == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* If no unused entry is found, we try to find the oldest entry and
|
||||
throw it away. */
|
||||
if(i == UIP_ARPTAB_SIZE) {
|
||||
tmpage = 0;
|
||||
c = 0;
|
||||
for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
|
||||
tabptr = &arp_table[i];
|
||||
if(arptime - tabptr->time > tmpage) {
|
||||
tmpage = arptime - tabptr->time;
|
||||
c = i;
|
||||
}
|
||||
}
|
||||
i = c;
|
||||
tabptr = &arp_table[i];
|
||||
}
|
||||
|
||||
/* Now, i is the ARP table entry which we will fill with the new
|
||||
information. */
|
||||
memcpy(tabptr->ipaddr, pipaddr, 4);
|
||||
memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
|
||||
tabptr->time = arptime;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* ARP processing for incoming IP packets
|
||||
*
|
||||
* This function should be called by the device driver when an IP
|
||||
* packet has been received. The function will check if the address is
|
||||
* in the ARP cache, and if so the ARP cache entry will be
|
||||
* refreshed. If no ARP cache entry was found, a new one is created.
|
||||
*
|
||||
* This function expects an IP packet with a prepended Ethernet header
|
||||
* in the uip_buf[] buffer, and the length of the packet in the global
|
||||
* variable uip_len.
|
||||
*/
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
#if 0
|
||||
void
|
||||
uip_arp_ipin(void)
|
||||
{
|
||||
uip_len -= sizeof(struct uip_eth_hdr);
|
||||
|
||||
/* Only insert/update an entry if the source IP address of the
|
||||
incoming IP packet comes from a host on the local network. */
|
||||
if((IPBUF->srcipaddr[0] & uip_netmask[0]) !=
|
||||
(uip_hostaddr[0] & uip_netmask[0])) {
|
||||
return;
|
||||
}
|
||||
if((IPBUF->srcipaddr[1] & uip_netmask[1]) !=
|
||||
(uip_hostaddr[1] & uip_netmask[1])) {
|
||||
return;
|
||||
}
|
||||
uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src));
|
||||
|
||||
return;
|
||||
}
|
||||
#endif /* 0 */
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* ARP processing for incoming ARP packets.
|
||||
*
|
||||
* This function should be called by the device driver when an ARP
|
||||
* packet has been received. The function will act differently
|
||||
* depending on the ARP packet type: if it is a reply for a request
|
||||
* that we previously sent out, the ARP cache will be filled in with
|
||||
* the values from the ARP reply. If the incoming ARP packet is an ARP
|
||||
* request for our IP address, an ARP reply packet is created and put
|
||||
* into the uip_buf[] buffer.
|
||||
*
|
||||
* When the function returns, the value of the global variable uip_len
|
||||
* indicates whether the device driver should send out a packet or
|
||||
* not. If uip_len is zero, no packet should be sent. If uip_len is
|
||||
* non-zero, it contains the length of the outbound packet that is
|
||||
* present in the uip_buf[] buffer.
|
||||
*
|
||||
* This function expects an ARP packet with a prepended Ethernet
|
||||
* header in the uip_buf[] buffer, and the length of the packet in the
|
||||
* global variable uip_len.
|
||||
*/
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
void
|
||||
uip_arp_arpin(void)
|
||||
{
|
||||
|
||||
if(uip_len < sizeof(struct arp_hdr)) {
|
||||
uip_len = 0;
|
||||
return;
|
||||
}
|
||||
uip_len = 0;
|
||||
|
||||
switch(BUF->opcode) {
|
||||
case HTONS(ARP_REQUEST):
|
||||
/* ARP request. If it asked for our address, we send out a
|
||||
reply. */
|
||||
if(uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) {
|
||||
/* First, we register the one who made the request in our ARP
|
||||
table, since it is likely that we will do more communication
|
||||
with this host in the future. */
|
||||
uip_arp_update(BUF->sipaddr, &BUF->shwaddr);
|
||||
|
||||
/* The reply opcode is 2. */
|
||||
BUF->opcode = HTONS(2);
|
||||
|
||||
memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6);
|
||||
memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6);
|
||||
memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
|
||||
memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6);
|
||||
|
||||
BUF->dipaddr[0] = BUF->sipaddr[0];
|
||||
BUF->dipaddr[1] = BUF->sipaddr[1];
|
||||
BUF->sipaddr[0] = uip_hostaddr[0];
|
||||
BUF->sipaddr[1] = uip_hostaddr[1];
|
||||
|
||||
BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);
|
||||
uip_len = sizeof(struct arp_hdr);
|
||||
}
|
||||
break;
|
||||
case HTONS(ARP_REPLY):
|
||||
/* ARP reply. We insert or update the ARP table if it was meant
|
||||
for us. */
|
||||
if(uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) {
|
||||
uip_arp_update(BUF->sipaddr, &BUF->shwaddr);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* Prepend Ethernet header to an outbound IP packet and see if we need
|
||||
* to send out an ARP request.
|
||||
*
|
||||
* This function should be called before sending out an IP packet. The
|
||||
* function checks the destination IP address of the IP packet to see
|
||||
* what Ethernet MAC address that should be used as a destination MAC
|
||||
* address on the Ethernet.
|
||||
*
|
||||
* If the destination IP address is in the local network (determined
|
||||
* by logical ANDing of netmask and our IP address), the function
|
||||
* checks the ARP cache to see if an entry for the destination IP
|
||||
* address is found. If so, an Ethernet header is prepended and the
|
||||
* function returns. If no ARP cache entry is found for the
|
||||
* destination IP address, the packet in the uip_buf[] is replaced by
|
||||
* an ARP request packet for the IP address. The IP packet is dropped
|
||||
* and it is assumed that they higher level protocols (e.g., TCP)
|
||||
* eventually will retransmit the dropped packet.
|
||||
*
|
||||
* If the destination IP address is not on the local network, the IP
|
||||
* address of the default router is used instead.
|
||||
*
|
||||
* When the function returns, a packet is present in the uip_buf[]
|
||||
* buffer, and the length of the packet is in the global variable
|
||||
* uip_len.
|
||||
*/
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
void
|
||||
uip_arp_out(void)
|
||||
{
|
||||
struct arp_entry *tabptr;
|
||||
|
||||
/* Find the destination IP address in the ARP table and construct
|
||||
the Ethernet header. If the destination IP addres isn't on the
|
||||
local network, we use the default router's IP address instead.
|
||||
|
||||
If not ARP table entry is found, we overwrite the original IP
|
||||
packet with an ARP request for the IP address. */
|
||||
|
||||
/* First check if destination is a local broadcast. */
|
||||
if(uip_ipaddr_cmp(IPBUF->destipaddr, broadcast_ipaddr)) {
|
||||
memcpy(IPBUF->ethhdr.dest.addr, broadcast_ethaddr.addr, 6);
|
||||
} else {
|
||||
/* Check if the destination address is on the local network. */
|
||||
if(!uip_ipaddr_maskcmp(IPBUF->destipaddr, uip_hostaddr, uip_netmask)) {
|
||||
/* Destination address was not on the local network, so we need to
|
||||
use the default router's IP address instead of the destination
|
||||
address when determining the MAC address. */
|
||||
uip_ipaddr_copy(ipaddr, uip_draddr);
|
||||
} else {
|
||||
/* Else, we use the destination IP address. */
|
||||
uip_ipaddr_copy(ipaddr, IPBUF->destipaddr);
|
||||
}
|
||||
|
||||
for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
|
||||
tabptr = &arp_table[i];
|
||||
if(uip_ipaddr_cmp(ipaddr, tabptr->ipaddr)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(i == UIP_ARPTAB_SIZE) {
|
||||
/* The destination address was not in our ARP table, so we
|
||||
overwrite the IP packet with an ARP request. */
|
||||
|
||||
memset(BUF->ethhdr.dest.addr, 0xff, 6);
|
||||
memset(BUF->dhwaddr.addr, 0x00, 6);
|
||||
memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
|
||||
memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6);
|
||||
|
||||
uip_ipaddr_copy(BUF->dipaddr, ipaddr);
|
||||
uip_ipaddr_copy(BUF->sipaddr, uip_hostaddr);
|
||||
BUF->opcode = HTONS(ARP_REQUEST); /* ARP request. */
|
||||
BUF->hwtype = HTONS(ARP_HWTYPE_ETH);
|
||||
BUF->protocol = HTONS(UIP_ETHTYPE_IP);
|
||||
BUF->hwlen = 6;
|
||||
BUF->protolen = 4;
|
||||
BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);
|
||||
|
||||
uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN];
|
||||
|
||||
uip_len = sizeof(struct arp_hdr);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Build an ethernet header. */
|
||||
memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6);
|
||||
}
|
||||
memcpy(IPBUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
|
||||
|
||||
IPBUF->ethhdr.type = HTONS(UIP_ETHTYPE_IP);
|
||||
|
||||
uip_len += sizeof(struct uip_eth_hdr);
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
|
||||
/** @} */
|
||||
/** @} */
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,176 @@
|
||||
/**
|
||||
* \addtogroup uipfw
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
* uIP packet forwarding header file.
|
||||
* \author Adam Dunkels <adam@sics.se>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2004, 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.
|
||||
*
|
||||
* This file is part of the uIP TCP/IP stack
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
* $Id: uip-fw.h,v 1.1.1.1 2007-08-26 23:04:07 patacongo Exp $
|
||||
*/
|
||||
#ifndef __UIP_FW_H__
|
||||
#define __UIP_FW_H__
|
||||
|
||||
#include <net/uip/uip.h>
|
||||
|
||||
/**
|
||||
* Representation of a uIP network interface.
|
||||
*/
|
||||
struct uip_fw_netif {
|
||||
struct uip_fw_netif *next; /**< Pointer to the next interface when
|
||||
linked in a list. */
|
||||
uint16 ipaddr[2]; /**< The IP address of this interface. */
|
||||
uint16 netmask[2]; /**< The netmask of the interface. */
|
||||
uint8 (* output)(void);
|
||||
/**< A pointer to the function that
|
||||
sends a packet. */
|
||||
};
|
||||
|
||||
/**
|
||||
* Intantiating macro for a uIP network interface.
|
||||
*
|
||||
* Example:
|
||||
\code
|
||||
struct uip_fw_netif slipnetif =
|
||||
{UIP_FW_NETIF(192,168,76,1, 255,255,255,0, slip_output)};
|
||||
\endcode
|
||||
* \param ip1,ip2,ip3,ip4 The IP address of the network interface.
|
||||
*
|
||||
* \param nm1,nm2,nm3,nm4 The netmask of the network interface.
|
||||
*
|
||||
* \param outputfunc A pointer to the output function of the network interface.
|
||||
*
|
||||
* \hideinitializer
|
||||
*/
|
||||
#define UIP_FW_NETIF(ip1,ip2,ip3,ip4, nm1,nm2,nm3,nm4, outputfunc) \
|
||||
NULL, \
|
||||
{HTONS((ip1 << 8) | ip2), HTONS((ip3 << 8) | ip4)}, \
|
||||
{HTONS((nm1 << 8) | nm2), HTONS((nm3 << 8) | nm4)}, \
|
||||
outputfunc
|
||||
|
||||
/**
|
||||
* Set the IP address of a network interface.
|
||||
*
|
||||
* \param netif A pointer to the uip_fw_netif structure for the network interface.
|
||||
*
|
||||
* \param addr A pointer to an IP address.
|
||||
*
|
||||
* \hideinitializer
|
||||
*/
|
||||
#define uip_fw_setipaddr(netif, addr) \
|
||||
do { (netif)->ipaddr[0] = ((uint16 *)(addr))[0]; \
|
||||
(netif)->ipaddr[1] = ((uint16 *)(addr))[1]; } while(0)
|
||||
/**
|
||||
* Set the netmask of a network interface.
|
||||
*
|
||||
* \param netif A pointer to the uip_fw_netif structure for the network interface.
|
||||
*
|
||||
* \param addr A pointer to an IP address representing the netmask.
|
||||
*
|
||||
* \hideinitializer
|
||||
*/
|
||||
#define uip_fw_setnetmask(netif, addr) \
|
||||
do { (netif)->netmask[0] = ((uint16 *)(addr))[0]; \
|
||||
(netif)->netmask[1] = ((uint16 *)(addr))[1]; } while(0)
|
||||
|
||||
void uip_fw_init(void);
|
||||
uint8 uip_fw_forward(void);
|
||||
uint8 uip_fw_output(void);
|
||||
void uip_fw_register(struct uip_fw_netif *netif);
|
||||
void uip_fw_default(struct uip_fw_netif *netif);
|
||||
void uip_fw_periodic(void);
|
||||
|
||||
|
||||
/**
|
||||
* A non-error message that indicates that a packet should be
|
||||
* processed locally.
|
||||
*
|
||||
* \hideinitializer
|
||||
*/
|
||||
#define UIP_FW_LOCAL 0
|
||||
|
||||
/**
|
||||
* A non-error message that indicates that something went OK.
|
||||
*
|
||||
* \hideinitializer
|
||||
*/
|
||||
#define UIP_FW_OK 0
|
||||
|
||||
/**
|
||||
* A non-error message that indicates that a packet was forwarded.
|
||||
*
|
||||
* \hideinitializer
|
||||
*/
|
||||
#define UIP_FW_FORWARDED 1
|
||||
|
||||
/**
|
||||
* A non-error message that indicates that a zero-length packet
|
||||
* transmission was attempted, and that no packet was sent.
|
||||
*
|
||||
* \hideinitializer
|
||||
*/
|
||||
#define UIP_FW_ZEROLEN 2
|
||||
|
||||
/**
|
||||
* An error message that indicates that a packet that was too large
|
||||
* for the outbound network interface was detected.
|
||||
*
|
||||
* \hideinitializer
|
||||
*/
|
||||
#define UIP_FW_TOOLARGE 3
|
||||
|
||||
/**
|
||||
* An error message that indicates that no suitable interface could be
|
||||
* found for an outbound packet.
|
||||
*
|
||||
* \hideinitializer
|
||||
*/
|
||||
#define UIP_FW_NOROUTE 4
|
||||
|
||||
/**
|
||||
* An error message that indicates that a packet that should be
|
||||
* forwarded or output was dropped.
|
||||
*
|
||||
* \hideinitializer
|
||||
*/
|
||||
#define UIP_FW_DROPPED 5
|
||||
|
||||
|
||||
#endif /* __UIP_FW_H__ */
|
||||
|
||||
/** @} */
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* Copyright (c) 2006, 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.
|
||||
*
|
||||
* This file is part of the uIP TCP/IP stack
|
||||
*
|
||||
* $Id: uip-neighbor.c,v 1.1.1.1 2007-08-26 23:04:08 patacongo Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
* Database of link-local neighbors, used by IPv6 code and
|
||||
* to be used by a future ARP code rewrite.
|
||||
* \author
|
||||
* Adam Dunkels <adam@sics.se>
|
||||
*/
|
||||
|
||||
#include "uip-neighbor.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_TIME 128
|
||||
|
||||
#ifdef UIP_NEIGHBOR_CONF_ENTRIES
|
||||
#define ENTRIES UIP_NEIGHBOR_CONF_ENTRIES
|
||||
#else /* UIP_NEIGHBOR_CONF_ENTRIES */
|
||||
#define ENTRIES 8
|
||||
#endif /* UIP_NEIGHBOR_CONF_ENTRIES */
|
||||
|
||||
struct neighbor_entry {
|
||||
uip_ipaddr_t ipaddr;
|
||||
struct uip_neighbor_addr addr;
|
||||
uint8 time;
|
||||
};
|
||||
static struct neighbor_entry entries[ENTRIES];
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
uip_neighbor_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = 0; i < ENTRIES; ++i) {
|
||||
entries[i].time = MAX_TIME;
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
uip_neighbor_periodic(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = 0; i < ENTRIES; ++i) {
|
||||
if(entries[i].time < MAX_TIME) {
|
||||
entries[i].time++;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
uip_neighbor_add(uip_ipaddr_t ipaddr, struct uip_neighbor_addr *addr)
|
||||
{
|
||||
int i, oldest;
|
||||
uint8 oldest_time;
|
||||
|
||||
printf("Adding neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
addr->addr.addr[0], addr->addr.addr[1], addr->addr.addr[2], addr->addr.addr[3],
|
||||
addr->addr.addr[4], addr->addr.addr[5]);
|
||||
|
||||
/* Find the first unused entry or the oldest used entry. */
|
||||
oldest_time = 0;
|
||||
oldest = 0;
|
||||
for(i = 0; i < ENTRIES; ++i) {
|
||||
if(entries[i].time == MAX_TIME) {
|
||||
oldest = i;
|
||||
break;
|
||||
}
|
||||
if(uip_ipaddr_cmp(entries[i].ipaddr, addr)) {
|
||||
oldest = i;
|
||||
break;
|
||||
}
|
||||
if(entries[i].time > oldest_time) {
|
||||
oldest = i;
|
||||
oldest_time = entries[i].time;
|
||||
}
|
||||
}
|
||||
|
||||
/* Use the oldest or first free entry (either pointed to by the
|
||||
"oldest" variable). */
|
||||
entries[oldest].time = 0;
|
||||
uip_ipaddr_copy(entries[oldest].ipaddr, ipaddr);
|
||||
memcpy(&entries[oldest].addr, addr, sizeof(struct uip_neighbor_addr));
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static struct neighbor_entry *
|
||||
find_entry(uip_ipaddr_t ipaddr)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = 0; i < ENTRIES; ++i) {
|
||||
if(uip_ipaddr_cmp(entries[i].ipaddr, ipaddr)) {
|
||||
return &entries[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
uip_neighbor_update(uip_ipaddr_t ipaddr)
|
||||
{
|
||||
struct neighbor_entry *e;
|
||||
|
||||
e = find_entry(ipaddr);
|
||||
if(e != NULL) {
|
||||
e->time = 0;
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
struct uip_neighbor_addr *
|
||||
uip_neighbor_lookup(uip_ipaddr_t ipaddr)
|
||||
{
|
||||
struct neighbor_entry *e;
|
||||
|
||||
e = find_entry(ipaddr);
|
||||
if(e != NULL) {
|
||||
/* printf("Lookup neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
e->addr.addr.addr[0], e->addr.addr.addr[1], e->addr.addr.addr[2], e->addr.addr.addr[3],
|
||||
e->addr.addr.addr[4], e->addr.addr.addr[5]);*/
|
||||
|
||||
return &e->addr;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
@@ -0,0 +1,54 @@
|
||||
/* uip-neighbor.h
|
||||
* Header file for database of link-local neighbors, used by IPv6 code and
|
||||
* to be used by future ARP code.
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
* Copyright (c) 2006, 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.
|
||||
*/
|
||||
|
||||
#ifndef __UIP_NEIGHBOR_H__
|
||||
#define __UIP_NEIGHBOR_H__
|
||||
|
||||
#include <net/uip/uip.h>
|
||||
|
||||
struct uip_neighbor_addr
|
||||
{
|
||||
#if UIP_NEIGHBOR_CONF_ADDRTYPE
|
||||
UIP_NEIGHBOR_CONF_ADDRTYPE addr;
|
||||
#else
|
||||
struct uip_eth_addr addr;
|
||||
#endif
|
||||
};
|
||||
|
||||
void uip_neighbor_init(void);
|
||||
void uip_neighbor_add(uip_ipaddr_t ipaddr, struct uip_neighbor_addr *addr);
|
||||
void uip_neighbor_update(uip_ipaddr_t ipaddr);
|
||||
struct uip_neighbor_addr *uip_neighbor_lookup(uip_ipaddr_t ipaddr);
|
||||
void uip_neighbor_periodic(void);
|
||||
|
||||
#endif /* __UIP-NEIGHBOR_H__ */
|
||||
@@ -0,0 +1,132 @@
|
||||
/* uip-split.c
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
* Copyright (c) 2004, 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.
|
||||
*/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <net/uip/uip.h>
|
||||
#include <net/uip/uip-arch.h>
|
||||
|
||||
#include "uip-split.h"
|
||||
#include "uip-fw.h"
|
||||
|
||||
#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
|
||||
|
||||
/*-----------------------------------------------------------------------------*/
|
||||
void
|
||||
uip_split_output(void)
|
||||
{
|
||||
uint16 tcplen, len1, len2;
|
||||
|
||||
/* We only try to split maximum sized TCP segments. */
|
||||
if(BUF->proto == UIP_PROTO_TCP &&
|
||||
uip_len == UIP_BUFSIZE - UIP_LLH_LEN) {
|
||||
|
||||
tcplen = uip_len - UIP_TCPIP_HLEN;
|
||||
/* Split the segment in two. If the original packet length was
|
||||
odd, we make the second packet one byte larger. */
|
||||
len1 = len2 = tcplen / 2;
|
||||
if(len1 + len2 < tcplen) {
|
||||
++len2;
|
||||
}
|
||||
|
||||
/* Create the first packet. This is done by altering the length
|
||||
field of the IP header and updating the checksums. */
|
||||
uip_len = len1 + UIP_TCPIP_HLEN;
|
||||
#ifdef CONFIG_NET_UIP_IPv6
|
||||
/* For IPv6, the IP length field does not include the IPv6 IP header
|
||||
length. */
|
||||
BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
|
||||
BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
|
||||
#else /* CONFIG_NET_UIP_IPv6 */
|
||||
BUF->len[0] = uip_len >> 8;
|
||||
BUF->len[1] = uip_len & 0xff;
|
||||
#endif /* CONFIG_NET_UIP_IPv6 */
|
||||
|
||||
/* Recalculate the TCP checksum. */
|
||||
BUF->tcpchksum = 0;
|
||||
BUF->tcpchksum = ~(uip_tcpchksum());
|
||||
|
||||
#ifndef CONFIG_NET_UIP_IPv6
|
||||
/* Recalculate the IP checksum. */
|
||||
BUF->ipchksum = 0;
|
||||
BUF->ipchksum = ~(uip_ipchksum());
|
||||
#endif /* CONFIG_NET_UIP_IPv6 */
|
||||
|
||||
/* Transmit the first packet. */
|
||||
/* uip_fw_output();*/
|
||||
tcpip_output();
|
||||
|
||||
/* Now, create the second packet. To do this, it is not enough to
|
||||
just alter the length field, but we must also update the TCP
|
||||
sequence number and point the uip_appdata to a new place in
|
||||
memory. This place is detemined by the length of the first
|
||||
packet (len1). */
|
||||
uip_len = len2 + UIP_TCPIP_HLEN;
|
||||
#ifdef CONFIG_NET_UIP_IPv6
|
||||
/* For IPv6, the IP length field does not include the IPv6 IP header
|
||||
length. */
|
||||
BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
|
||||
BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
|
||||
#else /* CONFIG_NET_UIP_IPv6 */
|
||||
BUF->len[0] = uip_len >> 8;
|
||||
BUF->len[1] = uip_len & 0xff;
|
||||
#endif /* CONFIG_NET_UIP_IPv6 */
|
||||
|
||||
/* uip_appdata += len1;*/
|
||||
memcpy(uip_appdata, (uint8 *)uip_appdata + len1, len2);
|
||||
|
||||
uip_add32(BUF->seqno, len1);
|
||||
BUF->seqno[0] = uip_acc32[0];
|
||||
BUF->seqno[1] = uip_acc32[1];
|
||||
BUF->seqno[2] = uip_acc32[2];
|
||||
BUF->seqno[3] = uip_acc32[3];
|
||||
|
||||
/* Recalculate the TCP checksum. */
|
||||
BUF->tcpchksum = 0;
|
||||
BUF->tcpchksum = ~(uip_tcpchksum());
|
||||
|
||||
#ifndef CONFIG_NET_UIP_IPv6
|
||||
/* Recalculate the IP checksum. */
|
||||
BUF->ipchksum = 0;
|
||||
BUF->ipchksum = ~(uip_ipchksum());
|
||||
#endif /* CONFIG_NET_UIP_IPv6 */
|
||||
|
||||
/* Transmit the second packet. */
|
||||
/* uip_fw_output();*/
|
||||
tcpip_output();
|
||||
} else {
|
||||
/* uip_fw_output();*/
|
||||
tcpip_output();
|
||||
}
|
||||
|
||||
}
|
||||
/*-----------------------------------------------------------------------------*/
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2004, 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.
|
||||
*
|
||||
* This file is part of the uIP TCP/IP stack
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
* $Id: uip-split.h,v 1.1.1.1 2007-08-26 23:04:08 patacongo Exp $
|
||||
*/
|
||||
/**
|
||||
* \addtogroup uip
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* \defgroup uipsplit uIP TCP throughput booster hack
|
||||
* @{
|
||||
*
|
||||
* The basic uIP TCP implementation only allows each TCP connection to
|
||||
* have a single TCP segment in flight at any given time. Because of
|
||||
* the delayed ACK algorithm employed by most TCP receivers, uIP's
|
||||
* limit on the amount of in-flight TCP segments seriously reduces the
|
||||
* maximum achievable throughput for sending data from uIP.
|
||||
*
|
||||
* The uip-split module is a hack which tries to remedy this
|
||||
* situation. By splitting maximum sized outgoing TCP segments into
|
||||
* two, the delayed ACK algorithm is not invoked at TCP
|
||||
* receivers. This improves the throughput when sending data from uIP
|
||||
* by orders of magnitude.
|
||||
*
|
||||
* The uip-split module uses the uip-fw module (uIP IP packet
|
||||
* forwarding) for sending packets. Therefore, the uip-fw module must
|
||||
* be set up with the appropriate network interfaces for this module
|
||||
* to work.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* \file
|
||||
* Module for splitting outbound TCP segments in two to avoid the
|
||||
* delayed ACK throughput degradation.
|
||||
* \author
|
||||
* Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __UIP_SPLIT_H__
|
||||
#define __UIP_SPLIT_H__
|
||||
|
||||
/**
|
||||
* Handle outgoing packets.
|
||||
*
|
||||
* This function inspects an outgoing packet in the uip_buf buffer and
|
||||
* sends it out using the uip_fw_output() function. If the packet is a
|
||||
* full-sized TCP segment it will be split into two segments and
|
||||
* transmitted separately. This function should be called instead of
|
||||
* the actual device driver output function, or the uip_fw_output()
|
||||
* function.
|
||||
*
|
||||
* The headers of the outgoing packet is assumed to be in the uip_buf
|
||||
* buffer and the payload is assumed to be wherever uip_appdata
|
||||
* points. The length of the outgoing packet is assumed to be in the
|
||||
* uip_len variable.
|
||||
*
|
||||
*/
|
||||
void uip_split_output(void);
|
||||
|
||||
#endif /* __UIP_SPLIT_H__ */
|
||||
|
||||
/** @} */
|
||||
/** @} */
|
||||
@@ -0,0 +1,180 @@
|
||||
/************************************************************
|
||||
* uip-wait.c
|
||||
*
|
||||
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
************************************************************/
|
||||
|
||||
/************************************************************
|
||||
* Included Files
|
||||
************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <semaphore.h>
|
||||
#include <wdog.h>
|
||||
#include <arch/irq.h>
|
||||
#include <net/uip/uip.h>
|
||||
|
||||
/************************************************************
|
||||
* Definitions
|
||||
************************************************************/
|
||||
|
||||
/************************************************************
|
||||
* Private Data
|
||||
************************************************************/
|
||||
|
||||
/* Threads and tasks can both wait a semaphore. This
|
||||
* initializer uses internal, non-portable knowledge of the\
|
||||
* structure of sem_t to initialize it to the value 0.
|
||||
*/
|
||||
|
||||
static sem_t uip_waitsem = { 0 }; /* Semaphore to wait on */
|
||||
static uint16 uip_waitflags = 0; /* UIP flags to wait for */
|
||||
|
||||
/************************************************************
|
||||
* Private Functions
|
||||
************************************************************/
|
||||
|
||||
/* Called from the timer interrupt handler when a event wait
|
||||
* watchdog expires.
|
||||
*/
|
||||
|
||||
static void uip_event_timeout(int argc, uint32 itcb, ...)
|
||||
{
|
||||
irqstate_t save = irqsave(); /* Should not be necessary */
|
||||
uip_flags |= UIP_APPTIMEOUT; /* Set software timeout event */
|
||||
uip_event_signal(); /* Signal the waiting thread/task */
|
||||
irqrestore(save); /* Restore interrupts */
|
||||
}
|
||||
|
||||
/************************************************************
|
||||
* Global Functions
|
||||
************************************************************/
|
||||
|
||||
/* This function is called user code to set up the wait */
|
||||
|
||||
int uip_event_timedwait(uint16 waitflags, int timeout)
|
||||
{
|
||||
/* Prevent conflicts with the interrupt level operation of
|
||||
* uip_event_signal().
|
||||
*/
|
||||
irqstate_t save = irqsave();
|
||||
WDOG_ID wdog;
|
||||
|
||||
/* At present, we support only a single waiter. If uip_waitflags
|
||||
* is non-zero on entry, then there is a problem.
|
||||
*/
|
||||
|
||||
if ( uip_waitflags == 0)
|
||||
{
|
||||
/* If the requested event bits are not set in the uip_flags,
|
||||
* then wait. We will be awakened as soon as an interrupt is
|
||||
* received that sets these bits.
|
||||
*/
|
||||
|
||||
goto errout_with_irqdisabled;
|
||||
}
|
||||
|
||||
/* Loop until the requested bits are set in the flags */
|
||||
|
||||
while ((waitflags & uip_flags) != 0)
|
||||
{
|
||||
/* Set the event flags that the caller is waiting on */
|
||||
|
||||
uip_waitflags = waitflags;
|
||||
|
||||
/* Was a timeut requested as well? */
|
||||
|
||||
if (timeout)
|
||||
{
|
||||
/* Yes, then set the application timeout event as well */
|
||||
|
||||
uip_waitflags |= UIP_APPTIMEOUT;
|
||||
|
||||
/* Create a watchdog */
|
||||
|
||||
wdog = wd_create();
|
||||
if (!wdog)
|
||||
{
|
||||
goto errout_with_irqdisabled;
|
||||
}
|
||||
|
||||
/* Start the watchdog */
|
||||
|
||||
wd_start(wdog, timeout, (wdentry_t)uip_event_timeout, 0);
|
||||
}
|
||||
|
||||
/* Wait for the event (or timeout) to occur */
|
||||
|
||||
if (sem_wait(&uip_waitsem) != 0)
|
||||
{
|
||||
goto errout_with_watchdog;
|
||||
}
|
||||
|
||||
/* We no longer need the watchdog */
|
||||
|
||||
if (wdog)
|
||||
{
|
||||
wd_delete(wdog);
|
||||
wdog = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
irqrestore(save);
|
||||
return OK;
|
||||
|
||||
errout_with_watchdog:
|
||||
if (wdog)
|
||||
{
|
||||
wd_delete(wdog);
|
||||
}
|
||||
|
||||
errout_with_irqdisabled:
|
||||
irqrestore(save);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* This function is called from uip_interrupt() to wake up any
|
||||
* waiting threads/tasks.
|
||||
*/
|
||||
|
||||
void uip_event_signal(void)
|
||||
{
|
||||
/* If any bits in uip_waitflags match bits set in uip_flags, then
|
||||
* there must be a thread/task waiting for this event to occur.
|
||||
*/
|
||||
|
||||
if ((uip_waitflags & uip_flags) != 0)
|
||||
{
|
||||
sem_post(&uip_waitsem); /* Post the event */
|
||||
uip_waitflags = 0; /* Prohibit posting the event multiple times */
|
||||
}
|
||||
}
|
||||
+2091
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user