Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples/dtls-sock: make use of helper functions #20125

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/dtls-sock/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ USEMODULE += netdev_default
USEMODULE += auto_init_gnrc_netif
# Specify the mandatory networking modules for IPv6 and UDP
USEMODULE += gnrc_ipv6_default
USEMODULE += netutils

# Specify DTLS implementation
USEPKG += tinydtls
Expand Down
91 changes: 16 additions & 75 deletions examples/dtls-sock/dtls-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "net/ipv6/addr.h"
#include "net/credman.h"
#include "net/sock/util.h"
#include "net/utils.h"

#include "tinydtls_keys.h"

Expand Down Expand Up @@ -88,27 +89,6 @@ static const credman_credential_t credential1 = {
}
},
};

static credman_tag_t _client_psk_cb(sock_dtls_t *sock, sock_udp_ep_t *ep, credman_tag_t tags[],
unsigned tags_len, const char *hint, size_t hint_len)
{
(void) sock;
(void) tags;
(void) tags_len;

/* this callback is here just to show the functionality, it only prints the received hint */
char addrstr[IPV6_ADDR_MAX_STR_LEN];
uint16_t port;

sock_udp_ep_fmt(ep, addrstr, &port);
printf("From [%s]:%d\n", addrstr, port);

if (hint && hint_len) {
printf("Client got hint: %.*s\n", (unsigned)hint_len, hint);
}

return CREDMAN_TAG_EMPTY;
}
#endif

static int client_send(char *addr_str, char *data, size_t datalen)
Expand All @@ -122,33 +102,17 @@ static int client_send(char *addr_str, char *data, size_t datalen)
local.port = 12345;
remote.port = DTLS_DEFAULT_PORT;
uint8_t buf[DTLS_HANDSHAKE_BUFSIZE];
credman_tag_t tag = SOCK_DTLS_CLIENT_TAG_0;

/* get interface */
char* iface = ipv6_addr_split_iface(addr_str);
if (iface) {
int pid = atoi(iface);
if (gnrc_netif_get_by_pid(pid) == NULL) {
puts("Invalid network interface");
return -1;
}
remote.netif = pid;
} else if (gnrc_netif_numof() == 1) {
/* assign the single interface found in gnrc_netif_numof() */
remote.netif = gnrc_netif_iter(NULL)->pid;
} else {
/* no interface is given, or given interface is invalid */
/* FIXME This probably is not valid with multiple interfaces */
remote.netif = SOCK_ADDR_ANY_NETIF;
netif_t *netif;
res = netutils_get_ipv6((void *)&remote.addr, &netif, addr_str);
if (res) {
printf("Error parsing remote address\n");
return res;
}

if (!ipv6_addr_from_str((ipv6_addr_t *)remote.addr.ipv6, addr_str)) {
puts("Error parsing destination address");
return -1;
}

if (sock_udp_create(&udp_sock, &local, NULL, 0) < 0) {
puts("Error creating UDP sock");
return -1;
if (netif) {
remote.netif = netif_get_id(netif);
}

res = credman_add(&credential0);
Expand All @@ -159,13 +123,6 @@ static int client_send(char *addr_str, char *data, size_t datalen)
return -1;
}

if (sock_dtls_create(&dtls_sock, &udp_sock, SOCK_DTLS_CLIENT_TAG_0,
SOCK_DTLS_1_2, SOCK_DTLS_CLIENT) < 0) {
puts("Error creating DTLS sock");
sock_udp_close(&udp_sock);
return -1;
}

#if IS_ACTIVE(CONFIG_DTLS_PSK)
/* register a second PSK credential */
res = credman_add(&credential1);
Expand All @@ -175,32 +132,17 @@ static int client_send(char *addr_str, char *data, size_t datalen)
sock_udp_close(&udp_sock);
return -1;
}

/* make the new credential available to the sock */
if (sock_dtls_add_credential(&dtls_sock, SOCK_DTLS_CLIENT_TAG_1) < 0) {
printf("Error cannot add credential to the sock: %" PRIdSIZE "\n", res);
sock_udp_close(&udp_sock);
return -1;
}

/* register a callback for PSK credential selection */
sock_dtls_set_client_psk_cb(&dtls_sock, _client_psk_cb);
tag = SOCK_DTLS_CLIENT_TAG_1;
#endif

res = sock_dtls_session_init(&dtls_sock, &remote, &session);
if (res <= 0) {
res = sock_dtls_establish_session(&udp_sock, &dtls_sock, &session, tag,
&local, &remote, buf, sizeof(buf));
if (res) {
sock_udp_close(&udp_sock);
printf("Error establishing connection: %d\n", (int)res);
return res;
}

res = sock_dtls_recv(&dtls_sock, &session, buf, sizeof(buf),
SOCK_NO_TIMEOUT);
if (res != -SOCK_DTLS_HANDSHAKE) {
printf("Error creating session: %" PRIdSIZE "\n", res);
sock_dtls_close(&dtls_sock);
sock_udp_close(&udp_sock);
return -1;
}
printf("Connection to server successful\n");

if (sock_dtls_send(&dtls_sock, &session, data, datalen, 0) < 0) {
Expand All @@ -209,11 +151,10 @@ static int client_send(char *addr_str, char *data, size_t datalen)
else {
printf("Sent DTLS message\n");

uint8_t rcv[512];
if ((res = sock_dtls_recv(&dtls_sock, &session, rcv, sizeof(rcv),
if ((res = sock_dtls_recv(&dtls_sock, &session, buf, sizeof(buf),
SOCK_NO_TIMEOUT)) >= 0) {
printf("Received %" PRIdSIZE " bytes: \"%.*s\"\n", res, (int)res,
(char *)rcv);
(char *)buf);
}
}

Expand Down
Loading