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