]> jfr.im git - irc/rqf/shadowircd.git/blobdiff - src/listener.c
use an rpath to avoid LD_LIBRARY_PATH stuff
[irc/rqf/shadowircd.git] / src / listener.c
index 304b51cf6705dee437aed274bee2ae1e809a910c..764a5c131d2f18cda8eafed1c84184d5ffa44f36 100644 (file)
@@ -51,19 +51,19 @@ static const struct in6_addr in6addr_any =
 { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
 #endif 
 
-static PF accept_connection;
-
 static listener_t *ListenerPollList = NULL;
+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 *
-make_listener(struct irc_sockaddr_storage *addr)
+make_listener(struct rb_sockaddr_storage *addr)
 {
        listener_t *listener = (listener_t *) rb_malloc(sizeof(listener_t));
        s_assert(0 != listener);
-
        listener->name = me.name;
-       listener->fd = -1;
-       memcpy(&listener->addr, addr, sizeof(struct irc_sockaddr_storage));
+       listener->F = NULL;
+
+       memcpy(&listener->addr, addr, sizeof(struct rb_sockaddr_storage));
        listener->next = NULL;
        return listener;
 }
@@ -165,14 +165,15 @@ show_ports(struct Client *source_p)
 static int
 inetport(listener_t *listener)
 {
-       int fd;
+       rb_fde_t *F;
+       int ret;
        int opt = 1;
 
        /*
         * At first, open a new socket
         */
        
-       fd = rb_socket(listener->addr.ss_family, SOCK_STREAM, 0, "Listener socket");
+       F = rb_socket(GET_SS_FAMILY(&listener->addr), SOCK_STREAM, 0, "Listener socket");
 
 #ifdef IPV6
        if(listener->addr.ss_family == AF_INET6)
@@ -194,29 +195,32 @@ 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)
+       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(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)))
+       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(fd);
+               rb_close(F);
                return 0;
        }
 
@@ -225,34 +229,32 @@ inetport(listener_t *listener)
         * else assume it is already open and try get something from it.
         */
 
-       if(bind(fd, (struct sockaddr *) &listener->addr, GET_SS_LEN(listener->addr)))
+       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(fd);
+               rb_close(F);
                return 0;
        }
 
-       if(listen(fd, RATBOX_SOMAXCONN))
+       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(fd);
+               rb_close(F);
                return 0;
        }
 
-       listener->fd = fd;
+       listener->F = F;
 
-       /* Listen completion events are READ events .. */
-
-       accept_connection(fd, listener);
+       rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener);
        return 1;
 }
 
 static listener_t *
