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 "dlinklist.h"
26 #include "libcli/nbt/libnbt.h"
27 #include "libcli/dgram/libdgram.h"
28 #include "lib/socket/socket.h"
30 #define DGRAM_MAX_PACKET_SIZE 2048
34 handle recv events on a nbt dgram socket
36 static void dgm_socket_recv(struct nbt_dgram_socket *nbtsock)
38 TALLOC_CTX *tmp_ctx = talloc_new(nbtsock);
44 struct nbt_dgram_packet *packet;
46 blob = data_blob_talloc(tmp_ctx, NULL, DGRAM_MAX_PACKET_SIZE);
47 if (blob.data == NULL) {
52 status = socket_recvfrom(nbtsock->sock, blob.data, blob.length, &nread, 0,
53 &src_addr, &src_port);
54 if (!NT_STATUS_IS_OK(status)) {
58 talloc_steal(tmp_ctx, src_addr);
61 DEBUG(0,("Received dgram packet of length %d from %s:%d\n",
62 blob.length, src_addr, src_port));
64 packet = talloc(tmp_ctx, struct nbt_dgram_packet);
70 /* parse the request */
71 status = ndr_pull_struct_blob(&blob, packet, packet,
72 (ndr_pull_flags_fn_t)ndr_pull_nbt_dgram_packet);
73 if (!NT_STATUS_IS_OK(status)) {
74 DEBUG(2,("Failed to parse incoming NBT DGRAM packet - %s\n",
80 NDR_PRINT_DEBUG(nbt_dgram_packet, packet);
87 handle fd events on a nbt_dgram_socket
89 static void dgm_socket_handler(struct event_context *ev, struct fd_event *fde,
90 uint16_t flags, void *private)
92 struct nbt_dgram_socket *dgmsock = talloc_get_type(private,
93 struct nbt_dgram_socket);
94 if (flags & EVENT_FD_WRITE) {
95 /* nothing at the moment */
96 } else if (flags & EVENT_FD_READ) {
97 dgm_socket_recv(dgmsock);
102 initialise a nbt_dgram_socket. The event_ctx is optional, if provided
103 then operations will use that event context
105 struct nbt_dgram_socket *nbt_dgram_socket_init(TALLOC_CTX *mem_ctx,
106 struct event_context *event_ctx)
108 struct nbt_dgram_socket *dgmsock;
111 dgmsock = talloc(mem_ctx, struct nbt_dgram_socket);
112 if (dgmsock == NULL) goto failed;
114 if (event_ctx == NULL) {
115 dgmsock->event_ctx = event_context_init(dgmsock);
117 dgmsock->event_ctx = talloc_reference(dgmsock, event_ctx);
119 if (dgmsock->event_ctx == NULL) goto failed;
121 status = socket_create("ip", SOCKET_TYPE_DGRAM, &dgmsock->sock, 0);
122 if (!NT_STATUS_IS_OK(status)) goto failed;
124 socket_set_option(dgmsock->sock, "SO_BROADCAST", "1");
126 talloc_steal(dgmsock, dgmsock->sock);
128 dgmsock->fde = event_add_fd(dgmsock->event_ctx, dgmsock,
129 socket_get_fd(dgmsock->sock), 0,
130 dgm_socket_handler, dgmsock);
135 talloc_free(dgmsock);
141 setup a handler for incoming requests
143 NTSTATUS dgram_set_incoming_handler(struct nbt_dgram_socket *dgmsock,
144 void (*handler)(struct nbt_dgram_socket *,
145 struct nbt_dgram_packet *,
149 dgmsock->incoming.handler = handler;
150 dgmsock->incoming.private = private;
151 EVENT_FD_READABLE(dgmsock->fde);