1  
//
1  
//
2  
// Copyright (c) 2026 Steve Gerbino
2  
// Copyright (c) 2026 Steve Gerbino
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/cppalliance/corosio
7  
// Official repository: https://github.com/cppalliance/corosio
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_COROSIO_UDP_HPP
10  
#ifndef BOOST_COROSIO_UDP_HPP
11  
#define BOOST_COROSIO_UDP_HPP
11  
#define BOOST_COROSIO_UDP_HPP
12  

12  

13  
#include <boost/corosio/detail/config.hpp>
13  
#include <boost/corosio/detail/config.hpp>
14  

14  

15  
namespace boost::corosio {
15  
namespace boost::corosio {
16  

16  

17  
class udp_socket;
17  
class udp_socket;
18  

18  

19  
/** Encapsulate the UDP protocol for socket creation.
19  
/** Encapsulate the UDP protocol for socket creation.
20  

20  

21  
    This class identifies the UDP protocol and its address family
21  
    This class identifies the UDP protocol and its address family
22  
    (IPv4 or IPv6). It is used to parameterize `udp_socket::open()`
22  
    (IPv4 or IPv6). It is used to parameterize `udp_socket::open()`
23  
    calls with a self-documenting type.
23  
    calls with a self-documenting type.
24  

24  

25  
    The `family()`, `type()`, and `protocol()` members are
25  
    The `family()`, `type()`, and `protocol()` members are
26  
    implemented in the compiled library to avoid exposing
26  
    implemented in the compiled library to avoid exposing
27  
    platform socket headers. For an inline variant that includes
27  
    platform socket headers. For an inline variant that includes
28  
    platform headers, use @ref native_udp.
28  
    platform headers, use @ref native_udp.
29  

29  

30  
    @par Example
30  
    @par Example
31  
    @code
31  
    @code
32  
    udp_socket sock( ioc );
32  
    udp_socket sock( ioc );
33  
    sock.open( udp::v4() );
33  
    sock.open( udp::v4() );
34  
    sock.bind( endpoint( ipv4_address::any(), 9000 ) );
34  
    sock.bind( endpoint( ipv4_address::any(), 9000 ) );
35  
    @endcode
35  
    @endcode
36  

36  

37  
    @see native_udp, udp_socket
37  
    @see native_udp, udp_socket
38  
*/
38  
*/
39  
class BOOST_COROSIO_DECL udp
39  
class BOOST_COROSIO_DECL udp
40  
{
40  
{
41  
    bool v6_;
41  
    bool v6_;
42  
    explicit constexpr udp(bool v6) noexcept : v6_(v6) {}
42  
    explicit constexpr udp(bool v6) noexcept : v6_(v6) {}
43  

43  

44  
public:
44  
public:
45  
    /// Construct an IPv4 UDP protocol.
45  
    /// Construct an IPv4 UDP protocol.
46  
    static constexpr udp v4() noexcept
46  
    static constexpr udp v4() noexcept
47  
    {
47  
    {
48  
        return udp(false);
48  
        return udp(false);
49  
    }
49  
    }
50  

50  

51  
    /// Construct an IPv6 UDP protocol.
51  
    /// Construct an IPv6 UDP protocol.
52  
    static constexpr udp v6() noexcept
52  
    static constexpr udp v6() noexcept
53  
    {
53  
    {
54  
        return udp(true);
54  
        return udp(true);
55  
    }
55  
    }
56  

56  

57  
    /// Return true if this is IPv6.
57  
    /// Return true if this is IPv6.
58  
    constexpr bool is_v6() const noexcept
58  
    constexpr bool is_v6() const noexcept
59  
    {
59  
    {
60  
        return v6_;
60  
        return v6_;
61  
    }
61  
    }
62  

62  

63  
    /// Return the address family (AF_INET or AF_INET6).
63  
    /// Return the address family (AF_INET or AF_INET6).
64  
    int family() const noexcept;
64  
    int family() const noexcept;
65  

65  

66  
    /// Return the socket type (SOCK_DGRAM).
66  
    /// Return the socket type (SOCK_DGRAM).
67  
    static int type() noexcept;
67  
    static int type() noexcept;
68  

68  

69  
    /// Return the IP protocol (IPPROTO_UDP).
69  
    /// Return the IP protocol (IPPROTO_UDP).
70  
    static int protocol() noexcept;
70  
    static int protocol() noexcept;
71  

71  

72  
    /// The associated socket type.
72  
    /// The associated socket type.
73  
    using socket = udp_socket;
73  
    using socket = udp_socket;
74  

74  

75  
    /// Test for equality.
75  
    /// Test for equality.
76  
    friend constexpr bool operator==(udp a, udp b) noexcept
76  
    friend constexpr bool operator==(udp a, udp b) noexcept
77  
    {
77  
    {
78  
        return a.v6_ == b.v6_;
78  
        return a.v6_ == b.v6_;
79  
    }
79  
    }
80  

80  

81  
    /// Test for inequality.
81  
    /// Test for inequality.
82  
    friend constexpr bool operator!=(udp a, udp b) noexcept
82  
    friend constexpr bool operator!=(udp a, udp b) noexcept
83  
    {
83  
    {
84  
        return a.v6_ != b.v6_;
84  
        return a.v6_ != b.v6_;
85  
    }
85  
    }
86  
};
86  
};
87  

87  

88  
} // namespace boost::corosio
88  
} // namespace boost::corosio
89  

89  

90  
#endif // BOOST_COROSIO_UDP_HPP
90  
#endif // BOOST_COROSIO_UDP_HPP