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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "nmbd/nmbd.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
)
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
));
45 SCVAL(p
,0,ANN_Election
); /* Election opcode. */
48 SCVAL(p
,0,((criterion
== 0 && timeup
== 0) ? 0 : ELECTION_VERSION
));
50 SIVAL(p
,5,timeup
*1000); /* ms - Despite what the spec says. */
52 unstrcpy(srv_name
, server_name
);
53 if (!strupper_m(srv_name
)) {
54 DEBUG(2,("strupper_m failed for %s\n", srv_name
));
57 /* The following call does UNIX -> DOS charset conversion. */
58 push_ascii(p
, srv_name
, sizeof(outbuf
)-PTR_DIFF(p
,outbuf
)-1, STR_TERMINATE
);
59 p
= skip_string(outbuf
,sizeof(outbuf
),p
);
61 send_mailslot(False
, BROWSE_MAILSLOT
, outbuf
, PTR_DIFF(p
,outbuf
),
64 subrec
->bcast_ip
, subrec
->myip
, DGRAM_PORT
);
67 /*******************************************************************
68 We found a current master browser on one of our broadcast interfaces.
69 ******************************************************************/
71 static void check_for_master_browser_success(struct subnet_record
*subrec
,
72 struct userdata_struct
*userdata
,
73 struct nmb_name
*answer_name
,
74 struct in_addr answer_ip
, struct res_rec
*rrec
)
77 pull_ascii_nstring(aname
, sizeof(aname
), answer_name
->name
);
78 DEBUG(3,("check_for_master_browser_success: Local master browser for workgroup %s exists at \
79 IP %s (just checking).\n", aname
, inet_ntoa(answer_ip
) ));
82 /*******************************************************************
83 We failed to find a current master browser on one of our broadcast interfaces.
84 ******************************************************************/
86 static void check_for_master_browser_fail( struct subnet_record
*subrec
,
87 struct response_record
*rrec
,
88 struct nmb_name
*question_name
,
91 unstring workgroup_name
;
92 struct work_record
*work
;
94 pull_ascii_nstring(workgroup_name
,sizeof(workgroup_name
),question_name
->name
);
96 work
= find_workgroup_on_subnet(subrec
, workgroup_name
);
98 DEBUG(0,("check_for_master_browser_fail: Unable to find workgroup %s on subnet %s.=\n",
99 workgroup_name
, subrec
->subnet_name
));
103 if (strequal(work
->work_group
, lp_workgroup())) {
105 if (lp_local_master()) {
106 /* We have discovered that there is no local master
107 browser, and we are configured to initiate
108 an election that we will participate in.
110 DEBUG(2,("check_for_master_browser_fail: Forcing election on workgroup %s subnet %s\n",
111 work
->work_group
, subrec
->subnet_name
));
113 /* Setting this means we will participate when the
114 election is run in run_elections(). */
115 work
->needelection
= True
;
117 /* We need to force an election, because we are configured
118 not to become the local master, but we still need one,
119 having detected that one doesn't exist.
121 send_election_dgram(subrec
, work
->work_group
, 0, 0, "");
126 /*******************************************************************
127 Ensure there is a local master browser for a workgroup on our
128 broadcast interfaces.
129 ******************************************************************/
131 void check_master_browser_exists(time_t t
)
133 static time_t lastrun
=0;
134 struct subnet_record
*subrec
;
135 const char *workgroup_name
= lp_workgroup();
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
)) {
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
,
159 /*******************************************************************
161 ******************************************************************/
163 void run_elections(time_t t
)
165 static time_t lastime
= 0;
167 struct subnet_record
*subrec
;
169 /* Send election packets once every 2 seconds - note */
170 if (lastime
&& (t
- lastime
< 2)) {
176 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
)) {
177 struct work_record
*work
;
179 for (work
= subrec
->workgrouplist
; work
; work
= work
->next
) {
180 if (work
->RunningElection
) {
182 * We can only run an election for a workgroup if we have
183 * registered the WORKGROUP<1e> name, as that's the name
186 struct nmb_name nmbname
;
188 make_nmb_name(&nmbname
, work
->work_group
, 0x1e);
189 if(find_name_on_subnet( subrec
, &nmbname
, FIND_SELF_NAME
)==NULL
) {
190 DEBUG(8,("run_elections: Cannot send election packet yet as name %s not \
191 yet registered on subnet %s\n", nmb_namestr(&nmbname
), subrec
->subnet_name
));
195 send_election_dgram(subrec
, work
->work_group
, work
->ElectionCriterion
,
196 t
- StartupTime
, lp_netbios_name());
198 if (work
->ElectionCount
++ >= 4) {
199 /* Won election (4 packets were sent out uncontested. */
200 DEBUG(2,("run_elections: >>> Won election for workgroup %s on subnet %s <<<\n",
201 work
->work_group
, subrec
->subnet_name
));
203 work
->RunningElection
= False
;
205 become_local_master_browser(subrec
, work
);
212 /*******************************************************************
213 Determine if I win an election.
214 ******************************************************************/
216 static bool win_election(struct work_record
*work
, int version
,
217 uint32 criterion
, int timeup
, const char *server_name
)
219 int mytimeup
= time(NULL
) - StartupTime
;
220 uint32 mycriterion
= work
->ElectionCriterion
;
222 /* If local master is false then never win in election broadcasts. */
223 if(!lp_local_master()) {
224 DEBUG(3,("win_election: Losing election as local master == False\n"));
228 DEBUG(4,("win_election: election comparison: %x:%x %x:%x %d:%d %s:%s\n",
229 version
, ELECTION_VERSION
,
230 criterion
, mycriterion
,
232 server_name
, lp_netbios_name()));
234 if (version
> ELECTION_VERSION
)
236 if (version
< ELECTION_VERSION
)
239 if (criterion
> mycriterion
)
241 if (criterion
< mycriterion
)
244 if (timeup
> mytimeup
)
246 if (timeup
< mytimeup
)
249 if (strcasecmp_m(lp_netbios_name(), server_name
) > 0)
255 /*******************************************************************
256 Process an incoming election datagram packet.
257 ******************************************************************/
259 void process_election(struct subnet_record
*subrec
, struct packet_struct
*p
, const char *buf
)
261 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
262 int version
= CVAL(buf
,0);
263 uint32 criterion
= IVAL(buf
,1);
264 int timeup
= IVAL(buf
,5)/1000;
265 unstring server_name
;
266 struct work_record
*work
;
267 unstring workgroup_name
;
269 pull_ascii_nstring(server_name
, sizeof(server_name
), buf
+13);
270 pull_ascii_nstring(workgroup_name
, sizeof(workgroup_name
), dgram
->dest_name
.name
);
274 DEBUG(3,("process_election: Election request from %s at IP %s on subnet %s for workgroup %s.\n",
275 server_name
,inet_ntoa(p
->ip
), subrec
->subnet_name
, workgroup_name
));
277 DEBUG(5,("process_election: vers=%d criterion=%08x timeup=%d\n", version
,criterion
,timeup
));
279 if(( work
= find_workgroup_on_subnet(subrec
, workgroup_name
)) == NULL
) {
280 DEBUG(0,("process_election: Cannot find workgroup %s on subnet %s.\n",
281 workgroup_name
, subrec
->subnet_name
));
285 if (!strequal(work
->work_group
, lp_workgroup())) {
286 DEBUG(3,("process_election: ignoring election request for workgroup %s on subnet %s as this \
287 is not my workgroup.\n", work
->work_group
, subrec
->subnet_name
));
291 if (win_election(work
, version
,criterion
,timeup
,server_name
)) {
292 /* We take precedence over the requesting server. */
293 if (!work
->RunningElection
) {
294 /* We weren't running an election - start running one. */
296 work
->needelection
= True
;
297 work
->ElectionCount
=0;
300 /* Note that if we were running an election for this workgroup on this
301 subnet already, we just ignore the server we take precedence over. */
303 /* We lost. Stop participating. */
304 work
->needelection
= False
;
306 if (work
->RunningElection
|| AM_LOCAL_MASTER_BROWSER(work
)) {
307 work
->RunningElection
= False
;
308 DEBUG(3,("process_election: >>> Lost election for workgroup %s on subnet %s <<<\n",
309 work
->work_group
, subrec
->subnet_name
));
310 if (AM_LOCAL_MASTER_BROWSER(work
))
311 unbecome_local_master_browser(subrec
, work
, False
);
318 /****************************************************************************
319 This function looks over all the workgroups known on all the broadcast
320 subnets and decides if a browser election is to be run on that workgroup.
321 It returns True if any election packets need to be sent (this will then
322 be done by run_elections().
323 ***************************************************************************/
325 bool check_elections(void)
327 struct subnet_record
*subrec
;
328 bool run_any_election
= False
;
330 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
)) {
331 struct work_record
*work
;
332 for (work
= subrec
->workgrouplist
; work
; work
= work
->next
) {
333 if (work
->RunningElection
) {
334 run_any_election
= work
->RunningElection
;
338 * Start an election if we have any chance of winning.
339 * Note this is a change to the previous code, that would
340 * only run an election if nmbd was in the potential browser
341 * state. We need to run elections in any state if we're told
345 if (work
->needelection
&& !work
->RunningElection
&& lp_local_master()) {
347 * We can only run an election for a workgroup if we have
348 * registered the WORKGROUP<1e> name, as that's the name
351 struct nmb_name nmbname
;
353 make_nmb_name(&nmbname
, work
->work_group
, 0x1e);
354 if(find_name_on_subnet( subrec
, &nmbname
, FIND_SELF_NAME
)==NULL
) {
355 DEBUG(8,("check_elections: Cannot send election packet yet as name %s not \
356 yet registered on subnet %s\n", nmb_namestr(&nmbname
), subrec
->subnet_name
));
360 DEBUG(3,("check_elections: >>> Starting election for workgroup %s on subnet %s <<<\n",
361 work
->work_group
, subrec
->subnet_name
));
363 work
->ElectionCount
= 0;
364 work
->RunningElection
= True
;
365 work
->needelection
= False
;
369 return run_any_election
;
372 /****************************************************************************
373 Process a internal Samba message forcing an election.
374 ***************************************************************************/
376 void nmbd_message_election(struct messaging_context
*msg
,
379 struct server_id server_id
,
382 struct subnet_record
*subrec
;
384 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
)) {
385 struct work_record
*work
;
386 for (work
= subrec
->workgrouplist
; work
; work
= work
->next
) {
387 if (strequal(work
->work_group
, lp_workgroup())) {
388 work
->needelection
= True
;
389 work
->ElectionCount
=0;
390 work
->mst_state
= lp_local_master() ? MST_POTENTIAL
: MST_NONE
;