This is a first step toward moving long namelists into a database. I
[samba.git] / source3 / nmbd / nmbd_namerelease.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
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
8    
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.
13    
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.
18    
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.
22    
23 */
24
25 #include "includes.h"
26
27 extern int DEBUGLEVEL;
28
29 extern pstring scope;
30
31 /****************************************************************************
32  Deal with a response packet when releasing one of our names.
33 ****************************************************************************/
34
35 static void release_name_response(struct subnet_record *subrec,
36                        struct response_record *rrec, struct packet_struct *p)
37 {
38   /* 
39    * If we are releasing broadcast, then getting a response is an
40    * error. If we are releasing unicast, then we expect to get a response.
41    */
42
43   struct nmb_packet *nmb = &p->packet.nmb;
44   BOOL bcast = nmb->header.nm_flags.bcast;
45   BOOL success = True;
46   struct nmb_name *question_name = &rrec->packet->packet.nmb.question.question_name;
47   struct nmb_name *answer_name = &nmb->answers->rr_name;
48   struct in_addr released_ip;
49
50   /* Sanity check. Ensure that the answer name in the incoming packet is the
51      same as the requested name in the outgoing packet. */
52
53   if(!nmb_name_equal(question_name, answer_name))
54   {
55     DEBUG(0,("release_name_response: Answer name %s differs from question \
56 name %s.\n", namestr(answer_name), namestr(question_name)));
57     return;
58   }
59
60   if(bcast)
61   {
62     /* Someone sent a response. This shouldn't happen/ */
63     DEBUG(1,("release_name_response: A response for releasing name %s was received on a \
64 broadcast subnet %s. This should not happen !\n", namestr(answer_name), subrec->subnet_name));
65     return;
66   }
67   else
68   {
69     /* Unicast - check to see if the response allows us to release the name. */
70     if(nmb->header.rcode != 0)
71     {
72       /* Error code - we were told not to release the name ! What now ! */
73       success = False;
74
75       DEBUG(0,("release_name_response: WINS server at IP %s rejected our \
76 name release of name %s with error code %d.\n", inet_ntoa(p->ip), 
77                   namestr(answer_name), nmb->header.rcode));
78
79     }
80     else if(nmb->header.opcode == NMB_WACK_OPCODE)
81     {
82       /* WINS server is telling us to wait. Pretend we didn't get
83          the response but don't send out any more release requests. */
84
85       DEBUG(5,("release_name_response: WACK from WINS server %s in releasing \
86 name %s on subnet %s.\n", inet_ntoa(p->ip), namestr(answer_name), subrec->subnet_name));
87
88       rrec->repeat_count = 0;
89       /* How long we should wait for. */
90       rrec->repeat_time = p->timestamp + nmb->answers->ttl;
91       rrec->num_msgs--;
92       return;
93     }
94   } 
95
96   DEBUG(5,("release_name_response: %s in releasing name %s on subnet %s.\n",
97         success ? "success" : "failure", namestr(answer_name), subrec->subnet_name));
98
99   if(success)
100   {
101     putip((char*)&released_ip ,&nmb->answers->rdata[2]);
102
103     if(rrec->success_fn)
104       (*rrec->success_fn)(subrec, rrec->userdata, answer_name, released_ip);
105     standard_success_release( subrec, rrec->userdata, answer_name, released_ip);
106   }
107   else
108   {
109     /* We have no standard_fail_release - maybe we should add one ? */
110     if(rrec->fail_fn)
111       (*rrec->fail_fn)(subrec, rrec, answer_name);
112   }
113
114   remove_response_record(subrec, rrec);
115 }
116
117 /****************************************************************************
118  Deal with a timeout when releasing one of our names.
119 ****************************************************************************/
120
121 static void release_name_timeout_response(struct subnet_record *subrec,
122                        struct response_record *rrec)
123 {
124   /*
125    * If we are releasing unicast, then NOT getting a response is an
126    * error - we could not release the name. If we are releasing broadcast,
127    * then we don't expect to get a response.
128    */
129
130   struct nmb_packet *sent_nmb = &rrec->packet->packet.nmb;
131   BOOL bcast = sent_nmb->header.nm_flags.bcast;
132   BOOL success = False;
133   struct nmb_name *question_name = &sent_nmb->question.question_name;
134   struct in_addr released_ip;
135
136   if(bcast)
137   {
138     if(rrec->num_msgs == 0)
139     {
140       /* Not receiving a message is success for broadcast release. */
141       success = True; 
142
143       /* Get the ip address we were trying to release. */
144       putip((char*)&released_ip ,&sent_nmb->additional->rdata[2]);
145     }
146   }
147   else
148   {
149     /* Unicast - if no responses then it's an error. */
150     if(rrec->num_msgs == 0)
151     {
152       DEBUG(2,("release_name_timeout_response: WINS server at address %s is not \
153 responding.\n", inet_ntoa(rrec->packet->ip)));
154
155       /* Keep trying to contact the WINS server periodically. This allows
156          us to work correctly if the WINS server is down temporarily when
157          we want to delete the name. */
158
159       /* Reset the number of attempts to zero and double the interval between
160          retries. Max out at 5 minutes. */
161       rrec->repeat_count = 3;
162       rrec->repeat_interval *= 2;
163       if(rrec->repeat_interval > (5 * 60))
164         rrec->repeat_interval = (5 * 60);
165       rrec->repeat_time = time(NULL) + rrec->repeat_interval;
166
167       DEBUG(5,("release_name_timeout_response: increasing WINS timeout to %d seconds.\n",
168               rrec->repeat_interval));
169       return; /* Don't remove the response record. */
170     }
171   }
172
173   DEBUG(5,("release_name_timeout_response: %s in releasing name %s on subnet %s.\n",
174         success ? "success" : "failure", namestr(question_name), subrec->subnet_name));
175
176   if(success && rrec->success_fn)
177   {
178     if(rrec->success_fn)
179       (*rrec->success_fn)(subrec, rrec->userdata, question_name, released_ip);
180     standard_success_release( subrec, rrec->userdata, question_name, released_ip);
181   }
182   else 
183   {
184     /* We have no standard_fail_release - maybe we should add one ? */
185     if( rrec->fail_fn)
186       (*rrec->fail_fn)(subrec, rrec, question_name);
187   }
188
189   remove_response_record(subrec, rrec);
190 }
191
192 /****************************************************************************
193  Try and release one of our names.
194 ****************************************************************************/
195
196 BOOL release_name(struct subnet_record *subrec, struct name_record *namerec,
197                    release_name_success_function success_fn,
198                    release_name_fail_function fail_fn,
199                    struct userdata_struct *userdata)
200 {
201   int i;
202
203   /* Ensure it's a SELF name, and in the ACTIVE state. */
204   if((namerec->data.source != SELF_NAME) || !NAME_IS_ACTIVE(namerec))
205   {
206     DEBUG(0,("release_name: Cannot release name %s from subnet %s. Source was %d \n",
207           namestr(&namerec->name), subrec->subnet_name, namerec->data.source)); 
208     return True;
209   }
210
211   /* Set the name into the deregistering state. */
212   namerec->data.nb_flags |= NB_DEREG;
213
214   /*  
215    * Go through and release the name for all known ip addresses.
216    * Only call the success/fail function on the last one (it should
217    * only be done once).
218    */
219
220   for( i = 0; i < namerec->data.num_ips; i++)
221   {
222     if(queue_release_name( subrec,
223         release_name_response,
224         release_name_timeout_response,
225         (i == (namerec->data.num_ips - 1)) ? success_fn : NULL,
226         (i == (namerec->data.num_ips - 1)) ? fail_fn : NULL,
227         (i == (namerec->data.num_ips - 1)) ? userdata : NULL,
228         &namerec->name,
229         namerec->data.nb_flags,
230         namerec->data.ip[i]) == NULL)
231     {
232       DEBUG(0,("release_name: Failed to send packet trying to release name %s IP %s\n",
233             namestr(&namerec->name), inet_ntoa(namerec->data.ip[i]) ));
234       return True;
235     }
236   }
237   return False;
238 }