r5121: added periodic name refresh requests for all our registered names, reporting any
authorAndrew Tridgell <tridge@samba.org>
Mon, 31 Jan 2005 06:55:25 +0000 (06:55 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:09:24 +0000 (13:09 -0500)
name conflicts

source/include/structs.h
source/libcli/config.mk
source/libcli/nbt/libnbt.h
source/libcli/nbt/namequery.c
source/libcli/nbt/namerefresh.c [new file with mode: 0644]
source/libcli/nbt/nameregister.c
source/nbt_server/register.c

index 9c1a1e329fde265bb98cb47602baf54b7ed74675..0027cf7e61a238aa2d7d5e8c4c36ade863fc3661 100644 (file)
@@ -151,6 +151,7 @@ struct nbt_name_socket;
 struct nbt_name_query;
 struct nbt_name_status;
 struct nbt_name_register;
+struct nbt_name_refresh;
 struct nbt_name_register_bcast;
 
 struct messaging_context;
index d3aa4ff5ab293848fab4537ca069d1603f4a9d60..8458eb780d59416481b9e839d236407ca3b6d056 100644 (file)
@@ -29,7 +29,8 @@ ADD_OBJ_FILES = \
        libcli/nbt/nbtname.o \
        libcli/nbt/nbtsocket.o \
        libcli/nbt/namequery.o \
-       libcli/nbt/nameregister.o
+       libcli/nbt/nameregister.o \
+       libcli/nbt/namerefresh.o
 REQUIRED_SUBSYSTEMS = LIBNDR_RAW NDR_NBT SOCKET LIBCLI_COMPOSITE_BASE
 
 [SUBSYSTEM::LIBCLI_RESOLVE]
index 35eff9961c747ab516bdb4f113ae54b4de6080f1..114d6d3b92d9d1261858161f461cb9c794d816b0 100644 (file)
@@ -169,3 +169,23 @@ struct nbt_name_register_bcast {
                uint32_t ttl;
        } in;
 };
+
+
+/* a name refresh request */
+struct nbt_name_refresh {
+       struct {
+               struct nbt_name name;
+               const char *dest_addr;
+               const char *address;
+               uint16_t nb_flags;
+               BOOL broadcast;
+               uint32_t ttl;
+               int timeout; /* in seconds */
+       } in;
+       struct {
+               const char *reply_from;
+               struct nbt_name name;
+               const char *reply_addr;
+               uint8_t rcode;
+       } out;
+};
index f6744e9f147400ffc33ddc56cb8e2bf0843d8a48..36fbfc8dd0b8b81fb73d23396d46fba11adf485f 100644 (file)
@@ -56,8 +56,7 @@ struct nbt_name_request *nbt_name_query_send(struct nbt_name_socket *nbtsock,
                                    timeval_current_ofs(io->in.timeout, 0), False);
        if (req == NULL) goto failed;
 
-       talloc_steal(req, packet);
-
+       talloc_free(packet);
        return req;
 
 failed:
@@ -157,8 +156,7 @@ struct nbt_name_request *nbt_name_status_send(struct nbt_name_socket *nbtsock,
                                    timeval_current_ofs(io->in.timeout, 0), False);
        if (req == NULL) goto failed;
 
-       talloc_steal(req, packet);
-
+       talloc_free(packet);
        return req;
 
 failed:
