RIP BOOL. Convert BOOL -> bool. I found a few interesting
[sfrench/samba-autobuild/.git] / source / nmbd / nmbd_namerelease.c
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
7    
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.
12    
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.
17    
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/>.
20    
21 */
22
23 #include "includes.h"
24
25 /****************************************************************************
26  Deal with a response packet when releasing one of our names.
27 ****************************************************************************/
28
29 static void release_name_response(struct subnet_record *subrec,
30                                   struct response_record *rrec, struct packet_struct *p)
31 {
32         /* 
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.
35          */
36         struct nmb_packet *nmb = &p->packet.nmb;
37         bool bcast = nmb->header.nm_flags.bcast;
38         bool success = True;
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;
42
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)));
48                 return;
49         }
50
51         if (bcast) {
52                 /* Someone sent a response to a bcast release? ignore it. */
53                 return;
54         }
55
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 ! */
59                 success = False;
60
61                 DEBUG(0,("release_name_response: WINS server at IP %s rejected our \
62 name release of name %s with error code %d.\n", 
63                          inet_ntoa(p->ip), 
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. */
68
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));
72
73                 rrec->repeat_count = 0;
74                 /* How long we should wait for. */
75                 rrec->repeat_time = p->timestamp + nmb->answers->ttl;
76                 rrec->num_msgs--;
77                 return;
78         }
79
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));
82         if (success) {
83                 putip((char*)&released_ip ,&nmb->answers->rdata[2]);
84
85                 if(rrec->success_fn)
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);
88         } else {
89                 /* We have no standard_fail_release - maybe we should add one ? */
90                 if (rrec->fail_fn) {
91                         (*(release_name_fail_function)rrec->fail_fn)(subrec, rrec, answer_name);
92                 }
93         }
94
95         remove_response_record(subrec, rrec);
96 }
97
98 /****************************************************************************
99  Deal with a timeout when releasing one of our names.
100 ****************************************************************************/
101
102 static void release_name_timeout_response(struct subnet_record *subrec,
103                                           struct response_record *rrec)
104 {
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;
113
114         /* Get the ip address we were trying to release. */
115         putip((char*)&released_ip ,&sent_nmb->additional->rdata[2]);
116
117         if (!bcast) {
118                 /* mark the WINS server temporarily dead */
119                 wins_srv_died(rrec->packet->ip, released_ip);
120         }
121
122         DEBUG(5,("release_name_timeout_response: success in releasing name %s on subnet %s.\n",
123                  nmb_namestr(question_name), subrec->subnet_name));
124
125         if (rrec->success_fn) {
126                 (*(release_name_success_function)rrec->success_fn)(subrec, rrec->userdata, question_name, released_ip);
127         }
128
129         standard_success_release( subrec, rrec->userdata, question_name, released_ip);
130         remove_response_record(subrec, rrec);
131 }
132
133
134 /*
135   when releasing a name with WINS we need to send the release to each of
136   the WINS groups
137 */
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)                       
142 {
143         int t, i;
144         char **wins_tags;
145
146         /* get the list of wins tags - we try to release for each of them */
147         wins_tags = wins_srv_tags();
148
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]);
152
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,
160                                                &namerec->name,
161                                                namerec->data.nb_flags,
162                                                namerec->data.ip[i],
163                                                wins_ip) == NULL) {
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]) ));
166                         }
167                 }
168         }
169
170         wins_srv_tags_free(wins_tags);
171 }
172
173
174 /****************************************************************************
175  Try and release one of our names.
176 ****************************************************************************/
177
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)
182 {
183         int i;
184
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)); 
189                 return;
190         }
191
192         /* Set the name into the deregistering state. */
193         namerec->data.nb_flags |= NB_DEREG;
194
195         /* wins releases are a bit different */
196         if (subrec == unicast_subnet) {
197                 wins_release_name(namerec, success_fn, fail_fn, userdata);
198                 return;
199         }
200
201         /*  
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).
205          */
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,
213                                        &namerec->name,
214                                        namerec->data.nb_flags,
215                                        namerec->data.ip[i],
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]) ));
219                 }
220         }
221 }