summaryrefslogtreecommitdiff
path: root/lib/addns/dnssock.c
blob: 5f9951913587f2edb83aa51de00d50ed11925b87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
  Linux DNS client library implementation

  Copyright (C) 2006 Krishna Ganugapati <krishnag@centeris.com>
  Copyright (C) 2006 Gerald Carter <jerry@samba.org>

     ** NOTE! The following LGPL license applies to the libaddns
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"
#include "dns.h"
#include <sys/time.h>
#include <unistd.h>
#include "system/select.h"

static int destroy_dns_connection(struct dns_connection *conn)
{
	return close(conn->s);
}

/********************************************************************
********************************************************************/

static DNS_ERROR dns_tcp_open( const char *nameserver,
			       TALLOC_CTX *mem_ctx,
			       struct dns_connection **result )
{
	uint32_t ulAddress;
	struct hostent *pHost;
	struct sockaddr_in s_in;
	struct dns_connection *conn;
	int res;

	if (!(conn = talloc(mem_ctx, struct dns_connection))) {
		return ERROR_DNS_NO_MEMORY;
	}

	if ( (ulAddress = inet_addr( nameserver )) == INADDR_NONE ) {
		if ( (pHost = gethostbyname( nameserver )) == NULL ) {
			TALLOC_FREE(conn);
			return ERROR_DNS_INVALID_NAME_SERVER;
		}
		memcpy( &ulAddress, pHost->h_addr, pHost->h_length );
	}

	conn->s = socket( PF_INET, SOCK_STREAM, 0 );
	if (conn->s == -1) {
		TALLOC_FREE(conn);
		return ERROR_DNS_CONNECTION_FAILED;
	}

	talloc_set_destructor(conn, destroy_dns_connection);

	s_in.sin_family = AF_INET;
	s_in.sin_addr.s_addr = ulAddress;
	s_in.sin_port = htons( DNS_TCP_PORT );

	res = connect(conn->s, (struct sockaddr*)&s_in, sizeof( s_in ));
	if (res == -1) {
		TALLOC_FREE(conn);
		return ERROR_DNS_CONNECTION_FAILED;
	}

	conn->hType = DNS_TCP;

	*result = conn;
	return ERROR_DNS_SUCCESS;
}

/********************************************************************
********************************************************************/

static DNS_ERROR dns_udp_open( const char *nameserver,
			       TALLOC_CTX *mem_ctx,
			       struct dns_connection **result )
{
	unsigned long ulAddress;
	struct hostent *pHost;
	struct sockaddr_in RecvAddr;
	struct dns_connection *conn;

	if (!(conn = talloc(NULL, struct dns_connection))) {
		return ERROR_DNS_NO_MEMORY;
	}

	if ( (ulAddress = inet_addr( nameserver )) == INADDR_NONE ) {
		if ( (pHost = gethostbyname( nameserver )) == NULL ) {
			TALLOC_FREE(conn);
			return ERROR_DNS_INVALID_NAME_SERVER;
		}
		memcpy( &ulAddress, pHost->h_addr, pHost->h_length );
	}

	/* Create a socket for sending data */

	conn->s = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
	if (conn->s == -1) {
		TALLOC_FREE(conn);
		return ERROR_DNS_CONNECTION_FAILED;
	}

	talloc_set_destructor(conn, destroy_dns_connection);

	/* Set up the RecvAddr structure with the IP address of
	   the receiver (in this example case "123.456.789.1")
	   and the specified port number. */

	ZERO_STRUCT(RecvAddr);
	RecvAddr.sin_family = AF_INET;
	RecvAddr.sin_port = htons( DNS_UDP_PORT );
	RecvAddr.sin_addr.s_addr = ulAddress;

	conn->hType = DNS_UDP;
	memcpy( &conn->RecvAddr, &RecvAddr, sizeof( struct sockaddr_in ) );

	*result = conn;
	return ERROR_DNS_SUCCESS;
}

/********************************************************************
********************************************************************/

DNS_ERROR dns_open_connection( const char *nameserver, int32_t dwType,
		    TALLOC_CTX *mem_ctx,
		    struct dns_connection **conn )
{
	switch ( dwType ) {
	case DNS_TCP:
		return dns_tcp_open( nameserver, mem_ctx, conn );
	case DNS_UDP:
		return dns_udp_open( nameserver, mem_ctx, conn );
	}

	return ERROR_DNS_INVALID_PARAMETER;
}

static DNS_ERROR write_all(int fd, uint8_t *data, size_t len)
{
	size_t total = 0;

	while (total < len) {

		ssize_t ret = write(fd, data + total, len - total);

		if (ret <= 0) {
			/*
			 * EOF or error
			 */
			return ERROR_DNS_SOCKET_ERROR;
		}

		total += ret;
	}

	return ERROR_DNS_SUCCESS;
}

static DNS_ERROR dns_send_tcp(struct dns_connection *conn,
			      const struct dns_buffer *buf)
{
	uint16_t len = htons(buf->offset);
	DNS_ERROR err;

	err = write_all(conn->s, (uint8_t *)&len, sizeof(len));
	if (!ERR_DNS_IS_OK(err)) return err;

	return write_all(conn->s, buf->data, buf->offset);
}

