Removed global_myworkgroup, global_myname, global_myscope. Added liberal
[bbaumbach/samba-autobuild/.git] / source / nmbd / nmbd_mynames.c
index 7ea8ffc9465697d02d6c6b8dc976d7836a3c7d51..dd668218395adeea0c135b9d490c776f26943788 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 char **my_netbios_names;
-extern fstring global_myworkgroup;
-
 extern uint16 samba_nb_type; /* Samba's NetBIOS type. */
 
 /****************************************************************************
@@ -54,10 +48,10 @@ void register_my_workgroup_one_subnet(struct subnet_record *subrec)
        struct work_record *work;
 
        /* Create the workgroup on the subnet. */
-       if((work = create_workgroup_on_subnet(subrec, global_myworkgroup
+       if((work = create_workgroup_on_subnet(subrec, lp_workgroup()
                                              PERMANENT_TTL)) == NULL) {
                DEBUG(0,("register_my_workgroup_and_names: Failed to create my workgroup %s on subnet %s. \
-Exiting.\n", global_myworkgroup, subrec->subnet_name));
+Exiting.\n", lp_workgroup(), subrec->subnet_name));
                return;
        }
 
@@ -66,14 +60,14 @@ Exiting.\n", global_myworkgroup, subrec->subnet_name));
        add_samba_names_to_subnet(subrec);
 
        /* Register all our names including aliases. */
-       for (i=0; my_netbios_names[i]; i++) {
-               register_name(subrec, my_netbios_names[i],0x20,samba_nb_type,
+       for (i=0; my_netbios_names(i); i++) {
+               register_name(subrec, my_netbios_names(i),0x20,samba_nb_type,
                              NULL,
                              my_name_register_failed, NULL);
-               register_name(subrec, my_netbios_names[i],0x03,samba_nb_type,
+               register_name(subrec, my_netbios_names(i),0x03,samba_nb_type,
                              NULL,
                              my_name_register_failed, NULL);
-               register_name(subrec, my_netbios_names[i],0x00,samba_nb_type,
+               register_name(subrec, my_netbios_names(i),0x00,samba_nb_type,
                              NULL,
                              my_name_register_failed, NULL);
        }
@@ -82,6 +76,35 @@ Exiting.\n", global_myworkgroup, subrec->subnet_name));
        initiate_myworkgroup_startup(subrec, work);
 }
 
+/*******************************************************************
+ Utility function to add a name to the unicast subnet, or add in
+ our IP address if it already exists.
+******************************************************************/
+
+static void insert_refresh_name_into_unicast( struct subnet_record *subrec,
+                                                struct nmb_name *nmbname, uint16 nb_type )
+{
+  struct name_record *namerec;
+
+  if (!we_are_a_wins_client()) {
+    insert_permanent_name_into_unicast(subrec, nmbname, nb_type);
+    return;
+  }
+
+  if((namerec = find_name_on_subnet(unicast_subnet, nmbname, FIND_SELF_NAME)) == NULL)
+  {
+    /* The name needs to be created on the unicast subnet. */
+    (void)add_name_to_subnet( unicast_subnet, nmbname->name,
+                              nmbname->name_type, nb_type,
+                              MIN(lp_max_ttl(), MAX_REFRESH_TIME), SELF_NAME, 1, &subrec->myip);
+  }
+  else
+  {
+    /* The name already exists on the unicast subnet. Add our local
+       IP for the given broadcast subnet to the name. */
+    add_ip_to_name_record( namerec, subrec->myip);
+  }
+}
 
 /****************************************************************************
   Add my workgroup and my given names to the subnet lists.
@@ -100,40 +123,15 @@ BOOL register_my_workgroup_and_names(void)
          register_my_workgroup_one_subnet(subrec);
   }
 
-  /* If we are not a WINS client, we still need to add the magic Samba
+  /* We still need to add the magic Samba
      names and the netbios names to the unicast subnet directly. This is
      to allow unicast node status requests and queries to still work
      in a broadcast only environment. */
 
-  if(we_are_a_wins_client() == False)
-  {
-    add_samba_names_to_subnet(unicast_subnet);
-
-    for (i=0; my_netbios_names[i]; i++)
-    {
-      for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
-      {
-        /*
-         * Ensure all the IP addresses are added if we are multihomed.
-         */
-        struct nmb_name nmbname;
-
-        make_nmb_name(&nmbname, my_netbios_names[i],0x20);
-        insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type);
-
-        make_nmb_name(&nmbname, my_netbios_names[i],0x3);
-        insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type);
-
-        make_nmb_name(&nmbname, my_netbios_names[i],0x0);
-        insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type);
-      }
-    }
-
-    /*
-     * Add the WORKGROUP<0> and WORKGROUP<1e> group names to the unicast subnet
-     * also for the same reasons.
-     */
+  add_samba_names_to_subnet(unicast_subnet);
 
+  for (i=0; my_netbios_names(i); i++)
+  {
     for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
     {
       /*
@@ -141,14 +139,36 @@ BOOL register_my_workgroup_and_names(void)
        */
       struct nmb_name nmbname;
 
-      make_nmb_name(&nmbname, global_myworkgroup, 0x0);
-      insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type|NB_GROUP);
+      make_nmb_name(&nmbname, my_netbios_names(i),0x20);
+      insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type);
+
+      make_nmb_name(&nmbname, my_netbios_names(i),0x3);
+      insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type);
 
-      make_nmb_name(&nmbname, global_myworkgroup, 0x1e);
-      insert_permanent_name_into_unicast(subrec, &nmbname, samba_nb_type|NB_GROUP);
+      make_nmb_name(&nmbname, my_netbios_names(i),0x0);
+      insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type);
     }
   }
 
