merging for 2.2.6pre1
[Samba.git] / source / libsmb / unexpected.c
blob2d17e3e9a41008249aff681216794abed9137bcd
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 handle unexpected packets
5 Copyright (C) Andrew Tridgell 2000
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 static TDB_CONTEXT *tdbd = NULL;
27 /* the key type used in the unexpeceted packet database */
28 struct unexpected_key {
29 enum packet_type packet_type;
30 time_t timestamp;
31 int count;
36 /****************************************************************************
37 all unexpected packets are passed in here, to be stored in a unexpected
38 packet database. This allows nmblookup and other tools to receive packets
39 erroneoously sent to the wrong port by broken MS systems
40 **************************************************************************/
41 void unexpected_packet(struct packet_struct *p)
43 static int count;
44 TDB_DATA kbuf, dbuf;
45 struct unexpected_key key;
46 char buf[1024];
47 int len=0;
49 if (!tdbd) {
50 tdbd = tdb_open_log(lock_path("unexpected.tdb"), 0,
51 TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
52 O_RDWR | O_CREAT, 0644);
53 if (!tdbd) {
54 DEBUG(0,("Failed to open unexpected.tdb\n"));
55 return;
59 memset(buf,'\0',sizeof(buf));
61 len = build_packet(buf, p);
63 key.packet_type = p->packet_type;
64 key.timestamp = p->timestamp;
65 key.count = count++;
67 kbuf.dptr = (char *)&key;
68 kbuf.dsize = sizeof(key);
69 dbuf.dptr = buf;
70 dbuf.dsize = len;
72 tdb_store(tdbd, kbuf, dbuf, TDB_REPLACE);
76 static time_t lastt;
78 /****************************************************************************
79 delete the record if it is too old
80 **************************************************************************/
81 static int traverse_fn(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
83 struct unexpected_key key;
85 memcpy(&key, kbuf.dptr, sizeof(key));
87 if (lastt - key.timestamp > NMBD_UNEXPECTED_TIMEOUT) {
88 tdb_delete(ttdb, kbuf);
91 return 0;
95 /****************************************************************************
96 delete all old unexpected packets
97 **************************************************************************/
98 void clear_unexpected(time_t t)
100 if (!tdbd) return;
102 if ((lastt != 0) && (t < lastt + NMBD_UNEXPECTED_TIMEOUT))
103 return;
105 lastt = t;
107 tdb_traverse(tdbd, traverse_fn, NULL);
111 static struct packet_struct *matched_packet;
112 static int match_id;
113 static enum packet_type match_type;
114 static char *match_name;
116 /****************************************************************************
117 tdb traversal fn to find a matching 137 packet
118 **************************************************************************/
119 static int traverse_match(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
121 struct unexpected_key key;
122 struct packet_struct *p;
124 memcpy(&key, kbuf.dptr, sizeof(key));
126 if (key.packet_type != match_type) return 0;
128 p = parse_packet(dbuf.dptr, dbuf.dsize, match_type);
130 if ((match_type == NMB_PACKET &&
131 p->packet.nmb.header.name_trn_id == match_id) ||
132 (match_type == DGRAM_PACKET &&
133 match_mailslot_name(p, match_name))) {
134 matched_packet = p;
135 return -1;
138 free_packet(p);
140 return 0;
144 /****************************************************************************
145 check for a particular packet in the unexpected packet queue
146 **************************************************************************/
147 struct packet_struct *receive_unexpected(enum packet_type packet_type, int id,
148 char *mailslot_name)
150 TDB_CONTEXT *tdb2 = NULL;
152 tdb2 = tdb_open_log(lock_path("unexpected.tdb"), 0, 0, O_RDONLY, 0);
153 if (!tdb2) return NULL;
155 matched_packet = NULL;
156 match_id = id;
157 match_type = packet_type;
158 match_name = mailslot_name;
160 tdb_traverse(tdb2, traverse_match, NULL);
162 tdb_close(tdb2);
164 return matched_packet;