s4-nbt: move libcli/nbt up one level.
[Samba/gebeck_regimport.git] / libcli / nbt / namequery.c
blobed3d8a24925bbf535bee3fa573dccb005dfc7a32
1 /*
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/>.
22 #include "includes.h"
23 #include "../libcli/nbt/libnbt.h"
24 #include "../libcli/nbt/nbt_proto.h"
25 #include "lib/socket/socket.h"
26 #include "param/param.h"
28 /**
29 send a nbt name query
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;
41 packet->qdcount = 1;
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;
64 talloc_free(packet);
65 return req;
67 failed:
68 talloc_free(packet);
69 return NULL;
72 /**
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)
78 NTSTATUS status;
79 struct nbt_name_packet *packet;
80 int i;
82 status = nbt_name_request_recv(req);
83 if (!NT_STATUS_IS_OK(status) ||
84 req->num_replies == 0) {
85 talloc_free(req);
86 return status;
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);
94 talloc_free(req);
95 return status;
98 if (packet->ancount != 1 ||
99 packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
100 packet->answers[0].rr_class != NBT_QCLASS_IP) {
101 talloc_free(req);
102 return status;
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) {
109 talloc_free(req);
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);
122 talloc_free(req);
124 return NT_STATUS_OK;
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;
151 packet->qdcount = 1;
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;
168 talloc_free(packet);
169 return req;
171 failed:
172 talloc_free(packet);
173 return NULL;
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)
182 NTSTATUS status;
183 struct nbt_name_packet *packet;
184 int i;
186 status = nbt_name_request_recv(req);
187 if (!NT_STATUS_IS_OK(status) ||
188 req->num_replies == 0) {
189 talloc_free(req);
190 return status;
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);
198 talloc_free(req);
199 return status;
202 if (packet->ancount != 1 ||
203 packet->answers[0].rr_type != NBT_QTYPE_STATUS ||
204 packet->answers[0].rr_class != NBT_QCLASS_IP) {
205 talloc_free(req);
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);
220 talloc_free(req);
222 return NT_STATUS_OK;
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);