r23792: convert Samba4 to GPLv3
[kai/samba-autobuild/.git] / source4 / libcli / nbt / namequery.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    make nbt name query requests
5
6    Copyright (C) Andrew Tridgell 2005
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/nbt/libnbt.h"
24 #include "lib/socket/socket.h"
25
26 /**
27   send a nbt name query
28 */
29 _PUBLIC_ struct nbt_name_request *nbt_name_query_send(struct nbt_name_socket *nbtsock,
30                                              struct nbt_name_query *io)
31 {
32         struct nbt_name_request *req;
33         struct nbt_name_packet *packet;
34         struct socket_address *dest;
35
36         packet = talloc_zero(nbtsock, struct nbt_name_packet);
37         if (packet == NULL) return NULL;
38
39         packet->qdcount = 1;
40         packet->operation = NBT_OPCODE_QUERY;
41         if (io->in.broadcast) {
42                 packet->operation |= NBT_FLAG_BROADCAST;
43         }
44         if (io->in.wins_lookup) {
45                 packet->operation |= NBT_FLAG_RECURSION_DESIRED;
46         }
47
48         packet->questions = talloc_array(packet, struct nbt_name_question, 1);
49         if (packet->questions == NULL) goto failed;
50
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;
54
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;
61
62         talloc_free(packet);
63         return req;
64
65 failed:
66         talloc_free(packet);
67         return NULL;    
68 }
69
70 /**
71   wait for a name query reply
72 */
73 _PUBLIC_ NTSTATUS nbt_name_query_recv(struct nbt_name_request *req, 
74                              TALLOC_CTX *mem_ctx, struct nbt_name_query *io)
75 {
76         NTSTATUS status;
77         struct nbt_name_packet *packet;
78         int i;
79
80         status = nbt_name_request_recv(req);
81         if (!NT_STATUS_IS_OK(status) ||
82             req->num_replies == 0) {
83                 talloc_free(req);
84                 return status;
85         }
86         
87         packet = req->replies[0].packet;
88         io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
89
90         if ((packet->operation & NBT_RCODE) != 0) {
91                 status = nbt_rcode_to_ntstatus(packet->operation & NBT_RCODE);
92                 talloc_free(req);
93                 return status;
94         }
95
96         if (packet->ancount != 1 ||
97             packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
98             packet->answers[0].rr_class != NBT_QCLASS_IP) {
99                 talloc_free(req);
100                 return status;
101         }
102
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) {
107                 talloc_free(req);
108                 return NT_STATUS_NO_MEMORY;
109         }
110         
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);
114         }
115         io->out.reply_addrs[i] = NULL;
116
117         talloc_steal(mem_ctx, io->out.name.name);
118         talloc_steal(mem_ctx, io->out.name.scope);
119             
120         talloc_free(req);
121
122         return NT_STATUS_OK;
123 }
124
125 /**
126   wait for a name query reply
127 */
128 _PUBLIC_ NTSTATUS nbt_name_query(struct nbt_name_socket *nbtsock, 
129                         TALLOC_CTX *mem_ctx, struct nbt_name_query *io)
130 {
131         struct nbt_name_request *req = nbt_name_query_send(nbtsock, io);
132         return nbt_name_query_recv(req, mem_ctx, io);
133 }
134
135
136 /**
137   send a nbt name status
138 */
139 _PUBLIC_ struct nbt_name_request *nbt_name_status_send(struct nbt_name_socket *nbtsock,
140                                               struct nbt_name_status *io)
141 {
142         struct nbt_name_request *req;
143         struct nbt_name_packet *packet;
144         struct socket_address *dest;
145
146         packet = talloc_zero(nbtsock, struct nbt_name_packet);
147         if (packet == NULL) return NULL;
148
149         packet->qdcount = 1;
150         packet->operation = NBT_OPCODE_QUERY;
151
152         packet->questions = talloc_array(packet, struct nbt_name_question, 1);
153         if (packet->questions == NULL) goto failed;
154
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;
158
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;
165
166         talloc_free(packet);
167         return req;
168
169 failed:
170         talloc_free(packet);
171         return NULL;    
172 }
173
174 /**
175   wait for a name status reply
176 */
177 _PUBLIC_ NTSTATUS nbt_name_status_recv(struct nbt_name_request *req, 
178                              TALLOC_CTX *mem_ctx, struct nbt_name_status *io)
179 {
180         NTSTATUS status;
181         struct nbt_name_packet *packet;
182         int i;
183
184         status = nbt_name_request_recv(req);
185         if (!NT_STATUS_IS_OK(status) ||
186             req->num_replies == 0) {
187                 talloc_free(req);
188                 return status;
189         }
190         
191         packet = req->replies[0].packet;
192         io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
193
194         if ((packet->operation & NBT_RCODE) != 0) {
195                 status = nbt_rcode_to_ntstatus(packet->operation & NBT_RCODE);
196                 talloc_free(req);
197                 return status;
198         }
199
200         if (packet->ancount != 1 ||
201             packet->answers[0].rr_type != NBT_QTYPE_STATUS ||
202             packet->answers[0].rr_class != NBT_QCLASS_IP) {
203                 talloc_free(req);
204                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
205         }
206
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);
210
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);
215         }
216
217             
218         talloc_free(req);
219
220         return NT_STATUS_OK;
221 }
222
223 /**
224   wait for a name status reply
225 */
226 _PUBLIC_ NTSTATUS nbt_name_status(struct nbt_name_socket *nbtsock, 
227                         TALLOC_CTX *mem_ctx, struct nbt_name_status *io)
228 {
229         struct nbt_name_request *req = nbt_name_status_send(nbtsock, io);
230         return nbt_name_status_recv(req, mem_ctx, io);
231 }
232
233