]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socket.h
Use IsCTCP in blockcolor for ignoring CTCPs.
[user/henk/code/inspircd.git] / include / socket.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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 Pippijn van Steenhoven <pip88nl@gmail.com>
11  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
12  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
13  *   Copyright (C) 2006, 2008, 2010 Craig Edwards <brain@inspircd.org>
14  *
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.
18  *
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
22  * details.
23  *
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/>.
26  */
27
28
29 #pragma once
30
31 #ifndef _WIN32
32
33 #include <arpa/inet.h>
34 #include <sys/time.h>
35 #include <sys/resource.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/stat.h>
39 #include <sys/un.h>
40 #include <netinet/in.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <netdb.h>
44
45 #else
46
47 #include "inspircd_win32wrapper.h"
48
49 #endif
50
51 #include <cerrno>
52
53 /* Contains irc-specific definitions */
54 namespace irc
55 {
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.
59          */
60         namespace sockets
61         {
62                 union CoreExport sockaddrs
63                 {
64                         struct sockaddr sa;
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). */
69                         int family() const;
70                         /** Return the size of the structure for syscall passing */
71                         socklen_t sa_size() const;
72                         /** Return port number or -1 if invalid */
73                         int port() const;
74                         /** Return IP only */
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); }
80                 };
81
82                 struct CoreExport cidr_mask
83                 {
84                         /** Type, AF_INET or AF_INET6 */
85                         unsigned char type;
86                         /** Length of the mask in bits (0-128) */
87                         unsigned char length;
88                         /** Raw bits. Unused bits must be zero */
89                         unsigned char bits[16];
90
91                         cidr_mask() {}
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;
104                 };
105
106                 /** Match CIDR, including an optional username/nickname part.
107                  *
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
112                  * IPV6 addresses.
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
117                  */
118                 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
119
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.
125                  */
126                 CoreExport bool aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa);
127
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.
132                  */
133                 CoreExport bool untosa(const std::string& path, irc::sockets::sockaddrs& sa);
134
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.
138                  */
139                 CoreExport bool isunix(const std::string& file);
140         }
141 }
142
143 /** Represents information about a failed port binding. */
144 struct CoreExport FailedPort
145 {
146         /** The error which happened during binding. */
147         int error;
148
149         /** The endpoint on which we were attempting to bind. */
150         irc::sockets::sockaddrs sa;
151
152         /** The config tag that the listener was created from. */
153         ConfigTag* tag;
154
155         FailedPort(int err, irc::sockets::sockaddrs& ep, ConfigTag* cfg)
156                 : error(err)
157                 , sa(ep)
158                 , tag(cfg)
159         {
160         }
161 };
162
163 /** A list of failed port bindings, used for informational purposes on startup */
164 typedef std::vector<FailedPort> FailedPortList;
165
166 #include "socketengine.h"
167
168 /** This class handles incoming connections on client ports.
169  * It will create a new User for every valid connection
170  * and assign it a file descriptor.
171  */
172 class CoreExport ListenSocket : public EventHandler
173 {
174  public:
175         reference<ConfigTag> bind_tag;
176         const irc::sockets::sockaddrs bind_sa;
177
178         class IOHookProvRef : public dynamic_reference_nocheck<IOHookProvider>
179         {
180          public:
181                 IOHookProvRef()
182                         : dynamic_reference_nocheck<IOHookProvider>(NULL, std::string())
183                 {
184                 }
185         };
186
187         typedef TR1NS::array<IOHookProvRef, 2> IOHookProvList;
188
189         /** IOHook providers for handling connections on this socket,
190          * may be empty.
191          */
192         IOHookProvList iohookprovs;
193
194         /** Create a new listening socket
195          */
196         ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_to);
197         /** Close the socket
198          */
199         ~ListenSocket();
200
201         /** Handles new connections, called by the socket engine
202          */
203         void OnEventHandlerRead() CXX11_OVERRIDE;
204
205         /** Inspects the bind block belonging to this socket to set the name of the IO hook
206          * provider which this socket will use for incoming connections.
207          */
208         void ResetIOHookProvider();
209 };