summaryrefslogtreecommitdiff
path: root/source4/wrepl_server/wrepl_out_connection.c
blob: 31a72307d708641cc47731a57b2cc205e1a1d914 (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
/* 
   Unix SMB/CIFS implementation.
   
   WINS Replication server
   
   Copyright (C) Stefan Metzmacher	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 "dlinklist.h"
#include "lib/events/events.h"
#include "lib/socket/socket.h"
#include "smbd/service_task.h"
#include "smbd/service_stream.h"
#include "lib/messaging/irpc.h"
#include "librpc/gen_ndr/ndr_winsrepl.h"
#include "wrepl_server/wrepl_server.h"
#include "nbt_server/wins/winsdb.h"
#include "ldb/include/ldb.h"
#include "libcli/composite/composite.h"
#include "libcli/wrepl/winsrepl.h"
#include "wrepl_server/wrepl_out_helpers.h"

static void wreplsrv_pull_handler_te(struct event_context *ev, struct timed_event *te,
				     struct timeval t, void *ptr);

static void wreplsrv_pull_handler_creq(struct composite_context *creq)
{
	struct wreplsrv_partner *partner = talloc_get_type(creq->async.private_data, struct wreplsrv_partner);
	uint32_t interval;

	partner->pull.last_status = wreplsrv_pull_cycle_recv(partner->pull.creq);
	partner->pull.creq = NULL;
	talloc_free(partner->pull.cycle_io);
	partner->pull.cycle_io = NULL;

	if (!NT_STATUS_IS_OK(partner->pull.last_status)) {
		interval = partner->pull.error_count * partner->pull.retry_interval;
		interval = MIN(interval, partner->pull.interval);
		partner->pull.error_count++;

		DEBUG(1,("wreplsrv_pull_cycle(%s): %s: next: %us\n",
			 partner->address, nt_errstr(partner->pull.last_status),
			 interval));
	} else {
		interval = partner->pull.interval;
		partner->pull.error_count = 0;

		DEBUG(2,("wreplsrv_pull_cycle(%s): %s: next: %us\n",
			 partner->address, nt_errstr(partner->pull.last_status),
			 interval));
	}

	partner->pull.te = event_add_timed(partner->service->task->event_ctx, partner,
					   timeval_current_ofs(interval, 0),
					   wreplsrv_pull_handler_te, partner);
	if (!partner->pull.te) {
		DEBUG(0,("wreplsrv_pull_handler_creq: event_add_timed() failed! no memory!\n"));
	}
}

static void wreplsrv_pull_handler_te(struct event_context *ev, struct timed_event *te,
				     struct timeval t, void *ptr)
{
	struct wreplsrv_partner *partner = talloc_get_type(ptr, struct wreplsrv_partner);

	partner->pull.te = NULL;

	partner->pull.cycle_io = talloc(partner, struct wreplsrv_pull_cycle_io);
	if (!partner->pull.cycle_io) {
		goto requeue;
	}

	partner->pull.cycle_io->in.partner	= partner;
	partner->pull.cycle_io->in.num_owners	= 0;
	partner->pull.cycle_io->in.owners	= NULL;
	partner->pull.cycle_io->in.wreplconn	= NULL;
	partner->pull.creq = wreplsrv_pull_cycle_send(partner->pull.cycle_io, partner->pull.cycle_io);
	if (!partner->pull.creq) {
		DEBUG(1,("wreplsrv_pull_cycle_send(%s) failed\n",
			 partner->address));
		goto requeue;
	}

	partner->pull.creq->async.fn		= wreplsrv_pull_handler_creq;
	partner->pull.creq->async.private_data	= partner;

	return;
requeue:
	talloc_free(partner->pull.cycle_io);
	partner->pull.cycle_io = NULL;
	/* retry later */
	partner->pull.te = event_add_timed(partner->service->task->event_ctx, partner,
					   timeval_add(&t, partner->pull.retry_interval, 0),
					   wreplsrv_pull_handler_te, partner);
	if (!partner->pull.te) {
		DEBUG(0,("wreplsrv_pull_handler_te: event_add_timed() failed! no memory!\n"));
	}
}

NTSTATUS wreplsrv_sched_inform_action(struct wreplsrv_partner *partner, struct wrepl_table *inform_in)
{
	if (partner->pull.creq) {
		/* there's already a pull in progress, so we're done */
		return NT_STATUS_OK;
	}

	/* remove the scheduled pull */
	talloc_free(partner->pull.te);
	partner->pull.te = NULL;

	partner->pull.cycle_io = talloc(partner, struct wreplsrv_pull_cycle_io);
	if (!partner->pull.cycle_io) {
		goto requeue;
	}

	partner->pull.cycle_io->in.partner	= partner;
	partner->pull.cycle_io->in.num_owners	= inform_in->partner_count;
	partner->pull.cycle_io->in.owners	= inform_in->partners;
	talloc_steal(partner->pull.cycle_io, inform_in->partners);
	partner->pull.cycle_io->in.wreplconn	= NULL;
	partner->pull.creq = wreplsrv_pull_cycle_send(partner->pull.cycle_io, partner->pull.cycle_io);
	if (!partner->pull.creq) {
		DEBUG(1,("wreplsrv_pull_cycle_send(%s) failed\n",
			 partner->address));
		goto requeue;
	}

	partner->pull.creq->async.fn		= wreplsrv_pull_handler_creq;
	partner->pull.creq->async.private_data	= partner;

	return NT_STATUS_OK;
requeue:
	talloc_free(partner->pull.cycle_io);
	partner->pull.cycle_io = NULL;
	/* retry later */
	partner->pull.te = event_add_timed(partner->service->task->event_ctx, partner,
					   timeval_current_ofs(partner->pull.retry_interval, 0),
					   wreplsrv_pull_handler_te, partner);
	if (!partner->pull.te) {
		DEBUG(0,("wreplsrv_pull_handler_te: event_add_timed() failed! no memory!\n"));
	}

	return NT_STATUS_OK;
}

NTSTATUS wreplsrv_setup_out_connections(struct wreplsrv_service *service)
{
	struct wreplsrv_partner *cur;

	for (cur = service->partners; cur; cur = cur->next) {
		if ((cur->type & WINSREPL_PARTNER_PULL) && cur->pull.interval) {
			cur->pull.te = event_add_timed(service->task->event_ctx, cur,
						       timeval_zero(), wreplsrv_pull_handler_te, cur);
			NT_STATUS_HAVE_NO_MEMORY(cur->pull.te);
		}
	}

	return NT_STATUS_OK;
}