]> jfr.im git - irc/rqf/shadowircd.git/blobdiff - src/listener.c
Merging s_newconf.c from nenolod and me
[irc/rqf/shadowircd.git] / src / listener.c
index 7ed9a695c836e62d8ecab7c7d914e04047d95071..e8647cfd7f73a147b17e31dc49ec7a466890d74e 100644 (file)
@@ -21,7 +21,7 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  *  USA
  *
- *  $Id: listener.c 1675 2006-06-15 22:32:23Z jilles $
+ *  $Id: listener.c 3460 2007-05-18 20:31:33Z jilles $
  */
 
 #include "stdinc.h"
 #include "ircd.h"
 #include "ircd_defs.h"
 #include "numeric.h"
-#include "commio.h"
 #include "s_conf.h"
 #include "s_newconf.h"
 #include "s_stats.h"
 #include "send.h"
-#include "memory.h"
 #include "s_auth.h"
 #include "reject.h"
+#include "s_conf.h"
+#include "hostmask.h"
 
 #ifndef INADDR_NONE
 #define INADDR_NONE ((unsigned int) 0xffffffff)
@@ -58,7 +58,7 @@ static listener_t *ListenerPollList = NULL;
 static listener_t *
 make_listener(struct irc_sockaddr_storage *addr)
 {
-       listener_t *listener = (listener_t *) MyMalloc(sizeof(listener_t));
+       listener_t *listener = (listener_t *) rb_malloc(sizeof(listener_t));
        s_assert(0 != listener);
 
        listener->name = me.name;
@@ -93,7 +93,7 @@ free_listener(listener_t *listener)
        }
 
        /* free */
-       MyFree(listener);
+       rb_free(listener);
 }
 
 #define PORTNAMELEN 6          /* ":31337" */
@@ -119,7 +119,7 @@ get_listener_name(const listener_t *listener)
 #endif
                port = ntohs(((const struct sockaddr_in *)&listener->addr)->sin_port);  
 
-       ircsnprintf(buf, sizeof(buf), "%s[%s/%u]", me.name, listener->name, port);
+       rb_snprintf(buf, sizeof(buf), "%s[%s/%u]", me.name, listener->name, port);
        return buf;
 }
 
@@ -172,7 +172,7 @@ inetport(listener_t *listener)
         * At first, open a new socket
         */
        
-       fd = comm_socket(listener->addr.ss_family, SOCK_STREAM, 0, "Listener socket");
+       fd = rb_socket(listener->addr.ss_family, SOCK_STREAM, 0, "Listener socket");
 
 #ifdef IPV6
        if(listener->addr.ss_family == AF_INET6)
@@ -194,7 +194,11 @@ inetport(listener_t *listener)
                }       
        }
 
-
+       /*
+        * At one point, we enforced a strange arbitrary limit here.
+        * We no longer do this, and just check if the fd is valid or not.
+        *    -nenolod
+        */
        if(fd == -1)
        {
                report_error("opening listener socket %s:%s",
@@ -202,14 +206,7 @@ inetport(listener_t *listener)
                             get_listener_name(listener), errno);
                return 0;
        }
-       else if((HARD_FDLIMIT - 10) < fd)
-       {
-               report_error("no more connections left for listener %s:%s",
-                            get_listener_name(listener), 
-                            get_listener_name(listener), errno);
-               comm_close(fd);
-               return 0;
-       }
+
        /*
         * XXX - we don't want to do all this crap for a listener
         * set_sock_opts(listener);
@@ -219,7 +216,7 @@ inetport(listener_t *listener)
                report_error("setting SO_REUSEADDR for listener %s:%s",
                             get_listener_name(listener), 
                             get_listener_name(listener), errno);
-               comm_close(fd);
+               rb_close(fd);
                return 0;
        }
 
@@ -233,7 +230,7 @@ inetport(listener_t *listener)
                report_error("binding listener socket %s:%s",
                             get_listener_name(listener), 
                             get_listener_name(listener), errno);
-               comm_close(fd);
+               rb_close(fd);
                return 0;
        }
 
@@ -242,7 +239,7 @@ inetport(listener_t *listener)
                report_error("listen failed for %s:%s", 
                             get_listener_name(listener), 
                             get_listener_name(listener), errno);
-               comm_close(fd);
+               rb_close(fd);
                return 0;
        }
 
@@ -404,7 +401,7 @@ close_listener(listener_t *listener)
                return;
        if(listener->fd >= 0)
        {
-               comm_close(listener->fd);
+               rb_close(listener->fd);
                listener->fd = -1;
        }
 
@@ -443,7 +440,7 @@ close_listeners()
  * any client list yet.
  */
 static void
