Merge branch 'v3-2-test' of ssh://git.samba.org/data/git/samba into v3-2-test
[ira/wip.git] / source3 / nmbd / nmbd_elections.c
index d4d6da081a680076c55ba25e5a5f1c4f59fda70c..b50d215b91d57c37b5287ceb9f076782c932561a 100644 (file)
@@ -7,7 +7,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -16,8 +16,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
 */
 
@@ -33,8 +32,8 @@ extern time_t StartupTime;
 static void send_election_dgram(struct subnet_record *subrec, const char *workgroup_name,
                                 uint32 criterion, int timeup,const char *server_name)
 {
-       pstring outbuf;
-       fstring srv_name;
+       char outbuf[1024];
+       unstring srv_name;
        char *p;
 
        DEBUG(2,("send_election_dgram: Sending election packet for workgroup %s on subnet %s\n",
@@ -49,12 +48,12 @@ static void send_election_dgram(struct subnet_record *subrec, const char *workgr
        SIVAL(p,1,criterion);
        SIVAL(p,5,timeup*1000); /* ms - Despite what the spec says. */
        p += 13;
-       fstrcpy(srv_name, server_name);
+       unstrcpy(srv_name, server_name);
        strupper_m(srv_name);
        /* The following call does UNIX -> DOS charset conversion. */
-       pstrcpy_base(p, srv_name, outbuf);
-       p = skip_string(p,1);
-  
+       push_ascii(p, srv_name, sizeof(outbuf)-PTR_DIFF(p,outbuf)-1, STR_TERMINATE);
+       p = skip_string(outbuf,sizeof(outbuf),p);
+
        send_mailslot(False, BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
                global_myname(), 0,
                workgroup_name, 0x1e,
@@ -70,8 +69,8 @@ static void check_for_master_browser_success(struct subnet_record *subrec,
                                  struct nmb_name *answer_name,
                                  struct in_addr answer_ip, struct res_rec *rrec)
 {
-       nstring aname;
-       pull_ascii_nstring(aname, answer_name->name);
+       unstring aname;
+       pull_ascii_nstring(aname, sizeof(aname), answer_name->name);
        DEBUG(3,("check_for_master_browser_success: Local master browser for workgroup %s exists at \
 IP %s (just checking).\n", aname, inet_ntoa(answer_ip) ));
 }
@@ -85,10 +84,10 @@ static void check_for_master_browser_fail( struct subnet_record *subrec,
                                            struct nmb_name *question_name,
                                            int fail_code)
 {
-       nstring workgroup_name;
+       unstring workgroup_name;
        struct work_record *work;
 
-       pull_ascii_nstring(workgroup_name,question_name->name);
+       pull_ascii_nstring(workgroup_name,sizeof(workgroup_name),question_name->name);
 
        work = find_workgroup_on_subnet(subrec, workgroup_name);
        if(work == NULL) {
@@ -166,13 +165,16 @@ void run_elections(time_t t)
   
        struct subnet_record *subrec;
   
+       START_PROFILE(run_elections);
+
        /* Send election packets once every 2 seconds - note */
-       if (lastime && (t - lastime < 2))
+       if (lastime && (t - lastime < 2)) {
+               END_PROFILE(run_elections);
                return;
+       }
   
        lastime = t;
   
-       START_PROFILE(run_elections);
        for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
                struct work_record *work;
 
@@ -214,7 +216,7 @@ yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
   Determine if I win an election.
 ******************************************************************/
 
-static BOOL win_election(struct work_record *work, int version,
+static bool win_election(struct work_record *work, int version,
                          uint32 criterion, int timeup, const char *server_name)
 {  
        int mytimeup = time(NULL) - StartupTime;
@@ -247,7 +249,7 @@ static BOOL win_election(struct work_record *work, int version,
        if (timeup < mytimeup)
                return(True);
 
-       if (strcasecmp(global_myname(), server_name) > 0)
+       if (StrCaseCmp(global_myname(), server_name) > 0)
                return(False);
   
        return(True);
@@ -263,14 +265,15 @@ void process_election(struct subnet_record *subrec, struct packet_struct *p, cha
        int version = CVAL(buf,0);
        uint32 criterion = IVAL(buf,1);
        int timeup = IVAL(buf,5)/1000;
-       nstring server_name;
+       unstring server_name;
        struct work_record *work;
-       nstring workgroup_name;
-
-       pull_ascii_nstring(server_name, buf+13);
-       pull_ascii_nstring(workgroup_name, dgram->dest_name.name);
+       unstring workgroup_name;
 
        START_PROFILE(election);
+
+       pull_ascii_nstring(server_name, sizeof(server_name), buf+13);
+       pull_ascii_nstring(workgroup_name, sizeof(workgroup_name), dgram->dest_name.name);
+
        server_name[15] = 0;  
 
        DEBUG(3,("process_election: Election request from %s at IP %s on subnet %s for workgroup %s.\n",
@@ -325,15 +328,17 @@ done:
   be done by run_elections().
 ***************************************************************************/
 
-BOOL check_elections(void)
+bool check_elections(void)
 {
        struct subnet_record *subrec;
-       BOOL run_any_election = False;
+       bool run_any_election = False;
 
        for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
                struct work_record *work;
                for (work = subrec->workgrouplist; work; work = work->next) {
-                       run_any_election |= work->RunningElection;
+                       if (work->RunningElection) {
+                               run_any_election = work->RunningElection;
+                       }
 
                        /* 
                         * Start an election if we have any chance of winning.
@@ -374,7 +379,11 @@ yet registered on subnet %s\n", nmb_namestr(&nmbname), subrec->subnet_name ));
  Process a internal Samba message forcing an election.
 ***************************************************************************/
 
-void nmbd_message_election(int msg_type, pid_t src, void *buf, size_t len)
+void nmbd_message_election(struct messaging_context *msg,
+                          void *private_data,
+                          uint32_t msg_type,
+                          struct server_id server_id,
+                          DATA_BLOB *data)
 {
        struct subnet_record *subrec;