Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Protocol.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_NET_PROTOCOL_HPP
18#define TOOLBOX_NET_PROTOCOL_HPP
19
20#include <netinet/in.h>
21
22namespace toolbox {
23inline namespace net {
24
27 {
28 return lhs.family_ == rhs.family_ && lhs.protocol_ == rhs.protocol_;
29 }
30 friend bool operator!=(DgramProtocol lhs, DgramProtocol rhs) { return !(lhs == rhs); }
31
32 constexpr explicit DgramProtocol(int family = AF_UNSPEC, int protocol = 0) noexcept
33 : family_{family}
34 , protocol_{protocol}
35 {
36 }
37 static constexpr auto ip4() noexcept { return DgramProtocol{AF_INET, 0}; }
38 static constexpr auto ip6() noexcept { return DgramProtocol{AF_INET6, 0}; }
39 static constexpr auto udp4() noexcept { return DgramProtocol{AF_INET, IPPROTO_UDP}; }
40 static constexpr auto udp6() noexcept { return DgramProtocol{AF_INET6, IPPROTO_UDP}; }
41 static constexpr auto unix() noexcept { return DgramProtocol{AF_UNIX, 0}; }
42 static constexpr auto unspec() noexcept { return DgramProtocol{AF_UNSPEC, 0}; }
43
44 constexpr int family() const noexcept { return family_; }
45 constexpr int type() const noexcept { return SOCK_DGRAM; }
46 constexpr int protocol() const noexcept { return protocol_; }
47
48 private:
49 int family_, protocol_;
50};
51
54 {
55 return lhs.family_ == rhs.family_ && lhs.protocol_ == rhs.protocol_;
56 }
57 friend bool operator!=(StreamProtocol lhs, StreamProtocol rhs) { return !(lhs == rhs); }
58
59 constexpr explicit StreamProtocol(int family = AF_UNSPEC, int protocol = 0) noexcept
60 : family_{family}
61 , protocol_{protocol}
62 {
63 }
64 static constexpr auto ip4() noexcept { return StreamProtocol{AF_INET, 0}; }
65 static constexpr auto ip6() noexcept { return StreamProtocol{AF_INET6, 0}; }
66 static constexpr auto tcp4() noexcept { return StreamProtocol{AF_INET, IPPROTO_TCP}; }
67 static constexpr auto tcp6() noexcept { return StreamProtocol{AF_INET6, IPPROTO_TCP}; }
68 static constexpr auto unix() noexcept { return StreamProtocol{AF_UNIX, 0}; }
69 static constexpr auto unspec() noexcept { return StreamProtocol{AF_UNSPEC, 0}; }
70
71 constexpr int family() const noexcept { return family_; }
72 constexpr int type() const noexcept { return SOCK_STREAM; }
73 constexpr int protocol() const noexcept { return protocol_; }
74
75 private:
76 int family_, protocol_;
77};
78
80 friend bool operator==(UdpProtocol lhs, UdpProtocol rhs) { return lhs.family_ == rhs.family_; }
81 friend bool operator!=(UdpProtocol lhs, UdpProtocol rhs) { return lhs.family_ != rhs.family_; }
82
83 static constexpr auto v4() noexcept { return UdpProtocol{AF_INET}; }
84 static constexpr auto v6() noexcept { return UdpProtocol{AF_INET6}; }
85
86 constexpr int family() const noexcept { return family_; }
87 constexpr int type() const noexcept { return SOCK_DGRAM; }
88 constexpr int protocol() const noexcept { return IPPROTO_UDP; }
89
90 private:
91 constexpr explicit UdpProtocol(int family) noexcept
92 : family_{family}
93 {
94 }
95 int family_;
96};
97
99 friend bool operator==(TcpProtocol lhs, TcpProtocol rhs) { return lhs.family_ == rhs.family_; }
100 friend bool operator!=(TcpProtocol lhs, TcpProtocol rhs) { return lhs.family_ != rhs.family_; }
101
102 static constexpr auto v4() noexcept { return TcpProtocol{AF_INET}; }
103 static constexpr auto v6() noexcept { return TcpProtocol{AF_INET6}; }
104
105 constexpr int family() const noexcept { return family_; }
106 constexpr int type() const noexcept { return SOCK_STREAM; }
107 constexpr int protocol() const noexcept { return IPPROTO_TCP; }
108
109 private:
110 constexpr explicit TcpProtocol(int family) noexcept
111 : family_{family}
112 {
113 }
114 int family_;
115};
116
118 constexpr int family() const noexcept { return AF_UNIX; }
119 constexpr int type() const noexcept { return SOCK_DGRAM; }
120 constexpr int protocol() const noexcept { return 0; }
121};
122
124 constexpr int family() const noexcept { return AF_UNIX; }
125 constexpr int type() const noexcept { return SOCK_STREAM; }
126 constexpr int protocol() const noexcept { return 0; }
127};
128
129} // namespace net
130} // namespace toolbox
131
132#endif // TOOLBOX_NET_PROTOCOL_HPP
constexpr auto bind() noexcept
Definition Slot.hpp:92
constexpr int protocol() const noexcept
Definition Protocol.hpp:46
friend bool operator!=(DgramProtocol lhs, DgramProtocol rhs)
Definition Protocol.hpp:30
constexpr int family() const noexcept
Definition Protocol.hpp:44
constexpr DgramProtocol(int family=AF_UNSPEC, int protocol=0) noexcept
Definition Protocol.hpp:32
static constexpr auto udp6() noexcept
Definition Protocol.hpp:40
static constexpr auto ip6() noexcept
Definition Protocol.hpp:38
constexpr int type() const noexcept
Definition Protocol.hpp:45
static constexpr auto ip4() noexcept
Definition Protocol.hpp:37
friend bool operator==(DgramProtocol lhs, DgramProtocol rhs)
Definition Protocol.hpp:26
static constexpr auto udp4() noexcept
Definition Protocol.hpp:39
static constexpr auto unix() noexcept
Definition Protocol.hpp:41
static constexpr auto unspec() noexcept
Definition Protocol.hpp:42
static constexpr auto tcp4() noexcept
Definition Protocol.hpp:66
static constexpr auto tcp6() noexcept
Definition Protocol.hpp:67
static constexpr auto ip6() noexcept
Definition Protocol.hpp:65
constexpr StreamProtocol(int family=AF_UNSPEC, int protocol=0) noexcept
Definition Protocol.hpp:59
static constexpr auto unspec() noexcept
Definition Protocol.hpp:69
constexpr int protocol() const noexcept
Definition Protocol.hpp:73
friend bool operator==(StreamProtocol lhs, StreamProtocol rhs)
Definition Protocol.hpp:53
friend bool operator!=(StreamProtocol lhs, StreamProtocol rhs)
Definition Protocol.hpp:57
static constexpr auto unix() noexcept
Definition Protocol.hpp:68
constexpr int family() const noexcept
Definition Protocol.hpp:71
static constexpr auto ip4() noexcept
Definition Protocol.hpp:64
constexpr int type() const noexcept
Definition Protocol.hpp:72
static constexpr auto v4() noexcept
Definition Protocol.hpp:102
constexpr int type() const noexcept
Definition Protocol.hpp:106
constexpr int protocol() const noexcept
Definition Protocol.hpp:107
friend bool operator!=(TcpProtocol lhs, TcpProtocol rhs)
Definition Protocol.hpp:100
constexpr int family() const noexcept
Definition Protocol.hpp:105
friend bool operator==(TcpProtocol lhs, TcpProtocol rhs)
Definition Protocol.hpp:99
static constexpr auto v6() noexcept
Definition Protocol.hpp:103
friend bool operator!=(UdpProtocol lhs, UdpProtocol rhs)
Definition Protocol.hpp:81
constexpr int protocol() const noexcept
Definition Protocol.hpp:88
static constexpr auto v6() noexcept
Definition Protocol.hpp:84
constexpr int family() const noexcept
Definition Protocol.hpp:86
friend bool operator==(UdpProtocol lhs, UdpProtocol rhs)
Definition Protocol.hpp:80
constexpr int type() const noexcept
Definition Protocol.hpp:87
static constexpr auto v4() noexcept
Definition Protocol.hpp:83
constexpr int family() const noexcept
Definition Protocol.hpp:118
constexpr int protocol() const noexcept
Definition Protocol.hpp:120
constexpr int type() const noexcept
Definition Protocol.hpp:119
constexpr int protocol() const noexcept
Definition Protocol.hpp:126
constexpr int family() const noexcept
Definition Protocol.hpp:124
constexpr int type() const noexcept
Definition Protocol.hpp:125