static DNS_ERROR dns_send_udp(struct dns_connection *conn,
			      const struct dns_buffer *buf)
{
	ssize_t ret;

	ret = sendto(conn->s, buf->data, buf->offset, 0,
		     (struct sockaddr *)&conn->RecvAddr,
		     sizeof(conn->RecvAddr));

	if (ret != buf->offset) {
		return ERROR_DNS_SOCKET_ERROR;
	}

	return ERROR_DNS_SUCCESS;
}

DNS_ERROR dns_send(struct dns_connection *conn, const struct dns_buffer *buf)
{
	if (conn->hType == DNS_TCP) {
		return dns_send_tcp(conn, buf);
	}

	if (conn->hType == DNS_UDP) {
		return dns_send_udp(conn, buf);
	}

	return ERROR_DNS_INVALID_PARAMETER;
}

static DNS_ERROR read_all(int fd, uint8_t *data, size_t len)
{
	size_t total = 0;

	while (total < len) {
		struct pollfd pfd;
		ssize_t ret;
		int fd_ready;

		ZERO_STRUCT(pfd);
		pfd.fd = fd;
		pfd.events = POLLIN|POLLHUP;

		fd_ready = poll(&pfd, 1, 10000);
		if ( fd_ready == 0 ) {
			/* read timeout */
			return ERROR_DNS_SOCKET_ERROR;
		}

		ret = read(fd, data + total, len - total);
		if (ret <= 0) {
			/* EOF or error */
			return ERROR_DNS_SOCKET_ERROR;
		}

		total += ret;
	}

	return ERROR_DNS_SUCCESS;
}

static DNS_ERROR dns_receive_tcp(TALLOC_CTX *mem_ctx,
				 struct dns_connection *conn,
				 struct dns_buffer **presult)
{
	struct dns_buffer *buf;
	DNS_ERROR err;
	uint16_t len;

	if (!(buf = talloc_zero(mem_ctx, struct dns_buffer))) {
		return ERROR_DNS_NO_MEMORY;
	}

	err = read_all(conn->s, (uint8_t *)&len, sizeof(len));
	if (!ERR_DNS_IS_OK(err)) {
		return err;
	}

	buf->size = ntohs(len);

	if (buf->size) {
		if (!(buf->data = talloc_array(buf, uint8_t, buf->size))) {
			TALLOC_FREE(buf);
			return ERROR_DNS_NO_MEMORY;
		}
	} else {
		buf->data = NULL;
	}

	err = read_all(conn->s, buf->data, buf->size);
	if (!ERR_DNS_IS_OK(err)) {
		TALLOC_FREE(buf);
		return err;
	}

	*presult = buf;
	return ERROR_DNS_SUCCESS;
}

static DNS_ERROR dns_receive_udp(TALLOC_CTX *mem_ctx,
				 struct dns_connection *conn,
				 struct dns_buffer **presult)
{
	struct dns_buffer *buf;
	ssize_t received;

	if (!(buf = talloc_zero(mem_ctx, struct dns_buffer))) {
		return ERROR_DNS_NO_MEMORY;
	}

	/*
	 * UDP based DNS can only be 512 bytes
	 */

	if (!(buf->data = talloc_array(buf, uint8_t, 512))) {
		TALLOC_FREE(buf);
		return ERROR_DNS_NO_MEMORY;
	}

	received = recv(conn->s, (void *)buf->data, 512, 0);

	if (received == -1) {
		TALLOC_FREE(buf);
		return ERROR_DNS_SOCKET_ERROR;
	}

	if (received > 512) {
		TALLOC_FREE(buf);
		return ERROR_DNS_BAD_RESPONSE;
	}

	buf->size = received;
	buf->offset = 0;

	*presult = buf;
	return ERROR_DNS_SUCCESS;
}

DNS_ERROR dns_receive(TALLOC_CTX *mem_ctx, struct dns_connection *conn,
		      struct dns_buffer **presult)
{
	if (conn->hType == DNS_TCP) {
		return dns_receive_tcp(mem_ctx, conn, presult);
	}

	if (conn->hType == DNS_UDP) {
		return dns_receive_udp(mem_ctx, conn, presult);
	}

	return ERROR_DNS_INVALID_PARAMETER;
}

DNS_ERROR dns_transaction(TALLOC_CTX *mem_ctx, struct dns_connection *conn,
			  const struct dns_request *req,
			  struct dns_request **resp)
{
	struct dns_buffer *buf = NULL;
	DNS_ERROR err;

	err = dns_marshall_request(mem_ctx, req, &buf);
	if (!ERR_DNS_IS_OK(err)) goto error;

	err = dns_send(conn, buf);
	if (!ERR_DNS_IS_OK(err)) goto error;
	TALLOC_FREE(buf);

	err = dns_receive(mem_ctx, conn, &buf);
	if (!ERR_DNS_IS_OK(err)) goto error;

	err = dns_unmarshall_request(mem_ctx, buf, resp);

 error:
	TALLOC_FREE(buf);
	return err;
}

DNS_ERROR dns_update_transaction(TALLOC_CTX *mem_ctx,
				 struct dns_connection *conn,
				 struct dns_update_request *up_req,
				 struct dns_update_request **up_resp)
{
	struct dns_request *resp;
	DNS_ERROR err;

	err = dns_transaction(mem_ctx, conn, dns_update2request(up_req),
			      &resp);

	if (!ERR_DNS_IS_OK(err)) return err;

	*up_resp = dns_request2update(resp);
	return ERROR_DNS_SUCCESS;
}