r23792: convert Samba4 to GPLv3
[garming/samba-autobuild/.git] / source4 / 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 "lib/socket/socket.h"
25
26 /*
27   send a nbt name release request
28 */
29 struct nbt_name_request *nbt_name_release_send(struct nbt_name_socket *nbtsock,
30                                                struct nbt_name_release *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->arcount = 1;
41         packet->operation = NBT_OPCODE_RELEASE;
42         if (io->in.broadcast) {
43                 packet->operation |= NBT_FLAG_BROADCAST;
44         }
45
46         packet->questions = talloc_array(packet, struct nbt_name_question, 1);
47         if (packet->questions == NULL) goto failed;
48
49         packet->questions[0].name           = io->in.name;
50         packet->questions[0].question_type  = NBT_QTYPE_NETBIOS;
51         packet->questions[0].question_class = NBT_QCLASS_IP;
52
53         packet->additional = talloc_array(packet, struct nbt_res_rec, 1);
54         if (packet->additional == NULL) goto failed;
55
56         packet->additional[0].name                   = io->in.name;
57         packet->additional[0].rr_type                = NBT_QTYPE_NETBIOS;
58         packet->additional[0].rr_class               = NBT_QCLASS_IP;
59         packet->additional[0].ttl                    = 0;
60         packet->additional[0].rdata.netbios.length   = 6;
61         packet->additional[0].rdata.netbios.addresses = talloc_array(packet->additional,
62                                                                      struct nbt_rdata_address, 1);
63         if (packet->additional[0].rdata.netbios.addresses == NULL) goto failed;
64         packet->additional[0].rdata.netbios.addresses[0].nb_flags = io->in.nb_flags;
65         packet->additional[0].rdata.netbios.addresses[0].ipaddr = 
66                 talloc_strdup(packet->additional, io->in.address);
67
68         dest = socket_address_from_strings(packet, nbtsock->sock->backend_name, 
69                                            io->in.dest_addr, lp_nbt_port());
70         if (dest == NULL) goto failed;
71         req = nbt_name_request_send(nbtsock, dest, packet,
72                                     io->in.timeout, io->in.retries, False);
73         if (req == NULL) goto failed;
74
75         talloc_free(packet);
76         return req;
77
78 failed:
79         talloc_free(packet);
80         return NULL;    
81 }
82
83 /*
84   wait for a release reply
85 */
86 NTSTATUS nbt_name_release_recv(struct nbt_name_request *req, 
87                                TALLOC_CTX *mem_ctx, struct nbt_name_release *io)
88 {
89         NTSTATUS status;
90         struct nbt_name_packet *packet;
91
92         status = nbt_name_request_recv(req);
93         if (!NT_STATUS_IS_OK(status) ||
94             req->num_replies == 0) {
95                 talloc_free(req);
96                 return status;
97         }
98         
99         packet = req->replies[0].packet;
100         io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
101
102         if (packet->ancount != 1 ||
103             packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
104             packet->answers[0].rr_class != NBT_QCLASS_IP) {
105                 talloc_free(req);
106                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
107         }
108
109         io->out.rcode = packet->operation & NBT_RCODE;
110         io->out.name = packet->answers[0].name;
111         if (packet->answers[0].rdata.netbios.length < 6) {
112                 talloc_free(req);
113                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
114         }
115         io->out.reply_addr = talloc_steal(mem_ctx, 
116                                           packet->answers[0].rdata.netbios.addresses[0].ipaddr);
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   synchronous name release request
127 */
128 NTSTATUS nbt_name_release(struct nbt_name_socket *nbtsock, 
129                            TALLOC_CTX *mem_ctx, struct nbt_name_release *io)
130 {
131         struct nbt_name_request *req = nbt_name_release_send(nbtsock, io);
132         return nbt_name_release_recv(req, mem_ctx, io);
133 }