]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socket.h
Start moving IO hooking from being bufferedsocket based to residing in EventHandler...
[user/henk/code/inspircd.git] / include / socket.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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         /* macros to the relevant system address description structs */
50 #ifdef IPV6
51                 /** insp_sockaddr for ipv6
52                  */
53                 typedef struct sockaddr_in6 insp_sockaddr;
54                 /** insp_inaddr for ipv6
55                  */
56                 typedef struct in6_addr     insp_inaddr;
57 #define AF_FAMILY AF_INET6
58 #define PF_PROTOCOL PF_INET6
59
60 #else
61                 /** insp_sockaddr for ipv4
62                  */
63                 typedef struct sockaddr_in  insp_sockaddr;
64                 /** insp_inaddr for ipv4
65                  */
66                 typedef struct in_addr      insp_inaddr;
67 #define AF_FAMILY AF_INET
68 #define PF_PROTOCOL PF_INET
69
70 #endif
71                 /** Match raw binary data using CIDR rules.
72                  *
73                  * This function will use binary comparison to compare the
74                  * two bit sequences, address and mask, up to mask_bits
75                  * bits in size. If they match, it will return true.
76                  * @param address The whole address, of 4 or 16 bytes in length
77                  * @param mask The mask, from 1 to 16 bytes in length, anything
78                  * from 1 to 128 bits of which is significant
79                  * @param mask_Bits How many bits of the mask parameter are significant
80                  * for this comparison.
81                  * @returns True if the first mask_bits of address matches the first
82                  * mask_bits of mask.
83                  */
84                 CoreExport bool MatchCIDRBits(const unsigned char* address, const unsigned char* mask, unsigned int mask_bits);
85
86                 /** Match CIDR, without matching username/nickname parts.
87                  *
88                  * This function will compare a human-readable address against a human-
89                  * readable CIDR mask, for example 1.2.3.4 against 1.2.0.0/16. This
90                  * method supports both IPV4 and IPV6 addresses.
91                  * @param address The human readable address, e.g. 1.2.3.4
92                  * @param cidr_mask The human readable mask, e.g. 1.2.0.0/16
93                  * @return True if the mask matches the address
94                  */
95                 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask);
96
97                 /** Match CIDR, including an optional username/nickname part.
98                  *
99                  * This function will compare a human-readable address (plus
100                  * optional username and nickname) against a human-readable
101                  * CIDR mask, for example joe!bloggs\@1.2.3.4 against
102                  * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
103                  * IPV6 addresses.
104                  * @param address The human readable address, e.g. fred\@1.2.3.4
105                  * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
106                  * @return True if the mask matches the address
107                  */
108                 CoreExport bool MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username);
109
110                 /** Convert an insp_inaddr into human readable form.
111                  *
112                  * @param n An insp_inaddr (IP address) structure
113                  * @return A human-readable address. IPV6 addresses
114                  * will be shortened to remove fields which are 0.
115                  */
116                 CoreExport const char* insp_ntoa(insp_inaddr n);
117
118                 /** Convert a human-readable address into an insp_inaddr.
119                  *
120                  * @param a A human-readable address
121                  * @param n An insp_inaddr struct which the result
122                  * will be copied into on success.
123                  * @return This method will return a negative value if address
124                  * does not contain a valid address family. 0 if the address is
125                  * does not contain a valid string representing a valid network
126                  * address. A positive value is returned if the network address
127                  * was successfully converted.
128
129                  * or any other number upon failure.
130                  */
131                 CoreExport int insp_aton(const char* a, insp_inaddr* n);
132
133                 /** Create a new valid file descriptor using socket()
134                  * @return On return this function will return a value >= 0 for success,
135                  * or a negative value upon failure (negative values are invalid file
136                  * descriptors)
137                  */
138                 CoreExport int OpenTCPSocket(char* addr, int socktype = SOCK_STREAM);
139         }
140 }
141
142
143
144 /** This class handles incoming connections on client ports.
145  * It will create a new User for every valid connection
146  * and assign it a file descriptor.
147  */
148 class CoreExport ListenSocket : public EventHandler
149 {
150  protected:
151         /** The creator/owner of this object
152          */
153         InspIRCd* ServerInstance;
154         /** Socket description (shown in stats p) */
155         std::string desc;
156         /** Socket address family */
157         int family;
158         /** Address socket is bound to */
159         std::string bind_addr;
160         /** Port socket is bound to */
161         int bind_port;
162
163         static sockaddr *sock_us;
164
165         static sockaddr *client;
166
167         static sockaddr *raddr;
168
169         static unsigned int socketcount;
170
171  public:
172         /** Create a new listening socket
173          */
174         ListenSocket(InspIRCd* Instance, int port, char* addr);
175         /** Handle an I/O event
176          */
177         void HandleEvent(EventType et, int errornum = 0);
178         /** Close the socket
179          */
180         ~ListenSocket();
181         /** Set descriptive text
182          */
183         void SetDescription(const std::string &description)
184         {
185                 desc = description;
186         }
187         /** Get description for socket
188          */
189         const std::string& GetDescription()
190         {
191                 return desc;
192         }
193         /** Get port number for socket
194          */
195         int GetPort()
196         {
197                 return bind_port;
198         }
199         /** Get IP address socket is bound to
200          */
201         std::string &GetIP()
202         {
203                 return bind_addr;
204         }
205 };
206
207 #endif
208