diff --git a/source/libcli/nbt/namerefresh.c b/source/libcli/nbt/namerefresh.c
new file mode 100644 (file)
index 0000000..47fcae4
--- /dev/null
@@ -0,0 +1,138 @@
+/* 
+   Unix SMB/CIFS implementation.
+
+   send out a name refresh request
+
+   Copyright (C) Andrew Tridgell 2005
+   
+   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
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   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.
+*/
+
+#include "includes.h"
+#include "libcli/nbt/libnbt.h"
+#include "libcli/raw/libcliraw.h"
+#include "libcli/composite/composite.h"
+#include "system/network.h"
+
+/*
+  send a nbt name refresh request
+*/
+struct nbt_name_request *nbt_name_refresh_send(struct nbt_name_socket *nbtsock,
+                                              struct nbt_name_refresh *io)
+{
+       struct nbt_name_request *req;
+       struct nbt_name_packet *packet;
+
+       packet = talloc_zero(nbtsock, struct nbt_name_packet);
+       if (packet == NULL) return NULL;
+
+       packet->qdcount = 1;
+       packet->arcount = 1;
+       packet->operation = NBT_OPCODE_REFRESH;
+       if (io->in.broadcast) {
+               packet->operation |= NBT_FLAG_BROADCAST;
+       }
+
+       packet->questions = talloc_array(packet, struct nbt_name_question, 1);
+       if (packet->questions == NULL) goto failed;
+
+       packet->questions[0].name           = io->in.name;
+       packet->questions[0].question_type  = NBT_QTYPE_NETBIOS;
+       packet->questions[0].question_class = NBT_QCLASS_IP;
+
+       packet->additional = talloc_array(packet, struct nbt_res_rec, 1);
+       if (packet->additional == NULL) goto failed;
+
+       packet->additional[0].name                   = io->in.name;
+       packet->additional[0].rr_type                = NBT_QTYPE_NETBIOS;
+       packet->additional[0].rr_class               = NBT_QCLASS_IP;
+       packet->additional[0].ttl                    = io->in.ttl;
+       packet->additional[0].rdata.netbios.length   = 6;
+       packet->additional[0].rdata.netbios.addresses = talloc_array(packet->additional,
+                                                                    struct nbt_rdata_address, 1);
+       if (packet->additional[0].rdata.netbios.addresses == NULL) goto failed;
+       packet->additional[0].rdata.netbios.addresses[0].nb_flags = io->in.nb_flags;
+       packet->additional[0].rdata.netbios.addresses[0].ipaddr   = htonl(inet_addr(io->in.address));
+       
+       req = nbt_name_request_send(nbtsock, io->in.dest_addr, lp_nbt_port(), packet,
+                                   timeval_current_ofs(io->in.timeout, 0), False);
+       if (req == NULL) goto failed;
+
+       talloc_free(packet);
+       return req;
+
+failed:
+       talloc_free(packet);
+       return NULL;    
+}
+
+/*
+  wait for a refresh reply
+*/
+NTSTATUS nbt_name_refresh_recv(struct nbt_name_request *req, 
+                               TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
+{
+       NTSTATUS status;
+       struct nbt_name_packet *packet;
+       const char *addr;
+       struct in_addr in;
+
+       status = nbt_name_request_recv(req);
+       if (!NT_STATUS_IS_OK(status) ||
+           req->num_replies == 0) {
+               talloc_free(req);
+               return status;
+       }
+       
+       packet = req->replies[0].packet;
+       io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].reply_addr);
+
+       if (packet->ancount != 1 ||
+           packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
+           packet->answers[0].rr_class != NBT_QCLASS_IP) {
+               talloc_free(req);
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+
+       io->out.rcode = packet->operation & NBT_RCODE;
+       io->out.name = packet->answers[0].name;
+       if (packet->answers[0].rdata.netbios.length < 6) {
+               talloc_free(req);
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+       in.s_addr = htonl(packet->answers[0].rdata.netbios.addresses[0].ipaddr);
+       addr = inet_ntoa(in);
+       if (addr == NULL) {
+               talloc_free(req);
+               return NT_STATUS_NO_MEMORY;
+       }
+       io->out.reply_addr = talloc_strdup(mem_ctx, addr);
+       talloc_steal(mem_ctx, io->out.name.name);
+       talloc_steal(mem_ctx, io->out.name.scope);
+           
+       talloc_free(req);
+
+       return NT_STATUS_OK;
+}
+
+/*
+  synchronous name refresh request
+*/
+NTSTATUS nbt_name_refresh(struct nbt_name_socket *nbtsock, 
+                          TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
+{
+       struct nbt_name_request *req = nbt_name_refresh_send(nbtsock, io);
+       return nbt_name_refresh_recv(req, mem_ctx, io);
+}
index 6d4938cd1944e0ca962c8715e1089121541b3206..703210cb4888107fcb76e405a2da4534c48d555b 100644 (file)
@@ -73,8 +73,7 @@ struct nbt_name_request *nbt_name_register_send(struct nbt_name_socket *nbtsock,
                                    timeval_current_ofs(io->in.timeout, 0), False);
        if (req == NULL) goto failed;
 
-       talloc_steal(req, packet);
-
+       talloc_free(packet);
        return req;
 
 failed:
index e5033f4f5eb5176d90572e607ad174af9dde3802..8890030d5f10eda7946af5668c12e7367228bb08 100644 (file)
 */
 
 #include "includes.h"
+#include "events.h"
 #include "dlinklist.h"
 #include "nbt_server/nbt_server.h"
+#include "smbd/service_task.h"
 #include "libcli/raw/libcliraw.h"
 #include "libcli/composite/composite.h"
 
+
+static void nbt_start_refresh_timer(struct nbt_iface_name *iname);
+
+/*
+  a name refresh request has completed
+*/
+static void refresh_completion_handler(struct nbt_name_request *req)
+{
+       struct nbt_iface_name *iname = talloc_get_type(req->async.private, struct nbt_iface_name);
+       NTSTATUS status;
+       struct nbt_name_refresh io;
+       TALLOC_CTX *tmp_ctx = talloc_new(iname);
+
+       status = nbt_name_refresh_recv(req, tmp_ctx, &io);
+       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
+               DEBUG(4,("Refreshed name %s<%02x> on %s\n", 
+                        iname->name.name, iname->name.type, iname->iface->ip_address));
+               iname->registration_time = timeval_current();
+               nbt_start_refresh_timer(iname);
+               talloc_free(tmp_ctx);
+               return;
+       }
+
+       iname->nb_flags |= NBT_NM_CONFLICT;
+       iname->nb_flags &= ~NBT_NM_ACTIVE;
+
+       if (NT_STATUS_IS_OK(status)) {
+               DEBUG(1,("Name conflict from %s refreshing name %s<%02x> on %s - rcode %d\n", 
+                        io.out.reply_addr, iname->name.name, iname->name.type, 
+                        iname->iface->ip_address, io.out.rcode));
+       } else {
+               DEBUG(1,("Error refreshing name %s<%02x> on %s - %s\n", 
+                        iname->name.name, iname->name.type, iname->iface->ip_address,
+                        nt_errstr(status)));
+       }
+
+       talloc_free(tmp_ctx);
+}
+
+
+/*
+  handle name refresh timer events
+*/
+static void name_refresh_handler(struct event_context *ev, struct timed_event *te, 
+                                struct timeval t)
+{
+       struct nbt_iface_name *iname = talloc_get_type(te->private, struct nbt_iface_name);
+       struct nbt_interface *iface = iname->iface;
+       struct nbt_name_refresh io;
+       struct nbt_name_request *req;
+
+       /* setup a name refresh request */
+       io.in.name            = iname->name;
+       io.in.dest_addr       = iface->bcast_address;
+       io.in.address         = iface->ip_address;
+       io.in.nb_flags        = iname->nb_flags;
+       io.in.ttl             = iname->ttl;
+       io.in.broadcast       = True;
+       io.in.timeout         = 3;
+
+       req = nbt_name_refresh_send(iface->nbtsock, &io);
+       if (req == NULL) return;
+
+       req->async.fn = refresh_completion_handler;
+       req->async.private = iname;
+}
+
+
 /*
   start a timer to refresh this name
 */
 static void nbt_start_refresh_timer(struct nbt_iface_name *iname)
 {
+       struct timed_event te;
+       uint32_t refresh_time;
+       uint32_t max_refresh_time = lp_parm_int(-1, "nbtd", "max_refresh_time", 7200);
+
+       refresh_time = MIN(max_refresh_time, iname->ttl/2);
+       
+       te.next_event = timeval_current_ofs(refresh_time, 0);
+       te.handler    = name_refresh_handler;
+       te.private    = iname;
+
+       event_add_timed(iname->iface->nbtsrv->task->event_ctx, &te, iname);
 }
 
 
@@ -48,6 +129,7 @@ static void nbt_register_handler(struct smbcli_composite *req)
                iname->nb_flags |= NBT_NM_ACTIVE;
                DEBUG(3,("Registered %s<%02x> on interface %s\n",
                         iname->name.name, iname->name.type, iname->iface->bcast_address));
+               iname->registration_time = timeval_current();
                nbt_start_refresh_timer(iname);
                return;
        }