Rewrite DNS lookup API to remove a memory leak and keep it from coming back.
[ircu2.10.12-pk.git] / ircd / s_bsd.c
index ead38dfb887866e08b27ab5f5e1391b224f8e530..27cdf30c8f6094cf0e4668fc4eb1ec8cdf93652e 100644 (file)
@@ -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."
@@ -172,14 +172,13 @@ void report_error(const char* text, const char* who, int err)
  * @param vptr The struct ConfItem representing the Connect block.
  * @param hp A pointer to the DNS lookup results (NULL on failure).
  */
-static void connect_dns_callback(void* vptr, struct DNSReply* hp)
+static void connect_dns_callback(void* vptr, const struct irc_in_addr *addr, const char *h_name)
 {
   struct ConfItem* aconf = (struct ConfItem*) vptr;
   assert(aconf);
   aconf->dns_pending = 0;
-  if (hp) {
-    memcpy(&aconf->address, &hp->addr, sizeof(aconf->address));
-    MyFree(hp);
+  if (addr) {
+    memcpy(&aconf->address, addr, sizeof(aconf->address));
     connect_server(aconf, 0);
   }
   else
@@ -193,10 +192,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);
 }
@@ -259,6 +260,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);
@@ -315,24 +322,9 @@ unsigned int deliver_it(struct Client *cptr, struct MsgQ *buf)
   return bytes_written;
 }
 
-/** Free the client's DNS reply, if any.
- * @param cptr Client to operate on.
- */
-void release_dns_reply(struct Client* cptr)
-{
-  assert(0 != cptr);
-  assert(MyConnect(cptr));
-
-  if (cli_dns_reply(cptr)) {
-    MyFree(cli_dns_reply(cptr)->h_name);
-    MyFree(cli_dns_reply(cptr));
-    cli_dns_reply(cptr) = 0;
-  }
-}
-
 /** 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)
@@ -414,7 +406,7 @@ void close_connection(struct Client *cptr)
      */
     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.
@@ -621,7 +613,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;
     }
   }
@@ -745,20 +737,15 @@ 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)
       && !ircd_aton(&aconf->address.addr, aconf->host)) {
     char buf[HOSTLEN + 1];
-    struct DNSQuery  query;
 
-    query.vptr     = aconf;
-    query.callback = connect_dns_callback;
     host_from_uh(buf, aconf->host, HOSTLEN);
-    buf[HOSTLEN] = '\0';
-
-    gethost_byname(buf, &query);
+    gethost_byname(buf, connect_dns_callback, aconf);
     aconf->dns_pending = 1;
     return 0;
   }
@@ -778,10 +765,10 @@ int connect_server(struct ConfItem* aconf, struct Client* by)
 
   if (!find_conf_byhost(cli_confs(cptr), aconf->host, CONF_SERVER)) {
     sendto_opmask_butone(0, SNO_OLDSNO, "Host %s is not enabled for "
-                         "connecting: no C-line", aconf->name);
+                         "connecting: no Connect block", aconf->name);
     if (by && IsUser(by) && !MyUser(by)) {
       sendcmdto_one(&me, CMD_NOTICE, by, "%C :Connect to host %s failed: no "
-                    "C-line", by, aconf->name);
+                    "Connect block", by, aconf->name);
     }
     det_confs_butmask(cptr, 0);
     free_client(cptr);
@@ -892,6 +879,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*/