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