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 * ---------------------------------------------------
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(irc::sockets::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 (getsockname(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 (getsockname(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));
132 ServerInstance->SE->NonBlocking(incomingSockfd);
133 ServerInstance->stats->statsAccept++;
134 ServerInstance->Users->AddUser(ServerInstance, incomingSockfd, in_port, false, this->family, client, target);
138 ServerInstance->SE->Shutdown(incomingSockfd, 2);
139 ServerInstance->SE->Close(incomingSockfd);
140 ServerInstance->stats->statsRefused++;
147 /** This will bind a socket to a port. It works for UDP/TCP.
148 * It can only bind to IP addresses, if you wish to bind to hostnames
149 * you should first resolve them using class 'Resolver'.
151 bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten)
153 /* We allocate 2 of these, because sockaddr_in6 is larger than sockaddr (ugh, hax) */
154 sockaddr* servaddr = new sockaddr[2];
155 memset(servaddr,0,sizeof(sockaddr)*2);
165 /* There is an address here. Is it ipv6? */
166 if (strchr(addr,':'))
170 if (inet_pton(AF_INET6, addr, &addy) < 1)
176 ((sockaddr_in6*)servaddr)->sin6_family = AF_INET6;
177 memcpy(&(((sockaddr_in6*)servaddr)->sin6_addr), &addy, sizeof(in6_addr));
178 ((sockaddr_in6*)servaddr)->sin6_port = htons(port);
179 size = sizeof(sockaddr_in6);
185 if (inet_pton(AF_INET, addr, &addy) < 1)
191 ((sockaddr_in*)servaddr)->sin_family = AF_INET;
192 ((sockaddr_in*)servaddr)->sin_addr = addy;
193 ((sockaddr_in*)servaddr)->sin_port = htons(port);
194 size = sizeof(sockaddr_in);
201 /* Port -1: Means UDP IPV4 port binding - Special case
202 * used by DNS engine.
204 ((sockaddr_in*)servaddr)->sin_family = AF_INET;
205 ((sockaddr_in*)servaddr)->sin_addr.s_addr = htonl(INADDR_ANY);
206 ((sockaddr_in*)servaddr)->sin_port = 0;
207 size = sizeof(sockaddr_in);
211 /* Theres no address here, default to ipv6 bind to all */
212 ((sockaddr_in6*)servaddr)->sin6_family = AF_INET6;
213 memset(&(((sockaddr_in6*)servaddr)->sin6_addr), 0, sizeof(in6_addr));
214 ((sockaddr_in6*)servaddr)->sin6_port = htons(port);
215 size = sizeof(sockaddr_in6);
219 /* If we aren't built with ipv6, the choice becomes simple */
220 ((sockaddr_in*)servaddr)->sin_family = AF_INET;
223 /* There is an address here. */
225 if (inet_pton(AF_INET, addr, &addy) < 1)
230 ((sockaddr_in*)servaddr)->sin_addr = addy;
234 /* Bind ipv4 to all */
235 ((sockaddr_in*)servaddr)->sin_addr.s_addr = htonl(INADDR_ANY);
237 /* Bind ipv4 port number */
238 ((sockaddr_in*)servaddr)->sin_port = htons(port);
239 size = sizeof(sockaddr_in);
241 ret = SE->Bind(sockfd, servaddr, size);
252 if (SE->Listen(sockfd, Config->MaxConn) == -1)
254 this->Logs->Log("SOCKET",DEFAULT,"ERROR in listen(): %s",strerror(errno));
259 this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d with listen: %s:%d", sockfd, addr, port);
260 SE->NonBlocking(sockfd);
266 this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d without listen: %s:%d", sockfd, addr, port);
273 int irc::sockets::OpenTCPSocket(char* addr, int socktype)
278 struct linger linger = { 0, 0 };
280 if (strchr(addr,':') || (!*addr))
281 sockfd = socket (PF_INET6, socktype, 0);
283 sockfd = socket (PF_INET, socktype, 0);
286 if ((sockfd = socket (PF_INET, socktype, 0)) < 0)
293 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
294 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
297 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (char*)&linger,sizeof(linger));
302 int InspIRCd::BindPorts(bool, int &ports_found, FailedPortList &failed_ports)
304 char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
306 bool started_with_nothing = (Config->ports.size() == 0);
307 std::vector<std::pair<std::string, int> > old_ports;
309 /* XXX: Make a copy of the old ip/port pairs here */
310 for (std::vector<ListenSocket*>::iterator o = Config->ports.begin(); o != Config->ports.end(); ++o)
311 old_ports.push_back(make_pair((*o)->GetIP(), (*o)->GetPort()));
313 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
315 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
316 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
317 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
319 if (strncmp(Addr, "::ffff:", 7) == 0)
320 this->Logs->Log("SOCKET",DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
322 if ((!*Type) || (!strcmp(Type,"clients")))
324 irc::portparser portrange(configToken, false);
326 while (0 != (portno = portrange.GetToken()))
332 for (std::vector<ListenSocket*>::iterator n = Config->ports.begin(); n != Config->ports.end(); ++n)
334 if (((*n)->GetIP() == Addr) && ((*n)->GetPort() == portno))
337 /* XXX: Here, erase from our copy of the list */
338 for (std::vector<std::pair<std::string, int> >::iterator k = old_ports.begin(); k != old_ports.end(); ++k)
340 if ((k->first == Addr) && (k->second == portno))
350 ListenSocket* ll = new ListenSocket(this, portno, Addr);
351 if (ll->GetFd() > -1)
354 Config->ports.push_back(ll);
358 failed_ports.push_back(std::make_pair((*Addr ? Addr : "*") + std::string(":") + ConvToStr(portno), strerror(errno)));
366 /* XXX: Here, anything left in our copy list, close as removed */
367 if (!started_with_nothing)
369 for (size_t k = 0; k < old_ports.size(); ++k)
371 for (std::vector<ListenSocket*>::iterator n = Config->ports.begin(); n != Config->ports.end(); ++n)
373 if (((*n)->GetIP() == old_ports[k].first) && ((*n)->GetPort() == old_ports[k].second))
375 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);
377 Config->ports.erase(n);
387 const char* irc::sockets::insp_ntoa(insp_inaddr n)
389 static char buf[1024];
390 inet_ntop(AF_FAMILY, &n, buf, sizeof(buf));
394 int irc::sockets::insp_aton(const char* a, insp_inaddr* n)
396 return inet_pton(AF_FAMILY, a, n);