Cellular: Check IP version of send to address

pull/11424/head
Mirela Chirica 2019-09-05 14:08:35 +03:00
parent ea1b2b8045
commit b44ba531a1
3 changed files with 38 additions and 6 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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;