]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
e386e8188931bde9fe4ac15c18db590b27732374
[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 #ifdef IPV6
47         server.sin6_family = AF_FAMILY;
48 #else
49         server.sin_family = AF_FAMILY;
50 #endif
51         if (!*addr)
52         {
53 #ifdef IPV6
54                 memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
55 #else
56                 server.sin_addr.s_addr = htonl(INADDR_ANY);
57 #endif
58         }
59         else
60         {
61 #ifdef IPV6
62                 memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
63 #else
64                 server.sin_addr = addy;
65 #endif
66         }
67 #ifdef IPV6
68         server.sin6_port = htons(port);
69 #else
70         server.sin_port = htons(port);
71 #endif
72         if (bind(sockfd,(struct sockaddr*)&server,sizeof(server)) < 0)
73         {
74                 return false;
75         }
76         else
77         {
78                 log(DEBUG,"Bound port %s:%d",*addr ? addr : "*",port);
79                 if (listen(sockfd, Config->MaxConn) == -1)
80                 {
81                         log(DEFAULT,"ERROR in listen(): %s",strerror(errno));
82                         return false;
83                 }
84                 else
85                 {
86                         NonBlocking(sockfd);
87                         return true;
88                 }
89         }
90 }
91
92
93 // Open a TCP Socket
94 int OpenTCPSocket()
95 {
96         int sockfd;
97         int on = 1;
98         struct linger linger = { 0 };
99   
100         if ((sockfd = socket (AF_FAMILY, SOCK_STREAM, 0)) < 0)
101         {
102                 log(DEFAULT,"Error creating TCP socket: %s",strerror(errno));
103                 return (ERROR);
104         }
105         else
106         {
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 */
109                 linger.l_onoff = 1;
110                 linger.l_linger = 1;
111                 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger,sizeof(linger));
112                 return (sockfd);
113         }
114 }
115
116 bool HasPort(int port, char* addr)
117 {
118         for (unsigned long count = 0; count < ServerInstance->stats->BoundPortCount; count++)
119         {
120                 if ((port == Config->ports[count]) && (!strcasecmp(Config->addrs[count],addr)))
121                 {
122                         return true;
123                 }
124         }
125         return false;
126 }
127
128 int BindPorts(bool bail)
129 {
130         char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
131         insp_sockaddr client, server;
132         int clientportcount = 0;
133         int BoundPortCount = 0;
134
135         if (!bail)
136         {
137                 int InitialPortCount = ServerInstance->stats->BoundPortCount;
138                 log(DEBUG,"Initial port count: %d",InitialPortCount);
139
140                 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
141                 {
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);
145
146                         if (((!*Type) || (!strcmp(Type,"clients"))) && (!HasPort(atoi(configToken),Addr)))
147                         {
148                                 // modules handle server bind types now
149                                 Config->ports[clientportcount+InitialPortCount] = atoi(configToken);
150                                 if (*Addr == '*')
151                                         *Addr = 0;
152
153                                 strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
154                                 clientportcount++;
155                                 log(DEBUG,"NEW binding %s:%s [%s] from config",Addr,configToken, Type);
156                         }
157                 }
158                 int PortCount = clientportcount;
159                 if (PortCount)
160                 {
161                         for (int count = InitialPortCount; count < InitialPortCount + PortCount; count++)
162                         {
163                                 if ((Config->openSockfd[count] = OpenTCPSocket()) == ERROR)
164                                 {
165                                         log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[count],Config->addrs[count],Config->ports[count]);
166                                         return ERROR;
167                                 }
168                                 if (!BindSocket(Config->openSockfd[count],client,server,Config->ports[count],Config->addrs[count]))
169                                 {
170                                         log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
171                                 }
172                                 else
173                                 {
174                                         /* Associate the new open port with a slot in the socket engine */
175                                         if (Config->openSockfd[count] > -1)
176                                         {
177                                                 ServerInstance->SE->AddFd(Config->openSockfd[count],true,X_LISTEN);
178                                                 BoundPortCount++;
179                                         }
180                                 }
181                         }
182                         return InitialPortCount + BoundPortCount;
183                 }
184                 else
185                 {
186                         log(DEBUG,"There is nothing new to bind!");
187                 }
188                 return InitialPortCount;
189         }
190
191         for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
192         {
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);
196
197                 if ((!*Type) || (!strcmp(Type,"clients")))
198                 {
199                         // modules handle server bind types now
200                         Config->ports[clientportcount] = atoi(configToken);
201
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'.
206                         if (*Addr == '*')
207                                 *Addr = 0;
208
209                         strlcpy(Config->addrs[clientportcount],Addr,256);
210                         clientportcount++;
211                         log(DEBUG,"Binding %s:%s [%s] from config",Addr,configToken, Type);
212                 }
213         }
214
215         int PortCount = clientportcount;
216
217         for (int count = 0; count < PortCount; count++)
218         {
219                 if ((Config->openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
220                 {
221                         log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[BoundPortCount],Config->addrs[count],Config->ports[count]);
222                         return ERROR;
223                 }
224
225                 if (!BindSocket(Config->openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]))
226                 {
227                         log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
228                 }
229                 else
230                 {
231                         /* well we at least bound to one socket so we'll continue */
232                         BoundPortCount++;
233                 }
234         }
235
236         /* if we didn't bind to anything then abort */
237         if (!BoundPortCount)
238         {
239                 log(DEFAULT,"No ports bound, bailing!");
240                 printf("\nERROR: Could not bind any of %d ports! Please check your configuration.\n\n", PortCount);
241                 return ERROR;
242         }
243
244         return BoundPortCount;
245 }
246
247 const char* insp_ntoa(insp_inaddr n)
248 {
249         static char buf[1024];
250         inet_ntop(AF_FAMILY, &n, buf, sizeof(buf));
251         return buf;
252 }
253
254 int insp_aton(const char* a, insp_inaddr* n)
255 {
256         return inet_pton(AF_FAMILY, a, n);
257 }
258