RIP BOOL. Convert BOOL -> bool. I found a few interesting
[amitay/samba.git] / source3 / nmbd / nmbd_responserecordsdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NBT netbios library routines
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 int num_response_packets = 0;
26
27 /***************************************************************************
28   Add an expected response record into the list
29   **************************************************************************/
30
31 static void add_response_record(struct subnet_record *subrec,
32                                 struct response_record *rrec)
33 {
34         struct response_record *rrec2;
35
36         num_response_packets++; /* count of total number of packets still around */
37
38         DEBUG(4,("add_response_record: adding response record id:%hu to subnet %s. num_records:%d\n",
39                 rrec->response_id, subrec->subnet_name, num_response_packets));
40
41         if (!subrec->responselist) {
42                 subrec->responselist = rrec;
43                 rrec->prev = NULL;
44                 rrec->next = NULL;
45                 return;
46         }
47   
48         for (rrec2 = subrec->responselist; rrec2->next; rrec2 = rrec2->next) 
49                 ;
50   
51         rrec2->next = rrec;
52         rrec->next = NULL;
53         rrec->prev = rrec2;
54 }
55
56 /***************************************************************************
57   Remove an expected response record from the list
58   **************************************************************************/
59
60 void remove_response_record(struct subnet_record *subrec,
61                                 struct response_record *rrec)
62 {
63         if (rrec->prev)
64                 rrec->prev->next = rrec->next;
65         if (rrec->next)
66                 rrec->next->prev = rrec->prev;
67
68         if (subrec->responselist == rrec) 
69                 subrec->responselist = rrec->next; 
70
71         if(rrec->userdata) {
72                 if(rrec->userdata->free_fn) {
73                         (*rrec->userdata->free_fn)(rrec->userdata);
74                 } else {
75                         ZERO_STRUCTP(rrec->userdata);
76                         SAFE_FREE(rrec->userdata);
77                 }
78         }
79
80         /* Ensure we can delete. */
81         rrec->packet->locked = False;
82         free_packet(rrec->packet);
83
84         ZERO_STRUCTP(rrec);
85         SAFE_FREE(rrec);
86
87         num_response_packets--; /* count of total number of packets still around */
88 }
89
90 /****************************************************************************
91   Create a response record for an outgoing packet.
92   **************************************************************************/
93
94 struct response_record *make_response_record( struct subnet_record *subrec,
95                                               struct packet_struct *p,
96                                               response_function resp_fn,
97                                               timeout_response_function timeout_fn,
98                                               success_function success_fn,
99                                               fail_function fail_fn,
100                                               struct userdata_struct *userdata)
101 {
102         struct response_record *rrec;
103         struct nmb_packet *nmb = &p->packet.nmb;
104
105         if (!(rrec = SMB_MALLOC_P(struct response_record))) {
106                 DEBUG(0,("make_response_queue_record: malloc fail for response_record.\n"));
107                 return NULL;
108         }
109
110         memset((char *)rrec, '\0', sizeof(*rrec));
111
112         rrec->response_id = nmb->header.name_trn_id;
113
114         rrec->resp_fn = resp_fn;
115         rrec->timeout_fn = timeout_fn;
116         rrec->success_fn = success_fn;
117         rrec->fail_fn = fail_fn;
118
119         rrec->packet = p;
120
121         if(userdata) {
122                 /* Intelligent userdata. */
123                 if(userdata->copy_fn) {
124                         if((rrec->userdata = (*userdata->copy_fn)(userdata)) == NULL) {
125                                 DEBUG(0,("make_response_queue_record: copy fail for userdata.\n"));
126                                 ZERO_STRUCTP(rrec);
127                                 SAFE_FREE(rrec);
128                                 return NULL;
129                         }
130                 } else {
131                         /* Primitive userdata, do a memcpy. */
132                         if((rrec->userdata = (struct userdata_struct *)
133                                         SMB_MALLOC(sizeof(struct userdata_struct)+userdata->userdata_len)) == NULL) {
134                                 DEBUG(0,("make_response_queue_record: malloc fail for userdata.\n"));
135                                 ZERO_STRUCTP(rrec);
136                                 SAFE_FREE(rrec);
137                                 return NULL;
138                         }
139                         rrec->userdata->copy_fn = userdata->copy_fn;
140                         rrec->userdata->free_fn = userdata->free_fn;
141                         rrec->userdata->userdata_len = userdata->userdata_len;
142                         memcpy(rrec->userdata->data, userdata->data, userdata->userdata_len);
143                 }
144         } else {
145                 rrec->userdata = NULL;
146         }
147
148         rrec->num_msgs = 0;
149
150         if(!nmb->header.nm_flags.bcast)
151                 rrec->repeat_interval = 5; /* 5 seconds for unicast packets. */
152         else
153                 rrec->repeat_interval = 1; /* XXXX should be in ms */
154         rrec->repeat_count = 3; /* 3 retries */
155         rrec->repeat_time = time(NULL) + rrec->repeat_interval; /* initial retry time */
156
157         /* This packet is not being processed. */
158         rrec->in_expiration_processing = False;
159
160         /* Lock the packet so we won't lose it while it's on the list. */
161         p->locked = True;
162
163         add_response_record(subrec, rrec);
164
165         return rrec;
166 }
167
168 /****************************************************************************
169   Find a response in a subnet's name query response list. 
170   **************************************************************************/
171
172 static struct response_record *find_response_record_on_subnet(
173                                 struct subnet_record *subrec, uint16 id)
174 {  
175         struct response_record *rrec = NULL;
176
177         for (rrec = subrec->responselist; rrec; rrec = rrec->next) {
178                 if (rrec->response_id == id) {
179                         DEBUG(4, ("find_response_record: found response record id = %hu on subnet %s\n",
180                                 id, subrec->subnet_name));
181                         break;
182                 }
183         }
184         return rrec;
185 }
186
187 /****************************************************************************
188   Find a response in any subnet's name query response list. 
189   **************************************************************************/
190
191 struct response_record *find_response_record(struct subnet_record **ppsubrec,
192                                 uint16 id)
193 {  
194         struct response_record *rrec = NULL;
195
196         for ((*ppsubrec) = FIRST_SUBNET; (*ppsubrec); 
197                                 (*ppsubrec) = NEXT_SUBNET_INCLUDING_UNICAST(*ppsubrec)) {
198                 if((rrec = find_response_record_on_subnet(*ppsubrec, id)) != NULL)
199                         return rrec;
200         }
201
202         /* There should never be response records on the remote_broadcast subnet.
203                         Sanity check to ensure this is so. */
204         if(remote_broadcast_subnet->responselist != NULL) {
205                 DEBUG(0,("find_response_record: response record found on subnet %s. This should \
206 never happen !\n", remote_broadcast_subnet->subnet_name));
207         }
208
209         /* Now check the WINS server subnet if it exists. */
210         if(wins_server_subnet != NULL) {
211                 *ppsubrec = wins_server_subnet;
212                 if((rrec = find_response_record_on_subnet(*ppsubrec, id))!= NULL)
213                         return rrec;
214         }
215
216         DEBUG(3,("find_response_record: response packet id %hu received with no \
217 matching record.\n", id));
218  
219         *ppsubrec = NULL;
220
221         return NULL;
222 }
223
224 /****************************************************************************
225   Check if a refresh is queued for a particular name on a particular subnet.
226   **************************************************************************/
227    
228 bool is_refresh_already_queued(struct subnet_record *subrec, struct name_record *namerec)
229 {  
230         struct response_record *rrec = NULL;
231    
232         for (rrec = subrec->responselist; rrec; rrec = rrec->next) {
233                 struct packet_struct *p = rrec->packet;
234                 struct nmb_packet *nmb = &p->packet.nmb;
235
236                 if((nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_8) ||
237                                 (nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_9)) {
238                         /* Yes it's a queued refresh - check if the name is correct. */
239                         if(nmb_name_equal(&nmb->question.question_name, &namerec->name))
240                         return True;
241                 }
242         }
243
244         return False;
245 }