IPv6 support (hopefully with fewer future transition pains)
[ircu2.10.12-pk.git] / ircd / listener.c
index f6ce0e3df52abef023ecd28c20da99e9be30f2ff..0556ce98eb82f0155d7f0589e6c93bd0038ec51e 100644 (file)
  *
  *  $Id$
  */
+#include "config.h"
+
 #include "listener.h"
 #include "client.h"
 #include "ircd.h"
 #include "ircd_alloc.h"
+#include "ircd_events.h"
+#include "ircd_features.h"
 #include "ircd_osdep.h"
 #include "ircd_reply.h"
+#include "ircd_snprintf.h"
 #include "ircd_string.h"
+#include "match.h"
 #include "numeric.h"
 #include "s_bsd.h"
 #include "s_conf.h"
 #include "s_misc.h"
+#include "s_stats.h"
 #include "send.h"
-#include "sprintf_irc.h"
 #include "sys.h"         /* MAXCLIENTS */
 
 #include <assert.h>
 
 struct Listener* ListenerPollList = 0;
 
-static struct Listener* make_listener(int port, struct in_addr addr)
+static void accept_connection(struct Event* ev);
+
+static struct Listener* make_listener(int port, const struct irc_in_addr *addr)
 {
-  struct Listener* listener = 
+  struct Listener* listener =
     (struct Listener*) MyMalloc(sizeof(struct Listener));
   assert(0 != listener);
 
   memset(listener, 0, sizeof(struct Listener));
 
   listener->fd          = -1;
-  listener->port        = port;
-  listener->addr.s_addr = addr.s_addr;
+  listener->addr.port   = port;
+  memcpy(&listener->addr.addr, addr, sizeof(listener->addr.addr));
 
 #ifdef NULL_POINTER_NOT_ZERO
   listener->next = NULL;
@@ -84,7 +92,7 @@ const char* get_listener_name(const struct Listener* listener)
 {
   static char buf[HOSTLEN + PORTNAMELEN + 4];
   assert(0 != listener);
-  sprintf_irc(buf, "%s:%u", me.name, listener->port);
+  ircd_snprintf(0, buf, sizeof(buf), "%s:%u", cli_name(&me), listener->addr.port);
   return buf;
 }
 
@@ -110,14 +118,22 @@ void count_listener_memory(int* count_out, size_t* size_out)
  * side effects - show ports
  * author       - Dianora
  */
-void show_ports(struct Client* sptr, int show_hidden, int port, int count)
+void show_ports(struct Client* sptr, struct StatDesc* sd, int stat,
+                char* param)
 {
-  struct Listener* listener = 0;
-  char             flags[8];
+  struct Listener *listener = 0;
+  char flags[8];
+  int show_hidden = IsOper(sptr);
+  int count = (IsOper(sptr) || MyUser(sptr)) ? 100 : 8;
+  int port = 0;
+
   assert(0 != sptr);
 
+  if (param)
+    port = atoi(param);
+
   for (listener = ListenerPollList; listener; listener = listener->next) {
-    if (port && port != listener->port)
+    if (port && port != listener->addr.port)
       continue;
     flags[0] = (listener->server) ? 'S' : 'C';
     if (listener->hidden) {
@@ -129,7 +145,7 @@ void show_ports(struct Client* sptr, int show_hidden, int port, int count)
     else
       flags[1] = '\0';
 
-    send_reply(sptr, RPL_STATSPLINE, listener->port, listener->ref_count,
+    send_reply(sptr, RPL_STATSPLINE, listener->addr.port, listener->ref_count,
               flags, (listener->active) ? "active" : "disabled");
     if (--count == 0)
       break;
@@ -153,41 +169,14 @@ void show_ports(struct Client* sptr, int show_hidden, int port, int count)
 
 static int inetport(struct Listener* listener)
 {
-  struct sockaddr_in sin;
   int                fd;
 
   /*
    * At first, open a new socket
    */
-  if (-1 == (fd = socket(AF_INET, SOCK_STREAM, 0))) {
-    report_error(SOCKET_ERROR_MSG, get_listener_name(listener), errno);
-    return 0;
-  }
-  else if (fd > MAXCLIENTS - 1) {
-    report_error(CONNLIMIT_ERROR_MSG, get_listener_name(listener), 0);
-    close(fd);
-    return 0;
-  }
-
-  if (!os_set_reuseaddr(fd)) {
-    report_error(REUSEADDR_ERROR_MSG, get_listener_name(listener), errno);
-    close(fd);
-    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.
-   */
-  memset(&sin, 0, sizeof(sin));
-  sin.sin_family = AF_INET;
-  sin.sin_addr   = listener->addr;
-  sin.sin_port   = htons(listener->port);
-
-  if (bind(fd, (struct sockaddr*) &sin, sizeof(sin))) {
-    report_error(BIND_ERROR_MSG, get_listener_name(listener), errno);
-    close(fd);
+  fd = os_socket(&listener->addr, SOCK_STREAM, get_listener_name(listener));
+  if (fd < 0)
     return 0;
-  }
   /*
    * Set the buffer sizes for the listener. Accepted connections
    * inherit the accepting sockets settings for SO_RCVBUF S_SNDBUF
@@ -195,7 +184,9 @@ static int inetport(struct Listener* listener)
    * else has no effect whatsoever on the connection.
    * NOTE: this must be set before listen is called
    */
-  if (!os_set_sockbufs(fd, (listener->server) ? SERVER_TCP_WINDOW : CLIENT_TCP_WINDOW)) {
+  if (!os_set_sockbufs(fd,
+                       (listener->server) ? feature_int(FEAT_SOCKSENDBUF) : CLIENT_TCP_WINDOW,
+                       (listener->server) ? feature_int(FEAT_SOCKRECVBUF) : CLIENT_TCP_WINDOW)) {
     report_error(SETBUFS_ERROR_MSG, get_listener_name(listener), errno);
     close(fd);
     return 0;
@@ -206,13 +197,19 @@ static int inetport(struct Listener* listener)
     return 0;
   }
   /*
-   * XXX - this should always work, performance will suck if it doesn't
+   * Set the TOS bits - this is nonfatal if it doesn't stick.
    */
-  if (!os_set_nonblocking(fd)) {
-    report_error(NONB_ERROR_MSG, get_listener_name(listener), errno);
+  if (!os_set_tos(fd,feature_int((listener->server)?FEAT_TOS_SERVER : FEAT_TOS_CLIENT))) {
+    report_error(TOS_ERROR_MSG, get_listener_name(listener), errno);
+  }
+
+  if (!socket_add(&listener->socket, accept_connection, (void*) listener,
+                 SS_LISTENING, 0, fd)) {
+    /* Error should already have been reported to the logs */
     close(fd);
     return 0;
   }
+
   listener->fd = fd;
 
   return 1;
@@ -224,69 +221,27 @@ static int inetport(struct Listener* listener)
  * XXX - this function does N comparisons so if the list is huge
  * we may want to do something else for this. (rehash and init use this)
  */
-static struct Listener* find_listener(int port, struct in_addr addr)
+static struct Listener* find_listener(int port, const struct irc_in_addr *addr)
 {
   struct Listener* listener;
   for (listener = ListenerPollList; listener; listener = listener->next) {
-    if (port == listener->port && addr.s_addr == listener->addr.s_addr)
+    if (port == listener->addr.port && !memcmp(addr, &listener->addr.addr, sizeof(*addr)))
       return listener;
   }
   return 0;
 }
 
 /*
- * set_listener_mask - set the connection mask for this listener
- */
-static void set_listener_mask(struct Listener* listener, const char* mask)
-{
-  int  ad[4];
-  char ipname[20];
-
-  assert(0 != listener);
-
-  if (EmptyString(mask) || 0 == strcmp(mask, "*")) {
-    listener->mask.s_addr = 0;
-    return;
-  }
-  ad[0] = ad[1] = ad[2] = ad[3] = 0;
-  /*
-   * do it this way because building ip# from separate values for each
-   * byte requires endian knowledge or some nasty messing. Also means
-   * easy conversion of "*" 0.0.0.0 or 134.* to 134.0.0.0 :-)
-   */
-  sscanf(mask, "%d.%d.%d.%d", &ad[0], &ad[1], &ad[2], &ad[3]);
-  sprintf_irc(ipname, "%d.%d.%d.%d", ad[0], ad[1], ad[2], ad[3]);
-  listener->mask.s_addr = inet_addr(ipname);
-}
-
-/*
- * connection_allowed - spin through mask and addr passed to see if connect 
- * allowed on a listener, uses mask generated by set_listener_mask
- */
-static int connection_allowed(const char* addr, const char* mask)
-{
-  int i = 4;
-  for ( ; i > 0; --i) {
-    if (*mask && *addr != *mask)
-      break;
-    ++addr;
-    ++mask;
-  }
-  return (0 == i);
-}
-
-
-/*
- * add_listener- create a new listener 
+ * add_listener- create a new listener
  * port - the port number to listen on
  * vhost_ip - if non-null must contain a valid IP address string in
  * the format "255.255.255.255"
  */
 void add_listener(int port, const char* vhost_ip, const char* mask,
-                  int is_server, int is_hidden) 
+                  int is_server, int is_hidden)
 {
   struct Listener* listener;
-  struct in_addr   vaddr;
+  struct irc_in_addr vaddr;
 
   /*
    * if no port in conf line, don't bother
@@ -294,35 +249,40 @@ void add_listener(int port, const char* vhost_ip, const char* mask,
   if (0 == port)
     return;
 
-  vaddr.s_addr = INADDR_ANY;
+  memset(&vaddr, 0, sizeof(vaddr));
 
-  if (!EmptyString(vhost_ip) && strcmp(vhost_ip,"*") != 0) {
-    vaddr.s_addr = inet_addr(vhost_ip);
-    if (INADDR_NONE == vaddr.s_addr)
+  if (!EmptyString(vhost_ip)
+      && strcmp(vhost_ip, "*")
+      && !ircd_aton(&vaddr, vhost_ip))
       return;
-  }
 
-  if ((listener = find_listener(port, vaddr))) {
+  if ((listener = find_listener(port, &vaddr))) {
     /*
-     * set active flag and change connect mask here, it's the only thing 
+     * set active flag and change connect mask here, it's the only thing
      * that can change on a rehash
      */
     listener->active = 1;
-    set_listener_mask(listener, mask);
+    if (mask)
+      ipmask_parse(mask, &listener->mask, &listener->mask_bits);
+    else
+      listener->mask_bits = 0;
     listener->hidden = is_hidden;
     listener->server = is_server;
     return;
   }
 
-  listener = make_listener(port, vaddr);
+  listener = make_listener(port, &vaddr);
 
   if (inetport(listener)) {
     listener->active = 1;
-    set_listener_mask(listener, mask);
+    if (mask)
+      ipmask_parse(mask, &listener->mask, &listener->mask_bits);
+    else
+      listener->mask_bits = 0;
     listener->hidden = is_hidden;
     listener->server = is_server;
     listener->next   = ListenerPollList;
-    ListenerPollList = listener; 
+    ListenerPollList = listener;
   }
   else
     free_listener(listener);
@@ -361,7 +321,7 @@ void close_listener(struct Listener* listener)
   }
   if (-1 < listener->fd)
     close(listener->fd);
-  free_listener(listener);
+  socket_del(&listener->socket);
 }
  
 /*
@@ -392,77 +352,97 @@ void release_listener(struct Listener* listener)
 /*
  * accept_connection - accept a connection on a listener
  */
-void accept_connection(struct Listener* listener)
+static void accept_connection(struct Event* ev)
 {
-  struct sockaddr_in addr = { 0 };
-  unsigned int       addrlen = sizeof(struct sockaddr_in);
-  int                fd;
+  struct Listener*    listener;
+  struct irc_sockaddr addr;
+  int                 fd;
 
-  assert(0 != listener);
+  assert(0 != ev_socket(ev));
+  assert(0 != s_data(ev_socket(ev)));
 
-  listener->last_accept = CurrentTime;
-  /*
-   * 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.
-   */
-  if (-1 == (fd = accept(listener->fd, (struct sockaddr*) &addr, &addrlen))) {
-    /* Lotsa admins seem to have problems with not giving enough file descriptors
-     * to their server so we'll add a generic warning mechanism here.  If it
-     * turns out too many messages are generated for meaningless reasons we
-     * can filter them back.
+  listener = (struct Listener*) s_data(ev_socket(ev));
+
+  if (ev_type(ev) == ET_DESTROY) /* being destroyed */
+    free_listener(listener);
+  else {
+    assert(ev_type(ev) == ET_ACCEPT || ev_type(ev) == ET_ERROR);
+
+    listener->last_accept = CurrentTime;
+    /*
+     * 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.
+     *
+     * This piece of code implements multi-accept, based
+     * on the idea that poll/select can only be efficient,
+     * if we succeed in handling all available events,
+     * i.e. accept all pending connections.
+     *
+     * http://www.hpl.hp.com/techreports/2000/HPL-2000-174.html
      */
-    sendto_op_mask(SNO_TCPCOMMON,"Unable to accept connection: %s", strerror(errno));
-    return;
-  }
-  /*
-   * check for connection limit
-   */
-  if (fd > MAXCLIENTS - 1) {
-    ++ServerStats->is_ref;
-    send(fd, "ERROR :All connections in use\r\n", 32, 0);
-    close(fd);
-    return;
-  }
-  /*
-   * check to see if listener is shutting down
-   */
-  if (!listener->active) {
-    ++ServerStats->is_ref;
-    send(fd, "ERROR :Use another port\r\n", 25, 0);
-    close(fd);
-    return;
-  }
-  /*
-   * check to see if connection is allowed for this address mask
-   */
-  if (!connection_allowed((const char*) &addr, (const char*) &listener->mask)) {
-    ++ServerStats->is_ref;
-    send(fd, "ERROR :Use another port\r\n", 25, 0);
-    close(fd);
-    return;
-  }
-#if 0
-  /*
-   * check conf for ip address access
-   */
-  if (!conf_connect_allowed(addr.sin_addr)) {
-    ++ServerStats->is_ref;
-    send(fd, "ERROR :Not authorized\r\n", 23, 0);
-    close(fd);
-    return;
-  }
+    while (1)
+    {
+      if ((fd = os_accept(listener->fd, &addr)) == -1)
+      {
+        if (errno == EAGAIN ||
+#ifdef EWOULDBLOCK
+            errno == EWOULDBLOCK)
 #endif
-  ++ServerStats->is_ac;
-  nextping = CurrentTime;
-
-  add_connection(listener, fd);
+          return;
+      /* Lotsa admins seem to have problems with not giving enough file
+       * descriptors to their server so we'll add a generic warning mechanism
+       * here.  If it turns out too many messages are generated for
+       * meaningless reasons we can filter them back.
+       */
+      sendto_opmask_butone(0, SNO_TCPCOMMON,
+                          "Unable to accept connection: %m");
+      return;
+      }
+      /*
+       * check for connection limit. If this fd exceeds the limit,
+       * all further accept()ed connections will also exceed it.
+       * Enable the server to clear out other connections before
+       * continuing to accept() new connections.
+       */
+      if (fd > MAXCLIENTS - 1)
+      {
+        ++ServerStats->is_ref;
+        send(fd, "ERROR :All connections in use\r\n", 32, 0);
+        close(fd);
+        return;
+      }
+      /*
+       * check to see if listener is shutting down. Continue
+       * to accept(), because it makes sense to clear our the
+       * socket's queue as fast as possible.
+       */
+      if (!listener->active)
+      {
+        ++ServerStats->is_ref;
+        send(fd, "ERROR :Use another port\r\n", 25, 0);
+        close(fd);
+        continue;
+      }
+      /*
+       * check to see if connection is allowed for this address mask
+       */
+      if (!ipmask_check(&addr.addr, &listener->mask, listener->mask_bits))
+      {
+        ++ServerStats->is_ref;
+        send(fd, "ERROR :Use another port\r\n", 25, 0);
+        close(fd);
+        continue;
+      }
+      ++ServerStats->is_ac;
+      /* nextping = CurrentTime; */
+      add_connection(listener, fd);
+    }
+  }
 }
-
-