2 Unix SMB/CIFS implementation.
4 packet utility functions
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "nbt_server/nbt_server.h"
25 #include "lib/socket/socket.h"
26 #include "librpc/gen_ndr/ndr_nbt.h"
29 we received a badly formed packet - log it
31 void nbtd_bad_packet(struct nbt_name_packet
*packet
,
32 const struct socket_address
*src
, const char *reason
)
34 DEBUG(2,("nbtd: bad packet '%s' from %s:%d\n", reason
, src
->addr
, src
->port
));
36 NDR_PRINT_DEBUG(nbt_name_packet
, packet
);
42 see if an incoming packet is a broadcast packet from one of our own
45 BOOL
nbtd_self_packet_and_bcast(struct nbt_name_socket
*nbtsock
,
46 struct nbt_name_packet
*packet
,
47 const struct socket_address
*src
)
49 struct nbtd_interface
*iface
= talloc_get_type(nbtsock
->incoming
.private,
50 struct nbtd_interface
);
52 /* if its not a broadcast then its not considered a self packet */
53 if (!(packet
->operation
& NBT_FLAG_BROADCAST
)) {
58 * this uses the fact that iface->nbtsock is the unicast listen address
59 * if the interface isn't the global bcast interface
61 * so if the request was directed to the unicast address it isn't a broadcast
64 if (iface
->nbtsock
== nbtsock
&&
65 iface
!= iface
->nbtsrv
->bcast_interface
) {
69 return nbtd_self_packet(nbtsock
, packet
, src
);
72 BOOL
nbtd_self_packet(struct nbt_name_socket
*nbtsock
,
73 struct nbt_name_packet
*packet
,
74 const struct socket_address
*src
)
76 struct nbtd_interface
*iface
= talloc_get_type(nbtsock
->incoming
.private,
77 struct nbtd_interface
);
78 struct nbtd_server
*nbtsrv
= iface
->nbtsrv
;
80 /* if its not from the nbt port, then it wasn't a broadcast from us */
81 if (src
->port
!= lp_nbt_port()) {
85 /* we have to loop over our interface list, seeing if its from
86 one of our own interfaces */
87 for (iface
=nbtsrv
->interfaces
;iface
;iface
=iface
->next
) {
88 if (strcmp(src
->addr
, iface
->ip_address
) == 0) {
98 send a name query reply
100 void nbtd_name_query_reply(struct nbt_name_socket
*nbtsock
,
101 struct nbt_name_packet
*request_packet
,
102 struct socket_address
*src
,
103 struct nbt_name
*name
, uint32_t ttl
,
104 uint16_t nb_flags
, const char **addresses
)
106 struct nbt_name_packet
*packet
;
107 size_t num_addresses
= str_list_length(addresses
);
108 struct nbtd_interface
*iface
= talloc_get_type(nbtsock
->incoming
.private,
109 struct nbtd_interface
);
110 struct nbtd_server
*nbtsrv
= iface
->nbtsrv
;
113 if (num_addresses
== 0) {
114 DEBUG(3,("No addresses in name query reply - failing\n"));
118 packet
= talloc_zero(nbtsock
, struct nbt_name_packet
);
119 if (packet
== NULL
) return;
121 packet
->name_trn_id
= request_packet
->name_trn_id
;
126 NBT_FLAG_AUTHORITIVE
|
127 NBT_FLAG_RECURSION_DESIRED
|
128 NBT_FLAG_RECURSION_AVAIL
;
130 packet
->answers
= talloc_array(packet
, struct nbt_res_rec
, 1);
131 if (packet
->answers
== NULL
) goto failed
;
133 packet
->answers
[0].name
= *name
;
134 packet
->answers
[0].rr_type
= NBT_QTYPE_NETBIOS
;
135 packet
->answers
[0].rr_class
= NBT_QCLASS_IP
;
136 packet
->answers
[0].ttl
= ttl
;
137 packet
->answers
[0].rdata
.netbios
.length
= num_addresses
*6;
138 packet
->answers
[0].rdata
.netbios
.addresses
=
139 talloc_array(packet
->answers
, struct nbt_rdata_address
, num_addresses
);
140 if (packet
->answers
[0].rdata
.netbios
.addresses
== NULL
) goto failed
;
142 for (i
=0;i
<num_addresses
;i
++) {
143 struct nbt_rdata_address
*addr
=
144 &packet
->answers
[0].rdata
.netbios
.addresses
[i
];
145 addr
->nb_flags
= nb_flags
;
146 addr
->ipaddr
= talloc_strdup(packet
->answers
, addresses
[i
]);
147 if (addr
->ipaddr
== NULL
) goto failed
;
150 DEBUG(7,("Sending name query reply for %s at %s to %s:%d\n",
151 nbt_name_string(packet
, name
), addresses
[0], src
->addr
, src
->port
));
153 nbtsrv
->stats
.total_sent
++;
154 nbt_name_reply_send(nbtsock
, src
, packet
);
162 send a negative name query reply
164 void nbtd_negative_name_query_reply(struct nbt_name_socket
*nbtsock
,
165 struct nbt_name_packet
*request_packet
,
166 struct socket_address
*src
)
168 struct nbt_name_packet
*packet
;
169 struct nbt_name
*name
= &request_packet
->questions
[0].name
;
170 struct nbtd_interface
*iface
= talloc_get_type(nbtsock
->incoming
.private,
171 struct nbtd_interface
);
172 struct nbtd_server
*nbtsrv
= iface
->nbtsrv
;
174 packet
= talloc_zero(nbtsock
, struct nbt_name_packet
);
175 if (packet
== NULL
) return;
177 packet
->name_trn_id
= request_packet
->name_trn_id
;
182 NBT_FLAG_AUTHORITIVE
|
185 packet
->answers
= talloc_array(packet
, struct nbt_res_rec
, 1);
186 if (packet
->answers
== NULL
) goto failed
;
188 packet
->answers
[0].name
= *name
;
189 packet
->answers
[0].rr_type
= NBT_QTYPE_NULL
;
190 packet
->answers
[0].rr_class
= NBT_QCLASS_IP
;
191 packet
->answers
[0].ttl
= 0;
192 ZERO_STRUCT(packet
->answers
[0].rdata
);
194 DEBUG(7,("Sending negative name query reply for %s to %s:%d\n",
195 nbt_name_string(packet
, name
), src
->addr
, src
->port
));
197 nbtsrv
->stats
.total_sent
++;
198 nbt_name_reply_send(nbtsock
, src
, packet
);
205 send a name registration reply
207 void nbtd_name_registration_reply(struct nbt_name_socket
*nbtsock
,
208 struct nbt_name_packet
*request_packet
,
209 struct socket_address
*src
,
212 struct nbt_name_packet
*packet
;
213 struct nbt_name
*name
= &request_packet
->questions
[0].name
;
214 struct nbtd_interface
*iface
= talloc_get_type(nbtsock
->incoming
.private,
215 struct nbtd_interface
);
216 struct nbtd_server
*nbtsrv
= iface
->nbtsrv
;
218 packet
= talloc_zero(nbtsock
, struct nbt_name_packet
);
219 if (packet
== NULL
) return;
221 packet
->name_trn_id
= request_packet
->name_trn_id
;
225 NBT_OPCODE_REGISTER
|
226 NBT_FLAG_AUTHORITIVE
|
227 NBT_FLAG_RECURSION_DESIRED
|
228 NBT_FLAG_RECURSION_AVAIL
|
231 packet
->answers
= talloc_array(packet
, struct nbt_res_rec
, 1);
232 if (packet
->answers
== NULL
) goto failed
;
234 packet
->answers
[0].name
= *name
;
235 packet
->answers
[0].rr_type
= NBT_QTYPE_NETBIOS
;
236 packet
->answers
[0].rr_class
= NBT_QCLASS_IP
;
237 packet
->answers
[0].ttl
= request_packet
->additional
[0].ttl
;
238 packet
->answers
[0].rdata
= request_packet
->additional
[0].rdata
;
240 DEBUG(7,("Sending %s name registration reply for %s to %s:%d\n",
241 rcode
==0?"positive":"negative",
242 nbt_name_string(packet
, name
), src
->addr
, src
->port
));
244 nbtsrv
->stats
.total_sent
++;
245 nbt_name_reply_send(nbtsock
, src
, packet
);
253 send a name release reply
255 void nbtd_name_release_reply(struct nbt_name_socket
*nbtsock
,
256 struct nbt_name_packet
*request_packet
,
257 struct socket_address
*src
,
260 struct nbt_name_packet
*packet
;
261 struct nbt_name
*name
= &request_packet
->questions
[0].name
;
262 struct nbtd_interface
*iface
= talloc_get_type(nbtsock
->incoming
.private,
263 struct nbtd_interface
);
264 struct nbtd_server
*nbtsrv
= iface
->nbtsrv
;
266 packet
= talloc_zero(nbtsock
, struct nbt_name_packet
);
267 if (packet
== NULL
) return;
269 packet
->name_trn_id
= request_packet
->name_trn_id
;
274 NBT_FLAG_AUTHORITIVE
|
277 packet
->answers
= talloc_array(packet
, struct nbt_res_rec
, 1);
278 if (packet
->answers
== NULL
) goto failed
;
280 packet
->answers
[0].name
= *name
;
281 packet
->answers
[0].rr_type
= NBT_QTYPE_NETBIOS
;
282 packet
->answers
[0].rr_class
= NBT_QCLASS_IP
;
283 packet
->answers
[0].ttl
= request_packet
->additional
[0].ttl
;
284 packet
->answers
[0].rdata
= request_packet
->additional
[0].rdata
;
286 DEBUG(7,("Sending %s name release reply for %s to %s:%d\n",
287 rcode
==0?"positive":"negative",
288 nbt_name_string(packet
, name
), src
->addr
, src
->port
));
290 nbtsrv
->stats
.total_sent
++;
291 nbt_name_reply_send(nbtsock
, src
, packet
);
301 void nbtd_wack_reply(struct nbt_name_socket
*nbtsock
,
302 struct nbt_name_packet
*request_packet
,
303 struct socket_address
*src
,
306 struct nbt_name_packet
*packet
;
307 struct nbt_name
*name
= &request_packet
->questions
[0].name
;
308 struct nbtd_interface
*iface
= talloc_get_type(nbtsock
->incoming
.private,
309 struct nbtd_interface
);
310 struct nbtd_server
*nbtsrv
= iface
->nbtsrv
;
312 packet
= talloc_zero(nbtsock
, struct nbt_name_packet
);
313 if (packet
== NULL
) return;
315 packet
->name_trn_id
= request_packet
->name_trn_id
;
320 NBT_FLAG_AUTHORITIVE
;
322 packet
->answers
= talloc_array(packet
, struct nbt_res_rec
, 1);
323 if (packet
->answers
== NULL
) goto failed
;
325 packet
->answers
[0].name
= *name
;
326 packet
->answers
[0].rr_type
= NBT_QTYPE_NETBIOS
;
327 packet
->answers
[0].rr_class
= NBT_QCLASS_IP
;
328 packet
->answers
[0].ttl
= ttl
;
329 packet
->answers
[0].rdata
.data
.length
= 2;
330 packet
->answers
[0].rdata
.data
.data
= talloc_size(packet
, 2);
331 if (packet
->answers
[0].rdata
.data
.data
== NULL
) goto failed
;
332 RSSVAL(packet
->answers
[0].rdata
.data
.data
, 0, request_packet
->operation
);
334 DEBUG(7,("Sending WACK reply for %s to %s:%d\n",
335 nbt_name_string(packet
, name
), src
->addr
, src
->port
));
337 nbtsrv
->stats
.total_sent
++;
338 nbt_name_reply_send(nbtsock
, src
, packet
);