Loading...
Searching...
No Matches
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
290extern "C" {
291#endif
292
301typedef struct sock_udp sock_udp_t;
302
303#if defined (__clang__)
304# pragma clang diagnostic pop
305#endif
306
310typedef 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 */
335#if defined(MODULE_SOCK_AUX_TTL) || defined(DOXYGEN)
341 uint8_t ttl;
342#endif /* MODULE_SOCK_AUX_TTL */
345
349typedef struct {
350#if defined(MODULE_SOCK_AUX_LOCAL) || defined(DOXYGEN)
357#endif /* MODULE_SOCK_AUX_ENDPOINT */
358#if defined(MODULE_SOCK_AUX_TIMESTAMP) || defined(DOXYGEN)
371 uint64_t timestamp;
372#endif /* MODULE_SOCK_AUX_TIMESTAP*/
375
426 const sock_udp_ep_t *remote, uint16_t flags);
427
436
449
461
495ssize_t sock_udp_recv_aux(sock_udp_t *sock, void *data, size_t max_len,
496 uint32_t timeout, sock_udp_ep_t *remote,
497 sock_udp_aux_rx_t *aux);
498
530static inline ssize_t sock_udp_recv(sock_udp_t *sock,
531 void *data, size_t max_len,
532 uint32_t timeout, sock_udp_ep_t *remote)
533{
534 return sock_udp_recv_aux(sock, data, max_len, timeout, remote, NULL);
535}
536
581ssize_t sock_udp_recv_buf_aux(sock_udp_t *sock, void **data, void **buf_ctx,
582 uint32_t timeout, sock_udp_ep_t *remote,
583 sock_udp_aux_rx_t *aux);
584
627static inline ssize_t sock_udp_recv_buf(sock_udp_t *sock,
628 void **data, void **buf_ctx,
629 uint32_t timeout,
630 sock_udp_ep_t *remote)
631{
632 return sock_udp_recv_buf_aux(sock, data, buf_ctx, timeout, remote, NULL);
633}
634
669ssize_t sock_udp_sendv_aux(sock_udp_t *sock, const iolist_t *snips,
670 const sock_udp_ep_t *remote, sock_udp_aux_tx_t *aux);
671
707static inline ssize_t sock_udp_send_aux(sock_udp_t *sock,
708 const void *data, size_t len,
709 const sock_udp_ep_t *remote,
711{
712 const iolist_t snip = {
713 NULL,
714 (void *)data,
715 len,
716 };
717
718 return sock_udp_sendv_aux(sock, &snip, remote, aux);
719}
720
754static inline ssize_t sock_udp_send(sock_udp_t *sock,
755 const void *data, size_t len,
756 const sock_udp_ep_t *remote)
757{
758 return sock_udp_send_aux(sock, data, len, remote, NULL);
759}
760
793static inline ssize_t sock_udp_sendv(sock_udp_t *sock,
794 const iolist_t *snips,
795 const sock_udp_ep_t *remote)
796{
797 return sock_udp_sendv_aux(sock, snips, remote, NULL);
798}
799
807static inline bool sock_udp_ep_is_multicast(const sock_udp_ep_t *ep)
808{
809 switch (ep->family) {
810#ifdef SOCK_HAS_IPV6
811 case AF_INET6:
812 return ipv6_addr_is_multicast((const ipv6_addr_t *)&ep->addr.ipv6);
813#endif
814#ifdef SOCK_HAS_IPV4
815 case AF_INET:
816 return ipv4_addr_is_multicast((const ipv4_addr_t *)&ep->addr.ipv4);
817#endif
818 default:
819 assert(0);
820 }
821
822 return false;
823}
824
832static inline bool sock_udp_ep_is_v6(const sock_udp_ep_t *ep)
833{
834#if !defined(SOCK_HAS_IPV6)
835 (void)ep;
836 return false;
837#elif !defined(SOCK_HAS_IPV4)
838 (void)ep;
839 return true;
840#else
841 return ep->family == AF_INET6;
842#endif
843}
844
845#include "sock_types.h"
846
847#ifdef __cplusplus
848}
849#endif
850
851#endif /* NET_SOCK_UDP_H */
Global UNIX address family definitions.
#define AF_INET
internetwork address family: UDP, TCP, etc.
Definition af.h:38
#define AF_INET6
internetwork address family with IPv6: UDP, TCP, etc.
Definition af.h:40
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:137
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: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.
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:832
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:627
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:707
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:793
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:807
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:754
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:530
uint8_t sock_aux_flags_t
Type holding the flags used to request specific auxiliary data.
Definition sock.h:349
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:215
int family
family of sock_ip_ep_t::addr
Definition sock.h:221
uint8_t ipv4[4]
IPv4 address mode.
Definition sock.h:232
union _sock_tl_ep::@386 addr
address
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
uint8_t ttl
TTL value of the received frame.
Definition udp.h:341
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:343
Auxiliary data provided when sending using an UDP sock object.
Definition udp.h:349
uint64_t timestamp
System time the datagram was send.
Definition udp.h:371
sock_udp_ep_t local
The local endpoint from which the datagram will be sent.
Definition udp.h:356
sock_aux_flags_t flags
Flags used request information.
Definition udp.h:373
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:72