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