Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bb330746 authored by Bernie Innocenti's avatar Bernie Innocenti
Browse files

Delete the _u._ext nonsense from res_state

Also eliminate _u._ext.nscount, which was nothing but a flag to
clear _u._ext.nssocks on the first run of res_nsend(). Moved the
initialization to res_init() instead.

We're starting to get closer to the final shape of this code. Next steps
would involve eliminating res_static and all the code in res_state.cpp
dealing with thread-local storage.

Test: m, flash, atest
Change-Id: I4585b9d798d5ff9b8482dc12619d998d77398a0b
parent 67a2790c
Loading
Loading
Loading
Loading
+8 −5
Original line number Diff line number Diff line
@@ -102,9 +102,12 @@ void res_init(res_state statp) {
    statp->ndots = 1;
    statp->_vcsock = -1;
    statp->_flags = 0;
    statp->_u._ext.nscount = 0;
    statp->netcontext_flags = 0;

    for (int ns = 0; ns < MAXNS; ns++) {
        statp->nssocks[ns] = -1;
    }

    // The following dummy initialization is probably useless because
    // it's overwritten later by _resolv_populate_res_for_net().
    // TODO: check if it's safe to remove.
@@ -132,10 +135,10 @@ void res_nclose(res_state statp) {
        statp->_vcsock = -1;
        statp->_flags &= ~RES_F_VC;
    }
    for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
        if (statp->_u._ext.nssocks[ns] != -1) {
            (void) close(statp->_u._ext.nssocks[ns]);
            statp->_u._ext.nssocks[ns] = -1;
    for (ns = 0; ns < MAXNS; ns++) {
        if (statp->nssocks[ns] != -1) {
            close(statp->nssocks[ns]);
            statp->nssocks[ns] = -1;
        }
    }
}
+8 −18
Original line number Diff line number Diff line
@@ -455,16 +455,6 @@ int res_nsend(res_state statp, const uint8_t* buf, int buflen, uint8_t* ans, int
        return -ESRCH;
    }

    /*
     * Maybe initialize our private copy of the ns_addr_list.
     */
    if (statp->_u._ext.nscount == 0) {
        for (int ns = 0; ns < statp->nscount; ns++) {
            statp->_u._ext.nssocks[ns] = -1;
        }
        statp->_u._ext.nscount = statp->nscount;
    }

    res_stats stats[MAXNS];
    res_params params;
    int revision_id = resolv_cache_get_resolver_stats(statp->netid, &params, stats);
@@ -956,9 +946,9 @@ static int send_dg(res_state statp, res_params* params, const uint8_t* buf, int

    nsap = get_nsaddr(statp, (size_t) ns);
    nsaplen = get_salen(nsap);
    if (statp->_u._ext.nssocks[ns] == -1) {
        statp->_u._ext.nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
        if (statp->_u._ext.nssocks[ns] < 0) {
    if (statp->nssocks[ns] == -1) {
        statp->nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
        if (statp->nssocks[ns] < 0) {
            switch (errno) {
                case EPROTONOSUPPORT:
                case EPFNOSUPPORT:
@@ -972,9 +962,9 @@ static int send_dg(res_state statp, res_params* params, const uint8_t* buf, int
            }
        }

        resolv_tag_socket(statp->_u._ext.nssocks[ns], statp->uid);
        resolv_tag_socket(statp->nssocks[ns], statp->uid);
        if (statp->_mark != MARK_UNSET) {
            if (setsockopt(statp->_u._ext.nssocks[ns], SOL_SOCKET, SO_MARK, &(statp->_mark),
            if (setsockopt(statp->nssocks[ns], SOL_SOCKET, SO_MARK, &(statp->_mark),
                           sizeof(statp->_mark)) < 0) {
                res_nclose(statp);
                return -1;
@@ -984,19 +974,19 @@ static int send_dg(res_state statp, res_params* params, const uint8_t* buf, int
        // on the next socket operation when the server responds with an
        // ICMP port-unreachable error. This way we can detect the absence of
        // a nameserver without timing out.
        if (random_bind(statp->_u._ext.nssocks[ns], nsap->sa_family) < 0) {
        if (random_bind(statp->nssocks[ns], nsap->sa_family) < 0) {
            dump_error("bind(dg)", nsap, nsaplen);
            res_nclose(statp);
            return (0);
        }
        if (connect(statp->_u._ext.nssocks[ns], nsap, (socklen_t) nsaplen) < 0) {
        if (connect(statp->nssocks[ns], nsap, (socklen_t)nsaplen) < 0) {
            dump_error("connect(dg)", nsap, nsaplen);
            res_nclose(statp);
            return (0);
        }
        LOG(DEBUG) << __func__ << ": new DG socket";
    }
    s = statp->_u._ext.nssocks[ns];
    s = statp->nssocks[ns];
    if (send(s, (const char*) buf, (size_t) buflen, 0) != buflen) {
        PLOG(DEBUG) << __func__ << ": send: ";
        res_nclose(statp);
+1 −6
Original line number Diff line number Diff line
@@ -95,16 +95,11 @@ struct __res_state {
    uint16_t id;                              // current message id
    std::vector<std::string> search_domains;  // domains to search
    sockaddr_union nsaddrs[MAXNS];
    int nssocks[MAXNS];
    unsigned ndots : 4;                       // threshold for initial abs. query
    unsigned _mark;       /* If non-0 SET_MARK to _mark on all request sockets */
    int _vcsock;          /* PRIVATE: for res_send VC i/o */
    uint32_t _flags;      /* PRIVATE: see below */
    union {
        struct {
            uint16_t nscount;
            int nssocks[MAXNS];
        } _ext;
    } _u;
    struct res_static rstatic[1];
    android::net::NetworkDnsEventReported* event;
    uint32_t netcontext_flags;