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"
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;
35 packet = talloc_zero(nbtsock, struct nbt_name_packet);
36 if (packet == NULL) return NULL;
39 packet->operation = NBT_OPCODE_QUERY;
40 if (io->in.broadcast) {
41 packet->operation |= NBT_FLAG_BROADCAST;
43 if (io->in.wins_lookup) {
44 packet->operation |= NBT_FLAG_RECURSION_DESIRED;
47 packet->questions = talloc_array(packet, struct nbt_name_question, 1);
48 if (packet->questions == NULL) goto failed;
50 packet->questions[0].name = io->in.name;
51 packet->questions[0].question_type = NBT_QTYPE_NETBIOS;
52 packet->questions[0].question_class = NBT_QCLASS_IP;
54 req = nbt_name_request_send(nbtsock, io->in.dest_addr, lp_nbt_port(), packet,
55 io->in.timeout, io->in.retries, False);
56 if (req == NULL) goto failed;
67 wait for a name query reply
69 NTSTATUS nbt_name_query_recv(struct nbt_name_request *req,
70 TALLOC_CTX *mem_ctx, struct nbt_name_query *io)
73 struct nbt_name_packet *packet;
76 status = nbt_name_request_recv(req);
77 if (!NT_STATUS_IS_OK(status) ||
78 req->num_replies == 0) {
83 packet = req->replies[0].packet;
84 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].reply_addr);
86 if ((packet->operation & NBT_RCODE) != 0) {
87 status = nbt_rcode_to_ntstatus(packet->operation & NBT_RCODE);
92 if (packet->ancount != 1 ||
93 packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
94 packet->answers[0].rr_class != NBT_QCLASS_IP) {
99 io->out.name = packet->answers[0].name;
100 io->out.num_addrs = packet->answers[0].rdata.netbios.length / 6;
101 io->out.reply_addrs = talloc_array(mem_ctx, const char *, io->out.num_addrs+1);
102 if (io->out.reply_addrs == NULL) {
104 return NT_STATUS_NO_MEMORY;
107 for (i=0;i<io->out.num_addrs;i++) {
108 io->out.reply_addrs[i] = talloc_steal(mem_ctx,
109 packet->answers[0].rdata.netbios.addresses[i].ipaddr);
111 io->out.reply_addrs[i] = NULL;
113 talloc_steal(mem_ctx, io->out.name.name);
114 talloc_steal(mem_ctx, io->out.name.scope);
122 wait for a name query reply
124 NTSTATUS nbt_name_query(struct nbt_name_socket *nbtsock,
125 TALLOC_CTX *mem_ctx, struct nbt_name_query *io)
127 struct nbt_name_request *req = nbt_name_query_send(nbtsock, io);
128 return nbt_name_query_recv(req, mem_ctx, io);
133 send a nbt name status
135 struct nbt_name_request *nbt_name_status_send(struct nbt_name_socket *nbtsock,
136 struct nbt_name_status *io)
138 struct nbt_name_request *req;
139 struct nbt_name_packet *packet;
141 packet = talloc_zero(nbtsock, struct nbt_name_packet);
142 if (packet == NULL) return NULL;
145 packet->operation = NBT_OPCODE_QUERY;
147 packet->questions = talloc_array(packet, struct nbt_name_question, 1);
148 if (packet->questions == NULL) goto failed;
150 packet->questions[0].name = io->in.name;
151 packet->questions[0].question_type = NBT_QTYPE_STATUS;
152 packet->questions[0].question_class = NBT_QCLASS_IP;
154 req = nbt_name_request_send(nbtsock, io->in.dest_addr, lp_nbt_port(), packet,
155 io->in.timeout, io->in.retries, False);
156 if (req == NULL) goto failed;
167 wait for a name status reply
169 NTSTATUS nbt_name_status_recv(struct nbt_name_request *req,
170 TALLOC_CTX *mem_ctx, struct nbt_name_status *io)
173 struct nbt_name_packet *packet;
176 status = nbt_name_request_recv(req);
177 if (!NT_STATUS_IS_OK(status) ||
178 req->num_replies == 0) {
183 packet = req->replies[0].packet;
184 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].reply_addr);
186 if ((packet->operation & NBT_RCODE) != 0) {
187 status = nbt_rcode_to_ntstatus(packet->operation & NBT_RCODE);
192 if (packet->ancount != 1 ||
193 packet->answers[0].rr_type != NBT_QTYPE_STATUS ||
194 packet->answers[0].rr_class != NBT_QCLASS_IP) {
196 return NT_STATUS_INVALID_NETWORK_RESPONSE;
199 io->out.name = packet->answers[0].name;
200 talloc_steal(mem_ctx, io->out.name.name);
201 talloc_steal(mem_ctx, io->out.name.scope);
203 io->out.status = packet->answers[0].rdata.status;
204 talloc_steal(mem_ctx, io->out.status.names);
205 for (i=0;i<io->out.status.num_names;i++) {
206 talloc_steal(io->out.status.names, io->out.status.names[i].name);
216 wait for a name status reply
218 NTSTATUS nbt_name_status(struct nbt_name_socket *nbtsock,
219 TALLOC_CTX *mem_ctx, struct nbt_name_status *io)
221 struct nbt_name_request *req = nbt_name_status_send(nbtsock, io);
222 return nbt_name_status_recv(req, mem_ctx, io);