Merge pull request #422 from BernardXiong/master

Add lwIP/NAT, DHCP server and dlib(LIBC of IAR)
This commit is contained in:
Bernard Xiong 2015-01-31 22:11:44 +08:00
commit 0f9139ecfc
24 changed files with 2329 additions and 44 deletions

View File

@ -131,6 +131,7 @@ int dfs_device_fs_close(struct dfs_fd *file)
int dfs_device_fs_open(struct dfs_fd *file) int dfs_device_fs_open(struct dfs_fd *file)
{ {
rt_err_t result;
rt_device_t device; rt_device_t device;
if (file->flags & DFS_O_CREAT) if (file->flags & DFS_O_CREAT)
@ -186,9 +187,16 @@ int dfs_device_fs_open(struct dfs_fd *file)
if (device == RT_NULL) if (device == RT_NULL)
return -DFS_STATUS_ENODEV; return -DFS_STATUS_ENODEV;
file->data = device; /* to open device */
result = rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
return DFS_STATUS_OK; if (result == RT_EOK || result == -RT_ENOSYS)
{
file->data = device;
return DFS_STATUS_OK;
}
/* open device failed. */
return -DFS_STATUS_EIO;
} }
int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st) int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)

View File

@ -11,6 +11,8 @@ if GetDepend('RT_USING_LIBC'):
objs = objs + SConscript('newlib/SConscript') objs = objs + SConscript('newlib/SConscript')
elif rtconfig.PLATFORM == 'armcc': elif rtconfig.PLATFORM == 'armcc':
objs = objs + SConscript('armlibc/SConscript') objs = objs + SConscript('armlibc/SConscript')
elif rtconfig.PLATFORM == 'iar':
objs = objs + SConscript('dlib/SConscript')
else: else:
if rtconfig.PLATFORM == 'gcc': if rtconfig.PLATFORM == 'gcc':
objs = objs + SConscript('minilibc/SConscript') objs = objs + SConscript('minilibc/SConscript')

View File

