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