r492: BUG 483: patch from Michel Gravey <michel.gravey@optogone.com> to fix password...
[Samba/gebeck_regimport.git] / source3 / nmbd / nmbd_responserecordsdb.c
blob30c0c129508da9ef5b4a7d2026c979496905f3a8
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) {
45 subrec->responselist = rrec;
46 rrec->prev = NULL;
47 rrec->next = NULL;
48 return;
51 for (rrec2 = subrec->responselist; rrec2->next; rrec2 = rrec2->next)
54 rrec2->next = rrec;
55 rrec->next = NULL;
56 rrec->prev = rrec2;
59 /***************************************************************************
60 Remove an expected response record from the list
61 **************************************************************************/
63 void remove_response_record(struct subnet_record *subrec,
64 struct response_record *rrec)
66 if (rrec->prev)
67 rrec->prev->next = rrec->next;
68 if (rrec->next)
69 rrec->next->prev = rrec->prev;
71 if (subrec->responselist == rrec)
72 subrec->responselist = rrec->next;
74 if(rrec->userdata) {
75 if(rrec->userdata->free_fn) {
76 (*rrec->userdata->free_fn)(rrec->userdata);
77 } else {
78 ZERO_STRUCTP(rrec->userdata);
79 SAFE_FREE(rrec->userdata);
83 /* Ensure we can delete. */
84 rrec->packet->locked = False;
85 free_packet(rrec->packet);
87 ZERO_STRUCTP(rrec);
88 SAFE_FREE(rrec);
90 num_response_packets--; /* count of total number of packets still around */
93 /****************************************************************************
94 Create a response record for an outgoing packet.
95 **************************************************************************/
97 struct response_record *make_response_record( struct subnet_record *subrec,
98 struct packet_struct *p,
99 response_function resp_fn,
100 timeout_response_function timeout_fn,
101 success_function success_fn,
102 fail_function fail_fn,
103 struct userdata_struct *userdata)
105 struct response_record *rrec;
106 struct nmb_packet *nmb = &p->packet.nmb;
108 if (!(rrec = (struct response_record *)malloc(sizeof(*rrec)))) {
109 DEBUG(0,("make_response_queue_record: malloc fail for response_record.\n"));
110 return NULL;
113 memset((char *)rrec, '\0', sizeof(*rrec));
115 rrec->response_id = nmb->header.name_trn_id;
117 rrec->resp_fn = resp_fn;
118 rrec->timeout_fn = timeout_fn;
119 rrec->success_fn = success_fn;
120 rrec->fail_fn = fail_fn;
122 rrec->packet = p;
124 if(userdata) {
125 /* Intelligent userdata. */
126 if(userdata->copy_fn) {
127 if((rrec->userdata = (*userdata->copy_fn)(userdata)) == NULL) {
128 DEBUG(0,("make_response_queue_record: copy fail for userdata.\n"));
129 ZERO_STRUCTP(rrec);
130 SAFE_FREE(rrec);
131 return NULL;
133 } else {
134 /* Primitive userdata, do a memcpy. */
135 if((rrec->userdata = (struct userdata_struct *)
136 malloc(sizeof(struct userdata_struct)+userdata->userdata_len)) == NULL) {
137 DEBUG(0,("make_response_queue_record: malloc fail for userdata.\n"));
138 ZERO_STRUCTP(rrec);
139 SAFE_FREE(rrec);
140 return NULL;
142 rrec->userdata->copy_fn = userdata->copy_fn;
143 rrec->userdata->free_fn = userdata->free_fn;
144 rrec->userdata->userdata_len = userdata->userdata_len;
145 memcpy(rrec->userdata->data, userdata->data, userdata->userdata_len);
147 } else {
148 rrec->userdata = NULL;
151 rrec->num_msgs = 0;
153 if(!nmb->header.nm_flags.bcast)
154 rrec->repeat_interval = 5; /* 5 seconds for unicast packets. */
155 else
156 rrec->repeat_interval = 1; /* XXXX should be in ms */
157 rrec->repeat_count = 3; /* 3 retries */
158 rrec->repeat_time = time(NULL) + rrec->repeat_interval; /* initial retry time */
160 /* This packet is not being processed. */
161 rrec->in_expiration_processing = False;
163 /* Lock the packet so we won't lose it while it's on the list. */
164 p->locked = True;
166 add_response_record(subrec, rrec);
168 return rrec;
171 /****************************************************************************
172 Find a response in a subnet's name query response list.
173 **************************************************************************/
175 static struct response_record *find_response_record_on_subnet(
176 struct subnet_record *subrec, uint16 id)
178 struct response_record *rrec = NULL;
180 for (rrec = subrec->responselist; rrec; rrec = rrec->next) {
181 if (rrec->response_id == id) {
182 DEBUG(4, ("find_response_record: found response record id = %hu on subnet %s\n",
183 id, subrec->subnet_name));
184 break;
187 return rrec;
190 /****************************************************************************
191 Find a response in any subnet's name query response list.
192 **************************************************************************/
194 struct response_record *find_response_record(struct subnet_record **ppsubrec,
195 uint16 id)
197 struct response_record *rrec = NULL;
199 for ((*ppsubrec) = FIRST_SUBNET; (*ppsubrec);
200 (*ppsubrec) = NEXT_SUBNET_INCLUDING_UNICAST(*ppsubrec)) {
201 if((rrec = find_response_record_on_subnet(*ppsubrec, id)) != NULL)
202 return rrec;
205 /* There should never be response records on the remote_broadcast subnet.
206 Sanity check to ensure this is so. */
207 if(remote_broadcast_subnet->responselist != NULL) {
208 DEBUG(0,("find_response_record: response record found on subnet %s. This should \
209 never happen !\n", remote_broadcast_subnet->subnet_name));
212 /* Now check the WINS server subnet if it exists. */
213 if(wins_server_subnet != NULL) {
214 *ppsubrec = wins_server_subnet;
215 if((rrec = find_response_record_on_subnet(*ppsubrec, id))!= NULL)
216 return rrec;
219 DEBUG(0,("find_response_record: response packet id %hu received with no \
220 matching record.\n", id));
222 *ppsubrec = NULL;
224 return NULL;
227 /****************************************************************************
228 Check if a refresh is queued for a particular name on a particular subnet.
229 **************************************************************************/
231 BOOL is_refresh_already_queued(struct subnet_record *subrec, struct name_record *namerec)
233 struct response_record *rrec = NULL;
235 for (rrec = subrec->responselist; rrec; rrec = rrec->next) {
236 struct packet_struct *p = rrec->packet;
237 struct nmb_packet *nmb = &p->packet.nmb;
239 if((nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_8) ||
240 (nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_9)) {
241 /* Yes it's a queued refresh - check if the name is correct. */
242 if(nmb_name_equal(&nmb->question.question_name, &namerec->name))
243 return True;
247 return False;