+  /*
+   * Add the WORKGROUP<0> and WORKGROUP<1e> group names to the unicast subnet
+   * also for the same reasons.
+   */
+
+  for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
+  {
+    /*
+     * Ensure all the IP addresses are added if we are multihomed.
+     */
+    struct nmb_name nmbname;
+
+    make_nmb_name(&nmbname, lp_workgroup(), 0x0);
+    insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type|NB_GROUP);
+
+    make_nmb_name(&nmbname, lp_workgroup(), 0x1e);
+    insert_refresh_name_into_unicast(subrec, &nmbname, samba_nb_type|NB_GROUP);
+  }
+
   /*
    * We need to add the Samba names to the remote broadcast subnet,
    * as NT 4.x does directed broadcast requests to the *<0x0> name.
@@ -161,63 +181,48 @@ BOOL register_my_workgroup_and_names(void)
 /****************************************************************************
   Remove all the names we registered.
 **************************************************************************/
-
-void release_my_names(void)
+void release_wins_names(void)
 {
-  struct subnet_record *subrec;
-
-  for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
-  {
-    struct name_record *namerec, *nextnamerec;
-
-    for (namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
-         namerec;
-         namerec = nextnamerec)
-    {
-      nextnamerec = (struct name_record *)ubi_trNext( namerec );
-      if( (namerec->data.source == SELF_NAME)
-       && !NAME_IS_DEREGISTERING(namerec) )
-        release_name( subrec, namerec, standard_success_release,
-                      NULL, NULL);
-    }
-  }
+       struct subnet_record *subrec = unicast_subnet;
+       struct name_record *namerec, *nextnamerec;
+
+       for (namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
+            namerec;
+            namerec = nextnamerec) {
+               nextnamerec = (struct name_record *)ubi_trNext( namerec );
+               if( (namerec->data.source == SELF_NAME)
+                   && !NAME_IS_DEREGISTERING(namerec) )
+                       release_name( subrec, namerec, standard_success_release,
+                                     NULL, NULL);
+       }
 }
 
 /*******************************************************************
-  Refresh our registered names.
+  Refresh our registered names with WINS
   ******************************************************************/
-
 void refresh_my_names(time_t t)
 {
-  struct subnet_record *subrec;
-
-  for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
-  {
-    struct name_record *namerec;
-         
-    /* B nodes don't send out name refresh requests, see RFC 1001, 15.5.1 */
-    if (subrec != unicast_subnet)
-      continue;
-          
-    for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
-         namerec;
-         namerec = (struct name_record *)ubi_trNext( namerec ) )
-    {
-      /* Each SELF name has an individual time to be refreshed. */
-      if( (namerec->data.source == SELF_NAME)
-       && (namerec->data.refresh_time < t)
-       && ( namerec->data.death_time != PERMANENT_TTL) )
-      {
-        /* We cheat here and pretend the refresh is going to be
-           successful & update the refresh times. This stops
-           multiple refresh calls being done. We actually
-           deal with refresh failure in the fail_fn.
-         */
-        if( !is_refresh_already_queued( subrec, namerec) )
-          refresh_name( subrec, namerec, NULL, NULL, NULL );
-        namerec->data.death_time += lp_max_ttl();
-        namerec->data.refresh_time += MIN(lp_max_ttl(), MAX_REFRESH_TIME);
-      }
-    }
-  }
+       struct name_record *namerec;
+
+       if (wins_srv_count() < 1) return;
+
+       for (namerec = (struct name_record *)ubi_trFirst(unicast_subnet->namelist);
+            namerec;
+            namerec = (struct name_record *)ubi_trNext(namerec)) {
+               /* Each SELF name has an individual time to be refreshed. */
+               if ((namerec->data.source == SELF_NAME) &&
+                   (namerec->data.refresh_time < t) &&
+                   (namerec->data.death_time != PERMANENT_TTL)) {
+                       /* We cheat here and pretend the refresh is going to be
+                          successful & update the refresh times. This stops
+                          multiple refresh calls being done. We actually
+                          deal with refresh failure in the fail_fn.
+                       */
+                       if (!is_refresh_already_queued(unicast_subnet, namerec)) {
+                               wins_refresh_name(namerec);
+                       }
+                       namerec->data.death_time = t + lp_max_ttl();
+                       namerec->data.refresh_time = t + MIN(lp_max_ttl()/2, MAX_REFRESH_TIME);
+               }
+       }
 }