Browse Source

dns_find_ip: Return an index.

This will allow for retrying more IPs.

Issue #115
Bryan Drewery 5 months ago
parent
commit
65b057f16e
4 changed files with 32 additions and 21 deletions
  1. 12 4
      src/adns.cc
  2. 1 1
      src/adns.h
  3. 13 10
      src/botnet.cc
  4. 6 6
      src/mod/server.mod/servmsg.cc

+ 12 - 4
src/adns.cc

@@ -1399,12 +1399,20 @@ bool valid_dns_id(int idx, int id)
 	return 0;
 }
 
-bd::String dns_find_ip(const bd::Array<bd::String>& ips, int af_type) {
-	for (const auto& ip : ips) {
+ssize_t
+dns_find_ip(const bd::Array<bd::String>& ips,
+    int af_type, ssize_t start_idx)
+{
+	ssize_t i = start_idx + 1;
+
+	assert(i >= 0);
+	while (size_t(i) < ips.length()) {
+		const auto& ip = ips[i];
 		if (is_dotted_ip(ip.c_str()) == af_type) {
-			return ip;
+			return i;
 		}
+		++i;
 	}
-	return bd::String();
+	return -1;
 }
 /* vim: set sts=0 sw=8 ts=8 noet: */

+ 1 - 1
src/adns.h

@@ -58,7 +58,7 @@ void tell_dnsdebug(int);
 void dns_cache_flush();
 bool valid_dns_id(int, int);
 int reverse_ip(const char *host, char *reverse);
-bd::String dns_find_ip(const bd::Array<bd::String>& ips, int af_type);
+ssize_t dns_find_ip(const bd::Array<bd::String>& ips, int af_type, ssize_t = -1);
 
 extern int		dns_sock, dns_idx;
 extern const char	*dns_ip;

+ 13 - 10
src/botnet.cc

@@ -1125,27 +1125,28 @@ static void botlink_dns_callback(int id, void *client_data, const char *host,
 //    idx = dcc[i].u.dns->caller_idx;
 //  }
 
-  bd::String ip_from_dns;
+  ssize_t ip_from_dns_idx = -1;
 
   if (ips.size()) {
 #ifdef USE_IPV6
     /* If IPv6 is available use it, otherwise fall back on IPv4 */
     if (conf.bot->net.v6)
-      ip_from_dns = dns_find_ip(ips, AF_INET6);
-    if (!ip_from_dns)
+      ip_from_dns_idx = dns_find_ip(ips, AF_INET6);
+    if (ip_from_dns_idx == -1)
 #endif /* USE_IPV6 */
     {
       /* Use IPv4 if no ipv6 is available */
-      ip_from_dns = dns_find_ip(ips, AF_INET);
+      ip_from_dns_idx = dns_find_ip(ips, AF_INET);
     }
   }
 
-  if (!ips.size() || !ip_from_dns) {
+  if (!ips.size() || ip_from_dns_idx == -1) {
     putlog(LOG_BOTS, "*", "Could not link to %s (DNS error).\n", dcc[i].nick);
     lostdcc(i);
     return;
   }
 
+  const bd::String ip_from_dns(ips[ip_from_dns_idx]);
   dcc[i].addr = inet_addr(ip_from_dns.c_str());
   strlcpy(dcc[i].host, ip_from_dns.c_str(), sizeof(dcc[i].host));
 
@@ -1310,22 +1311,22 @@ static void tandem_relay_dns_callback(int id, void *client_data,
     return;
   }
 
-  bd::String ip_from_dns;
+  ssize_t ip_from_dns_idx = -1;
 
   if (ips.size()) {
 #ifdef USE_IPV6
     /* If IPv6 is available use it, otherwise fall back on IPv4 */
     if (conf.bot->net.v6)
-      ip_from_dns = dns_find_ip(ips, AF_INET6);
-    if (!ip_from_dns)
+      ip_from_dns_idx = dns_find_ip(ips, AF_INET6);
+    if (ip_from_dns_idx == -1)
 #endif /* USE_IPV6 */
     {
       /* Use IPv4 if no ipv6 is available */
-      ip_from_dns = dns_find_ip(ips, AF_INET);
+      ip_from_dns_idx = dns_find_ip(ips, AF_INET);
     }
   }
 
-  if (!ips.size() || !ip_from_dns) {
+  if (!ips.size() || ip_from_dns_idx == -1) {
     struct chat_info *ci = dcc[idx].u.relay->chat;
 
     dprintf(idx, "Could not relay to %s (DNS error).\n", dcc[i].nick);
@@ -1337,6 +1338,8 @@ static void tandem_relay_dns_callback(int id, void *client_data,
     return;
   }
 
+  const bd::String ip_from_dns(ips[ip_from_dns_idx]);
+
 #ifdef USE_IPV6
   int af = is_dotted_ip(ip_from_dns.c_str());
 

+ 6 - 6
src/mod/server.mod/servmsg.cc

@@ -2159,13 +2159,13 @@ static void server_dns_callback(int id, void *client_data, const char *host,
   my_addr_t addr;
   char *ip = NULL;
   const char* dns_type = NULL;
-  bd::String ip_from_dns;
+  ssize_t ip_from_dns_idx;
 
 #ifdef USE_IPV6
   /* If IPv6 is wanted, ensure we are connecting to an IPv6 server, otherwise skip it */
   if (conf.bot->net.v6) {
-    ip_from_dns = dns_find_ip(ips, AF_INET6);
-    if (!ip_from_dns.length()) {
+    ip_from_dns_idx = dns_find_ip(ips, AF_INET6);
+    if (ip_from_dns_idx == -1) {
       dns_type = "IPv6";
       goto fatal_dns;
     }
@@ -2173,13 +2173,13 @@ static void server_dns_callback(int id, void *client_data, const char *host,
 #endif /* USE_IPV6 */
   {
     /* If IPv4 is wanted, don't connect to IPv6! */
-    ip_from_dns = dns_find_ip(ips, AF_INET);
-    if (!ip_from_dns.length()) {
+    ip_from_dns_idx = dns_find_ip(ips, AF_INET);
+    if (ip_from_dns_idx == -1) {
       dns_type = "IPv4";
       goto fatal_dns;
     }
   }
-  ip = ip_from_dns.dup();
+  ip = ips[ip_from_dns_idx].dup();
 
   get_addr(ip, &addr);