summaryrefslogtreecommitdiff
path: root/source4/libcli/wins/winsrepl.c
blob: 6ee948202faa7cddd362ce633aaacbe5062180d5 (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* 
   Unix SMB/CIFS implementation.

   low level WINS replication client code

   Copyright (C) Andrew Tridgell 2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program 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 General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "lib/events/events.h"
#include "dlinklist.h"
#include "lib/socket/socket.h"
#include "libcli/wins/winsrepl.h"

/*
  mark all pending requests as dead - called when a socket error happens
*/
static void wrepl_socket_dead(struct wrepl_socket *wrepl_socket)
{
	event_set_fd_flags(wrepl_socket->fde, 0);

	while (wrepl_socket->send_queue) {
		struct wrepl_request *req = wrepl_socket->send_queue;
		DLIST_REMOVE(wrepl_socket->send_queue, req);
		req->state = WREPL_REQUEST_ERROR;
		req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
		if (req->async.fn) {
			req->async.fn(req);
		}
	}
	while (wrepl_socket->recv_queue) {
		struct wrepl_request *req = wrepl_socket->recv_queue;
		DLIST_REMOVE(wrepl_socket->recv_queue, req);
		req->state = WREPL_REQUEST_ERROR;
		req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
		if (req->async.fn) {
			req->async.fn(req);
		}
	}
}

/*
  handle send events 
*/
static void wrepl_handler_send(struct wrepl_socket *wrepl_socket)
{
	while (wrepl_socket->send_queue) {
		struct wrepl_request *req = wrepl_socket->send_queue;
		size_t nsent;
		NTSTATUS status;

		status = socket_send(wrepl_socket->sock, &req->buffer, &nsent, 0);
		if (NT_STATUS_IS_ERR(status)) {
			wrepl_socket_dead(wrepl_socket);
			return;
		}
		if (!NT_STATUS_IS_OK(status) || nsent == 0) return;

		req->buffer.data   += nsent;
		req->buffer.length -= nsent;
		if (req->buffer.length != 0) {
			return;
		}

		DLIST_REMOVE(wrepl_socket->send_queue, req);
		DLIST_ADD_END(wrepl_socket->recv_queue, req, struct wrepl_request *);
		req->state = WREPL_REQUEST_RECV;

		EVENT_FD_READABLE(wrepl_socket->fde);
	}

	EVENT_FD_NOT_WRITEABLE(wrepl_socket->fde);
}


/*
  handle recv events 
*/
static void wrepl_handler_recv(struct wrepl_socket *wrepl_socket)
{
	size_t nread;
	struct wrepl_request *req = wrepl_socket->recv_queue;
	DATA_BLOB blob;

	if (req == NULL) {
		EVENT_FD_NOT_READABLE(wrepl_socket->fde);
		return;
	}

	if (req->buffer.length == 0) {
		req->buffer = data_blob_talloc(req, NULL, 4);
		if (req->buffer.data == NULL) {
			req->status = NT_STATUS_NO_MEMORY;
			goto failed;
		}
		req->num_read = 0;
	}

	/* read in the packet length */
	if (req->num_read < 4) {
		uint32_t req_length;

		req->status = socket_recv(wrepl_socket->sock, 
					  req->buffer.data + req->num_read,
					  4 - req->num_read,
					  &nread, 0);
		if (NT_STATUS_IS_ERR(req->status)) goto failed;
		if (!NT_STATUS_IS_OK(req->status)) return;

		req->num_read += nread;
		if (req->num_read != 4) return;

		req_length = RIVAL(req->buffer.data, 0) + 4;

		req->buffer.data = talloc_realloc(req, req->buffer.data, 
						  uint8_t, req_length);
		if (req->buffer.data == NULL) {
			req->status = NT_STATUS_NO_MEMORY;
			goto failed;
		}
		req->buffer.length = req_length;
	}

	/* read in the body */
	req->status = socket_recv(wrepl_socket->sock, 
				  req->buffer.data + req->num_read,
				  req->buffer.length - req->num_read,
				  &nread, 0);
	if (NT_STATUS_IS_ERR(req->status)) goto failed;
	if (!NT_STATUS_IS_OK(req->status)) return;

	req->num_read += nread;
	if (req->num_read != req->buffer.length) return;

	req->packet = talloc(req, struct wrepl_packet);
	if (req->packet == NULL) {
		req->status = NT_STATUS_NO_MEMORY;
		goto failed;
	}

	blob.data = req->buffer.data + 4;
	blob.length = req->buffer.length - 4;
	
	/* we have a full request - parse it */
	req->status = ndr_pull_struct_blob(&blob,
					   req->packet, req->packet,
					   (ndr_pull_flags_fn_t)ndr_pull_wrepl_packet);
	if (!NT_STATUS_IS_OK(req->status)) {
		DEBUG(2,("Failed to parse incoming WINS packet - %s\n",
			 nt_errstr(req->status)));
		DEBUG(10,("packet length %d\n", req->buffer.length));
		NDR_PRINT_DEBUG(wrepl_packet, req->packet);
		goto failed;
	}

	if (DEBUGLVL(10)) {
		DEBUG(10,("Received WINS packet of length %d\n", req->buffer.length));
		NDR_PRINT_DEBUG(wrepl_packet, req->packet);
	}

	DLIST_REMOVE(wrepl_socket->recv_queue, req);
	req->state = WREPL_REQUEST_DONE;
	if (req->async.fn) {
		req->async.fn(req);
	}
	return;

failed:
	if (req->state == WREPL_REQUEST_RECV) {
		DLIST_REMOVE(wrepl_socket->recv_queue, req);
	}
	req->state = WREPL_REQUEST_ERROR;
	if (req->async.fn) {
		req->async.fn(req);
	}
}


/*
  handler for winrepl events
*/
static void wrepl_handler(struct event_context *ev, struct fd_event *fde, 
			  uint16_t flags, void *private)
{
	struct wrepl_socket *wrepl_socket = talloc_get_type(private, 
							    struct wrepl_socket);
	if (flags & EVENT_FD_WRITE) {
		wrepl_handler_send(wrepl_socket);
		return;
	}
	if (flags & EVENT_FD_READ) {
		wrepl_handler_recv(wrepl_socket);
	}
}


/*
  handler for winrepl connection completion
*/
static void wrepl_connect_handler(struct event_context *ev, struct fd_event *fde, 
				  uint16_t flags, void *private)
{
	struct wrepl_socket *wrepl_socket = talloc_get_type(private, 
							    struct wrepl_socket);
	struct wrepl_request *req = wrepl_socket->recv_queue;

	talloc_free(fde);

	if (req == NULL) return;

	req->status = socket_connect_complete(wrepl_socket->sock, 0);
	if (NT_STATUS_IS_ERR(req->status)) goto failed;

	if (!NT_STATUS_IS_OK(req->status)) return;

	wrepl_socket->fde = event_add_fd(wrepl_socket->event_ctx, wrepl_socket, 
					 socket_get_fd(wrepl_socket->sock), 
					 0,
					 wrepl_handler, wrepl_socket);
	if (wrepl_socket->fde == NULL) {
		req->status = NT_STATUS_NO_MEMORY;
	}


failed:
	DLIST_REMOVE(wrepl_socket->recv_queue, req);
	if (!NT_STATUS_IS_OK(req->status)) {
		req->state = WREPL_REQUEST_ERROR;
	} else {
		req->state = WREPL_REQUEST_DONE;
	}
	if (req->async.fn) {
		req->async.fn(req);
	}
}


/*
  initialise a wrepl_socket. The event_ctx is optional, if provided then
  operations will use that event context
*/
struct wrepl_socket *wrepl_socket_init(TALLOC_CTX *mem_ctx, 
				       struct event_context *event_ctx)
{
	struct wrepl_socket *wrepl_socket;
	NTSTATUS status;

	wrepl_socket = talloc(mem_ctx, struct wrepl_socket);
	if (wrepl_socket == NULL) goto failed;

	if (event_ctx == NULL) {
		wrepl_socket->event_ctx = event_context_init(wrepl_socket);
	} else {
		wrepl_socket->event_ctx = talloc_reference(wrepl_socket, event_ctx);
	}
	if (wrepl_socket->event_ctx == NULL) goto failed;

	status = socket_create("ip", SOCKET_TYPE_STREAM, &wrepl_socket->sock, 0);
	if (!NT_STATUS_IS_OK(status)) goto failed;

	talloc_steal(wrepl_socket, wrepl_socket->sock);

	wrepl_socket->send_queue = NULL;
	wrepl_socket->recv_queue = NULL;

	wrepl_socket->fde = event_add_fd(wrepl_socket->event_ctx, wrepl_socket, 
					 socket_get_fd(wrepl_socket->sock), 
					 EVENT_FD_WRITE,
					 wrepl_connect_handler, wrepl_socket);

	set_blocking(socket_get_fd(wrepl_socket->sock), False);
	
	return wrepl_socket;

failed:
	talloc_free(wrepl_socket);
	return NULL;
}


/*
  destroy a wrepl_request
*/
static int wrepl_request_destructor(void *ptr)
{
	struct wrepl_request *req = talloc_get_type(ptr, struct wrepl_request);
	if (req->state == WREPL_REQUEST_SEND) {
		DLIST_REMOVE(req->wrepl_socket->send_queue, req);
	}
	if (req->state == WREPL_REQUEST_RECV) {
		DLIST_REMOVE(req->wrepl_socket->recv_queue, req);
	}
	req->state = WREPL_REQUEST_ERROR;
	return 0;
}

/*
  wait for a request to complete
*/
static NTSTATUS wrepl_request_wait(struct wrepl_request *req)
{
	NT_STATUS_HAVE_NO_MEMORY(req);
	while (req->state < WREPL_REQUEST_DONE) {
		event_loop_once(req->wrepl_socket->event_ctx);
	}
	return req->status;
}


/*
  connect a wrepl_socket to a WINS server
*/
struct wrepl_request *wrepl_connect_send(struct wrepl_socket *wrepl_socket,
					 const char *address)
{
	struct wrepl_request *req;
	NTSTATUS status;

	req = talloc_zero(wrepl_socket, struct wrepl_request);
	if (req == NULL) goto failed;

	req->wrepl_socket = wrepl_socket;
	req->state        = WREPL_REQUEST_RECV;

	DLIST_ADD(wrepl_socket->recv_queue, req);

	talloc_set_destructor(req, wrepl_request_destructor);
	
	status = socket_connect(wrepl_socket->sock, NULL, 0, address, 
				WINS_REPLICATION_PORT, 0);
	if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) goto failed;

	return req;

failed:
	talloc_free(req);
	return NULL;
}

