]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
Support CIDR, CIDR zline, /oper and CIDR <connect> tags. NOTE: With CIDR oper, ident...
[user/henk/code/inspircd.git] / src / socket.cpp
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 #include <string>
18 #include "configreader.h"
19 #include "socket.h"
20 #include "inspircd.h"
21 #include "inspstring.h"
22 #include "helperfuncs.h"
23 #include "socketengine.h"
24 #include "message.h"
25
26 extern InspIRCd* ServerInstance;
27 extern ServerConfig* Config;
28 extern time_t TIME;
29
30 /* Used when comparing CIDR masks for the modulus bits left over */
31
32 char inverted_bits[8] = { 0x00, /* 00000000 - 0 bits */
33                           0x80, /* 10000000 - 1 bits */
34                           0xC0, /* 11000000 - 2 bits */
35                           0xE0, /* 11100000 - 3 bits */
36                           0xF0, /* 11110000 - 4 bits */
37                           0xF8, /* 11111000 - 5 bits */
38                           0xFC, /* 11111100 - 6 bits */
39                           0xFE  /* 11111110 - 7 bits */
40 };
41
42 bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits)
43 {
44         unsigned int modulus = mask_bits % 8; /* Number of whole bytes in the mask */
45         unsigned int divisor = mask_bits / 8; /* Remaining bits in the mask after whole bytes are dealt with */
46
47         /* First compare the whole bytes, if they dont match, return false */
48         if (memcmp(address, mask, divisor))
49                 return false;
50
51         /* Now if there are any remainder bits, we compare them with logic AND */
52         if (modulus)
53                 if ((address[divisor] & inverted_bits[modulus]) != (mask[divisor] & inverted_bits[modulus]))
54                         /* If they dont match, return false */
55                         return false;
56
57         /* The address matches the mask, to mask_bits bits of mask */
58         return true;
59 }
60
61 bool MatchCIDR(const char* address, const char* cidr_mask)
62 {
63         unsigned char addr_raw[16];
64         unsigned char mask_raw[16];
65         unsigned int bits = 0;
66         char* mask = strdup(cidr_mask);
67
68         in_addr  address_in4;
69         in_addr  mask_in4;
70
71         char* bits_chars = strchr(mask,'/');
72
73         if (bits_chars)
74         {
75                 bits = atoi(bits_chars + 1);
76                 *bits_chars = 0;
77         }
78
79 #ifdef SUPPORT_IP6LINKS
80         in6_addr address_in6;
81         in6_addr mask_in6;
82
83         if (inet_pton(AF_INET6, address, &address_in6) > 0)
84         {
85                 if (inet_pton(AF_INET6, mask, &mask_in6) > 0)
86                 {
87                         memcpy(&addr_raw, &address_in6.s6_addr, 16);
88                         memcpy(&mask_raw, &mask_in6.s6_addr, 16);
89                 }
90                 else
91                 {
92                         free(mask);
93                         return false;
94                 }
95         }
96         else
97 #endif
98         if (inet_pton(AF_INET, address, &address_in4) > 0)
99         {
100                 if (inet_pton(AF_INET, mask, &mask_in4) > 0)
101                 {
102                         memcpy(&addr_raw, &address_in4.s_addr, 4);
103                         memcpy(&mask_raw, &mask_in4.s_addr, 4);
104                 }
105                 else
106                 {
107                         free(mask);
108                         return false;
109                 }
110         }
111         else
112         {
113                 free(mask);
114                 return false;
115         }
116
117         free(mask);
118         return MatchCIDRBits(addr_raw, mask_raw, bits);
119 }
120
121 /** This will bind a socket to a port. It works for UDP/TCP.
122  * It can only bind to IP addresses, if you wish to bind to hostnames
123  * you should first resolve them using class 'Resolver'.
124  */ 
125 bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr)
126 {
127         memset(&server,0,sizeof(server));
128         insp_inaddr addy;
129
130         if (*addr == '*')
131                 *addr = 0;
132
133         if ((*addr) && (insp_aton(addr,&addy) < 1))
134         {
135                 log(DEBUG,"Invalid IP '%s' given to BindSocket()", addr);
136                 return false;;
137         }
138
139 #ifdef IPV6
140         server.sin6_family = AF_FAMILY;
141 #else
142         server.sin_family = AF_FAMILY;
143 #endif
144         if (!*addr)
145         {
146 #ifdef IPV6
147                 memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
148 #else
149                 server.sin_addr.s_addr = htonl(INADDR_ANY);
150 #endif
151         }
152         else
153         {
154 #ifdef IPV6
155                 memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
156 #else
157                 server.sin_addr = addy;
158 #endif
159         }
160 #ifdef IPV6
161         server.sin6_port = htons(port);
162 #else
163         server.sin_port = htons(port);
164 #endif
165         if (bind(sockfd,(struct sockaddr*)&server,sizeof(server)) < 0)
166         {
167                 return false;
168         }
169         else
170         {
171                 log(DEBUG,"Bound port %s:%d",*addr ? addr : "*",port);
172                 if (listen(sockfd, Config->MaxConn) == -1)
173                 {
174                         log(DEFAULT,"ERROR in listen(): %s",strerror(errno));
175                         return false;
176                 }
177                 else
178                 {
179                         NonBlocking(sockfd);
180                         return true;
181                 }
182         }
183 }
184
185
186 // Open a TCP Socket
187 int OpenTCPSocket()
188 {
189         int sockfd;
190         int on = 1;
191         struct linger linger = { 0 };
192   
193         if ((sockfd = socket (AF_FAMILY, SOCK_STREAM, 0)) < 0)
194         {
195                 log(DEFAULT,"Error creating TCP socket: %s",strerror(errno));
196                 return (ERROR);
197         }
198         else
199         {
200                 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
201                 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
202                 linger.l_onoff = 1;
203                 linger.l_linger = 1;
204                 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger,sizeof(linger));
205                 return (sockfd);
206         }
207 }
208
209 bool HasPort(int port, char* addr)
210 {
211         for (unsigned long count = 0; count < ServerInstance->stats->BoundPortCount; count++)
212         {
213                 if ((port == Config->ports[count]) && (!strcasecmp(Config->addrs[count],addr)))
214                 {
215                         return true;
216                 }
217         }
218         return false;
219 }
220
221 int BindPorts(bool bail)
222 {
223         char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
224         insp_sockaddr client, server;
225         int clientportcount = 0;
226         int BoundPortCount = 0;
227
228         if (!bail)
229         {
230                 int InitialPortCount = ServerInstance->stats->BoundPortCount;
231                 log(DEBUG,"Initial port count: %d",InitialPortCount);
232
233                 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
234                 {
235                         Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
236                         Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
237                         Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
238
239                         if (((!*Type) || (!strcmp(Type,"clients"))) && (!HasPort(atoi(configToken),Addr)))
240                         {
241                                 // modules handle server bind types now
242                                 Config->ports[clientportcount+InitialPortCount] = atoi(configToken);
243                                 if (*Addr == '*')
244                                         *Addr = 0;
245
246                                 strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
247                                 clientportcount++;
248                                 log(DEBUG,"NEW binding %s:%s [%s] from config",Addr,configToken, Type);
249                         }
250                 }
251                 int PortCount = clientportcount;
252                 if (PortCount)
253                 {
254                         for (int count = InitialPortCount; count < InitialPortCount + PortCount; count++)
255                         {
256                                 if ((Config->openSockfd[count] = OpenTCPSocket()) == ERROR)
257                                 {
258                                         log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[count],Config->addrs[count],Config->ports[count]);
259                                         return ERROR;
260                                 }
261                                 if (!BindSocket(Config->openSockfd[count],client,server,Config->ports[count],Config->addrs[count]))
262                                 {
263                                         log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
264                                 }
265                                 else
266                                 {
267                                         /* Associate the new open port with a slot in the socket engine */
268                                         if (Config->openSockfd[count] > -1)
269                                         {
270                                                 ServerInstance->SE->AddFd(Config->openSockfd[count],true,X_LISTEN);
271                                                 BoundPortCount++;
272                                         }
273                                 }
274                         }
275                         return InitialPortCount + BoundPortCount;
276                 }
277                 else
278                 {
279                         log(DEBUG,"There is nothing new to bind!");
280                 }
281                 return InitialPortCount;
282         }
283
284         for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
285         {
286                 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
287                 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
288                 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
289
290                 if ((!*Type) || (!strcmp(Type,"clients")))
291                 {
292                         // modules handle server bind types now
293                         Config->ports[clientportcount] = atoi(configToken);
294
295                         // If the client put bind "*", this is an unrealism.
296                         // We don't actually support this as documented, but
297                         // i got fed up of people trying it, so now it converts
298                         // it to an empty string meaning the same 'bind to all'.
299                         if (*Addr == '*')
300                                 *Addr = 0;
301
302                         strlcpy(Config->addrs[clientportcount],Addr,256);
303                         clientportcount++;
304                         log(DEBUG,"Binding %s:%s [%s] from config",Addr,configToken, Type);
305                 }
306         }
307
308         int PortCount = clientportcount;
309
310         for (int count = 0; count < PortCount; count++)
311         {
312                 if ((Config->openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
313                 {
314                         log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[BoundPortCount],Config->addrs[count],Config->ports[count]);
315                         return ERROR;
316                 }
317
318                 if (!BindSocket(Config->openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]))
319                 {
320                         log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
321                 }
322                 else
323                 {
324                         /* well we at least bound to one socket so we'll continue */
325                         BoundPortCount++;
326                 }
327         }
328
329         /* if we didn't bind to anything then abort */
330         if (!BoundPortCount)
331         {
332                 log(DEFAULT,"No ports bound, bailing!");
333                 printf("\nERROR: Could not bind any of %d ports! Please check your configuration.\n\n", PortCount);
334                 return ERROR;
335         }
336
337         return BoundPortCount;
338 }
339
340 const char* insp_ntoa(insp_inaddr n)
341 {
342         static char buf[1024];
343         inet_ntop(AF_FAMILY, &n, buf, sizeof(buf));
344         return buf;
345 }
346
347 int insp_aton(const char* a, insp_inaddr* n)
348 {
349         return inet_pton(AF_FAMILY, a, n);
350 }
351