2 Unix SMB/CIFS implementation.
3 NBT netbios routines and daemon - version 2
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6 Copyright (C) Jeremy Allison 1994-1998
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.
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.
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.
26 extern uint16 samba_nb_type
; /* Samba's NetBIOS type. */
28 /****************************************************************************
29 Fail funtion when registering my netbios names.
30 **************************************************************************/
32 static void my_name_register_failed(struct subnet_record
*subrec
,
33 struct response_record
*rrec
, struct nmb_name
*nmbname
)
35 DEBUG(0,("my_name_register_failed: Failed to register my name %s on subnet %s.\n",
36 nmb_namestr(nmbname
), subrec
->subnet_name
));
40 /****************************************************************************
41 Add my workgroup and my given names to one subnet
42 Also add the magic Samba names.
43 **************************************************************************/
44 void register_my_workgroup_one_subnet(struct subnet_record
*subrec
)
48 struct work_record
*work
;
50 /* Create the workgroup on the subnet. */
51 if((work
= create_workgroup_on_subnet(subrec
, lp_workgroup(),
52 PERMANENT_TTL
)) == NULL
) {
53 DEBUG(0,("register_my_workgroup_and_names: Failed to create my workgroup %s on subnet %s. \
54 Exiting.\n", lp_workgroup(), subrec
->subnet_name
));
58 /* Each subnet entry, except for the wins_server_subnet has
59 the magic Samba names. */
60 add_samba_names_to_subnet(subrec
);
62 /* Register all our names including aliases. */
63 for (i
=0; my_netbios_names(i
); i
++) {
64 register_name(subrec
, my_netbios_names(i
),0x20,samba_nb_type
,
66 my_name_register_failed
, NULL
);
67 register_name(subrec
, my_netbios_names(i
),0x03,samba_nb_type
,
69 my_name_register_failed
, NULL
);
70 register_name(subrec
, my_netbios_names(i
),0x00,samba_nb_type
,
72 my_name_register_failed
, NULL
);
75 /* Initiate election processing, register the workgroup names etc. */
76 initiate_myworkgroup_startup(subrec
, work
);
79 /*******************************************************************
80 Utility function to add a name to the unicast subnet, or add in
81 our IP address if it already exists.
82 ******************************************************************/
84 static void insert_refresh_name_into_unicast( struct subnet_record
*subrec
,
85 struct nmb_name
*nmbname
, uint16 nb_type
)
87 struct name_record
*namerec
;
89 if (!we_are_a_wins_client()) {
90 insert_permanent_name_into_unicast(subrec
, nmbname
, nb_type
);
94 if((namerec
= find_name_on_subnet(unicast_subnet
, nmbname
, FIND_SELF_NAME
)) == NULL
)
96 /* The name needs to be created on the unicast subnet. */
97 (void)add_name_to_subnet( unicast_subnet
, nmbname
->name
,
98 nmbname
->name_type
, nb_type
,
99 MIN(lp_max_ttl(), MAX_REFRESH_TIME
), SELF_NAME
, 1, &subrec
->myip
);
103 /* The name already exists on the unicast subnet. Add our local
104 IP for the given broadcast subnet to the name. */
105 add_ip_to_name_record( namerec
, subrec
->myip
);
109 /****************************************************************************
110 Add my workgroup and my given names to the subnet lists.
111 Also add the magic Samba names.
112 **************************************************************************/
114 BOOL
register_my_workgroup_and_names(void)
116 struct subnet_record
*subrec
;
119 for(subrec
= FIRST_SUBNET
;
121 subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
))
123 register_my_workgroup_one_subnet(subrec
);
126 /* We still need to add the magic Samba
127 names and the netbios names to the unicast subnet directly. This is
128 to allow unicast node status requests and queries to still work
129 in a broadcast only environment. */
131 add_samba_names_to_subnet(unicast_subnet
);
133 for (i
=0; my_netbios_names(i
); i
++)
135 for(subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
))
138 * Ensure all the IP addresses are added if we are multihomed.
140 struct nmb_name nmbname
;
142 make_nmb_name(&nmbname
, my_netbios_names(i
),0x20);
143 insert_refresh_name_into_unicast(subrec
, &nmbname
, samba_nb_type
);
145 make_nmb_name(&nmbname
, my_netbios_names(i
),0x3);
146 insert_refresh_name_into_unicast(subrec
, &nmbname
, samba_nb_type
);
148 make_nmb_name(&nmbname
, my_netbios_names(i
),0x0);
149 insert_refresh_name_into_unicast(subrec
, &nmbname
, samba_nb_type
);
154 * Add the WORKGROUP<0> and WORKGROUP<1e> group names to the unicast subnet
155 * also for the same reasons.
158 for(subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
))
161 * Ensure all the IP addresses are added if we are multihomed.
163 struct nmb_name nmbname
;
165 make_nmb_name(&nmbname
, lp_workgroup(), 0x0);
166 insert_refresh_name_into_unicast(subrec
, &nmbname
, samba_nb_type
|NB_GROUP
);
168 make_nmb_name(&nmbname
, lp_workgroup(), 0x1e);
169 insert_refresh_name_into_unicast(subrec
, &nmbname
, samba_nb_type
|NB_GROUP
);
173 * We need to add the Samba names to the remote broadcast subnet,
174 * as NT 4.x does directed broadcast requests to the *<0x0> name.
176 add_samba_names_to_subnet(remote_broadcast_subnet
);
181 /****************************************************************************
182 Remove all the names we registered.
183 **************************************************************************/
184 void release_wins_names(void)
186 struct subnet_record
*subrec
= unicast_subnet
;
187 struct name_record
*namerec
, *nextnamerec
;
189 for (namerec
= (struct name_record
*)ubi_trFirst( subrec
->namelist
);
191 namerec
= nextnamerec
) {
192 nextnamerec
= (struct name_record
*)ubi_trNext( namerec
);
193 if( (namerec
->data
.source
== SELF_NAME
)
194 && !NAME_IS_DEREGISTERING(namerec
) )
195 release_name( subrec
, namerec
, standard_success_release
,
200 /*******************************************************************
201 Refresh our registered names with WINS
202 ******************************************************************/
203 void refresh_my_names(time_t t
)
205 struct name_record
*namerec
;
207 if (wins_srv_count() < 1) return;
209 for (namerec
= (struct name_record
*)ubi_trFirst(unicast_subnet
->namelist
);
211 namerec
= (struct name_record
*)ubi_trNext(namerec
)) {
212 /* Each SELF name has an individual time to be refreshed. */
213 if ((namerec
->data
.source
== SELF_NAME
) &&
214 (namerec
->data
.refresh_time
< t
) &&
215 (namerec
->data
.death_time
!= PERMANENT_TTL
)) {
216 /* We cheat here and pretend the refresh is going to be
217 successful & update the refresh times. This stops
218 multiple refresh calls being done. We actually
219 deal with refresh failure in the fail_fn.
221 if (!is_refresh_already_queued(unicast_subnet
, namerec
)) {
222 wins_refresh_name(namerec
);
224 namerec
->data
.death_time
= t
+ lp_max_ttl();
225 namerec
->data
.refresh_time
= t
+ MIN(lp_max_ttl()/2, MAX_REFRESH_TIME
);