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 /* First compare the whole bytes, if they dont match, return false */
49 if (memcmp(address, mask, divisor))
52 /* Now if there are any remainder bits, we compare them with logic AND */
54 if ((address[divisor] & inverted_bits[modulus]) != (mask[divisor] & inverted_bits[modulus]))
55 /* If they dont match, return false */
58 /* The address matches the mask, to mask_bits bits of mask */
62 /* 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
63 * If you have a lot of hosts to match, youre probably better off building your mask once
64 * and then using the lower level MatchCIDRBits directly.
66 bool MatchCIDR(const char* address, const char* cidr_mask)
68 unsigned char addr_raw[16];
69 unsigned char mask_raw[16];
70 unsigned int bits = 0;
71 char* mask = strdup(cidr_mask);
76 char* bits_chars = strchr(mask,'/');
80 bits = atoi(bits_chars + 1);
85 /* No 'number of bits' field! */
89 #ifdef SUPPORT_IP6LINKS
93 if (inet_pton(AF_INET6, address, &address_in6) > 0)
95 if (inet_pton(AF_INET6, mask, &mask_in6) > 0)
97 memcpy(&addr_raw, &address_in6.s6_addr, 16);
98 memcpy(&mask_raw, &mask_in6.s6_addr, 16);
111 if (inet_pton(AF_INET, address, &address_in4) > 0)
113 if (inet_pton(AF_INET, mask, &mask_in4) > 0)
115 memcpy(&addr_raw, &address_in4.s_addr, 4);
116 memcpy(&mask_raw, &mask_in4.s_addr, 4);
134 return MatchCIDRBits(addr_raw, mask_raw, bits);
137 /** This will bind a socket to a port. It works for UDP/TCP.
138 * It can only bind to IP addresses, if you wish to bind to hostnames
139 * you should first resolve them using class 'Resolver'.
141 bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr)
143 memset(&server,0,sizeof(server));
149 if ((*addr) && (insp_aton(addr,&addy) < 1))
151 log(DEBUG,"Invalid IP '%s' given to BindSocket()", addr);
156 server.sin6_family = AF_FAMILY;
158 server.sin_family = AF_FAMILY;
163 memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
165 server.sin_addr.s_addr = htonl(INADDR_ANY);
171 memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
173 server.sin_addr = addy;
177 server.sin6_port = htons(port);
179 server.sin_port = htons(port);
181 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server)) < 0)
187 log(DEBUG,"Bound port %s:%d",*addr ? addr : "*",port);
188 if (listen(sockfd, Config->MaxConn) == -1)
190 log(DEFAULT,"ERROR in listen(): %s",strerror(errno));
207 struct linger linger = { 0 };
209 if ((sockfd = socket (AF_FAMILY, SOCK_STREAM, 0)) < 0)
211 log(DEFAULT,"Error creating TCP socket: %s",strerror(errno));
216 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
217 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
220 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger,sizeof(linger));
225 bool HasPort(int port, char* addr)
227 for (unsigned long count = 0; count < ServerInstance->stats->BoundPortCount; count++)
229 if ((port == Config->ports[count]) && (!strcasecmp(Config->addrs[count],addr)))
237 int BindPorts(bool bail)
239 char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
240 insp_sockaddr client, server;
241 int clientportcount = 0;
242 int BoundPortCount = 0;
246 int InitialPortCount = ServerInstance->stats->BoundPortCount;
247 log(DEBUG,"Initial port count: %d",InitialPortCount);
249 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
251 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
252 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
253 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
255 if (((!*Type) || (!strcmp(Type,"clients"))) && (!HasPort(atoi(configToken),Addr)))
257 // modules handle server bind types now
258 Config->ports[clientportcount+InitialPortCount] = atoi(configToken);
262 strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
264 log(DEBUG,"NEW binding %s:%s [%s] from config",Addr,configToken, Type);
267 int PortCount = clientportcount;
270 for (int count = InitialPortCount; count < InitialPortCount + PortCount; count++)
272 if ((Config->openSockfd[count] = OpenTCPSocket()) == ERROR)
274 log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[count],Config->addrs[count],Config->ports[count]);
277 if (!BindSocket(Config->openSockfd[count],client,server,Config->ports[count],Config->addrs[count]))
279 log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
283 /* Associate the new open port with a slot in the socket engine */
284 if (Config->openSockfd[count] > -1)
286 ServerInstance->SE->AddFd(Config->openSockfd[count],true,X_LISTEN);
291 return InitialPortCount + BoundPortCount;
295 log(DEBUG,"There is nothing new to bind!");
297 return InitialPortCount;
300 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
302 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
303 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
304 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
306 if ((!*Type) || (!strcmp(Type,"clients")))
308 // modules handle server bind types now
309 Config->ports[clientportcount] = atoi(configToken);
311 // If the client put bind "*", this is an unrealism.
312 // We don't actually support this as documented, but
313 // i got fed up of people trying it, so now it converts
314 // it to an empty string meaning the same 'bind to all'.
318 strlcpy(Config->addrs[clientportcount],Addr,256);
320 log(DEBUG,"Binding %s:%s [%s] from config",Addr,configToken, Type);
324 int PortCount = clientportcount;
326 for (int count = 0; count < PortCount; count++)
328 if ((Config->openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
330 log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[BoundPortCount],Config->addrs[count],Config->ports[count]);
334 if (!BindSocket(Config->openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]))
336 log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
340 /* well we at least bound to one socket so we'll continue */
345 /* if we didn't bind to anything then abort */
348 log(DEFAULT,"No ports bound, bailing!");
349 printf("\nERROR: Could not bind any of %d ports! Please check your configuration.\n\n", PortCount);
353 return BoundPortCount;
356 const char* insp_ntoa(insp_inaddr n)
358 static char buf[1024];
359 inet_ntop(AF_FAMILY, &n, buf, sizeof(buf));
363 int insp_aton(const char* a, insp_inaddr* n)
365 return inet_pton(AF_FAMILY, a, n);