Skip to content

Commit 901dcbd

Browse files
committed
Fix C027 endianness issue
1 parent 88293b5 commit 901dcbd

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

net/C027Interface/C027Interface.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@
1919
#include "rtos.h"
2020

2121

22+
// Portable big -> little endian
23+
static inline MDMParser::IP ntohl(MDMParser::IP ip) {
24+
ip = ((0xff & (ip >> 24)) << 0)
25+
| ((0xff & (ip >> 16)) << 8)
26+
| ((0xff & (ip >> 8)) << 16)
27+
| ((0xff & (ip >> 0)) << 24);
28+
return ip;
29+
}
30+
31+
static inline MDMParser::IP htonl(MDMParser::IP ip) {
32+
return ntohl(ip);
33+
}
34+
35+
2236
// C027Interface implementation
2337
C027Interface::C027Interface(const char *simpin, bool debug)
2438
: _debug(debug)
@@ -54,7 +68,7 @@ int C027Interface::connect(const char *apn, const char *username, const char *pa
5468

5569
if (mdmOk) {
5670
// join the internet connection
57-
MDMParser::IP ip = _mdm->join(apn, username, password);
71+
MDMParser::IP ip = htonl(_mdm->join(apn, username, password));
5872
_ip_address.set_ip_bytes(&ip, NSAPI_IPv4);
5973
mdmOk = (ip != NOIP);
6074
}
@@ -228,7 +242,7 @@ int C027Interface::socket_sendto(void *handle, const SocketAddress &addr, const
228242

229243
socket->mutex.lock();
230244
int sent = _mdm->socketSendTo(socket->socket,
231-
*(MDMParser::IP *)addr.get_ip_bytes(), addr.get_port(),
245+
ntohl(*(MDMParser::IP *)addr.get_ip_bytes()), addr.get_port(),
232246
(const char *)data, size);
233247

234248
if (socket->callback) {
@@ -268,6 +282,7 @@ int C027Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data
268282
}
269283

270284
if (addr) {
285+
ip = htonl(ip);
271286
addr->set_ip_bytes(&ip, NSAPI_IPv4);
272287
addr->set_port(port);
273288
}

0 commit comments

Comments
 (0)