]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socket.h
Split IOHook into IOHook and IOHookProvider
[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 <netinet/in.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <netdb.h>
39
40 #else
41
42 #include "inspircd_win32wrapper.h"
43
44 #endif
45
46 #include <cerrno>
47
48 /* Contains irc-specific definitions */
49 namespace irc
50 {
51         /** This namespace contains various protocol-independent helper classes.
52          * It also contains some types which are often used by the core and modules
53          * in place of inet_* functions and types.
54          */
55         namespace sockets
56         {
57                 union CoreExport sockaddrs
58                 {
59                         struct sockaddr sa;
60                         struct sockaddr_in in4;
61                         struct sockaddr_in6 in6;
62                         /** Return the size of the structure for syscall passing */
63                         int sa_size() const;
64                         /** Return port number or -1 if invalid */
65                         int port() const;
66                         /** Return IP only */
67                         std::string addr() const;
68                         /** Return human-readable IP/port pair */
69                         std::string str() const;
70                         bool operator==(const sockaddrs& other) const;
71                         inline bool operator!=(const sockaddrs& other) const { return !(*this == other); }
72                 };
73
74                 struct CoreExport cidr_mask
75                 {
76                         /** Type, AF_INET or AF_INET6 */
77                         unsigned char type;
78                         /** Length of the mask in bits (0-128) */
79                         unsigned char length;
80                         /** Raw bits. Unused bits must be zero */
81                         unsigned char bits[16];
82
83                         cidr_mask() {}
84                         /** Construct a CIDR mask from the string. Will normalize (127.0.0.1/8 => 127.0.0.0/8). */
85                         cidr_mask(const std::string& mask);
86                         /** Construct a CIDR mask of a given length from the given address */
87                         cidr_mask(const irc::sockets::sockaddrs& addr, int len);
88                         /** Equality of bits, type, and length */
89                         bool operator==(const cidr_mask& other) const;
90                         /** Ordering defined for maps */
91                         bool operator<(const cidr_mask& other) const;
92                         /** Match within this CIDR? */
93                         bool match(const irc::sockets::sockaddrs& addr) const;
94                         /** Human-readable string */
95                         std::string str() const;
96                 };
97
98                 /** Match CIDR, including an optional username/nickname part.
99                  *
100                  * This function will compare a human-readable address (plus
101                  * optional username and nickname) against a human-readable
102                  * CIDR mask, for example joe!bloggs\@1.2.3.4 against
103                  * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
104                  * IPV6 addresses.
105                  * @param address The human readable address, e.g. fred\@1.2.3.4
106                  * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
107                  * @param match_with_username Does the  mask include a nickname segment?
108                  * @return True if the mask matches the address
109                  */
110                 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
111
112                 /** Convert an address-port pair into a binary sockaddr
113                  * @param addr The IP address, IPv4 or IPv6
114                  * @param port The port, 0 for unspecified
115                  * @param sa The structure to place the result in. Will be zeroed prior to conversion
116                  * @return true if the conversion was successful, false if not.
117                  */
118                 CoreExport bool aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa);
119
120                 /** Convert a binary sockaddr to an address-port pair
121                  * @param sa The structure to convert
122                  * @param addr the IP address
123                  * @param port the port
124                  * @return true if the conversion was successful, false if unknown address family
125                  */
126                 CoreExport bool satoap(const irc::sockets::sockaddrs& sa, std::string& addr, int &port);
127         }
128 }
129
130 #include "iohook.h"
131 #include "socketengine.h"
132 /** This class handles incoming connections on client ports.
133  * It will create a new User for every valid connection
134  * and assign it a file descriptor.
135  */
136 class CoreExport ListenSocket : public EventHandler
137 {
138  public:
139         reference<ConfigTag> bind_tag;
140         std::string bind_addr;
141         int bind_port;
142         /** Human-readable bind description */
143         std::string bind_desc;
144
145         /** The IOHook provider which handles connections on this socket,
146          * NULL if there is none.
147          */
148         dynamic_reference_nocheck<IOHookProvider> iohookprov;
149
150         /** Create a new listening socket
151          */
152         ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_to);
153         /** Handle an I/O event
154          */
155         void HandleEvent(EventType et, int errornum = 0);
156         /** Close the socket
157          */
158         ~ListenSocket();
159
160         /** Handles sockets internals crap of a connection, convenience wrapper really
161          */
162         void AcceptInternal();
163
164         /** Inspects the bind block belonging to this socket to set the name of the IO hook
165          * provider which this socket will use for incoming connections.
166          * @return True if the IO hook provider was found or none was given, false otherwise.
167          */
168         bool ResetIOHookProvider();
169 };