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;
30 using namespace irc::sockets;
32 /* Used when comparing CIDR masks for the modulus bits left over.
33 * A lot of ircd's seem to do this:
34 * ((-1) << (8 - (mask % 8)))
35 * But imho, it sucks in comparison to a nice neat lookup table.
37 const char inverted_bits[8] = { 0x00, /* 00000000 - 0 bits - never actually used */
38 0x80, /* 10000000 - 1 bits */
39 0xC0, /* 11000000 - 2 bits */
40 0xE0, /* 11100000 - 3 bits */
41 0xF0, /* 11110000 - 4 bits */
42 0xF8, /* 11111000 - 5 bits */
43 0xFC, /* 11111100 - 6 bits */
44 0xFE /* 11111110 - 7 bits */
47 /* Match raw bytes using CIDR bit matching, used by higher level MatchCIDR() */
48 bool irc::sockets::MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits)
50 unsigned int modulus = mask_bits % 8; /* Number of whole bytes in the mask */
51 unsigned int divisor = mask_bits / 8; /* Remaining bits in the mask after whole bytes are dealt with */
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 /* Match CIDR, but dont attempt to match() against leading *!*@ sections */
68 bool irc::sockets::MatchCIDR(const char* address, const char* cidr_mask)
70 return MatchCIDR(address, cidr_mask, false);
73 /* 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
74 * If you have a lot of hosts to match, youre probably better off building your mask once
75 * and then using the lower level MatchCIDRBits directly.
77 * This will also attempt to match any leading usernames or nicknames on the mask, using
78 * match(), when match_with_username is true.
80 bool irc::sockets::MatchCIDR(const char* address, const char* cidr_mask, bool match_with_username)
82 unsigned char addr_raw[16];
83 unsigned char mask_raw[16];
84 unsigned int bits = 0;
87 /* The caller is trying to match ident@<mask>/bits.
88 * Chop off the ident@ portion, use match() on it
91 if (match_with_username)
93 /* Duplicate the strings, and try to find the position
94 * of the @ symbol in each */
95 char* address_dupe = strdup(address);
96 char* cidr_dupe = strdup(cidr_mask);
98 /* Use strchr not strrchr, because its going to be nearer to the left */
99 char* username_mask_pos = strrchr(cidr_dupe, '@');
100 char* username_addr_pos = strrchr(address_dupe, '@');
102 /* Both strings have an @ symbol in them */
103 if (username_mask_pos && username_addr_pos)
105 /* Zero out the location of the @ symbol */
106 *username_mask_pos = *username_addr_pos = 0;
108 /* Try and match() the strings before the @
109 * symbols, and recursively call MatchCIDR without
110 * username matching enabled to match the host part.
112 bool result = (match(address_dupe, cidr_dupe) && MatchCIDR(username_addr_pos + 1, username_mask_pos + 1, false));
114 /* Free the stuff we created */
118 /* Return a result */
123 /* One or both didnt have an @ in,
128 mask = strdup(cidr_mask);
133 /* Make a copy of the cidr mask string,
134 * we're going to change it
136 mask = strdup(cidr_mask);
143 /* Use strrchr for this, its nearer to the right */
144 char* bits_chars = strrchr(mask,'/');
148 bits = atoi(bits_chars + 1);
153 /* No 'number of bits' field! */
157 #ifdef SUPPORT_IP6LINKS
158 in6_addr address_in6;
161 if (inet_pton(AF_INET6, address, &address_in6) > 0)
163 if (inet_pton(AF_INET6, mask, &mask_in6) > 0)
165 memcpy(&addr_raw, &address_in6.s6_addr, 16);
166 memcpy(&mask_raw, &mask_in6.s6_addr, 16);
173 /* The address was valid ipv6, but the mask
174 * that goes with it wasnt.
182 if (inet_pton(AF_INET, address, &address_in4) > 0)
184 if (inet_pton(AF_INET, mask, &mask_in4) > 0)
186 memcpy(&addr_raw, &address_in4.s_addr, 4);
187 memcpy(&mask_raw, &mask_in4.s_addr, 4);
194 /* The address was valid ipv4,
195 * but the mask that went with it wasnt.
203 /* The address was neither ipv4 or ipv6 */
208 /* Low-level-match the bits in the raw data */
210 return MatchCIDRBits(addr_raw, mask_raw, bits);
213 inline void irc::sockets::Blocking(int s)
215 int flags = fcntl(s, F_GETFL, 0);
216 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
219 inline void irc::sockets::NonBlocking(int s)
221 int flags = fcntl(s, F_GETFL, 0);
222 fcntl(s, F_SETFL, flags | O_NONBLOCK);
226 /** This will bind a socket to a port. It works for UDP/TCP.
227 * It can only bind to IP addresses, if you wish to bind to hostnames
228 * you should first resolve them using class 'Resolver'.
230 bool InspIRCd::BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr)
232 memset(&server,0,sizeof(server));
238 if ((*addr) && (insp_aton(addr,&addy) < 1))
240 log(DEBUG,"Invalid IP '%s' given to BindSocket()", addr);
245 server.sin6_family = AF_FAMILY;
247 server.sin_family = AF_FAMILY;
252 memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
254 server.sin_addr.s_addr = htonl(INADDR_ANY);
260 memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
262 server.sin_addr = addy;
266 server.sin6_port = htons(port);
268 server.sin_port = htons(port);
270 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server)) < 0)
276 log(DEBUG,"Bound port %s:%d",*addr ? addr : "*",port);
277 if (listen(sockfd, Config->MaxConn) == -1)
279 log(DEFAULT,"ERROR in listen(): %s",strerror(errno));
292 int irc::sockets::OpenTCPSocket()
296 struct linger linger = { 0 };
298 if ((sockfd = socket (AF_FAMILY, SOCK_STREAM, 0)) < 0)
300 log(DEFAULT,"Error creating TCP socket: %s",strerror(errno));
305 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
306 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
309 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger,sizeof(linger));
314 /* XXX: Probably belongs in class InspIRCd */
315 bool InspIRCd::HasPort(int port, char* addr)
317 for (unsigned long count = 0; count < stats->BoundPortCount; count++)
319 if ((port == Config->ports[count]) && (!strcasecmp(Config->addrs[count],addr)))
327 /* XXX: Probably belongs in class InspIRCd */
328 int InspIRCd::BindPorts(bool bail)
330 char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
331 insp_sockaddr client, server;
332 int clientportcount = 0;
333 int BoundPortCount = 0;
337 int InitialPortCount = stats->BoundPortCount;
338 log(DEBUG,"Initial port count: %d",InitialPortCount);
340 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
342 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
343 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
344 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
346 if (((!*Type) || (!strcmp(Type,"clients"))) && (!HasPort(atoi(configToken),Addr)))
348 // modules handle server bind types now
349 Config->ports[clientportcount+InitialPortCount] = atoi(configToken);
353 strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
355 log(DEBUG,"NEW binding %s:%s [%s] from config",Addr,configToken, Type);
358 int PortCount = clientportcount;
361 for (int count = InitialPortCount; count < InitialPortCount + PortCount; count++)
363 if ((Config->openSockfd[count] = OpenTCPSocket()) == ERROR)
365 log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[count],Config->addrs[count],Config->ports[count]);
369 if (!BindSocket(Config->openSockfd[count],client,server,Config->ports[count],Config->addrs[count]))
371 log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
375 /* Associate the new open port with a slot in the socket engine */
376 if (Config->openSockfd[count] > -1)
378 if (!SE->AddFd(Config->openSockfd[count],true,X_LISTEN))
380 log(DEFAULT,"ERK! Failed to add listening port to socket engine!");
381 shutdown(Config->openSockfd[count],2);
382 close(Config->openSockfd[count]);
390 return InitialPortCount + BoundPortCount;
394 log(DEBUG,"There is nothing new to bind!");
396 return InitialPortCount;
399 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
401 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
402 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
403 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
405 if ((!*Type) || (!strcmp(Type,"clients")))
407 // modules handle server bind types now
408 Config->ports[clientportcount] = atoi(configToken);
410 // If the client put bind "*", this is an unrealism.
411 // We don't actually support this as documented, but
412 // i got fed up of people trying it, so now it converts
413 // it to an empty string meaning the same 'bind to all'.
417 strlcpy(Config->addrs[clientportcount],Addr,256);
419 log(DEBUG,"Binding %s:%s [%s] from config",Addr,configToken, Type);
423 int PortCount = clientportcount;
425 for (int count = 0; count < PortCount; count++)
427 if ((Config->openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
429 log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[BoundPortCount],Config->addrs[count],Config->ports[count]);
433 if (!BindSocket(Config->openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]))
435 log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
439 /* well we at least bound to one socket so we'll continue */
444 return BoundPortCount;
447 const char* irc::sockets::insp_ntoa(insp_inaddr n)
449 static char buf[1024];
450 inet_ntop(AF_FAMILY, &n, buf, sizeof(buf));
454 int irc::sockets::insp_aton(const char* a, insp_inaddr* n)
456 return inet_pton(AF_FAMILY, a, n);