1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
18 #include "configreader.h"
21 #include "inspstring.h"
22 #include "helperfuncs.h"
23 #include "socketengine.h"
26 extern InspIRCd* ServerInstance;
27 extern ServerConfig* Config;
30 /** This will bind a socket to a port. It works for UDP/TCP.
31 * If a hostname is given to bind to, the function will first
32 * attempt to resolve the hostname, then bind to the IP the
33 * hostname resolves to. This is a blocking lookup blocking for
34 * a maximum of one second before it times out, using the DNS
35 * server specified in the configuration file.
37 bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr)
39 memset(&server,0,sizeof(server));
41 bool resolved = false;
42 char resolved_addr[128];
47 server.sin6_family = AF_FAMILY;
49 server.sin_family = AF_FAMILY;
54 memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
56 server.sin_addr.s_addr = htonl(INADDR_ANY);
62 memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
64 server.sin_addr = addy;
68 server.sin6_port = htons(port);
70 server.sin_port = htons(port);
72 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server)) < 0)
78 log(DEBUG,"Bound port %s:%d",*addr ? addr : "*",port);
79 if (listen(sockfd, Config->MaxConn) == -1)
81 log(DEFAULT,"ERROR in listen(): %s",strerror(errno));
98 struct linger linger = { 0 };
100 if ((sockfd = socket (AF_FAMILY, SOCK_STREAM, 0)) < 0)
102 log(DEFAULT,"Error creating TCP socket: %s",strerror(errno));
107 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
108 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
111 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger,sizeof(linger));
116 bool HasPort(int port, char* addr)
118 for (unsigned long count = 0; count < ServerInstance->stats->BoundPortCount; count++)
120 if ((port == Config->ports[count]) && (!strcasecmp(Config->addrs[count],addr)))
128 int BindPorts(bool bail)
130 char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
131 insp_sockaddr client, server;
132 int clientportcount = 0;
133 int BoundPortCount = 0;
137 int InitialPortCount = ServerInstance->stats->BoundPortCount;
138 log(DEBUG,"Initial port count: %d",InitialPortCount);
140 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
142 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
143 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
144 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
146 if (((!*Type) || (!strcmp(Type,"clients"))) && (!HasPort(atoi(configToken),Addr)))
148 // modules handle server bind types now
149 Config->ports[clientportcount+InitialPortCount] = atoi(configToken);
153 strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
155 log(DEBUG,"NEW binding %s:%s [%s] from config",Addr,configToken, Type);
158 int PortCount = clientportcount;
161 for (int count = InitialPortCount; count < InitialPortCount + PortCount; count++)
163 if ((Config->openSockfd[count] = OpenTCPSocket()) == ERROR)
165 log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[count],Config->addrs[count],Config->ports[count]);
168 if (!BindSocket(Config->openSockfd[count],client,server,Config->ports[count],Config->addrs[count]))
170 log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
174 /* Associate the new open port with a slot in the socket engine */
175 if (Config->openSockfd[count] > -1)
177 ServerInstance->SE->AddFd(Config->openSockfd[count],true,X_LISTEN);
182 return InitialPortCount + BoundPortCount;
186 log(DEBUG,"There is nothing new to bind!");
188 return InitialPortCount;
191 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
193 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
194 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
195 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
197 if ((!*Type) || (!strcmp(Type,"clients")))
199 // modules handle server bind types now
200 Config->ports[clientportcount] = atoi(configToken);
202 // If the client put bind "*", this is an unrealism.
203 // We don't actually support this as documented, but
204 // i got fed up of people trying it, so now it converts
205 // it to an empty string meaning the same 'bind to all'.
209 strlcpy(Config->addrs[clientportcount],Addr,256);
211 log(DEBUG,"Binding %s:%s [%s] from config",Addr,configToken, Type);
215 int PortCount = clientportcount;
217 for (int count = 0; count < PortCount; count++)
219 if ((Config->openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
221 log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[BoundPortCount],Config->addrs[count],Config->ports[count]);
225 if (!BindSocket(Config->openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]))
227 log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
231 /* well we at least bound to one socket so we'll continue */
236 /* if we didn't bind to anything then abort */
239 log(DEFAULT,"No ports bound, bailing!");
240 printf("\nERROR: Could not bind any of %d ports! Please check your configuration.\n\n", PortCount);
244 return BoundPortCount;
247 const char* insp_ntoa(insp_inaddr n)
249 static char buf[1024];
250 inet_ntop(AF_FAMILY, &n, buf, sizeof(buf));
254 int insp_aton(const char* a, insp_inaddr* n)
256 return inet_pton(AF_FAMILY, a, n);