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 int DEBUGLEVEL
;
29 extern pstring global_myname
;
30 extern fstring global_myworkgroup
;
32 /* Election parameters. */
33 extern time_t StartupTime
;
35 /****************************************************************************
36 Send an election datagram packet.
37 **************************************************************************/
38 static void send_election_dgram(struct subnet_record
*subrec
, char *workgroup_name
,
39 uint32 criterion
, int timeup
,char *server_name
)
44 DEBUG(2,("send_election_dgram: Sending election packet for workgroup %s on subnet %s\n",
45 workgroup_name
, subrec
->subnet_name
));
47 memset(outbuf
,'\0',sizeof(outbuf
));
49 CVAL(p
,0) = ANN_Election
; /* Election opcode. */
52 CVAL(p
,0) = (criterion
== 0 && timeup
== 0) ? 0 : ELECTION_VERSION
;
54 SIVAL(p
,5,timeup
*1000); /* ms - Despite what the spec says. */
56 pstrcpy(p
,server_name
);
60 send_mailslot(False
, BROWSE_MAILSLOT
, outbuf
, PTR_DIFF(p
,outbuf
),
63 subrec
->bcast_ip
, subrec
->myip
, DGRAM_PORT
);
66 /*******************************************************************
67 We found a current master browser on one of our broadcast interfaces.
68 ******************************************************************/
70 static void check_for_master_browser_success(struct subnet_record
*subrec
,
71 struct userdata_struct
*userdata
,
72 struct nmb_name
*answer_name
,
73 struct in_addr answer_ip
, struct res_rec
*rrec
)
75 DEBUG(3,("check_for_master_browser_success: Local master browser for workgroup %s exists at \
76 IP %s (just checking).\n", answer_name
->name
, 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
,
88 char *workgroup_name
= question_name
->name
;
89 struct work_record
*work
= find_workgroup_on_subnet(subrec
, workgroup_name
);
93 DEBUG(0,("check_for_master_browser_fail: Unable to find workgroup %s on subnet %s.=\n",
94 workgroup_name
, subrec
->subnet_name
));
98 if (strequal(work
->work_group
, global_myworkgroup
))
101 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
;
116 /* We need to force an election, because we are configured
117 not to become the local master, but we still need one,
118 having detected that one doesn't exist.
120 send_election_dgram(subrec
, work
->work_group
, 0, 0, "");
125 /*******************************************************************
126 Ensure there is a local master browser for a workgroup on our
127 broadcast interfaces.
128 ******************************************************************/
130 void check_master_browser_exists(time_t t
)
132 static time_t lastrun
=0;
133 struct subnet_record
*subrec
;
134 char *workgroup_name
= global_myworkgroup
;
139 if (t
< (lastrun
+ (CHECK_TIME_MST_BROWSE
* 60)))
144 dump_workgroups(False
);
146 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
))
148 struct work_record
*work
;
150 for (work
= subrec
->workgrouplist
; work
; work
= work
->next
)
152 if (strequal(work
->work_group
, workgroup_name
) && !AM_LOCAL_MASTER_BROWSER(work
))
154 /* Do a name query for the local master browser on this net. */
155 query_name( subrec
, work
->work_group
, 0x1d,
156 check_for_master_browser_success
,
157 check_for_master_browser_fail
,
164 /*******************************************************************
166 ******************************************************************/
168 void run_elections(time_t t
)
170 static time_t lastime
= 0;
172 struct subnet_record
*subrec
;
174 /* Send election packets once every 2 seconds - note */
175 if (lastime
&& (t
- lastime
< 2))
180 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
))
182 struct work_record
*work
;
184 for (work
= subrec
->workgrouplist
; work
; work
= work
->next
)
186 if (work
->RunningElection
)
189 * We can only run an election for a workgroup if we have
190 * registered the WORKGROUP<1e> name, as that's the name
193 struct nmb_name nmbname
;
195 make_nmb_name(&nmbname
, work
->work_group
, 0x1e);
196 if(find_name_on_subnet( subrec
, &nmbname
, FIND_SELF_NAME
)==NULL
) {
197 DEBUG(8,("run_elections: Cannot send election packet yet as name %s not \
198 yet registered on subnet %s\n", nmb_namestr(&nmbname
), subrec
->subnet_name
));
202 send_election_dgram(subrec
, work
->work_group
, work
->ElectionCriterion
,
203 t
- StartupTime
, global_myname
);
205 if (work
->ElectionCount
++ >= 4)
207 /* Won election (4 packets were sent out uncontested. */
208 DEBUG(2,("run_elections: >>> Won election for workgroup %s on subnet %s <<<\n",
209 work
->work_group
, subrec
->subnet_name
));
211 work
->RunningElection
= False
;
213 become_local_master_browser(subrec
, work
);
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
;
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
));
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
));
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. */
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
);
330 /****************************************************************************
331 This function looks over all the workgroups known on all the broadcast
332 subnets and decides if a browser election is to be run on that workgroup.
333 It returns True if any election packets need to be sent (this will then
334 be done by run_elections().
335 ***************************************************************************/
337 BOOL
check_elections(void)
339 struct subnet_record
*subrec
;
340 BOOL run_any_election
= False
;
342 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
))
344 struct work_record
*work
;
345 for (work
= subrec
->workgrouplist
; work
; work
= work
->next
)
347 run_any_election
|= work
->RunningElection
;
350 * Start an election if we have any chance of winning.
351 * Note this is a change to the previous code, that would
352 * only run an election if nmbd was in the potential browser
353 * state. We need to run elections in any state if we're told
357 if (work
->needelection
&& !work
->RunningElection
&& lp_local_master())
360 * We can only run an election for a workgroup if we have
361 * registered the WORKGROUP<1e> name, as that's the name
364 struct nmb_name nmbname
;
366 make_nmb_name(&nmbname
, work
->work_group
, 0x1e);
367 if(find_name_on_subnet( subrec
, &nmbname
, FIND_SELF_NAME
)==NULL
) {
368 DEBUG(8,("check_elections: Cannot send election packet yet as name %s not \
369 yet registered on subnet %s\n", nmb_namestr(&nmbname
), subrec
->subnet_name
));
373 DEBUG(3,("check_elections: >>> Starting election for workgroup %s on subnet %s <<<\n",
374 work
->work_group
, subrec
->subnet_name
));
376 work
->ElectionCount
= 0;
377 work
->RunningElection
= True
;
378 work
->needelection
= False
;
382 return run_any_election
;
387 /****************************************************************************
388 process a internal Samba message forcing an election
389 ***************************************************************************/
390 void nmbd_message_election(int msg_type
, pid_t src
, void *buf
, size_t len
)
392 struct subnet_record
*subrec
;
394 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
)) {
395 struct work_record
*work
;
396 for (work
= subrec
->workgrouplist
; work
; work
= work
->next
) {
397 if (strequal(work
->work_group
, global_myworkgroup
)) {
398 work
->needelection
= True
;
399 work
->ElectionCount
=0;
400 work
->mst_state
= lp_local_master() ? MST_POTENTIAL
: MST_NONE
;