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