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 3 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, see <http://www.gnu.org/licenses/>.
25 /****************************************************************************
26 Deal with a response packet when releasing one of our names.
27 ****************************************************************************/
29 static void release_name_response(struct subnet_record
*subrec
,
30 struct response_record
*rrec
, struct packet_struct
*p
)
33 * If we are releasing broadcast, then getting a response is an
34 * error. If we are releasing unicast, then we expect to get a response.
36 struct nmb_packet
*nmb
= &p
->packet
.nmb
;
37 bool bcast
= nmb
->header
.nm_flags
.bcast
;
39 struct nmb_name
*question_name
= &rrec
->packet
->packet
.nmb
.question
.question_name
;
40 struct nmb_name
*answer_name
= &nmb
->answers
->rr_name
;
41 struct in_addr released_ip
;
43 /* Sanity check. Ensure that the answer name in the incoming packet is the
44 same as the requested name in the outgoing packet. */
45 if (!nmb_name_equal(question_name
, answer_name
)) {
46 DEBUG(0,("release_name_response: Answer name %s differs from question name %s.\n",
47 nmb_namestr(answer_name
), nmb_namestr(question_name
)));
52 /* Someone sent a response to a bcast release? ignore it. */
56 /* Unicast - check to see if the response allows us to release the name. */
57 if (nmb
->header
.rcode
!= 0) {
58 /* Error code - we were told not to release the name ! What now ! */
61 DEBUG(0,("release_name_response: WINS server at IP %s rejected our \
62 name release of name %s with error code %d.\n",
64 nmb_namestr(answer_name
), nmb
->header
.rcode
));
65 } else if (nmb
->header
.opcode
== NMB_WACK_OPCODE
) {
66 /* WINS server is telling us to wait. Pretend we didn't get
67 the response but don't send out any more release requests. */
69 DEBUG(5,("release_name_response: WACK from WINS server %s in releasing \
70 name %s on subnet %s.\n",
71 inet_ntoa(p
->ip
), nmb_namestr(answer_name
), subrec
->subnet_name
));
73 rrec
->repeat_count
= 0;
74 /* How long we should wait for. */
75 rrec
->repeat_time
= p
->timestamp
+ nmb
->answers
->ttl
;
80 DEBUG(5,("release_name_response: %s in releasing name %s on subnet %s.\n",
81 success
? "success" : "failure", nmb_namestr(answer_name
), subrec
->subnet_name
));
83 putip((char*)&released_ip
,&nmb
->answers
->rdata
[2]);
86 (*(release_name_success_function
)rrec
->success_fn
)(subrec
, rrec
->userdata
, answer_name
, released_ip
);
87 standard_success_release( subrec
, rrec
->userdata
, answer_name
, released_ip
);
89 /* We have no standard_fail_release - maybe we should add one ? */
91 (*(release_name_fail_function
)rrec
->fail_fn
)(subrec
, rrec
, answer_name
);
95 remove_response_record(subrec
, rrec
);
98 /****************************************************************************
99 Deal with a timeout when releasing one of our names.
100 ****************************************************************************/
102 static void release_name_timeout_response(struct subnet_record
*subrec
,
103 struct response_record
*rrec
)
105 /* a release is *always* considered to be successful when it
106 times out. This doesn't cause problems as if a WINS server
107 doesn't respond and someone else wants the name then the
108 normal WACK/name query from the WINS server will cope */
109 struct nmb_packet
*sent_nmb
= &rrec
->packet
->packet
.nmb
;
110 bool bcast
= sent_nmb
->header
.nm_flags
.bcast
;
111 struct nmb_name
*question_name
= &sent_nmb
->question
.question_name
;
112 struct in_addr released_ip
;
114 /* Get the ip address we were trying to release. */
115 putip((char*)&released_ip
,&sent_nmb
->additional
->rdata
[2]);
118 /* mark the WINS server temporarily dead */
119 wins_srv_died(rrec
->packet
->ip
, released_ip
);
122 DEBUG(5,("release_name_timeout_response: success in releasing name %s on subnet %s.\n",
123 nmb_namestr(question_name
), subrec
->subnet_name
));
125 if (rrec
->success_fn
) {
126 (*(release_name_success_function
)rrec
->success_fn
)(subrec
, rrec
->userdata
, question_name
, released_ip
);
129 standard_success_release( subrec
, rrec
->userdata
, question_name
, released_ip
);
130 remove_response_record(subrec
, rrec
);
135 when releasing a name with WINS we need to send the release to each of
138 static void wins_release_name(struct name_record
*namerec
,
139 release_name_success_function success_fn
,
140 release_name_fail_function fail_fn
,
141 struct userdata_struct
*userdata
)
146 /* get the list of wins tags - we try to release for each of them */
147 wins_tags
= wins_srv_tags();
149 for (t
=0;wins_tags
&& wins_tags
[t
]; t
++) {
150 for (i
= 0; i
< namerec
->data
.num_ips
; i
++) {
151 struct in_addr wins_ip
= wins_srv_ip_tag(wins_tags
[t
], namerec
->data
.ip
[i
]);
153 bool last_one
= ((i
==namerec
->data
.num_ips
- 1) && !wins_tags
[t
+1]);
154 if (queue_release_name(unicast_subnet
,
155 release_name_response
,
156 release_name_timeout_response
,
157 last_one
?success_fn
: NULL
,
158 last_one
? fail_fn
: NULL
,
159 last_one
? userdata
: NULL
,
161 namerec
->data
.nb_flags
,
164 DEBUG(0,("release_name: Failed to send packet trying to release name %s IP %s\n",
165 nmb_namestr(&namerec
->name
), inet_ntoa(namerec
->data
.ip
[i
]) ));
170 wins_srv_tags_free(wins_tags
);
174 /****************************************************************************
175 Try and release one of our names.
176 ****************************************************************************/
178 void release_name(struct subnet_record
*subrec
, struct name_record
*namerec
,
179 release_name_success_function success_fn
,
180 release_name_fail_function fail_fn
,
181 struct userdata_struct
*userdata
)
185 /* Ensure it's a SELF name, and in the ACTIVE state. */
186 if ((namerec
->data
.source
!= SELF_NAME
) || !NAME_IS_ACTIVE(namerec
)) {
187 DEBUG(0,("release_name: Cannot release name %s from subnet %s. Source was %d \n",
188 nmb_namestr(&namerec
->name
), subrec
->subnet_name
, namerec
->data
.source
));
192 /* Set the name into the deregistering state. */
193 namerec
->data
.nb_flags
|= NB_DEREG
;
195 /* wins releases are a bit different */
196 if (subrec
== unicast_subnet
) {
197 wins_release_name(namerec
, success_fn
, fail_fn
, userdata
);
202 * Go through and release the name for all known ip addresses.
203 * Only call the success/fail function on the last one (it should
204 * only be done once).
206 for (i
= 0; i
< namerec
->data
.num_ips
; i
++) {
207 if (queue_release_name(subrec
,
208 release_name_response
,
209 release_name_timeout_response
,
210 (i
== (namerec
->data
.num_ips
- 1)) ? success_fn
: NULL
,
211 (i
== (namerec
->data
.num_ips
- 1)) ? fail_fn
: NULL
,
212 (i
== (namerec
->data
.num_ips
- 1)) ? userdata
: NULL
,
214 namerec
->data
.nb_flags
,
216 subrec
->bcast_ip
) == NULL
) {
217 DEBUG(0,("release_name: Failed to send packet trying to release name %s IP %s\n",
218 nmb_namestr(&namerec
->name
), inet_ntoa(namerec
->data
.ip
[i
]) ));