keep obsolete file in samba4 source directory.
[kai/samba-autobuild/.git] / libcli / nbt / namerelease.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    send out a name release request
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 "../libcli/nbt/nbt_proto.h"
25 #include "lib/socket/socket.h"
26 #include "param/param.h"
27
28 /*
29   send a nbt name release request
30 */
31 _PUBLIC_ struct nbt_name_request *nbt_name_release_send(struct nbt_name_socket *nbtsock,
32                                                struct nbt_name_release *io)
33 {
34         struct nbt_name_request *req;
35         struct nbt_name_packet *packet;
36         struct socket_address *dest;
37
38         packet = talloc_zero(nbtsock, struct nbt_name_packet);
39         if (packet == NULL) return NULL;
40
41         packet->qdcount = 1;
42         packet->arcount = 1;
43         packet->operation = NBT_OPCODE_RELEASE;
44         if (io->in.broadcast) {
45                 packet->operation |= NBT_FLAG_BROADCAST;
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         packet->additional = talloc_array(packet, struct nbt_res_rec, 1);
56         if (packet->additional == NULL) goto failed;
57
58         packet->additional[0].name                   = io->in.name;
59         packet->additional[0].rr_type                = NBT_QTYPE_NETBIOS;
60         packet->additional[0].rr_class               = NBT_QCLASS_IP;
61         packet->additional[0].ttl                    = 0;
62         packet->additional[0].rdata.netbios.length   = 6;
63         packet->additional[0].rdata.netbios.addresses = talloc_array(packet->additional,
64                                                                      struct nbt_rdata_address, 1);
65         if (packet->additional[0].rdata.netbios.addresses == NULL) goto failed;
66         packet->additional[0].rdata.netbios.addresses[0].nb_flags = io->in.nb_flags;
67         packet->additional[0].rdata.netbios.addresses[0].ipaddr =
68                 talloc_strdup(packet->additional, io->in.address);
69
70         dest = socket_address_from_strings(packet, nbtsock->sock->backend_name,
71                                            io->in.dest_addr, io->in.dest_port);
72         if (dest == NULL) goto failed;
73         req = nbt_name_request_send(nbtsock, dest, packet,
74                                     io->in.timeout, io->in.retries, false);
75         if (req == NULL) goto failed;
76
77         talloc_free(packet);
78         return req;
79
80 failed:
81         talloc_free(packet);
82         return NULL;
83 }
84
85 /*
86   wait for a release reply
87 */
88 _PUBLIC_ NTSTATUS nbt_name_release_recv(struct nbt_name_request *req,
89                                TALLOC_CTX *mem_ctx, struct nbt_name_release *io)
90 {
91         NTSTATUS status;
92         struct nbt_name_packet *packet;
93
94         status = nbt_name_request_recv(req);
95         if (!NT_STATUS_IS_OK(status) ||
96             req->num_replies == 0) {
97                 talloc_free(req);
98                 return status;
99         }
100
101         packet = req->replies[0].packet;
102         io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
103
104         if (packet->ancount != 1 ||
105             packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
106             packet->answers[0].rr_class != NBT_QCLASS_IP) {
107                 talloc_free(req);
108                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
109         }
110
111         io->out.rcode = packet->operation & NBT_RCODE;
112         io->out.name = packet->answers[0].name;
113         if (packet->answers[0].rdata.netbios.length < 6) {
114                 talloc_free(req);
115                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
116         }
117         io->out.reply_addr = talloc_steal(mem_ctx,
118                                           packet->answers[0].rdata.netbios.addresses[0].ipaddr);
119         talloc_steal(mem_ctx, io->out.name.name);
120         talloc_steal(mem_ctx, io->out.name.scope);
121
122         talloc_free(req);
123
124         return NT_STATUS_OK;
125 }
126
127 /*
128   synchronous name release request
129 */
130 _PUBLIC_ NTSTATUS nbt_name_release(struct nbt_name_socket *nbtsock,
131                            TALLOC_CTX *mem_ctx, struct nbt_name_release *io)
132 {
133         struct nbt_name_request *req = nbt_name_release_send(nbtsock, io);
134         return nbt_name_release_recv(req, mem_ctx, io);
135 }