2 Unix SMB/Netbios implementation.
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
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.
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.
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.
29 extern int DEBUGLEVEL;
32 extern struct in_addr ipzero;
34 int num_response_packets = 0;
36 /***************************************************************************
37 Add an expected response record into the list
38 **************************************************************************/
40 void add_response_record(struct subnet_record *subrec,
41 struct response_record *rrec)
43 struct response_record *rrec2;
45 num_response_packets++; /* count of total number of packets still around */
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));
50 if (!subrec->responselist)
52 subrec->responselist = rrec;
58 for (rrec2 = subrec->responselist; rrec2->next; rrec2 = rrec2->next)
66 /***************************************************************************
67 Remove an expected response record from the list
68 **************************************************************************/
70 void remove_response_record(struct subnet_record *subrec,
71 struct response_record *rrec)
74 rrec->prev->next = rrec->next;
76 rrec->next->prev = rrec->prev;
78 if (subrec->responselist == rrec)
79 subrec->responselist = rrec->next;
83 if(rrec->userdata->free_fn)
84 (*rrec->userdata->free_fn)(rrec->userdata);
86 free((char *)rrec->userdata);
89 /* Ensure we can delete. */
90 rrec->packet->locked = False;
91 free_packet(rrec->packet);
95 num_response_packets--; /* count of total number of packets still around */
98 /****************************************************************************
99 Create a response record for an outgoing packet.
100 **************************************************************************/
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)
110 struct response_record *rrec;
111 struct nmb_packet *nmb = &p->packet.nmb;
113 if (!(rrec = (struct response_record *)malloc(sizeof(*rrec))))
115 DEBUG(0,("make_response_queue_record: malloc fail for response_record.\n"));
119 bzero((char *)rrec, sizeof(*rrec));
121 rrec->response_id = nmb->header.name_trn_id;
123 rrec->resp_fn = resp_fn;
124 rrec->timeout_fn = timeout_fn;
125 rrec->success_fn = success_fn;
126 rrec->fail_fn = fail_fn;
132 /* Intelligent userdata. */
133 if(userdata->copy_fn)
135 if((rrec->userdata = (*userdata->copy_fn)(userdata)) == NULL)
137 DEBUG(0,("make_response_queue_record: copy fail for userdata.\n"));
144 /* Primitive userdata, do a memcpy. */
145 if((rrec->userdata = (struct userdata_struct *)
146 malloc(sizeof(struct userdata_struct)+userdata->userdata_len)) == NULL)
148 DEBUG(0,("make_response_queue_record: malloc fail for userdata.\n"));
152 rrec->userdata->copy_fn = userdata->copy_fn;
153 rrec->userdata->free_fn = userdata->free_fn;
154 rrec->userdata->userdata_len = userdata->userdata_len;
155 memcpy(rrec->userdata->data, userdata->data, userdata->userdata_len);
159 rrec->userdata = NULL;
163 if(!nmb->header.nm_flags.bcast)
164 rrec->repeat_interval = 5; /* 5 seconds for unicast packets. */
166 rrec->repeat_interval = 1; /* XXXX should be in ms */
167 rrec->repeat_count = 3; /* 3 retries */
168 rrec->repeat_time = time(NULL) + rrec->repeat_interval; /* initial retry time */
170 /* Lock the packet so we won't lose it while it's on the list. */
173 add_response_record(subrec, rrec);
178 /****************************************************************************
179 Find a response in a subnet's name query response list.
180 **************************************************************************/
182 static struct response_record *find_response_record_on_subnet(
183 struct subnet_record *subrec, uint16 id)
185 struct response_record *rrec = NULL;
187 for (rrec = subrec->responselist; rrec; rrec = rrec->next)
189 if (rrec->response_id == id)
191 DEBUG(4, ("find_response_record: found response record id = %hu on subnet %s\n",
192 id, subrec->subnet_name));
199 /****************************************************************************
200 Find a response in any subnet's name query response list.
201 **************************************************************************/
203 struct response_record *find_response_record(struct subnet_record **ppsubrec,
206 struct response_record *rrec = NULL;
208 for ((*ppsubrec) = FIRST_SUBNET; (*ppsubrec);
209 (*ppsubrec) = NEXT_SUBNET_INCLUDING_UNICAST(*ppsubrec))
211 if((rrec = find_response_record_on_subnet(*ppsubrec, id)) != NULL)
215 /* There should never be response records on the remote_broadcast subnet.
216 Sanity check to ensure this is so. */
217 if(remote_broadcast_subnet->responselist != NULL)
219 DEBUG(0,("find_response_record: response record found on subnet %s. This should \
220 never happen !\n", remote_broadcast_subnet->subnet_name));
223 /* Now check the WINS server subnet if it exists. */
224 if(wins_server_subnet != NULL)
226 *ppsubrec = wins_server_subnet;
227 if((rrec = find_response_record_on_subnet(*ppsubrec, id))!= NULL)
231 DEBUG(0,("find_response_record: response packet id %hu received with no \
232 matching record.\n", id));
239 /****************************************************************************
240 Check if a refresh is queued for a particular name on a particular subnet.
241 **************************************************************************/
243 BOOL is_refresh_already_queued(struct subnet_record *subrec, struct name_record *namerec)
245 struct response_record *rrec = NULL;
247 for (rrec = subrec->responselist; rrec; rrec = rrec->next)
249 struct packet_struct *p = rrec->packet;
250 struct nmb_packet *nmb = &p->packet.nmb;
252 if((nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_8) ||
253 (nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_9))
255 /* Yes it's a queued refresh - check if the name is correct. */
256 if(nmb_name_equal(&nmb->question.question_name, &namerec->name))