r12674: make use of the winsdb functions
[kai/samba-autobuild/.git] / source4 / wrepl_server / wrepl_out_push.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    WINS Replication server
5    
6    Copyright (C) Stefan Metzmacher      2005
7    
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 2 of the License, or
11    (at your option) any later version.
12    
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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/ndr_winsrepl.h"
25 #include "wrepl_server/wrepl_server.h"
26 #include "libcli/composite/composite.h"
27 #include "wrepl_server/wrepl_out_helpers.h"
28 #include "nbt_server/wins/winsdb.h"
29
30 static void wreplsrv_out_partner_push(struct wreplsrv_partner *partner, BOOL propagate);
31
32 static void wreplsrv_push_handler_creq(struct composite_context *creq)
33 {
34         struct wreplsrv_partner *partner = talloc_get_type(creq->async.private_data, struct wreplsrv_partner);
35         struct wreplsrv_push_notify_io *old_notify_io;
36
37         partner->push.last_status = wreplsrv_push_notify_recv(partner->push.creq);
38         partner->push.creq = NULL;
39
40         old_notify_io = partner->push.notify_io;
41         partner->push.notify_io = NULL;
42
43         if (NT_STATUS_IS_OK(partner->push.last_status)) {
44                 partner->push.error_count = 0;
45                 DEBUG(2,("wreplsrv_push_notify(%s): %s\n",
46                          partner->address, nt_errstr(partner->push.last_status)));
47                 goto done;
48         }
49
50         partner->push.error_count++;
51
52         if (partner->push.error_count > 1) {
53                 DEBUG(1,("wreplsrv_push_notify(%s): %s: error_count: %u: giving up\n",
54                          partner->address, nt_errstr(partner->push.last_status),
55                          partner->push.error_count));
56                 goto done;
57         }
58
59         DEBUG(1,("wreplsrv_push_notify(%s): %s: error_count: %u: retry\n",
60                  partner->address, nt_errstr(partner->push.last_status),
61                  partner->push.error_count));
62         wreplsrv_out_partner_push(partner, old_notify_io->in.propagate);
63 done:
64         talloc_free(old_notify_io);
65 }
66
67 static void wreplsrv_out_partner_push(struct wreplsrv_partner *partner, BOOL propagate)
68 {
69         /* a push for this partner is currently in progress, so we're done */
70         if (partner->push.creq) return;
71
72         /* now prepare the push notify */
73         partner->push.notify_io = talloc(partner, struct wreplsrv_push_notify_io);
74         if (!partner->push.notify_io) {
75                 goto nomem;
76         }
77
78         partner->push.notify_io->in.partner     = partner;
79         partner->push.notify_io->in.inform      = partner->push.use_inform;
80         partner->push.notify_io->in.propagate   = propagate;
81         partner->push.creq = wreplsrv_push_notify_send(partner->push.notify_io, partner->push.notify_io);
82         if (!partner->push.creq) {
83                 DEBUG(1,("wreplsrv_push_notify_send(%s) failed nomem?\n",
84                          partner->address));
85                 goto nomem;
86         }
87
88         partner->push.creq->async.fn            = wreplsrv_push_handler_creq;
89         partner->push.creq->async.private_data  = partner;
90
91         return;
92 nomem:
93         talloc_free(partner->push.notify_io);
94         partner->push.notify_io = NULL;
95         DEBUG(1,("wreplsrv_out_partner_push(%s,%u) failed nomem? (ignoring)\n",
96                  partner->address, propagate));
97         return;
98 }
99
100 static uint32_t wreplsrv_calc_change_count(struct wreplsrv_partner *partner, uint64_t seqnumber)
101 {
102         uint64_t tmp_diff = UINT32_MAX;
103
104         /* catch an overflow */
105         if (partner->push.seqnumber > seqnumber) {
106                 goto done;
107         }
108
109         tmp_diff = seqnumber - partner->push.seqnumber;
110
111         if (tmp_diff > UINT32_MAX) {
112                 tmp_diff = UINT32_MAX;
113                 goto done;
114         }
115
116 done:
117         partner->push.seqnumber = seqnumber;
118         return (uint32_t)(tmp_diff & UINT32_MAX);
119 }
120
121 NTSTATUS wreplsrv_out_push_run(struct wreplsrv_service *service)
122 {
123         struct wreplsrv_partner *partner;
124         uint64_t seqnumber;
125         uint32_t change_count;
126
127         seqnumber = winsdb_get_seqnumber(service->wins_db);
128
129         for (partner = service->partners; partner; partner = partner->next) {
130                 /* if it's not a push partner, go to the next partner */
131                 if (!(partner->type & WINSREPL_PARTNER_PUSH)) continue;
132
133                 /* if push notifies are disabled for this partner, go to the next partner */
134                 if (partner->push.change_count == 0) continue;
135
136                 /* get the actual change count for the partner */
137                 change_count = wreplsrv_calc_change_count(partner, seqnumber);
138
139                 /* if the configured change count isn't reached, go to the next partner */
140                 if (change_count < partner->push.change_count) continue;
141
142                 wreplsrv_out_partner_push(partner, False);
143         }
144
145         return NT_STATUS_OK;
146 }