Propagate +6 flag across network. Show correct error when outbound connections fail.
[ircu2.10.12-pk.git] / ircd / s_bsd.c
index 0ed3425b2667abd2f3389ccbf70ecbc8caf5bc57..63a0228517e68d5f3c18a87fe4f2c9062b8d9312 100644 (file)
 #include "s_user.h"
 #include "send.h"
 #include "struct.h"
-#include "support.h"
 #include "sys.h"
 #include "uping.h"
 #include "version.h"
 
-#include <arpa/inet.h>
-#include <assert.h>
+/* #include <assert.h> -- Now using assert in ircd_log.h */
 #include <errno.h>
 #include <fcntl.h>
 #include <netdb.h>
 struct Client*            LocalClientArray[MAXCONNECTIONS];
 /** Maximum file descriptor in current use. */
 int                       HighestFd = -1;
-/** Default local address for outbound connections. */
-struct irc_sockaddr       VirtualHost;
+/** Default local address for outbound IPv4 connections. */
+struct irc_sockaddr       VirtualHost_v4;
+/** Default local address for outbound IPv6 connections. */
+struct irc_sockaddr       VirtualHost_v6;
 /** Temporary buffer for reading data from a peer. */
 static char               readbuf[SERVER_TCP_WINDOW];
 
@@ -119,7 +119,7 @@ static void client_timer_callback(struct Event* ev);
  *   defined FD_SETSIZE to MAXCONNECTIONS+4 before including the system's headers 
  *   but sys/types.h might have abruptly redefined it so the check is still 
  *   done), you might already need to recompile your kernel.
- * For larger FD_SETSIZE your milage may vary (kernel patches may be needed).
+ * For larger FD_SETSIZE your mileage may vary (kernel patches may be needed).
  * The check is _NOT_ done if we will not use FD_SETS at all (USE_POLL)
  */
 #error "FD_SETSIZE is too small or MAXCONNECTIONS too large."
@@ -193,10 +193,12 @@ static void connect_dns_callback(void* vptr, struct DNSReply* hp)
 void close_connections(int close_stderr)
 {
   int i;
-  close(0);
-  close(1);
   if (close_stderr)
+  {
+    close(0);
+    close(1);
     close(2);
+  }
   for (i = 3; i < MAXCONNECTIONS; ++i)
     close(i);
 }
@@ -236,8 +238,10 @@ static int connect_inet(struct ConfItem* aconf, struct Client* cptr)
    */
   if (irc_in_addr_valid(&aconf->origin.addr))
     local = &aconf->origin;
+  else if (irc_in_addr_is_ipv4(&aconf->address.addr))
+    local = &VirtualHost_v4;
   else
-    local = &VirtualHost;
+    local = &VirtualHost_v6;
   cli_fd(cptr) = os_socket(local, SOCK_STREAM, cli_name(cptr));
   if (cli_fd(cptr) < 0)
     return 0;
@@ -246,7 +250,6 @@ static int connect_inet(struct ConfItem* aconf, struct Client* cptr)
    * save connection info in client
    */
   memcpy(&cli_ip(cptr), &aconf->address.addr, sizeof(cli_ip(cptr)));
