udp.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Alexander Aring <aar@pengutronix.de>
3  * Freie Universität Berlin
4  * HAW Hamburg
5  * Kaspar Schleiser <kaspar@schleiser.de>
6  *
7  * This file is subject to the terms and conditions of the GNU Lesser
8  * General Public License v2.1. See the file LICENSE in the top level
9  * directory for more details.
10  */
11 
12 #pragma once
13 
273 #include <assert.h>
274 #include <errno.h>
275 #include <stdint.h>
276 #include <stdlib.h>
277 #include <sys/types.h>
278 
279 /* net/sock/async/types.h included by net/sock.h needs to re-typedef the
280  * `sock_ip_t` to prevent cyclic includes */
281 #if defined (__clang__)
282 # pragma clang diagnostic push
283 # pragma clang diagnostic ignored "-Wtypedef-redefinition"
284 #endif
285 
286 #include "net/af.h"
287 #include "net/sock.h"
288 #include "net/ipv4/addr.h"
289 #include "net/ipv6/addr.h"
290 
291 #ifdef __cplusplus
292 extern "C" {
293 #endif
294 
295 typedef struct _sock_tl_ep sock_udp_ep_t;
303 typedef struct sock_udp sock_udp_t;
304 
305 #if defined (__clang__)
306 # pragma clang diagnostic pop
307 #endif
308 
312 typedef struct {
313 #if defined(MODULE_SOCK_AUX_LOCAL) || defined(DOXYGEN)
320 #endif /* MODULE_SOCK_AUX_ENDPOINT */
321 #if defined(MODULE_SOCK_AUX_TIMESTAMP) || defined(DOXYGEN)
327  uint64_t timestamp;
328 #endif /* MODULE_SOCK_AUX_TIMESTAP */
329 #if defined(MODULE_SOCK_AUX_RSSI) || defined(DOXYGEN)
335  int16_t rssi;
336 #endif /* MODULE_SOCK_AUX_RSSI */
337 #if defined(MODULE_SOCK_AUX_TTL) || defined(DOXYGEN)
343  uint8_t ttl;
344 #endif /* MODULE_SOCK_AUX_TTL */
347 
351 typedef struct {
352 #if defined(MODULE_SOCK_AUX_LOCAL) || defined(DOXYGEN)
359 #endif /* MODULE_SOCK_AUX_ENDPOINT */
360 #if defined(MODULE_SOCK_AUX_TIMESTAMP) || defined(DOXYGEN)
373  uint64_t timestamp;
374 #endif /* MODULE_SOCK_AUX_TIMESTAP*/
377 
427 int sock_udp_create(sock_udp_t *sock, const sock_udp_ep_t *local,
428  const sock_udp_ep_t *remote, uint16_t flags);
429 
438 
451 
463 
497 ssize_t sock_udp_recv_aux(sock_udp_t *sock, void *data, size_t max_len,
498  uint32_t timeout, sock_udp_ep_t *remote,
499  sock_udp_aux_rx_t *aux);
500 
532 static inline ssize_t sock_udp_recv(sock_udp_t *sock,
533  void *data, size_t max_len,
534  uint32_t timeout, sock_udp_ep_t *remote)
535 {
536  return sock_udp_recv_aux(sock, data, max_len, timeout, remote, NULL);
537 }
538 
583 ssize_t sock_udp_recv_buf_aux(sock_udp_t *sock, void **data, void **buf_ctx,
584  uint32_t timeout, sock_udp_ep_t *remote,
585  sock_udp_aux_rx_t *aux);
586 
629 static inline ssize_t sock_udp_recv_buf(sock_udp_t *sock,
630  void **data, void **buf_ctx,
631  uint32_t timeout,
632  sock_udp_ep_t *remote)
633 {
634  return sock_udp_recv_buf_aux(sock, data, buf_ctx, timeout, remote, NULL);
635 }
636 
671 ssize_t sock_udp_sendv_aux(sock_udp_t *sock, const iolist_t *snips,
672  const sock_udp_ep_t *remote, sock_udp_aux_tx_t *aux);
673 
709 static inline ssize_t sock_udp_send_aux(sock_udp_t *sock,
710  const void *data, size_t len,
711  const sock_udp_ep_t *remote,
712  sock_udp_aux_tx_t *aux)
713 {
714  const iolist_t snip = {
715  NULL,
716  (void *)data,
717  len,
718  };
719 
720  return sock_udp_sendv_aux(sock, &snip, remote, aux);
721 }
722 
756 static inline ssize_t sock_udp_send(sock_udp_t *sock,
757  const void *data, size_t len,
758  const sock_udp_ep_t *remote)
759 {
760  return sock_udp_send_aux(sock, data, len, remote, NULL);
761 }
762 
795 static inline ssize_t sock_udp_sendv(sock_udp_t *sock,
796  const iolist_t *snips,
797  const sock_udp_ep_t *remote)
798 {
799  return sock_udp_sendv_aux(sock, snips, remote, NULL);
800 }
801 
809 static inline bool sock_udp_ep_is_multicast(const sock_udp_ep_t *ep)
810 {
811  switch (ep->family) {
812 #ifdef SOCK_HAS_IPV6
813  case AF_INET6:
814  return ipv6_addr_is_multicast((const ipv6_addr_t *)&ep->addr.ipv6);
815 #endif
816 #ifdef SOCK_HAS_IPV4
817  case AF_INET:
818  return ipv4_addr_is_multicast((const ipv4_addr_t *)&ep->addr.ipv4);
819 #endif
820  default:
821  assert(0);
822  }
823 
824  return false;
825 }
826 
834 static inline bool sock_udp_ep_is_v6(const sock_udp_ep_t *ep)
835 {
836 #if !defined(SOCK_HAS_IPV6)
837  (void)ep;
838  return false;
839 #elif !defined(SOCK_HAS_IPV4)
840  (void)ep;
841  return true;
842 #else
843  return ep->family == AF_INET6;
844 #endif
845 }
846 
847 #include "sock_types.h"
848 
849 #ifdef __cplusplus
850 }
851 #endif
852 
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:146
@ AF_INET6
internetwork address family with IPv6: UDP, TCP, etc.
Definition: af.h:39
@ AF_INET
internetwork address family: UDP, TCP, etc.
Definition: af.h:37
static bool ipv4_addr_is_multicast(const ipv4_addr_t *addr)
Check if addr is a multicast address.
Definition: addr.h:84
static bool ipv6_addr_is_multicast(const ipv6_addr_t *addr)
Check if addr is a multicast address.
Definition: addr.h:345
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:834
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:629
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:709
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:795
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:809
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:756
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:532
uint8_t sock_aux_flags_t
Type holding the flags used to request specific auxiliary data.
Definition: sock.h:348
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:214
int family
family of sock_ip_ep_t::addr
Definition: sock.h:220
union _sock_tl_ep::@388 addr
address
uint8_t ipv4[4]
IPv4 address mode.
Definition: sock.h:231
uint8_t ipv6[16]
IPv6 address mode.
Definition: sock.h:229
iolist structure definition
Definition: iolist.h:38
Auxiliary data provided when receiving using an UDP sock object.
Definition: udp.h:312
sock_udp_ep_t local
The local endpoint the datagram was received on.
Definition: udp.h:319
uint8_t ttl
TTL value of the received frame.
Definition: udp.h:343
int16_t rssi
RSSI value of the received frame.
Definition: udp.h:335
uint64_t timestamp
System time the datagram was received.
Definition: udp.h:327
sock_aux_flags_t flags
Flags used request information.
Definition: udp.h:345
Auxiliary data provided when sending using an UDP sock object.
Definition: udp.h:351
uint64_t timestamp
System time the datagram was send.
Definition: udp.h:373
sock_udp_ep_t local
The local endpoint from which the datagram will be sent.
Definition: udp.h:358
sock_aux_flags_t flags
Flags used request information.
Definition: udp.h:375
UDP sock type.
Definition: sock_types.h:128
Data type to represent an IPv4 address.
Definition: addr.h:53
Data type to represent an IPv6 address.
Definition: addr.h:67