]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socket.h
6264b196f3ab78de6d56ebc965bd818b6cd6404a
[user/henk/code/inspircd.git] / include / socket.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #ifndef INSPIRCD_SOCKET_H
18 #define INSPIRCD_SOCKET_H
19
20 #include <arpa/inet.h>
21 #include <sys/time.h>
22 #include <sys/resource.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/stat.h>
26 #include <netinet/in.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <netdb.h>
30 #include <errno.h>
31 #include "inspircd_config.h"
32 #include "socketengine.h"
33
34 namespace irc
35 {
36         /** This namespace contains various protocol-independent helper classes.
37          * It also contains some types which are often used by the core and modules
38          * in place of inet_* functions and types.
39          */
40         namespace sockets
41         {
42
43         /* macros to the relevant system address description structs */
44 #ifdef IPV6
45                 /** insp_sockaddr for ipv6
46                  */
47                 typedef struct sockaddr_in6 insp_sockaddr;
48                 /** insp_inaddr for ipv6
49                  */
50                 typedef struct in6_addr     insp_inaddr;
51 #define AF_FAMILY AF_INET6
52 #define PF_PROTOCOL PF_INET6
53
54 #else
55                 /** insp_sockaddr for ipv4
56                  */
57                 typedef struct sockaddr_in  insp_sockaddr;
58                 /** insp_inaddr for ipv4
59                  */
60                 typedef struct in_addr      insp_inaddr;
61 #define AF_FAMILY AF_INET
62 #define PF_PROTOCOL PF_INET
63
64 #endif
65                 /** Match raw binary data using CIDR rules.
66                  * 
67                  * This function will use binary comparison to compare the
68                  * two bit sequences, address and mask, up to mask_bits
69                  * bits in size. If they match, it will return true.
70                  * @param address The whole address, of 4 or 16 bytes in length
71                  * @param mask The mask, from 1 to 16 bytes in length, anything
72                  * from 1 to 128 bits of which is significant
73                  * @param mask_Bits How many bits of the mask parameter are significant
74                  * for this comparison.
75                  * @returns True if the first mask_bits of address matches the first
76                  * mask_bits of mask.
77                  */
78                 bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits);
79
80                 /** Match CIDR, without matching username/nickname parts.
81                  *
82                  * This function will compare a human-readable address against a human-
83                  * readable CIDR mask, for example 1.2.3.4 against 1.2.0.0/16. This
84                  * method supports both IPV4 and IPV6 addresses.
85                  * @param address The human readable address, e.g. 1.2.3.4
86                  * @param cidr_mask The human readable mask, e.g. 1.2.0.0/16
87                  * @return True if the mask matches the address
88                  */
89                 bool MatchCIDR(const char* address, const char* cidr_mask);
90
91                 /** Match CIDR, including an optional username/nickname part.
92                  *
93                  * This function will compare a human-readable address (plus
94                  * optional username and nickname) against a human-readable
95                  * CIDR mask, for example joe!bloggs\@1.2.3.4 against
96                  * *!bloggs\@1.2.0.0/16. This method supports both IPV4 and
97                  * IPV6 addresses.
98                  * @param address The human readable address, e.g. fred\@1.2.3.4
99                  * @param cidr_mask The human readable mask, e.g. *\@1.2.0.0/16
100                  * @return True if the mask matches the address
101                  */
102                 bool MatchCIDR(const char* address, const char* cidr_mask, bool match_with_username);
103
104                 /** Convert an insp_inaddr into human readable form.
105                  * 
106                  * @param n An insp_inaddr (IP address) structure
107                  * @return A human-readable address. IPV6 addresses
108                  * will be shortened to remove fields which are 0.
109                  */
110                 const char* insp_ntoa(insp_inaddr n);
111
112                 /** Convert a human-readable address into an insp_inaddr.
113                  * 
114                  * @param a A human-readable address
115                  * @param n An insp_inaddr struct which the result
116                  * will be copied into on success.
117                  * @return This function will return 0 upon success,
118                  * or any other number upon failure.
119                  */
120                 int insp_aton(const char* a, insp_inaddr* n);
121
122                 /** Make a socket file descriptor a blocking socket
123                  * @param s A valid file descriptor
124                  */
125                 void Blocking(int s);
126
127                 /** Make a socket file descriptor into a nonblocking socket
128                  * @param s A valid file descriptor
129                  */
130                 void NonBlocking(int s);
131
132                 /** Create a new valid file descriptor using socket()
133                  * @return On return this function will return a value >= 0 for success,
134                  * or a negative value upon failure (negative values are invalid file
135                  * descriptors)
136                  */
137                 int OpenTCPSocket(); 
138         };
139 };
140
141 /** This class handles incoming connections on client ports.
142  * It will create a new userrec for every valid connection
143  * and assign it a file descriptor.
144  */
145 class ListenSocket : public EventHandler
146 {
147  protected:
148         /** The creator/owner of this object
149          */
150         InspIRCd* ServerInstance;
151  public:
152         /** Create a new listening socket
153          */
154         ListenSocket(InspIRCd* Instance, int sockfd, irc::sockets::insp_sockaddr client, irc::sockets::insp_sockaddr server, int port, char* addr);
155         /** Handle an I/O event
156          */
157         void HandleEvent(EventType et, int errornum = 0);
158 };
159
160 #endif