2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1998
6 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7 Copyright (C) Jeremy Allison 1994-1998
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 extern pstring global_myname;
28 extern fstring global_myworkgroup;
30 /* Election parameters. */
31 extern time_t StartupTime;
33 /****************************************************************************
34 Send an election datagram packet.
35 **************************************************************************/
36 static void send_election_dgram(struct subnet_record *subrec, char *workgroup_name,
37 uint32 criterion, int timeup,char *server_name)
42 DEBUG(2,("send_election_dgram: Sending election packet for workgroup %s on subnet %s\n",
43 workgroup_name, subrec->subnet_name ));
45 memset(outbuf,'\0',sizeof(outbuf));
47 SCVAL(p,0,ANN_Election); /* Election opcode. */
50 SCVAL(p,0,((criterion == 0 && timeup == 0) ? 0 : ELECTION_VERSION));
52 SIVAL(p,5,timeup*1000); /* ms - Despite what the spec says. */
54 pstrcpy(p,server_name);
58 send_mailslot(False, BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
61 subrec->bcast_ip, subrec->myip, DGRAM_PORT);
64 /*******************************************************************
65 We found a current master browser on one of our broadcast interfaces.
66 ******************************************************************/
68 static void check_for_master_browser_success(struct subnet_record *subrec,
69 struct userdata_struct *userdata,
70 struct nmb_name *answer_name,
71 struct in_addr answer_ip, struct res_rec *rrec)
73 DEBUG(3,("check_for_master_browser_success: Local master browser for workgroup %s exists at \
74 IP %s (just checking).\n", answer_name->name, inet_ntoa(answer_ip) ));
77 /*******************************************************************
78 We failed to find a current master browser on one of our broadcast interfaces.
79 ******************************************************************/
81 static void check_for_master_browser_fail( struct subnet_record *subrec,
82 struct response_record *rrec,
83 struct nmb_name *question_name,
86 char *workgroup_name = question_name->name;
87 struct work_record *work = find_workgroup_on_subnet(subrec, workgroup_name);
91 DEBUG(0,("check_for_master_browser_fail: Unable to find workgroup %s on subnet %s.=\n",
92 workgroup_name, subrec->subnet_name ));
96 if (strequal(work->work_group, global_myworkgroup))
99 if (lp_local_master())
101 /* We have discovered that there is no local master
102 browser, and we are configured to initiate
103 an election that we will participate in.
105 DEBUG(2,("check_for_master_browser_fail: Forcing election on workgroup %s subnet %s\n",
106 work->work_group, subrec->subnet_name ));
108 /* Setting this means we will participate when the
109 election is run in run_elections(). */
110 work->needelection = True;
114 /* We need to force an election, because we are configured
115 not to become the local master, but we still need one,
116 having detected that one doesn't exist.
118 send_election_dgram(subrec, work->work_group, 0, 0, "");
123 /*******************************************************************
124 Ensure there is a local master browser for a workgroup on our
125 broadcast interfaces.
126 ******************************************************************/
128 void check_master_browser_exists(time_t t)
130 static time_t lastrun=0;
131 struct subnet_record *subrec;
132 char *workgroup_name = global_myworkgroup;
137 if (t < (lastrun + (CHECK_TIME_MST_BROWSE * 60)))
142 dump_workgroups(False);
144 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
146 struct work_record *work;
148 for (work = subrec->workgrouplist; work; work = work->next)
150 if (strequal(work->work_group, workgroup_name) && !AM_LOCAL_MASTER_BROWSER(work))
152 /* Do a name query for the local master browser on this net. */
153 query_name( subrec, work->work_group, 0x1d,
154 check_for_master_browser_success,
155 check_for_master_browser_fail,
162 /*******************************************************************
164 ******************************************************************/
166 void run_elections(time_t t)
168 static time_t lastime = 0;
170 struct subnet_record *subrec;
172 /* Send election packets once every 2 seconds - note */
173 if (lastime && (t - lastime < 2))
178 START_PROFILE(run_elections);
179 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
181 struct work_record *work;
183 for (work = subrec->workgrouplist; work; work = work->next)
185 if (work->RunningElection)
188 * We can only run an election for a workgroup if we have
189 * registered the WORKGROUP<1e> name, as that's the name
192 struct nmb_name nmbname;
194 make_nmb_name(&nmbname, work->work_group, 0x1e);
195 if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
196 DEBUG(8,("run_elections: Cannot send election packet yet as name %s not \
197 yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
201 send_election_dgram(subrec, work->work_group, work->ElectionCriterion,
202 t - StartupTime, global_myname);
204 if (work->ElectionCount++ >= 4)
206 /* Won election (4 packets were sent out uncontested. */
207 DEBUG(2,("run_elections: >>> Won election for workgroup %s on subnet %s <<<\n",
208 work->work_group, subrec->subnet_name ));
210 work->RunningElection = False;
212 become_local_master_browser(subrec, work);
217 END_PROFILE(run_elections);
220 /*******************************************************************
221 Determine if I win an election.
222 ******************************************************************/
224 static BOOL win_election(struct work_record *work, int version,
225 uint32 criterion, int timeup, char *server_name)
227 int mytimeup = time(NULL) - StartupTime;
228 uint32 mycriterion = work->ElectionCriterion;
230 /* If local master is false then never win
231 in election broadcasts. */
232 if(!lp_local_master())
234 DEBUG(3,("win_election: Losing election as local master == False\n"));
238 DEBUG(4,("win_election: election comparison: %x:%x %x:%x %d:%d %s:%s\n",
239 version, ELECTION_VERSION,
240 criterion, mycriterion,
242 server_name, global_myname));
244 if (version > ELECTION_VERSION)
246 if (version < ELECTION_VERSION)
249 if (criterion > mycriterion)
251 if (criterion < mycriterion)
254 if (timeup > mytimeup)
256 if (timeup < mytimeup)
259 if (strcasecmp(global_myname, server_name) > 0)
265 /*******************************************************************
266 Process an incoming election datagram packet.
267 ******************************************************************/
269 void process_election(struct subnet_record *subrec, struct packet_struct *p, char *buf)
271 struct dgram_packet *dgram = &p->packet.dgram;
272 int version = CVAL(buf,0);
273 uint32 criterion = IVAL(buf,1);
274 int timeup = IVAL(buf,5)/1000;
275 char *server_name = buf+13;
276 struct work_record *work;
277 char *workgroup_name = dgram->dest_name.name;
279 START_PROFILE(election);
282 DEBUG(3,("process_election: Election request from %s at IP %s on subnet %s for workgroup %s.\n",
283 server_name,inet_ntoa(p->ip), subrec->subnet_name, workgroup_name ));
285 DEBUG(5,("process_election: vers=%d criterion=%08x timeup=%d\n", version,criterion,timeup));
287 if(( work = find_workgroup_on_subnet(subrec, workgroup_name)) == NULL)
289 DEBUG(0,("process_election: Cannot find workgroup %s on subnet %s.\n",
290 workgroup_name, subrec->subnet_name ));
294 if (!strequal(work->work_group, global_myworkgroup))
296 DEBUG(3,("process_election: ignoring election request for workgroup %s on subnet %s as this \
297 is not my workgroup.\n", work->work_group, subrec->subnet_name ));
301 if (win_election(work, version,criterion,timeup,server_name))
303 /* We take precedence over the requesting server. */
304 if (!work->RunningElection)
306 /* We weren't running an election - start running one. */
308 work->needelection = True;
309 work->ElectionCount=0;
312 /* Note that if we were running an election for this workgroup on this
313 subnet already, we just ignore the server we take precedence over. */
317 /* We lost. Stop participating. */
318 work->needelection = False;
320 if (work->RunningElection || AM_LOCAL_MASTER_BROWSER(work))
322 work->RunningElection = False;
323 DEBUG(3,("process_election: >>> Lost election for workgroup %s on subnet %s <<<\n",
324 work->work_group, subrec->subnet_name ));
325 if (AM_LOCAL_MASTER_BROWSER(work))
326 unbecome_local_master_browser(subrec, work, False);
330 END_PROFILE(election);
333 /****************************************************************************
334 This function looks over all the workgroups known on all the broadcast
335 subnets and decides if a browser election is to be run on that workgroup.
336 It returns True if any election packets need to be sent (this will then
337 be done by run_elections().
338 ***************************************************************************/
340 BOOL check_elections(void)
342 struct subnet_record *subrec;
343 BOOL run_any_election = False;
345 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
347 struct work_record *work;
348 for (work = subrec->workgrouplist; work; work = work->next)
350 run_any_election |= work->RunningElection;
353 * Start an election if we have any chance of winning.
354 * Note this is a change to the previous code, that would
355 * only run an election if nmbd was in the potential browser
356 * state. We need to run elections in any state if we're told
360 if (work->needelection && !work->RunningElection && lp_local_master())
363 * We can only run an election for a workgroup if we have
364 * registered the WORKGROUP<1e> name, as that's the name
367 struct nmb_name nmbname;
369 make_nmb_name(&nmbname, work->work_group, 0x1e);
370 if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
371 DEBUG(8,("check_elections: Cannot send election packet yet as name %s not \
372 yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
376 DEBUG(3,("check_elections: >>> Starting election for workgroup %s on subnet %s <<<\n",
377 work->work_group, subrec->subnet_name ));
379 work->ElectionCount = 0;
380 work->RunningElection = True;
381 work->needelection = False;
385 return run_any_election;
390 /****************************************************************************
391 process a internal Samba message forcing an election
392 ***************************************************************************/
393 void nmbd_message_election(int msg_type, pid_t src, void *buf, size_t len)
395 struct subnet_record *subrec;
397 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
398 struct work_record *work;
399 for (work = subrec->workgrouplist; work; work = work->next) {
400 if (strequal(work->work_group, global_myworkgroup)) {
401 work->needelection = True;
402 work->ElectionCount=0;
403 work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;