@ -13,7 +13,7 @@
* Date Author Notes * Date Author Notes
* 2012-11-23 Yihui The first version * 2012-11-23 Yihui The first version
* 2013-11-24 aozima fixed _sys_read()/_sys_write() issues. * 2013-11-24 aozima fixed _sys_read()/_sys_write() issues.
* 2014-08-03 bernard If using msh, use system() implementation * 2014-08-03 bernard If using msh, use system() implementation
* in msh. * in msh.
*/ */
@ -48,11 +48,11 @@ const char __stderr_name[] = "STDERR";
*/ */
FILEHANDLE _sys_open(const char *name, int openmode) FILEHANDLE _sys_open(const char *name, int openmode)
{ {
#ifdef RT_USING_DFS #ifdef RT_USING_DFS
int fd; int fd;
int mode = O_RDONLY; int mode = O_RDONLY;
#endif #endif
/* Register standard Input Output devices. */ /* Register standard Input Output devices. */
if (strcmp(name, __stdin_name) == 0) if (strcmp(name, __stdin_name) == 0)
return (STDIN); return (STDIN);
@ -64,34 +64,34 @@ FILEHANDLE _sys_open(const char *name, int openmode)
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return -1; return -1;
#else #else
/* Correct openmode from fopen to open */ /* Correct openmode from fopen to open */
if (openmode & OPEN_PLUS) if (openmode & OPEN_PLUS)
{ {
if (openmode & OPEN_W) if (openmode & OPEN_W)
{ {
mode |= (O_RDWR | O_TRUNC | O_CREAT); mode |= (O_RDWR | O_TRUNC | O_CREAT);
} }
else if (openmode & OPEN_A) else if (openmode & OPEN_A)
{ {
mode |= (O_RDWR | O_APPEND | O_CREAT); mode |= (O_RDWR | O_APPEND | O_CREAT);
} }
else else
mode |= O_RDWR; mode |= O_RDWR;
} }
else else
{ {
if (openmode & OPEN_W) if (openmode & OPEN_W)
{ {
mode |= (O_WRONLY | O_TRUNC | O_CREAT); mode |= (O_WRONLY | O_TRUNC | O_CREAT);
} }
else if (openmode & OPEN_A) else if (openmode & OPEN_A)
{ {
mode |= (O_WRONLY | O_APPEND | O_CREAT); mode |= (O_WRONLY | O_APPEND | O_CREAT);
} }
} }
fd = open(name, mode, 0); fd = open(name, mode, 0);
if(fd < 0) if (fd < 0)
return -1; return -1;
else else
return fd + STDERR + 1; return fd + STDERR + 1;
@ -121,10 +121,10 @@ int _sys_close(FILEHANDLE fh)
*/ */
int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
{ {
#ifdef RT_USING_DFS #ifdef RT_USING_DFS
int size; int size;
#endif #endif
if (fh == STDIN) if (fh == STDIN)
{ {
/* TODO */ /* TODO */
@ -138,7 +138,7 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
return 0; return 0;
#else #else
size = read(fh - STDERR - 1, buf, len); size = read(fh - STDERR - 1, buf, len);
if(size >= 0) if (size >= 0)
return len - size; return len - size;
else else
return -1; return -1;
@ -159,7 +159,7 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
#ifdef RT_USING_DFS #ifdef RT_USING_DFS
int size; int size;
#endif #endif
if ((fh == STDOUT) || (fh == STDERR)) if ((fh == STDOUT) || (fh == STDERR))
{ {
#ifndef RT_USING_CONSOLE #ifndef RT_USING_CONSOLE
@ -170,18 +170,18 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
console_device = rt_console_get_device(); console_device = rt_console_get_device();
if (console_device != 0) rt_device_write(console_device, 0, buf, len); if (console_device != 0) rt_device_write(console_device, 0, buf, len);
return 0; return 0;
#endif #endif
} }
if(fh == STDIN) if (fh == STDIN)
return -1; return -1;
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return 0; return 0;
#else #else
size = write(fh - STDERR - 1, buf, len); size = write(fh - STDERR - 1, buf, len);
if(size >= 0) if (size >= 0)
return len - size; return len - size;
else else
return -1; return -1;
@ -270,6 +270,6 @@ int remove(const char *filename)
int system(const char *string) int system(const char *string)
{ {
RT_ASSERT(0); RT_ASSERT(0);
for(;;); for (;;);
} }
#endif #endif

View File

@ -0,0 +1,4 @@
Dlib(IAR) porting for RT-Thread.
Please define RT_USING_LIBC and compile RT-Thread with IAR compiler.

View File

@ -0,0 +1,15 @@
from building import *
import rtconfig
src = Glob('*.c')
cwd = GetCurrentDir()
group = []
CPPPATH = [cwd]
CPPDEFINES = ['RT_USING_DLIBC']
if rtconfig.PLATFORM == 'iar':
group = DefineGroup('dlib', src, depend = ['RT_USING_LIBC'],
CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES)
Return('group')

View File

@ -0,0 +1,25 @@
/* File: environ.c
* this file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2015-01-28 Bernard first version
*/
const char *__environ = "OS=RT-Thread";

View File

@ -0,0 +1,74 @@
/*
* File : rmtx.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2015-01-28 Bernard first version
*/
#include <rtthread.h>
#include <yfuns.h>
/*
* for IAR compiler, we recommand to define _DLIB_THREAD_SUPPORT
* as 2 for dlib multi-thread support.
*/
#if _DLIB_THREAD_SUPPORT
typedef void* _Rmtx;
void _Mtxinit(_Rmtx *m)
{
rt_mutex_t mutex;
RT_ASSERT(m != RT_NULL);
mutex = (rt_mutex_t)m;
rt_mutex_init(mutex, "iarMtx", RT_IPC_FLAG_FIFO);
}
void _Mtxdst(_Rmtx *m)
{
rt_mutex_t mutex;
RT_ASSERT(m != RT_NULL);
mutex = (rt_mutex_t)m;
rt_mutex_detach(mutex);
}
void _Mtxlock(_Rmtx *m)
{
rt_mutex_t mutex;
RT_ASSERT(m != RT_NULL);
mutex = (rt_mutex_t)m;
rt_mutex_take(mutex, RT_WAITING_FOREVER);
}
void _Mtxunlock(_Rmtx *m)
{
rt_mutex_t mutex;
RT_ASSERT(m != RT_NULL);
mutex = (rt_mutex_t)m;
rt_mutex_release(mutex);
}
#endif

View File

@ -0,0 +1,43 @@
/*
* File : syscall_close.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2015-01-28 Bernard first version
*/
#include <rtthread.h>
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#endif
#include <yfuns.h>
#pragma module_name = "?__close"
int __close(int handle)
{
if (handle == _LLIO_STDOUT ||
handle == _LLIO_STDERR ||
handle == _LLIO_STDIN)
return _LLIO_ERROR;
#ifdef RT_USING_DFS
return close(handle);
#else
return 0;
#endif
}

View File

@ -0,0 +1,43 @@
/*
* File : syscall_lseek.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2015-01-28 Bernard first version
*/
#include <rtthread.h>
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#endif
#include <yfuns.h>
#pragma module_name = "?__lseek"
long __lseek(int handle, long offset, int whence)
{
if (handle == _LLIO_STDOUT ||
handle == _LLIO_STDERR ||
handle == _LLIO_STDIN)
return _LLIO_ERROR;
#ifdef RT_USING_DFS
return lseek(handle, offset, whence);
#else
return _LLIO_ERROR;
#endif
}

View File

@ -0,0 +1,44 @@
/*
* File : syscall_mem.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2015-01-28 Bernard first version
*/
#include <rtthread.h>
void *malloc(rt_size_t n)
{
return rt_malloc(n);
}
void *realloc(void *rmem, rt_size_t newsize)
{
return rt_realloc(rmem, newsize);
}
void *calloc(rt_size_t nelem, rt_size_t elsize)
{
return rt_calloc(nelem, elsize);
}
void free(void *rmem)
{
rt_free(rmem);
}

View File

@ -0,0 +1,88 @@
/*
* File : syscall_open.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2015-01-28 Bernard first version
*/
#include <rtthread.h>
#include <yfuns.h>
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#endif
#pragma module_name = "?__open"
int __open(const char *filename, int mode)
{
#ifndef RT_USING_DFS
return -1;
#else
int handle;
int open_mode = O_RDONLY;
if (mode & _LLIO_CREAT)
{
open_mode |= O_CREAT;
/* Check what we should do with it if it exists. */
if (mode & _LLIO_APPEND)
{
/* Append to the existing file. */
open_mode |= O_APPEND;
}
if (mode & _LLIO_TRUNC)
{
/* Truncate the existsing file. */
open_mode |= O_TRUNC;
}
}
if (mode & _LLIO_TEXT)
{
/* we didn't support text mode */
}
switch (mode & _LLIO_RDWRMASK)
{
case _LLIO_RDONLY:
break;
case _LLIO_WRONLY:
open_mode |= O_WRONLY;
break;
case _LLIO_RDWR:
/* The file should be opened for both reads and writes. */
open_mode |= O_RDWR;
break;
default:
return -1;
}
handle = open(filename, open_mode, 0);
if (handle < 0)
return -1;
return handle + _LLIO_STDERR + 1;
#endif
}

View File

@ -0,0 +1,53 @@
/*
* File : syscall_read.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2015-01-28 Bernard first version
*/
#include <rtthread.h>
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#endif
#include <yfuns.h>
#pragma module_name = "?__read"
size_t __read(int handle, unsigned char *buf, size_t len)
{
#ifdef RT_USING_DFS
int size;
#endif
if (handle == _LLIO_STDIN)
{
/* TODO */
return 0;
}
if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
return _LLIO_ERROR;
#ifndef RT_USING_DFS
return _LLIO_ERROR;
#else
size = read(handle - _LLIO_STDERR - 1, buf, len);
return size;
#endif
}

View File

@ -0,0 +1,38 @@
/*
* File : syscall_remove.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2015-01-28 Bernard first version
*/
#include <rtthread.h>
#ifdef RT_USING_DFS
#include <dfs_file.h>
#endif
#include <yfuns.h>
#pragma module_name = "?remove"
int remove(const char *val)
{
#ifdef RT_USING_DFS
dfs_file_unlink(val);
#endif
return 0;
}

View File

@ -0,0 +1,62 @@
/*
* File : syscall_write.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2015-01-28 Bernard first version
*/
#include <rtthread.h>
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#endif
#include <yfuns.h>
#pragma module_name = "?__write"
size_t __write(int handle, const unsigned char *buf, size_t len)
{
#ifdef RT_USING_DFS
int size;
#endif
if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
{
#ifndef RT_USING_CONSOLE
return _LLIO_ERROR;
#else
rt_device_t console_device;
console_device = rt_console_get_device();
if (console_device != 0) rt_device_write(console_device, 0, buf, len);
return len;
#endif
}
if (handle == _LLIO_STDIN) return -1;
#ifndef RT_USING_DFS
return _LLIO_ERROR;
#else
size = write(handle - _LLIO_STDERR - 1, buf, len);
return size;
#endif
}

View File

@ -0,0 +1,23 @@
/* File: syscalls.h
* this file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2015-01-28 Bernard first version
*/

View File

@ -312,6 +312,11 @@ ip_input(struct pbuf *p, struct netif *inp)
int check_ip_src=1; int check_ip_src=1;
#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */ #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
#if IP_NAT
extern u8_t ip_nat_input(struct pbuf *p);
extern u8_t ip_nat_out(struct pbuf *p);
#endif
IP_STATS_INC(ip.recv); IP_STATS_INC(ip.recv);
snmp_inc_ipinreceives(); snmp_inc_ipinreceives();
@ -487,15 +492,30 @@ ip_input(struct pbuf *p, struct netif *inp)
/* packet not for us? */ /* packet not for us? */
if (netif == NULL) { if (netif == NULL) {
#if IP_FORWARD || IP_NAT
u8_t taken = 0;
#endif /* IP_FORWARD || IP_NAT */
/* packet not for us, route or discard */ /* packet not for us, route or discard */
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n")); LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
#if IP_FORWARD #if IP_FORWARD || IP_NAT
/* non-broadcast packet? */ /* non-broadcast packet? */
if (!ip_addr_isbroadcast(&current_iphdr_dest, inp)) { if (!ip_addr_isbroadcast(&(iphdr->dest), inp)) {
/* try to forward IP packet on (other) interfaces */ #if IP_NAT
ip_forward(p, iphdr, inp); /* check if we want to perform NAT with this packet. */
} else taken = ip_nat_out(p);
if (!taken)
#endif /* IP_NAT */
{
#if IP_FORWARD
/* try to forward IP packet on (other) interfaces */
if (ip_forward(p, iphdr, inp) != NULL) {
taken = 1;
}
#endif /* IP_FORWARD */ #endif /* IP_FORWARD */
}
}
if (!taken)
#endif /* IP_FORWARD || IP_NAT */
{ {
snmp_inc_ipinaddrerrors(); snmp_inc_ipinaddrerrors();
snmp_inc_ipindiscards(); snmp_inc_ipindiscards();
@ -553,6 +573,13 @@ ip_input(struct pbuf *p, struct netif *inp)
current_netif = inp; current_netif = inp;
current_header = iphdr; current_header = iphdr;
#if IP_NAT
if (!ip_addr_isbroadcast(&(iphdr->dest), inp) &&
(ip_nat_input(p) != 0)) {
LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet consumed by nat layer\n"));
} else
#endif /* IP_NAT */
#if LWIP_RAW #if LWIP_RAW
/* raw input did not eat the packet? */ /* raw input did not eat the packet? */
if (raw_input(p, inp) == 0) if (raw_input(p, inp) == 0)

View File

@ -161,6 +161,12 @@
*/ */
#define SYS_LIGHTWEIGHT_PROT (NO_SYS==0) #define SYS_LIGHTWEIGHT_PROT (NO_SYS==0)
#ifdef LWIP_USING_NAT
#define IP_NAT 1
#else
#define IP_NAT 0
#endif
/* ---------- TCP options ---------- */ /* ---------- TCP options ---------- */
#ifdef RT_LWIP_TCP #ifdef RT_LWIP_TCP
#define LWIP_TCP 1 #define LWIP_TCP 1

View File

@ -0,0 +1,10 @@
from building import *
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
group = DefineGroup('LwIP', src, depend = ['RT_USING_LWIP', 'LWIP_USING_DHCPD'], CPPPATH = CPPPATH)
Return('group')

View File

@ -0,0 +1,398 @@
/*
* File : dhcp_server.c
* A simple DHCP server implementation
*
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2013-2015, Shanghai Real-Thread Technology Co., Ltd
* http://www.rt-thread.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2013-01-30 aozima the first version
* 2013-08-08 aozima support different network segments.
* 2015-01-30 bernard release to RT-Thread RTOS.
*/
#include <stdio.h>
#include <stdint.h>
#include <rtthread.h>
#include <lwip/opt.h>
#include <lwip/sockets.h>
#include <lwip/inet_chksum.h>
#include <netif/etharp.h>
#include <netif/ethernetif.h>
#include <lwip/ip.h>
/* DHCP server option */
/* allocated client ip range */
#ifndef DHCPD_CLIENT_IP_MIN
#define DHCPD_CLIENT_IP_MIN 2
#endif
#ifndef DHCPD_CLIENT_IP_MAX
#define DHCPD_CLIENT_IP_MAX 254
#endif
/* the DHCP server address */
#ifndef DHCPD_SERVER_IPADDR0
#define DHCPD_SERVER_IPADDR0 192UL
#define DHCPD_SERVER_IPADDR1 168UL
#define DHCPD_SERVER_IPADDR2 169UL
#define DHCPD_SERVER_IPADDR3 1UL
#endif
//#define DHCP_DEBUG_PRINTF
#ifdef DHCP_DEBUG_PRINTF
#define DEBUG_PRINTF rt_kprintf("[DHCP] "); rt_kprintf
#else
#define DEBUG_PRINTF(...)
#endif /* DHCP_DEBUG_PRINTF */
/* we need some routines in the DHCP of lwIP */
#undef LWIP_DHCP
#define LWIP_DHCP 1
#include <lwip/dhcp.h>
/* buffer size for receive DHCP packet */
#define BUFSZ 1024
static uint8_t next_client_ip = DHCPD_CLIENT_IP_MIN;
static rt_err_t _low_level_dhcp_send(struct netif *netif,
const void *buffer,
rt_size_t size)
{
struct pbuf *p;
struct eth_hdr *ethhdr;
struct ip_hdr *iphdr;
struct udp_hdr *udphdr;
p = pbuf_alloc(PBUF_LINK,
SIZEOF_ETH_HDR + sizeof(struct ip_hdr)
+ sizeof(struct udp_hdr) + size,
PBUF_RAM);
if (p == RT_NULL) return -RT_ENOMEM;
ethhdr = (struct eth_hdr *)p->payload;
iphdr = (struct ip_hdr *)((char *)ethhdr + SIZEOF_ETH_HDR);
udphdr = (struct udp_hdr *)((char *)iphdr + sizeof(struct ip_hdr));
ETHADDR32_COPY(&ethhdr->dest, (struct eth_addr *)&ethbroadcast);
ETHADDR16_COPY(&ethhdr->src, netif->hwaddr);
ethhdr->type = PP_HTONS(ETHTYPE_IP);
iphdr->src.addr = 0x00000000; /* src: 0.0.0.0 */
iphdr->dest.addr = 0xFFFFFFFF; /* src: 255.255.255.255 */
IPH_VHL_SET(iphdr, 4, IP_HLEN / 4);
IPH_TOS_SET(iphdr, 0x00);
IPH_LEN_SET(iphdr, htons(IP_HLEN + sizeof(struct udp_hdr) + size));
IPH_ID_SET(iphdr, htons(2));
IPH_OFFSET_SET(iphdr, 0);
IPH_TTL_SET(iphdr, 255);
IPH_PROTO_SET(iphdr, IP_PROTO_UDP);
IPH_CHKSUM_SET(iphdr, 0);
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
udphdr->src = htons(DHCP_SERVER_PORT);
udphdr->dest = htons(DHCP_CLIENT_PORT);
udphdr->len = htons(sizeof(struct udp_hdr) + size);
udphdr->chksum = 0;
memcpy((char *)udphdr + sizeof(struct udp_hdr),
buffer, size);
return netif->linkoutput(netif, p);
}
static void dhcpd_thread_entry(void *parameter)
{
struct netif *netif = RT_NULL;
int sock;
int bytes_read;
char *recv_data;
rt_uint32_t addr_len;
struct sockaddr_in server_addr, client_addr;
struct dhcp_msg *msg;
int optval = 1;
/* get ethernet interface. */
netif = (struct netif*) parameter;
RT_ASSERT(netif != RT_NULL);
/* our DHCP server information */
DEBUG_PRINTF("DHCP server IP: %d.%d.%d.%d client IP: %d.%d.%d.%d-%d\n",
DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
DHCPD_SERVER_IPADDR2, DHCPD_SERVER_IPADDR3,
DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
DHCPD_SERVER_IPADDR2, DHCPD_CLIENT_IP_MIN, DHCPD_CLIENT_IP_MAX);
/* allocate buffer for receive */
recv_data = rt_malloc(BUFSZ);
if (recv_data == RT_NULL)
{
/* No memory */
DEBUG_PRINTF("Out of memory\n");
return;
}
/* create a socket with UDP */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
DEBUG_PRINTF("create socket failed, errno = %d\n", errno);
rt_free(recv_data);
return;
}
/* set to receive broadcast packet */
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval));
/* initialize server address */
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(DHCP_SERVER_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
/* bind socket to the server address */
if (bind(sock, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1)
{
/* bind failed. */
DEBUG_PRINTF("bind server address failed, errno=%d\n", errno);
rt_free(recv_data);
return;
}
addr_len = sizeof(struct sockaddr);
DEBUG_PRINTF("DHCP server listen on port %d...\n", DHCP_SERVER_PORT);
while (1)
{
bytes_read = recvfrom(sock, recv_data, BUFSZ - 1, 0,
(struct sockaddr *)&client_addr, &addr_len);
if (bytes_read < DHCP_MSG_LEN)
{
DEBUG_PRINTF("packet too short, wait for next!\n");
continue;
}
msg = (struct dhcp_msg *)recv_data;
/* check message type to make sure we can handle it */
if ((msg->op != DHCP_BOOTREQUEST) || (msg->cookie != PP_HTONL(DHCP_MAGIC_COOKIE)))
{
continue;
}
/* handler. */
{
uint8_t *dhcp_opt;
uint8_t option;
uint8_t length;
uint8_t message_type = 0;
uint8_t finished = 0;
uint32_t request_ip = 0;
dhcp_opt = (uint8_t *)msg + DHCP_OPTIONS_OFS;
while (finished == 0)
{
option = *dhcp_opt;
length = *(dhcp_opt + 1);
switch (option)
{
case DHCP_OPTION_REQUESTED_IP:
request_ip = *(dhcp_opt + 2) << 24 | *(dhcp_opt + 3) << 16
| *(dhcp_opt + 4) << 8 | *(dhcp_opt + 5);
break;
case DHCP_OPTION_END:
finished = 1;
break;
case DHCP_OPTION_MESSAGE_TYPE:
message_type = *(dhcp_opt + 2);
break;
default:
break;
} /* switch(option) */
dhcp_opt += (2 + length);
}
/* reply. */
dhcp_opt = (uint8_t *)msg + DHCP_OPTIONS_OFS;
/* check. */
if (request_ip)
{
uint32_t client_ip = DHCPD_SERVER_IPADDR0 << 24 | DHCPD_SERVER_IPADDR1 << 16
| DHCPD_SERVER_IPADDR2 << 8 | (next_client_ip);
if (request_ip != client_ip)
{
*dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
*dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
*dhcp_opt++ = DHCP_NAK;
*dhcp_opt++ = DHCP_OPTION_END;
DEBUG_PRINTF("requested IP invalid, reply DHCP_NAK\n");
if (netif != RT_NULL)
{
int send_byte = (dhcp_opt - (uint8_t *)msg);
_low_level_dhcp_send(netif, msg, send_byte);
DEBUG_PRINTF("DHCP server send %d byte\n", send_byte);
}
next_client_ip++;
if (next_client_ip > DHCPD_CLIENT_IP_MAX)
next_client_ip = DHCPD_CLIENT_IP_MIN;
continue;
}
}
if (message_type == DHCP_DISCOVER)
{
DEBUG_PRINTF("request DHCP_DISCOVER\n");
DEBUG_PRINTF("reply DHCP_OFFER\n");
// DHCP_OPTION_MESSAGE_TYPE
*dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
*dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
*dhcp_opt++ = DHCP_OFFER;
// DHCP_OPTION_SERVER_ID
*dhcp_opt++ = DHCP_OPTION_SERVER_ID;
*dhcp_opt++ = 4;
*dhcp_opt++ = DHCPD_SERVER_IPADDR0;
*dhcp_opt++ = DHCPD_SERVER_IPADDR1;
*dhcp_opt++ = DHCPD_SERVER_IPADDR2;
*dhcp_opt++ = DHCPD_SERVER_IPADDR3;
// DHCP_OPTION_LEASE_TIME
*dhcp_opt++ = DHCP_OPTION_LEASE_TIME;
*dhcp_opt++ = 4;
*dhcp_opt++ = 0x00;
*dhcp_opt++ = 0x01;
*dhcp_opt++ = 0x51;
*dhcp_opt++ = 0x80;
}
else if (message_type == DHCP_REQUEST)
{
DEBUG_PRINTF("request DHCP_REQUEST\n");
DEBUG_PRINTF("reply DHCP_ACK\n");
// DHCP_OPTION_MESSAGE_TYPE
*dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
*dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
*dhcp_opt++ = DHCP_ACK;
// DHCP_OPTION_SERVER_ID
*dhcp_opt++ = DHCP_OPTION_SERVER_ID;
*dhcp_opt++ = 4;
*dhcp_opt++ = DHCPD_SERVER_IPADDR0;
*dhcp_opt++ = DHCPD_SERVER_IPADDR1;
*dhcp_opt++ = DHCPD_SERVER_IPADDR2;
*dhcp_opt++ = DHCPD_SERVER_IPADDR3;
// DHCP_OPTION_SUBNET_MASK
*dhcp_opt++ = DHCP_OPTION_SUBNET_MASK;
*dhcp_opt++ = 4;
*dhcp_opt++ = 0xFF;
*dhcp_opt++ = 0xFF;
*dhcp_opt++ = 0xFF;
*dhcp_opt++ = 0x00;
#ifdef DHCPD_USING_ROUTER
// DHCP_OPTION_ROUTER
*dhcp_opt++ = DHCP_OPTION_ROUTER;
*dhcp_opt++ = 4;
*dhcp_opt++ = DHCPD_SERVER_IPADDR0;
*dhcp_opt++ = DHCPD_SERVER_IPADDR1;
*dhcp_opt++ = DHCPD_SERVER_IPADDR2;
*dhcp_opt++ = 1;
#endif
// DHCP_OPTION_DNS_SERVER, use the default DNS server address in lwIP
*dhcp_opt++ = DHCP_OPTION_DNS_SERVER;
*dhcp_opt++ = 4;
*dhcp_opt++ = 208;
*dhcp_opt++ = 67;
*dhcp_opt++ = 222;
*dhcp_opt++ = 222;
// DHCP_OPTION_LEASE_TIME
*dhcp_opt++ = DHCP_OPTION_LEASE_TIME;
*dhcp_opt++ = 4;
*dhcp_opt++ = 0x00;
*dhcp_opt++ = 0x01;
*dhcp_opt++ = 0x51;
*dhcp_opt++ = 0x80;
}
else
{
DEBUG_PRINTF("un handle message:%d\n", message_type);
}
// append DHCP_OPTION_END
*dhcp_opt++ = DHCP_OPTION_END;
/* send reply. */
if ((message_type == DHCP_DISCOVER) || (message_type == DHCP_REQUEST))
{
msg->op = DHCP_BOOTREPLY;
IP4_ADDR(&msg->yiaddr,
DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
DHCPD_SERVER_IPADDR2, next_client_ip);
client_addr.sin_addr.s_addr = INADDR_BROADCAST;
if (netif != RT_NULL)
{
int send_byte = (dhcp_opt - (uint8_t *)msg);
_low_level_dhcp_send(netif, msg, send_byte);
DEBUG_PRINTF("DHCP server send %d byte\n", send_byte);
}
}
} /* handler. */
}
}
void dhcpd_start(char* netif_name)
{
rt_thread_t thread;
struct netif *netif = RT_NULL;
/* find ethernet interface. */
netif = netif_find(netif_name);
if (netif == RT_NULL)
{
DEBUG_PRINTF("Not found network interface:%s\n", netif_name);
}
thread = rt_thread_create("dhcpd",
dhcpd_thread_entry, netif,
1024,
RT_THREAD_PRIORITY_MAX - 3,
2);
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
}

View File

@ -0,0 +1,36 @@
/*
* File : dhcp_server.h
* A simple DHCP server implementation
*
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2013-2015, Shanghai Real-Thread Technology Co., Ltd
* http://www.rt-thread.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2013-01-30 aozima the first version
* 2013-08-08 aozima support different network segments.
* 2015-01-30 bernard release to RT-Thread RTOS.
*/
#ifndef DHCPV4_SERVER_H__
#define DHCPV4_SERVER_H__
void dhcpd_start(char* netif_name);
#endif

View File

@ -0,0 +1,19 @@
lwIP NAT componenent
If you want to use lwIP NAT componenent, please define LWIP_USING_NAT in rtconfig.h.
In this case the network 213.129.231.168/29 is nat'ed when packets are sent to the
destination network 10.0.0.0/24 (untypical example - most users will have the other
way around).
Use following code to add a NAT entry:
ip_nat_entry_t nat_entry;
nat_entry.out_if = (struct netif *)&emac_if1;
nat_entry.in_if = (struct netif *)&emac_if2;
IP4_ADDR(&nat_entry.source_net, 213, 129, 231, 168);
IP4_ADDR(&nat_entry.source_netmask, 255, 255, 255, 248);
IP4_ADDR(&nat_entry.dest_net, 10, 0, 0, 0);
IP4_ADDR(&nat_entry.source_netmask, 255, 0, 0, 0);
ip_nat_add(&_nat_entry);

View File

@ -0,0 +1,10 @@
from building import *
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
group = DefineGroup('LwIP', src, depend = ['RT_USING_LWIP', 'LWIP_USING_NAT'], CPPPATH = CPPPATH)
Return('group')

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,103 @@
/**
* NAT - NAT implementation for lwIP supporting TCP/UDP and ICMP.
* Copyright (c) 2009 Christian Walter, ?Embedded Solutions, Vienna 2009.
*
* Copyright (c) 2010 lwIP project ;-)
* 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.
*/
/*
* File : ipv4_nat.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2015, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2015-01-26 Hichard porting to RT-Thread
* 2015-01-27 Bernard code cleanup for lwIP in RT-Thread
*/
#ifndef __LWIP_NAT_H__
#define __LWIP_NAT_H__
#include <rtthread.h>
#ifdef LWIP_USING_NAT
#include "lwip/err.h"
#include "lwip/ip_addr.h"
#include "lwip/opt.h"
/** Timer interval at which to call ip_nat_tmr() */
#define LWIP_NAT_TMR_INTERVAL_SEC (30*1000)
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
struct netif;
struct pbuf;
typedef struct ip_nat_entry
{
ip_addr_t source_net;
ip_addr_t source_netmask;
ip_addr_t dest_net;
ip_addr_t dest_netmask;
struct netif *out_if;
struct netif *in_if;
} ip_nat_entry_t;
void ip_nat_init(void);
void ip_nat_tmr(void);
u8_t ip_nat_input(struct pbuf *p);
u8_t ip_nat_out(struct pbuf *p);
err_t ip_nat_add(const ip_nat_entry_t *new_entry);
void ip_nat_remove(const ip_nat_entry_t *remove_entry);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* IP_NAT */
#endif /* __LWIP_NAT_H__ */