f222148f4d6fccb2b8dd314c285e40a4be96d6f5
[garming/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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/nbt/libnbt.h"
25
26 /*
27   send a nbt name query
28 */
29 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
35         packet = talloc_zero(nbtsock, struct nbt_name_packet);
36         if (packet == NULL) return NULL;
37
38         packet->qdcount = 1;
39         packet->operation = NBT_OPCODE_QUERY;
40         if (io->in.broadcast) {
41                 packet->operation |= NBT_FLAG_BROADCAST;
42         }
43         if (io->in.wins_lookup) {
44                 packet->operation |= NBT_FLAG_RECURSION_DESIRED;
45         }
46
47         packet->questions = talloc_array(packet, struct nbt_name_question, 1);
48         if (packet->questions == NULL) goto failed;
49
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;
53         
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;
57
58         talloc_free(packet);
59         return req;
60
61 failed:
62         talloc_free(packet);
63         return NULL;    
64 }
65
66 /*
67   wait for a name query reply
68 */
69 NTSTATUS nbt_name_query_recv(struct nbt_name_request *req, 
70                              TALLOC_CTX *mem_ctx, struct nbt_name_query *io)
71 {
72         NTSTATUS status;
73         struct nbt_name_packet *packet;
74         int i;
75
76         status = nbt_name_request_recv(req);
77         if (!NT_STATUS_IS_OK(status) ||
78             req->num_replies == 0) {
79                 talloc_free(req);
80                 return status;
81         }
82         
83         packet = req->replies[0].packet;
84         io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].reply_addr);
85
86         if ((packet->operation & NBT_RCODE) != 0) {
87                 status = nbt_rcode_to_ntstatus(packet->operation & NBT_RCODE);
88                 talloc_free(req);
89                 return status;
90         }
91
92         if (packet->ancount != 1 ||
93             packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
94             packet->answers[0].rr_class != NBT_QCLASS_IP) {
95                 talloc_free(req);
96                 return status;
97         }
98
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) {
103                 talloc_free(req);
104                 return NT_STATUS_NO_MEMORY;
105         }
106         
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);
110         }
111         io->out.reply_addrs[i] = NULL;
112
113         talloc_steal(mem_ctx, io->out.name.name);
114         talloc_steal(mem_ctx, io->out.name.scope);
115             
116         talloc_free(req);
117
118         return NT_STATUS_OK;
119 }
120
121 /*
122   wait for a name query reply
123 */
124 NTSTATUS nbt_name_query(struct nbt_name_socket *nbtsock, 
125                         TALLOC_CTX *mem_ctx, struct nbt_name_query *io)
126 {
127         struct nbt_name_request *req = nbt_name_query_send(nbtsock, io);
128         return nbt_name_query_recv(req, mem_ctx, io);
129 }
130
131
132 /*
133   send a nbt name status
134 */
135 struct nbt_name_request *nbt_name_status_send(struct nbt_name_socket *nbtsock,
136                                               struct nbt_name_status *io)
137 {
138         struct nbt_name_request *req;
139         struct nbt_name_packet *packet;
140
141         packet = talloc_zero(nbtsock, struct nbt_name_packet);
142         if (packet == NULL) return NULL;
143
144         packet->qdcount = 1;
145         packet->operation = NBT_OPCODE_QUERY;
146
147         packet->questions = talloc_array(packet, struct nbt_name_question, 1);
148         if (packet->questions == NULL) goto failed;
149
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;
153         
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;
157
158         talloc_free(packet);
159         return req;
160
161 failed:
162         talloc_free(packet);
163         return NULL;    
164 }
165
166 /*
167   wait for a name status reply
168 */
169 NTSTATUS nbt_name_status_recv(struct nbt_name_request *req, 
170                              TALLOC_CTX *mem_ctx, struct nbt_name_status *io)
171 {
172         NTSTATUS status;
173         struct nbt_name_packet *packet;
174         int i;
175
176         status = nbt_name_request_recv(req);
177         if (!NT_STATUS_IS_OK(status) ||
178             req->num_replies == 0) {
179                 talloc_free(req);
180                 return status;
181         }
182         
183         packet = req->replies[0].packet;
184         io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].reply_addr);
185
186         if ((packet->operation & NBT_RCODE) != 0) {
187                 status = nbt_rcode_to_ntstatus(packet->operation & NBT_RCODE);
188                 talloc_free(req);
189                 return status;
190         }
191
192         if (packet->ancount != 1 ||
193             packet->answers[0].rr_type != NBT_QTYPE_STATUS ||
194             packet->answers[0].rr_class != NBT_QCLASS_IP) {
195                 talloc_free(req);
196                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
197         }
198
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);
202
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);
207         }
208
209             
210         talloc_free(req);
211
212         return NT_STATUS_OK;
213 }
214
215 /*
216   wait for a name status reply
217 */
218 NTSTATUS nbt_name_status(struct nbt_name_socket *nbtsock, 
219                         TALLOC_CTX *mem_ctx, struct nbt_name_status *io)
220 {
221         struct nbt_name_request *req = nbt_name_status_send(nbtsock, io);
222         return nbt_name_status_recv(req, mem_ctx, io);
223 }
224
225