mirror of
https://github.com/ep00ch/lwip-contrib-mac.git
synced 2025-02-22 08:29:06 +00:00
Add ping sample from unix ports to reuse it in any ports.
This commit is contained in:
parent
f949381963
commit
6126163ef6
170
apps/ping/ping.c
Normal file
170
apps/ping/ping.c
Normal file
@ -0,0 +1,170 @@
|
||||
#include "ping.h"
|
||||
|
||||
/* lwIP core includes */
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/mem.h"
|
||||
#include "lwip/raw.h"
|
||||
#include "lwip/icmp.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/sockets.h"
|
||||
|
||||
#if LWIP_RAW
|
||||
|
||||
/* lwIP ping identifier */
|
||||
#define LWIP_PING_ID 0xAFAF
|
||||
|
||||
/* ping variables */
|
||||
static int ping_seq_num;
|
||||
static u32_t ping_time;
|
||||
|
||||
#if NO_SYS
|
||||
/* port-defined functions used for timer execution */
|
||||
void sys_msleep(u32_t ms);
|
||||
u32_t sys_now();
|
||||
#endif /* NO_SYS */
|
||||
|
||||
#if LWIP_SOCKET
|
||||
/* Ping using the socket ip */
|
||||
static void
|
||||
ping_send(int s, struct ip_addr *addr)
|
||||
{
|
||||
struct icmp_echo_hdr *iecho;
|
||||
struct sockaddr_in to;
|
||||
|
||||
if (!(iecho = mem_malloc(sizeof(struct icmp_echo_hdr)))) {
|
||||
return;
|
||||
}
|
||||
|
||||
ICMPH_TYPE_SET(iecho,ICMP_ECHO);
|
||||
iecho->chksum = 0;
|
||||
iecho->id = LWIP_PING_ID;
|
||||
iecho->seqno = htons(++ping_seq_num);
|
||||
iecho->chksum = inet_chksum(iecho, sizeof(*iecho));
|
||||
|
||||
to.sin_len = sizeof(to);
|
||||
to.sin_family = AF_INET;
|
||||
to.sin_addr.s_addr = addr->addr;
|
||||
|
||||
lwip_sendto(s, iecho, sizeof(*iecho), 0, (struct sockaddr*)&to, sizeof(to));
|
||||
|
||||
mem_free(iecho);
|
||||
}
|
||||
|
||||
static void
|
||||
ping_recv(int s, struct ip_addr *addr)
|
||||
{
|
||||
char buf[256];
|
||||
int fromlen, len;
|
||||
struct sockaddr_in from;
|
||||
struct icmp_echo_hdr *iecho;
|
||||
|
||||
len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, &fromlen);
|
||||
|
||||
if (len >= sizeof(struct icmp_echo_hdr)) {
|
||||
iecho = (struct icmp_echo_hdr *)buf;
|
||||
if ((iecho->id == LWIP_PING_ID) && (iecho->seqno == htons(ping_seq_num))) {
|
||||
LWIP_DEBUGF( LWIP_DBG_ON, ("ping recv "));
|
||||
ip_addr_debug_print(LWIP_DBG_ON, (struct ip_addr *)&(from.sin_addr));
|
||||
LWIP_DEBUGF( LWIP_DBG_ON, (" %lu ms\n", (sys_now()-ping_time)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
ping_thread(void *arg)
|
||||
{
|
||||
int s;
|
||||
|
||||
if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
LWIP_DEBUGF( LWIP_DBG_ON, ("ping send "));
|
||||
ip_addr_debug_print(LWIP_DBG_ON, &(netif_default->gw));
|
||||
LWIP_DEBUGF( LWIP_DBG_ON, ("\n"));
|
||||
ping_send(s, &(netif_default->gw));
|
||||
ping_time = sys_now();
|
||||
ping_recv(s, &(netif_default->gw));
|
||||
sys_msleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
#else /* LWIP_SOCKET */
|
||||
|
||||
/* Ping using the raw ip */
|
||||
static u8_t
|
||||
ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, struct ip_addr *addr)
|
||||
{
|
||||
struct icmp_echo_hdr *iecho;
|
||||
|
||||
if (pbuf_header( p, -PBUF_IP_HLEN)==0) {
|
||||
iecho = p->payload;
|
||||
|
||||
if ((iecho->id == LWIP_PING_ID) && (iecho->seqno == htons(ping_seq_num))) {
|
||||
LWIP_DEBUGF( LWIP_DBG_ON, ("ping recv "));
|
||||
ip_addr_debug_print(LWIP_DBG_ON, addr);
|
||||
LWIP_DEBUGF( LWIP_DBG_ON, (" %lu ms\n", (sys_now()-ping_time)));
|
||||
}
|
||||
}
|
||||
|
||||
return 1; /* eat the event */
|
||||
}
|
||||
|
||||
static void
|
||||
ping_send(struct raw_pcb *raw, struct ip_addr *addr)
|
||||
{
|
||||
struct pbuf *p;
|
||||
struct icmp_echo_hdr *iecho;
|
||||
|
||||
if (!(p = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr), PBUF_RAM))) {
|
||||
return;
|
||||
}
|
||||
|
||||
iecho = p->payload;
|
||||
ICMPH_TYPE_SET(iecho,ICMP_ECHO);
|
||||
iecho->chksum = 0;
|
||||
iecho->id = LWIP_PING_ID;
|
||||
iecho->seqno = htons(++ping_seq_num);
|
||||
iecho->chksum = inet_chksum(iecho, p->len);
|
||||
|
||||
ping_time = sys_now();
|
||||
|
||||
raw_sendto(raw,p,addr);
|
||||
|
||||
pbuf_free(p);
|
||||
}
|
||||
|
||||
static void
|
||||
ping_thread(void *arg)
|
||||
{
|
||||
struct raw_pcb *raw;
|
||||
|
||||
if (!(raw = raw_new(IP_PROTO_ICMP))) {
|
||||
return;
|
||||
}
|
||||
|
||||
raw_recv(raw,ping_recv,NULL);
|
||||
raw_bind(raw, IP_ADDR_ANY);
|
||||
|
||||
while (1) {
|
||||
LWIP_DEBUGF( LWIP_DBG_ON, ("ping send "));
|
||||
ip_addr_debug_print(LWIP_DBG_ON, &(netif_default->gw));
|
||||
LWIP_DEBUGF( LWIP_DBG_ON, ("\n"));
|
||||
ping_send(raw, &(netif_default->gw));
|
||||
sys_msleep(1000);
|
||||
}
|
||||
|
||||
/* Never reaches this */
|
||||
raw_remove(raw);
|
||||
}
|
||||
|
||||
#endif /* LWIP_SOCKET */
|
||||
|
||||
void
|
||||
ping_init(void)
|
||||
{ sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
||||
}
|
||||
|
||||
#endif /* LWIP_RAW */
|
6
apps/ping/ping.h
Normal file
6
apps/ping/ping.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef __PING_H__
|
||||
#define __PING_H__
|
||||
|
||||
void ping_init(void);
|
||||
|
||||
#endif /* __PING_H__ */
|
@ -117,6 +117,12 @@ SOURCE="..\..\apps\netio\netio.c"
|
||||
SOURCE="..\..\apps\netbios\netbios.c"
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "ping"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE="..\..\apps\ping\ping.c"
|
||||
# End Source File
|
||||
# End Group
|
||||
# End Group
|
||||
# Begin Group "header"
|
||||
|
||||
@ -147,6 +153,12 @@ SOURCE="..\..\apps\netio\netio.h"
|
||||
SOURCE="..\..\apps\netbios\netbios.h"
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "ping"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE="..\..\apps\ping\ping.h"
|
||||
# End Source File
|
||||
# End Group
|
||||
# End Group
|
||||
# End Target
|
||||
# End Project
|
||||
|
@ -43,6 +43,8 @@
|
||||
#define LWIP_HAVE_LOOPIF 1
|
||||
|
||||
#define LWIP_COMPAT_SOCKETS 1
|
||||
#define LWIP_SO_RCVTIMEO 1
|
||||
#define LWIP_SO_RCVBUF 1
|
||||
|
||||
#define LWIP_TCPIP_CORE_LOCKING 0
|
||||
|
||||
|
@ -189,7 +189,7 @@ int init_adapter(int adapter_num, char* mac_addr)
|
||||
printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", ethaddr[0], ethaddr[1], ethaddr[2], ethaddr[3], ethaddr[4], ethaddr[5]);
|
||||
PacketSetBuff(lpAdapter,512000);
|
||||
PacketSetReadTimeout(lpAdapter,1);
|
||||
PacketSetHwFilter(lpAdapter,NDIS_PACKET_TYPE_ALL_LOCAL);
|
||||
PacketSetHwFilter(lpAdapter,NDIS_PACKET_TYPE_ALL_LOCAL | NDIS_PACKET_TYPE_PROMISCUOUS);
|
||||
if ((lpPacket = PacketAllocatePacket())==NULL) {
|
||||
return (-1);
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ static LONGLONG sys_get_ms_longlong()
|
||||
return (u32_t)(((ret)*1000)/freq.QuadPart);
|
||||
}
|
||||
|
||||
u32_t sys_get_ms()
|
||||
u32_t sys_now()
|
||||
{
|
||||
return sys_get_ms_longlong();
|
||||
}
|
||||
|
@ -57,6 +57,7 @@
|
||||
#include "../../apps/httpserver_raw/httpd.h"
|
||||
#include "../../apps/netio/netio.h"
|
||||
#include "../../apps/netbios/netbios.h"
|
||||
#include "../../apps/ping/ping.h"
|
||||
|
||||
#if NO_SYS
|
||||
/* ... then we need information about the timer intervals: */
|
||||
@ -77,8 +78,8 @@ void update_adapter(void);
|
||||
#if NO_SYS
|
||||
/* port-defined functions used for timer execution */
|
||||
void sys_init_timing();
|
||||
u32_t sys_get_ms();
|
||||
#endif /* NO_SYS*/
|
||||
u32_t sys_now();
|
||||
#endif /* NO_SYS */
|
||||
|
||||
/* globales variables for netifs */
|
||||
/* THE ethernet interface */
|
||||
@ -111,7 +112,7 @@ static timers_infos timers_table[] = {
|
||||
#endif /* LWIP_DHCP */
|
||||
#if IP_REASSEMBLY
|
||||
{ 0, IP_TMR_INTERVAL, ip_reass_tmr},
|
||||
#endif /* IP_REASSEMBLY*/
|
||||
#endif /* IP_REASSEMBLY */
|
||||
#if LWIP_AUTOIP
|
||||
{ 0, AUTOIP_TMR_INTERVAL, autoip_tmr},
|
||||
#endif /* LWIP_AUTOIP */
|
||||
@ -137,7 +138,7 @@ timers_update()
|
||||
|
||||
int cur_time, time_diff, idxtimer;
|
||||
|
||||
cur_time = sys_get_ms();
|
||||
cur_time = sys_now();
|
||||
time_diff = cur_time - last_time;
|
||||
|
||||
/* the '> 0' is an easy wrap-around check: the big gap at
|
||||
@ -200,6 +201,10 @@ msvc_netif_init()
|
||||
static void
|
||||
apps_init()
|
||||
{
|
||||
#if LWIP_RAW
|
||||
ping_init();
|
||||
#endif /* LWIP_RAW */
|
||||
|
||||
#if LWIP_UDP
|
||||
netbios_init();
|
||||
#endif /* LWIP_UDP */
|
||||
|
Loading…
x
Reference in New Issue
Block a user