Skip to content

Commit b44ba53

Browse files
author
Mirela Chirica
committed
Cellular: Check IP version of send to address
1 parent ea1b2b8 commit b44ba53

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

features/cellular/framework/AT/AT_CellularStack.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
using namespace mbed_cellular_util;
2424
using namespace mbed;
2525

26-
AT_CellularStack::AT_CellularStack(ATHandler &at, int cid, nsapi_ip_stack_t stack_type) : AT_CellularBase(at), _socket(NULL), _socket_count(0), _cid(cid), _stack_type(stack_type)
26+
AT_CellularStack::AT_CellularStack(ATHandler &at, int cid, nsapi_ip_stack_t stack_type) : AT_CellularBase(at), _socket(NULL), _socket_count(0), _cid(cid), _stack_type(stack_type), _ip_ver_sendto(NSAPI_UNSPEC)
2727
{
2828
memset(_ip, 0, PDP_IPV6_SIZE);
2929
}
@@ -272,6 +272,13 @@ nsapi_size_or_error_t AT_CellularStack::socket_sendto(nsapi_socket_t handle, con
272272
nsapi_size_or_error_t ret_val = NSAPI_ERROR_OK;
273273

274274
if (socket->id == -1) {
275+
276+
/* Check that stack type supports sendto address type*/
277+
if (!is_addr_stack_compatible(addr)) {
278+
return NSAPI_ERROR_PARAMETER;
279+
}
280+
281+
_ip_ver_sendto = addr.get_ip_version();
275282
_at.lock();
276283

277284
ret_val = create_socket_impl(socket);
@@ -283,9 +290,9 @@ nsapi_size_or_error_t AT_CellularStack::socket_sendto(nsapi_socket_t handle, con
283290
}
284291
}
285292

286-
/* Check parameters */
287-
if (addr.get_ip_version() == NSAPI_UNSPEC) {
288-
return NSAPI_ERROR_DEVICE_ERROR;
293+
/* Check parameters - sendto address is valid and stack type supports sending to that address type*/
294+
if (!is_addr_stack_compatible(addr)) {
295+
return NSAPI_ERROR_PARAMETER;
289296
}
290297

291298
_at.lock();
@@ -393,3 +400,14 @@ AT_CellularStack::CellularSocket *AT_CellularStack::find_socket(int sock_id)
393400
}
394401
return sock;
395402
}
403+
404+
bool AT_CellularStack::is_addr_stack_compatible(const SocketAddress &addr)
405+
{
406+
if ((addr.get_ip_version() == NSAPI_UNSPEC) ||
407+
(addr.get_ip_version() == NSAPI_IPv4 && _stack_type == IPV6_STACK) ||
408+
(addr.get_ip_version() == NSAPI_IPv6 && _stack_type == IPV4_STACK)) {
409+
return false;
410+
}
411+
return true;
412+
}
413+

features/cellular/framework/AT/AT_CellularStack.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
191191
*/
192192
int find_socket_index(nsapi_socket_t handle);
193193

194+
/**
195+
* Checks if send to address is valid and if current stack type supports sending to that address type
196+
*/
197+
bool is_addr_stack_compatible(const SocketAddress &addr);
198+
194199
// socket container
195200
CellularSocket **_socket;
196201

@@ -206,6 +211,9 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
206211
// stack type - initialised as PDP type and set accordingly after CGPADDR checked
207212
nsapi_ip_stack_t _stack_type;
208213

214+
// IP version of send to address
215+
nsapi_version_t _ip_ver_sendto;
216+
209217
private:
210218

211219
int get_socket_index_by_port(uint16_t port);

features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
212212

213213
if (socket->proto == NSAPI_UDP && !socket->connected) {
214214
_at.at_cmd_discard("+QIOPEN", "=", "%d%d%s%s%d%d%d", _cid, request_connect_id, "UDP SERVICE",
215-
(_stack_type == IPV4_STACK) ? "127.0.0.1" : "0:0:0:0:0:0:0:1",
215+
(_ip_ver_sendto == NSAPI_IPv4) ? "127.0.0.1" : "0:0:0:0:0:0:0:1",
216216
remote_port, socket->localAddress.get_port(), 0);
217217

218218
handle_open_socket_response(modem_connect_id, err);
@@ -225,7 +225,7 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
225225
socket_close_impl(modem_connect_id);
226226

227227
_at.at_cmd_discard("+QIOPEN", "=", "%d%d%s%s%d%d%d", _cid, request_connect_id, "UDP SERVICE",
228-
(_stack_type == IPV4_STACK) ? "127.0.0.1" : "0:0:0:0:0:0:0:1",
228+
(_ip_ver_sendto == NSAPI_IPv4) ? "127.0.0.1" : "0:0:0:0:0:0:0:1",
229229
remote_port, socket->localAddress.get_port(), 0);
230230

231231
handle_open_socket_response(modem_connect_id, err);
@@ -273,6 +273,12 @@ nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_sendto_impl(CellularSoc
273273
return NSAPI_ERROR_PARAMETER;
274274
}
275275

276+
if (_ip_ver_sendto != address.get_ip_version()) {
277+
_ip_ver_sendto = address.get_ip_version();
278+
socket_close_impl(socket->id);
279+
create_socket_impl(socket);
280+
}
281+
276282
int sent_len = 0;
277283
int sent_len_before = 0;
278284
int sent_len_after = 0;

0 commit comments

Comments
 (0)