Make specifying nameservers by name work for DBus API.
authorSimon Kelley <simon@thekelleys.org.uk>
Mon, 7 Nov 2022 23:00:34 +0000 (23:00 +0000)
committerSimon Kelley <simon@thekelleys.org.uk>
Mon, 7 Nov 2022 23:00:34 +0000 (23:00 +0000)
src/dbus.c
src/dnsmasq.h
src/option.c

index 870912a..ef80711 100644 (file)
@@ -379,21 +379,6 @@ static DBusMessage* dbus_read_servers_ex(DBusMessage *message, int strings)
          strcpy(str_addr, str);
        }
 
-      /* parse the IP address */
-      if ((addr_err = parse_server(str_addr, &sdetails)) ||
-          (addr_err = parse_server_addr(&sdetails)))
-       {
-          error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS,
-                                                "Invalid IP address '%s': %s",
-                                                str, addr_err);
-          break;
-        }
-      
-      /* 0.0.0.0 for server address == NULL, for Dbus */
-      if (addr.in.sin_family == AF_INET &&
-          addr.in.sin_addr.s_addr == 0)
-        flags |= SERV_LITERAL_ADDRESS;
-      
       if (strings)
        {
          char *p;
@@ -407,7 +392,31 @@ static DBusMessage* dbus_read_servers_ex(DBusMessage *message, int strings)
            else 
              p = NULL;
            
-           add_update_server(flags | SERV_FROM_DBUS, &addr, &source_addr, interface, str_domain, NULL);
+            if (strings && strlen(str_addr) == 0)
+              add_update_server(SERV_LITERAL_ADDRESS | SERV_FROM_DBUS, &addr, &source_addr, interface, str_domain, NULL);
+            else
+              {
+                if ((addr_err = parse_server(str_addr, &sdetails)))
+                  {
+                    error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS,
+                                                          "Invalid IP address '%s': %s",
+                                                          str, addr_err);
+                    break;
+                  }
+                
+                while (parse_server_next(&sdetails))
+                  {
+                    if ((addr_err = parse_server_addr(&sdetails)))
+                      {
+                        error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS,
+                                                              "Invalid IP address '%s': %s",
+                                                              str, addr_err);
+                        break;
+                      }
+                    
+                    add_update_server(flags | SERV_FROM_DBUS, &addr, &source_addr, interface, str_domain, NULL);
+                  }
+              }
          } while ((str_domain = p));
        }
       else
@@ -421,11 +430,40 @@ static DBusMessage* dbus_read_servers_ex(DBusMessage *message, int strings)
            if (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING)
              dbus_message_iter_get_basic(&string_iter, &str);
            dbus_message_iter_next (&string_iter);
+
+           if ((addr_err = parse_server(str_addr, &sdetails)))
+             {
+               error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS,
+                                                     "Invalid IP address '%s': %s",
+                                                     str, addr_err);
+               break;
+             }
            
-           add_update_server(flags | SERV_FROM_DBUS, &addr, &source_addr, interface, str, NULL);
+           while (parse_server_next(&sdetails))
+             {
+               if ((addr_err = parse_server_addr(&sdetails)))
+                 {
+                   error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS,
+                                                         "Invalid IP address '%s': %s",
+                                                         str, addr_err);
+                   break;
+                 }
+               
+               /* 0.0.0.0 for server address == NULL, for Dbus */
+               if (addr.in.sin_family == AF_INET &&
+                   addr.in.sin_addr.s_addr == 0)
+                 flags |= SERV_LITERAL_ADDRESS;
+               else
+                 flags &= ~SERV_LITERAL_ADDRESS;
+               
+               add_update_server(flags | SERV_FROM_DBUS, &addr, &source_addr, interface, str, NULL);
+             }
          } while (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING);
        }
-        
+      
+      if (sdetails.resolved)
+       freeaddrinfo(sdetails.hostinfo);
+      
       /* jump to next element in outer array */
       dbus_message_iter_next(&array_iter);
     }
index 9f06a7f..7b59823 100644 (file)
@@ -1478,6 +1478,7 @@ void reset_option_bool(unsigned int opt);
 struct hostsfile *expand_filelist(struct hostsfile *list);
 char *parse_server(char *arg, struct server_details *sdetails);
 char *parse_server_addr(struct server_details *sdetails);
+int parse_server_next(struct server_details *sdetails);
 int option_read_dynfile(char *file, int flags);
 
 /* forward.c */
index ab36050..d067a16 100644 (file)
@@ -1049,10 +1049,11 @@ char *parse_server_addr(struct server_details *sdetails)
     }
   else
     return _("bad address");
+  
   return NULL;
 }
 
-static int parse_server_next(struct server_details *sdetails)
+int parse_server_next(struct server_details *sdetails)
 {
   /* Looping over resolved addresses? */
   if (sdetails->hostinfo)