Fix debug level initialization for net.c
[Samba/gebeck_regimport.git] / source3 / nmbd / nmbd_elections.c
blobacff7a72e80706ad9c048634c0f64956bf3e0fdd
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-1998
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.
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.
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.
24 #include "includes.h"
26 extern pstring global_myname;
27 extern fstring global_myworkgroup;
29 /* Election parameters. */
30 extern time_t StartupTime;
32 /****************************************************************************
33 Send an election datagram packet.
34 **************************************************************************/
35 static void send_election_dgram(struct subnet_record *subrec, char *workgroup_name,
36 uint32 criterion, int timeup,char *server_name)
38 pstring outbuf;
39 char *p;
41 DEBUG(2,("send_election_dgram: Sending election packet for workgroup %s on subnet %s\n",
42 workgroup_name, subrec->subnet_name ));
44 memset(outbuf,'\0',sizeof(outbuf));
45 p = outbuf;
46 SCVAL(p,0,ANN_Election); /* Election opcode. */
47 p++;
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 pstrcpy(p,server_name);
54 strupper(p);
55 p = skip_string(p,1);
57 send_mailslot(False, BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
58 global_myname, 0,
59 workgroup_name, 0x1e,
60 subrec->bcast_ip, subrec->myip, DGRAM_PORT);
63 /*******************************************************************
64 We found a current master browser on one of our broadcast interfaces.
65 ******************************************************************/
67 static void check_for_master_browser_success(struct subnet_record *subrec,
68 struct userdata_struct *userdata,
69 struct nmb_name *answer_name,
70 struct in_addr answer_ip, struct res_rec *rrec)
72 DEBUG(3,("check_for_master_browser_success: Local master browser for workgroup %s exists at \
73 IP %s (just checking).\n", answer_name->name, inet_ntoa(answer_ip) ));
76 /*******************************************************************
77 We failed to find a current master browser on one of our broadcast interfaces.
78 ******************************************************************/
80 static void check_for_master_browser_fail( struct subnet_record *subrec,
81 struct response_record *rrec,
82 struct nmb_name *question_name,
83 int fail_code)
85 char *workgroup_name = question_name->name;
86 struct work_record *work = find_workgroup_on_subnet(subrec, workgroup_name);
88 if(work == NULL)
90 DEBUG(0,("check_for_master_browser_fail: Unable to find workgroup %s on subnet %s.=\n",
91 workgroup_name, subrec->subnet_name ));
92 return;
95 if (strequal(work->work_group, global_myworkgroup))
98 if (lp_local_master())
100 /* We have discovered that there is no local master
101 browser, and we are configured to initiate
102 an election that we will participate in.
104 DEBUG(2,("check_for_master_browser_fail: Forcing election on workgroup %s subnet %s\n",
105 work->work_group, subrec->subnet_name ));
107 /* Setting this means we will participate when the
108 election is run in run_elections(). */
109 work->needelection = True;
111 else
113 /* We need to force an election, because we are configured
114 not to become the local master, but we still need one,
115 having detected that one doesn't exist.
117 send_election_dgram(subrec, work->work_group, 0, 0, "");
122 /*******************************************************************
123 Ensure there is a local master browser for a workgroup on our
124 broadcast interfaces.
125 ******************************************************************/
127 void check_master_browser_exists(time_t t)
129 static time_t lastrun=0;
130 struct subnet_record *subrec;
131 char *workgroup_name = global_myworkgroup;
133 if (!lastrun)
134 lastrun = t;
136 if (t < (lastrun + (CHECK_TIME_MST_BROWSE * 60)))
137 return;
139 lastrun = t;
141 dump_workgroups(False);
143 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
145 struct work_record *work;
147 for (work = subrec->workgrouplist; work; work = work->next)
149 if (strequal(work->work_group, workgroup_name) && !AM_LOCAL_MASTER_BROWSER(work))
151 /* Do a name query for the local master browser on this net. */
152 query_name( subrec, work->work_group, 0x1d,
153 check_for_master_browser_success,
154 check_for_master_browser_fail,
155 NULL);
161 /*******************************************************************
162 Run an election.
163 ******************************************************************/
165 void run_elections(time_t t)
167 static time_t lastime = 0;
169 struct subnet_record *subrec;
171 /* Send election packets once every 2 seconds - note */
172 if (lastime && (t - lastime < 2))
173 return;
175 lastime = t;
177 START_PROFILE(run_elections);
178 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
180 struct work_record *work;
182 for (work = subrec->workgrouplist; work; work = work->next)
184 if (work->RunningElection)
187 * We can only run an election for a workgroup if we have
188 * registered the WORKGROUP<1e> name, as that's the name
189 * we must listen to.
191 struct nmb_name nmbname;
193 make_nmb_name(&nmbname, work->work_group, 0x1e);
194 if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
195 DEBUG(8,("run_elections: Cannot send election packet yet as name %s not \
196 yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
197 continue;
200 send_election_dgram(subrec, work->work_group, work->ElectionCriterion,
201 t - StartupTime, global_myname);
203 if (work->ElectionCount++ >= 4)
205 /* Won election (4 packets were sent out uncontested. */
206 DEBUG(2,("run_elections: >>> Won election for workgroup %s on subnet %s <<<\n",
207 work->work_group, subrec->subnet_name ));
209 work->RunningElection = False;
211 become_local_master_browser(subrec, work);
216 END_PROFILE(run_elections);
219 /*******************************************************************
220 Determine if I win an election.
221 ******************************************************************/
223 static BOOL win_election(struct work_record *work, int version,
224 uint32 criterion, int timeup, char *server_name)
226 int mytimeup = time(NULL) - StartupTime;
227 uint32 mycriterion = work->ElectionCriterion;
229 /* If local master is false then never win
230 in election broadcasts. */
231 if(!lp_local_master())
233 DEBUG(3,("win_election: Losing election as local master == False\n"));
234 return False;
237 DEBUG(4,("win_election: election comparison: %x:%x %x:%x %d:%d %s:%s\n",
238 version, ELECTION_VERSION,
239 criterion, mycriterion,
240 timeup, mytimeup,
241 server_name, global_myname));
243 if (version > ELECTION_VERSION)
244 return(False);
245 if (version < ELECTION_VERSION)
246 return(True);
248 if (criterion > mycriterion)
249 return(False);
250 if (criterion < mycriterion)
251 return(True);
253 if (timeup > mytimeup)
254 return(False);
255 if (timeup < mytimeup)
256 return(True);
258 if (strcasecmp(global_myname, server_name) > 0)
259 return(False);
261 return(True);
264 /*******************************************************************
265 Process an incoming election datagram packet.
266 ******************************************************************/
268 void process_election(struct subnet_record *subrec, struct packet_struct *p, char *buf)
270 struct dgram_packet *dgram = &p->packet.dgram;
271 int version = CVAL(buf,0);
272 uint32 criterion = IVAL(buf,1);
273 int timeup = IVAL(buf,5)/1000;
274 char *server_name = buf+13;
275 struct work_record *work;
276 char *workgroup_name = dgram->dest_name.name;
278 START_PROFILE(election);
279 server_name[15] = 0;
281 DEBUG(3,("process_election: Election request from %s at IP %s on subnet %s for workgroup %s.\n",
282 server_name,inet_ntoa(p->ip), subrec->subnet_name, workgroup_name ));
284 DEBUG(5,("process_election: vers=%d criterion=%08x timeup=%d\n", version,criterion,timeup));
286 if(( work = find_workgroup_on_subnet(subrec, workgroup_name)) == NULL)
288 DEBUG(0,("process_election: Cannot find workgroup %s on subnet %s.\n",
289 workgroup_name, subrec->subnet_name ));
290 goto done;
293 if (!strequal(work->work_group, global_myworkgroup))
295 DEBUG(3,("process_election: ignoring election request for workgroup %s on subnet %s as this \
296 is not my workgroup.\n", work->work_group, subrec->subnet_name ));
297 goto done;
300 if (win_election(work, version,criterion,timeup,server_name))
302 /* We take precedence over the requesting server. */
303 if (!work->RunningElection)
305 /* We weren't running an election - start running one. */
307 work->needelection = True;
308 work->ElectionCount=0;
311 /* Note that if we were running an election for this workgroup on this
312 subnet already, we just ignore the server we take precedence over. */
314 else
316 /* We lost. Stop participating. */
317 work->needelection = False;
319 if (work->RunningElection || AM_LOCAL_MASTER_BROWSER(work))
321 work->RunningElection = False;
322 DEBUG(3,("process_election: >>> Lost election for workgroup %s on subnet %s <<<\n",
323 work->work_group, subrec->subnet_name ));
324 if (AM_LOCAL_MASTER_BROWSER(work))
325 unbecome_local_master_browser(subrec, work, False);
328 done:
329 END_PROFILE(election);
332 /****************************************************************************
333 This function looks over all the workgroups known on all the broadcast
334 subnets and decides if a browser election is to be run on that workgroup.
335 It returns True if any election packets need to be sent (this will then
336 be done by run_elections().
337 ***************************************************************************/
339 BOOL check_elections(void)
341 struct subnet_record *subrec;
342 BOOL run_any_election = False;
344 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
346 struct work_record *work;
347 for (work = subrec->workgrouplist; work; work = work->next)
349 run_any_election |= work->RunningElection;
352 * Start an election if we have any chance of winning.
353 * Note this is a change to the previous code, that would
354 * only run an election if nmbd was in the potential browser
355 * state. We need to run elections in any state if we're told
356 * to. JRA.
359 if (work->needelection && !work->RunningElection && lp_local_master())
362 * We can only run an election for a workgroup if we have
363 * registered the WORKGROUP<1e> name, as that's the name
364 * we must listen to.
366 struct nmb_name nmbname;
368 make_nmb_name(&nmbname, work->work_group, 0x1e);
369 if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
370 DEBUG(8,("check_elections: Cannot send election packet yet as name %s not \
371 yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
372 continue;
375 DEBUG(3,("check_elections: >>> Starting election for workgroup %s on subnet %s <<<\n",
376 work->work_group, subrec->subnet_name ));
378 work->ElectionCount = 0;
379 work->RunningElection = True;
380 work->needelection = False;
384 return run_any_election;
389 /****************************************************************************
390 process a internal Samba message forcing an election
391 ***************************************************************************/
392 void nmbd_message_election(int msg_type, pid_t src, void *buf, size_t len)
394 struct subnet_record *subrec;
396 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
397 struct work_record *work;
398 for (work = subrec->workgrouplist; work; work = work->next) {
399 if (strequal(work->work_group, global_myworkgroup)) {
400 work->needelection = True;
401 work->ElectionCount=0;
402 work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;