r12607: fix the build
[abartlet/samba.git/.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 "dlinklist.h"
25 #include "nbt_server/nbt_server.h"
26 #include "smbd/service_task.h"
27 #include "lib/socket/socket.h"
28
29 /*
30   a list of mailslots that we have static handlers for
31 */
32 static const struct {
33         const char *mailslot_name;
34         dgram_mailslot_handler_t handler;
35 } mailslot_handlers[] = {
36         { NBT_MAILSLOT_NETLOGON, nbtd_mailslot_netlogon_handler },
37         { NBT_MAILSLOT_NTLOGON,  nbtd_mailslot_ntlogon_handler },
38         { NBT_MAILSLOT_BROWSE,   nbtd_mailslot_browse_handler }
39 };
40
41 /*
42   receive an incoming dgram request. This is used for general datagram
43   requests. Mailslot requests for our listening mailslots
44   are handled in the specific mailslot handlers
45 */
46 void dgram_request_handler(struct nbt_dgram_socket *dgmsock, 
47                            struct nbt_dgram_packet *packet,
48                            const struct nbt_peer_socket *src)
49 {
50         DEBUG(0,("General datagram request from %s:%d\n", src->addr, src->port));
51         NDR_PRINT_DEBUG(nbt_dgram_packet, packet);
52 }
53
54
55 /*
56   setup the port 138 datagram listener for a given interface
57 */
58 NTSTATUS nbtd_dgram_setup(struct nbtd_interface *iface, const char *bind_address)
59 {
60         struct nbt_dgram_socket *bcast_dgmsock;
61         struct nbtd_server *nbtsrv = iface->nbtsrv;
62         NTSTATUS status;
63         /* the list of mailslots that we are interested in */
64         int i;
65
66         /* listen for broadcasts on port 138 */
67         bcast_dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx);
68         NT_STATUS_HAVE_NO_MEMORY(bcast_dgmsock);
69         
70         status = socket_listen(bcast_dgmsock->sock, iface->bcast_address, 
71                                lp_dgram_port(), 0, 0);
72         if (!NT_STATUS_IS_OK(status)) {
73                 DEBUG(0,("Failed to bind to %s:%d - %s\n", 
74                          iface->bcast_address, lp_dgram_port(), nt_errstr(status)));
75                 return status;
76         }
77         
78         dgram_set_incoming_handler(bcast_dgmsock, dgram_request_handler, iface);
79
80         /* listen for unicasts on port 138 */
81         iface->dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx);
82         NT_STATUS_HAVE_NO_MEMORY(iface->dgmsock);
83
84         status = socket_listen(iface->dgmsock->sock, bind_address, 
85                                lp_dgram_port(), 0, 0);
86         if (!NT_STATUS_IS_OK(status)) {
87                 DEBUG(0,("Failed to bind to %s:%d - %s\n", 
88                          bind_address, lp_dgram_port(), nt_errstr(status)));
89                 return status;
90         }
91         dgram_set_incoming_handler(iface->dgmsock, dgram_request_handler, iface);
92
93
94         for (i=0;i<ARRAY_SIZE(mailslot_handlers);i++) {
95                 /* note that we don't need to keep the pointer
96                    to the dgmslot around - the callback is all
97                    we need */
98                 struct dgram_mailslot_handler *dgmslot;
99
100                 dgmslot = dgram_mailslot_listen(bcast_dgmsock, 
101                                                 mailslot_handlers[i].mailslot_name,
102                                                 mailslot_handlers[i].handler, iface);
103                 NT_STATUS_HAVE_NO_MEMORY(dgmslot);
104
105                 dgmslot = dgram_mailslot_listen(iface->dgmsock, 
106                                                 mailslot_handlers[i].mailslot_name,
107                                                 mailslot_handlers[i].handler, iface);
108                 NT_STATUS_HAVE_NO_MEMORY(dgmslot);
109         }
110
111         return NT_STATUS_OK;
112 }