コード例 #1
0
ファイル: test-resolve.c プロジェクト: Keruspe/systemd
int main(int argc, char *argv[]) {
        _cleanup_(sd_resolve_query_unrefp) sd_resolve_query *q1 = NULL, *q2 = NULL;
        _cleanup_(sd_resolve_unrefp) sd_resolve *resolve = NULL;
        int r = 0;

        struct addrinfo hints = {
                .ai_family = PF_UNSPEC,
                .ai_socktype = SOCK_STREAM,
                .ai_flags = AI_CANONNAME
        };

        struct sockaddr_in sa = {
                .sin_family = AF_INET,
                .sin_port = htons(80)
        };

        assert_se(sd_resolve_default(&resolve) >= 0);

        /* Test a floating resolver query */
        r = sd_resolve_getaddrinfo(resolve, NULL, "redhat.com", "http", NULL, getaddrinfo_handler, NULL);
        if (r < 0)
                log_error_errno(r, "sd_resolve_getaddrinfo(): %m");

        /* Make a name -> address query */
        r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints, getaddrinfo_handler, NULL);
        if (r < 0)
                log_error_errno(r, "sd_resolve_getaddrinfo(): %m");

        /* Make an address -> name query */
        sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
        r = sd_resolve_getnameinfo(resolve, &q2, (struct sockaddr*) &sa, sizeof(sa), 0, SD_RESOLVE_GET_BOTH, getnameinfo_handler, NULL);
        if (r < 0)
                log_error_errno(r, "sd_resolve_getnameinfo(): %m");

        /* Wait until all queries are completed */
        for (;;) {
                r = sd_resolve_wait(resolve, TEST_TIMEOUT_USEC);
                if (r == 0)
                        break;
                if (r == -ETIMEDOUT) {
                        /* Let's catch timeouts here, so that we can run safely in a CI that has no reliable DNS. Note
                         * that we invoke exit() directly here, as the stuck NSS call will not allow us to exit
                         * cleanly. */

                        log_notice_errno(r, "sd_resolve_wait() timed out, but that's OK");
                        exit(EXIT_SUCCESS);
                }
                if (r < 0) {
                        log_error_errno(r, "sd_resolve_wait(): %m");
                        assert_not_reached("sd_resolve_wait() failed");
                }
        }

        return 0;
}
コード例 #2
0
ファイル: test-resolve.c プロジェクト: robertalks/systemd
int main(int argc, char *argv[]) {
        _cleanup_(sd_resolve_query_unrefp) sd_resolve_query *q1 = NULL, *q2 = NULL;
        _cleanup_(sd_resolve_unrefp) sd_resolve *resolve = NULL;
        int r = 0;

        struct addrinfo hints = {
                .ai_family = PF_UNSPEC,
                .ai_socktype = SOCK_STREAM,
                .ai_flags = AI_CANONNAME
        };

        struct sockaddr_in sa = {
                .sin_family = AF_INET,
                .sin_port = htons(80)
        };

        assert_se(sd_resolve_default(&resolve) >= 0);

        /* Test a floating resolver query */
        sd_resolve_getaddrinfo(resolve, NULL, "redhat.com", "http", NULL, getaddrinfo_handler, NULL);

        /* Make a name -> address query */
        r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints, getaddrinfo_handler, NULL);
        if (r < 0)
                log_error_errno(r, "sd_resolve_getaddrinfo(): %m");

        /* Make an address -> name query */
        sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
        r = sd_resolve_getnameinfo(resolve, &q2, (struct sockaddr*) &sa, sizeof(sa), 0, SD_RESOLVE_GET_BOTH, getnameinfo_handler, NULL);
        if (r < 0)
                log_error_errno(r, "sd_resolve_getnameinfo(): %m");

        /* Wait until the two queries are completed */
        while (sd_resolve_query_is_done(q1) == 0 ||
               sd_resolve_query_is_done(q2) == 0) {

                r = sd_resolve_wait(resolve, (uint64_t) -1);
                if (r < 0) {
                        log_error_errno(r, "sd_resolve_wait(): %m");
                        assert_not_reached("sd_resolve_wait() failed");
                }
        }

        return 0;
}
コード例 #3
0
ファイル: test-resolve.c プロジェクト: kyoiora/systemd
int main(int argc, char *argv[]) {
    _cleanup_resolve_query_unref_ sd_resolve_query *q1 = NULL, *q2 = NULL, *q3 = NULL;
    _cleanup_resolve_unref_ sd_resolve *resolve = NULL;
    int r = 0;

    struct addrinfo hints = {
        .ai_family = PF_UNSPEC,
        .ai_socktype = SOCK_STREAM,
        .ai_flags = AI_CANONNAME
    };

    struct sockaddr_in sa = {
        .sin_family = AF_INET,
        .sin_port = htons(80)
    };

    assert_se(sd_resolve_default(&resolve) >= 0);

    /* Make a name -> address query */
    r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints, getaddrinfo_handler, NULL);
    if (r < 0)
        log_error("sd_resolve_getaddrinfo(): %s\n", strerror(-r));

    /* Make an address -> name query */
    sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
    r = sd_resolve_getnameinfo(resolve, &q2, (struct sockaddr*) &sa, sizeof(sa), 0, SD_RESOLVE_GET_BOTH, getnameinfo_handler, NULL);
    if (r < 0)
        log_error("sd_resolve_getnameinfo(): %s\n", strerror(-r));

    /* Make a res_query() call */
    r = sd_resolve_res_query(resolve, &q3, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV, res_handler, NULL);
    if (r < 0)
        log_error("sd_resolve_res_query(): %s\n", strerror(-r));

    /* Wait until the three queries are completed */
    while (sd_resolve_query_is_done(q1) == 0 ||
            sd_resolve_query_is_done(q2) == 0 ||
            sd_resolve_query_is_done(q3) == 0) {

        r = sd_resolve_wait(resolve, (uint64_t) -1);
        if (r < 0) {
            log_error("sd_resolve_wait(): %s\n", strerror(-r));
            assert_not_reached("sd_resolve_wait() failed");
        }
    }

    return 0;
}
コード例 #4
0
ファイル: socket-proxyd.c プロジェクト: floppym/systemd
static int resolve_remote(Connection *c) {

        static const struct addrinfo hints = {
                .ai_family = AF_UNSPEC,
                .ai_socktype = SOCK_STREAM,
                .ai_flags = AI_ADDRCONFIG
        };

        union sockaddr_union sa = {};
        const char *node, *service;
        int r;

        if (IN_SET(arg_remote_host[0], '/', '@')) {
                int salen;

                salen = sockaddr_un_set_path(&sa.un, arg_remote_host);
                if (salen < 0) {
                        log_error_errno(salen, "Specified address doesn't fit in an AF_UNIX address, refusing: %m");
                        goto fail;
                }

                return connection_start(c, &sa.sa, salen);
        }

        service = strrchr(arg_remote_host, ':');
        if (service) {
                node = strndupa(arg_remote_host, service - arg_remote_host);
                service++;
        } else {
                node = arg_remote_host;
                service = "80";
        }

        log_debug("Looking up address info for %s:%s", node, service);
        r = sd_resolve_getaddrinfo(c->context->resolve, &c->resolve_query, node, service, &hints, resolve_cb, c);
        if (r < 0) {
                log_error_errno(r, "Failed to resolve remote host: %m");
                goto fail;
        }

        return 0;

fail:
        connection_free(c);
        return 0; /* ignore errors, continue serving */
}
コード例 #5
0
int manager_connect(Manager *m) {
        int r;

        assert(m);

        manager_disconnect(m);

        m->event_retry = sd_event_source_unref(m->event_retry);
        if (!ratelimit_test(&m->ratelimit)) {
                log_debug("Slowing down attempts to contact servers.");

                r = sd_event_add_time(m->event, &m->event_retry, clock_boottime_or_monotonic(), now(clock_boottime_or_monotonic()) + RETRY_USEC, 0, manager_retry_connect, m);
                if (r < 0) {
                        log_error("Failed to create retry timer: %s", strerror(-r));
                        return r;
                }

                return 0;
        }

        /* If we already are operating on some address, switch to the
         * next one. */
        if (m->current_server_address && m->current_server_address->addresses_next)
                manager_set_server_address(m, m->current_server_address->addresses_next);
        else {
                struct addrinfo hints = {
                        .ai_flags = AI_NUMERICSERV|AI_ADDRCONFIG,
                        .ai_socktype = SOCK_DGRAM,
                };

                /* Hmm, we are through all addresses, let's look for the next host instead */
                if (m->current_server_name && m->current_server_name->names_next)
                        manager_set_server_name(m, m->current_server_name->names_next);
                else {
                        ServerName *f;

                        /* Our current server name list is exhausted,
                         * let's find the next one to iterate. First
                         * we try the system list, then the link list.
                         * After having processed the link list we
                         * jump back to the system list. However, if
                         * both lists are empty, we change to the
                         * fallback list. */
                        if (!m->current_server_name || m->current_server_name->type == SERVER_LINK) {
                                f = m->system_servers;
                                if (!f)
                                        f = m->link_servers;
                        } else {
                                f = m->link_servers;
                                if (!f)
                                        f = m->system_servers;
                        }

                        if (!f)
                                f = m->fallback_servers;

                        if (!f) {
                                manager_set_server_name(m, NULL);
                                log_debug("No server found.");
                                return 0;
                        }

                        manager_set_server_name(m, f);
                }

                /* Tell the resolver to reread /etc/resolv.conf, in
                 * case it changed. */
                res_init();

                /* Flush out any previously resolved addresses */
                server_name_flush_addresses(m->current_server_name);

                log_debug("Resolving %s...", m->current_server_name->string);

                r = sd_resolve_getaddrinfo(m->resolve, &m->resolve_query, m->current_server_name->string, "123", &hints, manager_resolve_handler, m);
                if (r < 0) {
                        log_error("Failed to create resolver: %s", strerror(-r));
                        return r;
                }

                return 1;
        }

        r = manager_begin(m);
        if (r < 0)
                return r;

        return 1;
}

void manager_disconnect(Manager *m) {
        assert(m);

        m->resolve_query = sd_resolve_query_unref(m->resolve_query);

        m->event_timer = sd_event_source_unref(m->event_timer);

        m->event_receive = sd_event_source_unref(m->event_receive);
        m->server_socket = safe_close(m->server_socket);

        m->event_clock_watch = sd_event_source_unref(m->event_clock_watch);
        m->clock_watch_fd = safe_close(m->clock_watch_fd);

        m->event_timeout = sd_event_source_unref(m->event_timeout);

        sd_notifyf(false, "STATUS=Idle.");
}