1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 #include "socketengine.h"
19 using irc::sockets::sockaddrs;
21 /** This will bind a socket to a port. It works for UDP/TCP.
22 * It can only bind to IP addresses, if you wish to bind to hostnames
23 * you should first resolve them using class 'Resolver'.
25 bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten)
35 irc::sockets::aptosa(addr, port, &servaddr);
39 memset(&servaddr, 0, sizeof(servaddr));
42 /* Port -1: Means UDP IPV4 port binding - Special case
45 servaddr.in4.sin_family = AF_INET;
51 /* Default to ipv6 bind to all */
52 servaddr.in6.sin6_family = AF_INET6;
53 servaddr.in6.sin6_port = htons(port);
55 /* Bind ipv4 to all */
56 servaddr.in4.sin_family = AF_INET;
57 servaddr.in4.sin_port = htons(port);
61 ret = SE->Bind(sockfd, &servaddr.sa, sa_size(servaddr));
71 if (SE->Listen(sockfd, Config->MaxConn) == -1)
73 this->Logs->Log("SOCKET",DEFAULT,"ERROR in listen(): %s",strerror(errno));
78 this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d with listen: %s:%d", sockfd, addr, port);
79 SE->NonBlocking(sockfd);
85 this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d without listen: %s:%d", sockfd, addr, port);
92 int irc::sockets::OpenTCPSocket(const char* addr, int socktype)
97 struct linger linger = { 0, 0 };
101 sockfd = socket (PF_INET6, socktype, 0);
104 sockfd = socket (PF_INET, socktype, 0);
106 else if (strchr(addr,':'))
107 sockfd = socket (PF_INET6, socktype, 0);
109 sockfd = socket (PF_INET, socktype, 0);
117 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
118 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
121 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
126 // XXX: it would be VERY nice to genericize this so all listen stuff (server/client) could use the one function. -- w00t
127 int InspIRCd::BindPorts(FailedPortList &failed_ports)
129 char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
131 bool started_with_nothing = (ports.size() == 0);
132 std::vector<std::pair<std::string, int> > old_ports;
134 /* XXX: Make a copy of the old ip/port pairs here */
135 for (std::vector<ListenSocketBase *>::iterator o = ports.begin(); o != ports.end(); ++o)
136 old_ports.push_back(make_pair((*o)->GetIP(), (*o)->GetPort()));
138 for (int count = 0; count < Config->ConfValueEnum("bind"); count++)
140 Config->ConfValue("bind", "port", count, configToken, MAXBUF);
141 Config->ConfValue("bind", "address", count, Addr, MAXBUF);
142 Config->ConfValue("bind", "type", count, Type, MAXBUF);
144 if (strncmp(Addr, "::ffff:", 7) == 0)
145 this->Logs->Log("SOCKET",DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
147 if ((!*Type) || (!strcmp(Type,"clients")))
149 irc::portparser portrange(configToken, false);
151 while (0 != (portno = portrange.GetToken()))
157 for (std::vector<ListenSocketBase *>::iterator n = ports.begin(); n != ports.end(); ++n)
159 if (((*n)->GetIP() == Addr) && ((*n)->GetPort() == portno))
162 /* XXX: Here, erase from our copy of the list */
163 for (std::vector<std::pair<std::string, int> >::iterator k = old_ports.begin(); k != old_ports.end(); ++k)
165 if ((k->first == Addr) && (k->second == portno))
175 ClientListenSocket *ll = new ClientListenSocket(this, portno, Addr);
176 if (ll->GetFd() > -1)
183 failed_ports.push_back(std::make_pair((*Addr ? Addr : "*") + std::string(":") + ConvToStr(portno), strerror(errno)));
190 /* XXX: Here, anything left in our copy list, close as removed */
191 if (!started_with_nothing)
193 for (size_t k = 0; k < old_ports.size(); ++k)
195 for (std::vector<ListenSocketBase *>::iterator n = ports.begin(); n != ports.end(); ++n)
197 if (((*n)->GetIP() == old_ports[k].first) && ((*n)->GetPort() == old_ports[k].second))
199 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);
211 bool irc::sockets::aptosa(const char* addr, int port, irc::sockets::sockaddrs* sa)
213 memset(sa, 0, sizeof(*sa));
217 sa->in6.sin6_family = AF_INET6;
218 sa->in6.sin6_port = htons(port);
220 sa->in4.sin_family = AF_INET;
221 sa->in4.sin_port = htons(port);
225 else if (inet_pton(AF_INET, addr, &sa->in4.sin_addr) > 0)
227 sa->in4.sin_family = AF_INET;
228 sa->in4.sin_port = htons(port);
231 else if (inet_pton(AF_INET6, addr, &sa->in6.sin6_addr) > 0)
233 sa->in6.sin6_family = AF_INET6;
234 sa->in6.sin6_port = htons(port);
240 bool irc::sockets::satoap(const irc::sockets::sockaddrs* sa, std::string& addr, int &port) {
241 char addrv[INET6_ADDRSTRLEN+1];
242 if (sa->sa.sa_family == AF_INET)
244 if (!inet_ntop(AF_INET, &sa->in4.sin_addr, addrv, sizeof(addrv)))
247 port = ntohs(sa->in4.sin_port);
250 else if (sa->sa.sa_family == AF_INET6)
252 if (!inet_ntop(AF_INET6, &sa->in6.sin6_addr, addrv, sizeof(addrv)))
255 port = ntohs(sa->in6.sin6_port);
261 int irc::sockets::sa_size(irc::sockets::sockaddrs& sa)
263 if (sa.sa.sa_family == AF_INET)
264 return sizeof(sa.in4);
265 if (sa.sa.sa_family == AF_INET6)
266 return sizeof(sa.in6);