s3-smbd: Don't segfault if user specified ports out for range.
[Samba.git] / libcli / dns / dns.c
blobda65ce4c4ffa951d9d3a8619c3ceac921b5f415a
1 /*
2 Unix SMB/CIFS implementation.
4 Small async DNS library for Samba with socketwrapper support
6 Copyright (C) 2010 Kai Blin <kai@samba.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "replace.h"
23 #include "system/network.h"
24 #include <tevent.h>
25 #include "lib/tsocket/tsocket.h"
26 #include "libcli/util/werror.h"
27 #include "libcli/dns/libdns.h"
28 #include "lib/util/tevent_werror.h"
29 #include "lib/util/samba_util.h"
30 #include "libcli/util/error.h"
31 #include "librpc/gen_ndr/dns.h"
33 struct dns_udp_request_state {
34 struct tevent_context *ev;
35 struct tdgram_context *dgram;
36 size_t query_len;
37 uint8_t *reply;
38 size_t reply_len;
41 /* Declare callback functions used below. */
42 static void dns_udp_request_get_reply(struct tevent_req *subreq);
43 static void dns_udp_request_done(struct tevent_req *subreq);
45 struct tevent_req *dns_udp_request_send(TALLOC_CTX *mem_ctx,
46 struct tevent_context *ev,
47 const char *server_addr_string,
48 const uint8_t *query,
49 size_t query_len)
51 struct tevent_req *req, *subreq;
52 struct dns_udp_request_state *state;
53 struct tsocket_address *local_addr, *server_addr;
54 struct tdgram_context *dgram;
55 int ret;
57 req = tevent_req_create(mem_ctx, &state, struct dns_udp_request_state);
58 if (req == NULL) {
59 return NULL;
62 state->ev = ev;
64 /* Use connected UDP sockets */
65 ret = tsocket_address_inet_from_strings(state, "ip", NULL, 0,
66 &local_addr);
67 if (ret != 0) {
68 tevent_req_werror(req, unix_to_werror(ret));
69 return tevent_req_post(req, ev);
72 ret = tsocket_address_inet_from_strings(state, "ip", server_addr_string,
73 DNS_SERVICE_PORT, &server_addr);
74 if (ret != 0) {
75 tevent_req_werror(req, unix_to_werror(ret));
76 return tevent_req_post(req, ev);
79 ret = tdgram_inet_udp_socket(local_addr, server_addr, state, &dgram);
80 if (ret != 0) {
81 tevent_req_werror(req, unix_to_werror(ret));
82 return tevent_req_post(req, ev);
85 state->dgram = dgram;
86 state->query_len = query_len;
88 dump_data(10, query, query_len);
90 subreq = tdgram_sendto_send(state, ev, dgram, query, query_len, NULL);
91 if (tevent_req_nomem(subreq, req)) {
92 return tevent_req_post(req, ev);
95 tevent_req_set_callback(subreq, dns_udp_request_get_reply, req);
96 return req;
99 static void dns_udp_request_get_reply(struct tevent_req *subreq)
101 struct tevent_req *req = tevent_req_callback_data(subreq,
102 struct tevent_req);
103 struct dns_udp_request_state *state = tevent_req_data(req,
104 struct dns_udp_request_state);
105 ssize_t len;
106 int err = 0;
108 len = tdgram_sendto_recv(subreq, &err);
109 TALLOC_FREE(subreq);
111 if (len == -1 && err != 0) {
112 tevent_req_werror(req, unix_to_werror(err));
113 return;
116 if (len != state->query_len) {
117 tevent_req_werror(req, WERR_NET_WRITE_FAULT);
118 return;
121 subreq = tdgram_recvfrom_send(state, state->ev, state->dgram);
122 if (tevent_req_nomem(subreq, req)) {
123 return;
126 tevent_req_set_callback(subreq, dns_udp_request_done, req);
127 return;
130 static void dns_udp_request_done(struct tevent_req *subreq)
132 struct tevent_req *req = tevent_req_callback_data(subreq,
133 struct tevent_req);
134 struct dns_udp_request_state *state = tevent_req_data(req,
135 struct dns_udp_request_state);
137 ssize_t len;
138 int err = 0;
140 len = tdgram_recvfrom_recv(subreq, &err, state, &state->reply, NULL);
141 TALLOC_FREE(subreq);
143 if (len == -1 && err != 0) {
144 tevent_req_werror(req, unix_to_werror(err));
145 return;
148 state->reply_len = len;
149 dump_data(10, state->reply, state->reply_len);
150 tevent_req_done(req);
153 WERROR dns_udp_request_recv(struct tevent_req *req,
154 TALLOC_CTX *mem_ctx,
155 uint8_t **reply,
156 size_t *reply_len)
158 struct dns_udp_request_state *state = tevent_req_data(req,
159 struct dns_udp_request_state);
160 WERROR w_error;
162 if (tevent_req_is_werror(req, &w_error)) {
163 tevent_req_received(req);
164 return w_error;
167 *reply = talloc_move(mem_ctx, &state->reply);
168 *reply_len = state->reply_len;
169 tevent_req_received(req);
171 return WERR_OK;