r12608: Remove some unused #include lines.
[kamenim/samba.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                            const struct nbt_peer_socket *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;
60         struct nbtd_server *nbtsrv = iface->nbtsrv;
61         NTSTATUS status;
62         /* the list of mailslots that we are interested in */
63         int i;
64
65         /* listen for broadcasts on port 138 */
66         bcast_dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx);
67         NT_STATUS_HAVE_NO_MEMORY(bcast_dgmsock);
68         
69         status = socket_listen(bcast_dgmsock->sock, iface->bcast_address, 
70                                lp_dgram_port(), 0, 0);
71         if (!NT_STATUS_IS_OK(status)) {
72                 DEBUG(0,("Failed to bind to %s:%d - %s\n", 
73                          iface->bcast_address, lp_dgram_port(), nt_errstr(status)));
74                 return status;
75         }
76         
77         dgram_set_incoming_handler(bcast_dgmsock, dgram_request_handler, iface);
78
79         /* listen for unicasts on port 138 */
80         iface->dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx);
81         NT_STATUS_HAVE_NO_MEMORY(iface->dgmsock);
82
83         status = socket_listen(iface->dgmsock->sock, bind_address, 
84                                lp_dgram_port(), 0, 0);
85         if (!NT_STATUS_IS_OK(status)) {
86                 DEBUG(0,("Failed to bind to %s:%d - %s\n", 
87                          bind_address, lp_dgram_port(), nt_errstr(status)));
88                 return status;
89         }
90         dgram_set_incoming_handler(iface->dgmsock, dgram_request_handler, iface);
91
92
93         for (i=0;i<ARRAY_SIZE(mailslot_handlers);i++) {
94                 /* note that we don't need to keep the pointer
95                    to the dgmslot around - the callback is all
96                    we need */
97                 struct dgram_mailslot_handler *dgmslot;
98
99                 dgmslot = dgram_mailslot_listen(bcast_dgmsock, 
100                                                 mailslot_handlers[i].mailslot_name,
101                                                 mailslot_handlers[i].handler, iface);
102                 NT_STATUS_HAVE_NO_MEMORY(dgmslot);
103
104                 dgmslot = dgram_mailslot_listen(iface->dgmsock, 
105                                                 mailslot_handlers[i].mailslot_name,
106                                                 mailslot_handlers[i].handler, iface);
107                 NT_STATUS_HAVE_NO_MEMORY(dgmslot);
108         }
109
110         return NT_STATUS_OK;
111 }