dtls.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 HAW Hamburg
3  * Freie Universität Berlin
4  * Inria
5  * Daniele Lacamera
6  * Ken Bannister
7  *
8  * This file is subject to the terms and conditions of the GNU Lesser
9  * General Public License v2.1. See the file LICENSE in the top level
10  * directory for more details.
11  */
12 
471 #ifndef NET_SOCK_DTLS_H
472 #define NET_SOCK_DTLS_H
473 
474 #include <assert.h>
475 #include <stdint.h>
476 #include <stdlib.h>
477 #include <sys/types.h>
478 
479 /* net/sock/async/types.h included by net/sock.h needs to re-typedef the
480  * `sock_dtls_t` to prevent cyclic includes */
481 #if defined (__clang__)
482 # pragma clang diagnostic push
483 # pragma clang diagnostic ignored "-Wtypedef-redefinition"
484 #endif
485 
486 #include "net/sock.h"
487 #include "net/sock/udp.h"
488 #include "net/credman.h"
489 
490 #ifdef __cplusplus
491 extern "C" {
492 #endif
493 
507 #ifndef CONFIG_DTLS_HANDSHAKE_BUFSIZE_EXP
508 #define CONFIG_DTLS_HANDSHAKE_BUFSIZE_EXP 8
509 #endif
510 
515 #ifndef DTLS_HANDSHAKE_BUFSIZE
516 #define DTLS_HANDSHAKE_BUFSIZE (1 << CONFIG_DTLS_HANDSHAKE_BUFSIZE_EXP)
517 #endif
518 
522 #define SOCK_DTLS_HANDSHAKE (EXDEV)
523 
529 enum {
533 };
541 enum {
544 };
553 typedef struct sock_dtls sock_dtls_t;
554 
555 #if defined (__clang__)
556 # pragma clang diagnostic pop
557 #endif
558 
563 
569 void sock_dtls_init(void);
570 
591 int sock_dtls_create(sock_dtls_t *sock, sock_udp_t *udp_sock,
592  credman_tag_t tag, unsigned version, unsigned role);
593 
604 
623  sock_dtls_session_t *remote);
624 
634 
662 ssize_t sock_dtls_recv(sock_dtls_t *sock, sock_dtls_session_t *remote,
663  void *data, size_t maxlen, uint32_t timeout);
664 
705 ssize_t sock_dtls_recv_buf(sock_dtls_t *sock, sock_dtls_session_t *remote,
706  void **data, void **buf_ctx, uint32_t timeout);
707 
740 ssize_t sock_dtls_send(sock_dtls_t *sock, sock_dtls_session_t *remote,
741  const void *data, size_t len, uint32_t timeout);
742 
755 void sock_dtls_close(sock_dtls_t *sock);
756 
781 static inline int sock_dtls_session_create(sock_dtls_t *sock,
782  const sock_udp_ep_t *ep,
783  sock_dtls_session_t *remote,
784  unsigned timeout)
785 {
786  int res;
787  uint8_t buf[DTLS_HANDSHAKE_BUFSIZE];
788 
789  assert(sock);
790  assert(remote);
791 
792  res = sock_dtls_session_init(sock, ep, remote);
793  if (res <= 0) {
794  return res;
795  }
796 
797  res = sock_dtls_recv(sock, remote, buf, sizeof(buf), timeout);
798  return res == -SOCK_DTLS_HANDSHAKE ? 0 : res;
799 }
800 
801 #include "sock_dtls_types.h"
802 
803 #ifdef __cplusplus
804 }
805 #endif
806 
807 #endif /* NET_SOCK_DTLS_H */
808 
credman.h
(D)TLS credentials management module definitions
sock_dtls_get_udp_sock
sock_udp_t * sock_dtls_get_udp_sock(sock_dtls_t *sock)
Get underlying UDP sock.
udp.h
UDP sock definitions.
assert
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
SOCK_DTLS_1_3
@ SOCK_DTLS_1_3
DTLS version 1.3.
Definition: dtls.h:532
assert.h
POSIX.1-2008 compliant version of the assert macro.
sock_dtls_init
void sock_dtls_init(void)
Called exactly once during auto_init.
sock_dtls_session_create
static int sock_dtls_session_create(sock_dtls_t *sock, const sock_udp_ep_t *ep, sock_dtls_session_t *remote, unsigned timeout)
Creates a new DTLS session.
Definition: dtls.h:781
SOCK_DTLS_CLIENT
@ SOCK_DTLS_CLIENT
Endpoint client role.
Definition: dtls.h:542
sock_dtls_session::ep
sock_udp_ep_t ep
Remote endpoint the session is connected to.
Definition: sock_dtls_types.h:85
SOCK_DTLS_1_0
@ SOCK_DTLS_1_0
DTLS version 1.0.
Definition: dtls.h:530
DTLS_HANDSHAKE_BUFSIZE
#define DTLS_HANDSHAKE_BUFSIZE
Size buffer used in handshake to hold credentials.
Definition: dtls.h:516
sock_dtls_session_destroy
void sock_dtls_session_destroy(sock_dtls_t *sock, sock_dtls_session_t *remote)
Destroys an existing DTLS session.
sock_dtls
Information about DTLS sock.
Definition: sock_dtls_types.h:40
SOCK_DTLS_HANDSHAKE
#define SOCK_DTLS_HANDSHAKE
Return value for a successful handshake.
Definition: dtls.h:522
sock_udp
UDP sock type.
Definition: sock_types.h:128
sock_dtls_close
void sock_dtls_close(sock_dtls_t *sock)
Closes a DTLS sock.
sock_dtls_recv_buf
ssize_t sock_dtls_recv_buf(sock_dtls_t *sock, sock_dtls_session_t *remote, void **data, void **buf_ctx, uint32_t timeout)
Decrypts and provides stack-internal buffer space containing a message from a remote peer.
_sock_tl_ep
Common IP-based transport layer end point.
Definition: sock.h:213
sock_dtls_session
Information about remote client connected to the server.
Definition: sock_dtls_types.h:84
sock_dtls_types.h
tinydtls-specific types and functions definitions
SOCK_DTLS_SERVER
@ SOCK_DTLS_SERVER
Endpoint server role.
Definition: dtls.h:543
sock_dtls_session_init
int sock_dtls_session_init(sock_dtls_t *sock, const sock_udp_ep_t *ep, sock_dtls_session_t *remote)
Initialize session handshake.
credman_tag_t
uint16_t credman_tag_t
Tag of the credential.
Definition: credman.h:88
sock_dtls_create
int sock_dtls_create(sock_dtls_t *sock, sock_udp_t *udp_sock, credman_tag_t tag, unsigned version, unsigned role)
Creates a new DTLS sock object.
SOCK_DTLS_1_2
@ SOCK_DTLS_1_2
DTLS version 1.2.
Definition: dtls.h:531
sock_dtls_recv
ssize_t sock_dtls_recv(sock_dtls_t *sock, sock_dtls_session_t *remote, void *data, size_t maxlen, uint32_t timeout)
Receive handshake messages and application data from remote peer.
sock_dtls_send
ssize_t sock_dtls_send(sock_dtls_t *sock, sock_dtls_session_t *remote, const void *data, size_t len, uint32_t timeout)
Encrypts and sends a message to a remote peer.
sock.h
Common sock API definitions.