SocketDescriptor.hxx 7.32 KB
Newer Older
1
/*
2
 * Copyright 2012-2019 Max Kellermann <max.kellermann@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef SOCKET_DESCRIPTOR_HXX
#define SOCKET_DESCRIPTOR_HXX

33
#include "Features.hxx"
34 35 36 37 38 39
#include "system/FileDescriptor.hxx"

#include <type_traits>

class SocketAddress;
class StaticSocketAddress;
40 41
class IPv4Address;
class IPv6Address;
42 43 44 45 46 47

/**
 * An OO wrapper for a UNIX socket descriptor.
 */
class SocketDescriptor : protected FileDescriptor {
protected:
48
	explicit constexpr SocketDescriptor(FileDescriptor _fd) noexcept
49 50 51 52 53
		:FileDescriptor(_fd) {}

public:
	SocketDescriptor() = default;

54
	explicit constexpr SocketDescriptor(int _fd) noexcept
55 56
		:FileDescriptor(_fd) {}

57
	constexpr bool operator==(SocketDescriptor other) const noexcept {
58 59 60 61 62 63 64 65 66 67
		return fd == other.fd;
	}

#ifndef _WIN32
	/**
	 * Convert a #FileDescriptor to a #SocketDescriptor.  This is only
	 * possible on operating systems where socket descriptors are the
	 * same as file descriptors (i.e. not on Windows).  Use this only
	 * when you know what you're doing.
	 */
68
	static constexpr SocketDescriptor FromFileDescriptor(FileDescriptor fd) noexcept {
69 70 71 72 73 74 75 76 77
		return SocketDescriptor(fd);
	}

	/**
	 * Convert this object to a #FileDescriptor instance.  This is only
	 * possible on operating systems where socket descriptors are the
	 * same as file descriptors (i.e. not on Windows).  Use this only
	 * when you know what you're doing.
	 */
78
	constexpr const FileDescriptor &ToFileDescriptor() const noexcept {
79 80 81 82 83
		return *this;
	}
#endif

	using FileDescriptor::IsDefined;
84
#ifndef _WIN32
85
	using FileDescriptor::IsValid;
86
	using FileDescriptor::IsSocket;
87
#endif
88 89 90 91 92 93 94 95 96 97 98 99 100 101

	/**
	 * Determine the socket type, i.e. SOCK_STREAM, SOCK_DGRAM or
	 * SOCK_SEQPACKET.  Returns -1 on error.
	 */
	gcc_pure
	int GetType() const noexcept;

	/**
	 * Is this a stream socket?
	 */
	gcc_pure
	bool IsStream() const noexcept;

102 103 104 105 106
	using FileDescriptor::Get;
	using FileDescriptor::Set;
	using FileDescriptor::Steal;
	using FileDescriptor::SetUndefined;

107
	static constexpr SocketDescriptor Undefined() noexcept {
108 109 110
		return SocketDescriptor(FileDescriptor::Undefined());
	}

111 112 113
	using FileDescriptor::EnableCloseOnExec;
	using FileDescriptor::DisableCloseOnExec;

114 115 116 117
#ifndef _WIN32
	using FileDescriptor::SetNonBlocking;
	using FileDescriptor::SetBlocking;
	using FileDescriptor::Duplicate;
118
	using FileDescriptor::CheckDuplicate;
119 120
	using FileDescriptor::Close;
#else
121 122
	bool SetNonBlocking() noexcept;

123 124 125 126 127 128
	/**
	 * This method replaces FileDescriptor::Close(), using closesocket()
	 * on Windows.  FileDescriptor::Close() is not virtual, so be
	 * careful when dealing with a FileDescriptor reference that is
	 * really a SocketDescriptor.
	 */
129
	void Close() noexcept;
130 131 132 133 134 135 136 137 138 139 140
#endif

	/**
	 * Create a socket.
	 *
	 * @param domain is the address domain
	 * @param type is the sochet type
	 * @param protocol is the protocol
	 * @return True on success, False on failure
	 * See man 2 socket for detailed information
	 */
141
	bool Create(int domain, int type, int protocol) noexcept;
142 143 144 145

