Skip to content

Commit 86d8500

Browse files
committed
Fixed recvfrom to return connected IP address
In the current version of the firmware, the esp8266 handles UDP in an odd way. Rather than packet based transactions, the esp8266 handles UDP like a connection based protocol. To return the correct address, the esp8266 just saves the last connection established by UDP. Additionally this patch added the ability of a socket to change connection addresses. Note: This firmware is still unable to recieve connections from arbitrary hosts.
1 parent 2b043ce commit 86d8500

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

ESP8266Interface.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ struct esp8266_socket {
131131
int id;
132132
nsapi_protocol_t proto;
133133
bool connected;
134+
SocketAddress addr;
134135
};
135136

136137
int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto)
@@ -234,20 +235,35 @@ int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size)
234235
int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
235236
{
236237
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
238+
239+
if (socket->connected && socket->addr != addr) {
240+
_esp.setTimeout(ESP8266_MISC_TIMEOUT);
241+
if (!_esp.close(socket->id)) {
242+
return NSAPI_ERROR_DEVICE_ERROR;
243+
}
244+
socket->connected = false;
245+
}
246+
237247
if (!socket->connected) {
238248
int err = socket_connect(socket, addr);
239249
if (err < 0) {
240250
return err;
241251
}
252+
socket->addr = addr;
242253
}
243254

244255
return socket_send(socket, data, size);
245256
}
246257

247258
int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
248259
{
249-
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
250-
return socket_recv(socket, data, size);
260+
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
261+
int ret = socket_recv(socket, data, size);
262+
if (ret >= 0 && addr) {
263+
*addr = socket->addr;
264+
}
265+
266+
return ret;
251267
}
252268

253269
void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data)

ESP8266Interface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
270270
void (*callback)(void *);
271271
void *data;
272272
} _cbs[ESP8266_SOCKET_COUNT];
273+
SocketAddress _udp_remotes[ESP8266_SOCKET_COUNT];
273274
};
274275

275276
#endif

0 commit comments

Comments
 (0)