From 537c17c6684c0df0452622006d104854b2dbc13a Mon Sep 17 00:00:00 2001 From: goldsimon Date: Fri, 18 May 2007 13:26:11 +0000 Subject: [PATCH] Added sys_arch.c to be able to use sys layer and sequential APIs under Win32 --- ports/msvc6/include/arch/sys_arch.h | 17 +- ports/msvc6/lwipopts.h | 8 +- ports/msvc6/pktif.c | 51 ++-- ports/msvc6/sys_arch.c | 398 ++++++++++++++++++++++++++++ ports/msvc6/test.c | 214 +++++++-------- 5 files changed, 543 insertions(+), 145 deletions(-) create mode 100644 ports/msvc6/sys_arch.c diff --git a/ports/msvc6/include/arch/sys_arch.h b/ports/msvc6/include/arch/sys_arch.h index 2353c79..29fe171 100644 --- a/ports/msvc6/include/arch/sys_arch.h +++ b/ports/msvc6/include/arch/sys_arch.h @@ -32,19 +32,18 @@ #ifndef __ARCH_SYS_ARCH_H__ #define __ARCH_SYS_ARCH_H__ -#define SYS_MBOX_NULL NULL -#define SYS_SEM_NULL NULL - -struct sys_sem; -typedef struct sys_sem * sys_sem_t; +/* HANDLE is used for sys_sem_t but we won't include windows.h */ +typedef void* sys_sem_t; #define SYS_SEM_NULL NULL -struct sys_mbox; -typedef struct sys_mbox *sys_mbox_t; +struct lwip_mbox; +typedef struct lwip_mbox* sys_mbox_t; #define SYS_MBOX_NULL NULL -struct sys_thread; -typedef struct sys_thread * sys_thread_t; +/* DWORD (thread id) is used for sys_thread_t but we won't include windows.h */ +typedef u32_t sys_thread_t; + +typedef u32_t sys_prot_t; #endif /* __ARCH_SYS_ARCH_H__ */ diff --git a/ports/msvc6/lwipopts.h b/ports/msvc6/lwipopts.h index c00f35d..414521b 100644 --- a/ports/msvc6/lwipopts.h +++ b/ports/msvc6/lwipopts.h @@ -31,6 +31,9 @@ */ #ifndef __LWIPOPTS_H__ #define __LWIPOPTS_H__ + +#define LWIP_HAVE_LOOPIF 1 + #define LWIP_DBG_MIN_LEVEL 0 #define LWIP_COMPAT_SOCKETS 1 #define TAPIF_DEBUG LWIP_DBG_ON @@ -69,7 +72,8 @@ #define LWIP_DBG_TYPES_ON (LWIP_DBG_ON|LWIP_DBG_TRACE|LWIP_DBG_STATE|LWIP_DBG_FRESH|LWIP_DBG_HALT) -#define NO_SYS 1 +#define NO_SYS 0 +#define LWIP_PROVIDE_ERRNO /* ---------- Memory options ---------- */ /* MEM_ALIGNMENT: should be set to the alignment of the CPU for which @@ -133,7 +137,7 @@ a lot of data that needs to be copied, this should be set high. */ * for certain critical regions during buffer allocation, deallocation and memory * allocation and deallocation. */ -/*#define SYS_LIGHTWEIGHT_PROT 1*/ +#define SYS_LIGHTWEIGHT_PROT 1 /* ---------- TCP options ---------- */ #define LWIP_TCP 1 diff --git a/ports/msvc6/pktif.c b/ports/msvc6/pktif.c index f4c4819..2a15c82 100644 --- a/ports/msvc6/pktif.c +++ b/ports/msvc6/pktif.c @@ -84,12 +84,7 @@ #define IFNAME0 'p' #define IFNAME1 'k' -struct ethernetif { - struct eth_addr *ethaddr; - /* Add whatever per-interface state that is needed here. */ -}; - -static const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}}; +static struct eth_addr broadcastaddr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; /* Forward declarations. */ static void ethernetif_input(struct netif *netif); @@ -107,14 +102,8 @@ extern int packet_send(void *buffer, int len); static void low_level_init(struct netif *netif) { - struct ethernetif *ethernetif; - - ethernetif = netif->state; - - memcpy(ðaddr,ethernetif->ethaddr,6); - #ifdef NETIF_DEBUG - LWIP_DEBUGF(NETIF_DEBUG, ("pktif: eth_addr %02X%02X%02X%02X%02X%02X\n",ethernetif->ethaddr->addr[0],ethernetif->ethaddr->addr[1],ethernetif->ethaddr->addr[2],ethernetif->ethaddr->addr[3],ethernetif->ethaddr->addr[4],ethernetif->ethaddr->addr[5])); + LWIP_DEBUGF(NETIF_DEBUG, ("pktif: eth_addr %02X%02X%02X%02X%02X%02X\n",netif->hwaddr[0],netif->hwaddr[1],netif->hwaddr[2],netif->hwaddr[3],netif->hwaddr[4],netif->hwaddr[5])); #endif /* NETIF_DEBUG */ /* Do whatever else is needed to initialize interface. */ @@ -174,10 +163,11 @@ low_level_output(struct netif *ethernetif, struct pbuf *p) */ /*-----------------------------------------------------------------------------------*/ static struct pbuf * -low_level_input(struct netif *ethernetif) +low_level_input(struct netif *netif) { struct pbuf *p, *q; int start, length; + struct eth_hdr *ethhdr; /* Obtain the size of the packet and put it into the "len" variable. */ @@ -185,6 +175,14 @@ low_level_input(struct netif *ethernetif) if (length<=0) return NULL; + ethhdr = (struct eth_hdr*)cur_packet; + /* MAC filter: only let my MAC or broadcast through */ + if((memcmp(ðhdr->dest, &netif->hwaddr, 6)) && (memcmp(ðhdr->dest, &broadcastaddr, 6))) { + /* acknowledge that packet has been read(); */ + cur_length=0; + return NULL; + } + /* We allocate a pbuf chain of pbufs from the pool. */ p = pbuf_alloc(PBUF_LINK, (u16_t)length, PBUF_POOL); #ifdef NETIF_DEBUG @@ -266,13 +264,20 @@ ethernetif_input(struct netif *netif) if (p != NULL) { -#ifdef LINK_STATS +#if LINK_STATS lwip_stats.link.recv++; #endif /* LINK_STATS */ ethhdr = p->payload; switch (htons(ethhdr->type)) { +#if ETHARP_TCPIP_INPUT + /* IP or ARP packet? */ + case ETHTYPE_IP: + case ETHTYPE_ARP: + netif->input(p, netif); + break; +#else case ETHTYPE_IP: etharp_ip_input(netif, p); pbuf_header(p, -14); @@ -281,6 +286,7 @@ ethernetif_input(struct netif *netif) case ETHTYPE_ARP: etharp_arp_input(netif, ethernetif->ethaddr, p); break; +#endif default: pbuf_free(p); break; @@ -288,13 +294,6 @@ ethernetif_input(struct netif *netif) } } /*-----------------------------------------------------------------------------------*/ -static void -arp_timer(void *arg) -{ - etharp_tmr(); - sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)arp_timer, NULL); -} -/*-----------------------------------------------------------------------------------*/ /* * ethernetif_init(): * @@ -307,10 +306,6 @@ arp_timer(void *arg) err_t ethernetif_init(struct netif *netif) { - struct ethernetif *ethernetif; - - ethernetif = mem_malloc(sizeof(struct ethernetif)); - netif->state = ethernetif; netif->name[0] = IFNAME0; netif->name[1] = IFNAME1; netif->linkoutput = low_level_output; @@ -319,12 +314,8 @@ ethernetif_init(struct netif *netif) netif->mtu = 1500; netif->flags = NETIF_FLAG_BROADCAST; netif->hwaddr_len = 6; - ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]); low_level_init(netif); - etharp_init(); - - sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)arp_timer, NULL); return ERR_OK; } diff --git a/ports/msvc6/sys_arch.c b/ports/msvc6/sys_arch.c new file mode 100644 index 0000000..b4d721c --- /dev/null +++ b/ports/msvc6/sys_arch.c @@ -0,0 +1,398 @@ +/* + * Copyright (c) 2001-2003 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. + * + * This file is part of the lwIP TCP/IP stack. + * + * Author: Adam Dunkels + * + */ + +/* +********************************************************************************************************* +* UCOS-II Port +* +* Target : Any processor +* Put together by : Michael Anburaj +* URL : http://geocities.com/michaelanburaj/ Email : michaelanburaj@hotmail.com +* +********************************************************************************************************* +*/ + +#include +#include // sprintf() for task names + +#include +#include +#include + +#include +#include +#include +#include + +#if !NO_SYS + +#define MAX_QUEUE_ENTRIES 100 + +struct threadlist { + struct sys_timeouts timeouts; + DWORD id; + struct threadlist *next; +}; + +struct lwip_mbox { + HANDLE sem; + void* q_mem[MAX_QUEUE_ENTRIES]; + u32_t head, tail; +}; + +struct threadlist *lwip_win32_threads = NULL; +LARGE_INTEGER freq, sys_start_time; +CRITICAL_SECTION critSec; + +void InitSysArchProtect() +{ + InitializeCriticalSection(&critSec); +} +u32_t sys_arch_protect() +{ + EnterCriticalSection(&critSec); + return 0; +} +void sys_arch_unprotect(u32_t pval) +{ + LeaveCriticalSection(&critSec); +} +void do_sleep(int ms) +{ + Sleep(ms); +} + +void sys_init(void) +{ + QueryPerformanceFrequency(&freq); + QueryPerformanceCounter(&sys_start_time); + InitSysArchProtect(); +} + +static LONGLONG sys_get_ms() +{ + LONGLONG ret; + LARGE_INTEGER now; + QueryPerformanceCounter(&now); + ret = now.QuadPart-sys_start_time.QuadPart; + return (u32_t)(((ret)*1000)/freq.QuadPart); +} + +sys_sem_t sys_sem_new(u8_t count) +{ + HANDLE new_sem = NULL; + new_sem = CreateSemaphore(0, count, 100000, 0); + LWIP_ASSERT("Error creating semaphore", new_sem != NULL); + if(new_sem != NULL) { +#if LWIP_STATS + lwip_stats.sys.sem.used++; + LWIP_ASSERT("sys_sem_new() counter overflow", lwip_stats.sys.sem.used != 0 ); + if (lwip_stats.sys.sem.used > lwip_stats.sys.sem.max) { + lwip_stats.sys.sem.max = lwip_stats.sys.sem.used; + } +#endif // LWIP_STATS + return new_sem; + } + + // failed to allocate memory... +#if LWIP_STATS + lwip_stats.sys.sem.err++; +#endif // LWIP_STATS + return SYS_SEM_NULL; +} + +void sys_sem_free(sys_sem_t sem) +{ + // parameter check + LWIP_ASSERT("sem != NULL", sem != NULL); + LWIP_ASSERT("sem != INVALID_HANDLE_VALUE", sem != INVALID_HANDLE_VALUE); + CloseHandle(sem); + +#if LWIP_STATS + lwip_stats.sys.sem.used--; + LWIP_ASSERT("sys_sem_free() closed more than created", lwip_stats.sys.sem.used != (u16_t)-1); +#endif // LWIP_STATS +} + +u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout) +{ + DWORD ret; + LONGLONG starttime, endtime; + LWIP_ASSERT("sem != NULL", sem != NULL); + LWIP_ASSERT("sem != INVALID_HANDLE_VALUE", sem != INVALID_HANDLE_VALUE); + if(!timeout) + { + // wait infinite + starttime = sys_get_ms(); + ret = WaitForSingleObject(sem, INFINITE); + LWIP_ASSERT("Error waiting for mutex", ret == WAIT_OBJECT_0); + endtime = sys_get_ms(); + // return the time we waited for the sem + return (u32_t)(endtime - starttime); + } + else + { + int ret; + starttime = sys_get_ms(); + ret = WaitForSingleObject(sem, timeout); + LWIP_ASSERT("Error waiting for mutex", (ret == WAIT_OBJECT_0) || (ret == WAIT_TIMEOUT)); + if(ret == WAIT_OBJECT_0) + { + endtime = sys_get_ms(); + // return the time we waited for the sem + return (u32_t)(endtime - starttime); + } + else + { + // timeout + return SYS_ARCH_TIMEOUT; + } + } +} + +void sys_sem_signal(sys_sem_t sem) +{ + DWORD ret; + LWIP_ASSERT("sem != NULL", sem != NULL); + LWIP_ASSERT("sem != INVALID_HANDLE_VALUE", sem != INVALID_HANDLE_VALUE); + ret = ReleaseSemaphore(sem, 1, NULL); + LWIP_ASSERT("Error releasing mutex", ret != 0); +} + +struct sys_timeouts *sys_arch_timeouts(void) +{ + struct sys_timeouts *ret = NULL; + struct threadlist *t, *new_thread; + SYS_ARCH_DECL_PROTECT(lev); + DWORD threadID; + + threadID = GetCurrentThreadId(); + SYS_ARCH_PROTECT(lev); + for(t = lwip_win32_threads; t != NULL; t = t->next) + { + if(t->id == threadID) + { + ret = &(t->timeouts); + SYS_ARCH_UNPROTECT(lev); + return ret; + } + } + new_thread = (struct threadlist*)malloc(sizeof(struct threadlist)); + LWIP_ASSERT("new_thread != NULL", new_thread != NULL); + if(new_thread != NULL) { + OutputDebugString("First call to sys_arch_timoeuts for thread"); + new_thread->next = lwip_win32_threads; + lwip_win32_threads = new_thread; + new_thread->id = threadID; + new_thread->timeouts.next = NULL; + ret = &(new_thread->timeouts); + SYS_ARCH_UNPROTECT(lev); + return ret; + } + SYS_ARCH_UNPROTECT(lev); + LWIP_ASSERT("should not come here", 0); + return 0; +} + +sys_thread_t sys_thread_new(void (* function)(void *arg), void *arg, int prio) +{ + struct threadlist *new_thread; + HANDLE h; + SYS_ARCH_DECL_PROTECT(lev); + + new_thread = (struct threadlist*)malloc(sizeof(struct threadlist)); + LWIP_ASSERT("new_thread != NULL", new_thread != NULL); + if(new_thread != NULL) { + SYS_ARCH_PROTECT(lev); + new_thread->next = lwip_win32_threads; + lwip_win32_threads = new_thread; + new_thread->timeouts.next = NULL; + + h = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)function, arg, 0, &(new_thread->id)); + LWIP_ASSERT("h != 0", h != 0); + LWIP_ASSERT("h != -1", h != INVALID_HANDLE_VALUE); + + SYS_ARCH_UNPROTECT(lev); + return new_thread->id; + } + return 0; +} + +sys_mbox_t sys_mbox_new() +{ + struct lwip_mbox *new_mbox; + new_mbox = (struct lwip_mbox*)malloc(sizeof(struct lwip_mbox)); + LWIP_ASSERT("new_mbox != NULL", new_mbox != NULL); + if(new_mbox == NULL) { +#if LWIP_STATS + lwip_stats.sys.mbox.err++; +#endif // LWIP_STATS + return SYS_SEM_NULL; + } + new_mbox->sem = CreateSemaphore(0, 0, MAX_QUEUE_ENTRIES, 0); + LWIP_ASSERT("Error creating semaphore", new_mbox->sem != NULL); + if(new_mbox->sem == NULL) { +#if LWIP_STATS + lwip_stats.sys.mbox.err++; +#endif // LWIP_STATS + free(new_mbox); + new_mbox = NULL; + return SYS_SEM_NULL; + } + memset(&new_mbox->q_mem, 0, sizeof(u32_t)*MAX_QUEUE_ENTRIES); + new_mbox->head = 0; + new_mbox->tail = 0; +#if LWIP_STATS + lwip_stats.sys.mbox.used++; + LWIP_ASSERT("sys_mbox_new() counter overflow", lwip_stats.sys.mbox.used != 0 ); + if (lwip_stats.sys.mbox.used > lwip_stats.sys.mbox.max) { + lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used; + } +#endif // LWIP_STATS + return new_mbox; +} + +void sys_mbox_free(sys_mbox_t mbox) +{ + // parameter check + LWIP_ASSERT("sys_mbox_free ", mbox != SYS_MBOX_NULL ); + LWIP_ASSERT("mbox->sem != NULL", mbox->sem != NULL); + LWIP_ASSERT("mbox->sem != INVALID_HANDLE_VALUE", mbox->sem != INVALID_HANDLE_VALUE); + + CloseHandle(mbox->sem); + free(mbox); + +#if LWIP_STATS + lwip_stats.sys.mbox.used--; + LWIP_ASSERT( "sys_mbox_free() ", lwip_stats.sys.mbox.used!= (u16_t)-1 ); +#endif // LWIP_STATS +} + +void sys_mbox_post(sys_mbox_t q, void *msg) +{ + DWORD ret; + SYS_ARCH_DECL_PROTECT(lev); + + // parameter check + LWIP_ASSERT("sys_mbox_free ", q != SYS_MBOX_NULL ); + LWIP_ASSERT("q->sem != NULL", q->sem != NULL); + LWIP_ASSERT("q->sem != INVALID_HANDLE_VALUE", q->sem != INVALID_HANDLE_VALUE); + + //printf("sys_mbox_post(%p %p)\n", q, msg); + + SYS_ARCH_PROTECT(lev); + q->q_mem[q->head] = msg; + (q->head)++; + if (q->head >= MAX_QUEUE_ENTRIES) { + q->head = 0; + } + LWIP_ASSERT("mbox is full!", q->head != q->tail); + ret = ReleaseSemaphore(q->sem, 1, 0); + LWIP_ASSERT("Error releasing sem", ret != 0); + + SYS_ARCH_UNPROTECT(lev); +} + +u32_t sys_arch_mbox_fetch(sys_mbox_t q, void **msg, u32_t timeout) +{ + DWORD ret; + LONGLONG starttime, endtime; + SYS_ARCH_DECL_PROTECT(lev); + + // parameter check + LWIP_ASSERT("sys_mbox_free ", q != SYS_MBOX_NULL ); + LWIP_ASSERT("q->sem != NULL", q->sem != NULL); + LWIP_ASSERT("q->sem != INVALID_HANDLE_VALUE", q->sem != INVALID_HANDLE_VALUE); + + if (timeout == 0) { + timeout = INFINITE; + } + starttime = sys_get_ms(); + if ((ret = WaitForSingleObject(q->sem, timeout)) == WAIT_OBJECT_0) { + SYS_ARCH_PROTECT(lev); + if(msg != NULL) { + *msg = q->q_mem[q->tail]; + } + //printf("sys_arch_mbox_fetch(%p) -> %p\n", q, msg?*msg:0); + (q->tail)++; + if (q->tail >= MAX_QUEUE_ENTRIES) { + q->tail = 0; + } + SYS_ARCH_UNPROTECT(lev); + endtime = sys_get_ms(); + return (u32_t)(endtime - starttime); + } + else + { + LWIP_ASSERT("Error waiting for sem", ret == WAIT_TIMEOUT); + if(msg != NULL) { + *msg = NULL; + } + //printf("sys_arch_mbox_fetch(%p) -> timeout\n", q); + return SYS_ARCH_TIMEOUT; + } +} + +u32_t sys_arch_mbox_tryfetch(sys_mbox_t q, void **msg) +{ + DWORD ret; + SYS_ARCH_DECL_PROTECT(lev); + + // parameter check + LWIP_ASSERT("sys_mbox_free ", q != SYS_MBOX_NULL ); + LWIP_ASSERT("q->sem != NULL", q->sem != NULL); + LWIP_ASSERT("q->sem != INVALID_HANDLE_VALUE", q->sem != INVALID_HANDLE_VALUE); + + if ((ret = WaitForSingleObject(q->sem, 0)) == WAIT_OBJECT_0) { + SYS_ARCH_PROTECT(lev); + if(msg != NULL) { + *msg = q->q_mem[q->tail]; + } + //printf("sys_arch_mbox_tryfetch(%p) -> %p\n", q, msg?*msg:0); + (q->tail)++; + if (q->tail >= MAX_QUEUE_ENTRIES) { + q->tail = 0; + } + SYS_ARCH_UNPROTECT(lev); + return 0; + } + else + { + LWIP_ASSERT("Error waiting for sem", ret == WAIT_TIMEOUT); + if(msg != NULL) { + *msg = NULL; + } + //printf("sys_arch_mbox_tryfetch(%p) -> timeout\n", q); + return SYS_ARCH_TIMEOUT; + } +} +#endif /* !NO_SYS */ diff --git a/ports/msvc6/test.c b/ports/msvc6/test.c index 66fe604..7cb7893 100644 --- a/ports/msvc6/test.c +++ b/ports/msvc6/test.c @@ -43,6 +43,8 @@ #include "lwip/mem.h" #include "lwip/memp.h" #include "lwip/sys.h" +#include "lwip/sockets.h" +#include "netif/etharp.h" #include "lwip/stats.h" @@ -66,150 +68,153 @@ void update_adapter(void); int dbg_printf(const char *fmt, ...) { - va_list v; - int r; + va_list v; + int r; - va_start(v, fmt); - r = vfprintf(stderr,fmt, v); - va_end(v); - return r; + va_start(v, fmt); + r = vfprintf(stderr,fmt, v); + va_end(v); + return r; } #if LWIP_TCP static err_t netio_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { - if (err == ERR_OK && p != NULL) - { - tcp_recved(pcb, p->tot_len); - pbuf_free(p); - } - else - pbuf_free(p); + if (err == ERR_OK && p != NULL) { + tcp_recved(pcb, p->tot_len); + pbuf_free(p); + } else { + pbuf_free(p); + } - if (err == ERR_OK && p == NULL) - { - tcp_arg(pcb, NULL); - tcp_sent(pcb, NULL); - tcp_recv(pcb, NULL); - tcp_close(pcb); - } + if (err == ERR_OK && p == NULL) { + tcp_arg(pcb, NULL); + tcp_sent(pcb, NULL); + tcp_recv(pcb, NULL); + tcp_close(pcb); + } - return ERR_OK; + return ERR_OK; } static err_t netio_accept(void *arg, struct tcp_pcb *pcb, err_t err) { - tcp_arg(pcb, NULL); - tcp_sent(pcb, NULL); - tcp_recv(pcb, netio_recv); - return ERR_OK; + tcp_arg(pcb, NULL); + tcp_sent(pcb, NULL); + tcp_recv(pcb, netio_recv); + return ERR_OK; } void netio_init(void) { - struct tcp_pcb *pcb; + struct tcp_pcb *pcb; - pcb = tcp_new(); - tcp_bind(pcb, IP_ADDR_ANY, 18767); - pcb = tcp_listen(pcb); - tcp_accept(pcb, netio_accept); + pcb = tcp_new(); + tcp_bind(pcb, IP_ADDR_ANY, 18767); + pcb = tcp_listen(pcb); + tcp_accept(pcb, netio_accept); } #endif /* LWIP_TCP */ struct netif netif; +struct netif loop_netif; void main_loop() { - struct ip_addr ipaddr, netmask, gw; + struct ip_addr ipaddr, netmask, gw; +#if LWIP_HAVE_LOOPIF + struct ip_addr loop_ipaddr, loop_netmask, loop_gw; +#endif #if NO_SYS - int last_time; - int timer1; - int timer2; + int last_time; + int timer1; + int timer2; #endif /* NO_SYS */ int done; - char mac_addr[6]; + char mac_addr[6]; + + IP4_ADDR(&gw, 192,168,1,1); + IP4_ADDR(&ipaddr, 192,168,1,200); + IP4_ADDR(&netmask, 255,255,255,0); + printf("Starting lwIP, local interface IP is %s\n", inet_ntoa(*(struct in_addr*)&ipaddr)); - IP4_ADDR(&gw, 192,168,1,1); - IP4_ADDR(&ipaddr, 192,168,1,200); - IP4_ADDR(&netmask, 255,255,255,0); - printf("Starting lwIP, local interface IP is %s\n", inet_ntoa(*(struct in_addr*)&ipaddr)); - if (init_adapter(PACKET_LIB_ADAPTER_NR, mac_addr) != 0) { printf("ERROR initializing network adapter %d!\n", PACKET_LIB_ADAPTER_NR); - return; + return; } - memcpy(&netif.hwaddr, mac_addr, 6); - /* increase the last octet so that lwIP netif has a similar but different MAC addr */ - netif.hwaddr[5]++; + memcpy(&netif.hwaddr, mac_addr, 6); + /* increase the last octet so that lwIP netif has a similar but different MAC addr */ + netif.hwaddr[5]++; #if NO_SYS - netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, ip_input)); + netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, ip_input)); #else /* NO_SYS */ - netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_ethinput)); + netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_ethinput)); #endif /* NO_SYS */ - netif_set_up(&netif); + netif_set_up(&netif); - /* - IP4_ADDR(&gw, 127,0,0,1); - IP4_ADDR(&ipaddr, 127,0,0,1); - IP4_ADDR(&netmask, 255,0,0,0); - - netif_add(&ipaddr, &netmask, &gw, loopif_init, - ip_input); - */ +#if LWIP_HAVE_LOOPIF + IP4_ADDR(&loop_gw, 127,0,0,1); + IP4_ADDR(&loop_ipaddr, 127,0,0,1); + IP4_ADDR(&loop_netmask, 255,0,0,0); +#if NO_SYS + netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, loopif_init, ip_input); +#else /* NO_SYS */ + netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, loopif_init, tcpip_ethinput); +#endif /* NO_SYS */ +#endif + /* + + */ #if NO_SYS - tcp_init(); - udp_init(); - ip_init(); + tcp_init(); + udp_init(); + ip_init(); #else /* NO_SYS */ tcpip_init(0,0); #endif /* NO_SYS */ - httpd_init(); - netio_init(); - //ftpd_init(); + httpd_init(); + netio_init(); + //ftpd_init(); #if NO_SYS - last_time=clock(); - timer1=0; - timer2=0; - done=0; + last_time=clock(); + timer1=0; + timer2=0; #endif /* NO_SYS */ + done=0; - while (!done) - { + while (!done) { #if NO_SYS - int cur_time; - int time_diff; + int cur_time; + int time_diff; + + cur_time=clock(); + time_diff=cur_time-last_time; + if (time_diff>0) { + last_time=cur_time; + timer1+=time_diff; + timer2+=time_diff; + } - cur_time=clock(); - time_diff=cur_time-last_time; - if (time_diff>0) - { - last_time=cur_time; - timer1+=time_diff; - timer2+=time_diff; - } + if (timer1>10) { + tcp_fasttmr(); + timer1=0; + } - if (timer1>10) - { - tcp_fasttmr(); - timer1=0; - } - - if (timer2>45) - { - tcp_slowtmr(); - timer2=0; - done=_kbhit(); - } + if (timer2>45) { + tcp_slowtmr(); + timer2=0; + done=_kbhit(); + } #endif /* NO_SYS */ - update_adapter(); - } + update_adapter(); + } - shutdown_adapter(); + shutdown_adapter(); } void bcopy(const void *src, void *dest, int len) @@ -224,27 +229,28 @@ void bzero(void *data, int n) int main(void) { - setvbuf(stdout,NULL,_IONBF,0); + setvbuf(stdout,NULL,_IONBF,0); #ifdef PERF - perf_init("/tmp/lwip.perf"); + perf_init("/tmp/lwip.perf"); #endif /* PERF */ #ifdef STATS - stats_init(); + stats_init(); #endif /* STATS */ - sys_init(); - mem_init(); - memp_init(); - pbuf_init(); + sys_init(); + mem_init(); + memp_init(); + pbuf_init(); + etharp_init(); #if !NO_SYS - lwip_socket_init(); + lwip_socket_init(); #endif /* !NO_SYS */ - //tcpdump_init(); + //tcpdump_init(); - printf("System initialized.\n"); + printf("System initialized.\n"); - main_loop(); + main_loop(); - return 0; + return 0; }