Skip to content

Commit 86ed47c

Browse files
authored
Merge pull request #16 from ARMmbed/fix-recvfrom-address
Fix invalid SocketAddress returned from recvfrom
2 parents dc37b65 + cd1bf66 commit 86ed47c

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,29 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
124124
*/
125125
virtual int scan(WiFiAccessPoint *res, unsigned count);
126126

127+
/** Translates a hostname to an IP address with specific version
128+
*
129+
* The hostname may be either a domain name or an IP address. If the
130+
* hostname is an IP address, no network transactions will be performed.
131+
*
132+
* If no stack-specific DNS resolution is provided, the hostname
133+
* will be resolve using a UDP socket on the stack.
134+
*
135+
* @param address Destination for the host SocketAddress
136+
* @param host Hostname to resolve
137+
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
138+
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
139+
* @return 0 on success, negative error code on failure
140+
*/
141+
using NetworkInterface::gethostbyname;
142+
143+
/** Add a domain name server to list of servers to query
144+
*
145+
* @param addr Destination for the host address
146+
* @return 0 on success, negative error code on failure
147+
*/
148+
using NetworkInterface::add_dns_server;
149+
127150
protected:
128151
/** Open a socket
129152
* @param handle Handle in which to store new socket

0 commit comments

Comments
 (0)