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 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/>.
23 #include "lib/events/events.h"
24 #include "../lib/util/dlinklist.h"
25 #include "libcli/dgram/libdgram.h"
26 #include "lib/socket/socket.h"
27 #include "librpc/gen_ndr/ndr_nbt.h"
31 handle recv events on a nbt dgram socket
33 static void dgm_socket_recv(struct nbt_dgram_socket
*dgmsock
)
35 TALLOC_CTX
*tmp_ctx
= talloc_new(dgmsock
);
37 struct socket_address
*src
;
40 struct nbt_dgram_packet
*packet
;
41 const char *mailslot_name
;
42 enum ndr_err_code ndr_err
;
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 ((dsize
!= 0) && (blob
.data
== NULL
)) {
56 status
= socket_recvfrom(dgmsock
->sock
, blob
.data
, blob
.length
, &nread
,
58 if (!NT_STATUS_IS_OK(status
)) {
64 DEBUG(5,("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 ndr_err
= ndr_pull_struct_blob(&blob
, packet
, packet
,
75 (ndr_pull_flags_fn_t
)ndr_pull_nbt_dgram_packet
);
76 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
77 status
= ndr_map_error2ntstatus(ndr_err
);
78 DEBUG(2,("Failed to parse incoming NBT DGRAM packet - %s\n",
84 /* if this is a mailslot message, then see if we can dispatch it to a handler */
85 mailslot_name
= dgram_mailslot_name(packet
);
87 struct dgram_mailslot_handler
*dgmslot
;
88 dgmslot
= dgram_mailslot_find(dgmsock
, mailslot_name
);
90 dgmslot
->handler(dgmslot
, packet
, src
);
92 DEBUG(2,("No mailslot handler for '%s'\n", mailslot_name
));
93 /* dispatch if there is a general handler */
94 if (dgmsock
->incoming
.handler
) {
95 dgmsock
->incoming
.handler(dgmsock
, packet
, src
);
99 /* dispatch if there is a general handler */
100 if (dgmsock
->incoming
.handler
) {
101 dgmsock
->incoming
.handler(dgmsock
, packet
, src
);
105 talloc_free(tmp_ctx
);
110 handle send events on a nbt dgram socket
112 static void dgm_socket_send(struct nbt_dgram_socket
*dgmsock
)
114 struct nbt_dgram_request
*req
;
117 while ((req
= dgmsock
->send_queue
)) {
120 len
= req
->encoded
.length
;
121 status
= socket_sendto(dgmsock
->sock
, &req
->encoded
, &len
,
123 if (NT_STATUS_IS_ERR(status
)) {
124 DEBUG(3,("Failed to send datagram of length %u to %s:%d: %s\n",
125 (unsigned)req
->encoded
.length
, req
->dest
->addr
, req
->dest
->port
,
127 DLIST_REMOVE(dgmsock
->send_queue
, req
);
132 if (!NT_STATUS_IS_OK(status
)) return;
134 DLIST_REMOVE(dgmsock
->send_queue
, req
);
138 TEVENT_FD_NOT_WRITEABLE(dgmsock
->fde
);
144 handle fd events on a nbt_dgram_socket
146 static void dgm_socket_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
147 uint16_t flags
, void *private_data
)
149 struct nbt_dgram_socket
*dgmsock
= talloc_get_type(private_data
,
150 struct nbt_dgram_socket
);
151 if (flags
& TEVENT_FD_WRITE
) {
152 dgm_socket_send(dgmsock
);
154 if (flags
& TEVENT_FD_READ
) {
155 dgm_socket_recv(dgmsock
);
160 initialise a nbt_dgram_socket. The event_ctx is optional, if provided
161 then operations will use that event context
163 struct nbt_dgram_socket
*nbt_dgram_socket_init(TALLOC_CTX
*mem_ctx
,
164 struct tevent_context
*event_ctx
)
166 struct nbt_dgram_socket
*dgmsock
;
169 dgmsock
= talloc(mem_ctx
, struct nbt_dgram_socket
);
170 if (dgmsock
== NULL
) goto failed
;
172 dgmsock
->event_ctx
= event_ctx
;
173 if (dgmsock
->event_ctx
== NULL
) goto failed
;
175 status
= socket_create(dgmsock
, "ip", SOCKET_TYPE_DGRAM
,
177 if (!NT_STATUS_IS_OK(status
)) goto failed
;
179 socket_set_option(dgmsock
->sock
, "SO_BROADCAST", "1");
181 dgmsock
->fde
= tevent_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_data
= private_data
;
208 TEVENT_FD_READABLE(dgmsock
->fde
);
212 NTSTATUS
nbt_dgram_send_raw(struct nbt_dgram_socket
*dgmsock
,
213 struct socket_address
*dest
,
214 const DATA_BLOB pkt_blob
)
216 struct nbt_dgram_request
*req
;
217 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
219 req
= talloc(dgmsock
, struct nbt_dgram_request
);
224 req
->dest
= socket_address_copy(req
, dest
);
225 if (req
->dest
== NULL
) {
229 req
->encoded
= data_blob_dup_talloc(req
, pkt_blob
);
230 if (req
->encoded
.length
!= pkt_blob
.length
) {
234 DLIST_ADD_END(dgmsock
->send_queue
, req
);
236 TEVENT_FD_WRITEABLE(dgmsock
->fde
);
246 queue a datagram for send
248 NTSTATUS
nbt_dgram_send(struct nbt_dgram_socket
*dgmsock
,
249 struct nbt_dgram_packet
*packet
,
250 struct socket_address
*dest
)
252 struct nbt_dgram_request
*req
;
253 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
254 enum ndr_err_code ndr_err
;
256 req
= talloc(dgmsock
, struct nbt_dgram_request
);
257 if (req
== NULL
) goto failed
;
259 req
->dest
= socket_address_copy(req
, dest
);
260 if (req
->dest
== NULL
) goto failed
;
262 ndr_err
= ndr_push_struct_blob(&req
->encoded
, req
, packet
,
263 (ndr_push_flags_fn_t
)ndr_push_nbt_dgram_packet
);
264 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
265 status
= ndr_map_error2ntstatus(ndr_err
);
269 DLIST_ADD_END(dgmsock
->send_queue
, req
);
271 TEVENT_FD_WRITEABLE(dgmsock
->fde
);