Skip to content
Snippets Groups Projects
Commit 98e45c3d authored by Martine Lenders's avatar Martine Lenders
Browse files

gnrc_sock: set remote network interface implicitly

When there is only one interface we are simplifying a lot for the users
if the interface is set implicitly.
parent 20780aac
No related branches found
No related tags found
No related merge requests found
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include "mbox.h" #include "mbox.h"
#include "net/af.h" #include "net/af.h"
#include "net/gnrc.h" #include "net/gnrc.h"
...@@ -96,6 +97,25 @@ static inline bool gnrc_ep_addr_any(const sock_ip_ep_t *ep) ...@@ -96,6 +97,25 @@ static inline bool gnrc_ep_addr_any(const sock_ip_ep_t *ep)
return true; return true;
} }
/**
* @brief Initializes a sock end-point from a given and sets the network
* interface implicitly if there is only one potential interface.
* @internal
*/
static inline void gnrc_ep_set(sock_ip_ep_t *out, const sock_ip_ep_t *in,
size_t in_size)
{
memcpy(out, in, in_size);
#if GNRC_NETIF_NUMOF == 1
/* set interface implicitly */
gnrc_netif_t *netif = gnrc_netif_iter(NULL);
if (netif != NULL) {
out->netif = netif->pid;
}
#endif
}
/** /**
* @brief Create a sock internally * @brief Create a sock internally
* @internal * @internal
......
...@@ -51,7 +51,7 @@ int sock_ip_create(sock_ip_t *sock, const sock_ip_ep_t *local, ...@@ -51,7 +51,7 @@ int sock_ip_create(sock_ip_t *sock, const sock_ip_ep_t *local,
if (gnrc_ep_addr_any(remote)) { if (gnrc_ep_addr_any(remote)) {
return -EINVAL; return -EINVAL;
} }
memcpy(&sock->remote, remote, sizeof(sock_ip_ep_t)); gnrc_ep_set(&sock->remote, remote, sizeof(sock_ip_ep_t));
} }
gnrc_sock_create(&sock->reg, GNRC_NETTYPE_IPV6, gnrc_sock_create(&sock->reg, GNRC_NETTYPE_IPV6,
proto); proto);
...@@ -165,7 +165,7 @@ ssize_t sock_ip_send(sock_ip_t *sock, const void *data, size_t len, ...@@ -165,7 +165,7 @@ ssize_t sock_ip_send(sock_ip_t *sock, const void *data, size_t len,
memcpy(&rem, &sock->remote, sizeof(rem)); memcpy(&rem, &sock->remote, sizeof(rem));
} }
else { else {
memcpy(&rem, remote, sizeof(rem)); gnrc_ep_set(&rem, remote, sizeof(rem));
} }
if ((remote != NULL) && (remote->family == AF_UNSPEC) && if ((remote != NULL) && (remote->family == AF_UNSPEC) &&
(sock->remote.family != AF_UNSPEC)) { (sock->remote.family != AF_UNSPEC)) {
......
...@@ -123,7 +123,8 @@ int sock_udp_create(sock_udp_t *sock, const sock_udp_ep_t *local, ...@@ -123,7 +123,8 @@ int sock_udp_create(sock_udp_t *sock, const sock_udp_ep_t *local,
if (gnrc_ep_addr_any((const sock_ip_ep_t *)remote)) { if (gnrc_ep_addr_any((const sock_ip_ep_t *)remote)) {
return -EINVAL; return -EINVAL;
} }
memcpy(&sock->remote, remote, sizeof(sock_udp_ep_t)); gnrc_ep_set((sock_ip_ep_t *)&sock->remote,
(sock_ip_ep_t *)remote, sizeof(sock_udp_ep_t));
} }
if (local != NULL) { if (local != NULL) {
/* listen only with local given */ /* listen only with local given */
...@@ -216,6 +217,7 @@ ssize_t sock_udp_send(sock_udp_t *sock, const void *data, size_t len, ...@@ -216,6 +217,7 @@ ssize_t sock_udp_send(sock_udp_t *sock, const void *data, size_t len,
gnrc_pktsnip_t *payload, *pkt; gnrc_pktsnip_t *payload, *pkt;
uint16_t src_port = 0, dst_port; uint16_t src_port = 0, dst_port;
sock_ip_ep_t local; sock_ip_ep_t local;
sock_udp_ep_t remote_cpy;
sock_ip_ep_t *rem; sock_ip_ep_t *rem;
assert((sock != NULL) || (remote != NULL)); assert((sock != NULL) || (remote != NULL));
...@@ -277,7 +279,8 @@ ssize_t sock_udp_send(sock_udp_t *sock, const void *data, size_t len, ...@@ -277,7 +279,8 @@ ssize_t sock_udp_send(sock_udp_t *sock, const void *data, size_t len,
dst_port = sock->remote.port; dst_port = sock->remote.port;
} }
else { else {
rem = (sock_ip_ep_t *)remote; rem = (sock_ip_ep_t *)&remote_cpy;
gnrc_ep_set(rem, (sock_ip_ep_t *)remote, sizeof(sock_udp_ep_t));
dst_port = remote->port; dst_port = remote->port;
} }
/* check for matching address families in local and remote */ /* check for matching address families in local and remote */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment