-/* +------------------------------------+\r * | Inspire Internet Relay Chat Daemon |\r * +------------------------------------+\r *\r * InspIRCd: (C) 2002-2007 InspIRCd Development Team\r * See: http://www.inspircd.org/wiki/index.php/Credits\r *\r * This program is free but copyrighted software; see\r * the file COPYING for details.\r *\r * ---------------------------------------------------\r */\r\r#include "inspircd.h"\r#include <string>\r#include "configreader.h"\r#include "socket.h"\r#include "socketengine.h"\r#include "wildcard.h"\r\rusing namespace irc::sockets;\r\r/* Used when comparing CIDR masks for the modulus bits left over.\r * A lot of ircd's seem to do this:\r * ((-1) << (8 - (mask % 8)))\r * But imho, it sucks in comparison to a nice neat lookup table.\r */\rconst unsigned char inverted_bits[8] = { 0x00, /* 00000000 - 0 bits - never actually used */\r 0x80, /* 10000000 - 1 bits */\r 0xC0, /* 11000000 - 2 bits */\r 0xE0, /* 11100000 - 3 bits */\r 0xF0, /* 11110000 - 4 bits */\r 0xF8, /* 11111000 - 5 bits */\r 0xFC, /* 11111100 - 6 bits */\r 0xFE /* 11111110 - 7 bits */\r};\r\r\rListenSocket::ListenSocket(InspIRCd* Instance, int port, char* addr) : ServerInstance(Instance), desc("plaintext"), bind_addr(addr), bind_port(port)\r{\r this->SetFd(OpenTCPSocket(addr));\r if (this->GetFd() > -1)\r {\r if (!Instance->BindSocket(this->fd,port,addr))\r this->fd = -1;\r#ifdef IPV6\r if ((!*addr) || (strchr(addr,':')))\r this->family = AF_INET6;\r else\r#endif\r this->family = AF_INET;\r Instance->SE->AddFd(this);\r }\r}\r\rListenSocket::~ListenSocket()\r{\r if (this->GetFd() > -1)\r {\r ServerInstance->SE->DelFd(this);\r ServerInstance->Log(DEBUG,"Shut down listener on fd %d", this->fd);\r if (shutdown(this->fd, 2) || close(this->fd))\r ServerInstance->Log(DEBUG,"Failed to cancel listener: %s", strerror(errno));\r this->fd = -1;\r }\r}\r\rvoid ListenSocket::HandleEvent(EventType et, int errornum)\r{\r sockaddr* sock_us = new sockaddr[2]; // our port number\r sockaddr* client = new sockaddr[2];\r socklen_t uslen, length; // length of our port number\r int incomingSockfd, in_port;\r\r#ifdef IPV6\r if (this->family == AF_INET6)\r {\r uslen = sizeof(sockaddr_in6);\r length = sizeof(sockaddr_in6);\r }\r else\r#endif\r {\r uslen = sizeof(sockaddr_in);\r length = sizeof(sockaddr_in);\r }\r\r incomingSockfd = _accept (this->GetFd(), (sockaddr*)client, &length);\r\r if ((incomingSockfd > -1) && (!_getsockname(incomingSockfd, sock_us, &uslen)))\r {\r char buf[MAXBUF];\r#ifdef IPV6\r if (this->family == AF_INET6)\r {\r inet_ntop(AF_INET6, &((const sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf));\r in_port = ntohs(((sockaddr_in6*)sock_us)->sin6_port);\r }\r else\r#endif\r {\r inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf));\r in_port = ntohs(((sockaddr_in*)sock_us)->sin_port);\r }\r\r NonBlocking(incomingSockfd);\r if (ServerInstance->Config->GetIOHook(in_port))\r {\r try\r {\r ServerInstance->Config->GetIOHook(in_port)->OnRawSocketAccept(incomingSockfd, buf, in_port);\r }\r catch (CoreException& modexcept)\r {\r ServerInstance->Log(DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());\r }\r }\r ServerInstance->stats->statsAccept++;\r userrec::AddClient(ServerInstance, incomingSockfd, in_port, false, this->family, client);\r }\r else\r {\r shutdown(incomingSockfd,2);\r close(incomingSockfd);\r ServerInstance->stats->statsRefused++;\r }\r delete[] client;\r delete[] sock_us;\r}\r\r/* Match raw bytes using CIDR bit matching, used by higher level MatchCIDR() */\rbool irc::sockets::MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits)\r{\r unsigned int modulus = mask_bits % 8; /* Number of whole bytes in the mask */\r unsigned int divisor = mask_bits / 8; /* Remaining bits in the mask after whole bytes are dealt with */\r\r /* First compare the whole bytes, if they dont match, return false */\r if (memcmp(address, mask, divisor))\r return false;\r\r /* Now if there are any remainder bits, we compare them with logic AND */\r if (modulus)\r if ((address[divisor] & inverted_bits[modulus]) != (mask[divisor] & inverted_bits[modulus]))\r /* If they dont match, return false */\r return false;\r\r /* The address matches the mask, to mask_bits bits of mask */\r return true;\r}\r\r/* Match CIDR, but dont attempt to match() against leading *!*@ sections */\rbool irc::sockets::MatchCIDR(const char* address, const char* cidr_mask)\r{\r return MatchCIDR(address, cidr_mask, false);\r}\r\r/* 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\r * If you have a lot of hosts to match, youre probably better off building your mask once\r * and then using the lower level MatchCIDRBits directly.\r *\r * This will also attempt to match any leading usernames or nicknames on the mask, using\r * match(), when match_with_username is true.\r */\rbool irc::sockets::MatchCIDR(const char* address, const char* cidr_mask, bool match_with_username)\r{\r unsigned char addr_raw[16];\r unsigned char mask_raw[16];\r unsigned int bits = 0;\r char* mask = NULL;\r\r /* The caller is trying to match ident@<mask>/bits.\r * Chop off the ident@ portion, use match() on it\r * seperately.\r */\r if (match_with_username)\r {\r /* Duplicate the strings, and try to find the position\r * of the @ symbol in each */\r char* address_dupe = strdup(address);\r char* cidr_dupe = strdup(cidr_mask);\r \r /* Use strchr not strrchr, because its going to be nearer to the left */\r char* username_mask_pos = strrchr(cidr_dupe, '@');\r char* username_addr_pos = strrchr(address_dupe, '@');\r\r /* Both strings have an @ symbol in them */\r if (username_mask_pos && username_addr_pos)\r {\r /* Zero out the location of the @ symbol */\r *username_mask_pos = *username_addr_pos = 0;\r\r /* Try and match() the strings before the @\r * symbols, and recursively call MatchCIDR without\r * username matching enabled to match the host part.\r */\r bool result = (match(address_dupe, cidr_dupe) && MatchCIDR(username_addr_pos + 1, username_mask_pos + 1, false));\r\r /* Free the stuff we created */\r free(address_dupe);\r free(cidr_dupe);\r\r /* Return a result */\r return result;\r }\r else\r {\r /* One or both didnt have an @ in,\r * just match as CIDR\r */\r free(address_dupe);\r free(cidr_dupe);\r mask = strdup(cidr_mask);\r }\r }\r else\r {\r /* Make a copy of the cidr mask string,\r * we're going to change it\r */\r mask = strdup(cidr_mask);\r }\r\r in_addr address_in4;\r in_addr mask_in4;\r\r\r /* Use strrchr for this, its nearer to the right */\r char* bits_chars = strrchr(mask,'/');\r\r if (bits_chars)\r {\r bits = atoi(bits_chars + 1);\r *bits_chars = 0;\r }\r else\r {\r /* No 'number of bits' field! */\r free(mask);\r return false;\r }\r\r#ifdef SUPPORT_IP6LINKS\r in6_addr address_in6;\r in6_addr mask_in6;\r\r if (inet_pton(AF_INET6, address, &address_in6) > 0)\r {\r if (inet_pton(AF_INET6, mask, &mask_in6) > 0)\r {\r memcpy(&addr_raw, &address_in6.s6_addr, 16);\r memcpy(&mask_raw, &mask_in6.s6_addr, 16);\r\r if (bits > 128)\r bits = 128;\r }\r else\r {\r /* The address was valid ipv6, but the mask\r * that goes with it wasnt.\r */\r free(mask);\r return false;\r }\r }\r else\r#endif\r if (inet_pton(AF_INET, address, &address_in4) > 0)\r {\r if (inet_pton(AF_INET, mask, &mask_in4) > 0)\r {\r memcpy(&addr_raw, &address_in4.s_addr, 4);\r memcpy(&mask_raw, &mask_in4.s_addr, 4);\r\r if (bits > 32)\r bits = 32;\r }\r else\r {\r /* The address was valid ipv4,\r * but the mask that went with it wasnt.\r */\r free(mask);\r return false;\r }\r }\r else\r {\r /* The address was neither ipv4 or ipv6 */\r free(mask);\r return false;\r }\r\r /* Low-level-match the bits in the raw data */\r free(mask);\r return MatchCIDRBits(addr_raw, mask_raw, bits);\r}\r\rvoid irc::sockets::Blocking(int s)\r{\r#ifndef WIN32\r int flags = fcntl(s, F_GETFL, 0);\r fcntl(s, F_SETFL, flags ^ O_NONBLOCK);\r#else\r unsigned long opt = 0;\r ioctlsocket(s, FIONBIO, &opt);\r#endif\r}\r\rvoid irc::sockets::NonBlocking(int s)\r{\r#ifndef WIN32\r int flags = fcntl(s, F_GETFL, 0);\r fcntl(s, F_SETFL, flags | O_NONBLOCK);\r#else\r unsigned long opt = 1;\r ioctlsocket(s, FIONBIO, &opt);\r#endif\r}\r\r/** This will bind a socket to a port. It works for UDP/TCP.\r * It can only bind to IP addresses, if you wish to bind to hostnames\r * you should first resolve them using class 'Resolver'.\r */ \rbool InspIRCd::BindSocket(int sockfd, int port, char* addr, bool dolisten)\r{\r /* We allocate 2 of these, because sockaddr_in6 is larger than sockaddr (ugh, hax) */\r sockaddr* server = new sockaddr[2];\r memset(server,0,sizeof(sockaddr)*2);\r\r int ret, size;\r\r if (*addr == '*')\r *addr = 0;\r\r#ifdef IPV6\r if (*addr)\r {\r /* There is an address here. Is it ipv6? */\r if (strchr(addr,':'))\r {\r /* Yes it is */\r in6_addr addy;\r if (inet_pton(AF_INET6, addr, &addy) < 1)\r {\r delete[] server;\r return false;\r }\r\r ((sockaddr_in6*)server)->sin6_family = AF_INET6;\r memcpy(&(((sockaddr_in6*)server)->sin6_addr), &addy, sizeof(in6_addr));\r ((sockaddr_in6*)server)->sin6_port = htons(port);\r size = sizeof(sockaddr_in6);\r }\r else\r {\r /* No, its not */\r in_addr addy;\r if (inet_pton(AF_INET, addr, &addy) < 1)\r {\r delete[] server;\r return false;\r }\r\r ((sockaddr_in*)server)->sin_family = AF_INET;\r ((sockaddr_in*)server)->sin_addr = addy;\r ((sockaddr_in*)server)->sin_port = htons(port);\r size = sizeof(sockaddr_in);\r }\r }\r else\r {\r if (port == -1)\r {\r /* Port -1: Means UDP IPV4 port binding - Special case\r * used by DNS engine.\r */\r ((sockaddr_in*)server)->sin_family = AF_INET;\r ((sockaddr_in*)server)->sin_addr.s_addr = htonl(INADDR_ANY);\r ((sockaddr_in*)server)->sin_port = 0;\r size = sizeof(sockaddr_in);\r }\r else\r {\r /* Theres no address here, default to ipv6 bind to all */\r ((sockaddr_in6*)server)->sin6_family = AF_INET6;\r memset(&(((sockaddr_in6*)server)->sin6_addr), 0, sizeof(in6_addr));\r ((sockaddr_in6*)server)->sin6_port = htons(port);\r size = sizeof(sockaddr_in6);\r }\r }\r#else\r /* If we aren't built with ipv6, the choice becomes simple */\r ((sockaddr_in*)server)->sin_family = AF_INET;\r if (*addr)\r {\r /* There is an address here. */\r in_addr addy;\r if (inet_pton(AF_INET, addr, &addy) < 1)\r {\r delete[] server;\r return false;\r }\r ((sockaddr_in*)server)->sin_addr = addy;\r }\r else\r {\r /* Bind ipv4 to all */\r ((sockaddr_in*)server)->sin_addr.s_addr = htonl(INADDR_ANY);\r }\r /* Bind ipv4 port number */\r ((sockaddr_in*)server)->sin_port = htons(port);\r size = sizeof(sockaddr_in);\r#endif\r ret = bind(sockfd, server, size);\r delete[] server;\r\r if (ret < 0)\r {\r return false;\r }\r else\r {\r if (dolisten)\r {\r if (listen(sockfd, Config->MaxConn) == -1)\r {\r this->Log(DEFAULT,"ERROR in listen(): %s",strerror(errno));\r return false;\r }\r else\r {\r this->Log(DEBUG,"New socket binding for %d with listen: %s:%d", sockfd, addr, port);\r NonBlocking(sockfd);\r return true;\r }\r }\r else\r {\r this->Log(DEBUG,"New socket binding for %d without listen: %s:%d", sockfd, addr, port);\r return true;\r }\r }\r}\r\r// Open a TCP Socket\rint irc::sockets::OpenTCPSocket(char* addr, int socktype)\r{\r int sockfd;\r int on = 1;\r struct linger linger = { 0 };\r#ifdef IPV6\r if (strchr(addr,':') || (!*addr))\r sockfd = socket (PF_INET6, socktype, 0);\r else\r sockfd = socket (PF_INET, socktype, 0);\r if (sockfd < 0)\r#else\r if ((sockfd = socket (PF_INET, socktype, 0)) < 0)\r#endif\r {\r return ERROR;\r }\r else\r {\r setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));\r /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */\r linger.l_onoff = 1;\r linger.l_linger = 1;\r setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (char*)&linger,sizeof(linger));\r return (sockfd);\r }\r}\r\r/* XXX: Probably belongs in class InspIRCd */\rint InspIRCd::BindPorts(bool bail, int &ports_found, FailedPortList &failed_ports)\r{\r char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];\r int bound = 0;\r bool started_with_nothing = (Config->ports.size() == 0);\r std::vector<std::pair<std::string, int> > old_ports;\r\r /* XXX: Make a copy of the old ip/port pairs here */\r for (std::vector<ListenSocket*>::iterator o = Config->ports.begin(); o != Config->ports.end(); ++o)\r old_ports.push_back(make_pair((*o)->GetIP(), (*o)->GetPort()));\r\r for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)\r {\r Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);\r Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);\r Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);\r\r if ((!*Type) || (!strcmp(Type,"clients")))\r {\r irc::portparser portrange(configToken, false);\r int portno = -1;\r while ((portno = portrange.GetToken()))\r {\r if (*Addr == '*')\r *Addr = 0;\r\r bool skip = false;\r for (std::vector<ListenSocket*>::iterator n = Config->ports.begin(); n != Config->ports.end(); ++n)\r {\r if (((*n)->GetIP() == Addr) && ((*n)->GetPort() == portno))\r {\r skip = true;\r /* XXX: Here, erase from our copy of the list */\r for (std::vector<std::pair<std::string, int> >::iterator k = old_ports.begin(); k != old_ports.end(); ++k)\r {\r if ((k->first == Addr) && (k->second == portno))\r {\r old_ports.erase(k);\r break;\r }\r }\r }\r }\r if (!skip)\r {\r ListenSocket* ll = new ListenSocket(this, portno, Addr);\r if (ll->GetFd() > -1)\r {\r bound++;\r Config->ports.push_back(ll);\r }\r else\r {\r failed_ports.push_back(std::make_pair(Addr, portno));\r }\r ports_found++;\r }\r }\r }\r }\r\r /* XXX: Here, anything left in our copy list, close as removed */\r if (!started_with_nothing)\r {\r for (size_t k = 0; k < old_ports.size(); ++k)\r {\r for (std::vector<ListenSocket*>::iterator n = Config->ports.begin(); n != Config->ports.end(); ++n)\r {\r if (((*n)->GetIP() == old_ports[k].first) && ((*n)->GetPort() == old_ports[k].second))\r {\r this->Log(DEFAULT,"Port binding %s:%d was removed from the config file, closing.", old_ports[k].first.c_str(), old_ports[k].second);\r delete *n;\r Config->ports.erase(n);\r break;\r }\r }\r }\r }\r\r return bound;\r}\r\rconst char* irc::sockets::insp_ntoa(insp_inaddr n)\r{\r static char buf[1024];\r inet_ntop(AF_FAMILY, &n, buf, sizeof(buf));\r return buf;\r}\r\rint irc::sockets::insp_aton(const char* a, insp_inaddr* n)\r{\r return inet_pton(AF_FAMILY, a, n);\r}\r\r
\ No newline at end of file
+/* +------------------------------------+
+ * | Inspire Internet Relay Chat Daemon |
+ * +------------------------------------+
+ *
+ * InspIRCd: (C) 2002-2008 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
+ * This program is free but copyrighted software; see
+ * the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
+
+/* $Core: libIRCDsocket */
+
+#include "inspircd.h"
+#include "socket.h"
+#include "socketengine.h"
+
+/* Private static member data must be initialized in this manner */
+unsigned int ListenSocket::socketcount = 0;
+sockaddr* ListenSocket::sock_us = NULL;
+sockaddr* ListenSocket::client = NULL;
+sockaddr* ListenSocket::raddr = NULL;
+
+ListenSocket::ListenSocket(InspIRCd* Instance, int port, char* addr) : ServerInstance(Instance), desc("plaintext"), bind_addr(addr), bind_port(port)
+{
+ this->SetFd(irc::sockets::OpenTCPSocket(addr));
+ if (this->GetFd() > -1)
+ {
+ if (!Instance->BindSocket(this->fd,port,addr))
+ this->fd = -1;
+#ifdef IPV6
+ if ((!*addr) || (strchr(addr,':')))
+ this->family = AF_INET6;
+ else
+#endif
+ this->family = AF_INET;
+ Instance->SE->AddFd(this);
+ }
+ /* Saves needless allocations */
+ if (socketcount == 0)
+ {
+ /* All instances of ListenSocket share these, so reference count it */
+ ServerInstance->Logs->Log("SOCKET", DEBUG,"Allocate sockaddr structures");
+ sock_us = new sockaddr[2];
+ client = new sockaddr[2];
+ raddr = new sockaddr[2];
+ }
+ socketcount++;
+}
+
+ListenSocket::~ListenSocket()
+{
+ if (this->GetFd() > -1)
+ {
+ ServerInstance->SE->DelFd(this);
+ ServerInstance->Logs->Log("SOCKET", DEBUG,"Shut down listener on fd %d", this->fd);
+ if (ServerInstance->SE->Shutdown(this, 2) || ServerInstance->SE->Close(this))
+ ServerInstance->Logs->Log("SOCKET", DEBUG,"Failed to cancel listener: %s", strerror(errno));
+ this->fd = -1;
+ }
+ socketcount--;
+ if (socketcount == 0)
+ {
+ delete[] sock_us;
+ delete[] client;
+ delete[] raddr;
+ }
+}
+
+void ListenSocket::HandleEvent(EventType e, int err)
+{
+ switch (e)
+ {
+ case EVENT_ERROR:
+ ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err));
+ break;
+ case EVENT_WRITE:
+ ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!");
+ break;
+ case EVENT_READ:
+ {
+ ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket");
+ socklen_t uslen, length; // length of our port number
+ int incomingSockfd, in_port;
+
+#ifdef IPV6
+ if (this->family == AF_INET6)
+ {
+ uslen = sizeof(sockaddr_in6);
+ length = sizeof(sockaddr_in6);
+ }
+ else
+#endif
+ {
+ uslen = sizeof(sockaddr_in);
+ length = sizeof(sockaddr_in);
+ }
+
+ incomingSockfd = ServerInstance->SE->Accept(this, (sockaddr*)client, &length);
+
+ if ((incomingSockfd > -1) && (!ServerInstance->SE->GetSockName(this, sock_us, &uslen)))
+ {
+ char buf[MAXBUF];
+ char target[MAXBUF];
+
+ *target = *buf = '\0';
+
+#ifdef IPV6
+ if (this->family == AF_INET6)
+ {
+ in_port = ntohs(((sockaddr_in6*)sock_us)->sin6_port);
+ inet_ntop(AF_INET6, &((const sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf));
+ socklen_t raddrsz = sizeof(sockaddr_in6);
+ if (getsockname(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
+ inet_ntop(AF_INET6, &((const sockaddr_in6*)raddr)->sin6_addr, target, sizeof(target));
+ else
+ ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
+ }
+ else
+#endif
+ {
+ inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf));
+ in_port = ntohs(((sockaddr_in*)sock_us)->sin_port);
+ socklen_t raddrsz = sizeof(sockaddr_in);
+ if (getsockname(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
+ inet_ntop(AF_INET, &((const sockaddr_in*)raddr)->sin_addr, target, sizeof(target));
+ else
+ ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
+ }
+ ServerInstance->SE->NonBlocking(incomingSockfd);
+ ServerInstance->stats->statsAccept++;
+ ServerInstance->Users->AddUser(ServerInstance, incomingSockfd, in_port, false, this->family, client, target);
+ }
+ else
+ {
+ ServerInstance->SE->Shutdown(incomingSockfd, 2);
+ ServerInstance->SE->Close(incomingSockfd);
+ ServerInstance->stats->statsRefused++;
+ }
+ }
+ break;
+ }
+}
+
+/** This will bind a socket to a port. It works for UDP/TCP.
+ * It can only bind to IP addresses, if you wish to bind to hostnames
+ * you should first resolve them using class 'Resolver'.
+ */
+bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten)
+{
+ /* We allocate 2 of these, because sockaddr_in6 is larger than sockaddr (ugh, hax) */
+ sockaddr* servaddr = new sockaddr[2];
+ memset(servaddr,0,sizeof(sockaddr)*2);
+
+ int ret, size;
+
+ if (*addr == '*')
+ addr = "";
+
+#ifdef IPV6
+ if (*addr)
+ {
+ /* There is an address here. Is it ipv6? */
+ if (strchr(addr,':'))
+ {
+ /* Yes it is */
+ in6_addr addy;
+ if (inet_pton(AF_INET6, addr, &addy) < 1)
+ {
+ delete[] servaddr;
+ return false;
+ }
+
+ ((sockaddr_in6*)servaddr)->sin6_family = AF_INET6;
+ memcpy(&(((sockaddr_in6*)servaddr)->sin6_addr), &addy, sizeof(in6_addr));
+ ((sockaddr_in6*)servaddr)->sin6_port = htons(port);
+ size = sizeof(sockaddr_in6);
+ }
+ else
+ {
+ /* No, its not */
+ in_addr addy;
+ if (inet_pton(AF_INET, addr, &addy) < 1)
+ {
+ delete[] servaddr;
+ return false;
+ }
+
+ ((sockaddr_in*)servaddr)->sin_family = AF_INET;
+ ((sockaddr_in*)servaddr)->sin_addr = addy;
+ ((sockaddr_in*)servaddr)->sin_port = htons(port);
+ size = sizeof(sockaddr_in);
+ }
+ }
+ else
+ {
+ if (port == -1)
+ {
+ /* Port -1: Means UDP IPV4 port binding - Special case
+ * used by DNS engine.
+ */
+ ((sockaddr_in*)servaddr)->sin_family = AF_INET;
+ ((sockaddr_in*)servaddr)->sin_addr.s_addr = htonl(INADDR_ANY);
+ ((sockaddr_in*)servaddr)->sin_port = 0;
+ size = sizeof(sockaddr_in);
+ }
+ else
+ {
+ /* Theres no address here, default to ipv6 bind to all */
+ ((sockaddr_in6*)servaddr)->sin6_family = AF_INET6;
+ memset(&(((sockaddr_in6*)servaddr)->sin6_addr), 0, sizeof(in6_addr));
+ ((sockaddr_in6*)servaddr)->sin6_port = htons(port);
+ size = sizeof(sockaddr_in6);
+ }
+ }
+#else
+ /* If we aren't built with ipv6, the choice becomes simple */
+ ((sockaddr_in*)servaddr)->sin_family = AF_INET;
+ if (*addr)
+ {
+ /* There is an address here. */
+ in_addr addy;
+ if (inet_pton(AF_INET, addr, &addy) < 1)
+ {
+ delete[] servaddr;
+ return false;
+ }
+ ((sockaddr_in*)servaddr)->sin_addr = addy;
+ }
+ else
+ {
+ /* Bind ipv4 to all */
+ ((sockaddr_in*)servaddr)->sin_addr.s_addr = htonl(INADDR_ANY);
+ }
+ /* Bind ipv4 port number */
+ ((sockaddr_in*)servaddr)->sin_port = htons(port);
+ size = sizeof(sockaddr_in);
+#endif
+ ret = SE->Bind(sockfd, servaddr, size);
+ delete[] servaddr;
+
+ if (ret < 0)
+ {
+ return false;
+ }
+ else
+ {
+ if (dolisten)
+ {
+ if (SE->Listen(sockfd, Config->MaxConn) == -1)
+ {
+ this->Logs->Log("SOCKET",DEFAULT,"ERROR in listen(): %s",strerror(errno));
+ return false;
+ }
+ else
+ {
+ this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d with listen: %s:%d", sockfd, addr, port);
+ SE->NonBlocking(sockfd);
+ return true;
+ }
+ }
+ else
+ {
+ this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d without listen: %s:%d", sockfd, addr, port);
+ return true;
+ }
+ }
+}
+
+// Open a TCP Socket
+int irc::sockets::OpenTCPSocket(char* addr, int socktype)
+{
+ int sockfd;
+ int on = 1;
+ addr = addr;
+ struct linger linger = { 0, 0 };
+#ifdef IPV6
+ if (strchr(addr,':') || (!*addr))
+ sockfd = socket (PF_INET6, socktype, 0);
+ else
+ sockfd = socket (PF_INET, socktype, 0);
+ if (sockfd < 0)
+#else
+ if ((sockfd = socket (PF_INET, socktype, 0)) < 0)
+#endif
+ {
+ return ERROR;
+ }
+ else
+ {
+ setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
+ /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
+ linger.l_onoff = 1;
+ linger.l_linger = 1;
+ setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (char*)&linger,sizeof(linger));
+ return (sockfd);
+ }
+}
+
+int InspIRCd::BindPorts(bool, int &ports_found, FailedPortList &failed_ports)
+{
+ char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
+ int bound = 0;
+ bool started_with_nothing = (Config->ports.size() == 0);
+ std::vector<std::pair<std::string, int> > old_ports;
+
+ /* XXX: Make a copy of the old ip/port pairs here */
+ for (std::vector<ListenSocket*>::iterator o = Config->ports.begin(); o != Config->ports.end(); ++o)
+ old_ports.push_back(make_pair((*o)->GetIP(), (*o)->GetPort()));
+
+ for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
+ {
+ Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
+ Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
+ Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
+
+ if (strncmp(Addr, "::ffff:", 7) == 0)
+ this->Logs->Log("SOCKET",DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
+
+ if ((!*Type) || (!strcmp(Type,"clients")))
+ {
+ irc::portparser portrange(configToken, false);
+ int portno = -1;
+ while (0 != (portno = portrange.GetToken()))
+ {
+ if (*Addr == '*')
+ *Addr = 0;
+
+ bool skip = false;
+ for (std::vector<ListenSocket*>::iterator n = Config->ports.begin(); n != Config->ports.end(); ++n)
+ {
+ if (((*n)->GetIP() == Addr) && ((*n)->GetPort() == portno))
+ {
+ skip = true;
+ /* XXX: Here, erase from our copy of the list */
+ for (std::vector<std::pair<std::string, int> >::iterator k = old_ports.begin(); k != old_ports.end(); ++k)
+ {
+ if ((k->first == Addr) && (k->second == portno))
+ {
+ old_ports.erase(k);
+ break;
+ }
+ }
+ }
+ }
+ if (!skip)
+ {
+ ListenSocket* ll = new ListenSocket(this, portno, Addr);
+ if (ll->GetFd() > -1)
+ {
+ bound++;
+ Config->ports.push_back(ll);
+ }
+ else
+ {
+ failed_ports.push_back(std::make_pair(Addr, portno));
+ }
+ ports_found++;
+ }
+ }
+ }
+ }
+
+ /* XXX: Here, anything left in our copy list, close as removed */
+ if (!started_with_nothing)
+ {
+ for (size_t k = 0; k < old_ports.size(); ++k)
+ {
+ for (std::vector<ListenSocket*>::iterator n = Config->ports.begin(); n != Config->ports.end(); ++n)
+ {
+ if (((*n)->GetIP() == old_ports[k].first) && ((*n)->GetPort() == old_ports[k].second))
+ {
+ this->Logs->Log("SOCKET",DEFAULT,"Port binding %s:%d was removed from the config file, closing.", old_ports[k].first.c_str(), old_ports[k].second);
+ delete *n;
+ Config->ports.erase(n);
+ break;
+ }
+ }
+ }
+ }
+
+ return bound;
+}
+
+const char* irc::sockets::insp_ntoa(insp_inaddr n)
+{
+ static char buf[1024];
+ inet_ntop(AF_FAMILY, &n, buf, sizeof(buf));
+ return buf;
+}
+
+int irc::sockets::insp_aton(const char* a, insp_inaddr* n)
+{
+ return inet_pton(AF_FAMILY, a, n);
+}
+
+