]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
Remove an extern, partly because it's unused, partly because it then gets shadowed...
[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 extern int openSockfd[MAX_DESCRIPTORS];
30
31 InspSocket* socket_ref[MAX_DESCRIPTORS];
32
33 /** This will bind a socket to a port. It works for UDP/TCP.
34  * If a hostname is given to bind to, the function will first
35  * attempt to resolve the hostname, then bind to the IP the 
36  * hostname resolves to. This is a blocking lookup blocking for
37  * a maximum of one second before it times out, using the DNS
38  * server specified in the configuration file.
39  */ 
40 bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr)
41 {
42         memset(&server,0,sizeof(server));
43         insp_inaddr addy;
44         bool resolved = false;
45         char resolved_addr[128];
46
47         if (*addr == '*')
48                 *addr = 0;
49
50         if (*addr && !inet_aton(addr,&addy))
51         {
52                 /* If they gave a hostname, bind to the IP it resolves to */
53                 if (CleanAndResolve(resolved_addr, addr, true))
54                 {
55                         inet_aton(resolved_addr,&addy);
56                         log(DEFAULT,"Resolved binding '%s' -> '%s'",addr,resolved_addr);
57                         server.sin_addr = addy;
58                         resolved = true;
59                 }
60                 else
61                 {
62                         log(DEFAULT,"WARNING: Could not resolve '%s' to an IP for binding to on port %d",addr,port);
63                         return false;
64                 }
65         }
66         server.sin_family = AF_INET;
67         if (!resolved)
68         {
69                 if (!*addr)
70                 {
71                         server.sin_addr.s_addr = htonl(INADDR_ANY);
72                 }
73                 else
74                 {
75                         server.sin_addr = addy;
76                 }
77         }
78         server.sin_port = htons(port);
79         if (bind(sockfd,(struct sockaddr*)&server,sizeof(server)) < 0)
80         {
81                 return false;
82         }
83         else
84         {
85                 log(DEBUG,"Bound port %s:%d",*addr ? addr : "*",port);
86                 if (listen(sockfd, Config->MaxConn) == -1)
87                 {
88                         log(DEFAULT,"ERROR in listen(): %s",strerror(errno));
89                         return false;
90                 }
91                 else
92                 {
93                         NonBlocking(sockfd);
94                         return true;
95                 }
96         }
97 }
98
99
100 // Open a TCP Socket
101 int OpenTCPSocket()
102 {
103         int sockfd;
104         int on = 1;
105         struct linger linger = { 0 };
106   
107         if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
108         {
109                 log(DEFAULT,"Error creating TCP socket: %s",strerror(errno));
110                 return (ERROR);
111         }
112         else
113         {
114                 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
115                 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
116                 linger.l_onoff = 1;
117                 linger.l_linger = 1;
118                 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger,sizeof(linger));
119                 return (sockfd);
120         }
121 }
122
123 bool HasPort(int port, char* addr)
124 {
125         for (int count = 0; count < ServerInstance->stats->BoundPortCount; count++)
126         {
127                 if ((port == Config->ports[count]) && (!strcasecmp(Config->addrs[count],addr)))
128                 {
129                         return true;
130                 }
131         }
132         return false;
133 }
134
135 int BindPorts(bool bail)
136 {
137         char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
138         insp_sockaddr client, server;
139         int clientportcount = 0;
140         int BoundPortCount = 0;
141
142         if (!bail)
143         {
144                 int InitialPortCount = ServerInstance->stats->BoundPortCount;
145                 log(DEBUG,"Initial port count: %d",InitialPortCount);
146
147                 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
148                 {
149                         Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
150                         Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
151                         Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
152
153                         if (((!*Type) || (!strcmp(Type,"clients"))) && (!HasPort(atoi(configToken),Addr)))
154                         {
155                                 // modules handle server bind types now
156                                 Config->ports[clientportcount+InitialPortCount] = atoi(configToken);
157                                 if (*Addr == '*')
158                                         *Addr = 0;
159
160                                 strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
161                                 clientportcount++;
162                                 log(DEBUG,"NEW binding %s:%s [%s] from config",Addr,configToken, Type);
163                         }
164                 }
165                 int PortCount = clientportcount;
166                 if (PortCount)
167                 {
168                         for (int count = InitialPortCount; count < InitialPortCount + PortCount; count++)
169                         {
170                                 if ((openSockfd[count] = OpenTCPSocket()) == ERROR)
171                                 {
172                                         log(DEBUG,"Bad fd %d binding port [%s:%d]",openSockfd[count],Config->addrs[count],Config->ports[count]);
173                                         return ERROR;
174                                 }
175                                 if (!BindSocket(openSockfd[count],client,server,Config->ports[count],Config->addrs[count]))
176                                 {
177                                         log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
178                                 }
179                                 else
180                                 {
181                                         /* Associate the new open port with a slot in the socket engine */
182                                         ServerInstance->SE->AddFd(openSockfd[count],true,X_LISTEN);
183                                         BoundPortCount++;
184                                 }
185                         }
186                         return InitialPortCount + BoundPortCount;
187                 }
188                 else
189                 {
190                         log(DEBUG,"There is nothing new to bind!");
191                 }
192                 return InitialPortCount;
193         }
194
195         for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
196         {
197                 Config->ConfValue(Config->config_data, "bind", "port", count, configToken, MAXBUF);
198                 Config->ConfValue(Config->config_data, "bind", "address", count, Addr, MAXBUF);
199                 Config->ConfValue(Config->config_data, "bind", "type", count, Type, MAXBUF);
200
201                 if ((!*Type) || (!strcmp(Type,"clients")))
202                 {
203                         // modules handle server bind types now
204                         Config->ports[clientportcount] = atoi(configToken);
205
206                         // If the client put bind "*", this is an unrealism.
207                         // We don't actually support this as documented, but
208                         // i got fed up of people trying it, so now it converts
209                         // it to an empty string meaning the same 'bind to all'.
210                         if (*Addr == '*')
211                                 *Addr = 0;
212
213                         strlcpy(Config->addrs[clientportcount],Addr,256);
214                         clientportcount++;
215                         log(DEBUG,"Binding %s:%s [%s] from config",Addr,configToken, Type);
216                 }
217         }
218
219         int PortCount = clientportcount;
220
221         for (int count = 0; count < PortCount; count++)
222         {
223                 if ((openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
224                 {
225                         log(DEBUG,"Bad fd %d binding port [%s:%d]",openSockfd[BoundPortCount],Config->addrs[count],Config->ports[count]);
226                         return ERROR;
227                 }
228
229                 if (!BindSocket(openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]))
230                 {
231                         log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
232                 }
233                 else
234                 {
235                         /* well we at least bound to one socket so we'll continue */
236                         BoundPortCount++;
237                 }
238         }
239
240         /* if we didn't bind to anything then abort */
241         if (!BoundPortCount)
242         {
243                 log(DEFAULT,"No ports bound, bailing!");
244                 printf("\nERROR: Could not bind any of %d ports! Please check your configuration.\n\n", PortCount);
245                 return ERROR;
246         }
247
248         return BoundPortCount;
249 }