]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socket.h
Get rid of socklen_t parameter to Bind, we are using C++ here and can do it other...
[user/henk/code/inspircd.git] / include / socket.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef INSPIRCD_SOCKET_H
15 #define INSPIRCD_SOCKET_H
16
17 #ifndef WIN32
18
19 #include <arpa/inet.h>
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/stat.h>
25 #include <netinet/in.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <netdb.h>
29
30 #else
31
32 #include "inspircd_win32wrapper.h"
33
34 #endif
35
36 #include <cerrno>
37
38 /* Contains irc-specific definitions */
39 namespace irc
40 {
41         /** This namespace contains various protocol-independent helper classes.
42          * It also contains some types which are often used by the core and modules
43          * in place of inet_* functions and types.
44          */
45         namespace sockets
46         {
47                 union CoreExport sockaddrs {
48                         struct sockaddr sa;
49                         struct sockaddr_in in4;
50                         struct sockaddr_in6 in6;
51                         /** Return the size of the structure for syscall passing */
52                         int sa_size() const;
53                         /** Return port number or -1 if invalid */
54                         int port() const;
55                         /** Return IP only */
56                         std::string addr() const;
57                         /** Return human-readable IP/port pair */
58                         std::string str() const;
59                 };
60
61                 struct cidr_mask
62                 {
63                         /** Type, AF_INET or AF_INET6 */
64                         unsigned char type;
65                         /** Length of the mask in bits (0-128) */
66                         unsigned char length;
67                         /** Raw bits. Unused bits must be zero */
68                         unsigned char bits[16];
69
70                         cidr_mask() {}
71                         /** Construct a CIDR mask from the string. Will normalize (127.0.0.1/8 => 127.0.0.0/8). */
72                         cidr_mask(const std::string& mask);
73                         /** Construct a CIDR mask of a given length from the given address */
74                         cidr_mask(const irc::sockets::sockaddrs& addr, int len);
75                         /** Equality of bits, type, and length */
76                         bool operator==(const cidr_mask& other) const;
77                         /** Ordering defined for maps */
78                         bool operator<(const cidr_mask& other) const;
79                         /** Match within this CIDR? */
80                         bool match(const irc::sockets::sockaddrs& addr) const;
81                         /** Human-readable string */
82                         std::string str() const;
83                 };
84
85                 /** Match CIDR, including an optional username/nickname part.
86                  *
87                  * This function will compare a human-readable address (plus
88                  * optional username and nickname) against a human-readable
89                  * CIDR mask, for example joe!bloggs\@1.2.3.4 against
90                  * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
91                  * IPV6 addresses.
92                  * @param address The human readable address, e.g. fred\@1.2.3.4
93                  * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
94                  * @return True if the mask matches the address
95                  */
96                 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
97
98                 /** Return the size of the structure for syscall passing */
99                 inline int sa_size(const irc::sockets::sockaddrs& sa) { return sa.sa_size(); }
100
101                 /** Convert an address-port pair into a binary sockaddr
102                  * @param addr The IP address, IPv4 or IPv6
103                  * @param port The port, 0 for unspecified
104                  * @param sa The structure to place the result in. Will be zeroed prior to conversion
105                  * @return true if the conversion was successful, false if not.
106                  */
107                 CoreExport bool aptosa(const std::string& addr, int port, irc::sockets::sockaddrs& sa);
108
109                 /** Convert a binary sockaddr to an address-port pair
110                  * @param sa The structure to convert
111                  * @param addr the IP address
112                  * @param port the port
113                  * @return true if the conversion was successful, false if unknown address family
114                  */
115                 CoreExport bool satoap(const irc::sockets::sockaddrs& sa, std::string& addr, int &port);
116
117                 /** Convert a binary sockaddr to a user-readable string.
118                  * This means IPv6 addresses are written as [::1]:6667, and *:6668 is used for 0.0.0.0:6668
119                  * @param sa The structure to convert
120                  * @return The string; "<unknown>" if not a valid address
121                  */
122                 inline std::string satouser(const irc::sockets::sockaddrs& sa) { return sa.str(); }
123         }
124 }
125
126 #include "socketengine.h"
127 /** This class handles incoming connections on client ports.
128  * It will create a new User for every valid connection
129  * and assign it a file descriptor.
130  */
131 class CoreExport ListenSocket : public EventHandler
132 {
133  public:
134         const reference<ConfigTag> bind_tag;
135         std::string bind_addr;
136         int bind_port;
137         /** Human-readable bind description */
138         std::string bind_desc;
139         /** Create a new listening socket
140          */
141         ListenSocket(ConfigTag* tag, const std::string& addr, int port);
142         /** Handle an I/O event
143          */
144         void HandleEvent(EventType et, int errornum = 0);
145         /** Close the socket
146          */
147         ~ListenSocket();
148
149         /** Handles sockets internals crap of a connection, convenience wrapper really
150          */
151         void AcceptInternal();
152 };
153
154 #endif
155