2 Unix SMB/CIFS implementation.
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 "dlinklist.h"
25 #include "system/network.h"
26 #include "nbt_server/nbt_server.h"
29 send a name query reply
31 static void nbtd_name_query_reply(struct nbt_name_socket *nbtsock,
32 struct nbt_name_packet *request_packet,
33 const char *src_address, int src_port,
34 struct nbt_name *name, uint32_t ttl,
35 uint16_t nb_flags, const char **addresses)
37 struct nbt_name_packet *packet;
38 size_t num_addresses = str_list_length(addresses);
41 if (num_addresses == 0) {
42 DEBUG(3,("No addresses in name query reply - failing\n"));
46 packet = talloc_zero(nbtsock, struct nbt_name_packet);
47 if (packet == NULL) return;
49 packet->name_trn_id = request_packet->name_trn_id;
54 NBT_FLAG_AUTHORITIVE |
55 NBT_FLAG_RECURSION_DESIRED |
56 NBT_FLAG_RECURSION_AVAIL;
58 packet->answers = talloc_array(packet, struct nbt_res_rec, 1);
59 if (packet->answers == NULL) goto failed;
61 packet->answers[0].name = *name;
62 packet->answers[0].rr_type = NBT_QTYPE_NETBIOS;
63 packet->answers[0].rr_class = NBT_QCLASS_IP;
64 packet->answers[0].ttl = ttl;
65 packet->answers[0].rdata.netbios.length = num_addresses*6;
66 packet->answers[0].rdata.netbios.addresses =
67 talloc_array(packet->answers, struct nbt_rdata_address, num_addresses);
68 if (packet->answers[0].rdata.netbios.addresses == NULL) goto failed;
70 for (i=0;i<num_addresses;i++) {
71 struct nbt_rdata_address *addr =
72 &packet->answers[0].rdata.netbios.addresses[i];
73 addr->nb_flags = nb_flags;
74 addr->ipaddr = talloc_strdup(packet->answers, addresses[i]);
75 if (addr->ipaddr == NULL) goto failed;
78 DEBUG(7,("Sending name query reply for %s<%02x> at %s to %s:%d\n",
79 name->name, name->type, src_address, addresses[0], src_port));
81 nbt_name_reply_send(nbtsock, src_address, src_port, packet);
91 void nbtd_request_query(struct nbt_name_socket *nbtsock,
92 struct nbt_name_packet *packet,
93 const char *src_address, int src_port)
95 struct nbtd_iface_name *iname;
96 struct nbt_name *name;
97 struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private,
98 struct nbtd_interface);
100 /* see if its a node status query */
101 if (packet->qdcount == 1 &&
102 packet->questions[0].question_type == NBT_QTYPE_STATUS) {
103 nbtd_query_status(nbtsock, packet, src_address, src_port);
107 NBT_ASSERT_PACKET(packet, src_address, packet->qdcount == 1);
108 NBT_ASSERT_PACKET(packet, src_address, packet->questions[0].question_type == NBT_QTYPE_NETBIOS);
109 NBT_ASSERT_PACKET(packet, src_address, packet->questions[0].question_class == NBT_QCLASS_IP);
111 /* see if we have the requested name on this interface */
112 name = &packet->questions[0].name;
114 iname = nbtd_find_iname(iface, name, 0);
116 DEBUG(7,("Query for %s<%02x> from %s - not found on %s\n",
117 name->name, name->type, src_address, iface->ip_address));
121 /* if the name is not yet active and its a broadcast query then
123 if (!(iname->nb_flags & NBT_NM_ACTIVE) &&
124 (packet->operation & NBT_FLAG_BROADCAST)) {
125 DEBUG(7,("Query for %s<%02x> from %s - name not active yet on %s\n",
126 name->name, name->type, src_address, iface->ip_address));
130 nbtd_name_query_reply(nbtsock, packet, src_address, src_port,
131 &iname->name, iname->ttl, iname->nb_flags,
132 nbtd_address_list(iface, packet));