Esempio n. 1
1
/*
 * Insert an additional listener in to the sorted list of listeners
 */
void
add_listener(struct Listener_head *listeners, struct Listener *listener) {
    assert(listeners != NULL);
    assert(listener != NULL);
    assert(listener->address != NULL);
    listener_ref_get(listener);

    if (SLIST_FIRST(listeners) == NULL ||
            address_compare(listener->address, SLIST_FIRST(listeners)->address) < 0) {
        SLIST_INSERT_HEAD(listeners, listener, entries);
        return;
    }

    struct Listener *iter;
    SLIST_FOREACH(iter, listeners, entries) {
        if (SLIST_NEXT(iter, entries) == NULL ||
                address_compare(listener->address, SLIST_NEXT(iter, entries)->address) < 0) {
            SLIST_INSERT_AFTER(iter, listener, entries);
            return;
        }
    }
}
Esempio n. 2
0
void
listeners_reload(struct Listener_head *existing_listeners,
        struct Listener_head *new_listeners,
        const struct Table_head *tables, struct ev_loop *loop) {
    struct Listener *iter_existing = SLIST_FIRST(existing_listeners);
    struct Listener *iter_new = SLIST_FIRST(new_listeners);

    while (iter_existing != NULL || iter_new != NULL) {
        int compare_result;
        char address[ADDRESS_BUFFER_SIZE];

        if (iter_existing == NULL)
            compare_result = 1;
        else if (iter_new == NULL)
            compare_result = -1;
        else
            compare_result = address_compare(iter_existing->address, iter_new->address);

        if (compare_result > 0) {
            struct Listener *new_listener = iter_new;
            iter_new = SLIST_NEXT(iter_new, entries);

            notice("Listener %s added.",
                    display_address(new_listener->address,
                            address, sizeof(address)));

            SLIST_REMOVE(new_listeners, new_listener, Listener, entries);
            add_listener(existing_listeners, new_listener);
            init_listener(new_listener, tables, loop);

            /* -1 for removing from new_listeners */
            listener_ref_put(new_listener);
        } else if (compare_result == 0) {
            notice ("Listener %s updated.",
                    display_address(iter_existing->address,
                            address, sizeof(address)));

            listener_update(iter_existing, iter_new, tables);

            iter_existing = SLIST_NEXT(iter_existing, entries);
            iter_new = SLIST_NEXT(iter_new, entries);
        } else {
            struct Listener *removed_listener = iter_existing;
            iter_existing = SLIST_NEXT(iter_existing, entries);

            notice("Listener %s removed.",
                    display_address(removed_listener->address,
                            address, sizeof(address)));

            SLIST_REMOVE(existing_listeners, removed_listener, Listener, entries);
            close_listener(loop, removed_listener);

            /* -1 for removing from existing_listeners */
            listener_ref_put(removed_listener);
        }
    }
}
Esempio n. 3
0
int compare_address_strings(const char *a, const char *b) {
    struct Address *addr_a = new_address(a);
    struct Address *addr_b = new_address(b);

    int result = address_compare(addr_a, addr_b);

    free(addr_a);
    free(addr_b);

    return result;
}
Esempio n. 4
0
/*
 * Copy contents of new_listener into existing listener
 */
