-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping.cpp
More file actions
161 lines (133 loc) · 4.67 KB
/
ping.cpp
File metadata and controls
161 lines (133 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* WebOutlet v1.01 by VDG
* Remote PowerOutlet WEB control on base ESP8266 chip.
* Auto-reset power if there are no answer on pings.
* Copyright (c) by Denis Vidjakin,
*
* PING related stuff
*
* 24.05.2017 v1.01 created by VDG
*/
#include "main.h"
#include "IPAddress.h"
#include <functional>
extern "C"
{
#include <lwip/ip.h>
#include <lwip/sys.h>
#include <lwip/raw.h>
#include <lwip/icmp.h>
#include <lwip/inet_chksum.h>
}
#define PING_DATA_SIZE 64
static struct raw_pcb *pcbraw = 0;
u8_t ping_recv( void *arg, raw_pcb *pcb, pbuf *pb, ip_addr *addr )
{ struct ip_hdr *ip = (struct ip_hdr*)pb->payload;
if( pbuf_header( pb, -PBUF_IP_HLEN ) == 0 )
{ struct icmp_echo_hdr *iecho = (struct icmp_echo_hdr*)pb->payload;
if( iecho->type == ICMP_ER )
{ gD.PingAsw( iecho->id, htons(iecho->seqno), ip->_ttl, ((byte*)iecho) + sizeof(struct icmp_echo_hdr) );
pbuf_free( pb );
return 1; /* eat the packet */
}
}
pbuf_header( pb, PBUF_IP_HLEN );
return 0; /* don't eat the packet */
}
//-------------------------------------------------------------------------------
struct raw_pcb *pcbGet( void )
{ if( !pcbraw )
{ pcbraw = raw_new(IP_PROTO_ICMP );
raw_recv( pcbraw, ping_recv, 0 );
raw_bind( pcbraw, IP_ADDR_ANY );
}
return pcbraw;
}
//-------------------------------------------------------------------------------
void CGlobalData::PingAsw( short Id, short SeqNo, short Ttl, const byte *Pkt )
{
if( mPingPhase != 1 ) return; // Not expecting answer
if( mPingSeqNo != SeqNo ) return; // Not expected pkt
mPingMs = millis() - mPingMs;
mPingPhase = 2;
}
//-------------------------------------------------------------------------------
int PingSend( byte *IpAddrB4, short Id, short Seq )
{ uint32_t long tm;
int i, Rc = 0,
Sz = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
struct pbuf *pb = pbuf_alloc( PBUF_IP, Sz, PBUF_RAM );
if( !pb )
return 0;
if( (pb->len == pb->tot_len) && (pb->next == NULL) )
{ struct icmp_echo_hdr *iecho = (struct icmp_echo_hdr *)pb->payload;
ICMPH_TYPE_SET( iecho, ICMP_ECHO );
ICMPH_CODE_SET( iecho, 0 );
iecho->chksum = 0;
iecho->id = Id; // 0000-ffff
iecho->seqno = htons( Seq ); // 0000-7fff
/* fill the additional data buffer with some data */
for(i = 0; i < PING_DATA_SIZE; i++)
((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)(Seq+i);
tm = millis();
//Serial.print( "#" ); Serial.print( iecho->id ); Serial.print( "-" ); Serial.print( Seq ); Serial.print( IpAddrB4[0] ); Serial.print( IpAddrB4[1] ); Serial.print( IpAddrB4[2] ); Serial.println( IpAddrB4[3] );
iecho->chksum = inet_chksum(iecho, Sz );
ip_addr_t addr;
memcpy( &addr, IpAddrB4, 4 );
raw_sendto( pcbGet(), pb, &addr );
Rc = 1;
}
pbuf_free(pb);
return Rc;
}
//-------------------------------------------------------------------------------
/*
void CGlobalData::PingCheck( void )
{
int i, n, bFin = 1;
byte *pb, *pt;
uint16_t us;
uint32_t ms = millis();
switch( cp->mPhase )
{ case 0: // New cycle
cp->mPhase++; // Sending pings
cp->mTime = ms;
case 1: // Wait for idle
if( ms - cp->mTime < cp->mInterval ) return; // Wait for interval before ping
cp->mPhase++;
cp->mTime = ms;
for( tsa=cp->mAddr; tsa; tsa=tsa->mNext ) // Clear data for new ping
{ if( tsa->mType != 8 ) continue; // Not a TSecAddr*
tsa->mVal = 0; // Reset fail counter
tsa->mPhase = 0;
}
case 2: // Sending pings
bFin = 1;
for( tsa=cp->mAddr; tsa; tsa=tsa->mNext )
{ if( tsa->mType != 8 ) continue; // Not a TSecAddr*
switch( tsa->mPhase )
{ case 0: // Send new ping
bFin = 0;
tsa->mPhase = 1; // Wait for data
tsa->mTime = millis();
if( ++mPingSeqNo & 0x8000 ) mPingSeqNo = 1;
tsa->mSeqNo = mPingSeqNo;
i = PingSend( tsa->mAddr, (long)tsa, tsa->mSeqNo, tsa );
if( !i )
{ tsa->mPhase = 4; // Failed to send
continue;
}
case 1: // Waiting for PingAsw
bFin = 0;
if( ms - tsa->mTime < cp->mTTL ) continue; // Wait for PingAsw
if( ++tsa->mVal >= cp->mTTLRetry ) // No answer
tsa->mPhase = 3; // No answer received and all retries are spent
else
tsa->mPhase = 0; // Retry to send ping
continue;
}
}
}
if( !bFin ) return; // Some address is not answered yet
}
*/
//-------------------------------------------------------------------------------