Removed version number from file header.
[jra/samba/.git] / source3 / nmbd / nmbd_elections.c
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-1998
7    
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.
12    
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.
17    
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.
21    
22 */
23
24 #include "includes.h"
25
26 extern pstring global_myname;
27 extern fstring global_myworkgroup;
28
29 /* Election parameters. */
30 extern time_t StartupTime;
31
32 /****************************************************************************
33   Send an election datagram packet.
34 **************************************************************************/
35 static void send_election_dgram(struct subnet_record *subrec, char *workgroup_name,
36                                 uint32 criterion, int timeup,char *server_name)
37 {
38   pstring outbuf;
39   char *p;
40
41   DEBUG(2,("send_election_dgram: Sending election packet for workgroup %s on subnet %s\n",
42         workgroup_name, subrec->subnet_name ));
43
44   memset(outbuf,'\0',sizeof(outbuf));
45   p = outbuf;
46   SCVAL(p,0,ANN_Election); /* Election opcode. */
47   p++;
48
49   SCVAL(p,0,((criterion == 0 && timeup == 0) ? 0 : ELECTION_VERSION));
50   SIVAL(p,1,criterion);
51   SIVAL(p,5,timeup*1000); /* ms - Despite what the spec says. */
52   p += 13;
53   pstrcpy(p,server_name);
54   strupper(p);
55   p = skip_string(p,1);
56   
57   send_mailslot(False, BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
58                 global_myname, 0,
59                 workgroup_name, 0x1e,
60                 subrec->bcast_ip, subrec->myip, DGRAM_PORT);
61 }
62
63 /*******************************************************************
64  We found a current master browser on one of our broadcast interfaces.
65 ******************************************************************/
66
67 static void check_for_master_browser_success(struct subnet_record *subrec,
68                                  struct userdata_struct *userdata,
69                                  struct nmb_name *answer_name,
70                                  struct in_addr answer_ip, struct res_rec *rrec)
71 {
72   DEBUG(3,("check_for_master_browser_success: Local master browser for workgroup %s exists at \
73 IP %s (just checking).\n", answer_name->name, inet_ntoa(answer_ip) ));
74 }
75
76 /*******************************************************************
77  We failed to find a current master browser on one of our broadcast interfaces.
78 ******************************************************************/
79
80 static void check_for_master_browser_fail( struct subnet_record *subrec,
81                                            struct response_record *rrec,
82                                            struct nmb_name *question_name,
83                                            int fail_code)
84 {
85   char *workgroup_name = question_name->name;
86   struct work_record *work = find_workgroup_on_subnet(subrec, workgroup_name);
87
88   if(work == NULL)
89   {
90     DEBUG(0,("check_for_master_browser_fail: Unable to find workgroup %s on subnet %s.=\n",
91           workgroup_name, subrec->subnet_name ));
92     return;
93   }
94
95   if (strequal(work->work_group, global_myworkgroup))
96   {
97
98     if (lp_local_master())
99     {
100       /* We have discovered that there is no local master
101          browser, and we are configured to initiate
102          an election that we will participate in.
103        */
104       DEBUG(2,("check_for_master_browser_fail: Forcing election on workgroup %s subnet %s\n",
105                work->work_group, subrec->subnet_name ));
106
107       /* Setting this means we will participate when the
108          election is run in run_elections(). */
109       work->needelection = True;
110     }
111     else
112     {
113       /* We need to force an election, because we are configured
114          not to become the local master, but we still need one,
115          having detected that one doesn't exist.
116        */
117       send_election_dgram(subrec, work->work_group, 0, 0, "");
118     }
119   }
120 }
121
122 /*******************************************************************
123   Ensure there is a local master browser for a workgroup on our
124   broadcast interfaces.
125 ******************************************************************/
126
127 void check_master_browser_exists(time_t t)
128 {
129   static time_t lastrun=0;
130   struct subnet_record *subrec;
131   char *workgroup_name = global_myworkgroup;
132
133   if (!lastrun)
134     lastrun = t;
135
136   if (t < (lastrun + (CHECK_TIME_MST_BROWSE * 60)))
137     return;
138
139   lastrun = t;
140
141   dump_workgroups(False);
142
143   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
144   {
145     struct work_record *work;
146
147     for (work = subrec->workgrouplist; work; work = work->next)
148     {
149       if (strequal(work->work_group, workgroup_name) && !AM_LOCAL_MASTER_BROWSER(work))
150       {
151         /* Do a name query for the local master browser on this net. */
152         query_name( subrec, work->work_group, 0x1d,
153                     check_for_master_browser_success,
154                     check_for_master_browser_fail,
155                     NULL);
156       }
157     }
158   }
159 }
160
161 /*******************************************************************
162   Run an election.
163 ******************************************************************/
164
165 void run_elections(time_t t)
166 {
167   static time_t lastime = 0;
168   
169   struct subnet_record *subrec;
170   
171   /* Send election packets once every 2 seconds - note */
172   if (lastime && (t - lastime < 2))
173     return;
174   
175   lastime = t;
176   
177   START_PROFILE(run_elections);
178   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
179   {
180     struct work_record *work;
181
182     for (work = subrec->workgrouplist; work; work = work->next)
183     {
184       if (work->RunningElection)
185       {
186         /*
187          * We can only run an election for a workgroup if we have
188          * registered the WORKGROUP<1e> name, as that's the name
189          * we must listen to.
190          */
191         struct nmb_name nmbname;
192
193         make_nmb_name(&nmbname, work->work_group, 0x1e);
194         if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
195           DEBUG(8,("run_elections: Cannot send election packet yet as name %s not \
196 yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
197           continue;
198         }
199
200         send_election_dgram(subrec, work->work_group, work->ElectionCriterion,
201                       t - StartupTime, global_myname);
202               
203         if (work->ElectionCount++ >= 4)
204         {
205           /* Won election (4 packets were sent out uncontested. */
206           DEBUG(2,("run_elections: >>> Won election for workgroup %s on subnet %s <<<\n",
207                     work->work_group, subrec->subnet_name ));
208
209           work->RunningElection = False;
210
211           become_local_master_browser(subrec, work);
212         }
213       }
214     }
215   }
216   END_PROFILE(run_elections);
217 }
218
219 /*******************************************************************
220   Determine if I win an election.
221 ******************************************************************/
222
223 static BOOL win_election(struct work_record *work, int version,
224                          uint32 criterion, int timeup, char *server_name)
225 {  
226   int mytimeup = time(NULL) - StartupTime;
227   uint32 mycriterion = work->ElectionCriterion;
228
229   /* If local master is false then never win
230      in election broadcasts. */
231   if(!lp_local_master())
232   {
233     DEBUG(3,("win_election: Losing election as local master == False\n"));
234     return False;
235   }
236  
237   DEBUG(4,("win_election: election comparison: %x:%x %x:%x %d:%d %s:%s\n",
238         version, ELECTION_VERSION,
239         criterion, mycriterion,
240         timeup, mytimeup,
241         server_name, global_myname));
242
243   if (version > ELECTION_VERSION)
244     return(False);
245   if (version < ELECTION_VERSION)
246     return(True);
247   
248   if (criterion > mycriterion)
249     return(False);
250   if (criterion < mycriterion)
251     return(True);
252
253   if (timeup > mytimeup)
254     return(False);
255   if (timeup < mytimeup)
256     return(True);
257
258   if (strcasecmp(global_myname, server_name) > 0)
259     return(False);
260   
261   return(True);
262 }
263
264 /*******************************************************************
265   Process an incoming election datagram packet.
266 ******************************************************************/
267
268 void process_election(struct subnet_record *subrec, struct packet_struct *p, char *buf)
269 {
270   struct dgram_packet *dgram = &p->packet.dgram;
271   int version = CVAL(buf,0);
272   uint32 criterion = IVAL(buf,1);
273   int timeup = IVAL(buf,5)/1000;
274   char *server_name = buf+13;
275   struct work_record *work;
276   char *workgroup_name = dgram->dest_name.name;
277
278   START_PROFILE(election);
279   server_name[15] = 0;  
280
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 ));
283
284   DEBUG(5,("process_election: vers=%d criterion=%08x timeup=%d\n", version,criterion,timeup));
285
286   if(( work = find_workgroup_on_subnet(subrec, workgroup_name)) == NULL)
287   {
288     DEBUG(0,("process_election: Cannot find workgroup %s on subnet %s.\n",
289           workgroup_name, subrec->subnet_name ));
290     goto done;
291   }
292
293   if (!strequal(work->work_group, global_myworkgroup))
294   {
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 ));
297     goto done;
298   }
299
300   if (win_election(work, version,criterion,timeup,server_name))
301   {
302     /* We take precedence over the requesting server. */
303     if (!work->RunningElection)
304     {
305       /* We weren't running an election - start running one. */
306
307       work->needelection = True;
308       work->ElectionCount=0;
309     }
310
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. */
313   }
314   else
315   {
316     /* We lost. Stop participating. */
317     work->needelection = False;
318
319     if (work->RunningElection || AM_LOCAL_MASTER_BROWSER(work))
320     {
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);
326     }
327   }
328 done:
329   END_PROFILE(election);
330 }
331
332 /****************************************************************************
333   This function looks over all the workgroups known on all the broadcast
334   subnets and decides if a browser election is to be run on that workgroup.
335   It returns True if any election packets need to be sent (this will then
336   be done by run_elections().
337 ***************************************************************************/
338
339 BOOL check_elections(void)
340 {
341   struct subnet_record *subrec;
342   BOOL run_any_election = False;
343
344   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
345   {
346     struct work_record *work;
347     for (work = subrec->workgrouplist; work; work = work->next)
348     {
349       run_any_election |= work->RunningElection;
350
351       /* 
352        * Start an election if we have any chance of winning.
353        * Note this is a change to the previous code, that would
354        * only run an election if nmbd was in the potential browser
355        * state. We need to run elections in any state if we're told
356        * to. JRA.
357        */
358
359       if (work->needelection && !work->RunningElection && lp_local_master())
360       {
361         /*
362          * We can only run an election for a workgroup if we have
363          * registered the WORKGROUP<1e> name, as that's the name
364          * we must listen to.
365          */
366         struct nmb_name nmbname;
367
368         make_nmb_name(&nmbname, work->work_group, 0x1e);
369         if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
370           DEBUG(8,("check_elections: Cannot send election packet yet as name %s not \
371 yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
372           continue;
373         }
374
375         DEBUG(3,("check_elections: >>> Starting election for workgroup %s on subnet %s <<<\n",
376               work->work_group, subrec->subnet_name ));
377
378         work->ElectionCount = 0;
379         work->RunningElection = True;
380         work->needelection = False;
381       }
382     }
383   }
384   return run_any_election;
385 }
386
387
388
389 /****************************************************************************
390 process a internal Samba message forcing an election
391 ***************************************************************************/
392 void nmbd_message_election(int msg_type, pid_t src, void *buf, size_t len)
393 {
394         struct subnet_record *subrec;
395
396         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
397                 struct work_record *work;
398                 for (work = subrec->workgrouplist; work; work = work->next) {
399                         if (strequal(work->work_group, global_myworkgroup)) {
400                                 work->needelection = True;
401                                 work->ElectionCount=0;
402                                 work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;
403                         }
404                 }
405         }
406 }