1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* $Core: libIRCDsocket */
18 #include "socketengine.h"
20 /* Private static member data must be initialized in this manner */
21 unsigned int ListenSocket::socketcount = 0;
22 sockaddr* ListenSocket::sock_us = NULL;
23 sockaddr* ListenSocket::client = NULL;
24 sockaddr* ListenSocket::raddr = NULL;
26 ListenSocket::ListenSocket(InspIRCd* Instance, int port, char* addr) : ServerInstance(Instance), desc("plaintext"), bind_addr(addr), bind_port(port)
28 this->SetFd(OpenTCPSocket(addr));
29 if (this->GetFd() > -1)
31 if (!Instance->BindSocket(this->fd,port,addr))
34 if ((!*addr) || (strchr(addr,':')))
35 this->family = AF_INET6;
38 this->family = AF_INET;
39 Instance->SE->AddFd(this);
41 /* Saves needless allocations */
44 /* All instances of ListenSocket share these, so reference count it */
45 ServerInstance->Logs->Log("SOCKET", DEBUG,"Allocate sockaddr structures");
46 sock_us = new sockaddr[2];
47 client = new sockaddr[2];
48 raddr = new sockaddr[2];
53 ListenSocket::~ListenSocket()
55 if (this->GetFd() > -1)
57 ServerInstance->SE->DelFd(this);
58 ServerInstance->Logs->Log("SOCKET", DEBUG,"Shut down listener on fd %d", this->fd);
59 if (ServerInstance->SE->Shutdown(this, 2) || ServerInstance->SE->Close(this))
60 ServerInstance->Logs->Log("SOCKET", DEBUG,"Failed to cancel listener: %s", strerror(errno));
72 void ListenSocket::HandleEvent(EventType e, int err)
77 ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err));
80 ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!");
84 ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket");
85 socklen_t uslen, length; // length of our port number
86 int incomingSockfd, in_port;
89 if (this->family == AF_INET6)
91 uslen = sizeof(sockaddr_in6);
92 length = sizeof(sockaddr_in6);
97 uslen = sizeof(sockaddr_in);
98 length = sizeof(sockaddr_in);
101 incomingSockfd = ServerInstance->SE->Accept(this, (sockaddr*)client, &length);
103 if ((incomingSockfd > -1) && (!ServerInstance->SE->GetSockName(this, sock_us, &uslen)))
108 *target = *buf = '\0';
111 if (this->family == AF_INET6)
113 in_port = ntohs(((sockaddr_in6*)sock_us)->sin6_port);
114 inet_ntop(AF_INET6, &((const sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf));
115 socklen_t raddrsz = sizeof(sockaddr_in6);
116 if (getpeername(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
117 inet_ntop(AF_INET6, &((const sockaddr_in6*)raddr)->sin6_addr, target, sizeof(target));
119 ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
124 inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf));
125 in_port = ntohs(((sockaddr_in*)sock_us)->sin_port);
126 socklen_t raddrsz = sizeof(sockaddr_in);
127 if (getpeername(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
128 inet_ntop(AF_INET, &((const sockaddr_in*)raddr)->sin_addr, target, sizeof(target));
130 ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
133 ServerInstance->SE->NonBlocking(incomingSockfd);
134 ServerInstance->stats->statsAccept++;
135 ServerInstance->Users->AddUser(ServerInstance, incomingSockfd, in_port, false, this->family, client, target);
139 ServerInstance->SE->Shutdown(incomingSockfd, 2);
140 ServerInstance->SE->Close(incomingSockfd);
141 ServerInstance->stats->statsRefused++;
148 /** This will bind a socket to a port. It works for UDP/TCP.
149 * It can only bind to IP addresses, if you wish to bind to hostnames
150 * you should first resolve them using class 'Resolver'.
152 bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten)
154 /* We allocate 2 of these, because sockaddr_in6 is larger than sockaddr (ugh, hax) */
155 sockaddr* servaddr = new sockaddr[2];
156 memset(servaddr,0,sizeof(sockaddr)*2);
166 /* There is an address here. Is it ipv6? */
167 if (strchr(addr,':'))
171 if (inet_pton(AF_INET6, addr, &addy) < 1)
177 ((sockaddr_in6*)servaddr)->sin6_family = AF_INET6;
178 memcpy(&(((sockaddr_in6*)servaddr)->sin6_addr), &addy, sizeof(in6_addr));
179 ((sockaddr_in6*)servaddr)->sin6_port = htons(port);
180 size = sizeof(sockaddr_in6);
186 if (inet_pton(AF_INET, addr, &addy) < 1)
192 ((sockaddr_in*)servaddr)->sin_family = AF_INET;
193 ((sockaddr_in*)servaddr)->sin_addr = addy;
194 ((sockaddr_in*)servaddr)->sin_port = htons(port);
195 size = sizeof(sockaddr_in);
202 /* Port -1: Means UDP IPV4 port binding - Special case
203 * used by DNS engine.
205 ((sockaddr_in*)servaddr)->sin_family = AF_INET;
206 ((sockaddr_in*)servaddr)->sin_addr.s_addr = htonl(INADDR_ANY);
207 ((sockaddr_in*)servaddr)->sin_port = 0;
208 size = sizeof(sockaddr_in);
212 /* Theres no address here, default to ipv6 bind to all */
213 ((sockaddr_in6*)servaddr)->sin6_family = AF_INET6;
214 memset(&(((sockaddr_in6*)servaddr)->sin6_addr), 0, sizeof(in6_addr));
215 ((sockaddr_in6*)servaddr)->sin6_port = htons(port);
216 size = sizeof(sockaddr_in6);
220 /* If we aren't built with ipv6, the choice becomes simple */
221 ((sockaddr_in*)servaddr)->sin_family = AF_INET;
224 /* There is an address here. */
226 if (inet_pton(AF_INET, addr, &addy) < 1)
231 ((sockaddr_in*)servaddr)->sin_addr = addy;
235 /* Bind ipv4 to all */
236 ((sockaddr_in*)servaddr)->sin_addr.s_addr = htonl(INADDR_ANY);
238 /* Bind ipv4 port number */
239 ((sockaddr_in*)servaddr)->sin_port = htons(port);
240 size = sizeof(sockaddr_in);
242 ret = SE->Bind(sockfd, servaddr, size);
253 if (SE->Listen(sockfd, Config->MaxConn) == -1)
255 this->Logs->Log("SOCKET",DEFAULT,"ERROR in listen(): %s",strerror(errno));
260 this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d with listen: %s:%d", sockfd, addr, port);
261 SE->NonBlocking(sockfd);
267 this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d without listen: %s:%d", sockfd, addr, port);
274 int irc::sockets::OpenTCPSocket(char* addr, int socktype)
279 struct linger linger = { 0, 0 };
281 if (strchr(addr,':') || (!*addr))
282 sockfd = socket (PF_INET6, socktype, 0);
284 sockfd = socket (PF_INET, socktype, 0);
287 if ((sockfd = socket (PF_INET, socktype, 0)) < 0)
294 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
295 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
298 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (char*)&linger,sizeof(linger));
303 int InspIRCd::BindPorts(bool, int &ports_found, FailedPortList &failed_ports)
305 char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
307 bool started_with_nothing = (Config->ports.size() == 0);
308 std::vector<std::pair<std::string, int> > old_ports;
310 /* XXX: Make a copy of the old ip/port pairs here */
311 for (std::vector<ListenSocket*>::iterator o = Config->ports.begin(); o != Config->ports.end(); ++o)
312 old_ports.push_back(make_pair((*o)->GetIP(), (*o)->GetPort()));
314 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
316 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
317 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
318 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
320 if (strncmp(Addr, "::ffff:", 7) == 0)
321 this->Logs->Log("SOCKET",DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
323 if ((!*Type) || (!strcmp(Type,"clients")))
325 irc::portparser portrange(configToken, false);
327 while (0 != (portno = portrange.GetToken()))
333 for (std::vector<ListenSocket*>::iterator n = Config->ports.begin(); n != Config->ports.end(); ++n)
335 if (((*n)->GetIP() == Addr) && ((*n)->GetPort() == portno))
338 /* XXX: Here, erase from our copy of the list */
339 for (std::vector<std::pair<std::string, int> >::iterator k = old_ports.begin(); k != old_ports.end(); ++k)
341 if ((k->first == Addr) && (k->second == portno))
351 ListenSocket* ll = new ListenSocket(this, portno, Addr);
352 if (ll->GetFd() > -1)
355 Config->ports.push_back(ll);
359 failed_ports.push_back(std::make_pair(Addr, portno));
367 /* XXX: Here, anything left in our copy list, close as removed */
368 if (!started_with_nothing)
370 for (size_t k = 0; k < old_ports.size(); ++k)
372 for (std::vector<ListenSocket*>::iterator n = Config->ports.begin(); n != Config->ports.end(); ++n)
374 if (((*n)->GetIP() == old_ports[k].first) && ((*n)->GetPort() == old_ports[k].second))
376 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);
378 Config->ports.erase(n);
388 const char* irc::sockets::insp_ntoa(insp_inaddr n)
390 static char buf[1024];
391 inet_ntop(AF_FAMILY, &n, buf, sizeof(buf));
395 int irc::sockets::insp_aton(const char* a, insp_inaddr* n)
397 return inet_pton(AF_FAMILY, a, n);