2 Unix SMB/CIFS implementation.
4 make nbt name query requests
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 "libcli/nbt/libnbt.h"
25 #include "lib/socket/socket.h"
29 struct nbt_name_request *nbt_name_query_send(struct nbt_name_socket *nbtsock,
30 struct nbt_name_query *io)
32 struct nbt_name_request *req;
33 struct nbt_name_packet *packet;
34 struct socket_address *dest;
36 packet = talloc_zero(nbtsock, struct nbt_name_packet);
37 if (packet == NULL) return NULL;
40 packet->operation = NBT_OPCODE_QUERY;
41 if (io->in.broadcast) {
42 packet->operation |= NBT_FLAG_BROADCAST;
44 if (io->in.wins_lookup) {
45 packet->operation |= NBT_FLAG_RECURSION_DESIRED;
48 packet->questions = talloc_array(packet, struct nbt_name_question, 1);
49 if (packet->questions == NULL) goto failed;
51 packet->questions[0].name = io->in.name;
52 packet->questions[0].question_type = NBT_QTYPE_NETBIOS;
53 packet->questions[0].question_class = NBT_QCLASS_IP;
55 dest = socket_address_from_strings(packet, nbtsock->sock->backend_name,
56 io->in.dest_addr, lp_nbt_port());
57 if (dest == NULL) goto failed;
58 req = nbt_name_request_send(nbtsock, dest, packet,
59 io->in.timeout, io->in.retries, False);
60 if (req == NULL) goto failed;
71 wait for a name query reply
73 NTSTATUS nbt_name_query_recv(struct nbt_name_request *req,
74 TALLOC_CTX *mem_ctx, struct nbt_name_query *io)
77 struct nbt_name_packet *packet;
80 status = nbt_name_request_recv(req);
81 if (!NT_STATUS_IS_OK(status) ||
82 req->num_replies == 0) {
87 packet = req->replies[0].packet;
88 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
90 if ((packet->operation & NBT_RCODE) != 0) {
91 status = nbt_rcode_to_ntstatus(packet->operation & NBT_RCODE);
96 if (packet->ancount != 1 ||
97 packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
98 packet->answers[0].rr_class != NBT_QCLASS_IP) {
103 io->out.name = packet->answers[0].name;
104 io->out.num_addrs = packet->answers[0].rdata.netbios.length / 6;
105 io->out.reply_addrs = talloc_array(mem_ctx, const char *, io->out.num_addrs+1);
106 if (io->out.reply_addrs == NULL) {
108 return NT_STATUS_NO_MEMORY;
111 for (i=0;i<io->out.num_addrs;i++) {
112 io->out.reply_addrs[i] = talloc_steal(io->out.reply_addrs,
113 packet->answers[0].rdata.netbios.addresses[i].ipaddr);
115 io->out.reply_addrs[i] = NULL;
117 talloc_steal(mem_ctx, io->out.name.name);
118 talloc_steal(mem_ctx, io->out.name.scope);
126 wait for a name query reply
128 NTSTATUS nbt_name_query(struct nbt_name_socket *nbtsock,
129 TALLOC_CTX *mem_ctx, struct nbt_name_query *io)
131 struct nbt_name_request *req = nbt_name_query_send(nbtsock, io);
132 return nbt_name_query_recv(req, mem_ctx, io);
137 send a nbt name status
139 struct nbt_name_request *nbt_name_status_send(struct nbt_name_socket *nbtsock,
140 struct nbt_name_status *io)
142 struct nbt_name_request *req;
143 struct nbt_name_packet *packet;
144 struct socket_address *dest;
146 packet = talloc_zero(nbtsock, struct nbt_name_packet);
147 if (packet == NULL) return NULL;
150 packet->operation = NBT_OPCODE_QUERY;
152 packet->questions = talloc_array(packet, struct nbt_name_question, 1);
153 if (packet->questions == NULL) goto failed;
155 packet->questions[0].name = io->in.name;
156 packet->questions[0].question_type = NBT_QTYPE_STATUS;
157 packet->questions[0].question_class = NBT_QCLASS_IP;
159 dest = socket_address_from_strings(packet, nbtsock->sock->backend_name,
160 io->in.dest_addr, lp_nbt_port());
161 if (dest == NULL) goto failed;
162 req = nbt_name_request_send(nbtsock, dest, packet,
163 io->in.timeout, io->in.retries, False);
164 if (req == NULL) goto failed;
175 wait for a name status reply
177 NTSTATUS nbt_name_status_recv(struct nbt_name_request *req,
178 TALLOC_CTX *mem_ctx, struct nbt_name_status *io)
181 struct nbt_name_packet *packet;
184 status = nbt_name_request_recv(req);
185 if (!NT_STATUS_IS_OK(status) ||
186 req->num_replies == 0) {
191 packet = req->replies[0].packet;
192 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
194 if ((packet->operation & NBT_RCODE) != 0) {
195 status = nbt_rcode_to_ntstatus(packet->operation & NBT_RCODE);
200 if (packet->ancount != 1 ||
201 packet->answers[0].rr_type != NBT_QTYPE_STATUS ||
202 packet->answers[0].rr_class != NBT_QCLASS_IP) {
204 return NT_STATUS_INVALID_NETWORK_RESPONSE;
207 io->out.name = packet->answers[0].name;
208 talloc_steal(mem_ctx, io->out.name.name);
209 talloc_steal(mem_ctx, io->out.name.scope);
211 io->out.status = packet->answers[0].rdata.status;
212 talloc_steal(mem_ctx, io->out.status.names);
213 for (i=0;i<io->out.status.num_names;i++) {
214 talloc_steal(io->out.status.names, io->out.status.names[i].name);
224 wait for a name status reply
226 NTSTATUS nbt_name_status(struct nbt_name_socket *nbtsock,
227 TALLOC_CTX *mem_ctx, struct nbt_name_status *io)
229 struct nbt_name_request *req = nbt_name_status_send(nbtsock, io);
230 return nbt_name_status_recv(req, mem_ctx, io);