-add_connection(listener_t *listener, int fd, struct sockaddr *sai)
+add_connection(listener_t *listener, int fd, struct sockaddr *sai, int exempt)
 {
        struct Client *new_client;
        s_assert(NULL != listener);
@@ -466,20 +463,19 @@ add_connection(listener_t *listener, int fd, struct sockaddr *sai)
 
        strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
 
-#ifdef IPV6
-       if(new_client->localClient->ip.ss_family == AF_INET6 && ConfigFileEntry.dot_in_ip6_addr == 1)
-       {
-               strlcat(new_client->host, ".", sizeof(new_client->host));
-       }
-#endif
-
-       new_client->localClient->fd = fd;
+       new_client->localClient->F = rb_add_fd(fd);
 
        new_client->localClient->listener = listener;
        ++listener->ref_count;
 
-       if(check_reject(new_client))
-               return; 
+       if(!exempt)
+       {
+               if(check_reject(new_client))
+                       return; 
+               if(add_unknown_ip(new_client))
+                       return;
+       }
+
        start_auth(new_client);
 }
 
@@ -488,14 +484,13 @@ static void
 accept_connection(int pfd, void *data)
 {
        static time_t last_oper_notice = 0;
-
        struct irc_sockaddr_storage sai;
        socklen_t addrlen = sizeof(sai);
        int fd;
        listener_t *listener = data;
-    struct ConfItem *aconf;
-    char buf[BUFSIZE];
-    
+       struct ConfItem *aconf;
+       char buf[BUFSIZE];
+
        s_assert(listener != NULL);
        if(listener == NULL)
                return;
@@ -511,11 +506,11 @@ accept_connection(int pfd, void *data)
         * be accepted until some old is closed first.
         */
 
-       fd = comm_accept(listener->fd, (struct sockaddr *)&sai, &addrlen);
+       fd = rb_accept(listener->fd, (struct sockaddr *)&sai, &addrlen);
        if(fd < 0)
        {
                /* Re-register a new IO request for the next accept .. */
-               comm_setselect(listener->fd, FDLIST_SERVICE,
+               rb_setselect(listener->fd, FDLIST_SERVICE,
                               COMM_SELECT_READ, accept_connection, listener, 0);
                return;
        }
@@ -525,60 +520,63 @@ accept_connection(int pfd, void *data)
 
        /*
         * check for connection limit
+        * TBD: this is stupid... either we have a socket or we don't. -nenolod
         */
-       if((MAXCONNECTIONS - 10) < fd)
+       if((rb_get_maxconnections() - 10) < fd)
        {
                ++ServerStats->is_ref;
                /*
                 * slow down the whining to opers bit
                 */
-               if((last_oper_notice + 20) <= CurrentTime)
+               if((last_oper_notice + 20) <= rb_current_time())
                {
                        sendto_realops_snomask(SNO_GENERAL, L_ALL,
                                             "All connections in use. (%s)",
                                             get_listener_name(listener));
-                       last_oper_notice = CurrentTime;
+                       last_oper_notice = rb_current_time();
                }
 
                write(fd, "ERROR :All connections in use\r\n", 32);
-               comm_close(fd);
+               rb_close(fd);
                /* Re-register a new IO request for the next accept .. */
-               comm_setselect(listener->fd, FDLIST_SERVICE,
+               rb_setselect(listener->fd, FDLIST_SERVICE,
                               COMM_SELECT_READ, accept_connection, listener, 0);
                return;
        }
 
        /* Do an initial check we aren't connecting too fast or with too many
         * from this IP... */
-       if((aconf = conf_connect_allowed((struct sockaddr *)&sai, sai.ss_family)) != NULL)
+       aconf = find_dline((struct sockaddr *) &sai, sai.ss_family);
+       /* check it wasn't an exempt */
+       if (aconf != NULL && (aconf->status & CONF_EXEMPTDLINE) == 0)
        {
                ServerStats->is_ref++;
 
-        if(ConfigFileEntry.dline_with_reason)
-        {
-            if (ircsnprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (sizeof(buf)-1))
-            {
-                buf[sizeof(buf) - 3] = '\r';
-                buf[sizeof(buf) - 2] = '\n';
-                buf[sizeof(buf) - 1] = '\0';
-            }
-        }
-        else
-           ircsprintf(buf, "ERROR :You have been D-lined.\r\n");
+               if(ConfigFileEntry.dline_with_reason)
+               {
+                       if (rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (sizeof(buf)-1))
+                       {
+                               buf[sizeof(buf) - 3] = '\r';
+                               buf[sizeof(buf) - 2] = '\n';
+                               buf[sizeof(buf) - 1] = '\0';
+                       }
+               }
+               else
+                       rb_sprintf(buf, "ERROR :You have been D-lined.\r\n");
         
-        write(fd, buf, strlen(buf));
-               comm_close(fd);
+               write(fd, buf, strlen(buf));
+               rb_close(fd);
 
                /* Re-register a new IO request for the next accept .. */
-               comm_setselect(listener->fd, FDLIST_SERVICE,
+               rb_setselect(listener->fd, FDLIST_SERVICE,
                               COMM_SELECT_READ, accept_connection, listener, 0);
                return;
        }
 
        ServerStats->is_ac++;
-       add_connection(listener, fd, (struct sockaddr *)&sai);
+       add_connection(listener, fd, (struct sockaddr *)&sai, aconf ? 1 : 0);
 
        /* Re-register a new IO request for the next accept .. */
-       comm_setselect(listener->fd, FDLIST_SERVICE, COMM_SELECT_READ,
+       rb_setselect(listener->fd, FDLIST_SERVICE, COMM_SELECT_READ,
                       accept_connection, listener, 0);
 }