Clarify the "I'm a leaf, define HUB" message.
[ircu2.10.12-pk.git] / ircd / m_server.c
index d092fa4f2dc9e2889ca7dd40dc2e7fc9bb0341f9..aae44c5092c2a4f1e2274c70871d503c12a701d6 100644 (file)
@@ -49,7 +49,7 @@
 #include "send.h"
 #include "userload.h"
 
-#include <assert.h>
+/* #include <assert.h> -- Now using assert in ircd_log.h */
 #include <stdlib.h>
 #include <string.h>
 
@@ -96,6 +96,14 @@ parse_protocol(const char *proto)
   return prot;
 }
 
+/** Reason not to accept a server's new announcement. */
+enum lh_type {
+  ALLOWED, /**< The new server link is accepted. */
+  MAX_HOPS_EXCEEDED, /**< The path to the server is too long. */
+  NOT_ALLOWED_TO_HUB, /**< My peer is not allowed to hub for the server. */
+  I_AM_NOT_HUB /**< I have another active server link but not FEAT_HUB. */
+};
+
 /** Check whether the introduction of a new server would cause a loop
  * or be disallowed by leaf and hub configuration directives.
  * @param[in] cptr Neighbor who sent the message.
@@ -115,7 +123,8 @@ check_loop_and_lh(struct Client* cptr, struct Client *sptr, time_t *ghost, const
   struct Client* acptr;
   struct Client* LHcptr = NULL;
   struct ConfItem* lhconf;
-  int active_lh_line = 0, ii;
+  enum lh_type active_lh_line = ALLOWED;
+  int ii;
 
   if (ghost)
     *ghost = 0;
@@ -123,25 +132,29 @@ check_loop_and_lh(struct Client* cptr, struct Client *sptr, time_t *ghost, const
   /*
    * Calculate type of connect limit and applicable config item.
    */
-  lhconf = find_conf_byname(cli_confs(cptr), host, CONF_SERVER);
+  lhconf = find_conf_byname(cli_confs(cptr), cli_name(cptr), CONF_SERVER);
   assert(lhconf != NULL);
