1 /*
2 * ----------------------------------------------------------------
3 * ircproxy - Listen I/O Functions
4 * ----------------------------------------------------------------
5 * Copyright (C) 1997-2009 Jonas Kvinge
6 *
7 * This file is part of ircproxy.
8 *
9 * ircproxy is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * ircproxy is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ircproxy. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * Additional permission under GNU GPL version 3 section 7
23 *
24 * If you modify ircproxy, or any covered work, by linking or
25 * combining it with openssl (or a modified version of that library),
26 * containing parts covered by the terms of the OpenSSL License and the
27 * SSLeay License, the licensors of ircproxy grant you additional
28 * permission to convey the resulting work.
29 *
30 * $Id: listen_io.c 54 2009-03-18 18:23:29Z jonasio $
31 *
32 */
33
34 #define LISTEN_IO_C
35
36 #define NEED_SYS_TYPES_H 1 /* Extra types */
37 #define NEED_SYS_PARAM_H 1 /* Some systems need this */
38 #define NEED_LIMITS_H 0 /* Kernel limits */
39 #define NEED_STDARG_H 1 /* va_list, etc */
40 #define NEED_ERRNO_H 1 /* errno */
41 #define NEED_CTYPE_H 1 /* isdigit(), etc */
42 #define NEED_NETINET_IN_H 1 /* in_addr, sockaddr_in, etc */
43 #define NEED_ARPA_INET_H 1 /* inet_ntoa(), inet_aton(), etc */
44 #define NEED_STDIO_H 1 /* Standard C UNIX functions */
45 #define NEED_STDLIB_H 1 /* malloc(), exit(), atoi(), etc */
46 #define NEED_TIME_H 1 /* time(), etc */
47 #define NEED_SYSCTL_H 0 /* sysctl(), etc */
48 #define NEED_SYS_STAT_H 0 /* chmod(), mkdir(), etc */
49 #define NEED_SYS_UIO_H 0 /* iovec, etc */
50 #define NEED_FCNTL_H 1 /* open(), creat(), fcntl(), etc */
51 #define NEED_SYS_IOCTL_H 1 /* ioctl(), etc */
52 #define NEED_SYS_FILIO_H 1 /* Solaris need this for ioctl(), etc */
53 #define NEED_UNISTD_H 1 /* Unix standard functions */
54 #define NEED_STRING_H 1 /* C string functions */
55 #define NEED_SIGNAL_H 0 /* Signal functions */
56 #define NEED_SYS_SOCKET_H 1 /* Socket functions */
57 #define NEED_NETDB_H 1 /* Network database functions */
58 #define NEED_ARPA_NAMESER_H 0 /* Nameserver definitions */
59 #define NEED_GETUSERPW_HEADERS 0 /* Functions to retrive system passwords */
60 #define NEED_ARES 1 /* Functions needed for ares */
61 #define NEED_SSL 1 /* Needed for SSL support */
62
63 #include "includes.h"
64
65 #include "conf.h"
66
67 #include "listen.h"
68 #include "listen_io.h"
69 #include "client.h"
70 #include "client_connection.h"
71
72 /* VARIABLES - JONAS (06.10.2000) */
73
74 extern struct Conf_Struct ConfS;
75 extern struct Listen_Struct *Listen_Head;
76 extern struct Listen_Struct *Listen_Tail;
77 extern struct Client_Struct *Client_Head;
78 #if ARES
79 extern ares_channel Ares_Channel;
80 #endif
81 #if SSL_SUPPORT
82 extern SSL_CTX *IRCPROXY_SSL_SERVER_CTX;
83 extern SSL_CTX *IRCPROXY_SSL_CLIENT_CTX;
84 #endif
85
86 /* LISTEN_START - JONAS (22.07.2001) */
87
listen_start(struct Listen_Struct * ListenS)88 void listen_start(struct Listen_Struct *ListenS) {
89
90 signed long int Result = 0;
91 struct sockaddr_in SockAddr = { 0 };
92 #if IPV6_SUPPORT
93 struct sockaddr_in6 SockAddr6 = { 0 };
94 #endif
95 unsigned long int Flags = 0;
96
97 assert(ListenS != NULL);
98
99 ListenS->Time = NOW;
100 ++ListenS->Tries;
101
102 DEBUGPRINT(BITMASK_DEBUG_LISTEN, "*** DEBUG *** listen_start(%s)", ListenS->Host);
103
104 if (!Listen_IsResolved(ListenS)) {
105
106 #if !ARES
107 struct hostent *HostEnt = NULL;
108 #endif
109
110 if (ListenS->Host[0] == '*') {
111 #if IPV6_SUPPORT
112 if (Listen_IsIPv6(ListenS)) {
113 memset(&ListenS->INAddr6, 0, sizeof(ListenS->INAddr6));
114 ListenS->INAddr6 = in6addr_any;
115 }
116 else {
117 #endif /* IPV6_SUPPORT */
118 memset(&ListenS->INAddr, 0, sizeof(ListenS->INAddr));
119 ListenS->INAddr.s_addr = INADDR_ANY;
120 #if IPV6_SUPPORT
121 }
122 #endif /* IPV6_SUPPORT */
123 ListenS->HostIPS = strrealloc(ListenS->HostIPS, "ALL");
124 Listen_SetResolved(ListenS);
125 }
126 else {
127 #if IPV6_SUPPORT
128 if (Listen_IsIPv6(ListenS)) {
129 memset(&ListenS->INAddr6, 0, sizeof(ListenS->INAddr6));
130 Result = inet_pton(AF_INET6, ListenS->Host, &ListenS->INAddr6);
131 }
132 else {
133 memset(&ListenS->INAddr, 0, sizeof(ListenS->INAddr));
134 Result = inet_pton(AF_INET, ListenS->Host, &ListenS->INAddr);
135 }
136 #else /* IPV6_SUPPORT */
137 memset(&ListenS->INAddr, 0, sizeof(ListenS->INAddr));
138 Result = inet_aton(ListenS->Host, &ListenS->INAddr);
139 #endif /* IPV6_SUPPORT */
140 if (Result <= 0) {
141 Listen_SetResolving(ListenS);
142 #if ARES
143 #if IPV6_SUPPORT
144 if (Listen_IsIPv6(ListenS)) {
145 ares_gethostbyname(Ares_Channel, ListenS->Host, AF_INET6, (ares_host_callback) listen_hosttoip, ListenS);
146 }
147 else {
148 #endif /* IPV6_SUPPORT */
149 ares_gethostbyname(Ares_Channel, ListenS->Host, AF_INET, (ares_host_callback) listen_hosttoip, ListenS);
150 #if IPV6_SUPPORT
151 }
152 #endif /* IPV6_SUPPORT */
153 return;
154 #else /* ARES */
155 #if IPV6_SUPPORT
156 if (Listen_IsIPv6(ListenS)) { HostEnt = gethostbyname2(ListenS->Host, AF_INET6); }
157 else { HostEnt = gethostbyname2(ListenS->Host, AF_INET); }
158 #else /* IPV6_SUPPORT */
159 HostEnt = gethostbyname(ListenS->Host);
160 #endif /* IPV6_SUPPORT */
161 listen_hosttoip(ListenS, errno, HostEnt);
162 if (!Listen_IsResolved(ListenS)) { return; }
163 #endif /* ARES */
164 }
165 else {
166 Listen_SetResolved(ListenS);
167 ListenS->HostIPS = strrealloc(ListenS->HostIPS, ListenS->Host);
168 if (aerrno != AESUCCESS) { listen_init(ListenS); return; }
169 }
170 }
171 }
172
173 /* CREATE SOCKET */
174
175 #if IPV6_SUPPORT
176 if (Listen_IsIPv6(ListenS)) {
177 Result = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
178 }
179 else {
180 #endif
181 Result = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
182 #if IPV6_SUPPORT
183 }
184 #endif
185 if (Result <= ERROR) {
186 sysprint(BITMASK_MAIN, "Listener %s(%s):%ld%s: Unable to create socket: [%d] %s", ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
187 listen_stop(ListenS);
188 return;
189 }
190 ListenS->FD = Result;
191 Listen_SetSocket(ListenS);
192
193 /* SET SOCKET IN NON-BLOCKING MODE */
194
195 #if defined(NBLOCK_SYSV)
196 Flags = 1;
197 Result = ioctl(ListenS->FD, FIONBIO, &Flags);
198 if (Result <= ERROR) {
199 sysprint(BITMASK_MAIN, "Listener %s(%s):%ld%s: Unable to set socket in non-blocking mode using ioctl(): [%d] %s", ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
200 listen_stop(ListenS);
201 return;
202 }
203 #else
204 Result = fcntl(ListenS->FD, F_GETFL, &Flags);
205 if (Result <= ERROR) {
206 sysprint(BITMASK_MAIN, "Listener %s(%s):%ld%s: Unable to get socket flags using fcntl(): [%d] %s", ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
207 listen_stop(ListenS);
208 return;
209 }
210 #if defined(NBLOCK_BSD)
211 Flags |= O_NDELAY;
212 #elif defined(NBLOCK_POSIX)
213 Flags |= O_NONBLOCK;
214 #else
215 #warning "This system does not support non-blocking sockets?"
216 Flags |= O_NONBLOCK;
217 #endif
218 Result = fcntl(ListenS->FD, F_SETFL, Flags);
219 if (Result <= ERROR) {
220 sysprint(BITMASK_MAIN, "Listener %s(%s):%ld%s: Unable to set socket in non-blocking mode using fcntl(): [%d] %s", ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
221 listen_stop(ListenS);
222 return;
223 }
224 #endif
225
226 if (LISTEN_SOCKKEEPALIVE == TRUE) {
227
228 unsigned long int OPT = 1;
229
230 Result = setsockopt(ListenS->FD, SOL_SOCKET, SO_KEEPALIVE, &OPT, sizeof(OPT));
231 if (Result <= ERROR) {
232 sysprint(BITMASK_MAIN, "Listener %s(%s):%ld%s: Unable to set Keep Alive option for socket: [%d] %s", ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
233 listen_stop(ListenS);
234 return;
235 }
236
237 }
238
239 if (LISTEN_SOCKREUSEADDR == TRUE) {
240
241 unsigned long int OPT = 1;
242
243 Result = setsockopt(ListenS->FD, SOL_SOCKET, SO_REUSEADDR, &OPT, sizeof(OPT));
244 if (Result <= ERROR) {
245 sysprint(BITMASK_MAIN, "Listener %s(%s):%ld%s: Unable to set Reuse Address option for socket: [%d] %s", ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
246 listen_stop(ListenS);
247 return;
248 }
249
250 }
251
252 /* BIND */
253
254 #if !WIN32
255 if (ListenS->PortH < 1024) { sysseteuid(0); }
256 #endif
257
258 #if IPV6_SUPPORT
259 if (Listen_IsIPv6(ListenS)) {
260 memset(&SockAddr6, 0, sizeof(SockAddr6));
261 SockAddr6.sin6_family = AF_INET6;
262 SockAddr6.sin6_addr = ListenS->INAddr6;
263 SockAddr6.sin6_port = ListenS->PortN;
264 Result = bind(ListenS->FD, (struct sockaddr *) &SockAddr6, sizeof(SockAddr6));
265 }
266 else {
267 #endif /* IPV6_SUPPORT */
268 memset(&SockAddr, 0, sizeof(SockAddr));
269 SockAddr.sin_family = AF_INET;
270 SockAddr.sin_addr = ListenS->INAddr;
271 SockAddr.sin_port = ListenS->PortN;
272 Result = bind(ListenS->FD, (struct sockaddr *) &SockAddr, sizeof(SockAddr));
273 #if IPV6_SUPPORT
274 }
275 #endif /* IPV6_SUPPORT */
276
277 #if !WIN32
278 if (ListenS->PortH < 1024) { sysseteuidnormal(); }
279 #endif
280
281 if (Result <= ERROR) {
282 sysprint(BITMASK_MAIN, "Listener %s(%s):%ld%s: Unable to bind socket to address: [%d] %s", ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
283 listen_stop(ListenS);
284 return;
285 }
286
287 /* LISTEN */
288
289 Result = listen(ListenS->FD, SOMAXCONN);
290 if (Result <= ERROR) {
291 sysprint(BITMASK_MAIN, "Listener %s(%s):%ld%s: Unable to listen on address: [%d] %s", ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
292 listen_stop(ListenS);
293 return;
294 }
295 else {
296 sysprint(BITMASK_MAIN, "Listening on %s(%s):%ld%s for incoming connections.", ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""));
297 Listen_SetListening(ListenS);
298 return;
299 }
300
301 }
302
303 /* LISTEN_HOSTTOIP FUNCTION - JONAS (01.03.2000) */
304
305 #if HAVE_CARES_CALLBACK_TIMEOUTS
listen_hosttoip(void * ArgPT,int ErrNo,int Timeouts,struct hostent * HostEnt)306 void listen_hosttoip(void *ArgPT, int ErrNo, int Timeouts, struct hostent *HostEnt) {
307 #else
308 void listen_hosttoip(void *ArgPT, int ErrNo, struct hostent *HostEnt) {
309 #endif
310
311 struct Listen_Struct *ListenS = ArgPT;
312 const char *HostIPPT = NULL;
313 #if IPV6_SUPPORT
314 char Host[INET6_ADDRSTRLEN+1] = "";
315 #endif
316
317 assert(ListenS != NULL);
318
319 if ((HostEnt == NULL) || (HostEnt->h_length < 1)) {
320 sysprint(BITMASK_MAIN, "Listener %s:%ld%s: Unable to resolve host %s to IP-address: [%d] %s", ListenS->Host, ListenS->PortH, ListenS->Host, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), ErrNo, res_strerror(ErrNo));
321 listen_init(ListenS);
322 return;
323 }
324
325 #if IPV6_SUPPORT
326 if (Listen_IsIPv6(ListenS)) {
327 memset(&ListenS->INAddr6, 0, sizeof(ListenS->INAddr6));
328 memcpy(&ListenS->INAddr6, HostEnt->h_addr, HostEnt->h_length);
329 HostIPPT = inet_ntop(AF_INET6, &ListenS->INAddr6, Host, INET6_ADDRSTRLEN);
330 if (HostIPPT == NULL) {
331 sysprint(BITMASK_MAIN, "Listener %s:%ld%s: inet_ntop() failed: [%d] %s", ListenS->Host, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
332 listen_init(ListenS);
333 return;
334 }
335 }
336 else {
337 memset(&ListenS->INAddr, 0, sizeof(ListenS->INAddr));
338 memcpy(&ListenS->INAddr, HostEnt->h_addr, HostEnt->h_length);
339 HostIPPT = inet_ntop(AF_INET, &ListenS->INAddr, Host, INET_ADDRSTRLEN);
340 if (HostIPPT == NULL) {
341 sysprint(BITMASK_MAIN, "Listener %s:%ld%s: inet_ntop() failed: [%d] %s", ListenS->Host, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
342 listen_init(ListenS);
343 return;
344 }
345 }
346 #else /* IPV6_SUPPORT */
347 memset(&ListenS->INAddr, 0, sizeof(ListenS->INAddr));
348 memcpy(&ListenS->INAddr, HostEnt->h_addr, HostEnt->h_length);
349 HostIPPT = inet_ntoa(ListenS->INAddr);
350 if (HostIPPT == NULL) {
351 sysprint(BITMASK_MAIN, "Listener %s:%ld%s: inet_ntoa() failed: [%d] %s", ListenS->Host, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
352 listen_init(ListenS);
353 return;
354 }
355 #endif
356 ListenS->HostIPS = strrealloc(ListenS->HostIPS, HostIPPT);
357 if (ListenS->HostIPS == NULL) {
358 listen_init(ListenS);
359 return;
360 }
361
362 Listen_ClearResolving(ListenS);
363 Listen_SetResolved(ListenS);
364
365 #if ARES
366 listen_start(ListenS);
367 #endif
368
369 }
370
371 /* LISTEN_STOP - JONAS (05.10.2000) */
372
373 void listen_stop(struct Listen_Struct *ListenS) {
374
375 assert(ListenS != NULL);
376
377 if (Listen_IsResolving(ListenS)) {
378 #if HAVE_ARES_CANCELQUERY
379 ares_cancelquery(Ares_Channel, ListenS);
380 #endif
381 return;
382 }
383
384 if (Listen_IsListening(ListenS)) { sysprint(BITMASK_MAIN, "Listener %s(%s):%ld%s has stopped.", ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : "")); }
385 if (Listen_IsSocket(ListenS)) {
386 close(ListenS->FD);
387 ListenS->FD = FD_NONE;
388 }
389
390 listen_init(ListenS);
391
392 }
393
394 /* LISTEN_FDS - JONAS (22.07.2001) */
395
396 void listen_fds(fd_set *ReadFDS, fd_set *WriteFDS, unsigned long int *FDS) {
397
398 struct Listen_Struct *ListenS = NULL;
399
400 for (ListenS = Listen_Head ; ListenS != NULL ;) {
401 if (Listen_IsRemove(ListenS)) {
402 struct Listen_Struct *ListenS_DEL = NULL;
403 if (Listen_IsResolving(ListenS)) { continue; }
404 listen_stop(ListenS);
405 ListenS_DEL = ListenS;
406 ListenS = ListenS->Next;
407 listen_rem(ListenS_DEL);
408 continue;
409 }
410 if (Listen_IsListening(ListenS)) { FD_SET(ListenS->FD, ReadFDS); }
411 else {
412 if (!Listen_IsResolving(ListenS)) {
413 time_t Duration = (NOW - ListenS->Time);
414 if (Duration >= LISTEN_INTERVAL) {
415 #if SSL_SUPPORT
416 if (ConfS.SSLSupport == TRUE) { listen_start(ListenS); }
417 else if (!Listen_IsSSL(ListenS)) { listen_start(ListenS); }
418 #else
419 if (!Listen_IsSSL(ListenS)) { listen_start(ListenS); }
420 #endif
421 }
422 }
423 }
424 ListenS = ListenS->Next;
425 }
426
427 }
428
429 /* LISTEN_IO - JONAS (06.10.2000) */
430
431 void listen_io(fd_set *ReadFDS, fd_set *WriteFDS, unsigned long int *FDS) {
432
433 struct Listen_Struct *ListenS = NULL;
434
435 for (ListenS = Listen_Head ; ListenS != NULL ; ListenS = ListenS->Next) {
436
437 unsigned short int Count = 0;
438
439 assert(*FDS >= 0);
440 if (*FDS <= 0) { return; }
441
442 if (!Listen_IsListening(ListenS)) { continue; }
443 if (!FD_ISSET(ListenS->FD, ReadFDS)) { continue; }
444 *FDS = *FDS - 1;
445 assert(*FDS >= 0);
446
447 for (Count = 0 ;; ++Count) {
448
449 signed long int Result = 0;
450 unsigned long int Flags = 0;
451 struct sockaddr_in SockAddr = { 0 };
452 accept_addrlen_type SockAddrLen = sizeof(SockAddr);
453 #if IPV6_SUPPORT
454 struct sockaddr_in6 SockAddr6 = { 0 };
455 accept_addrlen_type SockAddrLen6 = sizeof(SockAddr6);
456 #endif
457 unsigned long int FD = 0;
458 struct in_addr INAddr;
459 #if IPV6_SUPPORT
460 struct in6_addr INAddr6;
461 char Host[INET6_ADDRSTRLEN+1] = "";
462 #endif
463 unsigned long int PortH = 0;
464 unsigned long int PortN = 0;
465 const char *HostIPPT = NULL;
466 struct Client_Struct *ClientS = NULL;
467
468 #if SSL_SUPPORT
469 SSL *ClientSSL = NULL;
470 #endif
471
472 #if IPV6_SUPPORT
473 if (Listen_IsIPv6(ListenS)) {
474 Result = accept(ListenS->FD, (struct sockaddr *) &SockAddr6, &SockAddrLen6);
475 }
476 else {
477 #endif /* IPV6_SUPPORT */
478 Result = accept(ListenS->FD, (struct sockaddr *) &SockAddr, &SockAddrLen);
479 #if IPV6_SUPPORT
480 }
481 #endif /* IPV6_SUPPORT */
482
483 if (Result <= ERROR) {
484 if (Count == 0) { sysprint(BITMASK_MAIN, "Unable to accept incoming connection on address \"%s(%s):%ld\": [%d] %s", ListenS->Host, ListenS->HostIPS, ListenS->PortH, errno, strerror(errno)); }
485 return;
486 }
487
488 FD = Result;
489
490 #if SSL_SUPPORT
491 if ((Listen_IsSSL(ListenS)) && (IRCPROXY_SSL_SERVER_CTX == NULL)) { /* This is fatal and should never happen under normal circumstances */
492 sysprint(BITMASK_MAIN, "Closing socket to incoming connection on address \"%s(%s):%ld\": Missing SSL_CTX object.", ListenS->Host, ListenS->HostIPS, ListenS->PortH);
493 close(FD);
494 continue;
495 }
496 #endif
497
498 #if IPV6_SUPPORT
499 if (Listen_IsIPv6(ListenS)) {
500 memset(&INAddr6, 0, sizeof(INAddr6));
501 INAddr6 = SockAddr6.sin6_addr;
502 PortN = SockAddr6.sin6_port;
503 }
504 else {
505 #endif /* IPV6_SUPPORT */
506 memset(&INAddr, 0, sizeof(INAddr));
507 INAddr = SockAddr.sin_addr;
508 PortN = SockAddr.sin_port;
509 #if IPV6_SUPPORT
510 }
511 #endif /* IPV6_SUPPORT */
512
513 PortH = ntohs(PortN);
514
515 #if IPV6_SUPPORT
516 if (Listen_IsIPv6(ListenS)) {
517 HostIPPT = inet_ntop(AF_INET6, &INAddr6, Host, INET6_ADDRSTRLEN);
518 if (HostIPPT == NULL) {
519 close(FD);
520 sysprint(BITMASK_MAIN, "Failed inet_ntop() for incoming client connection on \"%s(%s):%ld%s\": [%d] %s", ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
521 continue;
522 }
523 }
524 else {
525 HostIPPT = inet_ntop(AF_INET, &INAddr, Host, INET6_ADDRSTRLEN);
526 if (HostIPPT == NULL) {
527 close(FD);
528 sysprint(BITMASK_MAIN, "Failed inet_ntop() for incoming client connection on \"%s(%s):%ld%s\": [%d] %s", ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
529 continue;
530 }
531 }
532 #else
533 HostIPPT = inet_ntoa(INAddr);
534 if (HostIPPT == NULL) {
535 close(FD);
536 sysprint(BITMASK_MAIN, "Failed inet_ntoa() for incoming client connection on \"%s(%s):%ld%s\": [%d] %s", ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
537 continue;
538 }
539 #endif
540 sysprint(BITMASK_MAIN, "Incoming client connection from \"%s:%ld\" on \"%s(%s):%ld%s\".", HostIPPT, PortH, ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""));
541
542 /* SET SOCKET IN NON-BLOCKING MODE */
543
544 #if defined(NBLOCK_SYSV)
545 Flags = 1;
546 Result = ioctl(FD, FIONBIO, &Flags);
547 if (Result <= ERROR) {
548 sysprint(BITMASK_MAIN, "Unable to set socket in non-blocking mode using ioctl() for incoming client \"%s:%ld\" on \"%s(%s):%ld%s\".: [%d] %s", HostIPPT, PortH, ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
549 close(FD);
550 continue;
551 }
552 #else
553 Result = fcntl(FD, F_GETFL, &Flags);
554 if (Result <= ERROR) {
555 sysprint(BITMASK_MAIN, "Unable to get socket flags using fcntl() for incoming client \"%s:%ld\" on \"%s(%s):%ld%s\".: [%d] %s", HostIPPT, PortH, ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
556 close(FD);
557 continue;
558 }
559 #if defined(NBLOCK_BSD)
560 Flags |= O_NDELAY;
561 #elif defined(NBLOCK_POSIX)
562 Flags |= O_NONBLOCK;
563 #else
564 #warning "This system does not support non-blocking sockets?"
565 Flags |= O_NONBLOCK;
566 #endif
567 Result = fcntl(FD, F_SETFL, Flags);
568 if (Result <= ERROR) {
569 sysprint(BITMASK_MAIN, "Unable to set socket in non-blocking mode using fcntl() for incoming client \"%s:%ld\" on \"%s(%s):%ld%s\".: [%d] %s", HostIPPT, PortH, ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
570 close(FD);
571 continue;
572 }
573 #endif
574
575 #if SSL_SUPPORT
576 if (Listen_IsSSL(ListenS)) {
577 assert(IRCPROXY_SSL_SERVER_CTX != NULL);
578 ClientSSL = SSL_new(IRCPROXY_SSL_SERVER_CTX);
579 if (ClientSSL == NULL) {
580 close(FD);
581 sysprint(BITMASK_MAIN, "Failed to create a new SSL structure for incoming client connection from \"%s:%ld\" on \"%s(%s):%ld%s\".: [%d] %s.", HostIPPT, PortH, ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), errno, strerror(errno));
582 continue;
583 }
584 Result = SSL_set_fd(ClientSSL, FD);
585 if (Result <= 0) {
586 signed long int sslerrno = SSL_get_error(ClientSSL, Result);
587 close(FD);
588 SSL_free(ClientSSL);
589 sysprint(BITMASK_MAIN, "Failed to connect the SSL object with file descriptor for incoming client connection from \"%s:%ld\" on \"%s(%s):%ld%s\".: [%ld] %s.", HostIPPT, PortH, ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""), sslerrno, ERR_error_string(sslerrno, NULL));
590 continue;
591 }
592 #if HAVE_SSL_SET_ACCEPT_STATE
593 SSL_set_accept_state(ClientSSL);
594 #endif
595 #if 0
596 SSL_set_mode(ClientSSL, (SSL_MODE_ENABLE_PARTIAL_WRITE|SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER));
597 #endif
598 }
599 #endif /* SSL_SUPPORT */
600
601 ClientS = client_add(ListenS, HostIPPT);
602 if (ClientS == NULL) {
603 close(FD);
604 #if SSL_SUPPORT
605 SSL_free(ClientSSL);
606 #endif
607 sysprint(BITMASK_MAIN, "Failed to allocate client structure for incoming client connection from \"%s:%ld\" on \"%s(%s):%ld%s\".", HostIPPT, PortH, ListenS->Host, ListenS->HostIPS, ListenS->PortH, (Listen_IsSSL(ListenS) ? "(SSL)" : ""));
608 continue;
609 }
610
611 ClientS->FD = FD;
612 ClientS->HostIPS = strrealloc(ClientS->HostIPS, HostIPPT);
613 ClientS->PortH = PortH;
614 ClientS->PortN = PortN;
615
616 #if IPV6_SUPPORT
617 if (Listen_IsIPv6(ListenS)) {
618 memset(&ClientS->INAddr6, 0, sizeof(ClientS->INAddr6));
619 ClientS->INAddr6 = INAddr6;
620 Client_SetIPv6(ClientS);
621 }
622 else {
623 #endif
624 memset(&ClientS->INAddr, 0, sizeof(ClientS->INAddr));
625 ClientS->INAddr = INAddr;
626 #if IPV6_SUPPORT
627 }
628 #endif
629
630 #if SSL_SUPPORT
631 if (Listen_IsSSL(ListenS)) {
632 Client_SetSSL(ClientS);
633 ClientS->SSL_H = ClientSSL;
634 }
635 #endif
636
637 ClientS->ListenS = ListenS;
638
639 client_resolve(ClientS);
640
641 }
642 }
643
644 }
645
646 /* LISTEN_CLOSEALL FUNCTION - JONAS (09.06.2001) */
647
648 unsigned short int listen_closeall(const char *const MessagePT, ...) {
649
650 struct Listen_Struct *ListenS = NULL;
651 struct Listen_Struct *ListenS_DEL = NULL;
652 unsigned short int Count = 0;
653
654 for (ListenS = Listen_Head ; ListenS != NULL ;) {
655 if (Listen_IsResolving(ListenS)) {
656 #if HAVE_ARES_CANCELQUERY
657 ares_cancelquery(Ares_Channel, ListenS);
658 #endif
659 ++Count;
660 Listen_SetRemove(ListenS);
661 continue;
662 }
663 listen_stop(ListenS);
664 ListenS_DEL = ListenS;
665 ListenS = ListenS->Next;
666 listen_rem(ListenS_DEL);
667 }
668
669 return(Count);
670
671 }
672
673
674