Same fix as went into 2.2 (I'm waiting for jerry to finish some code).
[jra/samba/.git] / source3 / nmbd / nmbd_elections.c
index ceef961d5e95e8fe7d50717091248b5b34cb03d9..ae225184951cbe353594586578238cba1717cbcd 100644 (file)
 
 #include "includes.h"
 
-extern int DEBUGLEVEL;
-
-extern pstring scope;
-
 extern pstring global_myname;
 extern fstring global_myworkgroup;
 
@@ -46,12 +42,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;
@@ -62,7 +58,7 @@ static void send_election_dgram(struct subnet_record *subrec, char *workgroup_na
   send_mailslot(False, BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
                 global_myname, 0,
                 workgroup_name, 0x1e,
-                subrec->bcast_ip, subrec->myip);
+                subrec->bcast_ip, subrec->myip, DGRAM_PORT);
 }
 
 /*******************************************************************
@@ -179,6 +175,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;
@@ -194,10 +191,10 @@ 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", namestr(&nmbname), subrec->subnet_name ));
+yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
           continue;
         }
 
@@ -217,6 +214,7 @@ yet registered on subnet %s\n", namestr(&nmbname), subrec->subnet_name ));
       }
     }
   }
+  END_PROFILE(run_elections);
 }
 
 /*******************************************************************
@@ -278,6 +276,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",
@@ -289,14 +288,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))
@@ -327,6 +326,8 @@ is not my workgroup.\n", work->work_group, subrec->subnet_name ));
         unbecome_local_master_browser(subrec, work, False);
     }
   }
+done:
+  END_PROFILE(election);
 }
 
 /****************************************************************************
@@ -365,10 +366,10 @@ 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", namestr(&nmbname), subrec->subnet_name ));
+yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
           continue;
         }
 
@@ -378,11 +379,29 @@ yet registered on subnet %s\n", namestr(&nmbname), subrec->subnet_name ));
         work->ElectionCount = 0;
         work->RunningElection = True;
         work->needelection = False;
-
-        /* Send a force election packet to begin. */
-        send_election_dgram(subrec, work->work_group, 0, 0, "");
       }
     }
   }
   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;
+                       }
+               }
+       }
+}