merge from APPLIANCE_HEAD
[Samba.git] / source / nmbd / nmbd_namerelease.c
blobfd35181f33f1f49993177b18378c101886c37093
1 /*
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.
24 #include "includes.h"
26 /****************************************************************************
27 Deal with a response packet when releasing one of our names.
28 ****************************************************************************/
30 static void release_name_response(struct subnet_record *subrec,
31 struct response_record *rrec, struct packet_struct *p)
33 /*
34 * If we are releasing broadcast, then getting a response is an
35 * error. If we are releasing unicast, then we expect to get a response.
38 struct nmb_packet *nmb = &p->packet.nmb;
39 BOOL bcast = nmb->header.nm_flags.bcast;
40 BOOL success = True;
41 struct nmb_name *question_name = &rrec->packet->packet.nmb.question.question_name;
42 struct nmb_name *answer_name = &nmb->answers->rr_name;
43 struct in_addr released_ip;
45 /* Sanity check. Ensure that the answer name in the incoming packet is the
46 same as the requested name in the outgoing packet. */
48 if(!nmb_name_equal(question_name, answer_name))
50 DEBUG(0,("release_name_response: Answer name %s differs from question \
51 name %s.\n", nmb_namestr(answer_name), nmb_namestr(question_name)));
52 return;
55 if(bcast)
57 /* Someone sent a response. This shouldn't happen/ */
58 DEBUG(1,("release_name_response: A response for releasing name %s was received on a \
59 broadcast subnet %s. This should not happen !\n", nmb_namestr(answer_name), subrec->subnet_name));
60 return;
62 else
64 /* Unicast - check to see if the response allows us to release the name. */
65 if(nmb->header.rcode != 0)
67 /* Error code - we were told not to release the name ! What now ! */
68 success = False;
70 DEBUG(0,("release_name_response: WINS server at IP %s rejected our \
71 name release of name %s with error code %d.\n", inet_ntoa(p->ip),
72 nmb_namestr(answer_name), nmb->header.rcode));
75 else if(nmb->header.opcode == NMB_WACK_OPCODE)
77 /* WINS server is telling us to wait. Pretend we didn't get
78 the response but don't send out any more release requests. */
80 DEBUG(5,("release_name_response: WACK from WINS server %s in releasing \
81 name %s on subnet %s.\n", inet_ntoa(p->ip), nmb_namestr(answer_name), subrec->subnet_name));
83 rrec->repeat_count = 0;
84 /* How long we should wait for. */
85 rrec->repeat_time = p->timestamp + nmb->answers->ttl;
86 rrec->num_msgs--;
87 return;
91 DEBUG(5,("release_name_response: %s in releasing name %s on subnet %s.\n",
92 success ? "success" : "failure", nmb_namestr(answer_name), subrec->subnet_name));
94 if(success)
96 putip((char*)&released_ip ,&nmb->answers->rdata[2]);
98 if(rrec->success_fn)
99 (*(release_name_success_function)rrec->success_fn)(subrec, rrec->userdata, answer_name, released_ip);
100 standard_success_release( subrec, rrec->userdata, answer_name, released_ip);
102 else
104 /* We have no standard_fail_release - maybe we should add one ? */
105 if(rrec->fail_fn)
106 (*(release_name_fail_function)rrec->fail_fn)(subrec, rrec, answer_name);
109 remove_response_record(subrec, rrec);
112 /****************************************************************************
113 Deal with a timeout when releasing one of our names.
114 ****************************************************************************/
116 static void release_name_timeout_response(struct subnet_record *subrec,
117 struct response_record *rrec)
120 * If we are releasing unicast, then NOT getting a response is an
121 * error - we could not release the name. If we are releasing broadcast,
122 * then we don't expect to get a response.
125 struct nmb_packet *sent_nmb = &rrec->packet->packet.nmb;
126 BOOL bcast = sent_nmb->header.nm_flags.bcast;
127 BOOL success = False;
128 struct nmb_name *question_name = &sent_nmb->question.question_name;
129 struct in_addr released_ip;
131 if(bcast)
133 if(rrec->num_msgs == 0)
135 /* Not receiving a message is success for broadcast release. */
136 success = True;
138 /* Get the ip address we were trying to release. */
139 putip((char*)&released_ip ,&sent_nmb->additional->rdata[2]);
142 else
144 /* Unicast - if no responses then it's an error. */
145 if(rrec->num_msgs == 0)
147 DEBUG(2,("release_name_timeout_response: WINS server at address %s is not \
148 responding.\n", inet_ntoa(rrec->packet->ip)));
150 /* Keep trying to contact the WINS server periodically. This allows
151 us to work correctly if the WINS server is down temporarily when
152 we want to delete the name. */
154 /* Reset the number of attempts to zero and double the interval between
155 retries. Max out at 5 minutes. */
156 rrec->repeat_count = 3;
157 rrec->repeat_interval *= 2;
158 if(rrec->repeat_interval > (5 * 60))
159 rrec->repeat_interval = (5 * 60);
160 rrec->repeat_time = time(NULL) + rrec->repeat_interval;
162 DEBUG(5,("release_name_timeout_response: increasing WINS timeout to %d seconds.\n",
163 (int)rrec->repeat_interval));
164 return; /* Don't remove the response record. */
168 DEBUG(5,("release_name_timeout_response: %s in releasing name %s on subnet %s.\n",
169 success ? "success" : "failure", nmb_namestr(question_name), subrec->subnet_name));
171 if(success && rrec->success_fn)
173 if(rrec->success_fn)
174 (*(release_name_success_function)rrec->success_fn)(subrec, rrec->userdata, question_name, released_ip);
175 standard_success_release( subrec, rrec->userdata, question_name, released_ip);
177 else
179 /* We have no standard_fail_release - maybe we should add one ? */
180 if( rrec->fail_fn)
181 (*(release_name_fail_function)rrec->fail_fn)(subrec, rrec, question_name);
184 remove_response_record(subrec, rrec);
187 /****************************************************************************
188 Try and release one of our names.
189 ****************************************************************************/
191 BOOL release_name(struct subnet_record *subrec, struct name_record *namerec,
192 release_name_success_function success_fn,
193 release_name_fail_function fail_fn,
194 struct userdata_struct *userdata)
196 int i;
198 /* Ensure it's a SELF name, and in the ACTIVE state. */
199 if((namerec->data.source != SELF_NAME) || !NAME_IS_ACTIVE(namerec))
201 DEBUG(0,("release_name: Cannot release name %s from subnet %s. Source was %d \n",
202 nmb_namestr(&namerec->name), subrec->subnet_name, namerec->data.source));
203 return True;
206 /* Set the name into the deregistering state. */
207 namerec->data.nb_flags |= NB_DEREG;
210 * Go through and release the name for all known ip addresses.
211 * Only call the success/fail function on the last one (it should
212 * only be done once).
215 for( i = 0; i < namerec->data.num_ips; i++)
217 if(queue_release_name( subrec,
218 release_name_response,
219 release_name_timeout_response,
220 (i == (namerec->data.num_ips - 1)) ? success_fn : NULL,
221 (i == (namerec->data.num_ips - 1)) ? fail_fn : NULL,
222 (i == (namerec->data.num_ips - 1)) ? userdata : NULL,
223 &namerec->name,
224 namerec->data.nb_flags,
225 namerec->data.ip[i]) == NULL)
227 DEBUG(0,("release_name: Failed to send packet trying to release name %s IP %s\n",
228 nmb_namestr(&namerec->name), inet_ntoa(namerec->data.ip[i]) ));
229 return True;
232 return False;