r25492: Start adding IPv6 compatible code to lib/util_sock.c and deal with
[nivanova/samba-autobuild/.git] / source3 / libsmb / unexpected.c
1 /*
2    Unix SMB/CIFS implementation.
3    handle unexpected packets
4    Copyright (C) Andrew Tridgell 2000
5
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 3 of the License, or
9    (at your option) any later version.
10
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.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "includes.h"
22
23 static TDB_CONTEXT *tdbd = NULL;
24
25 /* the key type used in the unexpeceted packet database */
26 struct unexpected_key {
27         enum packet_type packet_type;
28         time_t timestamp;
29         int count;
30 };
31
32 /****************************************************************************
33  All unexpected packets are passed in here, to be stored in a unexpected
34  packet database. This allows nmblookup and other tools to receive packets
35  erroneoously sent to the wrong port by broken MS systems.
36 **************************************************************************/
37
38 void unexpected_packet(struct packet_struct *p)
39 {
40         static int count;
41         TDB_DATA kbuf, dbuf;
42         struct unexpected_key key;
43         char buf[1024];
44         int len=0;
45         uint32_t enc_ip;
46
47         if (!tdbd) {
48                 tdbd = tdb_open_log(lock_path("unexpected.tdb"), 0,
49                                TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
50                                O_RDWR | O_CREAT, 0644);
51                 if (!tdbd) {
52                         DEBUG(0,("Failed to open unexpected.tdb\n"));
53                         return;
54                 }
55         }
56
57         memset(buf,'\0',sizeof(buf));
58
59         /* Encode the ip addr and port. */
60         enc_ip = ntohl(p->ip.s_addr);
61         SIVAL(buf,0,enc_ip);
62         SSVAL(buf,4,p->port);
63
64         len = build_packet(&buf[6], sizeof(buf)-6, p) + 6;
65
66         key.packet_type = p->packet_type;
67         key.timestamp = p->timestamp;
68         key.count = count++;
69
70         kbuf.dptr = (uint8_t *)&key;
71         kbuf.dsize = sizeof(key);
72         dbuf.dptr = (uint8_t *)buf;
73         dbuf.dsize = len;
74
75         tdb_store(tdbd, kbuf, dbuf, TDB_REPLACE);
76 }
77
78
79 static time_t lastt;
80
81 /****************************************************************************
82  Delete the record if it is too old.
83 **************************************************************************/
84
85 static int traverse_fn(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
86 {
87         struct unexpected_key key;
88
89         memcpy(&key, kbuf.dptr, sizeof(key));
90
91         if (lastt - key.timestamp > NMBD_UNEXPECTED_TIMEOUT) {
92                 tdb_delete(ttdb, kbuf);
93         }
94
95         return 0;
96 }
97
98
99 /****************************************************************************
100  Delete all old unexpected packets.
101 **************************************************************************/
102
103 void clear_unexpected(time_t t)
104 {
105         if (!tdbd) return;
106
107         if ((lastt != 0) && (t < lastt + NMBD_UNEXPECTED_TIMEOUT))
108                 return;
109
110         lastt = t;
111
112         tdb_traverse(tdbd, traverse_fn, NULL);
113 }
114
115
116 static struct packet_struct *matched_packet;
117 static int match_id;
118 static enum packet_type match_type;
119 static const char *match_name;
120
121 /****************************************************************************
122  tdb traversal fn to find a matching 137 packet.
123 **************************************************************************/
124
125 static int traverse_match(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
126 {
127         struct unexpected_key key;
128         struct in_addr ip;
129         uint32_t enc_ip;
130         int port;
131         struct packet_struct *p;
132
133         memcpy(&key, kbuf.dptr, sizeof(key));
134
135         if (key.packet_type != match_type) return 0;
136
137         if (dbuf.dsize < 6) {
138                 return 0;
139         }
140
141         /* Decode the ip addr and port. */
142         enc_ip = IVAL(dbuf.dptr,0);
143         ip.s_addr = htonl(enc_ip);
144         port = SVAL(dbuf.dptr,4);
145
146         p = parse_packet((char *)&dbuf.dptr[6],
147                         dbuf.dsize-6,
148                         match_type,
149                         ip,
150                         port);
151
152         if ((match_type == NMB_PACKET &&
153              p->packet.nmb.header.name_trn_id == match_id) ||
154             (match_type == DGRAM_PACKET &&
155              match_mailslot_name(p, match_name))) {
156                 matched_packet = p;
157                 return -1;
158         }
159
160         free_packet(p);
161
162         return 0;
163 }
164
165 /****************************************************************************
166  Check for a particular packet in the unexpected packet queue.
167 **************************************************************************/
168
169 struct packet_struct *receive_unexpected(enum packet_type packet_type, int id,
170                                          const char *mailslot_name)
171 {
172         TDB_CONTEXT *tdb2;
173
174         tdb2 = tdb_open_log(lock_path("unexpected.tdb"), 0, 0, O_RDONLY, 0);
175         if (!tdb2) return NULL;
176
177         matched_packet = NULL;
178         match_id = id;
179         match_type = packet_type;
180         match_name = mailslot_name;
181
182         tdb_traverse(tdb2, traverse_match, NULL);
183
184         tdb_close(tdb2);
185
186         return matched_packet;
187 }