2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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.
24 #include "nbt_server/nbt_server.h"
25 #include "smbd/service_task.h"
26 #include "lib/socket/socket.h"
29 a list of mailslots that we have static handlers for
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 }
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
45 void dgram_request_handler(struct nbt_dgram_socket *dgmsock,
46 struct nbt_dgram_packet *packet,
47 struct socket_address *src)
49 DEBUG(0,("General datagram request from %s:%d\n", src->addr, src->port));
50 NDR_PRINT_DEBUG(nbt_dgram_packet, packet);
55 setup the port 138 datagram listener for a given interface
57 NTSTATUS nbtd_dgram_setup(struct nbtd_interface *iface, const char *bind_address)
59 struct nbt_dgram_socket *bcast_dgmsock = NULL;
60 struct nbtd_server *nbtsrv = iface->nbtsrv;
61 struct socket_address *bcast_addr, *bind_addr;
63 TALLOC_CTX *tmp_ctx = talloc_new(iface);
64 /* the list of mailslots that we are interested in */
68 return NT_STATUS_NO_MEMORY;
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);
76 return NT_STATUS_NO_MEMORY;
79 bcast_addr = socket_address_from_strings(tmp_ctx, bcast_dgmsock->sock->backend_name,
80 iface->bcast_address, lp_dgram_port());
83 return NT_STATUS_NO_MEMORY;
86 status = socket_listen(bcast_dgmsock->sock, bcast_addr, 0, 0);
87 if (!NT_STATUS_IS_OK(status)) {
89 DEBUG(0,("Failed to bind to %s:%d - %s\n",
90 iface->bcast_address, lp_dgram_port(), nt_errstr(status)));
94 dgram_set_incoming_handler(bcast_dgmsock, dgram_request_handler, iface);
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;
104 bind_addr = socket_address_from_strings(tmp_ctx, iface->dgmsock->sock->backend_name,
105 bind_address, lp_dgram_port());
107 talloc_free(tmp_ctx);
108 return NT_STATUS_NO_MEMORY;
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)));
119 dgram_set_incoming_handler(iface->dgmsock, dgram_request_handler, iface);
121 talloc_free(tmp_ctx);
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
127 struct dgram_mailslot_handler *dgmslot;
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);
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);