2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2015-2016 Attila Molnar <attilamolnar@hush.com>
5 * Copyright (C) 2012-2013, 2017-2019 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
7 * Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
8 * Copyright (C) 2012 Adam <Adam@anope.org>
9 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10 * Copyright (C) 2008, 2010 Craig Edwards <brain@inspircd.org>
11 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
12 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
13 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
15 * This file is part of InspIRCd. InspIRCd is free software: you can
16 * redistribute it and/or modify it under the terms of the GNU General Public
17 * License as published by the Free Software Foundation, version 2.
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include <arpa/inet.h>
35 #include <sys/resource.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
40 #include <netinet/in.h>
47 #include "inspircd_win32wrapper.h"
53 /* Contains irc-specific definitions */
56 /** This namespace contains various protocol-independent helper classes.
57 * It also contains some types which are often used by the core and modules
58 * in place of inet_* functions and types.
62 union CoreExport sockaddrs
65 struct sockaddr_in in4;
66 struct sockaddr_in6 in6;
67 struct sockaddr_un un;
68 /** Return the family of the socket (e.g. AF_INET). */
70 /** Return the size of the structure for syscall passing */
71 socklen_t sa_size() const;
72 /** Return port number or -1 if invalid */
75 std::string addr() const;
76 /** Return human-readable IP/port pair */
77 std::string str() const;
78 bool operator==(const sockaddrs& other) const;
79 inline bool operator!=(const sockaddrs& other) const { return !(*this == other); }
82 struct CoreExport cidr_mask
84 /** Type, AF_INET or AF_INET6 */
86 /** Length of the mask in bits (0-128) */
88 /** Raw bits. Unused bits must be zero */
89 unsigned char bits[16];
92 /** Construct a CIDR mask from the string. Will normalize (127.0.0.1/8 => 127.0.0.0/8). */
93 cidr_mask(const std::string& mask);
94 /** Construct a CIDR mask of a given length from the given address */
95 cidr_mask(const irc::sockets::sockaddrs& addr, unsigned char len);
96 /** Equality of bits, type, and length */
97 bool operator==(const cidr_mask& other) const;
98 /** Ordering defined for maps */
99 bool operator<(const cidr_mask& other) const;
100 /** Match within this CIDR? */
101 bool match(const irc::sockets::sockaddrs& addr) const;
102 /** Human-readable string */
103 std::string str() const;
106 /** Match CIDR, including an optional username/nickname part.
108 * This function will compare a human-readable address (plus
109 * optional username and nickname) against a human-readable
110 * CIDR mask, for example joe!bloggs\@1.2.3.4 against
111 * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
113 * @param address The human readable address, e.g. fred\@1.2.3.4
114 * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
115 * @param match_with_username Does the mask include a nickname segment?
116 * @return True if the mask matches the address
118 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
120 /** Convert an address-port pair into a binary sockaddr
121 * @param addr The IP address, IPv4 or IPv6
122 * @param port The port, 0 for unspecified
123 * @param sa The structure to place the result in. Will be zeroed prior to conversion
124 * @return true if the conversion was successful, false if not.
126 CoreExport bool aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa);
128 /** Convert a UNIX socket path to a binary sockaddr.
129 * @param path The path to the UNIX socket.
130 * @param sa The structure to place the result in. Will be zeroed prior to conversion.
131 * @return True if the conversion was successful; otherwise, false.
133 CoreExport bool untosa(const std::string& path, irc::sockets::sockaddrs& sa);
135 /** Determines whether the specified file is a UNIX socket.
136 * @param file The path to the file to check.
137 * @return True if the file is a UNIX socket; otherwise, false.
139 CoreExport bool isunix(const std::string& file);
143 /** Represents information about a failed port binding. */
144 struct CoreExport FailedPort
147 /** The error which happened during binding. */
150 /** The endpoint on which we were attempting to bind. */
151 irc::sockets::sockaddrs sa;
153 /** The config tag that the listener was created from. */
156 FailedPort(int err, irc::sockets::sockaddrs& ep, ConfigTag* cfg)
164 /** A list of failed port bindings, used for informational purposes on startup */
165 typedef std::vector<FailedPort> FailedPortList;
167 #include "socketengine.h"
169 /** This class handles incoming connections on client ports.
170 * It will create a new User for every valid connection
171 * and assign it a file descriptor.
173 class CoreExport ListenSocket : public EventHandler
176 reference<ConfigTag> bind_tag;
177 const irc::sockets::sockaddrs bind_sa;
179 class IOHookProvRef : public dynamic_reference_nocheck<IOHookProvider>
183 : dynamic_reference_nocheck<IOHookProvider>(NULL, std::string())
188 typedef TR1NS::array<IOHookProvRef, 2> IOHookProvList;
190 /** IOHook providers for handling connections on this socket,
193 IOHookProvList iohookprovs;
195 /** Create a new listening socket
197 ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_to);
202 /** Handles new connections, called by the socket engine
204 void OnEventHandlerRead() CXX11_OVERRIDE;
206 /** Inspects the bind block belonging to this socket to set the name of the IO hook
207 * provider which this socket will use for incoming connections.
209 void ResetIOHookProvider();