2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1998
6 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7 Copyright (C) Jeremy Allison 1994-1998
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 /****************************************************************************
28 Deal with a response packet when releasing one of our names.
29 ****************************************************************************/
31 static void release_name_response(struct subnet_record
*subrec
,
32 struct response_record
*rrec
, struct packet_struct
*p
)
35 * If we are releasing broadcast, then getting a response is an
36 * error. If we are releasing unicast, then we expect to get a response.
39 struct nmb_packet
*nmb
= &p
->packet
.nmb
;
40 BOOL bcast
= nmb
->header
.nm_flags
.bcast
;
42 struct nmb_name
*question_name
= &rrec
->packet
->packet
.nmb
.question
.question_name
;
43 struct nmb_name
*answer_name
= &nmb
->answers
->rr_name
;
44 struct in_addr released_ip
;
46 /* Sanity check. Ensure that the answer name in the incoming packet is the
47 same as the requested name in the outgoing packet. */
49 if(!nmb_name_equal(question_name
, answer_name
))
51 DEBUG(0,("release_name_response: Answer name %s differs from question \
52 name %s.\n", nmb_namestr(answer_name
), nmb_namestr(question_name
)));
58 /* Someone sent a response. This shouldn't happen/ */
59 DEBUG(1,("release_name_response: A response for releasing name %s was received on a \
60 broadcast subnet %s. This should not happen !\n", nmb_namestr(answer_name
), subrec
->subnet_name
));
65 /* Unicast - check to see if the response allows us to release the name. */
66 if(nmb
->header
.rcode
!= 0)
68 /* Error code - we were told not to release the name ! What now ! */
71 DEBUG(0,("release_name_response: WINS server at IP %s rejected our \
72 name release of name %s with error code %d.\n", inet_ntoa(p
->ip
),
73 nmb_namestr(answer_name
), nmb
->header
.rcode
));
76 else if(nmb
->header
.opcode
== NMB_WACK_OPCODE
)
78 /* WINS server is telling us to wait. Pretend we didn't get
79 the response but don't send out any more release requests. */
81 DEBUG(5,("release_name_response: WACK from WINS server %s in releasing \
82 name %s on subnet %s.\n", inet_ntoa(p
->ip
), nmb_namestr(answer_name
), subrec
->subnet_name
));
84 rrec
->repeat_count
= 0;
85 /* How long we should wait for. */
86 rrec
->repeat_time
= p
->timestamp
+ nmb
->answers
->ttl
;
92 DEBUG(5,("release_name_response: %s in releasing name %s on subnet %s.\n",
93 success
? "success" : "failure", nmb_namestr(answer_name
), subrec
->subnet_name
));
97 putip((char*)&released_ip
,&nmb
->answers
->rdata
[2]);
100 (*(release_name_success_function
)rrec
->success_fn
)(subrec
, rrec
->userdata
, answer_name
, released_ip
);
101 standard_success_release( subrec
, rrec
->userdata
, answer_name
, released_ip
);
105 /* We have no standard_fail_release - maybe we should add one ? */
107 (*(release_name_fail_function
)rrec
->fail_fn
)(subrec
, rrec
, answer_name
);
110 remove_response_record(subrec
, rrec
);
113 /****************************************************************************
114 Deal with a timeout when releasing one of our names.
115 ****************************************************************************/
117 static void release_name_timeout_response(struct subnet_record
*subrec
,
118 struct response_record
*rrec
)
121 * If we are releasing unicast, then NOT getting a response is an
122 * error - we could not release the name. If we are releasing broadcast,
123 * then we don't expect to get a response.
126 struct nmb_packet
*sent_nmb
= &rrec
->packet
->packet
.nmb
;
127 BOOL bcast
= sent_nmb
->header
.nm_flags
.bcast
;
128 BOOL success
= False
;
129 struct nmb_name
*question_name
= &sent_nmb
->question
.question_name
;
130 struct in_addr released_ip
;
134 if(rrec
->num_msgs
== 0)
136 /* Not receiving a message is success for broadcast release. */
139 /* Get the ip address we were trying to release. */
140 putip((char*)&released_ip
,&sent_nmb
->additional
->rdata
[2]);
145 /* Unicast - if no responses then it's an error. */
146 if(rrec
->num_msgs
== 0)
148 DEBUG(2,("release_name_timeout_response: WINS server at address %s is not \
149 responding.\n", inet_ntoa(rrec
->packet
->ip
)));
151 /* Keep trying to contact the WINS server periodically. This allows
152 us to work correctly if the WINS server is down temporarily when
153 we want to delete the name. */
155 /* Reset the number of attempts to zero and double the interval between
156 retries. Max out at 5 minutes. */
157 rrec
->repeat_count
= 3;
158 rrec
->repeat_interval
*= 2;
159 if(rrec
->repeat_interval
> (5 * 60))
160 rrec
->repeat_interval
= (5 * 60);
161 rrec
->repeat_time
= time(NULL
) + rrec
->repeat_interval
;
163 DEBUG(5,("release_name_timeout_response: increasing WINS timeout to %d seconds.\n",
164 (int)rrec
->repeat_interval
));
165 return; /* Don't remove the response record. */
169 DEBUG(5,("release_name_timeout_response: %s in releasing name %s on subnet %s.\n",
170 success
? "success" : "failure", nmb_namestr(question_name
), subrec
->subnet_name
));
172 if(success
&& rrec
->success_fn
)
175 (*(release_name_success_function
)rrec
->success_fn
)(subrec
, rrec
->userdata
, question_name
, released_ip
);
176 standard_success_release( subrec
, rrec
->userdata
, question_name
, released_ip
);
180 /* We have no standard_fail_release - maybe we should add one ? */
182 (*(release_name_fail_function
)rrec
->fail_fn
)(subrec
, rrec
, question_name
);
185 remove_response_record(subrec
, rrec
);
188 /****************************************************************************
189 Try and release one of our names.
190 ****************************************************************************/
192 BOOL
release_name(struct subnet_record
*subrec
, struct name_record
*namerec
,
193 release_name_success_function success_fn
,
194 release_name_fail_function fail_fn
,
195 struct userdata_struct
*userdata
)
199 /* Ensure it's a SELF name, and in the ACTIVE state. */
200 if((namerec
->data
.source
!= SELF_NAME
) || !NAME_IS_ACTIVE(namerec
))
202 DEBUG(0,("release_name: Cannot release name %s from subnet %s. Source was %d \n",
203 nmb_namestr(&namerec
->name
), subrec
->subnet_name
, namerec
->data
.source
));
207 /* Set the name into the deregistering state. */
208 namerec
->data
.nb_flags
|= NB_DEREG
;
211 * Go through and release the name for all known ip addresses.
212 * Only call the success/fail function on the last one (it should
213 * only be done once).
216 for( i
= 0; i
< namerec
->data
.num_ips
; i
++)
218 if(queue_release_name( subrec
,
219 release_name_response
,
220 release_name_timeout_response
,
221 (i
== (namerec
->data
.num_ips
- 1)) ? success_fn
: NULL
,
222 (i
== (namerec
->data
.num_ips
- 1)) ? fail_fn
: NULL
,
223 (i
== (namerec
->data
.num_ips
- 1)) ? userdata
: NULL
,
225 namerec
->data
.nb_flags
,
226 namerec
->data
.ip
[i
]) == NULL
)
228 DEBUG(0,("release_name: Failed to send packet trying to release name %s IP %s\n",
229 nmb_namestr(&namerec
->name
), inet_ntoa(namerec
->data
.ip
[i
]) ));