Skip to content

Commit

Permalink
samples: cellular: modem_shell: connect UDP sockets
Browse files Browse the repository at this point in the history
This commit updates the sample to also connect UDP
sockets. UDP sockets are connectionless, however
the modem library supports invoking `connect` on a UDP
socket to set its peer address.
This simplifies the use of send which will internally
use the peer address.
This also fixes a problem when setting `RAI_NO_DATA`
on a UDP socket, since `RAI_NO_DATA` requires the socket
to be connected.

Signed-off-by: Mirko Covizzi <[email protected]>
  • Loading branch information
MirkoCovizzi authored and nordicjm committed Feb 1, 2024
1 parent baba310 commit b417d0e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ Cellular samples
* The MQTT and CoAP overlays to enable the Kconfig option :kconfig:option:`CONFIG_NRF_CLOUD_SEND_SERVICE_INFO_UI`.
The sample no longer sends a device shadow update for MQTT and CoAP builds; this is now handled by the :ref:`lib_nrf_cloud` library.
* To use the new :c:struct:`nrf_cloud_location_config` structure when calling the :c:func:`nrf_cloud_location_request` function.
* The ``connect`` subcommand to use the :c:func:`connect` function on non-secure datagram sockets.
This sets the peer address for the non-secure datagram socket.
This fixes a bug where using the ``connect`` subcommand and then using the ``rai_no_data`` option with the ``rai`` subcommand on a non-secure datagram socket would lead to an error.
The ``rai_no_data`` option requires the socket to be connected and have a peer address set.
This bug is caused by the non-secure datagram socket not being connected when using the ``connect`` subcommand.
* The ``send`` subcommand to use the :c:func:`send` function for non-secure datagram sockets that are connected and have a peer address set.

* :ref:`nrf_cloud_multi_service` sample:

Expand Down
21 changes: 5 additions & 16 deletions samples/cellular/modem_shell/src/sock/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,10 @@ int sock_open_and_connect(
}
}

if (type == SOCK_STREAM || (type == SOCK_DGRAM && secure)) {
/* Connect TCP and DTLS socket */
if (type == SOCK_STREAM || type == SOCK_DGRAM) {
/* Connect TCP or UDP socket.
* UDP socket supports connect for setting peer address.
*/
err = connect(
fd,
socket_info->addrinfo->ai_addr,
Expand Down Expand Up @@ -689,7 +691,6 @@ static int sock_send(
bool data_hex_format)
{
int bytes;
int dest_addr_len = 0;
int set, res;
char packet_number_prefix_str[5];

Expand Down Expand Up @@ -722,19 +723,7 @@ static int sock_send(
}
}

if (socket_info->type == SOCK_DGRAM && !socket_info->secure) {
/* UDP */
if (socket_info->family == AF_INET) {
dest_addr_len = sizeof(struct sockaddr_in);
} else if (socket_info->family == AF_INET6) {
dest_addr_len = sizeof(struct sockaddr_in6);
}
bytes = sendto(socket_info->fd, data, length, 0,
socket_info->addrinfo->ai_addr, dest_addr_len);
} else {
/* TCP, DTLS and raw socket */
bytes = send(socket_info->fd, data, length, 0);
}
bytes = send(socket_info->fd, data, length, 0);
if (bytes < 0) {
/* Ideally we'd like to log the failure here but non-blocking
* socket causes huge number of failures due to incorrectly
Expand Down

0 comments on commit b417d0e

Please sign in to comment.