Skip to content

Commit

Permalink
pkg/tinydtls: add IPv4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikVE committed Mar 7, 2022
1 parent 3ccedbe commit f979699
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 12 deletions.
69 changes: 57 additions & 12 deletions pkg/tinydtls/contrib/sock_dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ static int _write(struct dtls_context_t *ctx, session_t *session, uint8_t *buf,
sock_udp_ep_t remote;

_session_to_ep(session, &remote);
remote.family = AF_INET6;

ssize_t res = sock_udp_send(sock->udp_sock, buf, len, &remote);

Expand Down Expand Up @@ -529,15 +528,20 @@ int sock_dtls_session_init(sock_dtls_t *sock, const sock_udp_ep_t *ep,
if (!sock->udp_sock || (sock_udp_get_local(sock->udp_sock, &local) < 0)) {
return -EADDRNOTAVAIL;
}

if (ep->port == 0) {
return -EINVAL;
}

switch (ep->family) {
#ifdef SOCK_HAS_IPV4
case AF_INET:
#if IS_ACTIVE(SOCK_HAS_IPV6)
break;
#endif
#ifdef SOCK_HAS_IPV6
case AF_INET6:
#endif
break;
#endif
default:
return -EINVAL;
}
Expand Down Expand Up @@ -812,24 +816,65 @@ void sock_dtls_init(void)
static void _ep_to_session(const sock_udp_ep_t *ep, session_t *session)
{
session->port = ep->port;
session->size = sizeof(ipv6_addr_t) + /* addr */
session->addr_family = ep->family;

#if defined(SOCK_HAS_IPV4) && defined(SOCK_HAS_IPV6)
assert(sizeof(session->addr) == sizeof(ipv6_addr_t));
#elif defined(SOCK_HAS_IPV4)
assert(sizeof(session->addr) == sizeof(ipv4_addr_t));
#else
assert(sizeof(session->addr) == sizeof(ipv6_addr_t));
#endif

session->size = sizeof(session->addr) + /* addr */
sizeof(unsigned short); /* port */
if (ipv6_addr_is_link_local((ipv6_addr_t *)ep->addr.ipv6)) {
/* set ifindex for link-local addresses */
session->ifindex = ep->netif;
}
else {

switch (ep->family) {
#ifdef SOCK_HAS_IPV4
case AF_INET:
session->ifindex = SOCK_ADDR_ANY_NETIF;
memcpy(&session->addr, &ep->addr.ipv4, sizeof(ipv4_addr_t));
break;
#endif
#ifdef SOCK_HAS_IPV6
case AF_INET6:
if (ipv6_addr_is_link_local((ipv6_addr_t *)ep->addr.ipv6)) {
/* set ifindex for link-local addresses */
session->ifindex = ep->netif;
}
else {
session->ifindex = SOCK_ADDR_ANY_NETIF;
}
memcpy(&session->addr, &ep->addr.ipv6, sizeof(ipv6_addr_t));
break;
#endif
default:
assert(0);
return;
}
memcpy(&session->addr, &ep->addr.ipv6, sizeof(ipv6_addr_t));
}

static void _session_to_ep(const session_t *session, sock_udp_ep_t *ep)
{
ep->port = session->port;
ep->netif = session->ifindex;
ep->family = AF_INET6;
memcpy(&ep->addr.ipv6, &session->addr, sizeof(ipv6_addr_t));
ep->family = session->addr_family;

switch (session->addr_family) {
#ifdef SOCK_HAS_IPV4
case AF_INET:
memcpy(&ep->addr.ipv4, &session->addr, sizeof(ipv4_addr_t));
break;
#endif
#ifdef SOCK_HAS_IPV6
case AF_INET6:
memcpy(&ep->addr.ipv6, &session->addr, sizeof(ipv6_addr_t));
break;
#endif
default:
/* addr_family is actually ok to be 0 when coming from _copy_buffer */
return;
}
}

static inline uint32_t _update_timeout(uint32_t start, uint32_t timeout)
Expand Down
99 changes: 99 additions & 0 deletions pkg/tinydtls/patches/0003-RIOT-OS-Add-support-for-IPv4.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
From e8a60987d0b0b2c0d5d7f1e44302049904789ec7 Mon Sep 17 00:00:00 2001
From: HendrikVE <hendrik1995@msn.com>
Date: Sun, 6 Mar 2022 00:17:12 +0100
Subject: [PATCH] RIOT OS: Add support for IPv4

---
session.c | 38 +++++++++++++++++++++++++++++++++-----
session.h | 16 ++++++++++++++--
2 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/session.c b/session.c
index 8e9f544..dea8990 100644
--- a/session.c
+++ b/session.c
@@ -32,11 +32,23 @@
&& uip_ipaddr_cmp(&((A)->addr),&((B)->addr)) \
&& (A)->ifindex == (B)->ifindex)
#elif defined(WITH_RIOT)
-#define _dtls_address_equals_impl(A,B) \
- ((A)->size == (B)->size \
- && (A)->port == (B)->port \
- && ipv6_addr_equal(&((A)->addr),&((B)->addr)) \
- && (A)->ifindex == (B)->ifindex)
+ #include "net/af.h"
+ #ifdef SOCK_HAS_IPV4
+ #define _dtls_ipv4_address_equals_impl(A,B) \
+ ((A)->size == (B)->size \
+ && (A)->port == (B)->port \
+ && (A)->ifindex == (B)->ifindex) \
+ && (A)->addr_family == (B)->addr_family \
+ && ipv4_addr_equal(&((A)->addr.ipv4),&((B)->addr.ipv4))
+ #endif
+ #ifdef SOCK_HAS_IPV6
+ #define _dtls_ipv6_address_equals_impl(A,B) \
+ ((A)->size == (B)->size \
+ && (A)->port == (B)->port \
+ && (A)->ifindex == (B)->ifindex) \
+ && (A)->addr_family == (B)->addr_family \
+ && ipv6_addr_equal(&((A)->addr.ipv6),&((B)->addr.ipv6))
+ #endif
#else /* WITH_CONTIKI */

static inline int
@@ -120,5 +132,21 @@ dtls_session_addr(session_t *sess, socklen_t *addrlen) {
int
dtls_session_equals(const session_t *a, const session_t *b) {
assert(a); assert(b);
+#ifdef RIOT_VERSION
+ switch (a->addr_family) {
+ #ifdef SOCK_HAS_IPV4
+ case AF_INET:
+ return _dtls_ipv4_address_equals_impl(a, b);
+ #endif
+ #ifdef SOCK_HAS_IPV6
+ case AF_INET6:
+ return _dtls_ipv6_address_equals_impl(a, b);
+ #endif
+ default:
+ assert(0);
+ return false;
+ }
+#else
return _dtls_address_equals_impl(a, b);
+#endif /* RIOT_VERSION */
}
diff --git a/session.h b/session.h
index 8ba5fa2..b90b269 100644
--- a/session.h
+++ b/session.h
@@ -32,12 +32,24 @@ typedef struct {
} session_t;
/* TODO: Add support for RIOT over sockets */
#elif defined(WITH_RIOT)
+#include "net/ipv4/addr.h"
#include "net/ipv6/addr.h"
typedef struct {
unsigned char size;
- ipv6_addr_t addr;
- unsigned short port;
int ifindex;
+ int addr_family;
+ union {
+ #ifdef SOCK_HAS_IPV4
+ ipv4_addr_t ipv4;
+ #endif
+ #ifdef SOCK_HAS_IPV6
+ ipv6_addr_t ipv6;
+ #endif
+ } addr;
+ /* Note: Member "port" MUST follow directly after the member "addr"! dtls_hmac_update in
+ * dtls.c gets "session->size" as length input which is set to e.g.
+ * "sizeof(ipv6_addr_t) + sizeof(unsigned short)" = (addr + port) in sock_dtls.c */
+ unsigned short port;
} session_t;
#else /* ! WITH_CONTIKI && ! WITH_RIOT */

--
2.25.1

0 comments on commit f979699

Please sign in to comment.