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"
26 extern InspIRCd* ServerInstance;
27 extern ServerConfig* Config;
30 /* Used when comparing CIDR masks for the modulus bits left over */
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 */
42 /* Match raw bytes using CIDR bit matching, used by higher level MatchCIDR() */
43 bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits)
45 unsigned int modulus = mask_bits % 8; /* Number of whole bytes in the mask */
46 unsigned int divisor = mask_bits / 8; /* Remaining bits in the mask after whole bytes are dealt with */
48 /* We shouldnt match anything, /0 is always valid */
52 /* First compare the whole bytes, if they dont match, return false */
53 if (memcmp(address, mask, divisor))
56 /* Now if there are any remainder bits, we compare them with logic AND */
58 if ((address[divisor] & inverted_bits[modulus]) != (mask[divisor] & inverted_bits[modulus]))
59 /* If they dont match, return false */
62 /* The address matches the mask, to mask_bits bits of mask */
66 /* 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
67 * If you have a lot of hosts to match, youre probably better off building your mask once
68 * and then using the lower level MatchCIDRBits directly.
70 bool MatchCIDR(const char* address, const char* cidr_mask)
72 unsigned char addr_raw[16];
73 unsigned char mask_raw[16];
74 unsigned int bits = 0;
75 char* mask = strdup(cidr_mask);
80 char* bits_chars = strchr(mask,'/');
84 bits = atoi(bits_chars + 1);
89 /* No 'number of bits' field! */
93 #ifdef SUPPORT_IP6LINKS
97 if (inet_pton(AF_INET6, address, &address_in6) > 0)
99 if (inet_pton(AF_INET6, mask, &mask_in6) > 0)
101 memcpy(&addr_raw, &address_in6.s6_addr, 16);
102 memcpy(&mask_raw, &mask_in6.s6_addr, 16);
115 if (inet_pton(AF_INET, address, &address_in4) > 0)
117 if (inet_pton(AF_INET, mask, &mask_in4) > 0)
119 memcpy(&addr_raw, &address_in4.s_addr, 4);
120 memcpy(&mask_raw, &mask_in4.s_addr, 4);
138 return MatchCIDRBits(addr_raw, mask_raw, bits);
141 /** This will bind a socket to a port. It works for UDP/TCP.
142 * It can only bind to IP addresses, if you wish to bind to hostnames
143 * you should first resolve them using class 'Resolver'.
145 bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr)
147 memset(&server,0,sizeof(server));
153 if ((*addr) && (insp_aton(addr,&addy) < 1))
155 log(DEBUG,"Invalid IP '%s' given to BindSocket()", addr);
160 server.sin6_family = AF_FAMILY;
162 server.sin_family = AF_FAMILY;
167 memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
169 server.sin_addr.s_addr = htonl(INADDR_ANY);
175 memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
177 server.sin_addr = addy;
181 server.sin6_port = htons(port);
183 server.sin_port = htons(port);
185 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server)) < 0)
191 log(DEBUG,"Bound port %s:%d",*addr ? addr : "*",port);
192 if (listen(sockfd, Config->MaxConn) == -1)
194 log(DEFAULT,"ERROR in listen(): %s",strerror(errno));
211 struct linger linger = { 0 };
213 if ((sockfd = socket (AF_FAMILY, SOCK_STREAM, 0)) < 0)
215 log(DEFAULT,"Error creating TCP socket: %s",strerror(errno));
220 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
221 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
224 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger,sizeof(linger));
229 bool HasPort(int port, char* addr)
231 for (unsigned long count = 0; count < ServerInstance->stats->BoundPortCount; count++)
233 if ((port == Config->ports[count]) && (!strcasecmp(Config->addrs[count],addr)))
241 int BindPorts(bool bail)
243 char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
244 insp_sockaddr client, server;
245 int clientportcount = 0;
246 int BoundPortCount = 0;
250 int InitialPortCount = ServerInstance->stats->BoundPortCount;
251 log(DEBUG,"Initial port count: %d",InitialPortCount);
253 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
255 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
256 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
257 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
259 if (((!*Type) || (!strcmp(Type,"clients"))) && (!HasPort(atoi(configToken),Addr)))
261 // modules handle server bind types now
262 Config->ports[clientportcount+InitialPortCount] = atoi(configToken);
266 strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
268 log(DEBUG,"NEW binding %s:%s [%s] from config",Addr,configToken, Type);
271 int PortCount = clientportcount;
274 for (int count = InitialPortCount; count < InitialPortCount + PortCount; count++)
276 if ((Config->openSockfd[count] = OpenTCPSocket()) == ERROR)
278 log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[count],Config->addrs[count],Config->ports[count]);
281 if (!BindSocket(Config->openSockfd[count],client,server,Config->ports[count],Config->addrs[count]))
283 log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
287 /* Associate the new open port with a slot in the socket engine */
288 if (Config->openSockfd[count] > -1)
290 ServerInstance->SE->AddFd(Config->openSockfd[count],true,X_LISTEN);
295 return InitialPortCount + BoundPortCount;
299 log(DEBUG,"There is nothing new to bind!");
301 return InitialPortCount;
304 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
306 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
307 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
308 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
310 if ((!*Type) || (!strcmp(Type,"clients")))
312 // modules handle server bind types now
313 Config->ports[clientportcount] = atoi(configToken);
315 // If the client put bind "*", this is an unrealism.
316 // We don't actually support this as documented, but
317 // i got fed up of people trying it, so now it converts
318 // it to an empty string meaning the same 'bind to all'.
322 strlcpy(Config->addrs[clientportcount],Addr,256);
324 log(DEBUG,"Binding %s:%s [%s] from config",Addr,configToken, Type);
328 int PortCount = clientportcount;
330 for (int count = 0; count < PortCount; count++)
332 if ((Config->openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
334 log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[BoundPortCount],Config->addrs[count],Config->ports[count]);
338 if (!BindSocket(Config->openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]))
340 log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
344 /* well we at least bound to one socket so we'll continue */
349 /* if we didn't bind to anything then abort */
352 log(DEFAULT,"No ports bound, bailing!");
353 printf("\nERROR: Could not bind any of %d ports! Please check your configuration.\n\n", PortCount);
357 return BoundPortCount;
360 const char* insp_ntoa(insp_inaddr n)
362 static char buf[1024];
363 inet_ntop(AF_FAMILY, &n, buf, sizeof(buf));
367 int insp_aton(const char* a, insp_inaddr* n)
369 return inet_pton(AF_FAMILY, a, n);