Ejemplo n.º 1
0
void static_hosts_load(int in_chroot) {
    FILE *f;
    unsigned int line = 0;
    const char *filename = (in_chroot ? "/hosts" : AVAHI_CONFIG_DIR "/hosts");

    if (!(f = fopen(filename, "r")))
    {
        if (errno != ENOENT)
            avahi_log_error ("Failed to open static hosts file: %s", strerror (errno));
        return;
    }

    while (!feof(f)) {
        unsigned int len;
        char ln[256], *s;
        char *host, *ip;
        StaticHost *h;

        if (!fgets(ln, sizeof (ln), f))
            break;

        line++;

        /* Find the start of the line, ignore whitespace */
        s = ln + strspn(ln, " \t");
        /* Set the end of the string to NULL */
        s[strcspn(s, "#\r\n")] = 0;

        /* Ignore blank lines */
        if (*s == 0)
            continue;

        /* Read the first string (ip) up to the next whitespace */
        len = strcspn(s, " \t");
        ip = avahi_strndup(s, len);

        /* Skip past it */
        s += len;

        /* Find the next token */
        s += strspn(s, " \t");
        len = strcspn(s, " \t");
        host = avahi_strndup(s, len);

        if (*host == 0)
        {
            avahi_log_error("%s:%d: Error, unexpected end of line!", filename, line);
            avahi_free(host);
            avahi_free(ip);
	    break;
        }

        /* Skip over the host */
        s += len;

        /* Skip past any more spaces */
        s += strspn(s, " \t");
        
        /* Anything left? */
        if (*s != 0) {
            avahi_log_error ("%s:%d: Junk on the end of the line!", filename, line);
            avahi_free(host);
            avahi_free(ip);
            break;
        }

        h = static_host_new();
        h->host = host;
        h->ip = ip;
    }

    fclose(f);
}
Ejemplo n.º 2
0
void static_hosts_load(int in_chroot) {
    FILE *f;
    unsigned int line = 0;
    StaticHost *h, *next;
    const char *filename = in_chroot ? "/hosts" : AVAHI_CONFIG_DIR "/hosts";

    if (!(f = fopen(filename, "r"))) {
        if (errno != ENOENT)
            avahi_log_error ("Failed to open static hosts file: %s", strerror (errno));
        return;
    }

    current_iteration++;
    
    while (!feof(f)) {
        unsigned int len;
        char ln[256], *s;
        char *host, *ip;
        AvahiAddress a;

        if (!fgets(ln, sizeof (ln), f))
            break;

        line++;

        /* Find the start of the line, ignore whitespace */
        s = ln + strspn(ln, " \t");
        /* Set the end of the string to NULL */
        s[strcspn(s, "#\r\n")] = 0;

        /* Ignore blank lines */
        if (*s == 0)
            continue;

        /* Read the first string (ip) up to the next whitespace */
        len = strcspn(s, " \t");
        ip = avahi_strndup(s, len);

        /* Skip past it */
        s += len;

        /* Find the next token */
        s += strspn(s, " \t");
        len = strcspn(s, " \t");
        host = avahi_strndup(s, len);

        if (*host == 0)
        {
            avahi_log_error("%s:%d: Error, unexpected end of line!", filename, line);
            avahi_free(host);
            avahi_free(ip);
            goto fail;
        }

        /* Skip over the host */
        s += len;

        /* Skip past any more spaces */
        s += strspn(s, " \t");
        
        /* Anything left? */
        if (*s != 0) {
            avahi_log_error ("%s:%d: Junk on the end of the line!", filename, line);
            avahi_free(host);
            avahi_free(ip);
            goto fail;
        }

        if (!avahi_address_parse(ip, AVAHI_PROTO_UNSPEC, &a)) {
            avahi_log_error("Static host name %s: failed to parse address %s", host, ip);
            avahi_free(host);
            avahi_free(ip);
            goto fail;
        }

        avahi_free(ip);

        if ((h = static_host_find(host, &a)))
            avahi_free(host);
        else {
            h = static_host_new();
            h->host = host;
            h->address = a;

            avahi_log_info("Loading new static hostname %s.", h->host);
        }

        h->iteration = current_iteration;
    }

    for (h = hosts; h; h = next) {
        next = h->hosts_next;
        
        if (h->iteration != current_iteration) {
            avahi_log_info("Static hostname %s vanished, removing.", h->host);
            static_host_free(h);
        }
    }

fail:
    
    fclose(f);
}