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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
/*
Unix SMB/CIFS implementation.
Echo server service example
Copyright (C) 2010 Kai Blin <kai@samba.org>
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 "echo_server/echo_server.h"
/* Get at the config file settings */
#include "param/param.h"
/* This defines task_server_terminate */
#include "smbd/process_model.h"
/* We get load_interface_list from here */
#include "socket/netif.h"
/* NTSTATUS-related stuff */
#include "libcli/util/ntstatus.h"
/* tsocket-related functions */
#include "lib/tsocket/tsocket.h"
NTSTATUS server_service_echo_init(void);
/* Structure to hold an echo server socket */
struct echo_socket {
/* This can come handy for the task struct in there */
struct echo_server *echo;
struct tsocket_address *local_address;
};
/* Structure to hold udp socket */
struct echo_udp_socket {
struct echo_socket *echo_socket;
struct tdgram_context *dgram;
struct tevent_queue *send_queue;
};
/*
* Main processing function.
*
* This is the start of the package processing.
* In the echo server it doesn't do much, but for more complicated servers,
* your code goes here (or at least is called from here.
*/
static NTSTATUS echo_process(struct echo_server *echo,
TALLOC_CTX *mem_ctx,
DATA_BLOB *in,
DATA_BLOB *out)
{
uint8_t *buf = talloc_memdup(mem_ctx, in->data, in->length);
NT_STATUS_HAVE_NO_MEMORY(buf);
out->data = buf;
out->length = in->length;
return NT_STATUS_OK;
}
/* Structure keeping track of a single UDP echo server call */
struct echo_udp_call {
/* The UDP packet came from here, our reply goes there as well */
struct tsocket_address *src;
DATA_BLOB in;
DATA_BLOB out;
};
/** Prototype of the send callback */
static void echo_udp_call_sendto_done(struct tevent_req *subreq);
/* Callback to receive UDP packets */
static void echo_udp_call_loop(struct tevent_req *subreq)
{
/*
* Our socket structure is the callback data. Get it in a
* type-safe way
*/
struct echo_udp_socket *sock = tevent_req_callback_data(subreq,
struct echo_udp_socket);
struct echo_udp_call *call;
uint8_t *buf;
ssize_t len;
NTSTATUS status;
int sys_errno;
call = talloc(sock, struct echo_udp_call);
if (call == NULL) {
goto done;
}
len = tdgram_recvfrom_recv(subreq, &sys_errno, call, &buf, &call->src);
TALLOC_FREE(subreq);
if (len == -1) {
TALLOC_FREE(call);
goto done;
}
call->in.data = buf;
call->in.length = len;
DEBUG(10, ("Received echo UDP packet of %lu bytes from %s\n",
(long)len, tsocket_address_string(call->src, call)));
/* Handle the data coming in and compute the reply */
status = echo_process(sock->echo_socket->echo, call,
&call->in, &call->out);
if (!NT_STATUS_IS_OK(status)) {
TALLOC_FREE(call);
DEBUG(0, ("echo_process returned %s\n",
nt_errstr(status)));
goto done;
}
/* I said the task struct would come in handy. */
subreq = tdgram_sendto_queue_send(call,
sock->echo_socket->echo->task->event_ctx,
sock->dgram,
sock->send_queue,
call->out.data,
call->out.length,
call->src);
if (subreq == NULL) {
TALLOC_FREE(call);
goto done;
}
tevent_req_set_callback(subreq, echo_udp_call_sendto_done, call);
done:
/* Now loop for the next incoming UDP packet, the async way */
subreq = tdgram_recvfrom_send(sock,
sock->echo_socket->echo->task->event_ctx,
sock->dgram);
if (subreq == NULL) {
task_server_terminate(sock->echo_socket->echo->task,
"no memory for tdgram_recvfrom_send",
true);
return;
}
tevent_req_set_callback(subreq, echo_udp_call_loop, sock);
}
/* Callback to send UDP replies */
static void echo_udp_call_sendto_done(struct tevent_req *subreq)
{
struct echo_udp_call *call = tevent_req_callback_data(subreq,
struct echo_udp_call);
ssize_t ret;
int sys_errno;
ret = tdgram_sendto_queue_recv(subreq, &sys_errno);
/*
* We don't actually care about the error, just get on with our life.
* We already set a new echo_udp_call_loop callback already, so we're
* almost done, just some memory to free.
*/
TALLOC_FREE(call);
}
/* Start listening on a given address */
static NTSTATUS echo_add_socket(struct echo_server *echo,
const struct model_ops *ops,
const char *name,
const char *address,
uint16_t port)
{
struct echo_socket *echo_socket;
struct echo_udp_socket *echo_udp_socket;
struct tevent_req *udpsubreq;
NTSTATUS status;
int ret;
echo_socket = talloc(echo, struct echo_socket);
NT_STATUS_HAVE_NO_MEMORY(echo_socket);
echo_socket->echo = echo;
/*
* Initialize the tsocket_address.
* The nifty part is the "ip" string. This tells tsocket to autodetect
* ipv4 or ipv6 based on the IP address string passed.
*/
ret = tsocket_address_inet_from_strings(echo_socket, "ip",
address, port,
&echo_socket->local_address);
if (ret != 0) {
status = map_nt_error_from_unix(errno);
return status;
}
/* Now set up the udp socket */
echo_udp_socket = talloc(echo_socket, struct echo_udp_socket);
NT_STATUS_HAVE_NO_MEMORY(echo_udp_socket);
echo_udp_socket->echo_socket = echo_socket;
ret = tdgram_inet_udp_socket(echo_socket->local_address,
NULL,
echo_udp_socket,
&echo_udp_socket->dgram);
if (ret != 0) {
status = map_nt_error_from_unix(errno);
DEBUG(0, ("Failed to bind to %s:%u UDP - %s\n",
address, port, nt_errstr(status)));
return status;
}
/*
* We set up a send queue so we can have multiple UDP packets in flight
*/
echo_udp_socket->send_queue = tevent_queue_create(echo_udp_socket,
"echo_udp_send_queue");
NT_STATUS_HAVE_NO_MEMORY(echo_udp_socket->send_queue);
/*
* To handle the UDP requests, set up a new tevent request as a
* subrequest of the current one.
*/
udpsubreq = tdgram_recvfrom_send(echo_udp_socket,
echo->task->event_ctx,
echo_udp_socket->dgram);
NT_STATUS_HAVE_NO_MEMORY(udpsubreq);
tevent_req_set_callback(udpsubreq, echo_udp_call_loop, echo_udp_socket);
return NT_STATUS_OK;
}
/* Set up the listening sockets */
static NTSTATUS echo_startup_interfaces(struct echo_server *echo,
struct loadparm_context *lp_ctx,
struct interface *ifaces)
{
const struct model_ops *model_ops;
int num_interfaces;
TALLOC_CTX *tmp_ctx = talloc_new(echo);
NTSTATUS status;
int i;
/*
* Samba allows subtask to set their own process model.
* Available models currently are:
* - onefork (forks exactly one child process)
* - prefork (keep a couple of child processes around)
* - single (only run a single process)
* - standard (fork one subprocess per incoming connection)
* - thread (use threads instead of forks)
*
* For the echo server, the "single" process model works fine,
* you probably don't want to use the thread model unless you really
* know what you're doing.
*/
model_ops = process_model_startup("single");
if (model_ops == NULL) {
DEBUG(0, ("Can't find 'single' proces model_ops\n"));
return NT_STATUS_INTERNAL_ERROR;
}
num_interfaces = iface_list_count(ifaces);
for(i=0; i<num_interfaces; i++) {
const char *address = talloc_strdup(tmp_ctx, iface_list_n_ip(ifaces, i));
status = echo_add_socket(echo, model_ops, "echo", address, ECHO_SERVICE_PORT);
NT_STATUS_NOT_OK_RETURN(status);
}
TALLOC_FREE(tmp_ctx);
return NT_STATUS_OK;
}
/* Do the basic task initialization, check if the task should run */
static void echo_task_init(struct task_server *task)
{
struct interface *ifaces;
struct echo_server *echo;
NTSTATUS status;
/*
* For the purpose of the example, let's only start the server in DC
* and standalone modes, and not as a member server.
*/
switch(lpcfg_server_role(task->lp_ctx)) {
case ROLE_STANDALONE:
/* Yes, we want to run the echo server */
break;
case ROLE_DOMAIN_MEMBER:
task_server_terminate(task, "echo: Not starting echo server " \
"for domain members", false);
return;
case ROLE_DOMAIN_CONTROLLER:
/* Yes, we want to run the echo server */
break;
}
load_interface_list(task, task->lp_ctx, &ifaces);
if (iface_list_count(ifaces) == 0) {
task_server_terminate(task,
"echo: No network interfaces configured",
false);
return;
}
task_server_set_title(task, "task[echo]");
echo = talloc_zero(task, struct echo_server);
if (echo == NULL) {
task_server_terminate(task, "echo: Out of memory", true);
return;
}
echo->task = task;
status = echo_startup_interfaces(echo, task->lp_ctx, ifaces);
if (!NT_STATUS_IS_OK(status)) {
task_server_terminate(task, "echo: Failed to set up interfaces",
true);
return;
}
}
/*
* Register this server service with the main samba process.
*
* This is the function you need to put into the wscript_build file as
* init_function. All the real work happens in "echo_task_init" above.
*/
NTSTATUS server_service_echo_init(void)
{
return register_server_service("echo", echo_task_init);
}
|