-  cli_port(cptr) = aconf->address.port;
   ircd_ntoa_r(cli_sock_ip(cptr), &cli_ip(cptr));
   /*
    * we want a big buffer for server connections
@@ -258,6 +261,12 @@ static int connect_inet(struct ConfItem* aconf, struct Client* cptr)
     cli_fd(cptr) = -1;
     return 0;
   }
+  /*
+   * Set the TOS bits - this is nonfatal if it doesn't stick.
+   */
+  if (!os_set_tos(cli_fd(cptr), FEAT_TOS_SERVER)) {
+    report_error(TOS_ERROR_MSG, cli_name(cptr), errno);
+  }
   if ((result = os_connect_nonb(cli_fd(cptr), &aconf->address)) == IO_FAILURE) {
     cli_error(cptr) = errno;
     report_error(CONNECT_ERROR_MSG, cli_name(cptr), errno);
@@ -299,18 +308,7 @@ unsigned int deliver_it(struct Client *cptr, struct MsgQ *buf)
 
     cli_sendB(cptr) += bytes_written;
     cli_sendB(&me)  += bytes_written;
-    if (cli_sendB(cptr) > 1023) {
-      cli_sendK(cptr) += (cli_sendB(cptr) >> 10);
-      cli_sendB(cptr) &= 0x03ff;    /* 2^10 = 1024, 3ff = 1023 */
-    }
-    if (cli_sendB(&me) > 1023) {
-      cli_sendK(&me) += (cli_sendB(&me) >> 10);
-      cli_sendB(&me) &= 0x03ff;
-    }
-    /*
-     * XXX - hrmm.. set blocked here? the socket didn't
-     * say it was blocked
-     */
+    /* A partial write implies that future writes will block. */
     if (bytes_written < bytes_count)
       SetFlag(cptr, FLAG_BLOCKED);
     break;
@@ -342,7 +340,7 @@ void release_dns_reply(struct Client* cptr)
 
 /** Complete non-blocking connect()-sequence. Check access and
  * terminate connection, if trouble detected.
- * @param cptr Client to which we have connected, with all Confitem structs attached.
+ * @param cptr Client to which we have connected, with all ConfItem structs attached.
  * @return Zero on failure (caller should exit_client()), non-zero on success.
  */
 static int completed_connection(struct Client* cptr)
@@ -397,7 +395,7 @@ static int completed_connection(struct Client* cptr)
   cli_lasttime(cptr) = CurrentTime;
   SetFlag(cptr, FLAG_PINGSENT);
 
-  sendrawto_one(cptr, MSG_SERVER " %s 1 %Tu %Tu J%s %s%s +%s :%s",
+  sendrawto_one(cptr, MSG_SERVER " %s 1 %Tu %Tu J%s %s%s +%s6 :%s",
                 cli_name(&me), cli_serv(&me)->timestamp, newts,
                MAJOR_PROTOCOL, NumServCap(&me),
                feature_bool(FEAT_HUB) ? "h" : "", cli_info(&me));
@@ -417,24 +415,14 @@ void close_connection(struct Client *cptr)
     ServerStats->is_sv++;
     ServerStats->is_sbs += cli_sendB(cptr);
     ServerStats->is_sbr += cli_receiveB(cptr);
-    ServerStats->is_sks += cli_sendK(cptr);
-    ServerStats->is_skr += cli_receiveK(cptr);
     ServerStats->is_sti += CurrentTime - cli_firsttime(cptr);
-    if (ServerStats->is_sbs > 1023) {
-      ServerStats->is_sks += (ServerStats->is_sbs >> 10);
-      ServerStats->is_sbs &= 0x3ff;
-    }
-    if (ServerStats->is_sbr > 1023) {
-      ServerStats->is_skr += (ServerStats->is_sbr >> 10);
-      ServerStats->is_sbr &= 0x3ff;
-    }
     /*
      * If the connection has been up for a long amount of time, schedule
      * a 'quick' reconnect, else reset the next-connect cycle.
      */
-    if ((aconf = find_conf_exact(cli_name(cptr), 0, cli_sockhost(cptr), CONF_SERVER))) {
+    if ((aconf = find_conf_exact(cli_name(cptr), cptr, CONF_SERVER))) {
       /*
-       * Reschedule a faster reconnect, if this was a automaticly
+       * Reschedule a faster reconnect, if this was a automatically
        * connected configuration entry. (Note that if we have had
        * a rehash in between, the status has been changed to
        * CONF_ILLEGAL). But only do this if it was a "good" link.
@@ -451,17 +439,7 @@ void close_connection(struct Client *cptr)
     ServerStats->is_cl++;
     ServerStats->is_cbs += cli_sendB(cptr);
     ServerStats->is_cbr += cli_receiveB(cptr);
-    ServerStats->is_cks += cli_sendK(cptr);
-    ServerStats->is_ckr += cli_receiveK(cptr);
     ServerStats->is_cti += CurrentTime - cli_firsttime(cptr);
-    if (ServerStats->is_cbs > 1023) {
-      ServerStats->is_cks += (ServerStats->is_cbs >> 10);
-      ServerStats->is_cbs &= 0x3ff;
-    }
-    if (ServerStats->is_cbr > 1023) {
-      ServerStats->is_ckr += (ServerStats->is_cbr >> 10);
-      ServerStats->is_cbr &= 0x3ff;
-    }
   }
   else
     ServerStats->is_ni++;
@@ -580,7 +558,6 @@ void add_connection(struct Listener* listener, int fd) {
   ircd_ntoa_r(cli_sock_ip(new_client), &addr.addr);
   strcpy(cli_sockhost(new_client), cli_sock_ip(new_client));
   memcpy(&cli_ip(new_client), &addr.addr, sizeof(cli_ip(new_client)));
-  cli_port(new_client) = addr.port;
 
   if (next_target)
     cli_nexttarget(new_client) = next_target;
@@ -652,7 +629,7 @@ static int read_packet(struct Client *cptr, int socket_ready)
       break;
     case IO_FAILURE:
       cli_error(cptr) = errno;
-      /* SetFlag(cpt, FLAG_DEADSOCKET); */
+      /* SetFlag(cptr, FLAG_DEADSOCKET); */
       return 0;
     }
   }
@@ -776,7 +753,7 @@ int connect_server(struct ConfItem* aconf, struct Client* by)
     }
   }
   /*
-   * If we dont know the IP# for this host and it is a hostname and
+   * If we don't know the IP# for this host and it is a hostname and
    * not a ip# string, then try and find the appropriate host record.
    */
   if (!irc_in_addr_valid(&aconf->address.addr)
@@ -923,6 +900,11 @@ static void client_sock_callback(struct Event* ev)
     cli_error(cptr) = ev_data(ev);
     if (s_state(&(con_socket(con))) == SS_CONNECTING) {
       completed_connection(cptr);
+      /* for some reason, the os_get_sockerr() in completed_connect()
+       * can return 0 even when ev_data(ev) indicates a real error, so
+       * re-assign the client error here.
+       */
+      cli_error(cptr) = ev_data(ev);
       break;
     }
     /*FALLTHROUGH*/
@@ -943,7 +925,7 @@ static void client_sock_callback(struct Event* ev)
   case ET_WRITE: /* socket is writable */
     ClrFlag(cptr, FLAG_BLOCKED);
     if (cli_listing(cptr) && MsgQLength(&(cli_sendQ(cptr))) < 2048)
-      list_next_channels(cptr, 64);
+      list_next_channels(cptr);
     Debug((DEBUG_SEND, "Sending queued data to %C", cptr));
     send_queued(cptr);
     break;