2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2009
6 ** NOTE! The following LGPL license applies to the tevent
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "system/network.h"
27 #include "tsocket_internal.h"
29 struct tsocket_address *_tsocket_address_create(TALLOC_CTX *mem_ctx,
30 const struct tsocket_address_ops *ops,
36 void **ppstate = (void **)pstate;
37 struct tsocket_address *addr;
39 addr = talloc_zero(mem_ctx, struct tsocket_address);
44 addr->location = location;
45 addr->private_data = talloc_size(addr, psize);
46 if (!addr->private_data) {
50 talloc_set_name_const(addr->private_data, type);
52 *ppstate = addr->private_data;
56 char *tsocket_address_string(const struct tsocket_address *addr,
60 return talloc_strdup(mem_ctx, "NULL");
62 return addr->ops->string(addr, mem_ctx);
65 struct tsocket_address *_tsocket_address_copy(const struct tsocket_address *addr,
69 return addr->ops->copy(addr, mem_ctx, location);
72 struct tdgram_context {
74 const struct tdgram_context_ops *ops;
78 struct tdgram_context *_tdgram_context_create(TALLOC_CTX *mem_ctx,
79 const struct tdgram_context_ops *ops,
85 struct tdgram_context *dgram;
86 void **ppstate = (void **)pstate;
89 dgram = talloc(mem_ctx, struct tdgram_context);
93 dgram->location = location;
96 state = talloc_size(dgram, psize);
101 talloc_set_name_const(state, type);
103 dgram->private_data = state;
109 void *_tdgram_context_data(struct tdgram_context *dgram)
111 return dgram->private_data;
114 struct tdgram_recvfrom_state {
115 const struct tdgram_context_ops *ops;
118 struct tsocket_address *src;
121 static void tdgram_recvfrom_done(struct tevent_req *subreq);
123 struct tevent_req *tdgram_recvfrom_send(TALLOC_CTX *mem_ctx,
124 struct tevent_context *ev,
125 struct tdgram_context *dgram)
127 struct tevent_req *req;
128 struct tdgram_recvfrom_state *state;
129 struct tevent_req *subreq;
131 req = tevent_req_create(mem_ctx, &state,
132 struct tdgram_recvfrom_state);
137 state->ops = dgram->ops;
139 subreq = state->ops->recvfrom_send(state, ev, dgram);
140 if (tevent_req_nomem(subreq, req)) {
143 tevent_req_set_callback(subreq, tdgram_recvfrom_done, req);
148 tevent_req_post(req, ev);
152 static void tdgram_recvfrom_done(struct tevent_req *subreq)
154 struct tevent_req *req = tevent_req_callback_data(subreq,
156 struct tdgram_recvfrom_state *state = tevent_req_data(req,
157 struct tdgram_recvfrom_state);
161 ret = state->ops->recvfrom_recv(subreq, &sys_errno, state,
162 &state->buf, &state->src);
164 tevent_req_error(req, sys_errno);
170 tevent_req_done(req);
173 ssize_t tdgram_recvfrom_recv(struct tevent_req *req,
177 struct tsocket_address **src)
179 struct tdgram_recvfrom_state *state = tevent_req_data(req,
180 struct tdgram_recvfrom_state);
183 ret = tsocket_simple_int_recv(req, perrno);
185 *buf = talloc_move(mem_ctx, &state->buf);
188 *src = talloc_move(mem_ctx, &state->src);
192 tevent_req_received(req);
196 struct tdgram_sendto_state {
197 const struct tdgram_context_ops *ops;
201 static void tdgram_sendto_done(struct tevent_req *subreq);
203 struct tevent_req *tdgram_sendto_send(TALLOC_CTX *mem_ctx,
204 struct tevent_context *ev,
205 struct tdgram_context *dgram,
206 const uint8_t *buf, size_t len,
207 const struct tsocket_address *dst)
209 struct tevent_req *req;
210 struct tdgram_sendto_state *state;
211 struct tevent_req *subreq;
213 req = tevent_req_create(mem_ctx, &state,
214 struct tdgram_sendto_state);
219 state->ops = dgram->ops;
222 subreq = state->ops->sendto_send(state, ev, dgram,
224 if (tevent_req_nomem(subreq, req)) {
227 tevent_req_set_callback(subreq, tdgram_sendto_done, req);
232 tevent_req_post(req, ev);
236 static void tdgram_sendto_done(struct tevent_req *subreq)
238 struct tevent_req *req = tevent_req_callback_data(subreq,
240 struct tdgram_sendto_state *state = tevent_req_data(req,
241 struct tdgram_sendto_state);
245 ret = state->ops->sendto_recv(subreq, &sys_errno);
247 tevent_req_error(req, sys_errno);
253 tevent_req_done(req);
256 ssize_t tdgram_sendto_recv(struct tevent_req *req,
259 struct tdgram_sendto_state *state = tevent_req_data(req,
260 struct tdgram_sendto_state);
263 ret = tsocket_simple_int_recv(req, perrno);
268 tevent_req_received(req);
272 struct tdgram_disconnect_state {
273 const struct tdgram_context_ops *ops;
276 static void tdgram_disconnect_done(struct tevent_req *subreq);
278 struct tevent_req *tdgram_disconnect_send(TALLOC_CTX *mem_ctx,
279 struct tevent_context *ev,
280 struct tdgram_context *dgram)
282 struct tevent_req *req;
283 struct tdgram_disconnect_state *state;
284 struct tevent_req *subreq;
286 req = tevent_req_create(mem_ctx, &state,
287 struct tdgram_disconnect_state);
292 state->ops = dgram->ops;
294 subreq = state->ops->disconnect_send(state, ev, dgram);
295 if (tevent_req_nomem(subreq, req)) {
298 tevent_req_set_callback(subreq, tdgram_disconnect_done, req);
303 tevent_req_post(req, ev);
307 static void tdgram_disconnect_done(struct tevent_req *subreq)
309 struct tevent_req *req = tevent_req_callback_data(subreq,
311 struct tdgram_disconnect_state *state = tevent_req_data(req,
312 struct tdgram_disconnect_state);
316 ret = state->ops->disconnect_recv(subreq, &sys_errno);
318 tevent_req_error(req, sys_errno);
322 tevent_req_done(req);
325 int tdgram_disconnect_recv(struct tevent_req *req,
330 ret = tsocket_simple_int_recv(req, perrno);
332 tevent_req_received(req);
336 struct tdgram_sendto_queue_state {
337 /* this structs are owned by the caller */
339 struct tevent_context *ev;
340 struct tdgram_context *dgram;
343 const struct tsocket_address *dst;
348 static void tdgram_sendto_queue_trigger(struct tevent_req *req,
350 static void tdgram_sendto_queue_done(struct tevent_req *subreq);
353 * @brief Queue a dgram blob for sending through the socket
354 * @param[in] mem_ctx The memory context for the result
355 * @param[in] ev The event context the operation should work on
356 * @param[in] dgram The tdgram_context to send the message buffer
357 * @param[in] queue The existing dgram queue
358 * @param[in] buf The message buffer
359 * @param[in] len The message length
360 * @param[in] dst The destination socket address
361 * @retval The async request handle
363 * This function queues a blob for sending to destination through an existing
364 * dgram socket. The async callback is triggered when the whole blob is
365 * delivered to the underlying system socket.
367 * The caller needs to make sure that all non-scalar input parameters hang
368 * arround for the whole lifetime of the request.
370 struct tevent_req *tdgram_sendto_queue_send(TALLOC_CTX *mem_ctx,
371 struct tevent_context *ev,
372 struct tdgram_context *dgram,
373 struct tevent_queue *queue,
376 struct tsocket_address *dst)
378 struct tevent_req *req;
379 struct tdgram_sendto_queue_state *state;
382 req = tevent_req_create(mem_ctx, &state,
383 struct tdgram_sendto_queue_state);
388 state->caller.ev = ev;
389 state->caller.dgram = dgram;
390 state->caller.buf = buf;
391 state->caller.len = len;
392 state->caller.dst = dst;
395 ok = tevent_queue_add(queue,
398 tdgram_sendto_queue_trigger,
401 tevent_req_nomem(NULL, req);
408 tevent_req_post(req, ev);
412 static void tdgram_sendto_queue_trigger(struct tevent_req *req,
415 struct tdgram_sendto_queue_state *state = tevent_req_data(req,
416 struct tdgram_sendto_queue_state);
417 struct tevent_req *subreq;
419 subreq = tdgram_sendto_send(state,
425 if (tevent_req_nomem(subreq, req)) {
428 tevent_req_set_callback(subreq, tdgram_sendto_queue_done, req);
431 static void tdgram_sendto_queue_done(struct tevent_req *subreq)
433 struct tevent_req *req = tevent_req_callback_data(subreq,
435 struct tdgram_sendto_queue_state *state = tevent_req_data(req,
436 struct tdgram_sendto_queue_state);
440 ret = tdgram_sendto_recv(subreq, &sys_errno);
443 tevent_req_error(req, sys_errno);
448 tevent_req_done(req);
451 ssize_t tdgram_sendto_queue_recv(struct tevent_req *req, int *perrno)
453 struct tdgram_sendto_queue_state *state = tevent_req_data(req,
454 struct tdgram_sendto_queue_state);
457 ret = tsocket_simple_int_recv(req, perrno);
462 tevent_req_received(req);