r5117: used a composite function to add 4 stage name registration. We send 3
[samba.git] / source / nbt_server / register.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    register our names
5
6    Copyright (C) Andrew Tridgell        2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "dlinklist.h"
25 #include "nbt_server/nbt_server.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "libcli/composite/composite.h"
28
29 /*
30   start a timer to refresh this name
31 */
32 static void nbt_start_refresh_timer(struct nbt_iface_name *iname)
33 {
34 }
35
36
37 /*
38   a name registration has completed
39 */
40 static void nbt_register_handler(struct smbcli_composite *req)
41 {
42         struct nbt_iface_name *iname = talloc_get_type(req->async.private, struct nbt_iface_name);
43         NTSTATUS status;
44
45         status = nbt_name_register_bcast_recv(req);
46         if (NT_STATUS_IS_OK(status)) {
47                 /* good - nobody complained about our registration */
48                 iname->nb_flags |= NBT_NM_ACTIVE;
49                 DEBUG(3,("Registered %s<%02x> on interface %s\n",
50                          iname->name.name, iname->name.type, iname->iface->bcast_address));
51                 nbt_start_refresh_timer(iname);
52                 return;
53         }
54
55         /* someone must have replied with an objection! */
56         iname->nb_flags |= NBT_NM_CONFLICT;
57
58         DEBUG(1,("Error registering %s<%02x> on interface %s - %s\n",
59                  iname->name.name, iname->name.type, iname->iface->bcast_address,
60                  nt_errstr(status)));
61 }
62
63
64 /*
65   register a name on a network interface
66 */
67 static void nbt_register_name_iface(struct nbt_interface *iface,
68                                     const char *name, enum nbt_name_type type,
69                                     uint16_t nb_flags)
70 {
71         struct nbt_iface_name *iname;
72         const char *scope = lp_netbios_scope();
73         struct nbt_name_register_bcast io;
74         struct smbcli_composite *req;
75
76         iname = talloc(iface, struct nbt_iface_name);
77         if (!iname) return;
78
79         iname->iface     = iface;
80         iname->name.name = talloc_strdup(iname, name);
81         iname->name.type = type;
82         if (scope && *scope) {
83                 iname->name.scope = talloc_strdup(iname, scope);
84         } else {
85                 iname->name.scope = NULL;
86         }
87         iname->nb_flags          = nb_flags;
88         iname->ttl               = lp_parm_int(-1, "nbtd", "bcast_ttl", 300000);
89         iname->registration_time = timeval_zero();
90
91         DLIST_ADD(iface->names, iname);
92
93         if (nb_flags & NBT_NM_PERMANENT) {
94                 /* permanent names are not announced and are immediately active */
95                 iname->nb_flags |= NBT_NM_ACTIVE;
96                 iname->ttl       = 0;
97                 return;
98         }
99
100         /* setup a broadcast name registration request */
101         io.in.name            = iname->name;
102         io.in.dest_addr       = iface->bcast_address;
103         io.in.address         = iface->ip_address;
104         io.in.nb_flags        = nb_flags;
105         io.in.ttl             = iname->ttl;
106
107         req = nbt_name_register_bcast_send(iface->nbtsock, &io);
108         if (req == NULL) return;
109
110         req->async.fn = nbt_register_handler;
111         req->async.private = iname;
112 }
113
114
115 /*
116   register one name on all our interfaces
117 */
118 static void nbt_register_name(struct nbt_server *nbtsrv, 
119                               const char *name, enum nbt_name_type type,
120                               uint16_t nb_flags)
121 {
122         struct nbt_interface *iface;
123         
124         /* register with all the local interfaces */
125         for (iface=nbtsrv->interfaces;iface;iface=iface->next) {
126                 nbt_register_name_iface(iface, name, type, nb_flags);
127         }
128
129         /* register on our general broadcast interface as a permanent name */
130         nbt_register_name_iface(nbtsrv->bcast_interface, name, type, nb_flags | NBT_NM_PERMANENT);
131
132         /* TODO: register with our WINS servers */
133 }
134
135
136 /*
137   register our names on all interfaces
138 */
139 void nbt_register_names(struct nbt_server *nbtsrv)
140 {
141         uint16_t nb_flags = NBT_NODE_M;
142
143         /* note that we don't initially mark the names "ACTIVE". They are 
144            marked active once registration is successful */
145         nbt_register_name(nbtsrv, lp_netbios_name(), NBT_NAME_CLIENT, nb_flags);
146         nbt_register_name(nbtsrv, lp_netbios_name(), NBT_NAME_USER,   nb_flags);
147         nbt_register_name(nbtsrv, lp_netbios_name(), NBT_NAME_SERVER, nb_flags);
148         nbt_register_name(nbtsrv, lp_workgroup(),    NBT_NAME_CLIENT, nb_flags | NBT_NM_GROUP);
149         nbt_register_name(nbtsrv, lp_workgroup(),    NBT_NAME_SERVER, nb_flags | NBT_NM_GROUP);
150         nbt_register_name(nbtsrv, "__SAMBA__",       NBT_NAME_CLIENT, nb_flags | NBT_NM_PERMANENT);
151         nbt_register_name(nbtsrv, "__SAMBA__",       NBT_NAME_SERVER, nb_flags | NBT_NM_PERMANENT);
152 }