2 Unix SMB/CIFS implementation.
3 handle unexpected packets
4 Copyright (C) Andrew Tridgell 2000
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 static TDB_CONTEXT *tdbd = NULL;
26 /* the key type used in the unexpeceted packet database */
27 struct unexpected_key {
28 enum packet_type packet_type;
35 /****************************************************************************
36 all unexpected packets are passed in here, to be stored in a unexpected
37 packet database. This allows nmblookup and other tools to receive packets
38 erroneoously sent to the wrong port by broken MS systems
39 **************************************************************************/
40 void unexpected_packet(struct packet_struct *p)
44 struct unexpected_key key;
50 mem_ctx = talloc_init("receive_unexpected");
52 tdbd = tdb_open_log(lock_path(mem_ctx, "unexpected.tdb"), 0,
53 TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
54 O_RDWR | O_CREAT, 0644);
55 talloc_destroy(mem_ctx);
57 DEBUG(0,("Failed to open unexpected.tdb\n"));
62 memset(buf,'\0',sizeof(buf));
64 len = build_packet(buf, p);
66 key.packet_type = p->packet_type;
67 key.timestamp = p->timestamp;
70 kbuf.dptr = (char *)&key;
71 kbuf.dsize = sizeof(key);
75 tdb_store(tdbd, kbuf, dbuf, TDB_REPLACE);
81 /****************************************************************************
82 delete the record if it is too old
83 **************************************************************************/
84 static int traverse_fn(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
86 struct unexpected_key key;
88 memcpy(&key, kbuf.dptr, sizeof(key));
90 if (lastt - key.timestamp > NMBD_UNEXPECTED_TIMEOUT) {
91 tdb_delete(ttdb, kbuf);
98 /****************************************************************************
99 delete all old unexpected packets
100 **************************************************************************/
101 void clear_unexpected(time_t t)
105 if ((lastt != 0) && (t < lastt + NMBD_UNEXPECTED_TIMEOUT))
110 tdb_traverse(tdbd, traverse_fn, NULL);
114 static struct packet_struct *matched_packet;
116 static enum packet_type match_type;
117 static const char *match_name;
119 /****************************************************************************
120 tdb traversal fn to find a matching 137 packet
121 **************************************************************************/
122 static int traverse_match(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
124 struct unexpected_key key;
125 struct packet_struct *p;
127 memcpy(&key, kbuf.dptr, sizeof(key));
129 if (key.packet_type != match_type) return 0;
131 p = parse_packet(dbuf.dptr, dbuf.dsize, match_type);
133 if ((match_type == NMB_PACKET &&
134 p->packet.nmb.header.name_trn_id == match_id) ||
135 (match_type == DGRAM_PACKET &&
136 match_mailslot_name(p, match_name))) {
147 /****************************************************************************
148 check for a particular packet in the unexpected packet queue
149 **************************************************************************/
150 struct packet_struct *receive_unexpected(enum packet_type packet_type, int id,
151 const char *mailslot_name)
156 mem_ctx = talloc_init("receive_unexpected");
157 if (!mem_ctx) return NULL;
158 tdb2 = tdb_open_log(lock_path(mem_ctx, "unexpected.tdb"), 0, 0, O_RDONLY, 0);
159 talloc_destroy(mem_ctx);
160 if (!tdb2) return NULL;
162 matched_packet = NULL;
164 match_type = packet_type;
165 match_name = mailslot_name;
167 tdb_traverse(tdb2, traverse_match, NULL);
171 return matched_packet;