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 
sock_udp_t * sock_dtls_get_udp_sock(sock_dtls_t *sock)
Get underlying UDP sock.
void sock_dtls_init(void)
Called exactly once during auto_init.
void sock_dtls_session_destroy(sock_dtls_t *sock, sock_dtls_session_t *remote)
Destroys an existing DTLS session.
Information about DTLS sock.
Endpoint client role.
Definition: dtls.h:542
UDP sock definitions.
uint16_t credman_tag_t
Tag of the credential.
Definition: credman.h:88
Endpoint server role.
Definition: dtls.h:543
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
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.
Information about remote client connected to the server.
POSIX.1-2008 compliant version of the assert macro.
tinydtls-specific types and functions definitions
int sock_dtls_session_init(sock_dtls_t *sock, const sock_udp_ep_t *ep, sock_dtls_session_t *remote)
Initialize session handshake.
DTLS version 1.0.
Definition: dtls.h:530
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
DTLS version 1.3.
Definition: dtls.h:532
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.
Common IP-based transport layer end point.
Definition: sock.h:213
Common sock API definitions.
void sock_dtls_close(sock_dtls_t *sock)
Closes a DTLS sock.
UDP sock type.
Definition: sock_types.h:128
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...
#define DTLS_HANDSHAKE_BUFSIZE
Size buffer used in handshake to hold credentials.
Definition: dtls.h:516
DTLS version 1.2.
Definition: dtls.h:531
#define SOCK_DTLS_HANDSHAKE
Return value for a successful handshake.
Definition: dtls.h:522
(D)TLS credentials management module definitions
sock_udp_ep_t ep
Remote endpoint the session is connected to.
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.