2 Unix SMB/CIFS implementation.
4 low level socket handling for nbt dgram requests (UDP138)
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 "lib/events/events.h"
25 #include "lib/util/dlinklist.h"
26 #include "libcli/dgram/libdgram.h"
27 #include "lib/socket/socket.h"
28 #include "librpc/gen_ndr/ndr_nbt.h"
32 handle recv events on a nbt dgram socket
34 static void dgm_socket_recv(struct nbt_dgram_socket *dgmsock)
36 TALLOC_CTX *tmp_ctx = talloc_new(dgmsock);
38 struct socket_address *src;
41 struct nbt_dgram_packet *packet;
42 const char *mailslot_name;
44 status = socket_pending(dgmsock->sock, &dsize);
45 if (!NT_STATUS_IS_OK(status)) {
50 blob = data_blob_talloc(tmp_ctx, NULL, dsize);
51 if (blob.data == NULL) {
56 status = socket_recvfrom(dgmsock->sock, blob.data, blob.length, &nread,
58 if (!NT_STATUS_IS_OK(status)) {
64 DEBUG(2,("Received dgram packet of length %d from %s:%d\n",
65 (int)blob.length, src->addr, src->port));
67 packet = talloc(tmp_ctx, struct nbt_dgram_packet);
73 /* parse the request */
74 status = ndr_pull_struct_blob(&blob, packet, packet,
75 (ndr_pull_flags_fn_t)ndr_pull_nbt_dgram_packet);
76 if (!NT_STATUS_IS_OK(status)) {
77 DEBUG(2,("Failed to parse incoming NBT DGRAM packet - %s\n",
83 /* if this is a mailslot message, then see if we can dispatch it to a handler */
84 mailslot_name = dgram_mailslot_name(packet);
86 struct dgram_mailslot_handler *dgmslot;
87 dgmslot = dgram_mailslot_find(dgmsock, mailslot_name);
89 dgmslot->handler(dgmslot, packet, src);
91 DEBUG(2,("No mailslot handler for '%s'\n", mailslot_name));
94 /* dispatch if there is a general handler */
95 if (dgmsock->incoming.handler) {
96 dgmsock->incoming.handler(dgmsock, packet, src);
100 talloc_free(tmp_ctx);
105 handle send events on a nbt dgram socket
107 static void dgm_socket_send(struct nbt_dgram_socket *dgmsock)
109 struct nbt_dgram_request *req;
112 while ((req = dgmsock->send_queue)) {
115 len = req->encoded.length;
116 status = socket_sendto(dgmsock->sock, &req->encoded, &len,
118 if (NT_STATUS_IS_ERR(status)) {
119 DEBUG(3,("Failed to send datagram of length %u to %s:%d: %s\n",
120 (unsigned)req->encoded.length, req->dest->addr, req->dest->port,
122 DLIST_REMOVE(dgmsock->send_queue, req);
127 if (!NT_STATUS_IS_OK(status)) return;
129 DLIST_REMOVE(dgmsock->send_queue, req);
133 EVENT_FD_NOT_WRITEABLE(dgmsock->fde);
139 handle fd events on a nbt_dgram_socket
141 static void dgm_socket_handler(struct event_context *ev, struct fd_event *fde,
142 uint16_t flags, void *private)
144 struct nbt_dgram_socket *dgmsock = talloc_get_type(private,
145 struct nbt_dgram_socket);
146 if (flags & EVENT_FD_WRITE) {
147 dgm_socket_send(dgmsock);
149 if (flags & EVENT_FD_READ) {
150 dgm_socket_recv(dgmsock);
155 initialise a nbt_dgram_socket. The event_ctx is optional, if provided
156 then operations will use that event context
158 struct nbt_dgram_socket *nbt_dgram_socket_init(TALLOC_CTX *mem_ctx,
159 struct event_context *event_ctx)
161 struct nbt_dgram_socket *dgmsock;
164 dgmsock = talloc(mem_ctx, struct nbt_dgram_socket);
165 if (dgmsock == NULL) goto failed;
167 if (event_ctx == NULL) {
168 dgmsock->event_ctx = event_context_init(dgmsock);
170 dgmsock->event_ctx = talloc_reference(dgmsock, event_ctx);
172 if (dgmsock->event_ctx == NULL) goto failed;
174 status = socket_create("ip", SOCKET_TYPE_DGRAM, &dgmsock->sock, 0);
175 if (!NT_STATUS_IS_OK(status)) goto failed;
177 socket_set_option(dgmsock->sock, "SO_BROADCAST", "1");
179 talloc_steal(dgmsock, dgmsock->sock);
181 dgmsock->fde = event_add_fd(dgmsock->event_ctx, dgmsock,
182 socket_get_fd(dgmsock->sock), 0,
183 dgm_socket_handler, dgmsock);
185 dgmsock->send_queue = NULL;
186 dgmsock->incoming.handler = NULL;
187 dgmsock->mailslot_handlers = NULL;
192 talloc_free(dgmsock);
198 setup a handler for generic incoming requests
200 NTSTATUS dgram_set_incoming_handler(struct nbt_dgram_socket *dgmsock,
201 void (*handler)(struct nbt_dgram_socket *,
202 struct nbt_dgram_packet *,
203 struct socket_address *),
206 dgmsock->incoming.handler = handler;
207 dgmsock->incoming.private = private;
208 EVENT_FD_READABLE(dgmsock->fde);
214 queue a datagram for send
216 NTSTATUS nbt_dgram_send(struct nbt_dgram_socket *dgmsock,
217 struct nbt_dgram_packet *packet,
218 struct socket_address *dest)
220 struct nbt_dgram_request *req;
221 NTSTATUS status = NT_STATUS_NO_MEMORY;
223 req = talloc(dgmsock, struct nbt_dgram_request);
224 if (req == NULL) goto failed;
227 if (talloc_reference(req, dest) == NULL) goto failed;
229 status = ndr_push_struct_blob(&req->encoded, req, packet,
230 (ndr_push_flags_fn_t)ndr_push_nbt_dgram_packet);
231 if (!NT_STATUS_IS_OK(status)) goto failed;
233 DLIST_ADD_END(dgmsock->send_queue, req, struct nbt_dgram_request *);
235 EVENT_FD_WRITEABLE(dgmsock->fde);