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