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  
/** @file native_udp.hpp
10  
/** @file native_udp.hpp
11  

11  

12  
    Inline UDP protocol type using platform-specific constants.
12  
    Inline UDP protocol type using platform-specific constants.
13  
    All methods are `constexpr` or trivially inlined, giving zero
13  
    All methods are `constexpr` or trivially inlined, giving zero
14  
    overhead compared to hand-written socket creation calls.
14  
    overhead compared to hand-written socket creation calls.
15  

15  

16  
    This header includes platform socket headers
16  
    This header includes platform socket headers
17  
    (`<sys/socket.h>`, `<netinet/in.h>`, etc.).
17  
    (`<sys/socket.h>`, `<netinet/in.h>`, etc.).
18  
    For a version that avoids platform includes, use
18  
    For a version that avoids platform includes, use
19  
    `<boost/corosio/udp.hpp>` (`boost::corosio::udp`).
19  
    `<boost/corosio/udp.hpp>` (`boost::corosio::udp`).
20  

20  

21  
    Both variants satisfy the same protocol-type interface and work
21  
    Both variants satisfy the same protocol-type interface and work
22  
    interchangeably with `udp_socket::open`.
22  
    interchangeably with `udp_socket::open`.
23  

23  

24  
    @see boost::corosio::udp
24  
    @see boost::corosio::udp
25  
*/
25  
*/
26  

26  

27  
#ifndef BOOST_COROSIO_NATIVE_NATIVE_UDP_HPP
27  
#ifndef BOOST_COROSIO_NATIVE_NATIVE_UDP_HPP
28  
#define BOOST_COROSIO_NATIVE_NATIVE_UDP_HPP
28  
#define BOOST_COROSIO_NATIVE_NATIVE_UDP_HPP
29  

29  

30  
#ifdef _WIN32
30  
#ifdef _WIN32
31  
#include <winsock2.h>
31  
#include <winsock2.h>
32  
#include <ws2tcpip.h>
32  
#include <ws2tcpip.h>
33  
#else
33  
#else
34  
#include <netinet/in.h>
34  
#include <netinet/in.h>
35  
#include <sys/socket.h>
35  
#include <sys/socket.h>
36  
#endif
36  
#endif
37  

37  

38  
namespace boost::corosio {
38  
namespace boost::corosio {
39  

39  

40  
class udp_socket;
40  
class udp_socket;
41  

41  

42  
} // namespace boost::corosio
42  
} // namespace boost::corosio
43  

43  

44  
namespace boost::corosio {
44  
namespace boost::corosio {
45  

45  

46  
/** Inline UDP protocol type with platform constants.
46  
/** Inline UDP protocol type with platform constants.
47  

47  

48  
    Same shape as @ref boost::corosio::udp but with inline
48  
    Same shape as @ref boost::corosio::udp but with inline
49  
    `family()`, `type()`, and `protocol()` methods that
49  
    `family()`, `type()`, and `protocol()` methods that
50  
    resolve to compile-time constants.
50  
    resolve to compile-time constants.
51  

51  

52  
    @see boost::corosio::udp
52  
    @see boost::corosio::udp
53  
*/
53  
*/
54  
class native_udp
54  
class native_udp
55  
{
55  
{
56  
    bool v6_;
56  
    bool v6_;
57  
    explicit constexpr native_udp(bool v6) noexcept : v6_(v6) {}
57  
    explicit constexpr native_udp(bool v6) noexcept : v6_(v6) {}
58  

58  

59  
public:
59  
public:
60  
    /// Construct an IPv4 UDP protocol.
60  
    /// Construct an IPv4 UDP protocol.
61  
    static constexpr native_udp v4() noexcept
61  
    static constexpr native_udp v4() noexcept
62  
    {
62  
    {
63  
        return native_udp(false);
63  
        return native_udp(false);
64  
    }
64  
    }
65  

65  

66  
    /// Construct an IPv6 UDP protocol.
66  
    /// Construct an IPv6 UDP protocol.
67  
    static constexpr native_udp v6() noexcept
67  
    static constexpr native_udp v6() noexcept
68  
    {
68  
    {
69  
        return native_udp(true);
69  
        return native_udp(true);
70  
    }
70  
    }
71  

71  

72  
    /// Return true if this is IPv6.
72  
    /// Return true if this is IPv6.
73  
    constexpr bool is_v6() const noexcept
73  
    constexpr bool is_v6() const noexcept
74  
    {
74  
    {
75  
        return v6_;
75  
        return v6_;
76  
    }
76  
    }
77  

77  

78  
    /// Return the address family (AF_INET or AF_INET6).
78  
    /// Return the address family (AF_INET or AF_INET6).
79  
    int family() const noexcept
79  
    int family() const noexcept
80  
    {
80  
    {
81  
        return v6_ ? AF_INET6 : AF_INET;
81  
        return v6_ ? AF_INET6 : AF_INET;
82  
    }
82  
    }
83  

83  

84  
    /// Return the socket type (SOCK_DGRAM).
84  
    /// Return the socket type (SOCK_DGRAM).
85  
    static constexpr int type() noexcept
85  
    static constexpr int type() noexcept
86  
    {
86  
    {
87  
        return SOCK_DGRAM;
87  
        return SOCK_DGRAM;
88  
    }
88  
    }
89  

89  

90  
    /// Return the IP protocol (IPPROTO_UDP).
90  
    /// Return the IP protocol (IPPROTO_UDP).
91  
    static constexpr int protocol() noexcept
91  
    static constexpr int protocol() noexcept
92  
    {
92  
    {
93  
        return IPPROTO_UDP;
93  
        return IPPROTO_UDP;
94  
    }
94  
    }
95  

95  

96  
    /// The associated socket type.
96  
    /// The associated socket type.
97  
    using socket = udp_socket;
97  
    using socket = udp_socket;
98  

98  

99  
    friend constexpr bool operator==(native_udp a, native_udp b) noexcept
99  
    friend constexpr bool operator==(native_udp a, native_udp b) noexcept
100  
    {
100  
    {
101  
        return a.v6_ == b.v6_;
101  
        return a.v6_ == b.v6_;
102  
    }
102  
    }
103  

103  

104  
    friend constexpr bool operator!=(native_udp a, native_udp b) noexcept
104  
    friend constexpr bool operator!=(native_udp a, native_udp b) noexcept
105  
    {
105  
    {
106  
        return a.v6_ != b.v6_;
106  
        return a.v6_ != b.v6_;
107  
    }
107  
    }
108  
};
108  
};
109  

109  

110  
} // namespace boost::corosio
110  
} // namespace boost::corosio
111  

111  

112  
#endif // BOOST_COROSIO_NATIVE_NATIVE_UDP_HPP
112  
#endif // BOOST_COROSIO_NATIVE_NATIVE_UDP_HPP