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 
268 #ifndef NET_SOCK_UDP_H
269 #define NET_SOCK_UDP_H
270 
271 #include <assert.h>
272 #include <errno.h>
273 #include <stdint.h>
274 #include <stdlib.h>
275 #include <sys/types.h>
276 
277 /* net/sock/async/types.h included by net/sock.h needs to re-typedef the
278  * `sock_ip_t` to prevent cyclic includes */
279 #if defined (__clang__)
280 # pragma clang diagnostic push
281 # pragma clang diagnostic ignored "-Wtypedef-redefinition"
282 #endif
283 
284 #include "net/af.h"
285 #include "net/sock.h"
286 #include "net/ipv4/addr.h"
287 #include "net/ipv6/addr.h"
288 
289 #ifdef __cplusplus
290 extern "C" {
291 #endif
292 
293 typedef struct _sock_tl_ep sock_udp_ep_t;
301 typedef struct sock_udp sock_udp_t;
302 
303 #if defined (__clang__)
304 # pragma clang diagnostic pop
305 #endif
306 
310 typedef struct {
311 #if defined(MODULE_SOCK_AUX_LOCAL) || defined(DOXYGEN)
318 #endif /* MODULE_SOCK_AUX_ENDPOINT */
319 #if defined(MODULE_SOCK_AUX_TIMESTAMP) || defined(DOXYGEN)
325  uint64_t timestamp;
326 #endif /* MODULE_SOCK_AUX_TIMESTAP */
327 #if defined(MODULE_SOCK_AUX_RSSI) || defined(DOXYGEN)
333  int16_t rssi;
334 #endif /* MODULE_SOCK_AUX_RSSI */
337 
341 typedef struct {
342 #if defined(MODULE_SOCK_AUX_LOCAL) || defined(DOXYGEN)
349 #endif /* MODULE_SOCK_AUX_ENDPOINT */
350 #if defined(MODULE_SOCK_AUX_TIMESTAMP) || defined(DOXYGEN)
363  uint64_t timestamp;
364 #endif /* MODULE_SOCK_AUX_TIMESTAP*/
367 
417 int sock_udp_create(sock_udp_t *sock, const sock_udp_ep_t *local,
418  const sock_udp_ep_t *remote, uint16_t flags);
419 
428 
441 
453 
487 ssize_t sock_udp_recv_aux(sock_udp_t *sock, void *data, size_t max_len,
488  uint32_t timeout, sock_udp_ep_t *remote,
489  sock_udp_aux_rx_t *aux);
490 
522 static inline ssize_t sock_udp_recv(sock_udp_t *sock,
523  void *data, size_t max_len,
524  uint32_t timeout, sock_udp_ep_t *remote)
525 {
526  return sock_udp_recv_aux(sock, data, max_len, timeout, remote, NULL);
527 }
528 
573 ssize_t sock_udp_recv_buf_aux(sock_udp_t *sock, void **data, void **buf_ctx,
574  uint32_t timeout, sock_udp_ep_t *remote,
575  sock_udp_aux_rx_t *aux);
576 
619 static inline ssize_t sock_udp_recv_buf(sock_udp_t *sock,
620  void **data, void **buf_ctx,
621  uint32_t timeout,
622  sock_udp_ep_t *remote)
623 {
624  return sock_udp_recv_buf_aux(sock, data, buf_ctx, timeout, remote, NULL);
625 }
626 
661 ssize_t sock_udp_sendv_aux(sock_udp_t *sock, const iolist_t *snips,
662  const sock_udp_ep_t *remote, sock_udp_aux_tx_t *aux);
663 
699 static inline ssize_t sock_udp_send_aux(sock_udp_t *sock,
700  const void *data, size_t len,
701  const sock_udp_ep_t *remote,
702  sock_udp_aux_tx_t *aux)
703 {
704  const iolist_t snip = {
705  NULL,
706  (void *)data,
707  len,
708  };
709 
710  return sock_udp_sendv_aux(sock, &snip, remote, aux);
711 }
712 
746 static inline ssize_t sock_udp_send(sock_udp_t *sock,
747  const void *data, size_t len,
748  const sock_udp_ep_t *remote)
749 {
750  return sock_udp_send_aux(sock, data, len, remote, NULL);
751 }
752 
785 static inline ssize_t sock_udp_sendv(sock_udp_t *sock,
786  const iolist_t *snips,
787  const sock_udp_ep_t *remote)
788 {
789  return sock_udp_sendv_aux(sock, snips, remote, NULL);
790 }
791 
799 static inline bool sock_udp_ep_is_multicast(const sock_udp_ep_t *ep)
800 {
801  switch (ep->family) {
802 #ifdef SOCK_HAS_IPV6
803  case AF_INET6:
804  return ipv6_addr_is_multicast((const ipv6_addr_t *)&ep->addr.ipv6);
805 #endif
806 #ifdef SOCK_HAS_IPV4
807  case AF_INET:
808  return ipv4_addr_is_multicast((const ipv4_addr_t *)&ep->addr.ipv4);
809 #endif
810  default:
811  assert(0);
812  }
813 
814  return false;
815 }
816 
817 #include "sock_types.h"
818 
819 #ifdef __cplusplus
820 }
821 #endif
822 
823 #endif /* NET_SOCK_UDP_H */
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:117
@ 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:72
static bool ipv6_addr_is_multicast(const ipv6_addr_t *addr)
Check if addr is a multicast address.
Definition: addr.h:390
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.
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-continous 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:619
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:699
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-continous payload.
Definition: udp.h:785
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:799
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:746
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:522
uint8_t sock_aux_flags_t
Type holding the flags used to request specific auxiliary data.
Definition: sock.h:334
IPv6 address type and helper functions definitions.
Definitions for IPv6 addresses.
Common sock API definitions.
Common IP-based transport layer end point.
Definition: sock.h:215
union _sock_tl_ep::@363 addr
address
int family
family of sock_ip_ep_t::addr
Definition: sock.h:221
uint8_t ipv4[4]
IPv4 address mode.
Definition: sock.h:232
uint8_t ipv6[16]
IPv6 address mode.
Definition: sock.h:230
iolist structure definition
Definition: iolist.h:39
Auxiliary data provided when receiving using an UDP sock object.
Definition: udp.h:310
sock_udp_ep_t local
The local endpoint the datagram was received on.
Definition: udp.h:317
int16_t rssi
RSSI value of the received frame.
Definition: udp.h:333
uint64_t timestamp
System time the datagram was received.
Definition: udp.h:325
sock_aux_flags_t flags
Flags used request information.
Definition: udp.h:335
Auxiliary data provided when sending using an UDP sock object.
Definition: udp.h:341
uint64_t timestamp
System time the datagram was send.
Definition: udp.h:363
sock_udp_ep_t local
The local endpoint from which the datagram will be sent.
Definition: udp.h:348
sock_aux_flags_t flags
Flags used request information.
Definition: udp.h:365
UDP sock type.
Definition: sock_types.h:128
Data type to represent an IPv4 address.
Definition: addr.h:41
Data type to represent an IPv6 address.
Definition: addr.h:72