lib: Give lib/util/util_file.c its own header file
[samba.git] / source4 / 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 "lib/util/util_file.h"
25 #include "libcli/dgram/libdgram.h"
26 #include "lib/socket/socket.h"
27 #include "libcli/resolve/resolve.h"
28 #include "librpc/gen_ndr/ndr_nbt.h"
29 #include "param/param.h"
30
31 NTSTATUS dgram_mailslot_browse_send(struct nbt_dgram_socket *dgmsock,
32                                     struct nbt_name *dest_name,
33                                     struct socket_address *dest,
34                                     struct nbt_name *src_name,
35                                     struct nbt_browse_packet *request)
36 {
37         NTSTATUS status;
38         enum ndr_err_code ndr_err;
39         DATA_BLOB blob;
40         TALLOC_CTX *tmp_ctx = talloc_new(dgmsock);
41
42         ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, request,
43                                       (ndr_push_flags_fn_t)ndr_push_nbt_browse_packet);
44         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
45                 talloc_free(tmp_ctx);
46                 return ndr_map_error2ntstatus(ndr_err);
47         }
48
49         status = dgram_mailslot_send(dgmsock, DGRAM_DIRECT_UNIQUE,
50                                      NBT_MAILSLOT_BROWSE,
51                                      dest_name, dest,
52                                      src_name, &blob);
53         talloc_free(tmp_ctx);
54         return status;
55 }
56
57 NTSTATUS dgram_mailslot_browse_reply(struct nbt_dgram_socket *dgmsock,
58                                      struct nbt_dgram_packet *request,
59                                      const char *mailslot_name,
60                                      const char *my_netbios_name,
61                                      struct nbt_browse_packet *reply)
62 {
63         NTSTATUS status;
64         enum ndr_err_code ndr_err;
65         DATA_BLOB blob;
66         TALLOC_CTX *tmp_ctx = talloc_new(dgmsock);
67         struct nbt_name myname;
68         struct socket_address *dest;
69
70         ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, reply,
71                                       (ndr_push_flags_fn_t)ndr_push_nbt_browse_packet);
72         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
73                 talloc_free(tmp_ctx);
74                 return ndr_map_error2ntstatus(ndr_err);
75         }
76
77         make_nbt_name_client(&myname, my_netbios_name);
78
79         dest = socket_address_from_strings(tmp_ctx, dgmsock->sock->backend_name,
80                                            request->src_addr, request->src_port);
81         if (!dest) {
82                 talloc_free(tmp_ctx);
83                 return NT_STATUS_NO_MEMORY;
84         }
85
86         status = dgram_mailslot_send(dgmsock, DGRAM_DIRECT_UNIQUE,
87                                      mailslot_name,
88                                      &request->data.msg.source_name,
89                                      dest,
90                                      &myname, &blob);
91         talloc_free(tmp_ctx);
92         return status;
93 }
94
95 NTSTATUS dgram_mailslot_browse_parse(struct dgram_mailslot_handler *dgmslot,
96                                      TALLOC_CTX *mem_ctx,
97                                      struct nbt_dgram_packet *dgram,
98                                      struct nbt_browse_packet *pkt)
99 {
100         DATA_BLOB data = dgram_mailslot_data(dgram);
101         enum ndr_err_code ndr_err;
102
103         ndr_err = ndr_pull_struct_blob(&data, mem_ctx, pkt,
104                                       (ndr_pull_flags_fn_t)ndr_pull_nbt_browse_packet);
105         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
106                 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
107                 DEBUG(0,("Failed to parse browse packet of length %d: %s\n",
108                          (int)data.length, nt_errstr(status)));
109                 if (DEBUGLVL(10)) {
110                         (void)file_save("browse.dat", data.data, data.length);
111                 }
112                 return status;
113         }
114         return NT_STATUS_OK;
115 }