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