]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socket.h
eebc172cfb2414a657db6365b73763c4baf59507
[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 #include <arpa/inet.h>
18 #include <sys/time.h>
19 #include <sys/resource.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <netinet/in.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <netdb.h>
27 #include <errno.h>
28 #include "inspircd_config.h"
29 #include "socketengine.h"
30
31 namespace irc
32 {
33         /** This namespace contains various protocol-independent helper classes.
34          * It also contains some types which are often used by the core and modules
35          * in place of inet_* functions and types.
36          */
37         namespace sockets
38         {
39
40         /* macros to the relevant system address description structs */
41 #ifdef IPV6
42                 /** insp_sockaddr for ipv6
43                  */
44                 typedef struct sockaddr_in6 insp_sockaddr;
45                 /** insp_inaddr for ipv6
46                  */
47                 typedef struct in6_addr     insp_inaddr;
48 #define AF_FAMILY AF_INET6
49 #define PF_PROTOCOL PF_INET6
50
51 #else
52                 /** insp_sockaddr for ipv4
53                  */
54                 typedef struct sockaddr_in  insp_sockaddr;
55                 /** insp_inaddr for ipv4
56                  */
57                 typedef struct in_addr      insp_inaddr;
58 #define AF_FAMILY AF_INET
59 #define PF_PROTOCOL PF_INET
60
61 #endif
62                 /** Match raw binary data using CIDR rules.
63                  * 
64                  * This function will use binary comparison to compare the
65                  * two bit sequences, address and mask, up to mask_bits
66                  * bits in size. If they match, it will return true.
67                  * @param address The whole address, of 4 or 16 bytes in length
68                  * @param mask The mask, from 1 to 16 bytes in length, anything
69                  * from 1 to 128 bits of which is significant
70                  * @param mask_Bits How many bits of the mask parameter are significant
71                  * for this comparison.
72                  * @returns True if the first mask_bits of address matches the first
73                  * mask_bits of mask.
74                  */
75                 bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits);
76
77                 /** Match CIDR, without matching username/nickname parts.
78                  *
79                  * This function will compare a human-readable address against a human-
80                  * readable CIDR mask, for example 1.2.3.4 against 1.2.0.0/16. This
81                  * method supports both IPV4 and IPV6 addresses.
82                  * @param address The human readable address, e.g. 1.2.3.4
83                  * @param cidr_mask The human readable mask, e.g. 1.2.0.0/16
84                  * @return True if the mask matches the address
85                  */
86                 bool MatchCIDR(const char* address, const char* cidr_mask);
87
88                 /** Match CIDR, including an optional username/nickname part.
89                  *
90                  * This function will compare a human-readable address (plus
91                  * optional username and nickname) against a human-readable
92                  * CIDR mask, for example joe!bloggs\@1.2.3.4 against
93                  * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
94                  * IPV6 addresses.
95                  * @param address The human readable address, e.g. fred\@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                 bool MatchCIDR(const char* address, const char* cidr_mask, bool match_with_username);
100
101                 /** Convert an insp_inaddr into human readable form.
102                  * 
103                  * @param n An insp_inaddr (IP address) structure
104                  * @return A human-readable address. IPV6 addresses
105                  * will be shortened to remove fields which are 0.
106                  */
107                 const char* insp_ntoa(insp_inaddr n);
108
109                 /** Convert a human-readable address into an insp_inaddr.
110                  * 
111                  * @param a A human-readable address
112                  * @param n An insp_inaddr struct which the result
113                  * will be copied into on success.
114                  * @return This method will return a negative value if address
115                  * does not contain a valid address family. 0 if the address is
116                  * does not contain a valid string representing a valid network
117                  * address. A positive value is returned if the network address
118                  * was successfully converted.
119
120                  * or any other number upon failure.
121                  */
122                 int insp_aton(const char* a, insp_inaddr* n);
123
124                 /** Make a socket file descriptor a blocking socket
125                  * @param s A valid file descriptor
126                  */
127                 void Blocking(int s);
128
129                 /** Make a socket file descriptor into a nonblocking socket
130                  * @param s A valid file descriptor
131                  */
132                 void NonBlocking(int s);
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                 int OpenTCPSocket(char* addr);
140         };
141 };
142
143 /** This class handles incoming connections on client ports.
144  * It will create a new userrec for every valid connection
145  * and assign it a file descriptor.
146  */
147 class ListenSocket : public EventHandler
148 {
149  protected:
150         /** The creator/owner of this object
151          */
152         InspIRCd* ServerInstance;
153         std::string desc;
154  public:
155         /** Create a new listening socket
156          */
157         ListenSocket(InspIRCd* Instance, int sockfd, irc::sockets::insp_sockaddr client, irc::sockets::insp_sockaddr server, int port, char* addr);
158         /** Handle an I/O event
159          */
160         void HandleEvent(EventType et, int errornum = 0);
161         /** Close the socket
162          */
163         ~ListenSocket();
164         /** Set descriptive text
165          */
166         void SetDescription(const std::string &description)
167         {
168                 desc = description;
169         }
170
171         const std::string& GetDescription()
172         {
173                 return desc;
174         }
175 };
176
177 #endif