X-Git-Url: https://jfr.im/git/irc/rqf/shadowircd.git/blobdiff_plain/c48eb01dbed808464cc3f702cc910f3962351825..5d3b05b9b624b745527e84f605c9cb04c6831c78:/src/listener.c diff --git a/src/listener.c b/src/listener.c index 98dcde8..764a5c1 100644 --- a/src/listener.c +++ b/src/listener.c @@ -52,7 +52,7 @@ static const struct in6_addr in6addr_any = #endif static listener_t *ListenerPollList = NULL; -static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data); +static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data); static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data); static listener_t * @@ -195,60 +195,60 @@ inetport(listener_t *listener) } } - if(F == NULL) - { - report_error("opening listener socket %s:%s", - get_listener_name(listener), - get_listener_name(listener), errno); - return 0; - } - else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/ - { - report_error("no more connections left for listener %s:%s", - get_listener_name(listener), - get_listener_name(listener), errno); - rb_close(F); - return 0; + if(F == NULL) + { + report_error("opening listener socket %s:%s", + get_listener_name(listener), + get_listener_name(listener), errno); + return 0; + } + else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/ + { + report_error("no more connections left for listener %s:%s", + get_listener_name(listener), + get_listener_name(listener), errno); + rb_close(F); + return 0; } - /* - * XXX - we don't want to do all this crap for a listener - * set_sock_opts(listener); - */ - if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt))) - { - report_error("setting SO_REUSEADDR for listener %s:%s", - get_listener_name(listener), - get_listener_name(listener), errno); - rb_close(F); - return 0; + /* + * XXX - we don't want to do all this crap for a listener + * set_sock_opts(listener); + */ + if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt))) + { + report_error("setting SO_REUSEADDR for listener %s:%s", + get_listener_name(listener), + get_listener_name(listener), errno); + rb_close(F); + return 0; } - /* - * Bind a port to listen for new connections if port is non-null, - * else assume it is already open and try get something from it. - */ - - if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr))) - { - report_error("binding listener socket %s:%s", - get_listener_name(listener), - get_listener_name(listener), errno); - rb_close(F); - return 0; + /* + * Bind a port to listen for new connections if port is non-null, + * else assume it is already open and try get something from it. + */ + + if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr))) + { + report_error("binding listener socket %s:%s", + get_listener_name(listener), + get_listener_name(listener), errno); + rb_close(F); + return 0; } - if((ret = rb_listen(F, RATBOX_SOMAXCONN))) - { - report_error("listen failed for %s:%s", - get_listener_name(listener), - get_listener_name(listener), errno); - rb_close(F); - return 0; + if((ret = rb_listen(F, RATBOX_SOMAXCONN))) + { + report_error("listen failed for %s:%s", + get_listener_name(listener), + get_listener_name(listener), errno); + rb_close(F); + return 0; } - listener->F = F; - + listener->F = F; + rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener); return 1; } @@ -273,9 +273,9 @@ find_listener(struct rb_sockaddr_storage *addr) if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr && in4->sin_port == lin4->sin_port ) { - if(listener->F == NULL) - last_closed = listener; - else + if(listener->F == NULL) + last_closed = listener; + else return(listener); } break; @@ -288,9 +288,9 @@ find_listener(struct rb_sockaddr_storage *addr) if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) && in6->sin6_port == lin6->sin6_port) { - if(listener->F == NULL) - last_closed = listener; - else + if(listener->F == NULL) + last_closed = listener; + else return(listener); } break; @@ -374,7 +374,7 @@ add_listener(int port, const char *vhost_ip, int family) } if((listener = find_listener(&vaddr))) { - if(listener->F != NULL) + if(listener->F != NULL) return; } else @@ -401,10 +401,10 @@ close_listener(listener_t *listener) s_assert(listener != NULL); if(listener == NULL) return; - if(listener->F != NULL) - { - rb_close(listener->F); - listener->F = NULL; + if(listener->F != NULL) + { + rb_close(listener->F); + listener->F = NULL; } listener->active = 0; @@ -441,7 +441,7 @@ close_listeners() * The client is sent to the auth module for verification, and not put in * any client list yet. */ -static void +static void add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, int exempt) { struct Client *new_client; @@ -481,79 +481,79 @@ add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, int start_auth(new_client); } -static int -accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data) -{ - struct Listener *listener = (struct Listener *)data; - char buf[BUFSIZE]; - struct ConfItem *aconf; - static time_t last_oper_notice = 0; - - if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */ - { - ++ServerStats->is_ref; - /* - * slow down the whining to opers bit - */ - if((last_oper_notice + 20) <= rb_current_time()) - { - sendto_realops_flags(SNO_GENERAL, L_ALL, - "All connections in use. (%s)", - get_listener_name(listener)); - last_oper_notice = rb_current_time(); - } - - rb_write(F, "ERROR :All connections in use\r\n", 32); - rb_close(F); - /* Re-register a new IO request for the next accept .. */ - return 0; - } - - aconf = find_dline(addr, AF_INET); - if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE)) - return 1; - - /* Do an initial check we aren't connecting too fast or with too many - * from this IP... */ - if(aconf != NULL) - { - ServerStats->is_ref++; - - if(ConfigFileEntry.dline_with_reason) - { - if (rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (int)(sizeof(buf)-1)) - { - buf[sizeof(buf) - 3] = '\r'; - buf[sizeof(buf) - 2] = '\n'; - buf[sizeof(buf) - 1] = '\0'; - } - } - else - strcpy(buf, "ERROR :You have been D-lined.\r\n"); - - rb_write(F, buf, strlen(buf)); - rb_close(F); - return 0; - } - - return 1; -} - -static void -accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data) -{ - struct Listener *listener = data; - struct rb_sockaddr_storage lip; - unsigned int locallen = sizeof(struct rb_sockaddr_storage); - - ServerStats->is_ac++; - - if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0) - { - /* this shouldn't fail so... */ - /* XXX add logging of this */ - rb_close(F); - } - - add_connection(listener, F, addr, 1); +static int +accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data) +{ + struct Listener *listener = (struct Listener *)data; + char buf[BUFSIZE]; + struct ConfItem *aconf; + static time_t last_oper_notice = 0; + + if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */ + { + ++ServerStats->is_ref; + /* + * slow down the whining to opers bit + */ + if((last_oper_notice + 20) <= rb_current_time()) + { + sendto_realops_flags(SNO_GENERAL, L_ALL, + "All connections in use. (%s)", + get_listener_name(listener)); + last_oper_notice = rb_current_time(); + } + + rb_write(F, "ERROR :All connections in use\r\n", 32); + rb_close(F); + /* Re-register a new IO request for the next accept .. */ + return 0; + } + + aconf = find_dline(addr, AF_INET); + if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE)) + return 1; + + /* Do an initial check we aren't connecting too fast or with too many + * from this IP... */ + if(aconf != NULL) + { + ServerStats->is_ref++; + + if(ConfigFileEntry.dline_with_reason) + { + if (rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (int)(sizeof(buf)-1)) + { + buf[sizeof(buf) - 3] = '\r'; + buf[sizeof(buf) - 2] = '\n'; + buf[sizeof(buf) - 1] = '\0'; + } + } + else + strcpy(buf, "ERROR :You have been D-lined.\r\n"); + + rb_write(F, buf, strlen(buf)); + rb_close(F); + return 0; + } + + return 1; +} + +static void +accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data) +{ + struct Listener *listener = data; + struct rb_sockaddr_storage lip; + unsigned int locallen = sizeof(struct rb_sockaddr_storage); + + ServerStats->is_ac++; + + if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0) + { + /* this shouldn't fail so... */ + /* XXX add logging of this */ + rb_close(F); + } + + add_connection(listener, F, addr, 1); }