From 766b852849b5cfb9705e863abf4fc4751b5aedf5 Mon Sep 17 00:00:00 2001 From: Ernestas Kulik Date: Wed, 1 Apr 2026 15:03:32 +0300 Subject: [PATCH] Implement support for LLC sockets Useful in embedded contexts, where devices might only have a link-layer connection, but still need to exchange information. --- CMakeLists.txt | 27 +++++++++ cmake_coap_defines.h.in | 3 + configure.ac | 21 +++++++ examples/coap-client.c | 7 +++ examples/coap-server.c | 41 +++++++++---- include/coap3/coap_address.h | 40 +++++++++++- include/coap3/coap_io_internal.h | 1 + include/coap3/coap_uri.h | 10 +++ libcoap-3.map | 3 + libcoap-3.sym | 3 + man/coap-client.txt.in | 9 +++ man/coap-server.txt.in | 13 +++- man/coap_address.txt.in | 8 +-- src/coap_address.c | 98 +++++++++++++++++++++++++++++- src/coap_debug.c | 18 ++++++ src/coap_dgrm_posix.c | 59 +++++++++++++----- src/coap_strm_posix.c | 19 ++++++ src/coap_uri.c | 101 ++++++++++++++++++++++++++++++- tests/test_uri.c | 21 +++++++ 19 files changed, 463 insertions(+), 39 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a9709ee..776f4056 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -190,6 +190,12 @@ option( ENABLE_IPV6 "enable building with IPv6 support" ON) +if(LINUX) +option( + ENABLE_AF_LLC + "enable building with LLC socket support" + ON) +endif() option( ENABLE_AF_UNIX "enable building with Unix socket support" @@ -417,6 +423,24 @@ else() message(STATUS "compiling without IPv6 support") endif() +if(LINUX) +if(${ENABLE_AF_LLC}) + check_include_file(linux/llc.h HAVE_LINUX_LLC_H) + if(NOT HAVE_LINUX_LLC_H) + message(FATAL_ERROR "linux/llc.h is required for LLC socket support") + endif() + check_include_file(netinet/ether.h HAVE_NETINET_ETHER_H) + if(NOT HAVE_NETINET_ETHER_H) + message(FATAL_ERROR "netinet/ether.h is required for LLC socket support") + endif() + + set(COAP_AF_LLC_SUPPORT "1") + message(STATUS "compiling with LLC socket support") +else() + message(STATUS "compiling without LLC socket support") +endif() +endif() + if(${ENABLE_AF_UNIX}) set(COAP_AF_UNIX_SUPPORT "1") message(STATUS "compiling with Unix socket support") @@ -613,6 +637,9 @@ message(STATUS "ENABLE_DTLS:.....................${ENABLE_DTLS}") message(STATUS "ENABLE_TCP:......................${ENABLE_TCP}") message(STATUS "ENABLE_IPV4:.....................${ENABLE_IPV4}") message(STATUS "ENABLE_IPV6:.....................${ENABLE_IPV6}") +if(LINUX) +message(STATUS "ENABLE_AF_LLC:...................${ENABLE_AF_LLC}") +endif() message(STATUS "ENABLE_AF_UNIX:..................${ENABLE_AF_UNIX}") message(STATUS "ENABLE_WEBSOCKETS:...............${ENABLE_WS}") message(STATUS "ENABLE_Q_BLOCK:..................${ENABLE_Q_BLOCK}") diff --git a/cmake_coap_defines.h.in b/cmake_coap_defines.h.in index 6f5b0805..3d4c80ac 100644 --- a/cmake_coap_defines.h.in +++ b/cmake_coap_defines.h.in @@ -17,6 +17,9 @@ #ifndef COAP_DEFINES_H_ #define COAP_DEFINES_H_ +/* Define to 1 to build with LLC socket support. */ +#cmakedefine COAP_AF_LLC_SUPPORT @COAP_AF_LLC_SUPPORT@ + /* Define to 1 to build with Unix socket support. */ #cmakedefine COAP_AF_UNIX_SUPPORT @COAP_AF_UNIX_SUPPORT@ diff --git a/configure.ac b/configure.ac index 481b37d4..53b2cabe 100644 --- a/configure.ac +++ b/configure.ac @@ -994,6 +994,22 @@ AC_ARG_ENABLE([ipv6], AS_IF([test "x$build_ipv6" = "xyes"], [AC_DEFINE(COAP_IPV6_SUPPORT, [1], [Define to build support for IPv6 packets.])]) +# configure options +# __af_llc__ +AC_ARG_ENABLE([af_llc], + [AS_HELP_STRING([--enable-af-llc], + [Enable building with support for LLC sockets [default=yes]])], + [build_af_llc="$enableval"], + [build_af_llc="yes"]) + +AS_IF([test "x$build_af_llc" = "xyes"], + [AC_DEFINE(COAP_AF_LLC_SUPPORT, [1], [Define to build support for LLC sockets.])]) + +if test "x$with_epoll" = "xyes"; then + AC_CHECK_HEADER([netinet/ether.h]) + AC_CHECK_HEADER([linux/llc.h]) +fi + # configure options # __af_unix__ AC_ARG_ENABLE([af_unix], @@ -1455,6 +1471,11 @@ if test "x$build_ipv6" != "xno"; then else AC_MSG_RESULT([ build with IPv6 support : "no"]) fi +if test "x$build_af_llc" != "xno"; then + AC_MSG_RESULT([ build with LLC socket support : "yes"]) +else + AC_MSG_RESULT([ build with LLC socket support : "no"]) +fi if test "x$build_af_unix" != "xno"; then AC_MSG_RESULT([ build with Unix socket support : "yes"]) else diff --git a/examples/coap-client.c b/examples/coap-client.c index 88fbad6e..749c5e4d 100644 --- a/examples/coap-client.c +++ b/examples/coap-client.c @@ -2223,6 +2223,13 @@ main(int argc, char **argv) { memcpy(&dst, &info_list->addr, sizeof(dst)); coap_free_address_info(info_list); +#if COAP_AF_LLC_SUPPORT + if (coap_is_af_llc(&dst) && !node_str[0]) { + coap_log_err("-a option is required for LLC requests\n"); + goto failed; + } +#endif /* ! COAP_AF_LLC_SUPPORT */ + ctx = coap_new_context(NULL); if (!ctx) { coap_log_emerg("cannot create context\n"); diff --git a/examples/coap-server.c b/examples/coap-server.c index c01ec27f..833dc119 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -137,7 +137,7 @@ static uint32_t block_mode = COAP_BLOCK_USE_LIBCOAP; static int echo_back = 0; static uint32_t csm_max_message_size = 0; static size_t extended_token_size = COAP_TOKEN_DEFAULT_MAX; -static coap_proto_t use_unix_proto = COAP_PROTO_NONE; +static coap_proto_t use_proto = COAP_PROTO_NONE; static int enable_ws = 0; static int ws_port = 80; static int wss_port = 443; @@ -1738,6 +1738,22 @@ usage(const char *program, const char *version) { "\t \t\tis sent to the call-home client using a PUT request\n" "\t-A address\tInterface address to bind to\n" , program); + fprintf(stderr, + "\t \t\tExamples:\n" +#if COAP_IPV4_SUPPORT + "\t \t\t\tIPv4: 0.0.0.0\n" +#endif /* COAP_IPV4_SUPPORT */ +#if COAP_IPV6_SUPPORT + "\t \t\t\tIPv6: ::1\n" +#endif /* COAP_IPV6_SUPPORT */ +#if COAP_AF_UNIX_SUPPORT + "\t \t\t\tUnix domain: /run/user/1000/coap.sock\n" +#endif /* COAP_AF_UNIX_SUPPORT */ +#if COAP_AF_LLC_SUPPORT + "\t \t\t\tLLC: llc[70:d8:23:16:52:b5]:ac\n" + "\t \t\t\t ^ interface MAC ^ SAP\n" +#endif /* COAP_AF_LLC_SUPPORT */ + ); fprintf(stderr, "\t-B resource[:check]\n" "\t \t\tFor usage when setting up forward-dynamic proxy sessions\n" @@ -1784,7 +1800,8 @@ usage(const char *program, const char *version) { "\t \t\tThis option can be repeated to provide multiple upstream\n" "\t \t\tservers that are round-robin load balanced\n" "\t-T max_token_length\tSet the maximum token length (8-65804)\n" - "\t-U type\t\tTreat address defined by -A as a Unix socket address.\n" + "\t-U type\t\tDefine server listening protocol for Unix domain/LLC\n" + "\t \t\taddresses specified by the -A option. Default is 'coap'.\n" "\t \t\ttype is 'coap', 'coaps', 'coap+tcp' or 'coaps+tcp'\n" "\t-V num \t\tVerbosity level (default 3, maximum is 7) for (D)TLS\n" "\t \t\tlibrary logging\n" @@ -2058,7 +2075,7 @@ get_context(const char *node, const char *port) { } scheme_hint_bits = coap_get_available_scheme_hint_bits(cert_file != NULL || key_defined != 0, - enable_ws, use_unix_proto); + enable_ws, use_proto); info_list = coap_resolve_address_info(node ? &local : NULL, u_s_port, s_port, ws_port, wss_port, AI_PASSIVE | AI_NUMERICHOST, @@ -2458,30 +2475,30 @@ cmdline_read_identity_check(char *arg) { } static int -cmdline_unix(char *arg) { +cmdline_proto(char *arg) { if (!strcmp("coap", arg)) { - use_unix_proto = COAP_PROTO_UDP; + use_proto = COAP_PROTO_UDP; return 1; } else if (!strcmp("coaps", arg)) { if (!coap_dtls_is_supported()) { - coap_log_err("unix with dtls is not supported\n"); + coap_log_err("dtls is not supported\n"); return 0; } - use_unix_proto = COAP_PROTO_DTLS; + use_proto = COAP_PROTO_DTLS; return 1; } else if (!strcmp("coap+tcp", arg)) { if (!coap_tcp_is_supported()) { - coap_log_err("unix with stream is not supported\n"); + coap_log_err("tcp is not supported\n"); return 0; } - use_unix_proto = COAP_PROTO_TCP; + use_proto = COAP_PROTO_TCP; return 1; } else if (!strcmp("coaps+tcp", arg)) { if (!coap_tls_is_supported()) { - coap_log_err("unix with tls is not supported\n"); + coap_log_err("tls is not supported\n"); return 0; } - use_unix_proto = COAP_PROTO_TLS; + use_proto = COAP_PROTO_TLS; return 1; } return 0; @@ -2829,7 +2846,7 @@ main(int argc, char **argv) { #endif /* COAP_PROXY_SUPPORT */ break; case 'U': - if (!cmdline_unix(optarg)) { + if (!cmdline_proto(optarg)) { usage(argv[0], LIBCOAP_PACKAGE_VERSION); goto failed; } diff --git a/include/coap3/coap_address.h b/include/coap3/coap_address.h index 101ed312..333079c9 100644 --- a/include/coap3/coap_address.h +++ b/include/coap3/coap_address.h @@ -155,6 +155,21 @@ coap_address_clr_addr(coap_address_t *addr) { #else /* ! WITH_LWIP && ! WITH_CONTIKI && ! RIOT_VERSION */ +#if COAP_AF_LLC_SUPPORT + +/* Including net/if.h after linux/llc.h results in redefinitions of numerous + * symbols. + */ +#include +#include + +/* 00:11:22:33:44:55 */ +#define HW_ADDRSTRLEN 17 +/* llc[00:11:22:33:44:55]:DC */ +#define LLC_HOST_LEN (4 + HW_ADDRSTRLEN + 4) + +#endif /* COAP_AF_LLC_SUPPORT */ + #ifdef _WIN32 #define sa_family_t short #endif /* _WIN32 */ @@ -174,6 +189,9 @@ struct coap_address_t { struct sockaddr_in sin; struct sockaddr_in6 sin6; struct coap_sockaddr_un cun; /* CoAP shortened special */ +#if COAP_AF_LLC_SUPPORT + struct sockaddr_llc llc; +#endif /* COAP_AF_LLC_SUPPORT */ } addr; }; @@ -217,14 +235,14 @@ typedef struct coap_addr_info_t { * @param have_pki_psk Set to @c 1 if PSK/PKI information is known else @c 0. * @param ws_check Set to @c 1 is WebSockets is to be included in the list * else @c 0. - * @param use_unix_proto Set to the appropriate protocol to use for Unix + * @param use_proto Set to the appropriate protocol to use for Unix/LLC * sockets, else set to COAP_PROTO_NONE for INET / INET6 * sockets. * @return A bit mask of the available CoAP protocols (can be @c 0 if none) * suitable for passing to coap_resolve_address_info(). */ uint32_t coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check, - coap_proto_t use_unix_proto); + coap_proto_t use_proto); /** * coap_resolve_type_t values @@ -290,6 +308,18 @@ void coap_address_init(coap_address_t *addr); int coap_address_set_unix_domain(coap_address_t *addr, const uint8_t *host, size_t host_len); +/** + * Copy the parsed LLC host into @p addr, setting other fields as appropriate. + * + * @param addr coap_address_t to update. + * @param host The parsed host from the CoAP URI. + * @param host_len The length of the parsed host from the CoAP URI. + * + * @return @c 1 success, @c 0 failure. + */ +int coap_address_set_llc(coap_address_t *addr, + const uint8_t *host, size_t host_len); + /* Convenience function to copy IPv6 addresses without garbage. */ #if defined(WITH_LWIP) || defined(WITH_CONTIKI) || defined(RIOT_VERSION) COAP_STATIC_INLINE void @@ -346,6 +376,12 @@ int coap_is_bcast(const coap_address_t *a); */ int coap_is_af_unix(const coap_address_t *a); +/** + * Checks if given address @p a denotes an AF_LLC address. This function + * returns @c 1 if @p a is of type AF_LLC, @c 0 otherwise. + */ +int coap_is_af_llc(const coap_address_t *a); + #else /* WITH_CONTIKI || RIOT_VERSION */ /** diff --git a/include/coap3/coap_io_internal.h b/include/coap3/coap_io_internal.h index c02204e4..cf72a400 100644 --- a/include/coap3/coap_io_internal.h +++ b/include/coap3/coap_io_internal.h @@ -85,6 +85,7 @@ struct coap_socket_t { #define COAP_SOCKET_CAN_CONNECT 0x0800 /**< non blocking client socket can now connect without blocking */ #define COAP_SOCKET_MULTICAST 0x1000 /**< socket is used for multicast communication */ #define COAP_SOCKET_SLAVE 0x2000 /**< socket is a slave socket - do not close */ +#define COAP_SOCKET_WANT_SENDTO 0x4000 /**< socket requires a destination address for sending */ #if COAP_SERVER_SUPPORT coap_endpoint_t *coap_malloc_endpoint(void); diff --git a/include/coap3/coap_uri.h b/include/coap3/coap_uri.h index f9612f21..b94ebe69 100644 --- a/include/coap3/coap_uri.h +++ b/include/coap3/coap_uri.h @@ -98,6 +98,16 @@ coap_uri_scheme_is_secure(const coap_uri_t *uri) { */ int coap_host_is_unix_domain(const coap_str_const_t *host); +/** + * Determines from the @p host whether this is an LLC socket request. + * + * @param host The host object. + * + * @return @c 0 on failure, or @c 1 on success. + * + */ +int coap_host_is_llc(const coap_str_const_t *host); + /** * Creates a new coap_uri_t object from the specified URI. Returns the new * object or NULL on error. The memory allocated by the new coap_uri_t diff --git a/libcoap-3.map b/libcoap-3.map index 69a8fd7b..e5aa4634 100644 --- a/libcoap-3.map +++ b/libcoap-3.map @@ -18,6 +18,7 @@ global: coap_address_equals; coap_address_get_port; coap_address_init; + coap_address_set_llc; coap_address_set_port; coap_address_set_unix_domain; coap_af_unix_is_supported; @@ -134,6 +135,7 @@ global: coap_get_tls_library_version; coap_get_uri_path; coap_handle_event; + coap_host_is_llc; coap_host_is_unix_domain; coap_insert_optlist; coap_io_do_epoll; @@ -150,6 +152,7 @@ global: coap_io_process_with_fds; coap_ipv4_is_supported; coap_ipv6_is_supported; + coap_is_af_llc; coap_is_af_unix; coap_is_bcast; coap_is_mcast; diff --git a/libcoap-3.sym b/libcoap-3.sym index 168f39ec..ecccf7fb 100644 --- a/libcoap-3.sym +++ b/libcoap-3.sym @@ -16,6 +16,7 @@ coap_address_copy coap_address_equals coap_address_get_port coap_address_init +coap_address_set_llc coap_address_set_port coap_address_set_unix_domain coap_af_unix_is_supported @@ -132,6 +133,7 @@ coap_get_resource_from_uri_path coap_get_tls_library_version coap_get_uri_path coap_handle_event +coap_host_is_llc coap_host_is_unix_domain coap_insert_optlist coap_io_do_epoll @@ -148,6 +150,7 @@ coap_io_process_terminate_loop coap_io_process_with_fds coap_ipv4_is_supported coap_ipv6_is_supported +coap_is_af_llc coap_is_af_unix coap_is_bcast coap_is_mcast diff --git a/man/coap-client.txt.in b/man/coap-client.txt.in index ea08d5e0..aa773a58 100644 --- a/man/coap-client.txt.in +++ b/man/coap-client.txt.in @@ -384,6 +384,15 @@ Put the contents of file 'to_upload' with content type 'binary' (i.e. application/octet-stream) into resource 'ck' on 'fec0::3' using a token of '3a' via the 'PUT' method. +* Example +---- +coap-client -a llc[70:d8:23:16:52:b5]:3e coaps://llc[9c:6b:00:12:59:17]:3e/time +---- +Query the resource '/time' on server on a hardware interface with the MAC +address '9c:6b:00:12:59:17' on the Service Access Point (SAP) '3e' using DTLS, +receiving the response on a local hardware interface with the MAC address +'70:d8:23:16:52:b5' on the SAP '3e'. + FILES ------ There are no configuration files. diff --git a/man/coap-server.txt.in b/man/coap-server.txt.in index 9489b1e7..a4c02315 100644 --- a/man/coap-server.txt.in +++ b/man/coap-server.txt.in @@ -193,9 +193,10 @@ OPTIONS - General Set the maximum token length (8-65804). *-U* type:: - Treat address defined by *-A* as a Unix socket address. + Define server listening protocol (if *-A* is a Unix domain or LLC socket + address). Type is 'coap' (using datagram), 'coap+tcp' (using stream), 'coaps' - (DTLS using datagram) or 'coaps+tcp' (TLS using stream). + (DTLS using datagram) or 'coaps+tcp' (TLS using stream). Default is 'coap'. *-V* num:: The verbosity level to use (default 3, maximum is 7) for (D)TLS @@ -377,6 +378,14 @@ Let the server listen on localhost (port '5683') for UDP/TCP, and then open an upstream coap session to 'upstream-proxy' for proxying onwards the CoAP client's request. +* Example +---- +coap-server -U coaps -A llc[9c:6b:00:12:59:17]:3e +---- +Let the server listen on a hardware interface with the MAC address +'9c:6b:00:12:59:17' on a Service Access Point (SAP) '3e' (in hexadecimal), +accepting only DTLS connections. + FILES ------ There are no configuration files. diff --git a/man/coap_address.txt.in b/man/coap_address.txt.in index 44bf4dd5..191e8a32 100644 --- a/man/coap_address.txt.in +++ b/man/coap_address.txt.in @@ -50,7 +50,7 @@ SYNOPSIS *void coap_address_set_port(coap_address_t *_addr_, uint16_t _port_);* *uint32_t coap_get_available_scheme_hint_bits(int _have_pki_psk_, -int _ws_check_, coap_proto_t _use_unix_proto_);* +int _ws_check_, coap_proto_t _use_proto_);* *coap_addr_info_t *coap_resolve_address_info(const coap_str_const_t *_address_, uint16_t _port_, uint16_t _secure_port_, uint16_t _ws_port_, @@ -193,9 +193,9 @@ The *coap_get_available_scheme_hint_bits*() function is used for servers to determine what coap schemes are supported in the underlying libcoap library. _have_pki_psk_ can be set to 1 to check for (D)DTLS support, else 0. _ws_check_ can be set to 1 to check for WebSockets support, else 0. -_use_unix_proto_, if not set to COAP_PROTO_NONE, hints at the specific CoAP -protocol to use over a Unix socket. The output is suitable for input for the -*coap_address_resolve_info*()'s _scheme_hint_bits_. +_use_proto_, if not set to COAP_PROTO_NONE, hints at the specific CoAP +protocol to use over a Unix/LLC socket. The output is suitable for input for +the *coap_address_resolve_info*()'s _scheme_hint_bits_. *Function: coap_resolve_address_info()* diff --git a/src/coap_address.c b/src/coap_address.c index 58ad47aa..c14d60ae 100644 --- a/src/coap_address.c +++ b/src/coap_address.c @@ -73,6 +73,10 @@ coap_address_get_port(const coap_address_t *addr) { case AF_INET6: return ntohs(addr->addr.sin6.sin6_port); #endif /* COAP_IPV6_SUPPORT */ +#if COAP_AF_LLC_SUPPORT + case AF_LLC: + return addr->addr.llc.sllc_sap; +#endif /* COAP_AF_LLC_SUPPORT */ default: /* undefined */ ; } @@ -93,6 +97,10 @@ coap_address_set_port(coap_address_t *addr, uint16_t port) { addr->addr.sin6.sin6_port = htons(port); break; #endif /* COAP_IPV6_SUPPORT */ +#if COAP_AF_LLC_SUPPORT + case AF_LLC: + addr->addr.llc.sllc_sap = port & 0xFF; +#endif /* COAP_AF_LLC_SUPPORT */ default: /* undefined */ ; } @@ -160,6 +168,16 @@ coap_is_af_unix(const coap_address_t *a) { #endif /* ! COAP_AF_UNIX_SUPPORT */ } +int +coap_is_af_llc(const coap_address_t *a) { +#if COAP_AF_LLC_SUPPORT + return a->addr.sa.sa_family == AF_LLC; +#else /* ! COAP_AF_LLC_SUPPORT */ + (void)a; + return 0; +#endif /* ! COAP_AF_LLC_SUPPORT */ +} + int coap_is_mcast(const coap_address_t *a) { if (!a) @@ -404,6 +422,57 @@ coap_address_set_unix_domain(coap_address_t *addr, #endif /* ! COAP_AF_UNIX_SUPPORT */ } +#if COAP_AF_LLC_SUPPORT +/* Needed for ether_aton() and ARPHRD_ETHER. */ +#include +#endif /* ! COAP_AF_LLC_SUPPORT */ + +int +coap_address_set_llc(coap_address_t *addr, + const uint8_t *host, size_t host_len) { +#if COAP_AF_LLC_SUPPORT + char addrstr[HW_ADDRSTRLEN + 1] = { 0 }; + struct ether_addr *ether_addr = NULL; + char sapstr[3] = { 0 }; + unsigned long sap; + + if (host_len < LLC_HOST_LEN) + coap_log_err("%s(): Invalid address length (%zu)\n", __func__, host_len); + + host += 4; + strncpy(addrstr, (const char *)host, HW_ADDRSTRLEN); + + ether_addr = ether_aton(addrstr); + if (!ether_addr) + return 0; + + host += HW_ADDRSTRLEN + 2; + strncpy(sapstr, (const char *)host, 2); + + sap = strtoul(sapstr, NULL, 16); + if (sap == ULONG_MAX) + return 0; + + coap_address_init(addr); + + addr->size = sizeof(addr->addr.llc); + + addr->addr.llc.sllc_family = AF_LLC; + addr->addr.llc.sllc_arphrd = ARPHRD_ETHER; + addr->addr.llc.sllc_sap = sap & 0xFF; + + memcpy(addr->addr.llc.sllc_mac, ether_addr, IFHWADDRLEN); + + return 1; +#else /* ! COAP_AF_LLC_SUPPORT */ + (void)addr; + (void)host; + (void)host_len; + + return 0; +#endif /* ! COAP_AF_LLC_SUPPORT */ +} + static void update_port(coap_address_t *addr, uint16_t port, uint16_t default_port, int update_port0) { @@ -421,7 +490,7 @@ update_port(coap_address_t *addr, uint16_t port, uint16_t default_port, uint32_t coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check, - coap_proto_t use_unix_proto) { + coap_proto_t use_proto) { uint32_t scheme_hint_bits = 0; coap_uri_scheme_t scheme; @@ -463,7 +532,7 @@ coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check, } } - switch (use_unix_proto) { + switch (use_proto) { /* For AF_UNIX, can only listen on a single endpoint */ case COAP_PROTO_UDP: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP; @@ -483,7 +552,7 @@ coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check, case COAP_PROTO_WSS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_WS; break; - case COAP_PROTO_NONE: /* If use_unix_proto was not defined */ + case COAP_PROTO_NONE: /* If use_proto was not defined */ case COAP_PROTO_LAST: default: break; @@ -697,6 +766,29 @@ coap_resolve_address_info(const coap_str_const_t *address, return info; } #endif /* COAP_AF_UNIX_SUPPORT */ +#if COAP_AF_LLC_SUPPORT + if (address && coap_host_is_llc(address)) { + for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) { + if (scheme_hint_bits & (1 << scheme)) { + break; + } + } + if (scheme == COAP_URI_SCHEME_LAST) { + return NULL; + } + info = get_coap_addr_info(scheme); + if (info == NULL) { + return NULL; + } + + if (!coap_address_set_llc(&info->addr, address->s, address->length)) { + coap_free_type(COAP_STRING, info); + return NULL; + } + + return info; + } +#endif /* COAP_AF_LLC_SUPPORT */ memset(addrstr, 0, sizeof(addrstr)); if (address && address->length) { diff --git a/src/coap_debug.c b/src/coap_debug.c index 2144913d..882686ff 100644 --- a/src/coap_debug.c +++ b/src/coap_debug.c @@ -266,6 +266,13 @@ coap_print_addr(const coap_address_t *addr, unsigned char *buf, size_t len) { snprintf((char *)buf, len, "%s", addr->addr.cun.sun_path); break; #endif /* COAP_AF_UNIX_SUPPORT */ +#if COAP_AF_LLC_SUPPORT + case AF_LLC: + snprintf((char *)buf, len, "%s:%02x", + coap_print_ip_addr(addr, scratch, sizeof(scratch)), + coap_address_get_port(addr)); + break; +#endif /* COAP_AF_LLC_SUPPORT */ default: /* Include trailing NULL if possible */ memcpy(buf, "(unknown address type)", min(22+1, len)); @@ -442,6 +449,17 @@ coap_print_ip_addr(const coap_address_t *addr, char *buf, size_t len) { snprintf(buf, len, "%s", addr->addr.cun.sun_path); return buf; #endif /* COAP_AF_UNIX_SUPPORT */ +#if COAP_AF_LLC_SUPPORT + case AF_LLC: + snprintf((char *)buf, len, "llc[%02x:%02x:%02x:%02x:%02x:%02x]", + addr->addr.llc.sllc_mac[0], + addr->addr.llc.sllc_mac[1], + addr->addr.llc.sllc_mac[2], + addr->addr.llc.sllc_mac[3], + addr->addr.llc.sllc_mac[4], + addr->addr.llc.sllc_mac[5]); + return buf; +#endif /* COAP_AF_LLC_SUPPORT */ default: /* Include trailing NULL if possible */ memcpy(buf, "(unknown address type)", min(22+1, len)); diff --git a/src/coap_dgrm_posix.c b/src/coap_dgrm_posix.c index 14d3ce0d..88a083ab 100644 --- a/src/coap_dgrm_posix.c +++ b/src/coap_dgrm_posix.c @@ -161,6 +161,10 @@ coap_socket_bind_udp(coap_socket_t *sock, case AF_UNIX: break; #endif /* COAP_AF_UNIX_SUPPORT */ +#if COAP_AF_LLC_SUPPORT + case AF_LLC: + break; +#endif /* COAP_AF_LLC_SUPPORT */ default: coap_log_alert("coap_socket_bind_udp: unsupported sa_family\n"); break; @@ -258,6 +262,10 @@ coap_socket_connect_udp(coap_socket_t *sock, case AF_UNIX: break; #endif /* COAP_AF_UNIX_SUPPORT */ +#if COAP_AF_LLC_SUPPORT + case AF_LLC: + break; +#endif /* COAP_AF_LLC_SUPPORT */ default: coap_log_alert("coap_socket_connect_udp: unsupported sa_family %d\n", connect_addr.addr.sa.sa_family); @@ -325,18 +333,35 @@ coap_socket_connect_udp(coap_socket_t *sock, return 1; } - if (connect(sock->fd, &connect_addr.addr.sa, connect_addr.size) == COAP_SOCKET_ERROR) { + switch (connect_addr.addr.sa.sa_family) { +#if COAP_AF_LLC_SUPPORT + /* Only SOCK_STREAM type AF_LLC sockets can be connect()ed. */ + case AF_LLC: + break; +#endif /* COAP_AF_LLC_SUPPORT */ #if COAP_AF_UNIX_SUPPORT - if (connect_addr.addr.sa.sa_family == AF_UNIX) { - coap_log_warn("coap_socket_connect_udp: connect: %s: %s\n", - connect_addr.addr.cun.sun_path, coap_socket_strerror()); - } else + case AF_UNIX: #endif /* COAP_AF_UNIX_SUPPORT */ - { - coap_log_warn("coap_socket_connect_udp: connect: %s (%d)\n", - coap_socket_strerror(), connect_addr.addr.sa.sa_family); + default: + if (connect(sock->fd, &connect_addr.addr.sa, connect_addr.size) == COAP_SOCKET_ERROR) { +#if COAP_AF_UNIX_SUPPORT + if (connect_addr.addr.sa.sa_family == AF_UNIX) { + coap_log_warn("coap_socket_connect_udp: connect: %s: %s\n", + connect_addr.addr.cun.sun_path, coap_socket_strerror()); + } else +#endif /* COAP_AF_UNIX_SUPPORT */ + { + coap_log_warn("coap_socket_connect_udp: connect: %s (%d)\n", + coap_socket_strerror(), connect_addr.addr.sa.sa_family); + } + goto error; } - goto error; + + if (getpeername(sock->fd, &remote_addr->addr.sa, &remote_addr->size) == COAP_SOCKET_ERROR) { + coap_log_warn("coap_socket_connect_udp: getpeername: %s\n", + coap_socket_strerror()); + } + break; } if (getsockname(sock->fd, &local_addr->addr.sa, &local_addr->size) == COAP_SOCKET_ERROR) { @@ -344,10 +369,11 @@ coap_socket_connect_udp(coap_socket_t *sock, coap_socket_strerror()); } - if (getpeername(sock->fd, &remote_addr->addr.sa, &remote_addr->size) == COAP_SOCKET_ERROR) { - coap_log_warn("coap_socket_connect_udp: getpeername: %s\n", - coap_socket_strerror()); - } +#if COAP_AF_LLC_SUPPORT + /* AF_LLC sockets send to the local address without an explicit destination. */ + if (coap_is_af_llc(&connect_addr)) + sock->flags |= COAP_SOCKET_WANT_SENDTO; +#endif /* COAP_AF_LLC_SUPPORT */ sock->flags |= COAP_SOCKET_CONNECTED; return 1; @@ -489,7 +515,8 @@ coap_socket_send(coap_socket_t *sock, coap_session_t *session, bytes_written = -1; else bytes_written = (ssize_t)datalen; - } else if (sock->flags & COAP_SOCKET_CONNECTED) { + } else if (sock->flags & COAP_SOCKET_CONNECTED && + !(sock->flags & COAP_SOCKET_WANT_SENDTO)) { #ifdef _WIN32 bytes_written = send(sock->fd, (const char *)data, (int)datalen, 0); #else @@ -628,6 +655,10 @@ coap_socket_send(coap_socket_t *sock, coap_session_t *session, case AF_UNIX: break; #endif /* COAP_AF_UNIX_SUPPORT */ +#if COAP_AF_LLC_SUPPORT + case AF_LLC: + break; +#endif /* COAP_AF_LLC_SUPPORT */ default: /* error */ coap_log_warn("protocol not supported\n"); diff --git a/src/coap_strm_posix.c b/src/coap_strm_posix.c index 9b05faa2..de34c3ac 100644 --- a/src/coap_strm_posix.c +++ b/src/coap_strm_posix.c @@ -124,6 +124,10 @@ coap_socket_connect_tcp1(coap_socket_t *sock, case AF_UNIX: break; #endif /* COAP_AF_UNIX_SUPPORT */ +#if COAP_AF_LLC_SUPPORT + case AF_LLC: + break; +#endif /* COAP_AF_LLC_SUPPORT */ default: coap_log_alert("coap_socket_connect_tcp1: unsupported sa_family\n"); break; @@ -172,6 +176,17 @@ coap_socket_connect_tcp1(coap_socket_t *sock, coap_socket_strerror()); } +#if COAP_AF_LLC_SUPPORT + /* Current (7.0, 6.19.12 and similar vintage) Linux kernels do not return + * -EINPROGRESS for AF_LLC sockets with zero timeouts, as with AF_INET, so + * let’s assume we are connected, because getpeername() will error out. + */ + if (coap_is_af_llc(remote_addr)) { + sock->flags |= COAP_SOCKET_CONNECTED; + return 1; + } +#endif /* COAP_AF_LLC_SUPPORT */ + if (getpeername(sock->fd, &remote_addr->addr.sa, &remote_addr->size) == COAP_SOCKET_ERROR) { coap_log_warn("coap_socket_connect_tcp1: getpeername: %s\n", coap_socket_strerror()); @@ -289,6 +304,10 @@ coap_socket_bind_tcp(coap_socket_t *sock, case AF_UNIX: break; #endif /* COAP_AF_UNIX_SUPPORT */ +#if COAP_AF_LLC_SUPPORT + case AF_LLC: + break; +#endif /* COAP_AF_LLC_SUPPORT */ default: coap_log_alert("coap_socket_bind_tcp: unsupported sa_family\n"); } diff --git a/src/coap_uri.c b/src/coap_uri.c index b0c5c370..09c6fc00 100644 --- a/src/coap_uri.c +++ b/src/coap_uri.c @@ -70,8 +70,9 @@ coap_uri_info_t coap_uri_scheme[COAP_URI_SCHEME_LAST] = { * -2 No '://' * -3 Ipv6 definition error or no host defined after scheme:// * -4 Invalid port value - * -5 Port defined for Unix domain + * -5 Port defined for Unix domain or LLC host * -6 Hostname > 255 chars + * -7 Invalid LLC address */ static int coap_split_uri_sub(const uint8_t *str_var, @@ -82,6 +83,7 @@ coap_split_uri_sub(const uint8_t *str_var, int res = 0; size_t i; int is_unix_domain = 0; + int is_llc = 0; if (!str_var || !uri || len == 0) return -1; @@ -178,6 +180,7 @@ coap_split_uri_sub(const uint8_t *str_var, /* p points to beginning of Uri-Host */ q = p; + if (len && *p == '[') { /* IPv6 address reference or Unix domain */ ++p; @@ -197,6 +200,52 @@ coap_split_uri_sub(const uint8_t *str_var, COAP_SET_STR(&uri->host, q - p, p); ++q; --len; +#if COAP_AF_LLC_SUPPORT + } else if ((len >= LLC_HOST_LEN) && strncmp((const char *)p, "llc[", 4) == 0) { + unsigned long sap = 0; + + is_llc = 1; + + while (len && *q != ']') { + ++q; + --len; + } + + if (!len || *q != ']' || p == q) { + res = -7; + goto error; + } + + ++q; + --len; + + if (!len || *q != ':') { + coap_log_warn("LLC SAP missing in URI\n"); + res = -7; + goto error; + } + + ++q; + --len; + + while (len && isxdigit(*q)) { + if (*q >= 'a' && *q <= 'f') + sap = sap * 16 + (*q - 'a' + 10); + else if (*q >= 'A' && *q <= 'F') + sap = sap * 16 + (*q - 'A' + 10); + + ++q; + --len; + } + + if (sap > UINT8_MAX) { + coap_log_warn("LLC SAP invalid (%lu > 255)\n", sap); + res = -7; + goto error; + } + + COAP_SET_STR(&uri->host, q - p, p); +#endif /* COAP_AF_LLC_SUPPORT */ } else { /* IPv4 address, FQDN or Unix domain socket */ if (len >= 3 && p[0] == '%' && p[1] == '2' && @@ -226,7 +275,7 @@ coap_split_uri_sub(const uint8_t *str_var, /* check for Uri-Port (invalid for Unix) */ if (len && *q == ':') { - if (is_unix_domain) { + if (is_unix_domain || is_llc) { res = -5; goto error; } @@ -415,6 +464,54 @@ coap_host_is_unix_domain(const coap_str_const_t *host) { return 0; } +int +coap_host_is_llc(const coap_str_const_t *host) { +#if COAP_AF_LLC_SUPPORT + size_t length; + const uint8_t *s = NULL; + int i = 0; + + length = host->length; + if (length != LLC_HOST_LEN) + return 0; + + s = host->s; + + if (strncmp((const char *)s, "llc[", 4) != 0) + return 0; + + length -= 4; + s += 4; + + while (length) { + if (!isxdigit(s[0]) || !isxdigit(s[1])) + return 0; + + length -= 2; + s += 2; + + i++; + + if (length <= 0 || i >= 6) + break; + + if (s[0] != ':') + return 0; + + length--; + s++; + } + + if (s[0] != ']' || s[1] != ':' || !isxdigit(s[2]) || !isxdigit(s[3])) + return 0; + + return 1; +#else /* COAP_AF_LLC_SUPPORT */ + (void)host; + return 0; +#endif /* COAP_AF_LLC_SUPPORT */ +} + /** * Calculates decimal value from hexadecimal ASCII character given in * @p c. The caller must ensure that @p c actually represents a valid diff --git a/tests/test_uri.c b/tests/test_uri.c index 9dc770b3..1e64110a 100644 --- a/tests/test_uri.c +++ b/tests/test_uri.c @@ -1048,6 +1048,26 @@ t_parse_uri33(void) { coap_delete_pdu(pdu); } +static void +t_parse_uri34(void) { + const char *uri_s = "coap://llc[00:11:22:33:44:55]:DC/time"; + coap_uri_t uri; + + if (coap_split_uri((const uint8_t *)uri_s, strlen(uri_s), &uri)) + CU_FAIL("LLC URI parsing failed"); + + CU_ASSERT(uri.host.length == LLC_HOST_LEN); + CU_ASSERT_NSTRING_EQUAL(uri.host.s, "llc[00:11:22:33:44:55]:DC", 25); + + CU_ASSERT(uri.port == COAP_DEFAULT_PORT); + + CU_ASSERT(uri.path.length == 4); + CU_ASSERT_NSTRING_EQUAL(uri.path.s, "time", 4); + + CU_ASSERT(uri.query.length == 0); + CU_ASSERT(uri.query.s == NULL); +} + CU_pSuite t_init_uri_tests(void) { CU_pSuite suite; @@ -1099,6 +1119,7 @@ t_init_uri_tests(void) { URI_TEST(suite, t_parse_uri31); URI_TEST(suite, t_parse_uri32); URI_TEST(suite, t_parse_uri33); + URI_TEST(suite, t_parse_uri34); return suite; }