	/**
	 * Like Create(), but enable non-blocking mode.
	 */
146
	bool CreateNonBlock(int domain, int type, int protocol) noexcept;
147

148
#ifndef _WIN32
149
	static bool CreateSocketPair(int domain, int type, int protocol,
150 151
				     SocketDescriptor &a,
				     SocketDescriptor &b) noexcept;
152
	static bool CreateSocketPairNonBlock(int domain, int type, int protocol,
153 154
					     SocketDescriptor &a,
					     SocketDescriptor &b) noexcept;
155
#endif
156

157
	int GetError() noexcept;
158

159 160 161
	/**
	 * @return the value size or 0 on error
	 */
162 163
	size_t GetOption(int level, int name,
			 void *value, size_t size) const noexcept;
164 165 166 167 168 169 170 171 172 173

#ifdef HAVE_STRUCT_UCRED
	/**
	 * Receive peer credentials (SO_PEERCRED).  On error, the pid
	 * is -1.
	 */
	gcc_pure
	struct ucred GetPeerCredentials() const noexcept;
#endif

174 175
	bool SetOption(int level, int name,
		       const void *value, size_t size) noexcept;
176

177
	bool SetBoolOption(int level, int name, bool _value) noexcept {
178 179 180 181
		const int value = _value;
		return SetOption(level, name, &value, sizeof(value));
	}

182 183
	bool SetKeepAlive(bool value=true) noexcept;
	bool SetReuseAddress(bool value=true) noexcept;
184 185

#ifdef __linux__
186 187 188 189
	bool SetReusePort(bool value=true) noexcept;
	bool SetFreeBind(bool value=true) noexcept;
	bool SetNoDelay(bool value=true) noexcept;
	bool SetCork(bool value=true) noexcept;
190

191
	bool SetTcpDeferAccept(const int &seconds) noexcept;
192 193 194 195 196 197

	/**
	 * Setter for TCP_USER_TIMEOUT.
	 */
	bool SetTcpUserTimeout(const unsigned &milliseconds) noexcept;

198
	bool SetV6Only(bool value) noexcept;
199 200 201 202

	/**
	 * Setter for SO_BINDTODEVICE.
	 */
203
	bool SetBindToDevice(const char *name) noexcept;
204

205
	bool SetTcpFastOpen(int qlen=16) noexcept;
206 207 208 209

	bool AddMembership(const IPv4Address &address) noexcept;
	bool AddMembership(const IPv6Address &address) noexcept;
	bool AddMembership(SocketAddress address) noexcept;
210 211
#endif

212
	bool Bind(SocketAddress address) noexcept;
213 214 215 216 217

#ifdef __linux__
	/**
	 * Binds the socket to a unique abstract address.
	 */
218
	bool AutoBind() noexcept;
219 220
#endif

221
	bool Listen(int backlog) noexcept;
222

223 224 225
	SocketDescriptor Accept() noexcept;
	SocketDescriptor AcceptNonBlock() const noexcept;
	SocketDescriptor AcceptNonBlock(StaticSocketAddress &address) const noexcept;
226

227
	bool Connect(SocketAddress address) noexcept;
228 229

	gcc_pure
230
	StaticSocketAddress GetLocalAddress() const noexcept;
231 232

	gcc_pure
233
	StaticSocketAddress GetPeerAddress() const noexcept;
234

235 236
	ssize_t Read(void *buffer, size_t length) noexcept;
	ssize_t Write(const void *buffer, size_t length) noexcept;
237 238

#ifdef _WIN32
239 240
	int WaitReadable(int timeout_ms) const noexcept;
	int WaitWritable(int timeout_ms) const noexcept;
241 242 243 244 245 246 247 248 249 250
#else
	using FileDescriptor::WaitReadable;
	using FileDescriptor::WaitWritable;
	using FileDescriptor::IsReadyForWriting;
#endif

	/**
	 * Receive a datagram and return the source address.
	 */
	ssize_t Read(void *buffer, size_t length,
251
		     StaticSocketAddress &address) noexcept;
252 253 254 255 256

	/**
	 * Send a datagram to the specified address.
	 */
	ssize_t Write(const void *buffer, size_t length,
257
		      SocketAddress address) noexcept;
258 259 260 261 262 263

#ifndef _WIN32
	void Shutdown() noexcept;
	void ShutdownRead() noexcept;
	void ShutdownWrite() noexcept;
#endif
264 265 266 267 268
};

static_assert(std::is_trivial<SocketDescriptor>::value, "type is not trivial");

#endif