merge from APPLIANCE_HEAD
[Samba.git] / source / nmbd / nmbd_responserecordsdb.c
bloba2da5ddf4abae1396256ec6cc28516c5c4d1c0b8
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
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.
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.
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.
24 #include "includes.h"
26 extern int ClientNMB;
28 int num_response_packets = 0;
30 /***************************************************************************
31 Add an expected response record into the list
32 **************************************************************************/
34 static void add_response_record(struct subnet_record *subrec,
35 struct response_record *rrec)
37 struct response_record *rrec2;
39 num_response_packets++; /* count of total number of packets still around */
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));
44 if (!subrec->responselist)
46 subrec->responselist = rrec;
47 rrec->prev = NULL;
48 rrec->next = NULL;
49 return;
52 for (rrec2 = subrec->responselist; rrec2->next; rrec2 = rrec2->next)
55 rrec2->next = rrec;
56 rrec->next = NULL;
57 rrec->prev = rrec2;
60 /***************************************************************************
61 Remove an expected response record from the list
62 **************************************************************************/
64 void remove_response_record(struct subnet_record *subrec,
65 struct response_record *rrec)
67 if (rrec->prev)
68 rrec->prev->next = rrec->next;
69 if (rrec->next)
70 rrec->next->prev = rrec->prev;
72 if (subrec->responselist == rrec)
73 subrec->responselist = rrec->next;
75 if(rrec->userdata)
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);
85 /* Ensure we can delete. */
86 rrec->packet->locked = False;
87 free_packet(rrec->packet);
89 ZERO_STRUCTP(rrec);
90 SAFE_FREE(rrec);
92 num_response_packets--; /* count of total number of packets still around */
95 /****************************************************************************
96 Create a response record for an outgoing packet.
97 **************************************************************************/
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)
107 struct response_record *rrec;
108 struct nmb_packet *nmb = &p->packet.nmb;
110 if (!(rrec = (struct response_record *)malloc(sizeof(*rrec))))
112 DEBUG(0,("make_response_queue_record: malloc fail for response_record.\n"));
113 return NULL;
116 memset((char *)rrec, '\0', sizeof(*rrec));
118 rrec->response_id = nmb->header.name_trn_id;
120 rrec->resp_fn = resp_fn;
121 rrec->timeout_fn = timeout_fn;
122 rrec->success_fn = success_fn;
123 rrec->fail_fn = fail_fn;
125 rrec->packet = p;
127 if(userdata)
129 /* Intelligent userdata. */
130 if(userdata->copy_fn)
132 if((rrec->userdata = (*userdata->copy_fn)(userdata)) == NULL)
134 DEBUG(0,("make_response_queue_record: copy fail for userdata.\n"));
135 ZERO_STRUCTP(rrec);
136 SAFE_FREE(rrec);
137 return NULL;
140 else
142 /* Primitive userdata, do a memcpy. */
143 if((rrec->userdata = (struct userdata_struct *)
144 malloc(sizeof(struct userdata_struct)+userdata->userdata_len)) == NULL)
146 DEBUG(0,("make_response_queue_record: malloc fail for userdata.\n"));
147 ZERO_STRUCTP(rrec);
148 SAFE_FREE(rrec);
149 return NULL;
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);
157 else
158 rrec->userdata = NULL;
160 rrec->num_msgs = 0;
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 */
169 /* This packet is not being processed. */
170 rrec->in_expiration_processing = False;
172 /* Lock the packet so we won't lose it while it's on the list. */
173 p->locked = True;
175 add_response_record(subrec, rrec);
177 return rrec;
180 /****************************************************************************
181 Find a response in a subnet's name query response list.
182 **************************************************************************/
184 static struct response_record *find_response_record_on_subnet(
185 struct subnet_record *subrec, uint16 id)
187 struct response_record *rrec = NULL;
189 for (rrec = subrec->responselist; rrec; rrec = rrec->next)
191 if (rrec->response_id == id)
193 DEBUG(4, ("find_response_record: found response record id = %hu on subnet %s\n",
194 id, subrec->subnet_name));
195 break;
198 return rrec;
201 /****************************************************************************
202 Find a response in any subnet's name query response list.
203 **************************************************************************/
205 struct response_record *find_response_record(struct subnet_record **ppsubrec,
206 uint16 id)
208 struct response_record *rrec = NULL;
210 for ((*ppsubrec) = FIRST_SUBNET; (*ppsubrec);
211 (*ppsubrec) = NEXT_SUBNET_INCLUDING_UNICAST(*ppsubrec))
213 if((rrec = find_response_record_on_subnet(*ppsubrec, id)) != NULL)
214 return rrec;
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)
221 DEBUG(0,("find_response_record: response record found on subnet %s. This should \
222 never happen !\n", remote_broadcast_subnet->subnet_name));
225 /* Now check the WINS server subnet if it exists. */
226 if(wins_server_subnet != NULL)
228 *ppsubrec = wins_server_subnet;
229 if((rrec = find_response_record_on_subnet(*ppsubrec, id))!= NULL)
230 return rrec;
233 DEBUG(0,("find_response_record: response packet id %hu received with no \
234 matching record.\n", id));
236 *ppsubrec = NULL;
238 return NULL;
241 /****************************************************************************
242 Check if a refresh is queued for a particular name on a particular subnet.
243 **************************************************************************/
245 BOOL is_refresh_already_queued(struct subnet_record *subrec, struct name_record *namerec)
247 struct response_record *rrec = NULL;
249 for (rrec = subrec->responselist; rrec; rrec = rrec->next)
251 struct packet_struct *p = rrec->packet;
252 struct nmb_packet *nmb = &p->packet.nmb;
254 if((nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_8) ||
255 (nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_9))
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;
263 return False;