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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "../libcli/nbt/libnbt.h"
24 #include "../libcli/nbt/nbt_proto.h"
25 #include "lib/socket/socket.h"
26 #include "param/param.h"
31 _PUBLIC_ struct nbt_name_request *nbt_name_query_send(struct nbt_name_socket *nbtsock,
32 struct nbt_name_query *io)
34 struct nbt_name_request *req;
35 struct nbt_name_packet *packet;
36 struct socket_address *dest;
38 packet = talloc_zero(nbtsock, struct nbt_name_packet);
39 if (packet == NULL) return NULL;
42 packet->operation = NBT_OPCODE_QUERY;
43 if (io->in.broadcast) {
44 packet->operation |= NBT_FLAG_BROADCAST;
46 if (io->in.wins_lookup) {
47 packet->operation |= NBT_FLAG_RECURSION_DESIRED;
50 packet->questions = talloc_array(packet, struct nbt_name_question, 1);
51 if (packet->questions == NULL) goto failed;
53 packet->questions[0].name = io->in.name;
54 packet->questions[0].question_type = NBT_QTYPE_NETBIOS;
55 packet->questions[0].question_class = NBT_QCLASS_IP;
57 dest = socket_address_from_strings(packet, nbtsock->sock->backend_name,
58 io->in.dest_addr, io->in.dest_port);
59 if (dest == NULL) goto failed;
60 req = nbt_name_request_send(nbtsock, dest, packet,
61 io->in.timeout, io->in.retries, false);
62 if (req == NULL) goto failed;
73 wait for a name query reply
75 _PUBLIC_ NTSTATUS nbt_name_query_recv(struct nbt_name_request *req,
76 TALLOC_CTX *mem_ctx, struct nbt_name_query *io)
79 struct nbt_name_packet *packet;
82 status = nbt_name_request_recv(req);
83 if (!NT_STATUS_IS_OK(status) ||
84 req->num_replies == 0) {
89 packet = req->replies[0].packet;
90 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
92 if ((packet->operation & NBT_RCODE) != 0) {
93 status = nbt_rcode_to_ntstatus(packet->operation & NBT_RCODE);
98 if (packet->ancount != 1 ||
99 packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
100 packet->answers[0].rr_class != NBT_QCLASS_IP) {
105 io->out.name = packet->answers[0].name;
106 io->out.num_addrs = packet->answers[0].rdata.netbios.length / 6;
107 io->out.reply_addrs = talloc_array(mem_ctx, const char *, io->out.num_addrs+1);
108 if (io->out.reply_addrs == NULL) {
110 return NT_STATUS_NO_MEMORY;
113 for (i=0;i<io->out.num_addrs;i++) {
114 io->out.reply_addrs[i] = talloc_steal(io->out.reply_addrs,
115 packet->answers[0].rdata.netbios.addresses[i].ipaddr);
117 io->out.reply_addrs[i] = NULL;
119 talloc_steal(mem_ctx, io->out.name.name);
120 talloc_steal(mem_ctx, io->out.name.scope);
128 wait for a name query reply
130 _PUBLIC_ NTSTATUS nbt_name_query(struct nbt_name_socket *nbtsock,
131 TALLOC_CTX *mem_ctx, struct nbt_name_query *io)
133 struct nbt_name_request *req = nbt_name_query_send(nbtsock, io);
134 return nbt_name_query_recv(req, mem_ctx, io);
139 send a nbt name status
141 _PUBLIC_ struct nbt_name_request *nbt_name_status_send(struct nbt_name_socket *nbtsock,
142 struct nbt_name_status *io)
144 struct nbt_name_request *req;
145 struct nbt_name_packet *packet;
146 struct socket_address *dest;
148 packet = talloc_zero(nbtsock, struct nbt_name_packet);
149 if (packet == NULL) return NULL;
152 packet->operation = NBT_OPCODE_QUERY;
154 packet->questions = talloc_array(packet, struct nbt_name_question, 1);
155 if (packet->questions == NULL) goto failed;
157 packet->questions[0].name = io->in.name;
158 packet->questions[0].question_type = NBT_QTYPE_STATUS;
159 packet->questions[0].question_class = NBT_QCLASS_IP;
161 dest = socket_address_from_strings(packet, nbtsock->sock->backend_name,
162 io->in.dest_addr, io->in.dest_port);
163 if (dest == NULL) goto failed;
164 req = nbt_name_request_send(nbtsock, dest, packet,
165 io->in.timeout, io->in.retries, false);
166 if (req == NULL) goto failed;
177 wait for a name status reply
179 _PUBLIC_ NTSTATUS nbt_name_status_recv(struct nbt_name_request *req,
180 TALLOC_CTX *mem_ctx, struct nbt_name_status *io)
183 struct nbt_name_packet *packet;
186 status = nbt_name_request_recv(req);
187 if (!NT_STATUS_IS_OK(status) ||
188 req->num_replies == 0) {
193 packet = req->replies[0].packet;
194 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
196 if ((packet->operation & NBT_RCODE) != 0) {
197 status = nbt_rcode_to_ntstatus(packet->operation & NBT_RCODE);
202 if (packet->ancount != 1 ||
203 packet->answers[0].rr_type != NBT_QTYPE_STATUS ||
204 packet->answers[0].rr_class != NBT_QCLASS_IP) {
206 return NT_STATUS_INVALID_NETWORK_RESPONSE;
209 io->out.name = packet->answers[0].name;
210 talloc_steal(mem_ctx, io->out.name.name);
211 talloc_steal(mem_ctx, io->out.name.scope);
213 io->out.status = packet->answers[0].rdata.status;
214 talloc_steal(mem_ctx, io->out.status.names);
215 for (i=0;i<io->out.status.num_names;i++) {
216 talloc_steal(io->out.status.names, io->out.status.names[i].name);
226 wait for a name status reply
228 _PUBLIC_ NTSTATUS nbt_name_status(struct nbt_name_socket *nbtsock,
229 TALLOC_CTX *mem_ctx, struct nbt_name_status *io)
231 struct nbt_name_request *req = nbt_name_status_send(nbtsock, io);
232 return nbt_name_status_recv(req, mem_ctx, io);