Browse Source

simhost: remove ping implementation and use ping app instead

Improve ping app a bit
master
Dirk Ziegelmeier 9 years ago
parent
commit
8ea25831dd
  1. 97
      apps/ping/ping.c
  2. 5
      apps/ping/ping.h
  3. 1
      ports/unix/unixsim/lwipopts.h
  4. 110
      ports/unix/unixsim/simhost.c

97
apps/ping/ping.c

@ -65,11 +65,6 @@
#define PING_DEBUG LWIP_DBG_ON
#endif
/** ping target - should be an "ip4_addr_t" */
#ifndef PING_TARGET
#define PING_TARGET (netif_default ? *netif_ip4_gw(netif_default) : (*IP4_ADDR_ANY4))
#endif
/** ping receive timeout - in milliseconds */
#ifndef PING_RCV_TIMEO
#define PING_RCV_TIMEO 1000
@ -96,6 +91,7 @@
#endif
/* ping variables */
static const ip_addr_t* ping_target;
static u16_t ping_seq_num;
static u32_t ping_time;
#if !PING_USE_SOCKETS
@ -127,14 +123,18 @@ ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
/* Ping using the socket ip */
static err_t
ping_send(int s, ip_addr_t *addr)
ping_send(int s, const ip_addr_t *addr)
{
int err;
struct icmp_echo_hdr *iecho;
struct sockaddr_in to;
struct sockaddr_storage to;
size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
LWIP_ASSERT("ping: expect IPv4 address", !IP_IS_V6(addr));
if(IP_IS_V6(addr) && !ip6_addr_isipv6mappedipv4(ip_2_ip6(addr))) {
/* todo: support ICMP6 echo */
return ERR_VAL;
}
iecho = (struct icmp_echo_hdr *)mem_malloc((mem_size_t)ping_size);
if (!iecho) {
@ -142,10 +142,24 @@ ping_send(int s, ip_addr_t *addr)
}
ping_prepare_echo(iecho, (u16_t)ping_size);
to.sin_len = sizeof(to);
to.sin_family = AF_INET;
inet_addr_from_ip4addr(&to.sin_addr, ip_2_ip4(addr));
#if LWIP_IPV4
if(IP_IS_V4(addr)) {
struct sockaddr_in *to4 = (struct sockaddr_in*)&to;
to4->sin_len = sizeof(to4);
to4->sin_family = AF_INET;
inet_addr_from_ip4addr(&to4->sin_addr, ip_2_ip4(addr));
}
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
if(IP_IS_V6(addr)) {
struct sockaddr_in6 *to6 = (struct sockaddr_in6*)&to;
to6->sin6_len = sizeof(to6);
to6->sin6_family = AF_INET6;
inet6_addr_from_ip6addr(&to6->sin6_addr, ip_2_ip6(addr));
}
#endif /* LWIP_IPV6 */
err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
@ -159,23 +173,37 @@ ping_recv(int s)
{
char buf[64];
int len;
struct sockaddr_in from;
struct sockaddr_storage from;
struct ip_hdr *iphdr;
struct icmp_echo_hdr *iecho;
int fromlen = sizeof(from);
while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) {
if (len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) {
if (from.sin_family != AF_INET) {
/* Ping is not IPv4 */
LWIP_DEBUGF( PING_DEBUG, ("ping: invalid sin_family\n"));
} else {
ip4_addr_t fromaddr;
inet_addr_to_ip4addr(&fromaddr, &from.sin_addr);
LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
ip4_addr_debug_print(PING_DEBUG, &fromaddr);
LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now() - ping_time)));
ip_addr_t fromaddr;
#if LWIP_IPV4
if(from.ss_family == AF_INET) {
struct sockaddr_in *from4 = (struct sockaddr_in*)&from;
inet_addr_to_ip4addr(ip_2_ip4(&fromaddr), &from4->sin_addr);
IP_SET_TYPE(&fromaddr, IPADDR_TYPE_V4);
}
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
if(from.ss_family == AF_INET6) {
struct sockaddr_in6 *from6 = (struct sockaddr_in6*)&from;
inet6_addr_to_ip6addr(ip_2_ip6(&fromaddr), &from6->sin6_addr);
IP_SET_TYPE(&fromaddr, IPADDR_TYPE_V6);
}
#endif /* LWIP_IPV6 */
LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
ip_addr_debug_print(PING_DEBUG, &fromaddr);
LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now() - ping_time)));
/* todo: support ICMP6 echo */
if (IP_IS_V4_VAL(fromaddr)) {
iphdr = (struct ip_hdr *)buf;
iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4));
if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) {
@ -203,7 +231,7 @@ ping_thread(void *arg)
{
int s;
int ret;
ip_addr_t ping_target;
#if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
int timeout = PING_RCV_TIMEO;
#else
@ -213,7 +241,16 @@ ping_thread(void *arg)
#endif
LWIP_UNUSED_ARG(arg);
if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) {
#if LWIP_IPV6
if(IP_IS_V4(ping_target) || ip6_addr_isipv6mappedipv4(ip_2_ip6(ping_target))) {
s = lwip_socket(AF_INET6, SOCK_RAW, IP_PROTO_ICMP);
} else {
s = lwip_socket(AF_INET6, SOCK_RAW, IP6_NEXTH_ICMP6);
}
#else
s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP);
#endif
if (s < 0) {
return;
}
@ -221,18 +258,16 @@ ping_thread(void *arg)
LWIP_ASSERT("setting receive timeout failed", ret == 0);
while (1) {
ip_addr_copy_from_ip4(ping_target, PING_TARGET);
if (ping_send(s, &ping_target) == ERR_OK) {
if (ping_send(s, ping_target) == ERR_OK) {
LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
ip_addr_debug_print(PING_DEBUG, &ping_target);
ip_addr_debug_print(PING_DEBUG, ping_target);
LWIP_DEBUGF( PING_DEBUG, ("\n"));
ping_time = sys_now();
ping_recv(s);
} else {
LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
ip_addr_debug_print(PING_DEBUG, &ping_target);
ip_addr_debug_print(PING_DEBUG, ping_target);
LWIP_DEBUGF( PING_DEBUG, (" - error\n"));
}
sys_msleep(PING_DELAY);
@ -336,8 +371,10 @@ ping_send_now(void)
#endif /* PING_USE_SOCKETS */
void
ping_init(void)
ping_init(const ip_addr_t* ping_addr)
{
ping_target = ping_addr;
#if PING_USE_SOCKETS
sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
#else /* PING_USE_SOCKETS */

5
apps/ping/ping.h

@ -1,6 +1,8 @@
#ifndef LWIP_PING_H
#define LWIP_PING_H
#include "lwip/ip_addr.h"
/**
* PING_USE_SOCKETS: Set to 1 to use sockets, otherwise the raw api is used
*/
@ -8,8 +10,7 @@
#define PING_USE_SOCKETS LWIP_SOCKET
#endif
void ping_init(void);
void ping_init(const ip_addr_t* ping_addr);
#if !PING_USE_SOCKETS
void ping_send_now(void);

1
ports/unix/unixsim/lwipopts.h

@ -82,6 +82,7 @@ extern unsigned char debug_flags;
#define IP_SOF_BROADCAST_RECV 1
#define IP_SOF_BROADCAST 1
#define SO_REUSE_RXTOALL 1
#define LWIP_SO_RCVTIMEO (LWIP_SOCKET)
/* ---------- Memory options ---------- */
/* MEM_ALIGNMENT: should be set to the alignment of the CPU for which

110
ports/unix/unixsim/simhost.c

@ -90,6 +90,7 @@
#include "apps/shell/shell.h"
#include "apps/chargen/chargen.h"
#include "apps/netio/netio.h"
#include "apps/ping/ping.h"
#include "lwip/apps/netbiosns.h"
#include "lwip/apps/mdns.h"
#include "lwip/apps/sntp.h"
@ -295,110 +296,6 @@ tcpip_init_done(void *arg)
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
#if LWIP_SOCKET
/* Ping using the socket api */
static int seq_num;
static void
ping_send(int s, const ip_addr_t *addr)
{
struct icmp_echo_hdr *iecho;
struct sockaddr_storage to;
if (!(iecho = (struct icmp_echo_hdr *)malloc(sizeof(struct icmp_echo_hdr))))
return;
ICMPH_TYPE_SET(iecho,ICMP_ECHO);
iecho->chksum = 0;
iecho->seqno = lwip_htons(seq_num);
iecho->chksum = inet_chksum(iecho, sizeof(*iecho));
#if LWIP_IPV4
if(!IP_IS_V6(addr)) {
struct sockaddr_in *to4 = (struct sockaddr_in*)&to;
to4->sin_len = sizeof(to);
to4->sin_family = AF_INET;
inet_addr_from_ip4addr(&to4->sin_addr, ip_2_ip4(addr));
}
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
if(IP_IS_V6(addr)) {
struct sockaddr_in6 *to6 = (struct sockaddr_in6*)&to;
to6->sin6_len = sizeof(to);
to6->sin6_family = AF_INET6;
inet6_addr_from_ip6addr(&to6->sin6_addr, ip_2_ip6(addr));
}
#endif /* LWIP_IPV6 */
lwip_sendto(s, iecho, sizeof(*iecho), 0, (struct sockaddr*)&to, sizeof(to));
free(iecho);
seq_num++;
}
static void
ping_recv(int s, const ip_addr_t *addr)
{
char buf[200];
socklen_t fromlen;
int len;
struct sockaddr_storage from;
ip_addr_t ip_from;
LWIP_UNUSED_ARG(addr);
len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, &fromlen);
#if LWIP_IPV4
if(from.ss_family == AF_INET) {
struct sockaddr_in *from4 = (struct sockaddr_in*)&from;
inet_addr_to_ip4addr(ip_2_ip4(&ip_from), &from4->sin_addr);
IP_SET_TYPE(&ip_from, IPADDR_TYPE_V4);
}
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
if(from.ss_family == AF_INET6) {
struct sockaddr_in6 *from6 = (struct sockaddr_in6*)&from;
inet6_addr_to_ip6addr(ip_2_ip6(&ip_from), &from6->sin6_addr);
IP_SET_TYPE(&ip_from, IPADDR_TYPE_V6);
}
#endif /* LWIP_IPV6 */
printf("Received %d bytes from %s\n", len, ipaddr_ntoa(&ip_from));
}
static void
ping_thread(void *arg)
{
int s;
LWIP_UNUSED_ARG(arg);
#if LWIP_IPV6
if(IP_IS_V4_VAL(ping_addr) || ip6_addr_isipv6mappedipv4(ip_2_ip6(&ping_addr))) {
s = lwip_socket(AF_INET6, SOCK_RAW, IP_PROTO_ICMP);
} else {
s = lwip_socket(AF_INET6, SOCK_RAW, IP6_NEXTH_ICMP6);
}
#else
s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP);
#endif
if(s < 0) {
return;
}
while (1) {
printf("sending ping\n");
ping_send(s,&ping_addr);
ping_recv(s,&ping_addr);
sleep(1);
}
}
#endif /* LWIP_SOCKET */
#if LWIP_HAVE_SLIPIF
/* (manual) host IP configuration */
@ -670,14 +567,13 @@ main_thread(void *arg)
printf("TCP/IP initialized.\n");
#if LWIP_SOCKET
if(ping_flag) {
sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
if (ping_flag) {
ping_init(&ping_addr);
}
#endif
printf("Applications started.\n");
#ifdef MEM_PERF
mem_perf_init("/tmp/memstats.client");
#endif /* MEM_PERF */

Loading…
Cancel
Save