r25398: Parse loadparm context to all lp_*() functions.
[garming/samba-autobuild/.git] / source4 / nbt_server / dgram / request.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NBT datagram server
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 "nbt_server/nbt_server.h"
24 #include "smbd/service_task.h"
25 #include "lib/socket/socket.h"
26 #include "libcli/resolve/resolve.h"
27 #include "nbt_server/dgram/proto.h"
28 #include "librpc/gen_ndr/ndr_nbt.h"
29 #include "param/param.h"
30
31 /*
32   a list of mailslots that we have static handlers for
33 */
34 static const struct {
35         const char *mailslot_name;
36         dgram_mailslot_handler_t handler;
37 } mailslot_handlers[] = {
38         { NBT_MAILSLOT_NETLOGON, nbtd_mailslot_netlogon_handler },
39         { NBT_MAILSLOT_NTLOGON,  nbtd_mailslot_ntlogon_handler },
40         { NBT_MAILSLOT_BROWSE,   nbtd_mailslot_browse_handler }
41 };
42
43 /*
44   receive an incoming dgram request. This is used for general datagram
45   requests. Mailslot requests for our listening mailslots
46   are handled in the specific mailslot handlers
47 */
48 void dgram_request_handler(struct nbt_dgram_socket *dgmsock, 
49                            struct nbt_dgram_packet *packet,
50                            struct socket_address *src)
51 {
52         DEBUG(0,("General datagram request from %s:%d\n", src->addr, src->port));
53         NDR_PRINT_DEBUG(nbt_dgram_packet, packet);
54 }
55
56
57 /*
58   setup the port 138 datagram listener for a given interface
59 */
60 NTSTATUS nbtd_dgram_setup(struct nbtd_interface *iface, const char *bind_address)
61 {
62         struct nbt_dgram_socket *bcast_dgmsock = NULL;
63         struct nbtd_server *nbtsrv = iface->nbtsrv;
64         struct socket_address *bcast_addr, *bind_addr;
65         NTSTATUS status;
66         TALLOC_CTX *tmp_ctx = talloc_new(iface);
67         /* the list of mailslots that we are interested in */
68         int i;
69
70         if (!tmp_ctx) {
71                 return NT_STATUS_NO_MEMORY;
72         }
73
74         if (strcmp("0.0.0.0", iface->netmask) != 0) {
75                 /* listen for broadcasts on port 138 */
76                 bcast_dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx);
77                 if (!bcast_dgmsock) {
78                         talloc_free(tmp_ctx);
79                         return NT_STATUS_NO_MEMORY;
80                 }
81         
82                 bcast_addr = socket_address_from_strings(tmp_ctx, bcast_dgmsock->sock->backend_name, 
83                                                          iface->bcast_address, 
84                                                          lp_dgram_port(global_loadparm));
85                 if (!bcast_addr) {
86                         talloc_free(tmp_ctx);
87                         return NT_STATUS_NO_MEMORY;
88                 }
89
90                 status = socket_listen(bcast_dgmsock->sock, bcast_addr, 0, 0);
91                 if (!NT_STATUS_IS_OK(status)) {
92                         talloc_free(tmp_ctx);
93                         DEBUG(0,("Failed to bind to %s:%d - %s\n", 
94                                  iface->bcast_address, lp_dgram_port(global_loadparm), 
95                                  nt_errstr(status)));
96                         return status;
97                 }
98         
99                 dgram_set_incoming_handler(bcast_dgmsock, dgram_request_handler, iface);
100         }
101
102         /* listen for unicasts on port 138 */
103         iface->dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx);
104         if (!iface->dgmsock) {
105                 talloc_free(tmp_ctx);
106                 return NT_STATUS_NO_MEMORY;
107         }
108
109         bind_addr = socket_address_from_strings(tmp_ctx, iface->dgmsock->sock->backend_name, 
110                                                 bind_address, lp_dgram_port(global_loadparm));
111         if (!bind_addr) {
112                 talloc_free(tmp_ctx);
113                 return NT_STATUS_NO_MEMORY;
114         }
115
116         status = socket_listen(iface->dgmsock->sock, bind_addr, 0, 0);
117         if (!NT_STATUS_IS_OK(status)) {
118                 talloc_free(tmp_ctx);
119                 DEBUG(0,("Failed to bind to %s:%d - %s\n", 
120                          bind_address, lp_dgram_port(global_loadparm), nt_errstr(status)));
121                 return status;
122         }
123
124         dgram_set_incoming_handler(iface->dgmsock, dgram_request_handler, iface);
125
126         talloc_free(tmp_ctx);
127
128         for (i=0;i<ARRAY_SIZE(mailslot_handlers);i++) {
129                 /* note that we don't need to keep the pointer
130                    to the dgmslot around - the callback is all
131                    we need */
132                 struct dgram_mailslot_handler *dgmslot;
133
134                 if (bcast_dgmsock) {
135                         dgmslot = dgram_mailslot_listen(bcast_dgmsock, 
136                                                 mailslot_handlers[i].mailslot_name,
137                                                 mailslot_handlers[i].handler, iface);
138                         NT_STATUS_HAVE_NO_MEMORY(dgmslot);
139                 }
140
141                 dgmslot = dgram_mailslot_listen(iface->dgmsock, 
142                                                 mailslot_handlers[i].mailslot_name,
143                                                 mailslot_handlers[i].handler, iface);
144                 NT_STATUS_HAVE_NO_MEMORY(dgmslot);
145         }
146
147         return NT_STATUS_OK;
148 }