]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socket.h
66578cca6955aabdd803468fc18a4829fc735863
[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
49                 typedef union {
50                         struct sockaddr sa;
51                         struct sockaddr_in in4;
52                         struct sockaddr_in6 in6;
53                 } sockaddrs;
54
55         /* macros to the relevant system address description structs */
56 #ifdef IPV6
57                 /** insp_sockaddr for ipv6
58                  */
59                 typedef struct sockaddr_in6 insp_sockaddr;
60                 /** insp_inaddr for ipv6
61                  */
62                 typedef struct in6_addr     insp_inaddr;
63 #define AF_FAMILY AF_INET6
64
65 #else
66                 /** insp_sockaddr for ipv4
67                  */
68                 typedef struct sockaddr_in  insp_sockaddr;
69                 /** insp_inaddr for ipv4
70                  */
71                 typedef struct in_addr      insp_inaddr;
72 #define AF_FAMILY AF_INET
73
74 #endif
75                 /** Match raw binary data using CIDR rules.
76                  *
77                  * This function will use binary comparison to compare the
78                  * two bit sequences, address and mask, up to mask_bits
79                  * bits in size. If they match, it will return true.
80                  * @param address The whole address, of 4 or 16 bytes in length
81                  * @param mask The mask, from 1 to 16 bytes in length, anything
82                  * from 1 to 128 bits of which is significant
83                  * @param mask_Bits How many bits of the mask parameter are significant
84                  * for this comparison.
85                  * @returns True if the first mask_bits of address matches the first
86                  * mask_bits of mask.
87                  */
88                 CoreExport bool MatchCIDRBits(const unsigned char* address, const unsigned char* mask, unsigned int mask_bits);
89
90                 /** Match CIDR, without matching username/nickname parts.
91                  *
92                  * This function will compare a human-readable address against a human-
93                  * readable CIDR mask, for example 1.2.3.4 against 1.2.0.0/16. This
94                  * method supports both IPV4 and IPV6 addresses.
95                  * @param address The human readable address, e.g. 1.2.3.4
96                  * @param cidr_mask The human readable mask, e.g. 1.2.0.0/16
97                  * @return True if the mask matches the address
98                  */
99                 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask);
100
101                 /** Match CIDR, including an optional username/nickname part.
102                  *
103                  * This function will compare a human-readable address (plus
104                  * optional username and nickname) against a human-readable
105                  * CIDR mask, for example joe!bloggs\@1.2.3.4 against
106                  * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
107                  * IPV6 addresses.
108                  * @param address The human readable address, e.g. fred\@1.2.3.4
109                  * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
110                  * @return True if the mask matches the address
111                  */
112                 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
113
114                 /** Convert an insp_inaddr into human readable form.
115                  *
116                  * @param n An insp_inaddr (IP address) structure
117                  * @return A human-readable address. IPV6 addresses
118                  * will be shortened to remove fields which are 0.
119                  */
120                 CoreExport const char* insp_ntoa(insp_inaddr n);
121
122                 /** Convert a human-readable address into an insp_inaddr.
123                  *
124                  * @param a A human-readable address
125                  * @param n An insp_inaddr struct which the result
126                  * will be copied into on success.
127                  * @return This method will return a negative value if address
128                  * does not contain a valid address family. 0 if the address is
129                  * does not contain a valid string representing a valid network
130                  * address. A positive value is returned if the network address
131                  * was successfully converted.
132
133                  * or any other number upon failure.
134                  */
135                 CoreExport int insp_aton(const char* a, insp_inaddr* n);
136
137                 /** Create a new valid file descriptor using socket()
138                  * @return On return this function will return a value >= 0 for success,
139                  * or a negative value upon failure (negative values are invalid file
140                  * descriptors)
141                  */
142                 CoreExport int OpenTCPSocket(const char* addr, int socktype = SOCK_STREAM);
143
144                 /** Return the size of the structure for syscall passing */
145                 CoreExport int sa_size(irc::sockets::sockaddrs& sa);
146
147                 /** Convert an address-port pair into a binary sockaddr
148                  * @param addr The IP address, IPv4 or IPv6
149                  * @param port The port, 0 for unspecified
150                  * @param sa The structure to place the result in. Will be zeroed prior to conversion
151                  * @return true if the conversion was successful, false if not.
152                  */
153                 CoreExport int aptosa(const char* addr, int port, irc::sockets::sockaddrs* sa);
154                 /** Convert a binary sockaddr to an address-port pair
155                  * @param sa The structure to convert
156                  * @param addr the IP address
157                  * @param port the port
158                  * @return true if the conversion was successful, false if unknown address family
159                  */
160                 CoreExport int satoap(const irc::sockets::sockaddrs* sa, std::string& addr, int &port);
161         }
162 }
163
164
165
166 /** This class handles incoming connections on client ports.
167  * It will create a new User for every valid connection
168  * and assign it a file descriptor.
169  */
170 class CoreExport ListenSocketBase : public EventHandler
171 {
172  protected:
173         /** The creator/owner of this object
174          */
175         InspIRCd* ServerInstance;
176         /** Socket description (shown in stats p) */
177         std::string desc;
178
179         /** Address socket is bound to */
180         std::string bind_addr;
181         /** Port socket is bound to */
182         int bind_port;
183
184         /** The client address if the most recently connected client.
185          * Should only be used when accepting a new client.
186          */
187         static irc::sockets::sockaddrs client;
188         /** The server address used by the most recently connected client.
189          * This may differ from the bind address by having a nonzero address,
190          * if the port is wildcard bound, or being IPv4 on a 6to4 IPv6 port.
191          * The address family will always match that of "client"
192          */
193         static irc::sockets::sockaddrs server;
194
195  public:
196         /** Create a new listening socket
197          */
198         ListenSocketBase(InspIRCd* Instance, int port, const std::string &addr);
199         /** Handle an I/O event
200          */
201         void HandleEvent(EventType et, int errornum = 0);
202         /** Close the socket
203          */
204         ~ListenSocketBase();
205         /** Set descriptive text
206          */
207         void SetDescription(const std::string &description)
208         {
209                 desc = description;
210         }
211         /** Get description for socket
212          */
213         const std::string& GetDescription()
214         {
215                 return desc;
216         }
217         /** Get port number for socket
218          */
219         int GetPort() { return bind_port; }
220
221         /** Get IP address socket is bound to
222          */
223         const std::string &GetIP() { return bind_addr; }
224
225         /** Handles sockets internals crap of a connection, convenience wrapper really
226          */
227         void AcceptInternal();
228
229         /** Called when a new connection has successfully been accepted on this listener.
230          * @param ipconnectedto The IP address the connection arrived on
231          * @param fd The file descriptor of the new connection
232          * @param incomingip The IP from which the connection was made
233          */
234         virtual void OnAcceptReady(const std::string &ipconnectedto, int fd, const std::string &incomingip) = 0;
235 };
236
237 class CoreExport ClientListenSocket : public ListenSocketBase
238 {
239         virtual void OnAcceptReady(const std::string &ipconnectedto, int fd, const std::string &incomingip);
240  public:
241         ClientListenSocket(InspIRCd* Instance, int port, const std::string &addr) : ListenSocketBase(Instance, port, addr) { }
242 };
243
244 #endif
245