84a2a7e534e215efa6ef5b83fcdb714cc4a3c88b
[jelmer/samba4-debian.git] / source / libcli / dgram / browse.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    handling for browsing dgram requests
5
6    Copyright (C) Jelmer Vernooij 2005
7         Heavily based on ntlogon.c
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "libcli/dgram/libdgram.h"
25 #include "lib/socket/socket.h"
26 #include "libcli/resolve/resolve.h"
27 #include "librpc/gen_ndr/ndr_nbt.h"
28
29 NTSTATUS dgram_mailslot_browse_send(struct nbt_dgram_socket *dgmsock,
30                                     struct nbt_name *dest_name,
31                                     struct socket_address *dest,
32                                     struct nbt_name *src_name,
33                                     struct nbt_browse_packet *request)
34 {
35         NTSTATUS status;
36         DATA_BLOB blob;
37         TALLOC_CTX *tmp_ctx = talloc_new(dgmsock);
38
39         status = ndr_push_struct_blob(&blob, tmp_ctx, request, 
40                                       (ndr_push_flags_fn_t)ndr_push_nbt_browse_packet);
41         if (!NT_STATUS_IS_OK(status)) {
42                 talloc_free(tmp_ctx);
43                 return status;
44         }
45
46         status = dgram_mailslot_send(dgmsock, DGRAM_DIRECT_UNIQUE, 
47                                      NBT_MAILSLOT_BROWSE,
48                                      dest_name, dest, 
49                                      src_name, &blob);
50         talloc_free(tmp_ctx);
51         return status;
52 }
53
54 NTSTATUS dgram_mailslot_browse_reply(struct nbt_dgram_socket *dgmsock,
55                                      struct nbt_dgram_packet *request,
56                                      const char *mailslot_name,
57                                      struct nbt_browse_packet *reply)
58 {
59         NTSTATUS status;
60         DATA_BLOB blob;
61         TALLOC_CTX *tmp_ctx = talloc_new(dgmsock);
62         struct nbt_name myname;
63         struct socket_address *dest;
64
65         status = ndr_push_struct_blob(&blob, tmp_ctx, reply, 
66                                       (ndr_push_flags_fn_t)ndr_push_nbt_browse_packet);
67         if (!NT_STATUS_IS_OK(status)) {
68                 talloc_free(tmp_ctx);
69                 return status;
70         }
71
72         make_nbt_name_client(&myname, lp_netbios_name());
73
74         dest = socket_address_from_strings(tmp_ctx, dgmsock->sock->backend_name, 
75                                            request->src_addr, request->src_port);
76         if (!dest) {
77                 talloc_free(tmp_ctx);
78                 return NT_STATUS_NO_MEMORY;
79         }
80
81         status = dgram_mailslot_send(dgmsock, DGRAM_DIRECT_UNIQUE, 
82                                      mailslot_name,
83                                      &request->data.msg.source_name,
84                                      dest,
85                                      &myname, &blob);
86         talloc_free(tmp_ctx);
87         return status;
88 }
89
90 NTSTATUS dgram_mailslot_browse_parse(struct dgram_mailslot_handler *dgmslot,
91                                      TALLOC_CTX *mem_ctx,
92                                      struct nbt_dgram_packet *dgram,
93                                      struct nbt_browse_packet *pkt)
94 {
95         DATA_BLOB data = dgram_mailslot_data(dgram);
96         NTSTATUS status;
97
98         status = ndr_pull_struct_blob(&data, mem_ctx, pkt, 
99                                       (ndr_pull_flags_fn_t)ndr_pull_nbt_browse_packet);
100         if (!NT_STATUS_IS_OK(status)) {
101                 DEBUG(0,("Failed to parse browse packet of length %d\n", 
102                          (int)data.length));
103                 if (DEBUGLVL(10)) {
104                         file_save("browse.dat", data.data, data.length);
105                 }
106         }
107         return status;
108 }