-find_listener(struct irc_sockaddr_storage *addr)
+find_listener(struct rb_sockaddr_storage *addr)
 {
        listener_t *listener = NULL;
        listener_t *last_closed = NULL;
@@ -271,7 +273,7 @@ find_listener(struct irc_sockaddr_storage *addr)
                                if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr && 
                                        in4->sin_port == lin4->sin_port )
                                {
-                                       if(listener->fd == -1)
+                                       if(listener->F == NULL)
                                                last_closed = listener;
                                        else
                                                return(listener);
@@ -286,7 +288,7 @@ find_listener(struct irc_sockaddr_storage *addr)
                                if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) &&
                                  in6->sin6_port == lin6->sin6_port)
                                {
-                                       if(listener->fd == -1)
+                                       if(listener->F == NULL)
                                                last_closed = listener;
                                        else
                                                return(listener);
@@ -314,7 +316,7 @@ void
 add_listener(int port, const char *vhost_ip, int family)
 {
        listener_t *listener;
-       struct irc_sockaddr_storage vaddr;
+       struct rb_sockaddr_storage vaddr;
 
        /*
         * if no port in conf line, don't bother
@@ -358,12 +360,12 @@ add_listener(int port, const char *vhost_ip, int family)
        switch(family)
        {
                case AF_INET:
-                       SET_SS_LEN(vaddr, sizeof(struct sockaddr_in));
+                       SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in));
                        ((struct sockaddr_in *)&vaddr)->sin_port = htons(port);
                        break;
 #ifdef IPV6
                case AF_INET6:
-                       SET_SS_LEN(vaddr, sizeof(struct sockaddr_in6));
+                       SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6));
                        ((struct sockaddr_in6 *)&vaddr)->sin6_port = htons(port);
                        break;
 #endif
@@ -372,7 +374,7 @@ add_listener(int port, const char *vhost_ip, int family)
        }
        if((listener = find_listener(&vaddr)))
        {
-               if(listener->fd > -1)
+               if(listener->F != NULL)
                        return;
        }
        else
@@ -382,7 +384,7 @@ add_listener(int port, const char *vhost_ip, int family)
                ListenerPollList = listener;
        }
 
-       listener->fd = -1;
+       listener->F = NULL;
 
        if(inetport(listener))
                listener->active = 1;
@@ -399,10 +401,10 @@ close_listener(listener_t *listener)
        s_assert(listener != NULL);
        if(listener == NULL)
                return;
-       if(listener->fd >= 0)
+       if(listener->F != NULL)
        {
-               rb_close(listener->fd);
-               listener->fd = -1;
+               rb_close(listener->F);
+               listener->F = NULL;
        }
 
        listener->active = 0;
@@ -440,7 +442,7 @@ close_listeners()
  * any client list yet.
  */
 static void
-add_connection(listener_t *listener, int fd, struct sockaddr *sai, int exempt)
+add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, int exempt)
 {
        struct Client *new_client;
        s_assert(NULL != listener);
@@ -451,7 +453,7 @@ add_connection(listener_t *listener, int fd, struct sockaddr *sai, int exempt)
         */
        new_client = make_client(NULL);
 
-       memcpy(&new_client->localClient->ip, sai, sizeof(struct irc_sockaddr_storage));
+       memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage));
 
        /* 
         * copy address to 'sockhost' as a string, copy it to host too
@@ -463,7 +465,7 @@ add_connection(listener_t *listener, int fd, struct sockaddr *sai, int exempt)
 
        strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
 
-       new_client->localClient->F = rb_add_fd(fd);
+       new_client->localClient->F = F;
 
        new_client->localClient->listener = listener;
        ++listener->ref_count;
@@ -479,82 +481,47 @@ add_connection(listener_t *listener, int fd, struct sockaddr *sai, int exempt)
        start_auth(new_client);
 }
 
-
-static void
-accept_connection(int pfd, void *data)
+static int
+accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, 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;
+       struct Listener *listener = (struct Listener *)data;
        char buf[BUFSIZE];
+       struct ConfItem *aconf;
+       static time_t last_oper_notice = 0;
 
-       s_assert(listener != NULL);
-       if(listener == NULL)
-               return;
-       /*
-        * There may be many reasons for error return, but
-        * in otherwise correctly working environment the
-        * probable cause is running out of file descriptors
-        * (EMFILE, ENFILE or others?). The man pages for
-        * accept don't seem to list these as possible,
-        * although it's obvious that it may happen here.
-        * Thus no specific errors are tested at this
-        * point, just assume that connections cannot
-        * be accepted until some old is closed first.
-        */
-
-       fd = rb_accept(listener->fd, (struct sockaddr *)&sai, &addrlen);
-       if(fd < 0)
-       {
-               /* Re-register a new IO request for the next accept .. */
-               rb_setselect(listener->fd, FDLIST_SERVICE,
-                              COMM_SELECT_READ, accept_connection, listener, 0);
-               return;
-       }
-
-       /* This needs to be done here, otherwise we break dlines */
-       mangle_mapped_sockaddr((struct sockaddr *)&sai);
-
-       /*
-        * check for connection limit
-        * TBD: this is stupid... either we have a socket or we don't. -nenolod
-        */
-       if((rb_get_maxconnections() - 10) < fd)
+       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) <= CurrentTime)
+               if((last_oper_notice + 20) <= rb_current_time())
                {
-                       sendto_realops_snomask(SNO_GENERAL, L_ALL,
+                       sendto_realops_flags(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);
-               rb_close(fd);
+                       
+               rb_write(F, "ERROR :All connections in use\r\n", 32);
+               rb_close(F);
                /* Re-register a new IO request for the next accept .. */
-               rb_setselect(listener->fd, FDLIST_SERVICE,
-                              COMM_SELECT_READ, accept_connection, listener, 0);
-               return;
+               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... */
-       aconf = find_dline((struct sockaddr *) &sai, sai.ss_family);
-       /* check it wasn't an exempt */
-       if (aconf != NULL && (aconf->status & CONF_EXEMPTDLINE) == 0)
+       if(aconf != NULL)
        {
                ServerStats->is_ref++;
-
+                       
                if(ConfigFileEntry.dline_with_reason)
                {
-                       if (rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (sizeof(buf)-1))
+                       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';
@@ -562,21 +529,31 @@ accept_connection(int pfd, void *data)
                        }
                }
                else
-                       rb_sprintf(buf, "ERROR :You have been D-lined.\r\n");
-        
-               write(fd, buf, strlen(buf));
-               rb_close(fd);
-
-               /* Re-register a new IO request for the next accept .. */
-               rb_setselect(listener->fd, FDLIST_SERVICE,
-                              COMM_SELECT_READ, accept_connection, listener, 0);
-               return;
+                       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++;
-       add_connection(listener, fd, (struct sockaddr *)&sai, aconf ? 1 : 0);
 
-       /* Re-register a new IO request for the next accept .. */
-       rb_setselect(listener->fd, FDLIST_SERVICE, COMM_SELECT_READ,
-                      accept_connection, listener, 0);
+       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);
 }