1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/*
Unix SMB/CIFS implementation.
low level socket handling for nbt dgram requests (UDP138)
Copyright (C) Andrew Tridgell 2005
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "lib/events/events.h"
#include "../lib/util/dlinklist.h"
#include "libcli/dgram/libdgram.h"
#include "lib/socket/socket.h"
#include "librpc/gen_ndr/ndr_nbt.h"
/*
handle recv events on a nbt dgram socket
*/
static void dgm_socket_recv(struct nbt_dgram_socket *dgmsock)
{
TALLOC_CTX *tmp_ctx = talloc_new(dgmsock);
NTSTATUS status;
struct socket_address *src;
DATA_BLOB blob;
size_t nread, dsize;
struct nbt_dgram_packet *packet;
const char *mailslot_name;
enum ndr_err_code ndr_err;
status = socket_pending(dgmsock->sock, &dsize);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return;
}
blob = data_blob_talloc(tmp_ctx, NULL, dsize);
if ((dsize != 0) && (blob.data == NULL)) {
talloc_free(tmp_ctx);
return;
}
status = socket_recvfrom(dgmsock->sock, blob.data, blob.length, &nread,
tmp_ctx, &src);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return;
}
blob.length = nread;
DEBUG(5,("Received dgram packet of length %d from %s:%d\n",
(int)blob.length, src->addr, src->port));
packet = talloc(tmp_ctx, struct nbt_dgram_packet);
if (packet == NULL) {
talloc_free(tmp_ctx);
return;
}
/* parse the request */
ndr_err = ndr_pull_struct_blob(&blob, packet, packet,
(ndr_pull_flags_fn_t)ndr_pull_nbt_dgram_packet);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
status = ndr_map_error2ntstatus(ndr_err);
DEBUG(2,("Failed to parse incoming NBT DGRAM packet - %s\n",
nt_errstr(status)));
talloc_free(tmp_ctx);
return;
}
/* if this is a mailslot message, then see if we can dispatch it to a handler */
mailslot_name = dgram_mailslot_name(packet);
if (mailslot_name) {
struct dgram_mailslot_handler *dgmslot;
dgmslot = dgram_mailslot_find(dgmsock, mailslot_name);
if (dgmslot) {
dgmslot->handler(dgmslot, packet, src);
} else {
DEBUG(2,("No mailslot handler for '%s'\n", mailslot_name));
}
} else {
/* dispatch if there is a general handler */
if (dgmsock->incoming.handler) {
dgmsock->incoming.handler(dgmsock, packet, src);
}
}
talloc_free(tmp_ctx);
}
/*
handle send events on a nbt dgram socket
*/
static void dgm_socket_send(struct nbt_dgram_socket *dgmsock)
{
struct nbt_dgram_request *req;
NTSTATUS status;
while ((req = dgmsock->send_queue)) {
size_t len;
len = req->encoded.length;
status = socket_sendto(dgmsock->sock, &req->encoded, &len,
req->dest);
if (NT_STATUS_IS_ERR(status)) {
DEBUG(3,("Failed to send datagram of length %u to %s:%d: %s\n",
(unsigned)req->encoded.length, req->dest->addr, req->dest->port,
nt_errstr(status)));
DLIST_REMOVE(dgmsock->send_queue, req);
talloc_free(req);
continue;
}
if (!NT_STATUS_IS_OK(status)) return;
DLIST_REMOVE(dgmsock->send_queue, req);
talloc_free(req);
}
TEVENT_FD_NOT_WRITEABLE(dgmsock->fde);
return;
}
/*
handle fd events on a nbt_dgram_socket
*/
static void dgm_socket_handler(struct tevent_context *ev, struct tevent_fd *fde,
uint16_t flags, void *private_data)
{
struct nbt_dgram_socket *dgmsock = talloc_get_type(private_data,
struct nbt_dgram_socket);
if (flags & TEVENT_FD_WRITE) {
dgm_socket_send(dgmsock);
}
if (flags & TEVENT_FD_READ) {
dgm_socket_recv(dgmsock);
}
}
/*
initialise a nbt_dgram_socket. The event_ctx is optional, if provided
then operations will use that event context
*/
struct nbt_dgram_socket *nbt_dgram_socket_init(TALLOC_CTX *mem_ctx,
struct tevent_context *event_ctx)
{
struct nbt_dgram_socket *dgmsock;
NTSTATUS status;
dgmsock = talloc(mem_ctx, struct nbt_dgram_socket);
if (dgmsock == NULL) goto failed;
dgmsock->event_ctx = event_ctx;
if (dgmsock->event_ctx == NULL) goto failed;
status = socket_create("ip", SOCKET_TYPE_DGRAM, &dgmsock->sock, 0);
if (!NT_STATUS_IS_OK(status)) goto failed;
socket_set_option(dgmsock->sock, "SO_BROADCAST", "1");
talloc_steal(dgmsock, dgmsock->sock);
dgmsock->fde = tevent_add_fd(dgmsock->event_ctx, dgmsock,
socket_get_fd(dgmsock->sock), 0,
dgm_socket_handler, dgmsock);
dgmsock->send_queue = NULL;
dgmsock->incoming.handler = NULL;
dgmsock->mailslot_handlers = NULL;
return dgmsock;
failed:
talloc_free(dgmsock);
return NULL;
}
/*
setup a handler for generic incoming requests
*/
NTSTATUS dgram_set_incoming_handler(struct nbt_dgram_socket *dgmsock,
void (*handler)(struct nbt_dgram_socket *,
struct nbt_dgram_packet *,
struct socket_address *),
void *private_data)
{
dgmsock->incoming.handler = handler;
dgmsock->incoming.private_data = private_data;
TEVENT_FD_READABLE(dgmsock->fde);
return NT_STATUS_OK;
}
/*
queue a datagram for send
*/
NTSTATUS nbt_dgram_send(struct nbt_dgram_socket *dgmsock,
struct nbt_dgram_packet *packet,
struct socket_address *dest)
{
struct nbt_dgram_request *req;
NTSTATUS status = NT_STATUS_NO_MEMORY;
enum ndr_err_code ndr_err;
req = talloc(dgmsock, struct nbt_dgram_request);
if (req == NULL) goto failed;
req->dest = dest;
if (talloc_reference(req, dest) == NULL) goto failed;
ndr_err = ndr_push_struct_blob(&req->encoded, req, packet,
(ndr_push_flags_fn_t)ndr_push_nbt_dgram_packet);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
status = ndr_map_error2ntstatus(ndr_err);
goto failed;
}
DLIST_ADD_END(dgmsock->send_queue, req, struct nbt_dgram_request *);
TEVENT_FD_WRITEABLE(dgmsock->fde);
return NT_STATUS_OK;
failed:
talloc_free(req);
return status;
}
|