]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socket.h
7fa58f8ad992bfe444fafe3bd9d618a7dabd2a16
[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 #ifndef INSPIRCD_SOCKET_H
26 #define INSPIRCD_SOCKET_H
27
28 #ifndef WIN32
29
30 #include <arpa/inet.h>
31 #include <sys/time.h>
32 #include <sys/resource.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/stat.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                         /** Return the size of the structure for syscall passing */
64                         int sa_size() const;
65                         /** Return port number or -1 if invalid */
66                         int port() const;
67                         /** Return IP only */
68                         std::string addr() const;
69                         /** Return human-readable IP/port pair */
70                         std::string str() const;
71                         bool operator==(const sockaddrs& other) const;
72                         inline bool operator!=(const sockaddrs& other) const { return !(*this == other); }
73                 };
74
75                 struct CoreExport cidr_mask
76                 {
77                         /** Type, AF_INET or AF_INET6 */
78                         unsigned char type;
79                         /** Length of the mask in bits (0-128) */
80                         unsigned char length;
81                         /** Raw bits. Unused bits must be zero */
82                         unsigned char bits[16];
83
84                         cidr_mask() {}
85                         /** Construct a CIDR mask from the string. Will normalize (127.0.0.1/8 => 127.0.0.0/8). */
86                         cidr_mask(const std::string& mask);
87                         /** Construct a CIDR mask of a given length from the given address */
88                         cidr_mask(const irc::sockets::sockaddrs& addr, int len);
89                         /** Equality of bits, type, and length */
90                         bool operator==(const cidr_mask& other) const;
91                         /** Ordering defined for maps */
92                         bool operator<(const cidr_mask& other) const;
93                         /** Match within this CIDR? */
94                         bool match(const irc::sockets::sockaddrs& addr) const;
95                         /** Human-readable string */
96                         std::string str() const;
97                 };
98
99                 /** Match CIDR, including an optional username/nickname part.
100                  *
101                  * This function will compare a human-readable address (plus
102                  * optional username and nickname) against a human-readable
103                  * CIDR mask, for example joe!bloggs\@1.2.3.4 against
104                  * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
105                  * IPV6 addresses.
106                  * @param address The human readable address, e.g. fred\@1.2.3.4
107                  * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
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                 /** Return the size of the structure for syscall passing */
113                 inline int sa_size(const irc::sockets::sockaddrs& sa) { return sa.sa_size(); }
114
115                 /** Convert an address-port pair into a binary sockaddr
116                  * @param addr The IP address, IPv4 or IPv6
117                  * @param port The port, 0 for unspecified
118                  * @param sa The structure to place the result in. Will be zeroed prior to conversion
119                  * @return true if the conversion was successful, false if not.
120                  */
121                 CoreExport bool aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa);
122
123                 /** Convert a binary sockaddr to an address-port pair
124                  * @param sa The structure to convert
125                  * @param addr the IP address
126                  * @param port the port
127                  * @return true if the conversion was successful, false if unknown address family
128                  */
129                 CoreExport bool satoap(const irc::sockets::sockaddrs& sa, std::string& addr, int &port);
130
131                 /** Convert a binary sockaddr to a user-readable string.
132                  * This means IPv6 addresses are written as [::1]:6667, and *:6668 is used for 0.0.0.0:6668
133                  * @param sa The structure to convert
134                  * @return The string; "<unknown>" if not a valid address
135                  */
136                 inline std::string satouser(const irc::sockets::sockaddrs& sa) { return sa.str(); }
137         }
138 }
139
140 #include "socketengine.h"
141 /** This class handles incoming connections on client ports.
142  * It will create a new User for every valid connection
143  * and assign it a file descriptor.
144  */
145 class CoreExport ListenSocket : public EventHandler
146 {
147  public:
148         const reference<ConfigTag> bind_tag;
149         std::string bind_addr;
150         int bind_port;
151         /** Human-readable bind description */
152         std::string bind_desc;
153         /** Create a new listening socket
154          */
155         ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_to);
156         /** Handle an I/O event
157          */
158         void HandleEvent(EventType et, int errornum = 0);
159         /** Close the socket
160          */
161         ~ListenSocket();
162
163         /** Handles sockets internals crap of a connection, convenience wrapper really
164          */
165         void AcceptInternal();
166 };
167
168 #endif
169