Revert "Revert "s3-printing: update parent smbd pcap cache""
[Samba.git] / source4 / nbt_server / dgram / request.c
blob277b64741d1890adb7e8f1c706958e9e48a9ad37
1 /*
2 Unix SMB/CIFS implementation.
4 NBT datagram server
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 3 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, see <http://www.gnu.org/licenses/>.
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 #include "param/param.h"
32 a list of mailslots that we have static handlers for
34 static const struct {
35 const char *mailslot_name;
36 dgram_mailslot_handler_t handler;
37 } mailslot_handlers[] = {
38 /* Handle both NTLOGON and NETLOGON in the same function, as
39 * they are very similar */
40 { NBT_MAILSLOT_NETLOGON, nbtd_mailslot_netlogon_handler },
41 { NBT_MAILSLOT_NTLOGON, nbtd_mailslot_netlogon_handler },
42 { NBT_MAILSLOT_BROWSE, nbtd_mailslot_browse_handler }
46 receive an incoming dgram request. This is used for general datagram
47 requests. Mailslot requests for our listening mailslots
48 are handled in the specific mailslot handlers
50 void dgram_request_handler(struct nbt_dgram_socket *dgmsock,
51 struct nbt_dgram_packet *packet,
52 struct socket_address *src)
54 DEBUG(0,("General datagram request from %s:%d\n", src->addr, src->port));
55 NDR_PRINT_DEBUG(nbt_dgram_packet, packet);
60 setup the port 138 datagram listener for a given interface
62 NTSTATUS nbtd_dgram_setup(struct nbtd_interface *iface, const char *bind_address)
64 struct nbt_dgram_socket *bcast_dgmsock = NULL;
65 struct nbtd_server *nbtsrv = iface->nbtsrv;
66 struct socket_address *bcast_addr, *bind_addr;
67 NTSTATUS status;
68 TALLOC_CTX *tmp_ctx = talloc_new(iface);
69 /* the list of mailslots that we are interested in */
70 int i;
72 if (!tmp_ctx) {
73 return NT_STATUS_NO_MEMORY;
76 if (strcmp("0.0.0.0", iface->netmask) != 0) {
77 /* listen for broadcasts on port 138 */
78 bcast_dgmsock = nbt_dgram_socket_init(iface,
79 nbtsrv->task->event_ctx,
80 lp_iconv_convenience(nbtsrv->task->lp_ctx));
81 if (!bcast_dgmsock) {
82 talloc_free(tmp_ctx);
83 return NT_STATUS_NO_MEMORY;
86 bcast_addr = socket_address_from_strings(tmp_ctx, bcast_dgmsock->sock->backend_name,
87 iface->bcast_address,
88 lp_dgram_port(iface->nbtsrv->task->lp_ctx));
89 if (!bcast_addr) {
90 talloc_free(tmp_ctx);
91 return NT_STATUS_NO_MEMORY;
94 status = socket_listen(bcast_dgmsock->sock, bcast_addr, 0, 0);
95 if (!NT_STATUS_IS_OK(status)) {
96 talloc_free(tmp_ctx);
97 DEBUG(0,("Failed to bind to %s:%d - %s\n",
98 iface->bcast_address, lp_dgram_port(iface->nbtsrv->task->lp_ctx),
99 nt_errstr(status)));
100 return status;
103 dgram_set_incoming_handler(bcast_dgmsock, dgram_request_handler, iface);
106 /* listen for unicasts on port 138 */
107 iface->dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx,
108 lp_iconv_convenience(nbtsrv->task->lp_ctx));
109 if (!iface->dgmsock) {
110 talloc_free(tmp_ctx);
111 return NT_STATUS_NO_MEMORY;
114 bind_addr = socket_address_from_strings(tmp_ctx, iface->dgmsock->sock->backend_name,
115 bind_address, lp_dgram_port(iface->nbtsrv->task->lp_ctx));
116 if (!bind_addr) {
117 talloc_free(tmp_ctx);
118 return NT_STATUS_NO_MEMORY;
121 status = socket_listen(iface->dgmsock->sock, bind_addr, 0, 0);
122 if (!NT_STATUS_IS_OK(status)) {
123 talloc_free(tmp_ctx);
124 DEBUG(0,("Failed to bind to %s:%d - %s\n",
125 bind_address, lp_dgram_port(iface->nbtsrv->task->lp_ctx), nt_errstr(status)));
126 return status;
129 dgram_set_incoming_handler(iface->dgmsock, dgram_request_handler, iface);
131 talloc_free(tmp_ctx);
133 for (i=0;i<ARRAY_SIZE(mailslot_handlers);i++) {
134 /* note that we don't need to keep the pointer
135 to the dgmslot around - the callback is all
136 we need */
137 struct dgram_mailslot_handler *dgmslot;
139 if (bcast_dgmsock) {
140 dgmslot = dgram_mailslot_listen(bcast_dgmsock,
141 mailslot_handlers[i].mailslot_name,
142 mailslot_handlers[i].handler, iface);
143 NT_STATUS_HAVE_NO_MEMORY(dgmslot);
146 dgmslot = dgram_mailslot_listen(iface->dgmsock,
147 mailslot_handlers[i].mailslot_name,
148 mailslot_handlers[i].handler, iface);
149 NT_STATUS_HAVE_NO_MEMORY(dgmslot);
152 return NT_STATUS_OK;