diff options
| author | 陈豪 <[email protected]> | 2014-08-06 22:55:49 +0800 |
|---|---|---|
| committer | 陈豪 <[email protected]> | 2014-08-06 22:55:49 +0800 |
| commit | 5fc32bf12ef4356ecbd46cfa21079cddb522b5c7 (patch) | |
| tree | 8f77e59c1bcec59c3c5efd0fe022f3dc0a0c901e /components/net | |
| parent | 0c8335d5ab046ce9fe36c4e01d5d40e4ba152b49 (diff) | |
| parent | a8fadf15b1828569e7684049d983cc6cb7fda2c9 (diff) | |
Merge pull request #1 from RT-Thread/master
sync
Diffstat (limited to 'components/net')
| -rw-r--r-- | components/net/lwip-1.3.2/apps/ping.c | 189 | ||||
| -rw-r--r-- | components/net/lwip-1.4.1/src/arch/sys_arch.c | 48 | ||||
| -rw-r--r-- | components/net/lwip-1.4.1/src/include/netif/ethernetif.h | 4 | ||||
| -rw-r--r-- | components/net/lwip-1.4.1/src/lwipopts.h | 22 | ||||
| -rw-r--r-- | components/net/lwip-1.4.1/src/netif/ethernetif.c | 45 | ||||
| -rw-r--r-- | components/net/lwip/apps/ping.c | 189 |
6 files changed, 300 insertions, 197 deletions
diff --git a/components/net/lwip-1.3.2/apps/ping.c b/components/net/lwip-1.3.2/apps/ping.c index d3c3dd399b..1e595dce61 100644 --- a/components/net/lwip-1.3.2/apps/ping.c +++ b/components/net/lwip-1.3.2/apps/ping.c @@ -39,135 +39,140 @@ static u16_t ping_seq_num; struct _ip_addr { - rt_uint8_t addr0, addr1, addr2, addr3; + rt_uint8_t addr0, addr1, addr2, addr3; }; /** Prepare a echo ICMP request */ static void ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len) { - size_t i; - size_t data_len = len - sizeof(struct icmp_echo_hdr); + size_t i; + size_t data_len = len - sizeof(struct icmp_echo_hdr); - ICMPH_TYPE_SET(iecho, ICMP_ECHO); - ICMPH_CODE_SET(iecho, 0); - iecho->chksum = 0; - iecho->id = PING_ID; - iecho->seqno = htons(++ping_seq_num); + ICMPH_TYPE_SET(iecho, ICMP_ECHO); + ICMPH_CODE_SET(iecho, 0); + iecho->chksum = 0; + iecho->id = PING_ID; + iecho->seqno = htons(++ping_seq_num); - /* fill the additional data buffer with some data */ - for(i = 0; i < data_len; i++) - { - ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i; - } + /* fill the additional data buffer with some data */ + for(i = 0; i < data_len; i++) + { + ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i; + } - iecho->chksum = inet_chksum(iecho, len); + iecho->chksum = inet_chksum(iecho, len); } /* Ping using the socket ip */ -static err_t ping_send(int s, struct ip_addr *addr) +static err_t ping_send(int s, struct ip_addr *addr, int size) { - int err; - struct icmp_echo_hdr *iecho; - struct sockaddr_in to; - size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE; - LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff); + int err; + struct icmp_echo_hdr *iecho; + struct sockaddr_in to; + size_t ping_size = sizeof(struct icmp_echo_hdr) + size; + LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff); - iecho = rt_malloc(ping_size); - if (iecho == RT_NULL) - { - return ERR_MEM; - } + iecho = rt_malloc(ping_size); + if (iecho == RT_NULL) + { + return ERR_MEM; + } - ping_prepare_echo(iecho, (u16_t)ping_size); + ping_prepare_echo(iecho, (u16_t)ping_size); - to.sin_len = sizeof(to); - to.sin_family = AF_INET; - to.sin_addr.s_addr = addr->addr; + to.sin_len = sizeof(to); + to.sin_family = AF_INET; + to.sin_addr.s_addr = addr->addr; - err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to)); - rt_free(iecho); + err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to)); + rt_free(iecho); - return (err ? ERR_OK : ERR_VAL); + return (err ? ERR_OK : ERR_VAL); } static void ping_recv(int s) { - char buf[64]; - int fromlen, len; - struct sockaddr_in from; - struct ip_hdr *iphdr; - struct icmp_echo_hdr *iecho; - struct _ip_addr *addr; + char buf[64]; + int fromlen, len; + struct sockaddr_in from; + struct ip_hdr *iphdr; + struct icmp_echo_hdr *iecho; + struct _ip_addr *addr; - while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) - { - if (len >= (sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) - { - addr = (struct _ip_addr *)&(from.sin_addr); - rt_kprintf("ping: recv %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3); + while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) + { + if (len >= (sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) + { + addr = (struct _ip_addr *)&(from.sin_addr); + rt_kprintf("ping: recv %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3); - iphdr = (struct ip_hdr *)buf; - iecho = (struct icmp_echo_hdr *)(buf+(IPH_HL(iphdr) * 4)); - if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) - { - return; - } - else - { - rt_kprintf("ping: drop\n"); - } - } - } + iphdr = (struct ip_hdr *)buf; + iecho = (struct icmp_echo_hdr *)(buf+(IPH_HL(iphdr) * 4)); + if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) + { + return; + } + else + { + rt_kprintf("ping: drop\n"); + } + } + } - if (len <= 0) - { - rt_kprintf("ping: timeout\n"); - } + if (len <= 0) + { + rt_kprintf("ping: timeout\n"); + } } rt_err_t ping(char* target, rt_uint32_t time, rt_size_t size) { - int s; - int timeout = PING_RCV_TIMEO; - struct ip_addr ping_target; - rt_uint32_t send_time; - struct _ip_addr - { - rt_uint8_t addr0, addr1, addr2, addr3; - } *addr; + int s; + int timeout = PING_RCV_TIMEO; + struct ip_addr ping_target; + rt_uint32_t send_time; + struct _ip_addr + { + rt_uint8_t addr0, addr1, addr2, addr3; + } *addr; send_time = 0; - if (inet_aton(target, (struct in_addr*)&ping_target) == 0) return -RT_ERROR; - addr = (struct _ip_addr*)&ping_target; + if(size == 0) + size = PING_DATA_SIZE; - if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) - { - rt_kprintf("create socket failled\n"); - return -RT_ERROR; - } + if (inet_aton(target, (struct in_addr*)&ping_target) == 0) return -RT_ERROR; + addr = (struct _ip_addr*)&ping_target; - lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); + if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) + { + rt_kprintf("create socket failled\n"); + return -RT_ERROR; + } - while (1) - { - if (ping_send(s, &ping_target) == ERR_OK) - { - rt_kprintf("ping: send %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3); - ping_recv(s); - } - else - { - rt_kprintf("ping: send %d.%d.%d.%d - error\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3); - } + lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - send_time ++; - if (send_time >= time) break; /* send ping times reached, stop */ + while (1) + { + if (ping_send(s, &ping_target, size) == ERR_OK) + { + rt_kprintf("ping: send %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3); + ping_recv(s); + } + else + { + rt_kprintf("ping: send %d.%d.%d.%d - error\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3); + } - rt_thread_delay(PING_DELAY); /* take a delay */ - } + send_time ++; + if (send_time >= time) break; /* send ping times reached, stop */ - return RT_EOK; + rt_thread_delay(PING_DELAY); /* take a delay */ + } + + lwip_close(s); + + return RT_EOK; } #ifdef RT_USING_FINSH #include <finsh.h> diff --git a/components/net/lwip-1.4.1/src/arch/sys_arch.c b/components/net/lwip-1.4.1/src/arch/sys_arch.c index ee1d952410..287c861f04 100644 --- a/components/net/lwip-1.4.1/src/arch/sys_arch.c +++ b/components/net/lwip-1.4.1/src/arch/sys_arch.c @@ -29,6 +29,11 @@ #include <string.h> +/* + * Initialize the network interface device + * + * @return the operation status, ERR_OK on OK, ERR_IF on error + */ static err_t netif_device_init(struct netif *netif) { struct eth_device *ethif; @@ -53,7 +58,9 @@ static err_t netif_device_init(struct netif *netif) return ERR_IF; } - +/* + * Initialize the ethernetif layer and set network interface device up + */ static void tcpip_init_done_callback(void *arg) { rt_device_t device; @@ -108,7 +115,7 @@ static void tcpip_init_done_callback(void *arg) netif_set_up(ethif->netif); } -#ifdef LWIP_NETIF_LINK_CALLBACK +#if LWIP_NETIF_LINK_CALLBACK netif_set_link_up(ethif->netif); #endif @@ -182,6 +189,11 @@ void lwip_sys_init(void) lwip_system_init(); } +/* + * Create a new semaphore + * + * @return the operation status, ERR_OK on OK; others on error + */ err_t sys_sem_new(sys_sem_t *sem, u8_t count) { static unsigned short counter = 0; @@ -204,17 +216,31 @@ err_t sys_sem_new(sys_sem_t *sem, u8_t count) } } +/* + * Deallocate a semaphore + */ void sys_sem_free(sys_sem_t *sem) { RT_DEBUG_NOT_IN_INTERRUPT; rt_sem_delete(*sem); } +/* + * Signal a semaphore + */ void sys_sem_signal(sys_sem_t *sem) { rt_sem_release(*sem); } +/* + * Block the thread while waiting for the semaphore to be signaled + * + * @return If the timeout argument is non-zero, it will return the number of milliseconds + * spent waiting for the semaphore to be signaled; If the semaphore isn't signaled + * within the specified time, it will return SYS_ARCH_TIMEOUT; If the thread doesn't + * wait for the semaphore, it will return zero + */ u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout) { rt_err_t ret; @@ -354,6 +380,11 @@ void sys_mutex_set_invalid(sys_mutex_t *mutex) /* ====================== Mailbox ====================== */ +/* + * Create an empty mailbox for maximum "size" elements + * + * @return the operation status, ERR_OK on OK; others on error + */ err_t sys_mbox_new(sys_mbox_t *mbox, int size) { static unsigned short counter = 0; @@ -376,6 +407,9 @@ err_t sys_mbox_new(sys_mbox_t *mbox, int size) return ERR_MEM; } +/* + * Deallocate a mailbox + */ void sys_mbox_free(sys_mbox_t *mbox) { RT_DEBUG_NOT_IN_INTERRUPT; @@ -399,6 +433,11 @@ void sys_mbox_post(sys_mbox_t *mbox, void *msg) return; } +/* + * Try to post the "msg" to the mailbox + * + * @return return ERR_OK if the "msg" is posted, ERR_MEM if the mailbox is full + */ err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg) { if (rt_mb_send(*mbox, (rt_uint32_t)msg) == RT_EOK) @@ -502,6 +541,11 @@ void sys_mbox_set_invalid(sys_mbox_t *mbox) /* ====================== System ====================== */ +/* + * Start a new thread named "name" with priority "prio" that will begin + * its execution in the function "thread()". The "arg" argument will be + * passed as an argument to the thread() function + */ sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, diff --git a/components/net/lwip-1.4.1/src/include/netif/ethernetif.h b/components/net/lwip-1.4.1/src/include/netif/ethernetif.h index 53dacf2270..2019df7017 100644 --- a/components/net/lwip-1.4.1/src/include/netif/ethernetif.h +++ b/components/net/lwip-1.4.1/src/include/netif/ethernetif.h @@ -5,7 +5,11 @@ #include <rtthread.h> #define NIOCTL_GADDR 0x01 +#ifndef RT_LWIP_ETH_MTU #define ETHERNET_MTU 1500 +#else +#define ETHERNET_MTU RT_LWIP_ETH_MTU +#endif struct eth_device { diff --git a/components/net/lwip-1.4.1/src/lwipopts.h b/components/net/lwip-1.4.1/src/lwipopts.h index 10cb7893a7..bff172e280 100644 --- a/components/net/lwip-1.4.1/src/lwipopts.h +++ b/components/net/lwip-1.4.1/src/lwipopts.h @@ -38,8 +38,9 @@ #define LWIP_PLATFORM_BYTESWAP 0 #define BYTE_ORDER LITTLE_ENDIAN -/* Enable SO_RCVTIMEO processing. */ +/* Enable SO_RCVTIMEO/LWIP_SO_SNDTIMEO processing. */ #define LWIP_SO_RCVTIMEO 1 +#define LWIP_SO_SNDTIMEO 1 /* #define RT_LWIP_DEBUG */ @@ -117,6 +118,8 @@ /* the number of simultaneously queued TCP */ #ifdef RT_LWIP_TCP_SEG_NUM +#define MEMP_NUM_TCP_SEG RT_LWIP_TCP_SEG_NUM +#else #define MEMP_NUM_TCP_SEG TCP_SND_QUEUELEN #endif @@ -135,12 +138,12 @@ /* ---------- Pbuf options ---------- */ /* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */ #ifdef RT_LWIP_PBUF_NUM -#define PBUF_POOL_SIZE RT_LWIP_PBUF_NUM +#define PBUF_POOL_SIZE RT_LWIP_PBUF_NUM #endif /* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */ #ifdef RT_LWIP_PBUF_POOL_BUFSIZE -#define PBUF_POOL_BUFSIZE RT_LWIP_PBUF_POOL_BUFSIZE +#define PBUF_POOL_BUFSIZE RT_LWIP_PBUF_POOL_BUFSIZE #endif /* PBUF_LINK_HLEN: the number of bytes that should be allocated for a @@ -176,7 +179,7 @@ /* TCP sender buffer space (bytes). */ #ifdef RT_LWIP_TCP_SND_BUF -#define TCP_SND_BUF RT_LWIP_TCP_SND_BUF +#define TCP_SND_BUF RT_LWIP_TCP_SND_BUF #else #define TCP_SND_BUF (TCP_MSS * 2) #endif @@ -230,10 +233,15 @@ /* IP reassembly and segmentation.These are orthogonal even * if they both deal with IP fragments */ -#define IP_REASSEMBLY 0 +#ifdef RT_LWIP_REASSEMBLY_FRAG +#define IP_REASSEMBLY 1 +#define IP_FRAG 1 #define IP_REASS_MAX_PBUFS 10 #define MEMP_NUM_REASSDATA 10 +#else +#define IP_REASSEMBLY 0 #define IP_FRAG 0 +#endif /* ---------- ICMP options ---------- */ #define ICMP_TTL 255 @@ -307,7 +315,7 @@ * in this file. */ #ifdef RT_LWIP_PPPOE -#define PPPOE_SUPPORT 1 +#define PPPOE_SUPPORT 1 #else #define PPPOE_SUPPORT 0 #endif @@ -315,7 +323,7 @@ #ifdef RT_LWIP_PPPOS #define PPPOS_SUPPORT 1 #else -#define PPPOS_SUPPORT 0 +#define PPPOS_SUPPORT 0 #endif #define PAP_SUPPORT 1 /* Set > 0 for PAP. */ diff --git a/components/net/lwip-1.4.1/src/netif/ethernetif.c b/components/net/lwip-1.4.1/src/netif/ethernetif.c index 2853fdcd7c..ab7743290a 100644 --- a/components/net/lwip-1.4.1/src/netif/ethernetif.c +++ b/components/net/lwip-1.4.1/src/netif/ethernetif.c @@ -67,6 +67,7 @@ #define netifapi_netif_set_link_up(n) netifapi_netif_common(n, netif_set_link_up, NULL) #define netifapi_netif_set_link_down(n) netifapi_netif_common(n, netif_set_link_down, NULL) +#ifndef LWIP_NO_TX_THREAD /** * Tx message structure for Ethernet interface */ @@ -85,7 +86,9 @@ static char eth_tx_thread_stack[512]; static char eth_tx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * 4]; static char eth_tx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE]; #endif +#endif +#ifndef LWIP_NO_RX_THREAD static struct rt_mailbox eth_rx_thread_mb; static struct rt_thread eth_rx_thread; #ifndef RT_LWIP_ETHTHREAD_PRIORITY @@ -97,12 +100,15 @@ static char eth_rx_thread_stack[1024]; static char eth_rx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * 4]; static char eth_rx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE]; #endif +#endif static err_t ethernetif_linkoutput(struct netif *netif, struct pbuf *p) { +#ifndef LWIP_NO_TX_THREAD struct eth_tx_msg msg; struct eth_device* enetif; + RT_ASSERT(netif != RT_NULL); enetif = (struct eth_device*)netif->state; /* send a message to eth tx thread */ @@ -113,7 +119,17 @@ static err_t ethernetif_linkoutput(struct netif *netif, struct pbuf *p) /* waiting for ack */ rt_sem_take(&(enetif->tx_ack), RT_WAITING_FOREVER); } +#else + struct eth_device* enetif; + RT_ASSERT(netif != RT_NULL); + enetif = (struct eth_device*)netif->state; + + if (enetif->eth_tx(&(enetif->parent), p) != RT_EOK) + { + return ERR_IF; + } +#endif return ERR_OK; } @@ -241,6 +257,7 @@ rt_err_t eth_device_init(struct eth_device * dev, char *name) return eth_device_init_with_flag(dev, name, flags); } +#ifndef LWIP_NO_RX_THREAD rt_err_t eth_device_ready(struct eth_device* dev) { if (dev->netif) @@ -267,7 +284,20 @@ rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up) /* post message to ethernet thread */ return rt_mb_send(ð_rx_thread_mb, (rt_uint32_t)dev); } +#else +/* NOTE: please not use it in interrupt when no RxThread exist */ +rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up) +{ + if (up == RT_TRUE) + netifapi_netif_set_link_up(dev->netif); + else + netifapi_netif_set_link_down(dev->netif); + + return RT_EOK; +} +#endif +#ifndef LWIP_NO_TX_THREAD /* Ethernet Tx Thread */ static void eth_tx_thread_entry(void* parameter) { @@ -297,7 +327,9 @@ static void eth_tx_thread_entry(void* parameter) } } } +#endif +#ifndef LWIP_NO_RX_THREAD /* Ethernet Rx Thread */ static void eth_rx_thread_entry(void* parameter) { @@ -349,13 +381,15 @@ static void eth_rx_thread_entry(void* parameter) } } } +#endif int eth_system_device_init(void) { rt_err_t result = RT_EOK; - /* initialize Rx thread. - * initialize mailbox and create Ethernet Rx thread */ + /* initialize Rx thread. */ +#ifndef LWIP_NO_RX_THREAD + /* initialize mailbox and create Ethernet Rx thread */ result = rt_mb_init(ð_rx_thread_mb, "erxmb", ð_rx_thread_mb_pool[0], sizeof(eth_rx_thread_mb_pool)/4, RT_IPC_FLAG_FIFO); @@ -367,8 +401,10 @@ int eth_system_device_init(void) RT_ASSERT(result == RT_EOK); result = rt_thread_startup(ð_rx_thread); RT_ASSERT(result == RT_EOK); +#endif /* initialize Tx thread */ +#ifndef LWIP_NO_TX_THREAD /* initialize mailbox and create Ethernet Tx thread */ result = rt_mb_init(ð_tx_thread_mb, "etxmb", ð_tx_thread_mb_pool[0], sizeof(eth_tx_thread_mb_pool)/4, @@ -382,8 +418,9 @@ int eth_system_device_init(void) result = rt_thread_startup(ð_tx_thread); RT_ASSERT(result == RT_EOK); - - return 0; +#endif + + return (int)result; } INIT_DEVICE_EXPORT(eth_system_device_init); diff --git a/components/net/lwip/apps/ping.c b/components/net/lwip/apps/ping.c index d3c3dd399b..1e595dce61 100644 --- a/components/net/lwip/apps/ping.c +++ b/components/net/lwip/apps/ping.c @@ -39,135 +39,140 @@ static u16_t ping_seq_num; struct _ip_addr { - rt_uint8_t addr0, addr1, addr2, addr3; + rt_uint8_t addr0, addr1, addr2, addr3; }; /** Prepare a echo ICMP request */ static void ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len) { - size_t i; - size_t data_len = len - sizeof(struct icmp_echo_hdr); + size_t i; + size_t data_len = len - sizeof(struct icmp_echo_hdr); - ICMPH_TYPE_SET(iecho, ICMP_ECHO); - ICMPH_CODE_SET(iecho, 0); - iecho->chksum = 0; - iecho->id = PING_ID; - iecho->seqno = htons(++ping_seq_num); + ICMPH_TYPE_SET(iecho, ICMP_ECHO); + ICMPH_CODE_SET(iecho, 0); + iecho->chksum = 0; + iecho->id = PING_ID; + iecho->seqno = htons(++ping_seq_num); - /* fill the additional data buffer with some data */ - for(i = 0; i < data_len; i++) - { - ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i; - } + /* fill the additional data buffer with some data */ + for(i = 0; i < data_len; i++) + { + ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i; + } - iecho->chksum = inet_chksum(iecho, len); + iecho->chksum = inet_chksum(iecho, len); } /* Ping using the socket ip */ -static err_t ping_send(int s, struct ip_addr *addr) +static err_t ping_send(int s, struct ip_addr *addr, int size) { - int err; - struct icmp_echo_hdr *iecho; - struct sockaddr_in to; - size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE; - LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff); + int err; + struct icmp_echo_hdr *iecho; + struct sockaddr_in to; + size_t ping_size = sizeof(struct icmp_echo_hdr) + size; + LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff); - iecho = rt_malloc(ping_size); - if (iecho == RT_NULL) - { - return ERR_MEM; - } + iecho = rt_malloc(ping_size); + if (iecho == RT_NULL) + { + return ERR_MEM; + } - ping_prepare_echo(iecho, (u16_t)ping_size); + ping_prepare_echo(iecho, (u16_t)ping_size); - to.sin_len = sizeof(to); - to.sin_family = AF_INET; - to.sin_addr.s_addr = addr->addr; + to.sin_len = sizeof(to); + to.sin_family = AF_INET; + to.sin_addr.s_addr = addr->addr; - err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to)); - rt_free(iecho); + err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to)); + rt_free(iecho); - return (err ? ERR_OK : ERR_VAL); + return (err ? ERR_OK : ERR_VAL); } static void ping_recv(int s) { - char buf[64]; - int fromlen, len; - struct sockaddr_in from; - struct ip_hdr *iphdr; - struct icmp_echo_hdr *iecho; - struct _ip_addr *addr; + char buf[64]; + int fromlen, len; + struct sockaddr_in from; + struct ip_hdr *iphdr; + struct icmp_echo_hdr *iecho; + struct _ip_addr *addr; - while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) - { - if (len >= (sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) - { - addr = (struct _ip_addr *)&(from.sin_addr); - rt_kprintf("ping: recv %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3); + while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) + { + if (len >= (sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) + { + addr = (struct _ip_addr *)&(from.sin_addr); + rt_kprintf("ping: recv %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3); - iphdr = (struct ip_hdr *)buf; - iecho = (struct icmp_echo_hdr *)(buf+(IPH_HL(iphdr) * 4)); - if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) - { - return; - } - else - { - rt_kprintf("ping: drop\n"); - } - } - } + iphdr = (struct ip_hdr *)buf; + iecho = (struct icmp_echo_hdr *)(buf+(IPH_HL(iphdr) * 4)); + if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) + { + return; + } + else + { + rt_kprintf("ping: drop\n"); + } + } + } - if (len <= 0) - { - rt_kprintf("ping: timeout\n"); - } + if (len <= 0) + { + rt_kprintf("ping: timeout\n"); + } } rt_err_t ping(char* target, rt_uint32_t time, rt_size_t size) { - int s; - int timeout = PING_RCV_TIMEO; - struct ip_addr ping_target; - rt_uint32_t send_time; - struct _ip_addr - { - rt_uint8_t addr0, addr1, addr2, addr3; - } *addr; + int s; + int timeout = PING_RCV_TIMEO; + struct ip_addr ping_target; + rt_uint32_t send_time; + struct _ip_addr + { + rt_uint8_t addr0, addr1, addr2, addr3; + } *addr; send_time = 0; - if (inet_aton(target, (struct in_addr*)&ping_target) == 0) return -RT_ERROR; - addr = (struct _ip_addr*)&ping_target; + if(size == 0) + size = PING_DATA_SIZE; - if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) - { - rt_kprintf("create socket failled\n"); - return -RT_ERROR; - } + if (inet_aton(target, (struct in_addr*)&ping_target) == 0) return -RT_ERROR; + addr = (struct _ip_addr*)&ping_target; - lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); + if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) + { + rt_kprintf("create socket failled\n"); + return -RT_ERROR; + } - while (1) - { - if (ping_send(s, &ping_target) == ERR_OK) - { - rt_kprintf("ping: send %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3); - ping_recv(s); - } - else - { - rt_kprintf("ping: send %d.%d.%d.%d - error\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3); - } + lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - send_time ++; - if (send_time >= time) break; /* send ping times reached, stop */ + while (1) + { + if (ping_send(s, &ping_target, size) == ERR_OK) + { + rt_kprintf("ping: send %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3); + ping_recv(s); + } + else + { + rt_kprintf("ping: send %d.%d.%d.%d - error\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3); + } - rt_thread_delay(PING_DELAY); /* take a delay */ - } + send_time ++; + if (send_time >= time) break; /* send ping times reached, stop */ - return RT_EOK; + rt_thread_delay(PING_DELAY); /* take a delay */ + } + + lwip_close(s); + + return RT_EOK; } #ifdef RT_USING_FINSH #include <finsh.h> |
