]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socket.h
OOPS! We try again, since I'm smoking craq. LF is 0x0a NOT CR.
[user/henk/code/inspircd.git] / include / socket.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 <errno.h>
37 #include "inspircd_config.h"
38 #include "socketengine.h"
39
40 /* Accept Define */
41 #ifdef CONFIG_USE_IOCP
42 /* IOCP wrapper for accept() */
43 #define _accept(s, addr, addrlen) __accept_socket(s, addr, addrlen, m_acceptEvent)
44 /* IOCP wrapper for getsockname() */
45 #define _getsockname(fd, sockptr, socklen) __getsockname(fd, sockptr, socklen, m_acceptEvent)
46 /* IOCP wrapper for recvfrom() */
47 #define _recvfrom(s, buf, len, flags, from, fromlen) __recvfrom(s, buf, len, flags, from, fromlen, ((IOCPEngine*)ServerInstance->SE)->udp_ov)
48 #else
49 /* No wrapper for recvfrom() */
50 #define _recvfrom recvfrom
51 /* No wrapper for accept() */
52 #define _accept accept
53 /* No wrapper for getsockname() */
54 #define _getsockname getsockname
55 #endif
56
57 /* Contains irc-specific definitions */
58 namespace irc
59 {
60         /** This namespace contains various protocol-independent helper classes.
61          * It also contains some types which are often used by the core and modules
62          * in place of inet_* functions and types.
63          */
64         namespace sockets
65         {
66
67         /* macros to the relevant system address description structs */
68 #ifdef IPV6
69                 /** insp_sockaddr for ipv6
70                  */
71                 typedef struct sockaddr_in6 insp_sockaddr;
72                 /** insp_inaddr for ipv6
73                  */
74                 typedef struct in6_addr     insp_inaddr;
75 #define AF_FAMILY AF_INET6
76 #define PF_PROTOCOL PF_INET6
77
78 #else
79                 /** insp_sockaddr for ipv4
80                  */
81                 typedef struct sockaddr_in  insp_sockaddr;
82                 /** insp_inaddr for ipv4
83                  */
84                 typedef struct in_addr      insp_inaddr;
85 #define AF_FAMILY AF_INET
86 #define PF_PROTOCOL PF_INET
87
88 #endif
89                 /** Match raw binary data using CIDR rules.
90                  * 
91                  * This function will use binary comparison to compare the
92                  * two bit sequences, address and mask, up to mask_bits
93                  * bits in size. If they match, it will return true.
94                  * @param address The whole address, of 4 or 16 bytes in length
95                  * @param mask The mask, from 1 to 16 bytes in length, anything
96                  * from 1 to 128 bits of which is significant
97                  * @param mask_Bits How many bits of the mask parameter are significant
98                  * for this comparison.
99                  * @returns True if the first mask_bits of address matches the first
100                  * mask_bits of mask.
101                  */
102                 CoreExport bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits);
103
104                 /** Match CIDR, without matching username/nickname parts.
105                  *
106                  * This function will compare a human-readable address against a human-
107                  * readable CIDR mask, for example 1.2.3.4 against 1.2.0.0/16. This
108                  * method supports both IPV4 and IPV6 addresses.
109                  * @param address The human readable address, e.g. 1.2.3.4
110                  * @param cidr_mask The human readable mask, e.g. 1.2.0.0/16
111                  * @return True if the mask matches the address
112                  */
113                 CoreExport bool MatchCIDR(const char* address, const char* cidr_mask);
114
115                 /** Match CIDR, including an optional username/nickname part.
116                  *
117                  * This function will compare a human-readable address (plus
118                  * optional username and nickname) against a human-readable
119                  * CIDR mask, for example joe!bloggs\@1.2.3.4 against
120                  * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
121                  * IPV6 addresses.
122                  * @param address The human readable address, e.g. fred\@1.2.3.4
123                  * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
124                  * @return True if the mask matches the address
125                  */
126                 CoreExport bool MatchCIDR(const char* address, const char* cidr_mask, bool match_with_username);
127
128                 /** Convert an insp_inaddr into human readable form.
129                  * 
130                  * @param n An insp_inaddr (IP address) structure
131                  * @return A human-readable address. IPV6 addresses
132                  * will be shortened to remove fields which are 0.
133                  */
134                 CoreExport const char* insp_ntoa(insp_inaddr n);
135
136                 /** Convert a human-readable address into an insp_inaddr.
137                  * 
138                  * @param a A human-readable address
139                  * @param n An insp_inaddr struct which the result
140                  * will be copied into on success.
141                  * @return This method will return a negative value if address
142                  * does not contain a valid address family. 0 if the address is
143                  * does not contain a valid string representing a valid network
144                  * address. A positive value is returned if the network address
145                  * was successfully converted.
146
147                  * or any other number upon failure.
148                  */
149                 CoreExport int insp_aton(const char* a, insp_inaddr* n);
150
151                 /** Make a socket file descriptor a blocking socket
152                  * @param s A valid file descriptor
153                  */
154                 CoreExport void Blocking(int s);
155
156                 /** Make a socket file descriptor into a nonblocking socket
157                  * @param s A valid file descriptor
158                  */
159                 CoreExport void NonBlocking(int s);
160
161                 /** Create a new valid file descriptor using socket()
162                  * @return On return this function will return a value >= 0 for success,
163                  * or a negative value upon failure (negative values are invalid file
164                  * descriptors)
165                  */
166                 CoreExport int OpenTCPSocket(char* addr, int socktype = SOCK_STREAM);
167         }
168 }
169
170 /** This class handles incoming connections on client ports.
171  * It will create a new userrec for every valid connection
172  * and assign it a file descriptor.
173  */
174 class CoreExport ListenSocket : public EventHandler
175 {
176  protected:
177         /** The creator/owner of this object
178          */
179         InspIRCd* ServerInstance;
180         /** Socket description (shown in stats p) */
181         std::string desc;
182         /** Socket address family */
183         int family;
184         /** Address socket is bound to */
185         std::string bind_addr;
186         /** Port socket is bound to */
187         int bind_port;
188  public:
189         /** Create a new listening socket
190          */
191         ListenSocket(InspIRCd* Instance, int port, char* addr);
192         /** Handle an I/O event
193          */
194         void HandleEvent(EventType et, int errornum = 0);
195         /** Close the socket
196          */
197         ~ListenSocket();
198         /** Set descriptive text
199          */
200         void SetDescription(const std::string &description)
201         {
202                 desc = description;
203         }
204         /** Get description for socket
205          */
206         const std::string& GetDescription()
207         {
208                 return desc;
209         }
210         /** Get port number for socket
211          */
212         int GetPort()
213         {
214                 return bind_port;
215         }
216         /** Get IP address socket is bound to
217          */
218         std::string &GetIP()
219         {
220                 return bind_addr;
221         }
222 };
223
224 #endif
225