summaryrefslogtreecommitdiff
path: root/source3/nmbd/nmbd_responserecordsdb.c
blob: fe294647732d492a03c3f5f31e16dea1178af636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   NBT netbios library routines
   Copyright (C) Andrew Tridgell 1994-1998
   Copyright (C) Luke Kenneth Casson Leighton 1994-1998
   Copyright (C) Jeremy Allison 1994-1998
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   
*/

#include "includes.h"

extern int ClientNMB;

extern int DEBUGLEVEL;

extern pstring scope;
extern struct in_addr ipzero;

int num_response_packets = 0;

/***************************************************************************
  Add an expected response record into the list
  **************************************************************************/

static void add_response_record(struct subnet_record *subrec,
				struct response_record *rrec)
{
  struct response_record *rrec2;

  num_response_packets++; /* count of total number of packets still around */

  DEBUG(4,("add_response_record: adding response record id:%hu to subnet %s. num_records:%d\n",
            rrec->response_id, subrec->subnet_name, num_response_packets));

  if (!subrec->responselist)
  {
    subrec->responselist = rrec;
    rrec->prev = NULL;
    rrec->next = NULL;
    return;
  }
  
  for (rrec2 = subrec->responselist; rrec2->next; rrec2 = rrec2->next) 
    ;
  
  rrec2->next = rrec;
  rrec->next = NULL;
  rrec->prev = rrec2;
}

/***************************************************************************
  Remove an expected response record from the list
  **************************************************************************/

void remove_response_record(struct subnet_record *subrec,
				struct response_record *rrec)
{
  if (rrec->prev)
    rrec->prev->next = rrec->next;
  if (rrec->next)
    rrec->next->prev = rrec->prev;

  if (subrec->responselist == rrec) 
    subrec->responselist = rrec->next; 

  if(rrec->userdata)
  {
	  if(rrec->userdata->free_fn) {
		  (*rrec->userdata->free_fn)(rrec->userdata);
	  } else {
		  ZERO_STRUCTP(rrec->userdata);
		  free((char *)rrec->userdata);
	  }
  }

  /* Ensure we can delete. */
  rrec->packet->locked = False;
  free_packet(rrec->packet);

  ZERO_STRUCTP(rrec);
  free((char *)rrec);

  num_response_packets--; /* count of total number of packets still around */
}

/****************************************************************************
  Create a response record for an outgoing packet.
  **************************************************************************/

struct response_record *make_response_record( struct subnet_record *subrec,
                    struct packet_struct *p,
                    response_function resp_fn,
                    timeout_response_function timeout_fn,
                    success_function success_fn,
                    fail_function fail_fn,
                    struct userdata_struct *userdata)
{
  struct response_record *rrec;
  struct nmb_packet *nmb = &p->packet.nmb;

  if (!(rrec = (struct response_record *)malloc(sizeof(*rrec)))) 
  {
    DEBUG(0,("make_response_queue_record: malloc fail for response_record.\n"));
    return NULL;
  }

  memset((char *)rrec, '\0', sizeof(*rrec));

  rrec->response_id = nmb->header.name_trn_id;

  rrec->resp_fn = resp_fn;
  rrec->timeout_fn = timeout_fn;
  rrec->success_fn = success_fn;
  rrec->fail_fn = fail_fn;

  rrec->packet = p;

  if(userdata)
  {
    /* Intelligent userdata. */
    if(userdata->copy_fn)
    {
      if((rrec->userdata = (*userdata->copy_fn)(userdata)) == NULL)
      {
        DEBUG(0,("make_response_queue_record: copy fail for userdata.\n"));
	ZERO_STRUCTP(rrec);
        free(rrec);
        return NULL;
      }
    }
    else
    {
      /* Primitive userdata, do a memcpy. */
      if((rrec->userdata = (struct userdata_struct *)
           malloc(sizeof(struct userdata_struct)+userdata->userdata_len)) == NULL)
      {
        DEBUG(0,("make_response_queue_record: malloc fail for userdata.\n"));
	ZERO_STRUCTP(rrec);
        free(rrec);
        return NULL;
      }
      rrec->userdata->copy_fn = userdata->copy_fn;
      rrec->userdata->free_fn = userdata->free_fn;
      rrec->userdata->userdata_len = userdata->userdata_len;
      memcpy(rrec->userdata->data, userdata->data, userdata->userdata_len);
    }
  }
  else
    rrec->userdata = NULL;

  rrec->num_msgs = 0;

  if(!nmb->header.nm_flags.bcast)
    rrec->repeat_interval = 5; /* 5 seconds for unicast packets. */
  else
    rrec->repeat_interval = 1; /* XXXX should be in ms */
  rrec->repeat_count = 3; /* 3 retries */
  rrec->repeat_time = time(NULL) + rrec->repeat_interval; /* initial retry time */

  /* This packet is not being processed. */
  rrec->in_expiration_processing = False;

  /* Lock the packet so we won't lose it while it's on the list. */
  p->locked = True;

  add_response_record(subrec, rrec);

  return rrec;
}

/****************************************************************************
  Find a response in a subnet's name query response list. 
  **************************************************************************/

static struct response_record *find_response_record_on_subnet(
                                struct subnet_record *subrec, uint16 id)
{  
  struct response_record *rrec = NULL;

  for (rrec = subrec->responselist; rrec; rrec = rrec->next)
  {
    if (rrec->response_id == id) 
    {
      DEBUG(4, ("find_response_record: found response record id = %hu on subnet %s\n",
               id, subrec->subnet_name));
      break;
    }
  }
  return rrec;
}

/****************************************************************************
  Find a response in any subnet's name query response list. 
  **************************************************************************/

struct response_record *find_response_record(struct subnet_record **ppsubrec,
				uint16 id)
{  
  struct response_record *rrec = NULL;

  for ((*ppsubrec) = FIRST_SUBNET; (*ppsubrec); 
                  (*ppsubrec) = NEXT_SUBNET_INCLUDING_UNICAST(*ppsubrec))
  {
    if((rrec = find_response_record_on_subnet(*ppsubrec, id)) != NULL)
      return rrec;
  }

  /* There should never be response records on the remote_broadcast subnet.
     Sanity check to ensure this is so. */
  if(remote_broadcast_subnet->responselist != NULL)
  {
    DEBUG(0,("find_response_record: response record found on subnet %s. This should \
never happen !\n", remote_broadcast_subnet->subnet_name));
  }

  /* Now check the WINS server subnet if it exists. */
  if(wins_server_subnet != NULL)
  {
    *ppsubrec = wins_server_subnet;
    if((rrec = find_response_record_on_subnet(*ppsubrec, id))!= NULL)
      return rrec;
  }

  DEBUG(0,("find_response_record: response packet id %hu received with no \
matching record.\n", id));
 
  *ppsubrec = NULL;

  return NULL;
}

/****************************************************************************
  Check if a refresh is queued for a particular name on a particular subnet.
  **************************************************************************/
   
BOOL is_refresh_already_queued(struct subnet_record *subrec, struct name_record *namerec)
{  
  struct response_record *rrec = NULL;
   
  for (rrec = subrec->responselist; rrec; rrec = rrec->next)
  {
    struct packet_struct *p = rrec->packet;
    struct nmb_packet *nmb = &p->packet.nmb;

    if((nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_8) ||
       (nmb->header.opcode == NMB_NAME_REFRESH_OPCODE_9))
    {
      /* Yes it's a queued refresh - check if the name is correct. */
      if(nmb_name_equal(&nmb->question.question_name, &namerec->name))
        return True;
    }
  }

  return False;
}