r12388: fix debug messages
[samba.git] / source / 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 "dlinklist.h"
25 #include "lib/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "smbd/service_task.h"
28 #include "smbd/service_stream.h"
29 #include "lib/messaging/irpc.h"
30 #include "librpc/gen_ndr/ndr_winsrepl.h"
31 #include "wrepl_server/wrepl_server.h"
32 #include "nbt_server/wins/winsdb.h"
33 #include "ldb/include/ldb.h"
34 #include "libcli/composite/composite.h"
35 #include "libcli/wrepl/winsrepl.h"
36 #include "wrepl_server/wrepl_out_helpers.h"
37
38 static void wreplsrv_out_partner_push(struct wreplsrv_partner *partner, BOOL propagate);
39
40 static void wreplsrv_push_handler_creq(struct composite_context *creq)
41 {
42         struct wreplsrv_partner *partner = talloc_get_type(creq->async.private_data, struct wreplsrv_partner);
43         struct wreplsrv_push_notify_io *old_notify_io;
44
45         partner->push.last_status = wreplsrv_push_notify_recv(partner->push.creq);
46         partner->push.creq = NULL;
47
48         old_notify_io = partner->push.notify_io;
49         partner->push.notify_io = NULL;
50
51         if (NT_STATUS_IS_OK(partner->push.last_status)) {
52                 partner->push.error_count = 0;
53                 DEBUG(2,("wreplsrv_push_notify(%s): %s\n",
54                          partner->address, nt_errstr(partner->push.last_status)));
55                 goto done;
56         }
57
58         partner->push.error_count++;
59
60         if (partner->push.error_count > 1) {
61                 DEBUG(1,("wreplsrv_push_notify(%s): %s: error_count: %u: giving up\n",
62                          partner->address, nt_errstr(partner->push.last_status),
63                          partner->push.error_count));
64                 goto done;
65         }
66
67         DEBUG(1,("wreplsrv_push_notify(%s): %s: error_count: %u: retry\n",
68                  partner->address, nt_errstr(partner->push.last_status),
69                  partner->push.error_count));
70         wreplsrv_out_partner_push(partner, old_notify_io->in.propagate);
71 done:
72         talloc_free(old_notify_io);
73 }
74
75 static void wreplsrv_out_partner_push(struct wreplsrv_partner *partner, BOOL propagate)
76 {
77         /* a push for this partner is currently in progress, so we're done */
78         if (partner->push.creq) return;
79
80         /* now prepare the push notify */
81         partner->push.notify_io = talloc(partner, struct wreplsrv_push_notify_io);
82         if (!partner->push.notify_io) {
83                 goto nomem;
84         }
85
86         partner->push.notify_io->in.partner     = partner;
87         partner->push.notify_io->in.inform      = partner->push.use_inform;
88         partner->push.notify_io->in.propagate   = propagate;
89         partner->push.creq = wreplsrv_push_notify_send(partner->push.notify_io, partner->push.notify_io);
90         if (!partner->push.creq) {
91                 DEBUG(1,("wreplsrv_push_notify_send(%s) failed nomem?\n",
92                          partner->address));
93                 goto nomem;
94         }
95
96         partner->push.creq->async.fn            = wreplsrv_push_handler_creq;
97         partner->push.creq->async.private_data  = partner;
98
99         return;
100 nomem:
101         talloc_free(partner->push.notify_io);
102         partner->push.notify_io = NULL;
103         DEBUG(1,("wreplsrv_out_partner_push(%s,%u) failed nomem? (ignoring)\n",
104                  partner->address, propagate));
105         return;
106 }
107
108 static uint32_t wreplsrv_calc_change_count(struct wreplsrv_partner *partner)
109 {
110         /* TODO: add a real implementation here */
111         return (uint32_t)-1;
112 }
113
114 NTSTATUS wreplsrv_out_push_run(struct wreplsrv_service *service)
115 {
116         struct wreplsrv_partner *partner;
117         uint32_t change_count;
118
119         for (partner = service->partners; partner; partner = partner->next) {
120                 /* if it's not a push partner, go to the next partner */
121                 if (!(partner->type & WINSREPL_PARTNER_PUSH)) continue;
122
123                 /* if push notifies are disabled for this partner, go to the next partner */
124                 if (partner->push.change_count == 0) continue;
125
126                 /* get the actual change count for the partner */
127                 change_count = wreplsrv_calc_change_count(partner);
128
129                 /* if the configured change count isn't reached, go to the next partner */
130                 if (change_count < partner->push.change_count) continue;
131
132                 wreplsrv_out_partner_push(partner, False);
133         }
134
135         return NT_STATUS_OK;
136 }