udp.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016 Alexander Aring <aar@pengutronix.de>
3  * SPDX-FileCopyrightText: 2016 Freie Universität Berlin
4  * SPDX-FileCopyrightText: 2016 HAW Hamburg
5  * SPDX-FileCopyrightText: 2016 Kaspar Schleiser <kaspar@schleiser.de>
6  * SPDX-License-Identifier: LGPL-2.1-only
7  */
8 
9 #pragma once
10 
270 #include <assert.h>
271 #include <errno.h>
272 #include <stdint.h>
273 #include <stdlib.h>
274 #include <sys/types.h>
275 
276 /* net/sock/async/types.h included by net/sock.h needs to re-typedef the
277  * `sock_ip_t` to prevent cyclic includes */
278 #if defined (__clang__)
279 # pragma clang diagnostic push
280 # pragma clang diagnostic ignored "-Wtypedef-redefinition"
281 #endif
282 
283 #include "net/af.h"
284 #include "net/sock.h"
285 #include "net/ipv4/addr.h"
286 #include "net/ipv6/addr.h"
287 
288 #ifdef __cplusplus
289 extern "C" {
290 #endif
291 
292 typedef struct _sock_tl_ep sock_udp_ep_t;
300 typedef struct sock_udp sock_udp_t;
301 
302 #if defined (__clang__)
303 # pragma clang diagnostic pop
304 #endif
305 
309 typedef struct {
310 #if defined(MODULE_SOCK_AUX_LOCAL) || defined(DOXYGEN)
317 #endif /* MODULE_SOCK_AUX_ENDPOINT */
318 #if defined(MODULE_SOCK_AUX_TIMESTAMP) || defined(DOXYGEN)
324  uint64_t timestamp;
325 #endif /* MODULE_SOCK_AUX_TIMESTAP */
326 #if defined(MODULE_SOCK_AUX_RSSI) || defined(DOXYGEN)
332  int16_t rssi;
333 #endif /* MODULE_SOCK_AUX_RSSI */
334 #if defined(MODULE_SOCK_AUX_TTL) || defined(DOXYGEN)
340  uint8_t ttl;
341 #endif /* MODULE_SOCK_AUX_TTL */
344 
348 typedef struct {
349 #if defined(MODULE_SOCK_AUX_LOCAL) || defined(DOXYGEN)
356 #endif /* MODULE_SOCK_AUX_ENDPOINT */
357 #if defined(MODULE_SOCK_AUX_TIMESTAMP) || defined(DOXYGEN)
370  uint64_t timestamp;
371 #endif /* MODULE_SOCK_AUX_TIMESTAP*/
374 
424 int sock_udp_create(sock_udp_t *sock, const sock_udp_ep_t *local,
425  const sock_udp_ep_t *remote, uint16_t flags);
426 
435 
448 
460 
494 ssize_t sock_udp_recv_aux(sock_udp_t *sock, void *data, size_t max_len,
495  uint32_t timeout, sock_udp_ep_t *remote,
496  sock_udp_aux_rx_t *aux);
497 
529 static inline ssize_t sock_udp_recv(sock_udp_t *sock,
530  void *data, size_t max_len,
531  uint32_t timeout, sock_udp_ep_t *remote)
532 {
533  return sock_udp_recv_aux(sock, data, max_len, timeout, remote, NULL);
534 }
535 
580 ssize_t sock_udp_recv_buf_aux(sock_udp_t *sock, void **data, void **buf_ctx,
581  uint32_t timeout, sock_udp_ep_t *remote,
582  sock_udp_aux_rx_t *aux);
583 
626 static inline ssize_t sock_udp_recv_buf(sock_udp_t *sock,
627  void **data, void **buf_ctx,
628  uint32_t timeout,
629  sock_udp_ep_t *remote)
630 {
631  return sock_udp_recv_buf_aux(sock, data, buf_ctx, timeout, remote, NULL);
632 }
633 
668 ssize_t sock_udp_sendv_aux(sock_udp_t *sock, const iolist_t *snips,
669  const sock_udp_ep_t *remote, sock_udp_aux_tx_t *aux);
670 
706 static inline ssize_t sock_udp_send_aux(sock_udp_t *sock,
707  const void *data, size_t len,
708  const sock_udp_ep_t *remote,
709  sock_udp_aux_tx_t *aux)
710 {
711  const iolist_t snip = {
712  NULL,
713  (void *)data,
714  len,
715  };
716 
717  return sock_udp_sendv_aux(sock, &snip, remote, aux);
718 }
719 
753 static inline ssize_t sock_udp_send(sock_udp_t *sock,
754  const void *data, size_t len,
755  const sock_udp_ep_t *remote)
756 {
757  return sock_udp_send_aux(sock, data, len, remote, NULL);
758 }
759 
792 static inline ssize_t sock_udp_sendv(sock_udp_t *sock,
793  const iolist_t *snips,
794  const sock_udp_ep_t *remote)
795 {
796  return sock_udp_sendv_aux(sock, snips, remote, NULL);
797 }
798 
806 static inline bool sock_udp_ep_is_multicast(const sock_udp_ep_t *ep)
807 {
808  switch (ep->family) {
809 #ifdef SOCK_HAS_IPV6
810  case AF_INET6:
811  return ipv6_addr_is_multicast((const ipv6_addr_t *)&ep->addr.ipv6);
812 #endif
813 #ifdef SOCK_HAS_IPV4
814  case AF_INET:
815  return ipv4_addr_is_multicast((const ipv4_addr_t *)&ep->addr.ipv4);
816 #endif
817  default:
818  assert(0);
819  }
820 
821  return false;
822 }
823 
831 static inline bool sock_udp_ep_is_v6(const sock_udp_ep_t *ep)
832 {
833 #if !defined(SOCK_HAS_IPV6)
834  (void)ep;
835  return false;
836 #elif !defined(SOCK_HAS_IPV4)
837  (void)ep;
838  return true;
839 #else
840  return ep->family == AF_INET6;
841 #endif
842 }
843 
844 #include "sock_types.h"
845 
846 #ifdef __cplusplus
847 }
848 #endif
849 
Global UNIX address family definitions.
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:143
@ AF_INET6
internetwork address family with IPv6: UDP, TCP, etc.
Definition: af.h:36
@ AF_INET
internetwork address family: UDP, TCP, etc.
Definition: af.h:34
static bool ipv4_addr_is_multicast(const ipv4_addr_t *addr)
Check if addr is a multicast address.
Definition: addr.h:86
static bool ipv6_addr_is_multicast(const ipv6_addr_t *addr)
Check if addr is a multicast address.
Definition: addr.h:342
ssize_t sock_udp_recv_aux(sock_udp_t *sock, void *data, size_t max_len, uint32_t timeout, sock_udp_ep_t *remote, sock_udp_aux_rx_t *aux)
Receives a UDP message from a remote end point.
int sock_udp_get_local(sock_udp_t *sock, sock_udp_ep_t *ep)
Gets the local end point of a UDP sock object.
int sock_udp_create(sock_udp_t *sock, const sock_udp_ep_t *local, const sock_udp_ep_t *remote, uint16_t flags)
Creates a new UDP sock object.
static bool sock_udp_ep_is_v6(const sock_udp_ep_t *ep)
Checks if the IP address of an endpoint is an IPv6 address.
Definition: udp.h:831
ssize_t sock_udp_sendv_aux(sock_udp_t *sock, const iolist_t *snips, const sock_udp_ep_t *remote, sock_udp_aux_tx_t *aux)
Sends a UDP message to remote end point with non-continuous payload.
static ssize_t sock_udp_recv_buf(sock_udp_t *sock, void **data, void **buf_ctx, uint32_t timeout, sock_udp_ep_t *remote)
Provides stack-internal buffer space containing a UDP message from a remote end point.
Definition: udp.h:626
static ssize_t sock_udp_send_aux(sock_udp_t *sock, const void *data, size_t len, const sock_udp_ep_t *remote, sock_udp_aux_tx_t *aux)
Sends a UDP message to remote end point.
Definition: udp.h:706
int sock_udp_get_remote(sock_udp_t *sock, sock_udp_ep_t *ep)
Gets the remote end point of a UDP sock object.
static ssize_t sock_udp_sendv(sock_udp_t *sock, const iolist_t *snips, const sock_udp_ep_t *remote)
Sends a UDP message to remote end point with non-continuous payload.
Definition: udp.h:792
void sock_udp_close(sock_udp_t *sock)
Closes a UDP sock object.
ssize_t sock_udp_recv_buf_aux(sock_udp_t *sock, void **data, void **buf_ctx, uint32_t timeout, sock_udp_ep_t *remote, sock_udp_aux_rx_t *aux)
Provides stack-internal buffer space containing a UDP message from a remote end point.
static bool sock_udp_ep_is_multicast(const sock_udp_ep_t *ep)
Checks if the IP address of an endpoint is multicast.
Definition: udp.h:806
static ssize_t sock_udp_send(sock_udp_t *sock, const void *data, size_t len, const sock_udp_ep_t *remote)
Sends a UDP message to remote end point.
Definition: udp.h:753
static ssize_t sock_udp_recv(sock_udp_t *sock, void *data, size_t max_len, uint32_t timeout, sock_udp_ep_t *remote)
Receives a UDP message from a remote end point.
Definition: udp.h:529
uint8_t sock_aux_flags_t
Type holding the flags used to request specific auxiliary data.
Definition: sock.h:345
IPv4 address type and helper functions definitions.
Definitions for IPv6 addresses.
Common sock API definitions.
Common IP-based transport layer end point.
Definition: sock.h:211
int family
family of sock_ip_ep_t::addr
Definition: sock.h:217
uint8_t ipv4[4]
IPv4 address mode.
Definition: sock.h:228
union _sock_tl_ep::@390 addr
address
uint8_t ipv6[16]
IPv6 address mode.
Definition: sock.h:226
iolist structure definition
Definition: iolist.h:35
Auxiliary data provided when receiving using an UDP sock object.
Definition: udp.h:309
sock_udp_ep_t local
The local endpoint the datagram was received on.
Definition: udp.h:316
uint8_t ttl
TTL value of the received frame.
Definition: udp.h:340
int16_t rssi
RSSI value of the received frame.
Definition: udp.h:332
uint64_t timestamp
System time the datagram was received.
Definition: udp.h:324
sock_aux_flags_t flags
Flags used request information.
Definition: udp.h:342
Auxiliary data provided when sending using an UDP sock object.
Definition: udp.h:348
uint64_t timestamp
System time the datagram was send.
Definition: udp.h:370
sock_udp_ep_t local
The local endpoint from which the datagram will be sent.
Definition: udp.h:355
sock_aux_flags_t flags
Flags used request information.
Definition: udp.h:372
UDP sock type.
Definition: sock_types.h:128
Data type to represent an IPv4 address.
Definition: addr.h:55
Data type to represent an IPv6 address.
Definition: addr.h:64