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