]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
ff460c473c702df22d9041899ec6c3d8d992efbc
[user/henk/code/inspircd.git] / src / socket.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include <string>
18 #include "configreader.h"
19 #include "socket.h"
20 #include "inspircd.h"
21 #include "inspstring.h"
22 #include "helperfuncs.h"
23 #include "socketengine.h"
24 #include "message.h"
25
26 extern InspIRCd* ServerInstance;
27 extern ServerConfig* Config;
28 extern time_t TIME;
29
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.
36  */ 
37 bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr)
38 {
39         memset(&server,0,sizeof(server));
40         insp_inaddr addy;
41         bool resolved = false;
42         char resolved_addr[128];
43
44         if (*addr == '*')
45                 *addr = 0;
46
47         if (*addr && (insp_aton(addr,&addy) < 1))
48         {
49                 /* If they gave a hostname, bind to the IP it resolves to */
50                 if (CleanAndResolve(resolved_addr, addr, true, 1))
51                 {
52                         insp_aton(resolved_addr,&addy);
53                         log(DEFAULT,"Resolved binding '%s' -> '%s'",addr,resolved_addr);
54 #ifdef IPV6
55                         /* Todo: Deal with resolution of IPV6 */
56                         server.sin6_addr = addy;
57 #else
58                         server.sin_addr = addy;
59 #endif
60                         resolved = true;
61                 }
62                 else
63                 {
64                         log(DEFAULT,"WARNING: Could not resolve '%s' to an IP for binding to on port %d",addr,port);
65                         return false;
66                 }
67         }
68 #ifdef IPV6
69         server.sin6_family = AF_FAMILY;
70 #else
71         server.sin_family = AF_FAMILY;
72 #endif
73         if (!resolved)
74         {
75                 if (!*addr)
76                 {
77 #ifdef IPV6
78                         memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
79 #else
80                         server.sin_addr.s_addr = htonl(INADDR_ANY);
81 #endif
82                 }
83                 else
84                 {
85 #ifdef IPV6
86                         memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
87 #else
88                         server.sin_addr = addy;
89 #endif
90                 }
91         }
92 #ifdef IPV6
93         server.sin6_port = htons(port);
94 #else
95         server.sin_port = htons(port);
96 #endif
97         if (bind(sockfd,(struct sockaddr*)&server,sizeof(server)) < 0)
98         {
99                 return false;
100         }
101         else
102         {
103                 log(DEBUG,"Bound port %s:%d",*addr ? addr : "*",port);
104                 if (listen(sockfd, Config->MaxConn) == -1)
105                 {
106                         log(DEFAULT,"ERROR in listen(): %s",strerror(errno));
107                         return false;
108                 }
109                 else
110                 {
111                         NonBlocking(sockfd);
112                         return true;
113                 }
114         }
115 }
116
117
118 // Open a TCP Socket
119 int OpenTCPSocket()
120 {
121         int sockfd;
122         int on = 1;
123         struct linger linger = { 0 };
124   
125         if ((sockfd = socket (AF_FAMILY, SOCK_STREAM, 0)) < 0)
126         {
127                 log(DEFAULT,"Error creating TCP socket: %s",strerror(errno));
128                 return (ERROR);
129         }
130         else
131         {
132                 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
133                 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
134                 linger.l_onoff = 1;
135                 linger.l_linger = 1;
136                 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger,sizeof(linger));
137                 return (sockfd);
138         }
139 }
140
141 bool HasPort(int port, char* addr)
142 {
143         for (unsigned long count = 0; count < ServerInstance->stats->BoundPortCount; count++)
144         {
145                 if ((port == Config->ports[count]) && (!strcasecmp(Config->addrs[count],addr)))
146                 {
147                         return true;
148                 }
149         }
150         return false;
151 }
152
153 int BindPorts(bool bail)
154 {
155         char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
156         insp_sockaddr client, server;
157         int clientportcount = 0;
158         int BoundPortCount = 0;
159
160         if (!bail)
161         {
162                 int InitialPortCount = ServerInstance->stats->BoundPortCount;
163                 log(DEBUG,"Initial port count: %d",InitialPortCount);
164
165                 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
166                 {
167                         Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
168                         Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
169                         Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
170
171                         if (((!*Type) || (!strcmp(Type,"clients"))) && (!HasPort(atoi(configToken),Addr)))
172                         {
173                                 // modules handle server bind types now
174                                 Config->ports[clientportcount+InitialPortCount] = atoi(configToken);
175                                 if (*Addr == '*')
176                                         *Addr = 0;
177
178                                 strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
179                                 clientportcount++;
180                                 log(DEBUG,"NEW binding %s:%s [%s] from config",Addr,configToken, Type);
181                         }
182                 }
183                 int PortCount = clientportcount;
184                 if (PortCount)
185                 {
186                         for (int count = InitialPortCount; count < InitialPortCount + PortCount; count++)
187                         {
188                                 if ((Config->openSockfd[count] = OpenTCPSocket()) == ERROR)
189                                 {
190                                         log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[count],Config->addrs[count],Config->ports[count]);
191                                         return ERROR;
192                                 }
193                                 if (!BindSocket(Config->openSockfd[count],client,server,Config->ports[count],Config->addrs[count]))
194                                 {
195                                         log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
196                                 }
197                                 else
198                                 {
199                                         /* Associate the new open port with a slot in the socket engine */
200                                         if (Config->openSockfd[count] > -1)
201                                         {
202                                                 ServerInstance->SE->AddFd(Config->openSockfd[count],true,X_LISTEN);
203                                                 BoundPortCount++;
204                                         }
205                                 }
206                         }
207                         return InitialPortCount + BoundPortCount;
208                 }
209                 else
210                 {
211                         log(DEBUG,"There is nothing new to bind!");
212                 }
213                 return InitialPortCount;
214         }
215
216         for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
217         {
218                 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
219                 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
220                 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
221
222                 if ((!*Type) || (!strcmp(Type,"clients")))
223                 {
224                         // modules handle server bind types now
225                         Config->ports[clientportcount] = atoi(configToken);
226
227                         // If the client put bind "*", this is an unrealism.
228                         // We don't actually support this as documented, but
229                         // i got fed up of people trying it, so now it converts
230                         // it to an empty string meaning the same 'bind to all'.
231                         if (*Addr == '*')
232                                 *Addr = 0;
233
234                         strlcpy(Config->addrs[clientportcount],Addr,256);
235                         clientportcount++;
236                         log(DEBUG,"Binding %s:%s [%s] from config",Addr,configToken, Type);
237                 }
238         }
239
240         int PortCount = clientportcount;
241
242         for (int count = 0; count < PortCount; count++)
243         {
244                 if ((Config->openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
245                 {
246                         log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[BoundPortCount],Config->addrs[count],Config->ports[count]);
247                         return ERROR;
248                 }
249
250                 if (!BindSocket(Config->openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]))
251                 {
252                         log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
253                 }
254                 else
255                 {
256                         /* well we at least bound to one socket so we'll continue */
257                         BoundPortCount++;
258                 }
259         }
260
261         /* if we didn't bind to anything then abort */
262         if (!BoundPortCount)
263         {
264                 log(DEFAULT,"No ports bound, bailing!");
265                 printf("\nERROR: Could not bind any of %d ports! Please check your configuration.\n\n", PortCount);
266                 return ERROR;
267         }
268
269         return BoundPortCount;
270 }
271
272 const char* insp_ntoa(insp_inaddr n)
273 {
274         static char buf[1024];
275         inet_ntop(AF_FAMILY, &n, buf, sizeof(buf));
276         return buf;
277 }
278
279 int insp_aton(const char* a, insp_inaddr* n)
280 {
281         return inet_pton(AF_FAMILY, a, n);
282 }
283