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; 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))) { ip_addr_t fromaddr; inet_addr_to_ipaddr(&fromaddr, &from.sin_addr); LWIP_DEBUGF( PING_DEBUG, ("ping: recv ")); ip_addr_debug_print(PING_DEBUG, &fromaddr); LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\r\n", (sys_now() - ping_time))); 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))) { /* do some ping result processing */ PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER)); return; } else { LWIP_DEBUGF( PING_DEBUG, ("ping: drop\r\n")); } } } if (len == 0) { LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %"U32_F" ms - timeout\r\n", (sys_now()-ping_time))); } /* do some ping result processing */ PING_RESULT(0); }
/* Ping using the raw ip */ static u8_t ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *addr) { struct icmp_echo_hdr *iecho; LWIP_UNUSED_ARG(arg); LWIP_UNUSED_ARG(pcb); LWIP_UNUSED_ARG(addr); LWIP_ASSERT("p != NULL", p != NULL); if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) && pbuf_header( p, -PBUF_IP_HLEN) == 0) { iecho = (struct icmp_echo_hdr *)p->payload; if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) { LWIP_DEBUGF( PING_DEBUG, ("ping: recv ")); ip_addr_debug_print(PING_DEBUG, addr); LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now()-ping_time))); /* do some ping result processing */ PING_RESULT(1); pbuf_free(p); return 1; /* eat the packet */ } } return 0; /* don't eat the packet */ }
// Ping using the raw ip static u8_t ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *addr) { struct icmp_echo_hdr *iecho; LWIP_UNUSED_ARG(arg); LWIP_UNUSED_ARG(pcb); LWIP_UNUSED_ARG(addr); LWIP_ASSERT("p != NULL", p != NULL); if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) && pbuf_header(p, -PBUF_IP_HLEN) == 0) { iecho = (struct icmp_echo_hdr *)p->payload; if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) { LWIP_DEBUGF( PING_DEBUG, ("ping: recv ")); LWIP_DEBUGF( PING_DEBUG, ("%u.%u.%u.%u ",((u8_t *)addr)[0],((u8_t *)addr)[1],((u8_t *)addr)[2],((u8_t *)addr)[3])); LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\r\n", (sys_now()-ping_time))); // do some ping result processing PING_RESULT(1); pbuf_free(p); return 1; // eat the packet } // not eaten, restore original packet pbuf_header(p, PBUF_IP_HLEN); } return 0; // don't eat the packet }
/* Ping using the raw ip */ static u8_t ICACHE_FLASH_ATTR ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *addr) { struct icmp_echo_hdr *iecho = NULL; static u16_t seqno = 0; // struct ping_msg *pingmsg = (struct ping_msg*)arg; LWIP_UNUSED_ARG(arg); LWIP_UNUSED_ARG(pcb); LWIP_UNUSED_ARG(addr); LWIP_ASSERT("p != NULL", p != NULL); if (pbuf_header( p, -PBUF_IP_HLEN)==0) { iecho = (struct icmp_echo_hdr *)p->payload; if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num)) && iecho->type == ICMP_ER) { LWIP_DEBUGF( PING_DEBUG, ("ping: recv ")); ip_addr_debug_print(PING_DEBUG, addr); LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now()-ping_time))); if (iecho->seqno != seqno){ /* do some ping result processing */ { struct ip_hdr *iphdr = NULL; char ipaddrstr[16]; ip_addr_t source_ip; sys_untimeout(ping_timeout, pingmsg); os_bzero(&source_ip, sizeof(ip_addr_t)); os_bzero(ipaddrstr, sizeof(ipaddrstr)); uint32 delay = system_relative_time(pingmsg->ping_sent); delay /= PING_COARSE; iphdr = (struct ip_hdr*)((u8*)iecho - PBUF_IP_HLEN); source_ip.addr = iphdr->src.addr; ipaddr_ntoa_r(&source_ip,ipaddrstr, sizeof(ipaddrstr)); if (pingmsg->ping_opt->recv_function == NULL){ os_printf("recv %s: byte = %d, time = %d ms, seq = %d\n",ipaddrstr, PING_DATA_SIZE, delay, ntohs(iecho->seqno)); } else { struct ping_resp pingresp; os_bzero(&pingresp, sizeof(struct ping_resp)); pingresp.bytes = PING_DATA_SIZE; pingresp.resp_time = delay; pingresp.seqno = ntohs(iecho->seqno); pingresp.ping_err = 0; pingmsg->ping_opt->recv_function(pingmsg->ping_opt,(void*) &pingresp); } } seqno = iecho->seqno; } PING_RESULT(1); pbuf_free(p); return 1; /* eat the packet */ } // } else if(iecho->type == ICMP_ECHO){ // struct pbuf *q = NULL; // os_printf("receive ping request:seq=%d\n", ntohs(iecho->seqno)); // q = pbuf_alloc(PBUF_IP, (u16_t)p->tot_len, PBUF_RAM); // if (q!=NULL) { // pbuf_copy(q, p); // iecho = (struct icmp_echo_hdr *)q->payload; // ping_prepare_er(iecho, q->tot_len); // raw_sendto(pcb, q, addr); // pbuf_free(q); // } // pbuf_free(p); // return 1; // } } return 0; /* don't eat the packet */ }