r15837: starting sync up for 3.0.23rc1 (in sync with SAMBA_3_0 r15822)
[Samba.git] / source / nmbd / nmbd_elections.c
blob50e13729361e2ca39660c3b934eb315bba03004b
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
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 /* Election parameters. */
27 extern time_t StartupTime;
29 /****************************************************************************
30 Send an election datagram packet.
31 **************************************************************************/
33 static void send_election_dgram(struct subnet_record *subrec, const char *workgroup_name,
34 uint32 criterion, int timeup,const char *server_name)
36 pstring outbuf;
37 unstring srv_name;
38 char *p;
40 DEBUG(2,("send_election_dgram: Sending election packet for workgroup %s on subnet %s\n",
41 workgroup_name, subrec->subnet_name ));
43 memset(outbuf,'\0',sizeof(outbuf));
44 p = outbuf;
45 SCVAL(p,0,ANN_Election); /* Election opcode. */
46 p++;
48 SCVAL(p,0,((criterion == 0 && timeup == 0) ? 0 : ELECTION_VERSION));
49 SIVAL(p,1,criterion);
50 SIVAL(p,5,timeup*1000); /* ms - Despite what the spec says. */
51 p += 13;
52 unstrcpy(srv_name, server_name);
53 strupper_m(srv_name);
54 /* The following call does UNIX -> DOS charset conversion. */
55 pstrcpy_base(p, srv_name, outbuf);
56 p = skip_string(p,1);
58 send_mailslot(False, BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
59 global_myname(), 0,
60 workgroup_name, 0x1e,
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 unstring aname;
74 pull_ascii_nstring(aname, sizeof(aname), answer_name->name);
75 DEBUG(3,("check_for_master_browser_success: Local master browser for workgroup %s exists at \
76 IP %s (just checking).\n", aname, inet_ntoa(answer_ip) ));
79 /*******************************************************************
80 We failed to find a current master browser on one of our broadcast interfaces.
81 ******************************************************************/
83 static void check_for_master_browser_fail( struct subnet_record *subrec,
84 struct response_record *rrec,
85 struct nmb_name *question_name,
86 int fail_code)
88 unstring workgroup_name;
89 struct work_record *work;
91 pull_ascii_nstring(workgroup_name,sizeof(workgroup_name),question_name->name);
93 work = find_workgroup_on_subnet(subrec, workgroup_name);
94 if(work == NULL) {
95 DEBUG(0,("check_for_master_browser_fail: Unable to find workgroup %s on subnet %s.=\n",
96 workgroup_name, subrec->subnet_name ));
97 return;
100 if (strequal(work->work_group, lp_workgroup())) {
102 if (lp_local_master()) {
103 /* We have discovered that there is no local master
104 browser, and we are configured to initiate
105 an election that we will participate in.
107 DEBUG(2,("check_for_master_browser_fail: Forcing election on workgroup %s subnet %s\n",
108 work->work_group, subrec->subnet_name ));
110 /* Setting this means we will participate when the
111 election is run in run_elections(). */
112 work->needelection = True;
113 } else {
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 const char *workgroup_name = lp_workgroup();
134 if (!lastrun)
135 lastrun = t;
137 if (t < (lastrun + (CHECK_TIME_MST_BROWSE * 60)))
138 return;
140 lastrun = t;
142 dump_workgroups(False);
144 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) {
148 if (strequal(work->work_group, workgroup_name) && !AM_LOCAL_MASTER_BROWSER(work)) {
149 /* Do a name query for the local master browser on this net. */
150 query_name( subrec, work->work_group, 0x1d,
151 check_for_master_browser_success,
152 check_for_master_browser_fail,
153 NULL);
159 /*******************************************************************
160 Run an election.
161 ******************************************************************/
163 void run_elections(time_t t)
165 static time_t lastime = 0;
167 struct subnet_record *subrec;
169 START_PROFILE(run_elections);
171 /* Send election packets once every 2 seconds - note */
172 if (lastime && (t - lastime < 2)) {
173 END_PROFILE(run_elections);
174 return;
177 lastime = t;
179 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) {
183 if (work->RunningElection) {
185 * We can only run an election for a workgroup if we have
186 * registered the WORKGROUP<1e> name, as that's the name
187 * we must listen to.
189 struct nmb_name nmbname;
191 make_nmb_name(&nmbname, work->work_group, 0x1e);
192 if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
193 DEBUG(8,("run_elections: Cannot send election packet yet as name %s not \
194 yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
195 continue;
198 send_election_dgram(subrec, work->work_group, work->ElectionCriterion,
199 t - StartupTime, global_myname());
201 if (work->ElectionCount++ >= 4) {
202 /* Won election (4 packets were sent out uncontested. */
203 DEBUG(2,("run_elections: >>> Won election for workgroup %s on subnet %s <<<\n",
204 work->work_group, subrec->subnet_name ));
206 work->RunningElection = False;
208 become_local_master_browser(subrec, work);
213 END_PROFILE(run_elections);
216 /*******************************************************************
217 Determine if I win an election.
218 ******************************************************************/
220 static BOOL win_election(struct work_record *work, int version,
221 uint32 criterion, int timeup, const char *server_name)
223 int mytimeup = time(NULL) - StartupTime;
224 uint32 mycriterion = work->ElectionCriterion;
226 /* If local master is false then never win in election broadcasts. */
227 if(!lp_local_master()) {
228 DEBUG(3,("win_election: Losing election as local master == False\n"));
229 return False;
232 DEBUG(4,("win_election: election comparison: %x:%x %x:%x %d:%d %s:%s\n",
233 version, ELECTION_VERSION,
234 criterion, mycriterion,
235 timeup, mytimeup,
236 server_name, global_myname()));
238 if (version > ELECTION_VERSION)
239 return(False);
240 if (version < ELECTION_VERSION)
241 return(True);
243 if (criterion > mycriterion)
244 return(False);
245 if (criterion < mycriterion)
246 return(True);
248 if (timeup > mytimeup)
249 return(False);
250 if (timeup < mytimeup)
251 return(True);
253 if (StrCaseCmp(global_myname(), server_name) > 0)
254 return(False);
256 return(True);
259 /*******************************************************************
260 Process an incoming election datagram packet.
261 ******************************************************************/
263 void process_election(struct subnet_record *subrec, struct packet_struct *p, char *buf)
265 struct dgram_packet *dgram = &p->packet.dgram;
266 int version = CVAL(buf,0);
267 uint32 criterion = IVAL(buf,1);
268 int timeup = IVAL(buf,5)/1000;
269 unstring server_name;
270 struct work_record *work;
271 unstring workgroup_name;
273 START_PROFILE(election);
275 pull_ascii_nstring(server_name, sizeof(server_name), buf+13);
276 pull_ascii_nstring(workgroup_name, sizeof(workgroup_name), dgram->dest_name.name);
278 server_name[15] = 0;
280 DEBUG(3,("process_election: Election request from %s at IP %s on subnet %s for workgroup %s.\n",
281 server_name,inet_ntoa(p->ip), subrec->subnet_name, workgroup_name ));
283 DEBUG(5,("process_election: vers=%d criterion=%08x timeup=%d\n", version,criterion,timeup));
285 if(( work = find_workgroup_on_subnet(subrec, workgroup_name)) == NULL) {
286 DEBUG(0,("process_election: Cannot find workgroup %s on subnet %s.\n",
287 workgroup_name, subrec->subnet_name ));
288 goto done;
291 if (!strequal(work->work_group, lp_workgroup())) {
292 DEBUG(3,("process_election: ignoring election request for workgroup %s on subnet %s as this \
293 is not my workgroup.\n", work->work_group, subrec->subnet_name ));
294 goto done;
297 if (win_election(work, version,criterion,timeup,server_name)) {
298 /* We take precedence over the requesting server. */
299 if (!work->RunningElection) {
300 /* We weren't running an election - start running one. */
302 work->needelection = True;
303 work->ElectionCount=0;
306 /* Note that if we were running an election for this workgroup on this
307 subnet already, we just ignore the server we take precedence over. */
308 } else {
309 /* We lost. Stop participating. */
310 work->needelection = False;
312 if (work->RunningElection || AM_LOCAL_MASTER_BROWSER(work)) {
313 work->RunningElection = False;
314 DEBUG(3,("process_election: >>> Lost election for workgroup %s on subnet %s <<<\n",
315 work->work_group, subrec->subnet_name ));
316 if (AM_LOCAL_MASTER_BROWSER(work))
317 unbecome_local_master_browser(subrec, work, False);
320 done:
322 END_PROFILE(election);
325 /****************************************************************************
326 This function looks over all the workgroups known on all the broadcast
327 subnets and decides if a browser election is to be run on that workgroup.
328 It returns True if any election packets need to be sent (this will then
329 be done by run_elections().
330 ***************************************************************************/
332 BOOL check_elections(void)
334 struct subnet_record *subrec;
335 BOOL run_any_election = False;
337 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
338 struct work_record *work;
339 for (work = subrec->workgrouplist; work; work = work->next) {
340 run_any_election |= work->RunningElection;
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.
350 if (work->needelection && !work->RunningElection && lp_local_master()) {
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.
356 struct nmb_name nmbname;
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 \
361 yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
362 continue;
365 DEBUG(3,("check_elections: >>> Starting election for workgroup %s on subnet %s <<<\n",
366 work->work_group, subrec->subnet_name ));
368 work->ElectionCount = 0;
369 work->RunningElection = True;
370 work->needelection = False;
374 return run_any_election;
377 /****************************************************************************
378 Process a internal Samba message forcing an election.
379 ***************************************************************************/
381 void nmbd_message_election(int msg_type, struct process_id src,
382 void *buf, size_t len)
384 struct subnet_record *subrec;
386 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
387 struct work_record *work;
388 for (work = subrec->workgrouplist; work; work = work->next) {
389 if (strequal(work->work_group, lp_workgroup())) {
390 work->needelection = True;
391 work->ElectionCount=0;
392 work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;