|
16 | 16 | #include "EtherUtil.h" |
17 | 17 | #undef word // arduino nonsense |
18 | 18 |
|
19 | | -#define PINGPATTERN 0x42 |
| 19 | +#define ICMP_PING_PAYLOAD_PATTERN 0x42 |
| 20 | +#define ICMP_PING_PAYLOAD_SIZE 56 |
20 | 21 |
|
21 | 22 | // Avoid spurious pgmspace warnings - http://forum.jeelabs.net/node/327 |
22 | 23 | // See also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34734 |
@@ -247,10 +248,9 @@ static void make_arp_answer_from_request() { |
247 | 248 |
|
248 | 249 | static void make_echo_reply_from_request(uint16_t len) { |
249 | 250 | make_eth_ip_reply(); |
250 | | - gPB[ICMP_TYPE_P] = ICMP_TYPE_ECHOREPLY_V; |
251 | | - if (gPB[ICMP_CHECKSUM_P] > (0xFF-0x08)) |
252 | | - gPB[ICMP_CHECKSUM_P+1]++; |
253 | | - gPB[ICMP_CHECKSUM_P] += 0x08; |
| 251 | + IcmpHeader &ih = icmp_header(); |
| 252 | + ih.type = ICMP_TYPE_ECHOREPLY_V; |
| 253 | + ih.checksum = ih.checksum + 0x08; |
254 | 254 | EtherCard::packetSend(len); |
255 | 255 | } |
256 | 256 |
|
@@ -370,17 +370,15 @@ void EtherCard::clientIcmpRequest(const uint8_t *destip) { |
370 | 370 | IpHeader &iph = init_ip_frame(destip, IP_PROTO_ICMP_V); |
371 | 371 | iph.totalLen = HTONS(0x54); |
372 | 372 | fill_ip_hdr_checksum(iph); |
373 | | - gPB[ICMP_TYPE_P] = ICMP_TYPE_ECHOREQUEST_V; |
374 | | - gPB[ICMP_TYPE_P+1] = 0; // code |
375 | | - gPB[ICMP_CHECKSUM_H_P] = 0; |
376 | | - gPB[ICMP_CHECKSUM_L_P] = 0; |
377 | | - gPB[ICMP_IDENT_H_P] = 5; // some number |
378 | | - gPB[ICMP_IDENT_L_P] = EtherCard::myip[3]; // last byte of my IP |
379 | | - gPB[ICMP_IDENT_L_P+1] = 0; // seq number, high byte |
380 | | - gPB[ICMP_IDENT_L_P+2] = 1; // seq number, low byte, we send only 1 ping at a time |
381 | | - memset(gPB + ICMP_DATA_P, PINGPATTERN, 56); |
382 | | - fill_checksum(ICMP_CHECKSUM_H_P, ICMP_TYPE_P, 56+8,0); |
383 | | - packetSend(98); |
| 373 | + IcmpHeader &ih = icmp_header(); |
| 374 | + ih.type = ICMP_TYPE_ECHOREQUEST_V; |
| 375 | + ih.code = 0; |
| 376 | + ih.checksum = 0; |
| 377 | + ih.ping.identifier = HTONS(0x0500 | EtherCard::myip[3]); |
| 378 | + ih.ping.sequence = HTONS(1); |
| 379 | + memset(icmp_payload(), ICMP_PING_PAYLOAD_PATTERN, ICMP_PING_PAYLOAD_SIZE); |
| 380 | + fill_checksum(ih.checksum, (const uint8_t *)&ih, sizeof(IcmpHeader) + ICMP_PING_PAYLOAD_SIZE, 0); |
| 381 | + packetSend(icmp_payload() - gPB + ICMP_PING_PAYLOAD_SIZE); |
384 | 382 | } |
385 | 383 |
|
386 | 384 | void EtherCard::ntpRequest (uint8_t *ntpip, uint8_t srcport) { |
@@ -654,8 +652,8 @@ void EtherCard::registerPingCallback (const IcmpCallback callback) { |
654 | 652 | uint8_t EtherCard::packetLoopIcmpCheckReply (const uint8_t *ip_monitoredhost) { |
655 | 653 | const IpHeader &iph = ip_header(); |
656 | 654 | return iph.protocol == IP_PROTO_ICMP_V && |
657 | | - gPB[ICMP_TYPE_P]==ICMP_TYPE_ECHOREPLY_V && |
658 | | - gPB[ICMP_DATA_P]== PINGPATTERN && |
| 655 | + icmp_header().type == ICMP_TYPE_ECHOREPLY_V && |
| 656 | + icmp_payload()[0] == ICMP_PING_PAYLOAD_PATTERN && |
659 | 657 | check_ip_message_is_from(iph, ip_monitoredhost); |
660 | 658 | } |
661 | 659 |
|
@@ -790,11 +788,15 @@ uint16_t EtherCard::packetLoop (uint16_t plen) { |
790 | 788 | arpStoreSet(is_lan(myip, iph.spaddr) ? iph.spaddr : gwip, eh.shaddr); |
791 | 789 |
|
792 | 790 | #if ETHERCARD_ICMP |
793 | | - if (iph.protocol == IP_PROTO_ICMP_V && gPB[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V) |
794 | | - { //Service ICMP echo request (ping) |
795 | | - if (icmp_cb) |
796 | | - (*icmp_cb)(iph.spaddr); |
797 | | - make_echo_reply_from_request(plen); |
| 791 | + if (iph.protocol == IP_PROTO_ICMP_V) |
| 792 | + { |
| 793 | + const IcmpHeader &ih = icmp_header(); |
| 794 | + if (ih.type == ICMP_TYPE_ECHOREQUEST_V) |
| 795 | + { //Service ICMP echo request (ping) |
| 796 | + if (icmp_cb) |
| 797 | + (*icmp_cb)(iph.spaddr); |
| 798 | + make_echo_reply_from_request(plen); |
| 799 | + } |
798 | 800 | return 0; |
799 | 801 | } |
800 | 802 | #endif |
|
0 commit comments