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