r12309: fix a crash bug, which appens in an error case
[jra/samba/.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 "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         partner->push.last_run = timeval_current();
48
49         old_notify_io = partner->push.notify_io;
50         partner->push.notify_io = NULL;
51
52         if (NT_STATUS_IS_OK(partner->push.last_status)) {
53                 partner->push.error_count = 0;
54                 DEBUG(2,("wreplsrv_push_notify(%s): %s\n",
55                          partner->address, nt_errstr(partner->push.last_status)));
56                 goto done;
57         }
58
59         partner->push.error_count++;
60
61         if (partner->push.error_count > 1) {
62                 DEBUG(1,("wreplsrv_push_notify(%s): %s: error_count: %u: giving up\n",
63                          partner->address, nt_errstr(partner->push.last_status),
64                          partner->push.error_count));
65                 goto done;
66         }
67
68         DEBUG(1,("wreplsrv_push_notify(%s): %s: error_count: %u: retry\n",
69                  partner->address, nt_errstr(partner->push.last_status),
70                  partner->push.error_count));
71         wreplsrv_out_partner_push(partner, old_notify_io->in.propagate);
72 done:
73         talloc_free(old_notify_io);
74 }
75
76 static void wreplsrv_out_partner_push(struct wreplsrv_partner *partner, BOOL propagate)
77 {
78         /* a push for this partner is currently in progress, so we're done */
79         if (partner->push.creq) return;
80
81         /* now prepare the push notify */
82         partner->push.notify_io = talloc(partner, struct wreplsrv_push_notify_io);
83         if (!partner->push.notify_io) {
84                 goto nomem;
85         }
86
87         partner->push.notify_io->in.partner     = partner;
88         partner->push.notify_io->in.inform      = partner->push.use_inform;
89         partner->push.notify_io->in.propagate   = propagate;
90         partner->push.creq = wreplsrv_push_notify_send(partner->push.notify_io, partner->push.notify_io);
91         if (!partner->push.creq) {
92                 DEBUG(1,("wreplsrv_push_notify_send(%s) failed\n",
93                          partner->address));
94                 goto nomem;
95         }
96
97         partner->push.creq->async.fn            = wreplsrv_push_handler_creq;
98         partner->push.creq->async.private_data  = partner;
99
100         return;
101 nomem:
102         talloc_free(partner->push.notify_io);
103         partner->push.notify_io = NULL;
104         DEBUG(1,("wreplsrv_push_notify_send(%s) failed nomem? (ignoring)\n",
105                  partner->address));
106         return;
107 }
108
109 static uint32_t wreplsrv_calc_change_count(struct wreplsrv_partner *partner)
110 {
111         /* TODO: add a real implementation here */
112         return (uint32_t)-1;
113 }
114
115 uint32_t wreplsrv_out_push_run(struct wreplsrv_service *service, uint32_t next_interval)
116 {
117         struct wreplsrv_partner *partner;
118         uint32_t change_count;
119
120         for (partner = service->partners; partner; partner = partner->next) {
121                 /* if it's not a push partner, go to the next partner */
122                 if (!(partner->type & WINSREPL_PARTNER_PUSH)) continue;
123
124                 /* if push notifies are disabled for this partner, go to the next partner */
125                 if (partner->push.change_count == 0) continue;
126
127                 /* get the actual change count for the partner */
128                 change_count = wreplsrv_calc_change_count(partner);
129
130                 /* if the configured change count isn't reached, go to the next partner */
131                 if (change_count < partner->push.change_count) continue;
132
133                 wreplsrv_out_partner_push(partner, False);
134         }
135
136         return next_interval;
137 }