source: trunk/server/source3/nmbd/nmbd_elections.c

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 13.4 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 NBT netbios routines and daemon - version 2
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6 Copyright (C) Jeremy Allison 1994-2003
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 3 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, see <http://www.gnu.org/licenses/>.
20
21*/
22
23#include "includes.h"
24#include "nmbd/nmbd.h"
25#include "smbprofile.h"
26
27/* Election parameters. */
28extern time_t StartupTime;
29
30/****************************************************************************
31 Send an election datagram packet.
32**************************************************************************/
33
34static void send_election_dgram(struct subnet_record *subrec, const char *workgroup_name,
35 uint32 criterion, int timeup,const char *server_name)
36{
37 char outbuf[1024];
38 unstring srv_name;
39 char *p;
40
41 DEBUG(2,("send_election_dgram: Sending election packet for workgroup %s on subnet %s\n",
42 workgroup_name, subrec->subnet_name ));
43
44 memset(outbuf,'\0',sizeof(outbuf));
45 p = outbuf;
46 SCVAL(p,0,ANN_Election); /* Election opcode. */
47 p++;
48
49 SCVAL(p,0,((criterion == 0 && timeup == 0) ? 0 : ELECTION_VERSION));
50 SIVAL(p,1,criterion);
51 SIVAL(p,5,timeup*1000); /* ms - Despite what the spec says. */
52 p += 13;
53 unstrcpy(srv_name, server_name);
54 strupper_m(srv_name);
55 /* The following call does UNIX -> DOS charset conversion. */
56 push_ascii(p, srv_name, sizeof(outbuf)-PTR_DIFF(p,outbuf)-1, STR_TERMINATE);
57 p = skip_string(outbuf,sizeof(outbuf),p);
58
59 send_mailslot(False, BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
60 global_myname(), 0,
61 workgroup_name, 0x1e,
62 subrec->bcast_ip, subrec->myip, DGRAM_PORT);
63}
64
65/*******************************************************************
66 We found a current master browser on one of our broadcast interfaces.
67******************************************************************/
68
69static void check_for_master_browser_success(struct subnet_record *subrec,
70 struct userdata_struct *userdata,
71 struct nmb_name *answer_name,
72 struct in_addr answer_ip, struct res_rec *rrec)
73{
74 unstring aname;
75 pull_ascii_nstring(aname, sizeof(aname), answer_name->name);
76 DEBUG(3,("check_for_master_browser_success: Local master browser for workgroup %s exists at \
77IP %s (just checking).\n", aname, inet_ntoa(answer_ip) ));
78}
79
80/*******************************************************************
81 We failed to find a current master browser on one of our broadcast interfaces.
82******************************************************************/
83
84static void check_for_master_browser_fail( struct subnet_record *subrec,
85 struct response_record *rrec,
86 struct nmb_name *question_name,
87 int fail_code)
88{
89 unstring workgroup_name;
90 struct work_record *work;
91
92 pull_ascii_nstring(workgroup_name,sizeof(workgroup_name),question_name->name);
93
94 work = find_workgroup_on_subnet(subrec, workgroup_name);
95 if(work == NULL) {
96 DEBUG(0,("check_for_master_browser_fail: Unable to find workgroup %s on subnet %s.=\n",
97 workgroup_name, subrec->subnet_name ));
98 return;
99 }
100
101 if (strequal(work->work_group, lp_workgroup())) {
102
103 if (lp_local_master()) {
104 /* We have discovered that there is no local master
105 browser, and we are configured to initiate
106 an election that we will participate in.
107 */
108 DEBUG(2,("check_for_master_browser_fail: Forcing election on workgroup %s subnet %s\n",
109 work->work_group, subrec->subnet_name ));
110
111 /* Setting this means we will participate when the
112 election is run in run_elections(). */
113 work->needelection = True;
114 } else {
115 /* We need to force an election, because we are configured
116 not to become the local master, but we still need one,
117 having detected that one doesn't exist.
118 */
119 send_election_dgram(subrec, work->work_group, 0, 0, "");
120 }
121 }
122}
123
124/*******************************************************************
125 Ensure there is a local master browser for a workgroup on our
126 broadcast interfaces.
127******************************************************************/
128
129void check_master_browser_exists(time_t t)
130{
131 static time_t lastrun=0;
132 struct subnet_record *subrec;
133 const char *workgroup_name = lp_workgroup();
134
135 if (t < (lastrun + (CHECK_TIME_MST_BROWSE * 60)))
136 return;
137
138 lastrun = t;
139
140 dump_workgroups(False);
141
142 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
143 struct work_record *work;
144
145 for (work = subrec->workgrouplist; work; work = work->next) {
146 if (strequal(work->work_group, workgroup_name) && !AM_LOCAL_MASTER_BROWSER(work)) {
147 /* Do a name query for the local master browser on this net. */
148 query_name( subrec, work->work_group, 0x1d,
149 check_for_master_browser_success,
150 check_for_master_browser_fail,
151 NULL);
152 }
153 }
154 }
155}
156
157/*******************************************************************
158 Run an election.
159******************************************************************/
160
161void run_elections(time_t t)
162{
163 static time_t lastime = 0;
164
165 struct subnet_record *subrec;
166
167 START_PROFILE(run_elections);
168
169 /* Send election packets once every 2 seconds - note */
170 if (lastime && (t - lastime < 2)) {
171 END_PROFILE(run_elections);
172 return;
173 }
174
175 lastime = t;
176
177 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
178 struct work_record *work;
179
180 for (work = subrec->workgrouplist; work; work = work->next) {
181 if (work->RunningElection) {
182 /*
183 * We can only run an election for a workgroup if we have
184 * registered the WORKGROUP<1e> name, as that's the name
185 * we must listen to.
186 */
187 struct nmb_name nmbname;
188
189 make_nmb_name(&nmbname, work->work_group, 0x1e);
190 if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
191 DEBUG(8,("run_elections: Cannot send election packet yet as name %s not \
192yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
193 continue;
194 }
195
196 send_election_dgram(subrec, work->work_group, work->ElectionCriterion,
197 t - StartupTime, global_myname());
198
199 if (work->ElectionCount++ >= 4) {
200 /* Won election (4 packets were sent out uncontested. */
201 DEBUG(2,("run_elections: >>> Won election for workgroup %s on subnet %s <<<\n",
202 work->work_group, subrec->subnet_name ));
203
204 work->RunningElection = False;
205
206 become_local_master_browser(subrec, work);
207 }
208 }
209 }
210 }
211 END_PROFILE(run_elections);
212}
213
214/*******************************************************************
215 Determine if I win an election.
216******************************************************************/
217
218static bool win_election(struct work_record *work, int version,
219 uint32 criterion, int timeup, const char *server_name)
220{
221 int mytimeup = time(NULL) - StartupTime;
222 uint32 mycriterion = work->ElectionCriterion;
223
224 /* If local master is false then never win in election broadcasts. */
225 if(!lp_local_master()) {
226 DEBUG(3,("win_election: Losing election as local master == False\n"));
227 return False;
228 }
229
230 DEBUG(4,("win_election: election comparison: %x:%x %x:%x %d:%d %s:%s\n",
231 version, ELECTION_VERSION,
232 criterion, mycriterion,
233 timeup, mytimeup,
234 server_name, global_myname()));
235
236 if (version > ELECTION_VERSION)
237 return(False);
238 if (version < ELECTION_VERSION)
239 return(True);
240
241 if (criterion > mycriterion)
242 return(False);
243 if (criterion < mycriterion)
244 return(True);
245
246 if (timeup > mytimeup)
247 return(False);
248 if (timeup < mytimeup)
249 return(True);
250
251 if (StrCaseCmp(global_myname(), server_name) > 0)
252 return(False);
253
254 return(True);
255}
256
257/*******************************************************************
258 Process an incoming election datagram packet.
259******************************************************************/
260
261void process_election(struct subnet_record *subrec, struct packet_struct *p, const char *buf)
262{
263 struct dgram_packet *dgram = &p->packet.dgram;
264 int version = CVAL(buf,0);
265 uint32 criterion = IVAL(buf,1);
266 int timeup = IVAL(buf,5)/1000;
267 unstring server_name;
268 struct work_record *work;
269 unstring workgroup_name;
270
271 START_PROFILE(election);
272
273 pull_ascii_nstring(server_name, sizeof(server_name), buf+13);
274 pull_ascii_nstring(workgroup_name, sizeof(workgroup_name), dgram->dest_name.name);
275
276 server_name[15] = 0;
277
278 DEBUG(3,("process_election: Election request from %s at IP %s on subnet %s for workgroup %s.\n",
279 server_name,inet_ntoa(p->ip), subrec->subnet_name, workgroup_name ));
280
281 DEBUG(5,("process_election: vers=%d criterion=%08x timeup=%d\n", version,criterion,timeup));
282
283 if(( work = find_workgroup_on_subnet(subrec, workgroup_name)) == NULL) {
284 DEBUG(0,("process_election: Cannot find workgroup %s on subnet %s.\n",
285 workgroup_name, subrec->subnet_name ));
286 goto done;
287 }
288
289 if (!strequal(work->work_group, lp_workgroup())) {
290 DEBUG(3,("process_election: ignoring election request for workgroup %s on subnet %s as this \
291is not my workgroup.\n", work->work_group, subrec->subnet_name ));
292 goto done;
293 }
294
295 if (win_election(work, version,criterion,timeup,server_name)) {
296 /* We take precedence over the requesting server. */
297 if (!work->RunningElection) {
298 /* We weren't running an election - start running one. */
299
300 work->needelection = True;
301 work->ElectionCount=0;
302 }
303
304 /* Note that if we were running an election for this workgroup on this
305 subnet already, we just ignore the server we take precedence over. */
306 } else {
307 /* We lost. Stop participating. */
308 work->needelection = False;
309
310 if (work->RunningElection || AM_LOCAL_MASTER_BROWSER(work)) {
311 work->RunningElection = False;
312 DEBUG(3,("process_election: >>> Lost election for workgroup %s on subnet %s <<<\n",
313 work->work_group, subrec->subnet_name ));
314 if (AM_LOCAL_MASTER_BROWSER(work))
315 unbecome_local_master_browser(subrec, work, False);
316 }
317 }
318done:
319
320 END_PROFILE(election);
321}
322
323/****************************************************************************
324 This function looks over all the workgroups known on all the broadcast
325 subnets and decides if a browser election is to be run on that workgroup.
326 It returns True if any election packets need to be sent (this will then
327 be done by run_elections().
328***************************************************************************/
329
330bool check_elections(void)
331{
332 struct subnet_record *subrec;
333 bool run_any_election = False;
334
335 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
336 struct work_record *work;
337 for (work = subrec->workgrouplist; work; work = work->next) {
338 if (work->RunningElection) {
339 run_any_election = work->RunningElection;
340 }
341
342 /*
343 * Start an election if we have any chance of winning.
344 * Note this is a change to the previous code, that would
345 * only run an election if nmbd was in the potential browser
346 * state. We need to run elections in any state if we're told
347 * to. JRA.
348 */
349
350 if (work->needelection && !work->RunningElection && lp_local_master()) {
351 /*
352 * We can only run an election for a workgroup if we have
353 * registered the WORKGROUP<1e> name, as that's the name
354 * we must listen to.
355 */
356 struct nmb_name nmbname;
357
358 make_nmb_name(&nmbname, work->work_group, 0x1e);
359 if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
360 DEBUG(8,("check_elections: Cannot send election packet yet as name %s not \
361yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
362 continue;
363 }
364
365 DEBUG(3,("check_elections: >>> Starting election for workgroup %s on subnet %s <<<\n",
366 work->work_group, subrec->subnet_name ));
367
368 work->ElectionCount = 0;
369 work->RunningElection = True;
370 work->needelection = False;
371 }
372 }
373 }
374 return run_any_election;
375}
376
377/****************************************************************************
378 Process a internal Samba message forcing an election.
379***************************************************************************/
380
381void nmbd_message_election(struct messaging_context *msg,
382 void *private_data,
383 uint32_t msg_type,
384 struct server_id server_id,
385 DATA_BLOB *data)
386{
387 struct subnet_record *subrec;
388
389 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
390 struct work_record *work;
391 for (work = subrec->workgrouplist; work; work = work->next) {
392 if (strequal(work->work_group, lp_workgroup())) {
393 work->needelection = True;
394 work->ElectionCount=0;
395 work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;
396 }
397 }
398 }
399}
Note: See TracBrowser for help on using the repository browser.