1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
18 #include "configreader.h"
21 #include "inspstring.h"
22 #include "helperfuncs.h"
23 #include "socketengine.h"
27 extern InspIRCd* ServerInstance;
28 extern ServerConfig* Config;
31 /* Used when comparing CIDR masks for the modulus bits left over */
33 char inverted_bits[8] = { 0x00, /* 00000000 - 0 bits */
34 0x80, /* 10000000 - 1 bits */
35 0xC0, /* 11000000 - 2 bits */
36 0xE0, /* 11100000 - 3 bits */
37 0xF0, /* 11110000 - 4 bits */
38 0xF8, /* 11111000 - 5 bits */
39 0xFC, /* 11111100 - 6 bits */
40 0xFE /* 11111110 - 7 bits */
43 /* Match raw bytes using CIDR bit matching, used by higher level MatchCIDR() */
44 bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits)
46 unsigned int modulus = mask_bits % 8; /* Number of whole bytes in the mask */
47 unsigned int divisor = mask_bits / 8; /* Remaining bits in the mask after whole bytes are dealt with */
49 /* We shouldnt match anything, /0 is always valid */
53 /* First compare the whole bytes, if they dont match, return false */
54 if (memcmp(address, mask, divisor))
57 /* Now if there are any remainder bits, we compare them with logic AND */
59 if ((address[divisor] & inverted_bits[modulus]) != (mask[divisor] & inverted_bits[modulus]))
60 /* If they dont match, return false */
63 /* The address matches the mask, to mask_bits bits of mask */
67 bool MatchCIDR(const char* address, const char* cidr_mask)
69 return MatchCIDR(address, cidr_mask, false);
72 /* Match CIDR strings, e.g. 127.0.0.1 to 127.0.0.0/8 or 3ffe:1:5:6::8 to 3ffe:1::0/32
73 * If you have a lot of hosts to match, youre probably better off building your mask once
74 * and then using the lower level MatchCIDRBits directly.
76 bool MatchCIDR(const char* address, const char* cidr_mask, bool match_with_username)
78 unsigned char addr_raw[16];
79 unsigned char mask_raw[16];
80 unsigned int bits = 0;
83 /* The caller is trying to match ident@<mask>/bits.
84 * Chop off the ident@ portion, use match() on it
87 if (match_with_username)
89 /* Duplicate the strings, and try to find the position
90 * of the @ symbol in each */
91 char* address_dupe = strdup(address);
92 char* cidr_dupe = strdup(cidr_mask);
94 char* username_mask_pos = strchr(cidr_dupe, '@');
95 char* username_addr_pos = strchr(address_dupe, '@');
97 if (username_mask_pos && username_addr_pos)
99 *username_mask_pos = *username_addr_pos = 0;
101 bool result = (match(address_dupe, cidr_dupe) && MatchCIDR(username_addr_pos + 1, username_mask_pos + 1));
112 mask = strdup(cidr_mask);
117 mask = strdup(cidr_mask);
124 char* bits_chars = strchr(mask,'/');
128 bits = atoi(bits_chars + 1);
133 /* No 'number of bits' field! */
137 #ifdef SUPPORT_IP6LINKS
138 in6_addr address_in6;
141 if (inet_pton(AF_INET6, address, &address_in6) > 0)
143 if (inet_pton(AF_INET6, mask, &mask_in6) > 0)
145 memcpy(&addr_raw, &address_in6.s6_addr, 16);
146 memcpy(&mask_raw, &mask_in6.s6_addr, 16);
159 if (inet_pton(AF_INET, address, &address_in4) > 0)
161 if (inet_pton(AF_INET, mask, &mask_in4) > 0)
163 memcpy(&addr_raw, &address_in4.s_addr, 4);
164 memcpy(&mask_raw, &mask_in4.s_addr, 4);
182 return MatchCIDRBits(addr_raw, mask_raw, bits);
185 /** This will bind a socket to a port. It works for UDP/TCP.
186 * It can only bind to IP addresses, if you wish to bind to hostnames
187 * you should first resolve them using class 'Resolver'.
189 bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr)
191 memset(&server,0,sizeof(server));
197 if ((*addr) && (insp_aton(addr,&addy) < 1))
199 log(DEBUG,"Invalid IP '%s' given to BindSocket()", addr);
204 server.sin6_family = AF_FAMILY;
206 server.sin_family = AF_FAMILY;
211 memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
213 server.sin_addr.s_addr = htonl(INADDR_ANY);
219 memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
221 server.sin_addr = addy;
225 server.sin6_port = htons(port);
227 server.sin_port = htons(port);
229 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server)) < 0)
235 log(DEBUG,"Bound port %s:%d",*addr ? addr : "*",port);
236 if (listen(sockfd, Config->MaxConn) == -1)
238 log(DEFAULT,"ERROR in listen(): %s",strerror(errno));
255 struct linger linger = { 0 };
257 if ((sockfd = socket (AF_FAMILY, SOCK_STREAM, 0)) < 0)
259 log(DEFAULT,"Error creating TCP socket: %s",strerror(errno));
264 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
265 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
268 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger,sizeof(linger));
273 bool HasPort(int port, char* addr)
275 for (unsigned long count = 0; count < ServerInstance->stats->BoundPortCount; count++)
277 if ((port == Config->ports[count]) && (!strcasecmp(Config->addrs[count],addr)))
285 int BindPorts(bool bail)
287 char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
288 insp_sockaddr client, server;
289 int clientportcount = 0;
290 int BoundPortCount = 0;
294 int InitialPortCount = ServerInstance->stats->BoundPortCount;
295 log(DEBUG,"Initial port count: %d",InitialPortCount);
297 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
299 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
300 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
301 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
303 if (((!*Type) || (!strcmp(Type,"clients"))) && (!HasPort(atoi(configToken),Addr)))
305 // modules handle server bind types now
306 Config->ports[clientportcount+InitialPortCount] = atoi(configToken);
310 strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
312 log(DEBUG,"NEW binding %s:%s [%s] from config",Addr,configToken, Type);
315 int PortCount = clientportcount;
318 for (int count = InitialPortCount; count < InitialPortCount + PortCount; count++)
320 if ((Config->openSockfd[count] = OpenTCPSocket()) == ERROR)
322 log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[count],Config->addrs[count],Config->ports[count]);
325 if (!BindSocket(Config->openSockfd[count],client,server,Config->ports[count],Config->addrs[count]))
327 log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
331 /* Associate the new open port with a slot in the socket engine */
332 if (Config->openSockfd[count] > -1)
334 ServerInstance->SE->AddFd(Config->openSockfd[count],true,X_LISTEN);
339 return InitialPortCount + BoundPortCount;
343 log(DEBUG,"There is nothing new to bind!");
345 return InitialPortCount;
348 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
350 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
351 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
352 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
354 if ((!*Type) || (!strcmp(Type,"clients")))
356 // modules handle server bind types now
357 Config->ports[clientportcount] = atoi(configToken);
359 // If the client put bind "*", this is an unrealism.
360 // We don't actually support this as documented, but
361 // i got fed up of people trying it, so now it converts
362 // it to an empty string meaning the same 'bind to all'.
366 strlcpy(Config->addrs[clientportcount],Addr,256);
368 log(DEBUG,"Binding %s:%s [%s] from config",Addr,configToken, Type);
372 int PortCount = clientportcount;
374 for (int count = 0; count < PortCount; count++)
376 if ((Config->openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
378 log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[BoundPortCount],Config->addrs[count],Config->ports[count]);
382 if (!BindSocket(Config->openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]))
384 log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
388 /* well we at least bound to one socket so we'll continue */
393 /* if we didn't bind to anything then abort */
396 log(DEFAULT,"No ports bound, bailing!");
397 printf("\nERROR: Could not bind any of %d ports! Please check your configuration.\n\n", PortCount);
401 return BoundPortCount;
404 const char* insp_ntoa(insp_inaddr n)
406 static char buf[1024];
407 inet_ntop(AF_FAMILY, &n, buf, sizeof(buf));
411 int insp_aton(const char* a, insp_inaddr* n)
413 return inet_pton(AF_FAMILY, a, n);