Removed version number from file header.
[ira/wip.git] / source3 / nmbd / nmbd_elections.c
index 8f876eab0c531737fea94f560c1f178806f7e2ae..acff7a72e80706ad9c048634c0f64956bf3e0fdd 100644 (file)
@@ -1,6 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    NBT netbios routines and daemon - version 2
    Copyright (C) Andrew Tridgell 1994-1998
    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
 
 #include "includes.h"
 
-extern int DEBUGLEVEL;
-
-extern pstring scope;
-
 extern pstring global_myname;
 extern fstring global_myworkgroup;
 
@@ -46,12 +41,12 @@ static void send_election_dgram(struct subnet_record *subrec, char *workgroup_na
   DEBUG(2,("send_election_dgram: Sending election packet for workgroup %s on subnet %s\n",
        workgroup_name, subrec->subnet_name ));
 
-  bzero(outbuf,sizeof(outbuf));
+  memset(outbuf,'\0',sizeof(outbuf));
   p = outbuf;
-  CVAL(p,0) = ANN_Election; /* Election opcode. */
+  SCVAL(p,0,ANN_Election); /* Election opcode. */
   p++;
 
-  CVAL(p,0) = (criterion == 0 && timeup == 0) ? 0 : ELECTION_VERSION;
+  SCVAL(p,0,((criterion == 0 && timeup == 0) ? 0 : ELECTION_VERSION));
   SIVAL(p,1,criterion);
   SIVAL(p,5,timeup*1000); /* ms - Despite what the spec says. */
   p += 13;
@@ -135,6 +130,9 @@ void check_master_browser_exists(time_t t)
   struct subnet_record *subrec;
   char *workgroup_name = global_myworkgroup;
 
+  if (!lastrun)
+    lastrun = t;
+
   if (t < (lastrun + (CHECK_TIME_MST_BROWSE * 60)))
     return;
 
@@ -176,6 +174,7 @@ void run_elections(time_t t)
   
   lastime = t;
   
+  START_PROFILE(run_elections);
   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
   {
     struct work_record *work;
@@ -191,7 +190,7 @@ void run_elections(time_t t)
          */
         struct nmb_name nmbname;
 
-        make_nmb_name(&nmbname, work->work_group, 0x1e, scope);
+        make_nmb_name(&nmbname, work->work_group, 0x1e);
         if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
           DEBUG(8,("run_elections: Cannot send election packet yet as name %s not \
 yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
@@ -214,6 +213,7 @@ yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
       }
     }
   }
+  END_PROFILE(run_elections);
 }
 
 /*******************************************************************
@@ -275,6 +275,7 @@ void process_election(struct subnet_record *subrec, struct packet_struct *p, cha
   struct work_record *work;
   char *workgroup_name = dgram->dest_name.name;
 
+  START_PROFILE(election);
   server_name[15] = 0;  
 
   DEBUG(3,("process_election: Election request from %s at IP %s on subnet %s for workgroup %s.\n",
@@ -286,14 +287,14 @@ void process_election(struct subnet_record *subrec, struct packet_struct *p, cha
   {
     DEBUG(0,("process_election: Cannot find workgroup %s on subnet %s.\n",
           workgroup_name, subrec->subnet_name ));
-    return;
+    goto done;
   }
 
   if (!strequal(work->work_group, global_myworkgroup))
   {
     DEBUG(3,("process_election: ignoring election request for workgroup %s on subnet %s as this \
 is not my workgroup.\n", work->work_group, subrec->subnet_name ));
-    return;
+    goto done;
   }
 
   if (win_election(work, version,criterion,timeup,server_name))
@@ -324,6 +325,8 @@ is not my workgroup.\n", work->work_group, subrec->subnet_name ));
         unbecome_local_master_browser(subrec, work, False);
     }
   }
+done:
+  END_PROFILE(election);
 }
 
 /****************************************************************************
@@ -362,7 +365,7 @@ BOOL check_elections(void)
          */
         struct nmb_name nmbname;
 
-        make_nmb_name(&nmbname, work->work_group, 0x1e, scope);
+        make_nmb_name(&nmbname, work->work_group, 0x1e);
         if(find_name_on_subnet( subrec, &nmbname, FIND_SELF_NAME)==NULL) {
           DEBUG(8,("check_elections: Cannot send election packet yet as name %s not \
 yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
@@ -380,3 +383,24 @@ yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
   }
   return run_any_election;
 }
+
+
+
+/****************************************************************************
+process a internal Samba message forcing an election
+***************************************************************************/
+void nmbd_message_election(int msg_type, pid_t src, void *buf, size_t len)
+{
+       struct subnet_record *subrec;
+
+       for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
+               struct work_record *work;
+               for (work = subrec->workgrouplist; work; work = work->next) {
+                       if (strequal(work->work_group, global_myworkgroup)) {
+                               work->needelection = True;
+                               work->ElectionCount=0;
+                               work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;
+                       }
+               }
+       }
+}