summaryrefslogtreecommitdiffstats
path: root/source4/wrepl_server/wrepl_out_push.c
blob: 9c8c9fad8d5e5706496ee1fc4e1e8b4997a6b119 (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
/* 
   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 "librpc/gen_ndr/ndr_winsrepl.h"
#include "wrepl_server/wrepl_server.h"
#include "libcli/composite/composite.h"
#include "nbt_server/wins/winsdb.h"

static void wreplsrv_out_partner_push(struct wreplsrv_partner *partner, BOOL propagate);

static void wreplsrv_push_handler_creq(struct composite_context *creq)
{
	struct wreplsrv_partner *partner = talloc_get_type(creq->async.private_data, struct wreplsrv_partner);
	struct wreplsrv_push_notify_io *old_notify_io;

	partner->push.last_status = wreplsrv_push_notify_recv(partner->push.creq);
	partner->push.creq = NULL;

	old_notify_io = partner->push.notify_io;
	partner->push.notify_io = NULL;

	if (NT_STATUS_IS_OK(partner->push.last_status)) {
		partner->push.error_count = 0;
		DEBUG(2,("wreplsrv_push_notify(%s): %s\n",
			 partner->address, nt_errstr(partner->push.last_status)));
		goto done;
	}

	partner->push.error_count++;

	if (partner->push.error_count > 1) {
		DEBUG(1,("wreplsrv_push_notify(%s): %s: error_count: %u: giving up\n",
			 partner->address, nt_errstr(partner->push.last_status),
			 partner->push.error_count));
		goto done;
	}

	DEBUG(1,("wreplsrv_push_notify(%s): %s: error_count: %u: retry\n",
		 partner->address, nt_errstr(partner->push.last_status),
		 partner->push.error_count));
	wreplsrv_out_partner_push(partner, old_notify_io->in.propagate);
done:
	talloc_free(old_notify_io);
}

static void wreplsrv_out_partner_push(struct wreplsrv_partner *partner, BOOL propagate)
{
	/* a push for this partner is currently in progress, so we're done */
	if (partner->push.creq) return;

	/* now prepare the push notify */
	partner->push.notify_io = talloc(partner, struct wreplsrv_push_notify_io);
	if (!partner->push.notify_io) {
		goto nomem;
	}

	partner->push.notify_io->in.partner	= partner;
	partner->push.notify_io->in.inform	= partner->push.use_inform;
	partner->push.notify_io->in.propagate	= propagate;
	partner->push.creq = wreplsrv_push_notify_send(partner->push.notify_io, partner->push.notify_io);
	if (!partner->push.creq) {
		DEBUG(1,("wreplsrv_push_notify_send(%s) failed nomem?\n",
			 partner->address));
		goto nomem;
	}

	partner->push.creq->async.fn		= wreplsrv_push_handler_creq;
	partner->push.creq->async.private_data	= partner;

	return;
nomem:
	talloc_free(partner->push.notify_io);
	partner->push.notify_io = NULL;
	DEBUG(1,("wreplsrv_out_partner_push(%s,%u) failed nomem? (ignoring)\n",
		 partner->address, propagate));
	return;
}

static uint32_t wreplsrv_calc_change_count(struct wreplsrv_partner *partner, uint64_t maxVersionID)
{
	uint64_t tmp_diff = UINT32_MAX;

	/* catch an overflow */
	if (partner->push.maxVersionID > maxVersionID) {
		goto done;
	}

	tmp_diff = maxVersionID - partner->push.maxVersionID;

	if (tmp_diff > UINT32_MAX) {
		tmp_diff = UINT32_MAX;
		goto done;
	}

done:
	partner->push.maxVersionID = maxVersionID;
	return (uint32_t)(tmp_diff & UINT32_MAX);
}

NTSTATUS wreplsrv_out_push_run(struct wreplsrv_service *service)
{
	struct wreplsrv_partner *partner;
	uint64_t seqnumber;
	uint32_t change_count;

	seqnumber = winsdb_get_maxVersion(service->wins_db);

	for (partner = service->partners; partner; partner = partner->next) {
		/* if it's not a push partner, go to the next partner */
		if (!(partner->type & WINSREPL_PARTNER_PUSH)) continue;

		/* if push notifies are disabled for this partner, go to the next partner */
		if (partner->push.change_count == 0) continue;

		/* get the actual change count for the partner */
		change_count = wreplsrv_calc_change_count(partner, seqnumber);

		/* if the configured change count isn't reached, go to the next partner */
		if (change_count < partner->push.change_count) continue;

		wreplsrv_out_partner_push(partner, False);
	}

	return NT_STATUS_OK;
}