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 /** This will bind a socket to a port. It works for UDP/TCP.
21 * It can only bind to IP addresses, if you wish to bind to hostnames
22 * you should first resolve them using class 'Resolver'.
24 bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten)
26 /* We allocate 2 of these, because sockaddr_in6 is larger than sockaddr (ugh, hax) */
27 sockaddr* servaddr = new sockaddr[2];
28 memset(servaddr,0,sizeof(sockaddr)*2);
38 /* There is an address here. Is it ipv6? */
43 if (inet_pton(AF_INET6, addr, &addy) < 1)
49 ((sockaddr_in6*)servaddr)->sin6_family = AF_INET6;
50 memcpy(&(((sockaddr_in6*)servaddr)->sin6_addr), &addy, sizeof(in6_addr));
51 ((sockaddr_in6*)servaddr)->sin6_port = htons(port);
52 size = sizeof(sockaddr_in6);
58 if (inet_pton(AF_INET, addr, &addy) < 1)
64 ((sockaddr_in*)servaddr)->sin_family = AF_INET;
65 ((sockaddr_in*)servaddr)->sin_addr = addy;
66 ((sockaddr_in*)servaddr)->sin_port = htons(port);
67 size = sizeof(sockaddr_in);
74 /* Port -1: Means UDP IPV4 port binding - Special case
77 ((sockaddr_in*)servaddr)->sin_family = AF_INET;
78 ((sockaddr_in*)servaddr)->sin_addr.s_addr = htonl(INADDR_ANY);
79 ((sockaddr_in*)servaddr)->sin_port = 0;
80 size = sizeof(sockaddr_in);
84 /* Theres no address here, default to ipv6 bind to all */
85 ((sockaddr_in6*)servaddr)->sin6_family = AF_INET6;
86 memset(&(((sockaddr_in6*)servaddr)->sin6_addr), 0, sizeof(in6_addr));
87 ((sockaddr_in6*)servaddr)->sin6_port = htons(port);
88 size = sizeof(sockaddr_in6);
92 /* If we aren't built with ipv6, the choice becomes simple */
93 ((sockaddr_in*)servaddr)->sin_family = AF_INET;
96 /* There is an address here. */
98 if (inet_pton(AF_INET, addr, &addy) < 1)
103 ((sockaddr_in*)servaddr)->sin_addr = addy;
107 /* Bind ipv4 to all */
108 ((sockaddr_in*)servaddr)->sin_addr.s_addr = htonl(INADDR_ANY);
110 /* Bind ipv4 port number */
111 ((sockaddr_in*)servaddr)->sin_port = htons(port);
112 size = sizeof(sockaddr_in);
114 ret = SE->Bind(sockfd, servaddr, size);
125 if (SE->Listen(sockfd, Config->MaxConn) == -1)
127 this->Logs->Log("SOCKET",DEFAULT,"ERROR in listen(): %s",strerror(errno));
132 this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d with listen: %s:%d", sockfd, addr, port);
133 SE->NonBlocking(sockfd);
139 this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d without listen: %s:%d", sockfd, addr, port);
146 int irc::sockets::OpenTCPSocket(const char* addr, int socktype)
151 struct linger linger = { 0, 0 };
153 if (strchr(addr,':') || (!*addr))
154 sockfd = socket (PF_INET6, socktype, 0);
156 sockfd = socket (PF_INET, socktype, 0);
159 if ((sockfd = socket (PF_INET, socktype, 0)) < 0)
166 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
167 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
170 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (char*)&linger,sizeof(linger));
175 // XXX: it would be VERY nice to genericize this so all listen stuff (server/client) could use the one function. -- w00t
176 int InspIRCd::BindPorts(bool, int &ports_found, FailedPortList &failed_ports)
178 char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
180 bool started_with_nothing = (Config->ports.size() == 0);
181 std::vector<std::pair<std::string, int> > old_ports;
183 /* XXX: Make a copy of the old ip/port pairs here */
184 for (std::vector<ListenSocketBase *>::iterator o = Config->ports.begin(); o != Config->ports.end(); ++o)
185 old_ports.push_back(make_pair((*o)->GetIP(), (*o)->GetPort()));
187 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
189 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
190 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
191 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
193 if (strncmp(Addr, "::ffff:", 7) == 0)
194 this->Logs->Log("SOCKET",DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
196 if ((!*Type) || (!strcmp(Type,"clients")))
198 irc::portparser portrange(configToken, false);
200 while (0 != (portno = portrange.GetToken()))
206 for (std::vector<ListenSocketBase *>::iterator n = Config->ports.begin(); n != Config->ports.end(); ++n)
208 if (((*n)->GetIP() == Addr) && ((*n)->GetPort() == portno))
211 /* XXX: Here, erase from our copy of the list */
212 for (std::vector<std::pair<std::string, int> >::iterator k = old_ports.begin(); k != old_ports.end(); ++k)
214 if ((k->first == Addr) && (k->second == portno))
224 ClientListenSocket *ll = new ClientListenSocket(this, portno, Addr);
225 if (ll->GetFd() > -1)
228 Config->ports.push_back(ll);
232 failed_ports.push_back(std::make_pair((*Addr ? Addr : "*") + std::string(":") + ConvToStr(portno), strerror(errno)));
240 /* XXX: Here, anything left in our copy list, close as removed */
241 if (!started_with_nothing)
243 for (size_t k = 0; k < old_ports.size(); ++k)
245 for (std::vector<ListenSocketBase *>::iterator n = Config->ports.begin(); n != Config->ports.end(); ++n)
247 if (((*n)->GetIP() == old_ports[k].first) && ((*n)->GetPort() == old_ports[k].second))
249 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);
251 Config->ports.erase(n);
261 const char* irc::sockets::insp_ntoa(insp_inaddr n)
263 static char buf[1024];
264 inet_ntop(AF_FAMILY, &n, buf, sizeof(buf));
268 int irc::sockets::insp_aton(const char* a, insp_inaddr* n)
270 return inet_pton(AF_FAMILY, a, n);