ServerSocket.hxx 3 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20 21
#ifndef MPD_SERVER_SOCKET_HXX
#define MPD_SERVER_SOCKET_HXX
22

23
#include <list>
24

25
#include <stddef.h>
26 27

struct sockaddr;
28
class EventLoop;
29
class Error;
30
class AllocatedPath;
31 32 33 34 35 36

typedef void (*server_socket_callback_t)(int fd,
					 const struct sockaddr *address,
					 size_t address_length, int uid,
					 void *ctx);

37
class OneServerSocket;
38

39 40 41
/**
 * A socket that accepts incoming stream connections (e.g. TCP).
 */
42 43
class ServerSocket {
	friend class OneServerSocket;
44

45
	EventLoop &loop;
46

47
	std::list<OneServerSocket> sockets;
48

49
	unsigned next_serial;
50

51
public:
52
	ServerSocket(EventLoop &_loop);
53
	~ServerSocket();
54

55 56 57 58
	EventLoop &GetEventLoop() {
		return loop;
	}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
private:
	OneServerSocket &AddAddress(const sockaddr &address, size_t length);

	/**
	 * Add a listener on a port on all IPv4 interfaces.
	 *
	 * @param port the TCP port
	 */
	void AddPortIPv4(unsigned port);

	/**
	 * Add a listener on a port on all IPv6 interfaces.
	 *
	 * @param port the TCP port
	 */
	void AddPortIPv6(unsigned port);

public:
	/**
	 * Add a listener on a port on all interfaces.
	 *
	 * @param port the TCP port
81
	 * @param error_r location to store the error occurring, or nullptr to
82 83 84
	 * ignore errors
	 * @return true on success
	 */
85
	bool AddPort(unsigned port, Error &error);
86 87 88 89 90 91 92

	/**
	 * Resolves a host name, and adds listeners on all addresses in the
	 * result set.
	 *
	 * @param hostname the host name to be resolved
	 * @param port the TCP port
93
	 * @param error_r location to store the error occurring, or nullptr to
94 95 96
	 * ignore errors
	 * @return true on success
	 */
97
	bool AddHost(const char *hostname, unsigned port, Error &error);
98 99 100 101 102

	/**
	 * Add a listener on a Unix domain socket.
	 *
	 * @param path the absolute socket path
103
	 * @param error_r location to store the error occurring, or nullptr to
104 105 106
	 * ignore errors
	 * @return true on success
	 */
107
	bool AddPath(AllocatedPath &&path, Error &error);
108 109 110 111 112 113

	/**
	 * Add a socket descriptor that is accepting connections.  After this
	 * has been called, don't call server_socket_open(), because the
	 * socket is already open.
	 */
114
	bool AddFD(int fd, Error &error);
115

116
	bool Open(Error &error);
117
	void Close();
118 119 120 121

protected:
	virtual void OnAccept(int fd, const sockaddr &address,
			      size_t address_length, int uid) = 0;
122
};
123 124

#endif