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"
30 _PUBLIC_
struct nbt_name_request
*nbt_name_query_send(struct nbt_name_socket
*nbtsock
,
31 struct nbt_name_query
*io
)
33 struct nbt_name_request
*req
;
34 struct nbt_name_packet
*packet
;
35 struct socket_address
*dest
;
37 packet
= talloc_zero(nbtsock
, struct nbt_name_packet
);
38 if (packet
== NULL
) return NULL
;
41 packet
->operation
= NBT_OPCODE_QUERY
;
42 if (io
->in
.broadcast
) {
43 packet
->operation
|= NBT_FLAG_BROADCAST
;
45 if (io
->in
.wins_lookup
) {
46 packet
->operation
|= NBT_FLAG_RECURSION_DESIRED
;
49 packet
->questions
= talloc_array(packet
, struct nbt_name_question
, 1);
50 if (packet
->questions
== NULL
) goto failed
;
52 packet
->questions
[0].name
= io
->in
.name
;
53 packet
->questions
[0].question_type
= NBT_QTYPE_NETBIOS
;
54 packet
->questions
[0].question_class
= NBT_QCLASS_IP
;
56 dest
= socket_address_from_strings(packet
, nbtsock
->sock
->backend_name
,
57 io
->in
.dest_addr
, io
->in
.dest_port
);
58 if (dest
== NULL
) goto failed
;
59 req
= nbt_name_request_send(nbtsock
, nbtsock
, dest
, packet
,
60 io
->in
.timeout
, io
->in
.retries
, false);
61 if (req
== NULL
) goto failed
;
72 wait for a name query reply
74 _PUBLIC_ NTSTATUS
nbt_name_query_recv(struct nbt_name_request
*req
,
75 TALLOC_CTX
*mem_ctx
, struct nbt_name_query
*io
)
78 struct nbt_name_packet
*packet
;
81 status
= nbt_name_request_recv(req
);
82 if (!NT_STATUS_IS_OK(status
) ||
83 req
->num_replies
== 0) {
88 packet
= req
->replies
[0].packet
;
89 io
->out
.reply_from
= talloc_steal(mem_ctx
, req
->replies
[0].dest
->addr
);
91 if ((packet
->operation
& NBT_RCODE
) != 0) {
92 status
= nbt_rcode_to_ntstatus(packet
->operation
& NBT_RCODE
);
97 if (packet
->ancount
!= 1 ||
98 packet
->answers
[0].rr_type
!= NBT_QTYPE_NETBIOS
||
99 packet
->answers
[0].rr_class
!= NBT_QCLASS_IP
) {
104 io
->out
.name
= packet
->answers
[0].name
;
105 io
->out
.num_addrs
= packet
->answers
[0].rdata
.netbios
.length
/ 6;
106 io
->out
.reply_addrs
= talloc_array(mem_ctx
, const char *, io
->out
.num_addrs
+1);
107 if (io
->out
.reply_addrs
== NULL
) {
109 return NT_STATUS_NO_MEMORY
;
112 for (i
=0;i
<io
->out
.num_addrs
;i
++) {
113 io
->out
.reply_addrs
[i
] = talloc_steal(io
->out
.reply_addrs
,
114 packet
->answers
[0].rdata
.netbios
.addresses
[i
].ipaddr
);
116 io
->out
.reply_addrs
[i
] = NULL
;
118 talloc_steal(mem_ctx
, io
->out
.name
.name
);
119 talloc_steal(mem_ctx
, io
->out
.name
.scope
);
127 wait for a name query reply
129 _PUBLIC_ NTSTATUS
nbt_name_query(struct nbt_name_socket
*nbtsock
,
130 TALLOC_CTX
*mem_ctx
, struct nbt_name_query
*io
)
132 struct nbt_name_request
*req
= nbt_name_query_send(nbtsock
, io
);
133 return nbt_name_query_recv(req
, mem_ctx
, io
);
138 send a nbt name status
140 _PUBLIC_
struct nbt_name_request
*nbt_name_status_send(struct nbt_name_socket
*nbtsock
,
141 struct nbt_name_status
*io
)
143 struct nbt_name_request
*req
;
144 struct nbt_name_packet
*packet
;
145 struct socket_address
*dest
;
147 packet
= talloc_zero(nbtsock
, struct nbt_name_packet
);
148 if (packet
== NULL
) return NULL
;
151 packet
->operation
= NBT_OPCODE_QUERY
;
153 packet
->questions
= talloc_array(packet
, struct nbt_name_question
, 1);
154 if (packet
->questions
== NULL
) goto failed
;
156 packet
->questions
[0].name
= io
->in
.name
;
157 packet
->questions
[0].question_type
= NBT_QTYPE_STATUS
;
158 packet
->questions
[0].question_class
= NBT_QCLASS_IP
;
160 dest
= socket_address_from_strings(packet
, nbtsock
->sock
->backend_name
,
161 io
->in
.dest_addr
, io
->in
.dest_port
);
162 if (dest
== NULL
) goto failed
;
163 req
= nbt_name_request_send(nbtsock
, nbtsock
, dest
, packet
,
164 io
->in
.timeout
, io
->in
.retries
, false);
165 if (req
== NULL
) goto failed
;
176 wait for a name status reply
178 _PUBLIC_ NTSTATUS
nbt_name_status_recv(struct nbt_name_request
*req
,
179 TALLOC_CTX
*mem_ctx
, struct nbt_name_status
*io
)
182 struct nbt_name_packet
*packet
;
185 status
= nbt_name_request_recv(req
);
186 if (!NT_STATUS_IS_OK(status
) ||
187 req
->num_replies
== 0) {
192 packet
= req
->replies
[0].packet
;
193 io
->out
.reply_from
= talloc_steal(mem_ctx
, req
->replies
[0].dest
->addr
);
195 if ((packet
->operation
& NBT_RCODE
) != 0) {
196 status
= nbt_rcode_to_ntstatus(packet
->operation
& NBT_RCODE
);
201 if (packet
->ancount
!= 1 ||
202 packet
->answers
[0].rr_type
!= NBT_QTYPE_STATUS
||
203 packet
->answers
[0].rr_class
!= NBT_QCLASS_IP
) {
205 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
208 io
->out
.name
= packet
->answers
[0].name
;
209 talloc_steal(mem_ctx
, io
->out
.name
.name
);
210 talloc_steal(mem_ctx
, io
->out
.name
.scope
);
212 io
->out
.status
= packet
->answers
[0].rdata
.status
;
213 talloc_steal(mem_ctx
, io
->out
.status
.names
);
214 for (i
=0;i
<io
->out
.status
.num_names
;i
++) {
215 talloc_steal(io
->out
.status
.names
, io
->out
.status
.names
[i
].name
);
225 wait for a name status reply
227 _PUBLIC_ NTSTATUS
nbt_name_status(struct nbt_name_socket
*nbtsock
,
228 TALLOC_CTX
*mem_ctx
, struct nbt_name_status
*io
)
230 struct nbt_name_request
*req
= nbt_name_status_send(nbtsock
, io
);
231 return nbt_name_status_recv(req
, mem_ctx
, io
);