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