2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1995
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 14 jan 96: lkcl@pires.co.uk
24 added multiple workgroup domain master support
31 extern int ClientDGRAM;
33 extern int DEBUGLEVEL;
36 extern pstring myname;
38 /* machine comment for host announcements */
39 extern pstring ServerComment;
41 /* here are my election parameters */
43 extern time_t StartupTime;
45 #define AM_MASTER(work) (work->ServerType & SV_TYPE_MASTER_BROWSER)
47 #define BROWSE_MAILSLOT "\\MAILSLOT\\BROWSE"
49 extern struct subnet_record *subnetlist;
52 /*******************************************************************
53 occasionally check to see if the master browser is around
54 ******************************************************************/
55 void check_master_browser(void)
57 static time_t lastrun=0;
58 time_t t = time(NULL);
59 struct subnet_record *d;
61 if (!lastrun) lastrun = t;
62 if (t < lastrun + CHECK_TIME_MST_BROWSE * 60)
69 for (d = subnetlist; d; d = d->next)
71 struct work_record *work;
73 for (work = d->workgrouplist; work; work = work->next)
75 /* if we are not the browse master of a workgroup, and we can't
76 find a browser on the subnet, do something about it. */
80 queue_netbios_packet(ClientNMB,NMB_QUERY,CHECK_MASTER,
81 work->work_group,0x1d,0,
82 True,False,d->bcast_ip);
89 /*******************************************************************
90 what to do if a master browser DOESN't exist
91 ******************************************************************/
92 void browser_gone(char *work_name, struct in_addr ip)
94 struct subnet_record *d = find_domain(ip);
95 struct work_record *work = find_workgroupstruct(d, work_name, False);
97 if (!work || !d) return;
99 if (strequal(work->work_group, lp_workgroup()) &&
103 DEBUG(2,("Forcing election on %s %s\n",
104 work->work_group,inet_ntoa(d->bcast_ip)));
106 /* we can attempt to become master browser */
107 work->needelection = True;
111 /* XXXX note: this will delete entries that have been added in by
112 lmhosts as well. a flag to ensure that these are not deleted may
115 /* workgroup with no master browser is not the default workgroup:
116 it's also not on our subnet. therefore delete it: it can be
117 recreated dynamically */
119 send_election(d, work->work_group, 0, 0, myname);
120 remove_workgroup(d, work);
124 /****************************************************************************
125 send an election packet
126 **************************************************************************/
127 void send_election(struct subnet_record *d, char *group,uint32 criterion,
128 int timeup,char *name)
135 DEBUG(2,("Sending election to %s for workgroup %s\n",
136 inet_ntoa(d->bcast_ip),group));
138 bzero(outbuf,sizeof(outbuf));
140 CVAL(p,0) = 8; /* election */
143 CVAL(p,0) = (criterion == 0 && timeup == 0) ? 0 : ELECTION_VERSION;
144 SIVAL(p,1,criterion);
145 SIVAL(p,5,timeup*1000); /* ms - despite the spec */
149 p = skip_string(p,1);
151 send_mailslot_reply(BROWSE_MAILSLOT,ClientDGRAM,outbuf,PTR_DIFF(p,outbuf),
152 name,group,0,0x1e,d->bcast_ip,*iface_ip(d->bcast_ip));
156 /*******************************************************************
157 become the master browser
158 ******************************************************************/
159 static void become_master(struct subnet_record *d, struct work_record *work)
161 uint32 domain_type = SV_TYPE_DOMAIN_ENUM | SV_TYPE_SERVER_UNIX | 0x00400000;
165 DEBUG(2,("Becoming master for %s\n",work->work_group));
167 work->ServerType |= SV_TYPE_MASTER_BROWSER;
168 work->ServerType &= ~SV_TYPE_POTENTIAL_BROWSER;
169 work->ElectionCriterion |= 0x5;
171 /* add browse, master and general names to database or register with WINS */
172 add_name_entry(MSBROWSE ,0x01,NB_ACTIVE|NB_GROUP);
173 add_name_entry(work->work_group,0x1d,NB_ACTIVE );
175 if (lp_domain_master())
177 DEBUG(4,("Domain master: adding names...\n"));
179 /* add domain master and domain member names or register with WINS */
180 add_name_entry(work->work_group,0x1b,NB_ACTIVE);
181 work->ServerType |= SV_TYPE_DOMAIN_MASTER;
183 if (lp_domain_logons())
185 work->ServerType |= SV_TYPE_DOMAIN_CTRL;
186 work->ServerType |= SV_TYPE_DOMAIN_MEMBER;
190 /* update our server status */
191 add_server_entry(d,work,work->work_group,domain_type,0,myname,True);
192 add_server_entry(d,work,myname,work->ServerType,0,ServerComment,True);
196 /* ask all servers on our local net to announce to us */
197 announce_request(work, d->bcast_ip);
202 /*******************************************************************
203 unbecome the master browser
204 ******************************************************************/
205 void become_nonmaster(struct subnet_record *d, struct work_record *work)
207 DEBUG(2,("Becoming non-master for %s\n",work->work_group));
209 work->ServerType &= ~SV_TYPE_MASTER_BROWSER;
210 work->ServerType &= ~SV_TYPE_DOMAIN_MASTER;
211 work->ServerType |= SV_TYPE_POTENTIAL_BROWSER;
213 work->ElectionCriterion &= ~0x4;
215 remove_name_entry(work->work_group,0x1b);
216 remove_name_entry(work->work_group,0x1d);
217 remove_name_entry(MSBROWSE ,0x01);
221 /*******************************************************************
223 ******************************************************************/
224 void run_elections(void)
226 time_t t = time(NULL);
227 static time_t lastime = 0;
229 struct subnet_record *d;
231 /* send election packets once a second */
232 if (lastime && t-lastime <= 0) return;
236 for (d = subnetlist; d; d = d->next)
238 struct work_record *work;
239 for (work = d->workgrouplist; work; work = work->next)
241 if (work->RunningElection)
243 send_election(d,work->work_group, work->ElectionCriterion,
244 t-StartupTime,myname);
246 if (work->ElectionCount++ >= 4)
248 /* I won! now what :-) */
249 DEBUG(2,(">>> Won election on %s %s <<<\n",
250 work->work_group,inet_ntoa(d->bcast_ip)));
252 work->RunningElection = False;
253 become_master(d, work);
261 /*******************************************************************
262 work out if I win an election
263 ******************************************************************/
264 static BOOL win_election(struct work_record *work,int version,uint32 criterion,
265 int timeup,char *name)
267 time_t t = time(NULL);
269 if (version > ELECTION_VERSION) return(False);
270 if (version < ELECTION_VERSION) return(True);
272 mycriterion = work->ElectionCriterion;
274 if (criterion > mycriterion) return(False);
275 if (criterion < mycriterion) return(True);
277 if (timeup > (t - StartupTime)) return(False);
278 if (timeup < (t - StartupTime)) return(True);
280 if (strcasecmp(myname,name) > 0) return(False);
286 /*******************************************************************
287 process a election packet
289 An election dynamically decides who will be the master.
290 ******************************************************************/
291 void process_election(struct packet_struct *p,char *buf)
293 struct dgram_packet *dgram = &p->packet.dgram;
294 struct in_addr ip = dgram->header.source_ip;
295 struct subnet_record *d = find_domain(ip);
296 int version = CVAL(buf,0);
297 uint32 criterion = IVAL(buf,1);
298 int timeup = IVAL(buf,5)/1000;
300 struct work_record *work;
306 DEBUG(3,("Election request from %s vers=%d criterion=%08x timeup=%d\n",
307 name,version,criterion,timeup));
309 if (same_context(dgram)) return;
311 for (work = d->workgrouplist; work; work = work->next)
313 if (listening_name(work, &dgram->dest_name) &&
314 strequal(work->work_group, lp_workgroup()) &&
317 if (win_election(work, version,criterion,timeup,name))
319 if (!work->RunningElection)
321 work->needelection = True;
322 work->ElectionCount=0;
327 work->needelection = False;
329 if (work->RunningElection)
331 work->RunningElection = False;
332 DEBUG(3,(">>> Lost election on %s %s <<<\n",
333 work->work_group,inet_ntoa(d->bcast_ip)));
335 /* if we are the master then remove our masterly names */
338 become_nonmaster(d, work);
347 /****************************************************************************
348 checks whether a browser election is to be run on any workgroup
349 ***************************************************************************/
350 BOOL check_elections(void)
352 struct subnet_record *d;
353 BOOL run_any_election = False;
355 for (d = subnetlist; d; d = d->next)
357 struct work_record *work;
358 for (work = d->workgrouplist; work; work = work->next)
360 run_any_election |= work->RunningElection;
362 if (work->needelection && !work->RunningElection)
364 DEBUG(3,(">>> Starting election on %s %s <<<\n",
365 work->work_group,inet_ntoa(d->bcast_ip)));
366 work->ElectionCount = 0;
367 work->RunningElection = True;
368 work->needelection = False;
372 return run_any_election;