Skip to content

Commit

Permalink
shell/cord_ep: Add user guidance & prevent accidental crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Teufelchen1 committed Sep 22, 2022
1 parent c79a7f7 commit b83dce0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
1 change: 1 addition & 0 deletions sys/shell/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ endif
ifneq (,$(filter shell_cmd_cord_ep,$(USEMODULE)))
USEMODULE += cord_ep
USEMODULE += sock_util
USEMODULE += uri_parser
endif
ifneq (,$(filter shell_cmd_cryptoauthlib,$(USEPKG)))
USEMODULE += cryptoauthlib
Expand Down
57 changes: 45 additions & 12 deletions sys/shell/cmds/cord_ep.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
#include "net/gnrc/netif.h"
#include "net/nanocoap.h"
#include "net/sock/util.h"
#include "uri_parser.h"
#include "shell.h"

static int make_sock_ep(sock_udp_ep_t *ep, const char *addr)
#define COAPS_PORT 5684

static int _make_sock_ep(sock_udp_ep_t *ep, const char *addr)
{
ep->port = 0;
if (sock_udp_name2ep(ep, addr) < 0) {
Expand All @@ -39,7 +42,7 @@ static int make_sock_ep(sock_udp_ep_t *ep, const char *addr)
/* assign the single interface found in gnrc_netif_numof() */
ep->netif = (uint16_t)gnrc_netif_iter(NULL)->pid;
}
ep->family = AF_INET6;
ep->family = AF_INET6;
if (ep->port == 0) {
ep->port = COAP_PORT;
}
Expand All @@ -51,23 +54,53 @@ static int _cord_ep_handler(int argc, char **argv)
int res;

if ((argc > 1) && (strcmp(argv[1], "register") == 0)) {
char *regif = NULL;
if (argc < 3) {
printf("usage: %s register <server address> [registration interface]\n",
argv[0]);
printf("usage: %s register <registration uri>\n", argv[0]);
puts(
"If the registration URI's path is empty, the registration resource is auto-discovered");
printf("example: %s register coap://[2001:db8::1]:99\n", argv[0]);
return 1;
}

uri_parser_result_t uri_result;
if (uri_parser_process_string(&uri_result, argv[2]) != 0) {
puts("error: unable to parse uri");
return 1;
}
sock_udp_ep_t remote;
if (make_sock_ep(&remote, argv[2]) < 0) {
printf("error: unable to parse address\n");
remote.family = AF_INET6;
remote.netif = SOCK_ADDR_ANY_NETIF;
remote.port = uri_result.port;

if ((uri_result.scheme_len == 4) && (strncmp(uri_result.scheme, "coap", 4) == 0)) {
if (uri_result.port == 0) {
remote.port = COAP_PORT;
}
}
else if ((uri_result.scheme_len == 5) && (strncmp(uri_result.scheme, "coaps", 5) == 0)) {
if (uri_result.port == 0) {
remote.port = COAPS_PORT;
}
}
else {
puts("error: Only coap schemes are supported");
return 1;
}
if (argc > 3) {
regif = argv[3];

if (uri_result.ipv6addr == NULL) {
puts("error: Only ipv6 addresses are supported");
return 1;
}

if (!ipv6_addr_from_buf((ipv6_addr_t *)&remote.addr.ipv6, uri_result.ipv6addr,
uri_result.ipv6addr_len)) {
puts("error: IPv6 address malformed");
return 1;
}

puts("Registering with RD now, this may take a short while...");
if (cord_ep_register(&remote, regif) != CORD_EP_OK) {
puts("error: registration failed");
if (cord_ep_register(&remote, uri_result.path) != CORD_EP_OK) {
puts("failure: registration failed");
}
else {
puts("registration successful\n");
Expand All @@ -81,7 +114,7 @@ static int _cord_ep_handler(int argc, char **argv)
}
char regif[CONFIG_NANOCOAP_URI_MAX];
sock_udp_ep_t remote;
if (make_sock_ep(&remote, argv[2]) < 0) {
if (_make_sock_ep(&remote, argv[2]) < 0) {
printf("error: unable to parse address\n");
return 1;
}
Expand Down

0 comments on commit b83dce0

Please sign in to comment.