[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#362029: Patch to attach



Matthew Palmer <mpalmer@debian.org> writes:

> Index: netcfg/dhcp.c
> ===================================================================
> --- netcfg/dhcp.c	(revision 66154)
> +++ netcfg/dhcp.c	(working copy)
> @@ -521,6 +521,7 @@ (after udhcpc exited with a lease)
>                      }
>  
>                      netcfg_nameservers_to_array (nameservers, nameserver_array);
> +                    netcfg_write_resolv (domain, nameserver_array);
>                  }
>  
>                  state = HOSTNAME;
> @@ -602,6 +603,16 @@ (in case DOMAIN:)
>              else {
>                  netcfg_write_common(ipaddress, hostname, domain);
>                  netcfg_write_dhcp(interface, dhostname);
> +                /* If the resolv.conf was written by udhcpc, then nameserver_array
> +                 * will be empty and we'll need to populate it.  If we asked for
> +                 * the nameservers, then it'll be full, but nobody will care if we
> +                 * refill it.
> +                 */
> +                if (read_resolv_conf_nameservers(nameserver_array))
> +                    netcfg_write_resolv(domain, nameserver_array);
> +                else
> +                    printf("Error reading resolv.conf for nameservers\n");
> +
>                  return 0;
>              }
>              break;

Hi,

This seems to do the job, but the control flow in netcfg is already
convoluted enough, isn't there a better alternative?  I mean, for a
static configuration, netcfg_write_resolv is called from
netcfg_write_static, can't we follow suit in netcfg_write_dhcp?  If
netcfg always removed resolv.conf before calling the DHCP client, then
we could modify resolv_conf_entries to also populate nameserver_array, 
and then unconditionally write out resolv.conf in netcfg_write_dhcp,
couldn't we?  Just thinking out loud....

> @@ -646,3 +657,41 @@
>  
>      return count;
>  }
> +
> +/* Read the nameserver entries out of resolv.conf and stick them into
> + * nameservers_array, so we can write out a newer, shinier resolv.conf
> + */
> +int read_resolv_conf_nameservers(struct in_addr array[])
> +{
> +    FILE *f;
> +    int i = 0;
> +    
> +    if ((f = fopen(RESOLV_FILE, "r")) != NULL) {
> +        char buf[256];
> +
> +        while (fgets(buf, 256, f) != NULL) {
> +            char *ptr;
> +
> +            if (strncmp(buf, "nameserver ", strlen("nameserver ")) == 0) {
> +                /* Chop off trailing \n */
> +                while (buf[strlen(buf)-1] == '\n')
> +                    buf[strlen(buf)-1] = '\0';

Why is this "while" instead of "if"?

> +                ptr = buf + strlen("nameserver ");
> +                inet_pton(AF_INET, ptr, &array[i++]);
> +                if (i == 3) {

We really should use a global constant here (and in netcfg.h and in
netcfg_nameservers_to_array at least) instead of the magic number 3...

> +                    /* We can only hold so many nameservers, and we've reached
> +                     * our limit.  Sorry.
> +                     */
-- 
Regards,
Feri.



Reply to: