s4:lib/tevent: rename structs
[samba.git] / source4 / nbt_server / wins / winsclient.c
index cfb68a3aaf26928fa28b44307f7bcc116f81f646..2546a26279d031ff3e0f1f9e4c68c9b18392dcf6 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,
    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/>.
 */
 
 #include "includes.h"
 #include "nbt_server/nbt_server.h"
-#include "libcli/raw/libcliraw.h"
+#include "nbt_server/wins/winsserver.h"
 #include "libcli/composite/composite.h"
 #include "lib/events/events.h"
+#include "librpc/gen_ndr/ndr_nbt.h"
 #include "smbd/service_task.h"
+#include "param/param.h"
 
+static void nbtd_wins_refresh_handler(struct composite_context *c);
 
 /* we send WINS client requests using our primary network interface 
 */
@@ -37,19 +39,34 @@ static struct nbt_name_socket *wins_socket(struct nbtd_interface *iface)
 }
 
 
-static void nbtd_wins_refresh(struct event_context *ev, struct timed_event *te,
-                             struct timeval t, void *private);
+static void nbtd_wins_refresh(struct tevent_context *ev, struct tevent_timer *te,
+                             struct timeval t, void *private_data);
 
 /*
   retry a WINS name registration
 */
-static void nbtd_wins_register_retry(struct event_context *ev, struct timed_event *te,
-                                    struct timeval t, void *private)
+static void nbtd_wins_register_retry(struct tevent_context *ev, struct tevent_timer *te,
+                                    struct timeval t, void *private_data)
 {
-       struct nbtd_iface_name *iname = talloc_get_type(private, struct nbtd_iface_name);
+       struct nbtd_iface_name *iname = talloc_get_type(private_data, struct nbtd_iface_name);
        nbtd_winsclient_register(iname);
 }
 
+/*
+  start a timer to refresh this name
+*/
+static void nbtd_wins_start_refresh_timer(struct nbtd_iface_name *iname)
+{
+       uint32_t refresh_time;
+       uint32_t max_refresh_time = lp_parm_int(iname->iface->nbtsrv->task->lp_ctx, NULL, "nbtd", "max_refresh_time", 7200);
+
+       refresh_time = MIN(max_refresh_time, iname->ttl/2);
+       
+       event_add_timed(iname->iface->nbtsrv->task->event_ctx, 
+                       iname, 
+                       timeval_add(&iname->registration_time, refresh_time, 0),
+                       nbtd_wins_refresh, iname);
+}
 
 /*
   called when a wins name refresh has completed
@@ -58,7 +75,7 @@ static void nbtd_wins_refresh_handler(struct composite_context *c)
 {
        NTSTATUS status;
        struct nbt_name_refresh_wins io;
-       struct nbtd_iface_name *iname = talloc_get_type(c->async.private, 
+       struct nbtd_iface_name *iname = talloc_get_type(c->async.private_data
                                                        struct nbtd_iface_name);
        TALLOC_CTX *tmp_ctx = talloc_new(iname);
 
@@ -78,7 +95,7 @@ static void nbtd_wins_refresh_handler(struct composite_context *c)
                         nbt_name_string(tmp_ctx, &iname->name), nt_errstr(status)));
                talloc_free(tmp_ctx);
                return;
-       }       
+       }
 
        if (io.out.rcode != 0) {
                DEBUG(1,("WINS server %s rejected name refresh of %s - %s\n", 
@@ -94,16 +111,16 @@ static void nbtd_wins_refresh_handler(struct composite_context *c)
        /* success - start a periodic name refresh */
        iname->nb_flags |= NBT_NM_ACTIVE;
        if (iname->wins_server) {
-               talloc_free(iname->wins_server);
+               /*
+                * talloc_free() would generate a warning,
+                * so steal it into the tmp context
+                */
+               talloc_steal(tmp_ctx, iname->wins_server);
        }
        iname->wins_server = talloc_steal(iname, io.out.wins_server);
 
        iname->registration_time = timeval_current();
-       event_add_timed(iname->iface->nbtsrv->task->event_ctx, 
-                       iname,
-                       timeval_add(&iname->registration_time, iname->ttl/2, 0),
-                       nbtd_wins_refresh,
-                       iname);
+       nbtd_wins_start_refresh_timer(iname);
 
        talloc_free(tmp_ctx);
 }
@@ -112,10 +129,10 @@ static void nbtd_wins_refresh_handler(struct composite_context *c)
 /*
   refresh a WINS name registration
 */
-static void nbtd_wins_refresh(struct event_context *ev, struct timed_event *te,
-                             struct timeval t, void *private)
+static void nbtd_wins_refresh(struct tevent_context *ev, struct tevent_timer *te,
+                             struct timeval t, void *private_data)
 {
-       struct nbtd_iface_name *iname = talloc_get_type(private, struct nbtd_iface_name);
+       struct nbtd_iface_name *iname = talloc_get_type(private_data, struct nbtd_iface_name);
        struct nbtd_interface *iface = iname->iface;
        struct nbt_name_refresh_wins io;
        struct composite_context *c;
@@ -123,11 +140,17 @@ static void nbtd_wins_refresh(struct event_context *ev, struct timed_event *te,
 
        /* setup a wins name refresh request */
        io.in.name            = iname->name;
-       io.in.wins_servers    = str_list_make(tmp_ctx, iname->wins_server, NULL);
+       io.in.wins_servers    = (const char **)str_list_make(tmp_ctx, iname->wins_server, NULL);
+       io.in.wins_port       = lp_nbt_port(iface->nbtsrv->task->lp_ctx);
        io.in.addresses       = nbtd_address_list(iface, tmp_ctx);
        io.in.nb_flags        = iname->nb_flags;
        io.in.ttl             = iname->ttl;
 
+       if (!io.in.addresses) {
+               talloc_free(tmp_ctx);
+               return;
+       }
+
        c = nbt_name_refresh_wins_send(wins_socket(iface), &io);
        if (c == NULL) {
                talloc_free(tmp_ctx);
@@ -136,7 +159,7 @@ static void nbtd_wins_refresh(struct event_context *ev, struct timed_event *te,
        talloc_steal(c, io.in.addresses);
 
        c->async.fn = nbtd_wins_refresh_handler;
-       c->async.private = iname;
+       c->async.private_data = iname;
 
        talloc_free(tmp_ctx);
 }
@@ -149,7 +172,7 @@ static void nbtd_wins_register_handler(struct composite_context *c)
 {
        NTSTATUS status;
        struct nbt_name_register_wins io;
-       struct nbtd_iface_name *iname = talloc_get_type(c->async.private, 
+       struct nbtd_iface_name *iname = talloc_get_type(c->async.private_data
                                                        struct nbtd_iface_name);
        TALLOC_CTX *tmp_ctx = talloc_new(iname);
 
@@ -157,7 +180,7 @@ static void nbtd_wins_register_handler(struct composite_context *c)
        if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
                /* none of the WINS servers responded - try again 
                   periodically */
-               int wins_retry_time = lp_parm_int(-1, "nbtd", "wins_retry", 300);
+               int wins_retry_time = lp_parm_int(iname->iface->nbtsrv->task->lp_ctx, NULL, "nbtd", "wins_retry", 300);
                event_add_timed(iname->iface->nbtsrv->task->event_ctx, 
                                iname,
                                timeval_current_ofs(wins_retry_time, 0),
@@ -186,16 +209,16 @@ static void nbtd_wins_register_handler(struct composite_context *c)
        /* success - start a periodic name refresh */
        iname->nb_flags |= NBT_NM_ACTIVE;
        if (iname->wins_server) {
-               talloc_free(iname->wins_server);
+               /*
+                * talloc_free() would generate a warning,
+                * so steal it into the tmp context
+                */
+               talloc_steal(tmp_ctx, iname->wins_server);
        }
        iname->wins_server = talloc_steal(iname, io.out.wins_server);
 
        iname->registration_time = timeval_current();
-       event_add_timed(iname->iface->nbtsrv->task->event_ctx, 
-                       iname,
-                       timeval_add(&iname->registration_time, iname->ttl/2, 0),
-                       nbtd_wins_refresh,
-                       iname);
+       nbtd_wins_start_refresh_timer(iname);
 
        DEBUG(3,("Registered %s with WINS server %s\n",
                 nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
@@ -214,11 +237,16 @@ void nbtd_winsclient_register(struct nbtd_iface_name *iname)
 
        /* setup a wins name register request */
        io.in.name            = iname->name;
-       io.in.wins_servers    = lp_wins_server_list();
+       io.in.wins_port       = lp_nbt_port(iname->iface->nbtsrv->task->lp_ctx);
+       io.in.wins_servers    = lp_wins_server_list(iname->iface->nbtsrv->task->lp_ctx);
        io.in.addresses       = nbtd_address_list(iface, iname);
        io.in.nb_flags        = iname->nb_flags;
        io.in.ttl             = iname->ttl;
 
+       if (!io.in.addresses) {
+               return;
+       }
+
        c = nbt_name_register_wins_send(wins_socket(iface), &io);
        if (c == NULL) {
                talloc_free(io.in.addresses);
@@ -227,5 +255,5 @@ void nbtd_winsclient_register(struct nbtd_iface_name *iname)
        talloc_steal(c, io.in.addresses);
 
        c->async.fn = nbtd_wins_register_handler;
-       c->async.private = iname;
+       c->async.private_data = iname;
 }