]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/net/socket_defs.hh
import 15.2.0 Octopus source
[ceph.git] / ceph / src / seastar / include / seastar / net / socket_defs.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
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,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright (C) 2016 ScyllaDB.
20 */
21 #pragma once
22
23 #include <iosfwd>
24 #include <array>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include <netinet/ip.h>
28 #include <seastar/net/byteorder.hh>
29 #include <seastar/net/unix_address.hh>
30 #include <cassert>
31
32 namespace seastar {
33
34 namespace net {
35 class inet_address;
36 }
37
38 struct ipv4_addr;
39 struct ipv6_addr;
40
41 class socket_address {
42 public:
43 socklen_t addr_length; ///!< actual size of the relevant 'u' member
44 union {
45 ::sockaddr_storage sas;
46 ::sockaddr sa;
47 ::sockaddr_in in;
48 ::sockaddr_in6 in6;
49 ::sockaddr_un un;
50 } u;
51 socket_address(const sockaddr_in& sa) : addr_length{sizeof(::sockaddr_in)} {
52 u.in = sa;
53 }
54 socket_address(const sockaddr_in6& sa) : addr_length{sizeof(::sockaddr_in6)} {
55 u.in6 = sa;
56 }
57 socket_address(uint16_t);
58 socket_address(ipv4_addr);
59 socket_address(const ipv6_addr&);
60 socket_address(const ipv6_addr&, uint32_t scope);
61 socket_address(const net::inet_address&, uint16_t p = 0);
62 explicit socket_address(const unix_domain_addr&);
63 /** creates an uninitialized socket_address. this can be written into, or used as
64 * "unspecified" for such addresses as bind(addr) or local address in socket::connect
65 * (i.e. system picks)
66 */
67 socket_address();
68
69 ::sockaddr& as_posix_sockaddr() { return u.sa; }
70 ::sockaddr_in& as_posix_sockaddr_in() { return u.in; }
71 ::sockaddr_in6& as_posix_sockaddr_in6() { return u.in6; }
72 const ::sockaddr& as_posix_sockaddr() const { return u.sa; }
73 const ::sockaddr_in& as_posix_sockaddr_in() const { return u.in; }
74 const ::sockaddr_in6& as_posix_sockaddr_in6() const { return u.in6; }
75
76 socket_address(uint32_t, uint16_t p = 0);
77
78 socklen_t length() const { return addr_length; };
79
80 bool is_af_unix() const {
81 return u.sa.sa_family == AF_UNIX;
82 }
83
84 bool is_unspecified() const;
85
86 sa_family_t family() const {
87 return u.sa.sa_family;
88 }
89
90 net::inet_address addr() const;
91 ::in_port_t port() const;
92 bool is_wildcard() const;
93
94 bool operator==(const socket_address&) const;
95 bool operator!=(const socket_address& a) const {
96 return !(*this == a);
97 }
98 };
99
100 std::ostream& operator<<(std::ostream&, const socket_address&);
101
102 enum class transport {
103 TCP = IPPROTO_TCP,
104 SCTP = IPPROTO_SCTP
105 };
106
107 struct ipv4_addr {
108 uint32_t ip;
109 uint16_t port;
110
111 ipv4_addr() : ip(0), port(0) {}
112 ipv4_addr(uint32_t ip, uint16_t port) : ip(ip), port(port) {}
113 ipv4_addr(uint16_t port) : ip(0), port(port) {}
114 ipv4_addr(const std::string &addr);
115 ipv4_addr(const std::string &addr, uint16_t port);
116 ipv4_addr(const net::inet_address&, uint16_t);
117 ipv4_addr(const socket_address &);
118 ipv4_addr(const ::in_addr&, uint16_t = 0);
119
120 bool is_ip_unspecified() const {
121 return ip == 0;
122 }
123 bool is_port_unspecified() const {
124 return port == 0;
125 }
126 };
127
128 struct ipv6_addr {
129 using ipv6_bytes = std::array<uint8_t, 16>;
130
131 ipv6_bytes ip;
132 uint16_t port;
133
134 ipv6_addr(const ipv6_bytes&, uint16_t port = 0);
135 ipv6_addr(uint16_t port = 0);
136 ipv6_addr(const std::string&);
137 ipv6_addr(const std::string&, uint16_t port);
138 ipv6_addr(const net::inet_address&, uint16_t = 0);
139 ipv6_addr(const ::in6_addr&, uint16_t = 0);
140 ipv6_addr(const ::sockaddr_in6&);
141 ipv6_addr(const socket_address&);
142
143 bool is_ip_unspecified() const;
144 bool is_port_unspecified() const {
145 return port == 0;
146 }
147 };
148
149 std::ostream& operator<<(std::ostream&, const ipv4_addr&);
150 std::ostream& operator<<(std::ostream&, const ipv6_addr&);
151
152 inline bool operator==(const ipv4_addr &lhs, const ipv4_addr& rhs) {
153 return lhs.ip == rhs.ip && lhs.port == rhs.port;
154 }
155
156 }
157
158 namespace std {
159 template<>
160 struct hash<seastar::socket_address> {
161 size_t operator()(const seastar::socket_address&) const;
162 };
163 template<>
164 struct hash<seastar::ipv4_addr> {
165 size_t operator()(const seastar::ipv4_addr&) const;
166 };
167 template<>
168 struct hash<seastar::unix_domain_addr> {
169 size_t operator()(const seastar::unix_domain_addr&) const;
170 };
171 template<>
172 struct hash<::sockaddr_un> {
173 size_t operator()(const ::sockaddr_un&) const;
174 };
175
176 template <>
177 struct hash<seastar::transport> {
178 size_t operator()(seastar::transport tr) const {
179 return static_cast<size_t>(tr);
180 }
181 };
182
183 }