static void
listener_update(struct Listener *existing_listener, struct Listener *new_listener, const struct Table_head *tables) {
    assert(existing_listener != NULL);
    assert(new_listener != NULL);
    assert(address_compare(existing_listener->address, new_listener->address) == 0);

    free(existing_listener->fallback_address);
    existing_listener->fallback_address = new_listener->fallback_address;
    new_listener->fallback_address = NULL;

    free(existing_listener->source_address);
    existing_listener->source_address = new_listener->source_address;
    new_listener->source_address = NULL;

    existing_listener->protocol = new_listener->protocol;

    free(existing_listener->table_name);
    existing_listener->table_name = new_listener->table_name;
    new_listener->table_name = NULL;

    logger_ref_put(existing_listener->access_log);
    existing_listener->access_log = logger_ref_get(new_listener->access_log);

    existing_listener->log_bad_requests = new_listener->log_bad_requests;

    struct Table *new_table =
            table_lookup(tables, existing_listener->table_name);

    if (new_table != NULL) {
        init_table(new_table);

        table_ref_put(existing_listener->table);
        existing_listener->table = table_ref_get(new_table);

        table_ref_put(new_listener->table);
        new_listener->table = NULL;
    }
}
Esempio n. 5
0
int main() {
    /* using volatile variables so we can example core dumps */
    for (volatile unsigned int i = 0; i < sizeof(good) / sizeof(struct Test); i++) {
        int port;
        char buffer[255];
        struct Address *addr = new_address(good[i].input);

        assert(addr != NULL);
        assert(address_compare(addr, addr) == 0);
        assert(address_compare(NULL, addr) < 0);
        assert(address_compare(addr, NULL) > 0);
        assert(address_len(addr) > 0);

        if (good[i].expected_type & TYPE_HOSTNAME) {
            assert(address_is_hostname(addr));
            assert(!address_is_sockaddr(addr));
            assert(!address_is_wildcard(addr));
            assert(address_hostname(addr) != NULL);
            assert(address_sa(addr) == NULL);
            assert(address_sa_len(addr) == 0);
        } else if (good[i].expected_type & TYPE_SOCKADDR) {
            assert(!address_is_hostname(addr));
            assert(address_is_sockaddr(addr));
            assert(!address_is_wildcard(addr));
            assert(address_hostname(addr) == NULL);
            assert(address_sa(addr) != NULL);
            assert(address_sa_len(addr) > 0);
        } else if (good[i].expected_type & TYPE_WILDCARD) {
            assert(!address_is_hostname(addr));
            assert(!address_is_sockaddr(addr));
            assert(address_is_wildcard(addr));
            assert(address_hostname(addr) == NULL);
            assert(address_sa(addr) == NULL);
            assert(address_sa_len(addr) == 0);
        }

        display_address(addr, buffer, sizeof(buffer));

        if (strcmp(buffer, good[i].output)) {
            fprintf(stderr, "display_address(%p) returned \"%s\", expected \"%s\"\n", addr, buffer, good[i].output);
            return 1;
        }

        assert(display_address(addr, NULL, 0) == NULL);

        port = address_port(addr);

        if (good[i].port != port) {
            fprintf(stderr, "address_port(%p) return %d, expected %d\n", addr, port, good[i].port);
            return 1;
        }

        address_set_port(addr, port);

        if (good[i].port != port) {
            fprintf(stderr, "address_port(%p) return %d, expected %d\n", addr, port, good[i].port);
            return 1;
        }

        free(addr);
    }

    for (volatile unsigned int i = 0; i < sizeof(bad) / sizeof(const char *); i++) {
        struct Address *addr = new_address(bad[i]);

        if (addr != NULL) {
            fprintf(stderr, "Accepted bad hostname \"%s\"\n", bad[i]);
            return 1;
        }
    }

    assert(compare_address_strings("unix:/dev/log", "127.0.0.1") < 0);
    assert(compare_address_strings("unix:/dev/log", "unix:/dev/logsocket") < 0);
    assert(compare_address_strings("0.0.0.0", "127.0.0.1") < 0);
    assert(compare_address_strings("127.0.0.1", "0.0.0.0") > 0);
    assert(compare_address_strings("127.0.0.1", "127.0.0.1") == 0);
    assert(compare_address_strings("127.0.0.1:80", "127.0.0.1:81") < 0);
    assert(compare_address_strings("*:80", "*:81") < 0);
    assert(compare_address_strings("*:81", "*:80") > 0);
    assert(compare_address_strings("example.com", "example.net") < 0);
    assert(compare_address_strings("example.net", "example.com") > 0);
    assert(compare_address_strings("example.com", "example.com.net") < 0);
    assert(compare_address_strings("example.com.net", "example.com") > 0);
    assert(compare_address_strings("example.com", "example.com:80") < 0);
    assert(compare_address_strings("example.com:80", "example.com") > 0);
    assert(compare_address_strings(NULL, "example.com") < 0);
    assert(compare_address_strings("example.com", NULL) > 0);
    assert(compare_address_strings("example.com", "::") < 0);
    assert(compare_address_strings("::", "example.com") > 0);
    assert(compare_address_strings("0.0.0.0", "*") < 0);
    assert(compare_address_strings("*", "0.0.0.0") > 0);

    do {
        struct Address *addr = new_address("*");

        assert(addr != NULL);
        assert(address_len(addr) > 0);

        free(addr);
    } while (0);

    return 0;
}