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