]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socket.h
Add an oper only parameter to Simple{Channel,User}ModeHandler.
[user/henk/code/inspircd.git] / include / socket.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2005-2007 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
8  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
9  *   Copyright (C) 2006 William Pitcock <nenolod@dereferenced.org>
10  *
11  * This file is part of InspIRCd.  InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24
25 #pragma once
26
27 #ifndef _WIN32
28
29 #include <arpa/inet.h>
30 #include <sys/time.h>
31 #include <sys/resource.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <sys/un.h>
36 #include <netinet/in.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <netdb.h>
40
41 #else
42
43 #include "inspircd_win32wrapper.h"
44
45 #endif
46
47 #include <cerrno>
48
49 /* Contains irc-specific definitions */
50 namespace irc
51 {
52         /** This namespace contains various protocol-independent helper classes.
53          * It also contains some types which are often used by the core and modules
54          * in place of inet_* functions and types.
55          */
56         namespace sockets
57         {
58                 union CoreExport sockaddrs
59                 {
60                         struct sockaddr sa;
61                         struct sockaddr_in in4;
62                         struct sockaddr_in6 in6;
63                         struct sockaddr_un un;
64                         /** Return the family of the socket (e.g. AF_INET). */
65                         int family() const;
66                         /** Return the size of the structure for syscall passing */
67                         socklen_t sa_size() const;
68                         /** Return port number or -1 if invalid */
69                         int port() const;
70                         /** Return IP only */
71                         std::string addr() const;
72                         /** Return human-readable IP/port pair */
73                         std::string str() const;
74                         bool operator==(const sockaddrs& other) const;
75                         inline bool operator!=(const sockaddrs& other) const { return !(*this == other); }
76                 };
77
78                 struct CoreExport cidr_mask
79                 {
80                         /** Type, AF_INET or AF_INET6 */
81                         unsigned char type;
82                         /** Length of the mask in bits (0-128) */
83                         unsigned char length;
84                         /** Raw bits. Unused bits must be zero */
85                         unsigned char bits[16];
86
87                         cidr_mask() {}
88                         /** Construct a CIDR mask from the string. Will normalize (127.0.0.1/8 => 127.0.0.0/8). */
89                         cidr_mask(const std::string& mask);
90                         /** Construct a CIDR mask of a given length from the given address */
91                         cidr_mask(const irc::sockets::sockaddrs& addr, unsigned char len);
92                         /** Equality of bits, type, and length */
93                         bool operator==(const cidr_mask& other) const;
94                         /** Ordering defined for maps */
95                         bool operator<(const cidr_mask& other) const;
96                         /** Match within this CIDR? */
97                         bool match(const irc::sockets::sockaddrs& addr) const;
98                         /** Human-readable string */
99                         std::string str() const;
100                 };
101
102                 /** Match CIDR, including an optional username/nickname part.
103                  *
104                  * This function will compare a human-readable address (plus
105                  * optional username and nickname) against a human-readable
106                  * CIDR mask, for example joe!bloggs\@1.2.3.4 against
107                  * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
108                  * IPV6 addresses.
109                  * @param address The human readable address, e.g. fred\@1.2.3.4
110                  * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
111                  * @param match_with_username Does the  mask include a nickname segment?
112                  * @return True if the mask matches the address
113                  */
114                 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
115
116                 /** Convert an address-port pair into a binary sockaddr
117                  * @param addr The IP address, IPv4 or IPv6
118                  * @param port The port, 0 for unspecified
119                  * @param sa The structure to place the result in. Will be zeroed prior to conversion
120                  * @return true if the conversion was successful, false if not.
121                  */
122                 CoreExport bool aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa);
123
124                 /** Convert a UNIX socket path to a binary sockaddr.
125                  * @param path The path to the UNIX socket.
126                  * @param sa The structure to place the result in. Will be zeroed prior to conversion.
127                  * @return True if the conversion was successful; otherwise, false.
128                  */
129                 CoreExport bool untosa(const std::string& path, irc::sockets::sockaddrs& sa);
130
131                 /** Determines whether the specified file is a UNIX socket.
132                  * @param file The path to the file to check.
133                  * @return True if the file is a UNIX socket; otherwise, false.
134                  */
135                 CoreExport bool isunix(const std::string& file);
136         }
137 }
138
139 /** Represents information about a failed port binding. */
140 struct CoreExport FailedPort
141 {
142         
143         /** The error which happened during binding. */
144         int error;
145
146         /** The endpoint on which we were attempting to bind. */
147         irc::sockets::sockaddrs sa;
148
149         /** The config tag that the listener was created from. */
150         ConfigTag* tag;
151
152         FailedPort(int err, irc::sockets::sockaddrs& ep, ConfigTag* cfg)
153                 : error(err)
154                 , sa(ep)
155                 , tag(cfg)
156         {
157         }
158 };
159
160 /** A list of failed port bindings, used for informational purposes on startup */
161 typedef std::vector<FailedPort> FailedPortList;
162
163 #include "socketengine.h"
164
165 /** This class handles incoming connections on client ports.
166  * It will create a new User for every valid connection
167  * and assign it a file descriptor.
168  */
169 class CoreExport ListenSocket : public EventHandler
170 {
171  public:
172         reference<ConfigTag> bind_tag;
173         const irc::sockets::sockaddrs bind_sa;
174
175         class IOHookProvRef : public dynamic_reference_nocheck<IOHookProvider>
176         {
177          public:
178                 IOHookProvRef()
179                         : dynamic_reference_nocheck<IOHookProvider>(NULL, std::string())
180                 {
181                 }
182         };
183
184         typedef TR1NS::array<IOHookProvRef, 2> IOHookProvList;
185
186         /** IOHook providers for handling connections on this socket,
187          * may be empty.
188          */
189         IOHookProvList iohookprovs;
190
191         /** Create a new listening socket
192          */
193         ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_to);
194         /** Close the socket
195          */
196         ~ListenSocket();
197
198         /** Handles new connections, called by the socket engine
199          */
200         void OnEventHandlerRead() CXX11_OVERRIDE;
201
202         /** Inspects the bind block belonging to this socket to set the name of the IO hook
203          * provider which this socket will use for incoming connections.
204          */
205         void ResetIOHookProvider();
206 };