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