]>
jfr.im git - irc/quakenet/snircd.git/blob - ircd/listener.c
1 /************************************************************************
2 * IRC - Internet Relay Chat, src/listener.c
3 * Copyright (C) 1999 Thomas Helvey <tomh@inxpress.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 1, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * @brief Implementation for handling listening sockets.
21 * @version $Id: listener.c,v 1.28 2005/08/15 23:22:50 entrope Exp $
28 #include "ircd_alloc.h"
29 #include "ircd_events.h"
30 #include "ircd_features.h"
32 #include "ircd_osdep.h"
33 #include "ircd_reply.h"
34 #include "ircd_snprintf.h"
35 #include "ircd_string.h"
43 #include "sys.h" /* MAXCLIENTS */
45 /* #include <assert.h> -- Now using assert in ircd_log.h */
52 #include <sys/socket.h>
54 /** List of listening sockets. */
55 struct Listener
* ListenerPollList
= 0;
57 static void accept_connection(struct Event
* ev
);
59 /** Allocate and initialize a new Listener structure for a particular
61 * @param[in] port Port number to listen on.
62 * @param[in] addr Local address to listen on.
63 * @return Newly allocated and initialized Listener.
65 static struct Listener
* make_listener(int port
, const struct irc_in_addr
*addr
)
67 struct Listener
* listener
=
68 (struct Listener
*) MyMalloc(sizeof(struct Listener
));
69 assert(0 != listener
);
71 memset(listener
, 0, sizeof(struct Listener
));
74 listener
->addr
.port
= port
;
75 memcpy(&listener
->addr
.addr
, addr
, sizeof(listener
->addr
.addr
));
77 #ifdef NULL_POINTER_NOT_ZERO
78 listener
->next
= NULL
;
79 listener
->conf
= NULL
;
84 /** Deallocate a Listener structure.
85 * @param[in] listener Listener to be freed.
87 static void free_listener(struct Listener
* listener
)
89 assert(0 != listener
);
93 /** Maximum length for a port number. */
94 #define PORTNAMELEN 10 /* ":31337" */
96 /** Return displayable listener name and port.
97 * @param[in] listener %Listener to format as a text string.
98 * @return Pointer to a static buffer that contains "server.name:6667".
100 const char* get_listener_name(const struct Listener
* listener
)
102 static char buf
[HOSTLEN
+ PORTNAMELEN
+ 4];
103 assert(0 != listener
);
104 ircd_snprintf(0, buf
, sizeof(buf
), "%s:%u", cli_name(&me
), listener
->addr
.port
);
108 /** Count allocated listeners and the memory they use.
109 * @param[out] count_out Receives number of allocated listeners.
110 * @param[out] size_out Receives bytes used by listeners.
112 void count_listener_memory(int* count_out
, size_t* size_out
)
116 assert(0 != count_out
);
117 assert(0 != size_out
);
118 for (l
= ListenerPollList
; l
; l
= l
->next
)
121 *size_out
= count
* sizeof(struct Listener
);
124 /** Report listening ports to a client.
125 * @param[in] sptr Client requesting statistics.
126 * @param[in] sd Stats descriptor for request (ignored).
127 * @param[in] param Extra parameter from user (port number to search for).
129 void show_ports(struct Client
* sptr
, const struct StatDesc
* sd
,
132 struct Listener
*listener
= 0;
134 int show_hidden
= IsOper(sptr
);
135 int count
= (IsOper(sptr
) || MyUser(sptr
)) ? 100 : 8;
143 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
) {
144 if (port
&& port
!= listener
->addr
.port
)
146 flags
[0] = (listener
->server
) ? 'S' : 'C';
147 if (listener
->hidden
) {
156 send_reply(sptr
, RPL_STATSPLINE
, listener
->addr
.port
, listener
->ref_count
,
157 flags
, (listener
->active
) ? "active" : "disabled");
164 * inetport - create a listener socket in the AF_INET domain,
165 * bind it to the port given in 'port' and listen to it
166 * returns true (1) if successful false (0) on error.
168 * If the operating system has a define for SOMAXCONN, use it, otherwise
169 * use HYBRID_SOMAXCONN -Dianora
170 * NOTE: Do this in os_xxxx.c files
173 #define HYBRID_SOMAXCONN SOMAXCONN
175 /** Maximum length of socket connection backlog. */
176 #define HYBRID_SOMAXCONN 64
179 /** Open listening socket for \a listener.
180 * @param[in,out] listener Listener to make a socket for.
181 * @return Non-zero on success, zero on failure.
183 static int inetport(struct Listener
* listener
)
188 * At first, open a new socket
190 fd
= os_socket(&listener
->addr
, SOCK_STREAM
, get_listener_name(listener
));
194 * Set the buffer sizes for the listener. Accepted connections
195 * inherit the accepting sockets settings for SO_RCVBUF S_SNDBUF
196 * The window size is set during the SYN ACK so setting it anywhere
197 * else has no effect whatsoever on the connection.
198 * NOTE: this must be set before listen is called
200 if (!os_set_sockbufs(fd
,
201 (listener
->server
) ? feature_int(FEAT_SOCKSENDBUF
) : CLIENT_TCP_WINDOW
,
202 (listener
->server
) ? feature_int(FEAT_SOCKRECVBUF
) : CLIENT_TCP_WINDOW
)) {
203 report_error(SETBUFS_ERROR_MSG
, get_listener_name(listener
), errno
);
207 if (!os_set_listen(fd
, HYBRID_SOMAXCONN
)) {
208 report_error(LISTEN_ERROR_MSG
, get_listener_name(listener
), errno
);
213 * Set the TOS bits - this is nonfatal if it doesn't stick.
215 if (!os_set_tos(fd
,feature_int((listener
->server
)?FEAT_TOS_SERVER
: FEAT_TOS_CLIENT
))) {
216 report_error(TOS_ERROR_MSG
, get_listener_name(listener
), errno
);
219 if (!socket_add(&listener
->socket
, accept_connection
, (void*) listener
,
220 SS_LISTENING
, 0, fd
)) {
221 /* Error should already have been reported to the logs */
231 /** Find the listener (if any) for a particular port and address.
232 * @param[in] port Port number to search for.
233 * @param[in] addr Local address to search for.
234 * @return Listener that matches (or NULL if none match).
236 static struct Listener
* find_listener(int port
, const struct irc_in_addr
*addr
)
238 struct Listener
* listener
;
239 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
) {
240 if (port
== listener
->addr
.port
&& !memcmp(addr
, &listener
->addr
.addr
, sizeof(*addr
)))
246 /** Make sure we have a listener for \a port on \a vhost_ip.
247 * If one does not exist, create it. Then mark it as active and set
248 * the peer mask, server, and hidden flags according to the other
250 * @param[in] port Port number to listen on.
251 * @param[in] vhost_ip Local address to listen on.
252 * @param[in] mask Address mask to accept connections from.
253 * @param[in] is_server Non-zero if the port should only accept server connections.
254 * @param[in] is_hidden Non-zero if the port should be hidden from /STATS P output.
256 void add_listener(int port
, const char* vhost_ip
, const char* mask
,
257 int is_server
, int is_hidden
)
259 struct Listener
* listener
;
260 struct irc_in_addr vaddr
;
263 * if no port in conf line, don't bother
268 memset(&vaddr
, 0, sizeof(vaddr
));
270 if (!EmptyString(vhost_ip
)
271 && strcmp(vhost_ip
, "*")
272 && !ircd_aton(&vaddr
, vhost_ip
))
275 listener
= find_listener(port
, &vaddr
);
277 listener
= make_listener(port
, &vaddr
);
278 listener
->active
= 1;
279 listener
->hidden
= is_hidden
;
280 listener
->server
= is_server
;
282 ipmask_parse(mask
, &listener
->mask
, &listener
->mask_bits
);
284 listener
->mask_bits
= 0;
286 if (listener
->fd
>= 0) {
287 /* If the listener is already open, do not try to re-open. */
289 else if (inetport(listener
)) {
290 listener
->next
= ListenerPollList
;
291 ListenerPollList
= listener
;
294 free_listener(listener
);
297 /** Mark all listeners as closing (inactive).
298 * This is done so unused listeners are closed after a rehash.
300 void mark_listeners_closing(void)
302 struct Listener
* listener
;
303 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
304 listener
->active
= 0;
307 /** Close a single listener.
308 * @param[in] listener Listener to close.
310 void close_listener(struct Listener
* listener
)
312 assert(0 != listener
);
314 * remove from listener list
316 if (listener
== ListenerPollList
)
317 ListenerPollList
= listener
->next
;
319 struct Listener
* prev
= ListenerPollList
;
320 for ( ; prev
; prev
= prev
->next
) {
321 if (listener
== prev
->next
) {
322 prev
->next
= listener
->next
;
327 if (-1 < listener
->fd
)
329 socket_del(&listener
->socket
);
332 /** Close all inactive listeners. */
333 void close_listeners(void)
335 struct Listener
* listener
;
336 struct Listener
* listener_next
= 0;
338 * close all 'extra' listening ports we have
340 for (listener
= ListenerPollList
; listener
; listener
= listener_next
) {
341 listener_next
= listener
->next
;
342 if (0 == listener
->active
&& 0 == listener
->ref_count
)
343 close_listener(listener
);
347 /** Dereference the listener previously associated with a client.
348 * @param[in] listener Listener to dereference.
350 void release_listener(struct Listener
* listener
)
352 assert(0 != listener
);
353 assert(0 < listener
->ref_count
);
354 if (0 == --listener
->ref_count
&& !listener
->active
)
355 close_listener(listener
);
358 /** Accept a connection on a listener.
359 * @param[in] ev Socket callback structure.
361 static void accept_connection(struct Event
* ev
)
363 struct Listener
* listener
;
364 struct irc_sockaddr addr
;
367 assert(0 != ev_socket(ev
));
368 assert(0 != s_data(ev_socket(ev
)));
370 listener
= (struct Listener
*) s_data(ev_socket(ev
));
372 if (ev_type(ev
) == ET_DESTROY
) /* being destroyed */
373 free_listener(listener
);
375 assert(ev_type(ev
) == ET_ACCEPT
|| ev_type(ev
) == ET_ERROR
);
377 listener
->last_accept
= CurrentTime
;
379 * There may be many reasons for error return, but
380 * in otherwise correctly working environment the
381 * probable cause is running out of file descriptors
382 * (EMFILE, ENFILE or others?). The man pages for
383 * accept don't seem to list these as possible,
384 * although it's obvious that it may happen here.
385 * Thus no specific errors are tested at this
386 * point, just assume that connections cannot
387 * be accepted until some old is closed first.
389 * This piece of code implements multi-accept, based
390 * on the idea that poll/select can only be efficient,
391 * if we succeed in handling all available events,
392 * i.e. accept all pending connections.
394 * http://www.hpl.hp.com/techreports/2000/HPL-2000-174.html
398 if ((fd
= os_accept(listener
->fd
, &addr
)) == -1)
400 if (errno
== EAGAIN
||
402 errno
== EWOULDBLOCK
)
405 /* Lotsa admins seem to have problems with not giving enough file
406 * descriptors to their server so we'll add a generic warning mechanism
407 * here. If it turns out too many messages are generated for
408 * meaningless reasons we can filter them back.
410 sendto_opmask_butone(0, SNO_TCPCOMMON
,
411 "Unable to accept connection: %m");
415 * check for connection limit. If this fd exceeds the limit,
416 * all further accept()ed connections will also exceed it.
417 * Enable the server to clear out other connections before
418 * continuing to accept() new connections.
420 if (fd
> MAXCLIENTS
- 1)
422 ++ServerStats
->is_ref
;
423 send(fd
, "ERROR :All connections in use\r\n", 32, 0);
428 * check to see if listener is shutting down. Continue
429 * to accept(), because it makes sense to clear our the
430 * socket's queue as fast as possible.
432 if (!listener
->active
)
434 ++ServerStats
->is_ref
;
435 send(fd
, "ERROR :Use another port\r\n", 25, 0);
440 * check to see if connection is allowed for this address mask
442 if (!ipmask_check(&addr
.addr
, &listener
->mask
, listener
->mask_bits
))
444 ++ServerStats
->is_ref
;
445 send(fd
, "ERROR :Use another port\r\n", 25, 0);
449 ++ServerStats
->is_ac
;
450 /* nextping = CurrentTime; */
451 add_connection(listener
, fd
);