-  if (cptr == sptr)
+  if (ghost)
   {
     if (!feature_bool(FEAT_HUB))
       for (ii = 0; ii <= HighestFd; ii++)
         if (LocalClientArray[ii] && IsServer(LocalClientArray[ii])) {
-          active_lh_line = 3;
+          active_lh_line = I_AM_NOT_HUB;
           break;
         }
   }
   else if (hop > lhconf->maximum)
   {
-    active_lh_line = 1;
+    /* Because "maximum" should be 0 for non-hub links, check whether
+     * there is a hub mask -- if not, complain that the server isn't
+     * allowed to hub.
+     */
+    active_lh_line = lhconf->hub_limit ? MAX_HOPS_EXCEEDED : NOT_ALLOWED_TO_HUB;
   }
   else if (lhconf->hub_limit && match(lhconf->hub_limit, host))
   {
     struct Client *ac3ptr;
-    active_lh_line = 2;
+    active_lh_line = NOT_ALLOWED_TO_HUB;
     if (junction)
       for (ac3ptr = sptr; ac3ptr != &me; ac3ptr = cli_serv(ac3ptr)->up)
         if (IsJunction(ac3ptr)) {
@@ -192,7 +205,7 @@ check_loop_and_lh(struct Client* cptr, struct Client *sptr, time_t *ghost, const
      */
     if (IsConnecting(acptr))
     {
-      if (!active_lh_line && exit_client(cptr, acptr, &me,
+      if (active_lh_line == ALLOWED && exit_client(cptr, acptr, &me,
           "Just connected via another link") == CPTR_KILLED)
         return CPTR_KILLED;
       /*
@@ -204,7 +217,7 @@ check_loop_and_lh(struct Client* cptr, struct Client *sptr, time_t *ghost, const
     }
     /*
      * Avoid other nick collisions...
-     * This is a doubtfull test though, what else would it be
+     * This is a doubtful test though, what else would it be
      * when it has a server.name ?
      */
     else if (!IsServer(acptr) && !IsHandshake(acptr))
@@ -319,7 +332,7 @@ check_loop_and_lh(struct Client* cptr, struct Client *sptr, time_t *ghost, const
       else if (cli_from(c2ptr) == cptr || IsServer(sptr))
       {
         struct Client *killedptrfrom = cli_from(c2ptr);
-        if (active_lh_line)
+        if (active_lh_line != ALLOWED)
         {
           /*
            * If the L: or H: line also gets rid of this link,
@@ -332,7 +345,7 @@ check_loop_and_lh(struct Client* cptr, struct Client *sptr, time_t *ghost, const
            * line problem, we don't squit that.
            */
           if (cli_from(c2ptr) == cptr || (LHcptr && a_kills_b_too(c2ptr, LHcptr)))
-            active_lh_line = 0;
+            active_lh_line = ALLOWED;
           else
           {
             /*
@@ -369,12 +382,12 @@ check_loop_and_lh(struct Client* cptr, struct Client *sptr, time_t *ghost, const
       }
       else
       {
-        if (active_lh_line)
+        if (active_lh_line != ALLOWED)
         {
           if (LHcptr && a_kills_b_too(LHcptr, acptr))
             break;
           if (cli_from(acptr) == cptr || (LHcptr && a_kills_b_too(acptr, LHcptr)))
-            active_lh_line = 0;
+            active_lh_line = ALLOWED;
           else
           {
             LHcptr = 0;
@@ -397,38 +410,27 @@ check_loop_and_lh(struct Client* cptr, struct Client *sptr, time_t *ghost, const
     }
   }
 
-  if (active_lh_line)
+  if (active_lh_line != ALLOWED)
   {
-    int killed = 0;
-    if (LHcptr)
-      killed = a_kills_b_too(LHcptr, sptr);
-    else
+    if (!LHcptr)
       LHcptr = sptr;
-    if (active_lh_line == 1)
+    if (active_lh_line == MAX_HOPS_EXCEEDED)
     {
-      if (exit_client_msg(cptr, LHcptr, &me,
-                          "Leaf-only link %s <- %s, check L:",
-                          cli_name(cptr), host) == CPTR_KILLED)
-        return CPTR_KILLED;
+      return exit_client_msg(cptr, LHcptr, &me,
+                             "Maximum hops exceeded for %s at %s",
+                             cli_name(cptr), host);
     }
-    else if (active_lh_line == 2)
+    else if (active_lh_line == NOT_ALLOWED_TO_HUB)
     {
-      if (exit_client_msg(cptr, LHcptr, &me,
-                          "Non-Hub link %s <- %s, check H:",
-                          cli_name(cptr), host) == CPTR_KILLED)
-        return CPTR_KILLED;
+      return exit_client_msg(cptr, LHcptr, &me,
+                             "%s is not allowed to hub for %s",
+                             cli_name(cptr), host);
     }
-    else
+    else /* I_AM_NOT_HUB */
     {
       ServerStats->is_ref++;
-      if (exit_client(cptr, LHcptr, &me, "I'm a leaf, define HUB") == CPTR_KILLED)
-        return CPTR_KILLED;
+      return exit_client(cptr, LHcptr, &me, "I'm a leaf, define the HUB feature");
     }
-    /*
-     * Did we kill the incoming server off already ?
-     */
-    if (killed)
-      return 0;
   }
 
   return 1;
@@ -479,6 +481,20 @@ check_start_timestamp(struct Client *cptr, time_t timestamp, time_t start_timest
   }
 }
 
+/** Interpret a server's flags.
+ *
+ * @param[in] cptr New server structure.
+ * @param[in] flags String listing server's P10 flags.
+ */
+void set_server_flags(struct Client *cptr, const char *flags)
+{
+    while (*flags) switch (*flags++) {
+    case 'h': SetHub(cptr); break;
+    case 's': SetService(cptr); break;
+    case '6': SetIPv6(cptr); break;
+    }
+}
+
 /** Handle a SERVER message from an unregistered connection.
  *
  * \a parv has the following elements:
@@ -499,7 +515,6 @@ check_start_timestamp(struct Client *cptr, time_t timestamp, time_t start_timest
  */
 int mr_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
 {
-  char*            ch;
   char*            host;
   struct ConfItem* aconf;
   struct Jupe*     ajupe;
@@ -544,6 +559,9 @@ int mr_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
   /*
    * Detect protocol
    */
+  hop = atoi(parv[2]);
+  start_timestamp = atoi(parv[3]);
+  timestamp = atoi(parv[4]);
   prot = parse_protocol(parv[5]);
   if (!prot)
     return exit_client_msg(cptr, sptr, &me, "Bogus protocol (%s)", parv[5]);
@@ -551,9 +569,6 @@ int mr_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
     return exit_new_server(cptr, sptr, host, timestamp,
                            "Incompatible protocol: %s", parv[5]);
 
-  hop = atoi(parv[2]);
-  start_timestamp = atoi(parv[3]);
-  timestamp = atoi(parv[4]);
   Debug((DEBUG_INFO, "Got SERVER %s with timestamp [%s] age %Tu (%Tu)",
         host, parv[4], start_timestamp, cli_serv(&me)->timestamp));
 
@@ -577,7 +592,7 @@ int mr_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
     log_write(LS_NETWORK, L_NOTICE, LOG_NOSNOTICE, "Received unauthorized "
               "connection from %C [%s]", cptr,
               ircd_ntoa(&cli_ip(cptr)));
-    return exit_client(cptr, cptr, &me, "No C:line");
+    return exit_client(cptr, cptr, &me, "No Connect block");
   }
 
   host = cli_name(cptr);
@@ -610,21 +625,18 @@ int mr_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
   cli_serv(cptr)->timestamp = timestamp;
   cli_serv(cptr)->prot = prot;
   cli_serv(cptr)->ghost = ghost;
+  memset(cli_privs(cptr), 255, sizeof(struct Privs));
+  ClrPriv(cptr, PRIV_SET);
   SetServerYXX(cptr, cptr, parv[6]);
 
-  if (*parv[7] == '+') {
-    for (ch = parv[7] + 1; *ch; ch++)
-      switch (*ch) {
-      case 'h':
-        SetHub(cptr);
-       break;
-      case 's':
-       SetService(cptr);
-       break;
-      }
-  }
+  /* Attach any necessary UWorld config items. */
+  attach_confs_byhost(cptr, host, CONF_UWORLD);
 
-  check_start_timestamp(cptr, timestamp, start_timestamp, TStime());
+  if (*parv[7] == '+')
+    set_server_flags(cptr, parv[7] + 1);
+
+  recv_time = TStime();
+  check_start_timestamp(cptr, timestamp, start_timestamp, recv_time);
   ret = server_estab(cptr, aconf);
 
   if (feature_bool(FEAT_RELIABLE_CLOCK) &&
@@ -660,7 +672,6 @@ int mr_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
  */
 int ms_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
 {
-  char*            ch;
   int              i;
   char*            host;
   struct Client*   acptr;
@@ -687,6 +698,9 @@ int ms_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
   /*
    * Detect protocol
    */
+  hop = atoi(parv[2]);
+  start_timestamp = atoi(parv[3]);
+  timestamp = atoi(parv[4]);
   prot = parse_protocol(parv[5]);
   if (!prot)
     return exit_client_msg(cptr, sptr, &me, "Bogus protocol (%s)", parv[5]);
@@ -694,9 +708,6 @@ int ms_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
     return exit_new_server(cptr, sptr, host, timestamp,
                            "Incompatible protocol: %s", parv[5]);
 
-  hop = atoi(parv[2]);
-  start_timestamp = atoi(parv[3]);
-  timestamp = atoi(parv[4]);
   Debug((DEBUG_INFO, "Got SERVER %s with timestamp [%s] age %Tu (%Tu)",
         host, parv[4], start_timestamp, cli_serv(&me)->timestamp));
 
@@ -732,17 +743,11 @@ int ms_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
      for numeric nicks ! */
   SetServerYXX(cptr, acptr, parv[6]);
 
-  if (*parv[7] == '+') {
-    for (ch = parv[7] + 1; *ch; ch++)
-      switch (*ch) {
-      case 'h':
-        SetHub(acptr);
-       break;
-      case 's':
-       SetService(acptr);
-       break;
-      }
-  }
+  /* Attach any necessary UWorld config items. */
+  attach_confs_byhost(cptr, host, CONF_UWORLD);
+
+  if (*parv[7] == '+')
+    set_server_flags(acptr, parv[7] + 1);
 
   Count_newremoteserver(UserStats);
   if (Protocol(acptr) < 10)
@@ -752,9 +757,13 @@ int ms_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
   if (*parv[5] == 'J')
   {
     SetBurst(acptr);
-    sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s",
-                         cli_name(sptr), cli_name(acptr));
     SetJunction(acptr);
+    for (bcptr = cli_serv(acptr)->up; !IsMe(bcptr); bcptr = cli_serv(bcptr)->up)
+      if (IsBurstOrBurstAck(bcptr))
+          break;
+    if (IsMe(bcptr))
+      sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s",
+                           cli_name(sptr), cli_name(acptr));
   }
   /*
    * Old sendto_serv_but_one() call removed because we now need to send
@@ -767,10 +776,11 @@ int ms_server(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
       continue;
     if (0 == match(cli_name(&me), cli_name(acptr)))
       continue;
-    sendcmdto_one(sptr, CMD_SERVER, bcptr, "%s %d 0 %s %s %s%s +%s%s :%s",
+    sendcmdto_one(sptr, CMD_SERVER, bcptr, "%s %d 0 %s %s %s%s +%s%s%s :%s",
                   cli_name(acptr), hop + 1, parv[4], parv[5],
                   NumServCap(acptr), IsHub(acptr) ? "h" : "",
-                  IsService(acptr) ? "s" : "", cli_info(acptr));
+                  IsService(acptr) ? "s" : "", IsIPv6(acptr) ? "6" : "",
+                  cli_info(acptr));
   }
   return 0;
 }