-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathEthernetClient.cpp
213 lines (190 loc) · 6.53 KB
/
EthernetClient.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* Copyright 2018 Paul Stoffregen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <Arduino.h>
#include "Ethernet.h"
#include "Dns.h"
#include "utility/w5100.h"
int EthernetClient::connect(const char * host, uint16_t port)
{
DNSClient dns; // Look up the host first
IPAddress remote_addr;
if (_sockindex < MAX_SOCK_NUM) {
if (Ethernet.socketStatus(_sockindex) != SnSR::CLOSED) {
Ethernet.socketDisconnect(_sockindex); // TODO: should we call stop()?
}
_sockindex = MAX_SOCK_NUM;
}
dns.begin(Ethernet.dnsServerIP());
if (!dns.getHostByName(host, remote_addr)) return 0; // TODO: use _timeout
return connect(remote_addr, port);
}
int EthernetClient::connect(IPAddress ip, uint16_t port)
{
if (_sockindex < MAX_SOCK_NUM) {
if (Ethernet.socketStatus(_sockindex) != SnSR::CLOSED) {
Ethernet.socketDisconnect(_sockindex); // TODO: should we call stop()?
}
_sockindex = MAX_SOCK_NUM;
}
#if defined(ESP8266) || defined(ESP32)
if (ip == IPAddress((uint32_t)0) || ip == IPAddress(0xFFFFFFFFul)) return 0;
#else
if (ip == IPAddress(0ul) || ip == IPAddress(0xFFFFFFFFul)) return 0;
#endif
_sockindex = Ethernet.socketBegin(SnMR::TCP, 0);
if (_sockindex >= MAX_SOCK_NUM) return 0;
Ethernet.socketConnect(_sockindex, rawIPAddress(ip), port);
uint32_t start = millis();
while (1) {
uint8_t stat = Ethernet.socketStatus(_sockindex);
if (stat == SnSR::ESTABLISHED) return 1;
if (stat == SnSR::CLOSE_WAIT) return 1;
if (stat == SnSR::CLOSED) return 0;
if (millis() - start > _timeout) break;
delay(1);
}
Ethernet.socketClose(_sockindex);
_sockindex = MAX_SOCK_NUM;
return 0;
}
int EthernetClient::availableForWrite(void)
{
if (_sockindex >= MAX_SOCK_NUM) return 0;
return Ethernet.socketSendAvailable(_sockindex);
}
size_t EthernetClient::write(uint8_t b)
{
return write(&b, 1);
}
size_t EthernetClient::write(const uint8_t *buf, size_t size)
{
if (_sockindex >= MAX_SOCK_NUM) return 0;
if (Ethernet.socketSend(_sockindex, buf, size)) return size;
setWriteError();
return 0;
}
int EthernetClient::available()
{
if (_sockindex >= MAX_SOCK_NUM) return 0;
return Ethernet.socketRecvAvailable(_sockindex);
// TODO: do the WIZnet chips automatically retransmit TCP ACK
// packets if they are lost by the network? Someday this should
// be checked by a man-in-the-middle test which discards certain
// packets. If ACKs aren't resent, we would need to check for
// returning 0 here and after a timeout do another Sock_RECV
// command to cause the WIZnet chip to resend the ACK packet.
}
int EthernetClient::read(uint8_t *buf, size_t size)
{
if (_sockindex >= MAX_SOCK_NUM) return 0;
return Ethernet.socketRecv(_sockindex, buf, size);
}
int EthernetClient::peek()
{
if (_sockindex >= MAX_SOCK_NUM) return -1;
if (!available()) return -1;
return Ethernet.socketPeek(_sockindex);
}
int EthernetClient::read()
{
uint8_t b;
if (Ethernet.socketRecv(_sockindex, &b, 1) > 0) return b;
return -1;
}
void EthernetClient::flush()
{
while (_sockindex < MAX_SOCK_NUM) {
uint8_t stat = Ethernet.socketStatus(_sockindex);
if (stat != SnSR::ESTABLISHED && stat != SnSR::CLOSE_WAIT) return;
if (Ethernet.socketSendAvailable(_sockindex) >= W5100.SSIZE) return;
}
}
void EthernetClient::stop()
{
if (_sockindex >= MAX_SOCK_NUM) return;
// attempt to close the connection gracefully (send a FIN to other side)
Ethernet.socketDisconnect(_sockindex);
unsigned long start = millis();
// wait up to a second for the connection to close
do {
if (Ethernet.socketStatus(_sockindex) == SnSR::CLOSED) {
_sockindex = MAX_SOCK_NUM;
return; // exit the loop
}
delay(1);
} while (millis() - start < _timeout);
// if it hasn't closed, close it forcefully
Ethernet.socketClose(_sockindex);
_sockindex = MAX_SOCK_NUM;
}
uint8_t EthernetClient::connected()
{
if (_sockindex >= MAX_SOCK_NUM) return 0;
uint8_t s = Ethernet.socketStatus(_sockindex);
return !(s == SnSR::LISTEN || s == SnSR::CLOSED || s == SnSR::FIN_WAIT ||
(s == SnSR::CLOSE_WAIT && !available()));
}
uint8_t EthernetClient::status()
{
if (_sockindex >= MAX_SOCK_NUM) return SnSR::CLOSED;
return Ethernet.socketStatus(_sockindex);
}
// the next function allows us to use the client returned by
// EthernetServer::available() as the condition in an if-statement.
bool EthernetClient::operator==(const EthernetClient& rhs)
{
if (_sockindex != rhs._sockindex) return false;
if (_sockindex >= MAX_SOCK_NUM) return false;
if (rhs._sockindex >= MAX_SOCK_NUM) return false;
return true;
}
// https://github.com/per1234/EthernetMod
// from: https://github.com/ntruchsess/Arduino-1/commit/937bce1a0bb2567f6d03b15df79525569377dabd
uint16_t EthernetClient::localPort()
{
if (_sockindex >= MAX_SOCK_NUM) return 0;
uint16_t port;
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
port = W5100.readSnPORT(_sockindex);
SPI.endTransaction();
return port;
}
// https://github.com/per1234/EthernetMod
// returns the remote IP address: https://forum.arduino.cc/index.php?topic=82416.0
IPAddress EthernetClient::remoteIP()
{
if (_sockindex >= MAX_SOCK_NUM) return IPAddress((uint32_t)0);
uint8_t remoteIParray[4];
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.readSnDIPR(_sockindex, remoteIParray);
SPI.endTransaction();
return IPAddress(remoteIParray);
}
// https://github.com/per1234/EthernetMod
// from: https://github.com/ntruchsess/Arduino-1/commit/ca37de4ba4ecbdb941f14ac1fe7dd40f3008af75
uint16_t EthernetClient::remotePort()
{
if (_sockindex >= MAX_SOCK_NUM) return 0;
uint16_t port;
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
port = W5100.readSnDPORT(_sockindex);
SPI.endTransaction();
return port;
}