From b44ba531a15a1340d06e8bc62660dca62e45699f Mon Sep 17 00:00:00 2001 From: Mirela Chirica Date: Thu, 5 Sep 2019 14:08:35 +0300 Subject: [PATCH] Cellular: Check IP version of send to address --- .../framework/AT/AT_CellularStack.cpp | 26 ++++++++++++++++--- .../cellular/framework/AT/AT_CellularStack.h | 8 ++++++ .../BG96/QUECTEL_BG96_CellularStack.cpp | 10 +++++-- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/features/cellular/framework/AT/AT_CellularStack.cpp b/features/cellular/framework/AT/AT_CellularStack.cpp index dc0f5732c2..621431eabe 100644 --- a/features/cellular/framework/AT/AT_CellularStack.cpp +++ b/features/cellular/framework/AT/AT_CellularStack.cpp @@ -23,7 +23,7 @@ using namespace mbed_cellular_util; using namespace mbed; -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) +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) { memset(_ip, 0, PDP_IPV6_SIZE); } @@ -272,6 +272,13 @@ nsapi_size_or_error_t AT_CellularStack::socket_sendto(nsapi_socket_t handle, con nsapi_size_or_error_t ret_val = NSAPI_ERROR_OK; if (socket->id == -1) { + + /* Check that stack type supports sendto address type*/ + if (!is_addr_stack_compatible(addr)) { + return NSAPI_ERROR_PARAMETER; + } + + _ip_ver_sendto = addr.get_ip_version(); _at.lock(); ret_val = create_socket_impl(socket); @@ -283,9 +290,9 @@ nsapi_size_or_error_t AT_CellularStack::socket_sendto(nsapi_socket_t handle, con } } - /* Check parameters */ - if (addr.get_ip_version() == NSAPI_UNSPEC) { - return NSAPI_ERROR_DEVICE_ERROR; + /* Check parameters - sendto address is valid and stack type supports sending to that address type*/ + if (!is_addr_stack_compatible(addr)) { + return NSAPI_ERROR_PARAMETER; } _at.lock(); @@ -393,3 +400,14 @@ AT_CellularStack::CellularSocket *AT_CellularStack::find_socket(int sock_id) } return sock; } + +bool AT_CellularStack::is_addr_stack_compatible(const SocketAddress &addr) +{ + if ((addr.get_ip_version() == NSAPI_UNSPEC) || + (addr.get_ip_version() == NSAPI_IPv4 && _stack_type == IPV6_STACK) || + (addr.get_ip_version() == NSAPI_IPv6 && _stack_type == IPV4_STACK)) { + return false; + } + return true; +} + diff --git a/features/cellular/framework/AT/AT_CellularStack.h b/features/cellular/framework/AT/AT_CellularStack.h index a61515672f..9bbedd4fe6 100644 --- a/features/cellular/framework/AT/AT_CellularStack.h +++ b/features/cellular/framework/AT/AT_CellularStack.h @@ -191,6 +191,11 @@ protected: */ int find_socket_index(nsapi_socket_t handle); + /** + * Checks if send to address is valid and if current stack type supports sending to that address type + */ + bool is_addr_stack_compatible(const SocketAddress &addr); + // socket container CellularSocket **_socket; @@ -206,6 +211,9 @@ protected: // stack type - initialised as PDP type and set accordingly after CGPADDR checked nsapi_ip_stack_t _stack_type; + // IP version of send to address + nsapi_version_t _ip_ver_sendto; + private: int get_socket_index_by_port(uint16_t port); diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.cpp b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.cpp index 2cfef67cd3..1c26340204 100644 --- a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.cpp +++ b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.cpp @@ -212,7 +212,7 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc if (socket->proto == NSAPI_UDP && !socket->connected) { _at.at_cmd_discard("+QIOPEN", "=", "%d%d%s%s%d%d%d", _cid, request_connect_id, "UDP SERVICE", - (_stack_type == IPV4_STACK) ? "127.0.0.1" : "0:0:0:0:0:0:0:1", + (_ip_ver_sendto == NSAPI_IPv4) ? "127.0.0.1" : "0:0:0:0:0:0:0:1", remote_port, socket->localAddress.get_port(), 0); handle_open_socket_response(modem_connect_id, err); @@ -225,7 +225,7 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc socket_close_impl(modem_connect_id); _at.at_cmd_discard("+QIOPEN", "=", "%d%d%s%s%d%d%d", _cid, request_connect_id, "UDP SERVICE", - (_stack_type == IPV4_STACK) ? "127.0.0.1" : "0:0:0:0:0:0:0:1", + (_ip_ver_sendto == NSAPI_IPv4) ? "127.0.0.1" : "0:0:0:0:0:0:0:1", remote_port, socket->localAddress.get_port(), 0); handle_open_socket_response(modem_connect_id, err); @@ -273,6 +273,12 @@ nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_sendto_impl(CellularSoc return NSAPI_ERROR_PARAMETER; } + if (_ip_ver_sendto != address.get_ip_version()) { + _ip_ver_sendto = address.get_ip_version(); + socket_close_impl(socket->id); + create_socket_impl(socket); + } + int sent_len = 0; int sent_len_before = 0; int sent_len_after = 0;