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