libdns: tsocket returns -1 and sets errno
[obnox/samba/samba-obnox.git] / libcli / dns / dns.c
index ac0c9e4995954bf89dafbeb02f629f04214eadee..90b7618bdca06f8a509cf50b853a7749beaa1eea 100644 (file)
@@ -38,9 +38,11 @@ struct dns_udp_request_state {
        size_t reply_len;
 };
 
+#define DNS_REQUEST_TIMEOUT 2
+
 /* Declare callback functions used below. */
-static void dns_request_get_reply(struct tevent_req *subreq);
-static void dns_request_done(struct tevent_req *subreq);
+static void dns_udp_request_get_reply(struct tevent_req *subreq);
+static void dns_udp_request_done(struct tevent_req *subreq);
 
 struct tevent_req *dns_udp_request_send(TALLOC_CTX *mem_ctx,
                                        struct tevent_context *ev,
@@ -65,20 +67,20 @@ struct tevent_req *dns_udp_request_send(TALLOC_CTX *mem_ctx,
        ret = tsocket_address_inet_from_strings(state, "ip", NULL, 0,
                                                &local_addr);
        if (ret != 0) {
-               tevent_req_werror(req, unix_to_werror(ret));
+               tevent_req_werror(req, unix_to_werror(errno));
                return tevent_req_post(req, ev);
        }
 
        ret = tsocket_address_inet_from_strings(state, "ip", server_addr_string,
                                                DNS_SERVICE_PORT, &server_addr);
        if (ret != 0) {
-               tevent_req_werror(req, unix_to_werror(ret));
+               tevent_req_werror(req, unix_to_werror(errno));
                return tevent_req_post(req, ev);
        }
 
        ret = tdgram_inet_udp_socket(local_addr, server_addr, state, &dgram);
        if (ret != 0) {
-               tevent_req_werror(req, unix_to_werror(ret));
+               tevent_req_werror(req, unix_to_werror(errno));
                return tevent_req_post(req, ev);
        }
 
@@ -92,11 +94,17 @@ struct tevent_req *dns_udp_request_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
 
-       tevent_req_set_callback(subreq, dns_request_get_reply, req);
+       if (!tevent_req_set_endtime(req, ev,
+                               timeval_current_ofs(DNS_REQUEST_TIMEOUT, 0))) {
+               return tevent_req_post(req, ev);
+       }
+
+
+       tevent_req_set_callback(subreq, dns_udp_request_get_reply, req);
        return req;
 }
 
-static void dns_request_get_reply(struct tevent_req *subreq)
+static void dns_udp_request_get_reply(struct tevent_req *subreq)
 {
        struct tevent_req *req = tevent_req_callback_data(subreq,
                                                struct tevent_req);
@@ -123,11 +131,11 @@ static void dns_request_get_reply(struct tevent_req *subreq)
                return;
        }
 
-       tevent_req_set_callback(subreq, dns_request_done, req);
+       tevent_req_set_callback(subreq, dns_udp_request_done, req);
        return;
 }
 
-static void dns_request_done(struct tevent_req *subreq)
+static void dns_udp_request_done(struct tevent_req *subreq)
 {
        struct tevent_req *req = tevent_req_callback_data(subreq,
                                                struct tevent_req);