/*
  connect a wrepl_socket to a WINS server - recv side
*/
NTSTATUS wrepl_connect_recv(struct wrepl_request *req)
{
	return wrepl_request_wait(req);
}


/*
  connect a wrepl_socket to a WINS server - sync API
*/
NTSTATUS wrepl_connect(struct wrepl_socket *wrepl_socket, const char *address)
{
	struct wrepl_request *req = wrepl_connect_send(wrepl_socket, address);
	return wrepl_connect_recv(req);
}


/*
  send a generic wins replication request
*/
struct wrepl_request *wrepl_request_send(struct wrepl_socket *wrepl_socket,
					 struct wrepl_packet *packet)
{
	struct wrepl_request *req;
	struct wrepl_wrap wrap;

	req = talloc_zero(wrepl_socket, struct wrepl_request);
	if (req == NULL) goto failed;

	req->wrepl_socket = wrepl_socket;
	req->state        = WREPL_REQUEST_SEND;

	wrap.packet = *packet;
	req->status = ndr_push_struct_blob(&req->buffer, req, &wrap,
					   (ndr_push_flags_fn_t)ndr_push_wrepl_wrap);	
	if (!NT_STATUS_IS_OK(req->status)) goto failed;

	if (DEBUGLVL(10)) {
		DEBUG(10,("Sending WINS packet of length %d\n", req->buffer.length));
		NDR_PRINT_DEBUG(wrepl_packet, &wrap.packet);
	}

	DLIST_ADD(wrepl_socket->send_queue, req);

	talloc_set_destructor(req, wrepl_request_destructor);

	EVENT_FD_WRITEABLE(wrepl_socket->fde);
	
	return req;

failed:
	talloc_free(req);
	return NULL;
}

/*
  receive a generic WINS replication reply
*/
NTSTATUS wrepl_request_recv(struct wrepl_request *req,
			    TALLOC_CTX *mem_ctx,
			    struct wrepl_packet **packet)
{
	NTSTATUS status = wrepl_request_wait(req);
	if (NT_STATUS_IS_OK(status)) {
		*packet = talloc_steal(mem_ctx, req->packet);
	}
	talloc_free(req);
	return status;
}

/*
  a full WINS replication request/response
*/
NTSTATUS wrepl_request(struct wrepl_socket *wrepl_socket,
		       TALLOC_CTX *mem_ctx,
		       struct wrepl_packet *req_packet,
		       struct wrepl_packet **reply_packet)
{
	struct wrepl_request *req = wrepl_request_send(wrepl_socket, req_packet);
	return wrepl_request_recv(req, mem_ctx, reply_packet);
}