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/filesys.h"
27 #include "tsocket_internal.h"
29 int tsocket_simple_int_recv(struct tevent_req *req, int *perrno)
31 enum tevent_req_state state;
34 if (!tevent_req_is_error(req, &state, &error)) {
39 case TEVENT_REQ_NO_MEMORY:
42 case TEVENT_REQ_TIMED_OUT:
45 case TEVENT_REQ_USER_ERROR:
57 struct tsocket_address *_tsocket_address_create(TALLOC_CTX *mem_ctx,
58 const struct tsocket_address_ops *ops,
64 void **ppstate = (void **)pstate;
65 struct tsocket_address *addr;
67 addr = talloc_zero(mem_ctx, struct tsocket_address);
72 addr->location = location;
73 addr->private_data = talloc_size(addr, psize);
74 if (!addr->private_data) {
78 talloc_set_name_const(addr->private_data, type);
80 *ppstate = addr->private_data;
84 char *tsocket_address_string(const struct tsocket_address *addr,
88 return talloc_strdup(mem_ctx, "NULL");
90 return addr->ops->string(addr, mem_ctx);
93 struct tsocket_address *_tsocket_address_copy(const struct tsocket_address *addr,
97 return addr->ops->copy(addr, mem_ctx, location);
100 struct tdgram_context {
101 const char *location;
102 const struct tdgram_context_ops *ops;
105 struct tevent_req *recvfrom_req;
106 struct tevent_req *sendto_req;
109 static int tdgram_context_destructor(struct tdgram_context *dgram)
111 if (dgram->recvfrom_req) {
112 tevent_req_received(dgram->recvfrom_req);
115 if (dgram->sendto_req) {
116 tevent_req_received(dgram->sendto_req);
122 struct tdgram_context *_tdgram_context_create(TALLOC_CTX *mem_ctx,
123 const struct tdgram_context_ops *ops,
127 const char *location)
129 struct tdgram_context *dgram;
130 void **ppstate = (void **)pstate;
133 dgram = talloc(mem_ctx, struct tdgram_context);
137 dgram->location = location;
139 dgram->recvfrom_req = NULL;
140 dgram->sendto_req = NULL;
142 state = talloc_size(dgram, psize);
147 talloc_set_name_const(state, type);
149 dgram->private_data = state;
151 talloc_set_destructor(dgram, tdgram_context_destructor);
157 void *_tdgram_context_data(struct tdgram_context *dgram)
159 return dgram->private_data;
162 struct tdgram_recvfrom_state {
163 const struct tdgram_context_ops *ops;
164 struct tdgram_context *dgram;
167 struct tsocket_address *src;
170 static int tdgram_recvfrom_destructor(struct tdgram_recvfrom_state *state)
173 state->dgram->recvfrom_req = NULL;
179 static void tdgram_recvfrom_done(struct tevent_req *subreq);
181 struct tevent_req *tdgram_recvfrom_send(TALLOC_CTX *mem_ctx,
182 struct tevent_context *ev,
183 struct tdgram_context *dgram)
185 struct tevent_req *req;
186 struct tdgram_recvfrom_state *state;
187 struct tevent_req *subreq;
189 req = tevent_req_create(mem_ctx, &state,
190 struct tdgram_recvfrom_state);
195 state->ops = dgram->ops;
196 state->dgram = dgram;
201 if (dgram->recvfrom_req) {
202 tevent_req_error(req, EBUSY);
205 dgram->recvfrom_req = req;
207 talloc_set_destructor(state, tdgram_recvfrom_destructor);
209 subreq = state->ops->recvfrom_send(state, ev, dgram);
210 if (tevent_req_nomem(subreq, req)) {
213 tevent_req_set_callback(subreq, tdgram_recvfrom_done, req);
218 tevent_req_post(req, ev);
222 static void tdgram_recvfrom_done(struct tevent_req *subreq)
224 struct tevent_req *req = tevent_req_callback_data(subreq,
226 struct tdgram_recvfrom_state *state = tevent_req_data(req,
227 struct tdgram_recvfrom_state);
231 ret = state->ops->recvfrom_recv(subreq, &sys_errno, state,
232 &state->buf, &state->src);
234 tevent_req_error(req, sys_errno);
240 tevent_req_done(req);
243 ssize_t tdgram_recvfrom_recv(struct tevent_req *req,
247 struct tsocket_address **src)
249 struct tdgram_recvfrom_state *state = tevent_req_data(req,
250 struct tdgram_recvfrom_state);
253 ret = tsocket_simple_int_recv(req, perrno);
255 *buf = talloc_move(mem_ctx, &state->buf);
258 *src = talloc_move(mem_ctx, &state->src);
262 tevent_req_received(req);
266 struct tdgram_sendto_state {
267 const struct tdgram_context_ops *ops;
268 struct tdgram_context *dgram;
272 static int tdgram_sendto_destructor(struct tdgram_sendto_state *state)
275 state->dgram->sendto_req = NULL;
281 static void tdgram_sendto_done(struct tevent_req *subreq);
283 struct tevent_req *tdgram_sendto_send(TALLOC_CTX *mem_ctx,
284 struct tevent_context *ev,
285 struct tdgram_context *dgram,
286 const uint8_t *buf, size_t len,
287 const struct tsocket_address *dst)
289 struct tevent_req *req;
290 struct tdgram_sendto_state *state;
291 struct tevent_req *subreq;
293 req = tevent_req_create(mem_ctx, &state,
294 struct tdgram_sendto_state);
299 state->ops = dgram->ops;
300 state->dgram = dgram;
304 tevent_req_error(req, EINVAL);
308 if (dgram->sendto_req) {
309 tevent_req_error(req, EBUSY);
312 dgram->sendto_req = req;
314 talloc_set_destructor(state, tdgram_sendto_destructor);
316 subreq = state->ops->sendto_send(state, ev, dgram,
318 if (tevent_req_nomem(subreq, req)) {
321 tevent_req_set_callback(subreq, tdgram_sendto_done, req);
326 tevent_req_post(req, ev);
330 static void tdgram_sendto_done(struct tevent_req *subreq)
332 struct tevent_req *req = tevent_req_callback_data(subreq,
334 struct tdgram_sendto_state *state = tevent_req_data(req,
335 struct tdgram_sendto_state);
339 ret = state->ops->sendto_recv(subreq, &sys_errno);
341 tevent_req_error(req, sys_errno);
347 tevent_req_done(req);
350 ssize_t tdgram_sendto_recv(struct tevent_req *req,
353 struct tdgram_sendto_state *state = tevent_req_data(req,
354 struct tdgram_sendto_state);
357 ret = tsocket_simple_int_recv(req, perrno);
362 tevent_req_received(req);
366 struct tdgram_disconnect_state {
367 const struct tdgram_context_ops *ops;
370 static void tdgram_disconnect_done(struct tevent_req *subreq);
372 struct tevent_req *tdgram_disconnect_send(TALLOC_CTX *mem_ctx,
373 struct tevent_context *ev,
374 struct tdgram_context *dgram)
376 struct tevent_req *req;
377 struct tdgram_disconnect_state *state;
378 struct tevent_req *subreq;
380 req = tevent_req_create(mem_ctx, &state,
381 struct tdgram_disconnect_state);
386 state->ops = dgram->ops;
388 if (dgram->recvfrom_req || dgram->sendto_req) {
389 tevent_req_error(req, EBUSY);
393 subreq = state->ops->disconnect_send(state, ev, dgram);
394 if (tevent_req_nomem(subreq, req)) {
397 tevent_req_set_callback(subreq, tdgram_disconnect_done, req);
402 tevent_req_post(req, ev);
406 static void tdgram_disconnect_done(struct tevent_req *subreq)
408 struct tevent_req *req = tevent_req_callback_data(subreq,
410 struct tdgram_disconnect_state *state = tevent_req_data(req,
411 struct tdgram_disconnect_state);
415 ret = state->ops->disconnect_recv(subreq, &sys_errno);
417 tevent_req_error(req, sys_errno);
421 tevent_req_done(req);
424 int tdgram_disconnect_recv(struct tevent_req *req,
429 ret = tsocket_simple_int_recv(req, perrno);
431 tevent_req_received(req);
435 struct tstream_context {
436 const char *location;
437 const struct tstream_context_ops *ops;
440 struct tevent_req *readv_req;
441 struct tevent_req *writev_req;
444 static int tstream_context_destructor(struct tstream_context *stream)
446 if (stream->readv_req) {
447 tevent_req_received(stream->readv_req);
450 if (stream->writev_req) {
451 tevent_req_received(stream->writev_req);
457 struct tstream_context *_tstream_context_create(TALLOC_CTX *mem_ctx,
458 const struct tstream_context_ops *ops,
462 const char *location)
464 struct tstream_context *stream;
465 void **ppstate = (void **)pstate;
468 stream = talloc(mem_ctx, struct tstream_context);
469 if (stream == NULL) {
472 stream->location = location;
474 stream->readv_req = NULL;
475 stream->writev_req = NULL;
477 state = talloc_size(stream, psize);
482 talloc_set_name_const(state, type);
484 stream->private_data = state;
486 talloc_set_destructor(stream, tstream_context_destructor);
492 void *_tstream_context_data(struct tstream_context *stream)
494 return stream->private_data;
497 ssize_t tstream_pending_bytes(struct tstream_context *stream)
499 return stream->ops->pending_bytes(stream);
502 struct tstream_readv_state {
503 const struct tstream_context_ops *ops;
504 struct tstream_context *stream;
508 static int tstream_readv_destructor(struct tstream_readv_state *state)
511 state->stream->readv_req = NULL;
517 static void tstream_readv_done(struct tevent_req *subreq);
519 struct tevent_req *tstream_readv_send(TALLOC_CTX *mem_ctx,
520 struct tevent_context *ev,
521 struct tstream_context *stream,
522 struct iovec *vector,
525 struct tevent_req *req;
526 struct tstream_readv_state *state;
527 struct tevent_req *subreq;
531 req = tevent_req_create(mem_ctx, &state,
532 struct tstream_readv_state);
537 state->ops = stream->ops;
538 state->stream = stream;
541 /* first check if the input is ok */
543 if (count > IOV_MAX) {
544 tevent_req_error(req, EMSGSIZE);
549 for (i=0; i < count; i++) {
551 tmp += vector[i].iov_len;
554 tevent_req_error(req, EMSGSIZE);
562 tevent_req_error(req, EINVAL);
566 if (stream->readv_req) {
567 tevent_req_error(req, EBUSY);
570 stream->readv_req = req;
572 talloc_set_destructor(state, tstream_readv_destructor);
574 subreq = state->ops->readv_send(state, ev, stream, vector, count);
575 if (tevent_req_nomem(subreq, req)) {
578 tevent_req_set_callback(subreq, tstream_readv_done, req);
583 tevent_req_post(req, ev);
587 static void tstream_readv_done(struct tevent_req *subreq)
589 struct tevent_req *req = tevent_req_callback_data(subreq,
591 struct tstream_readv_state *state = tevent_req_data(req,
592 struct tstream_readv_state);
596 ret = state->ops->readv_recv(subreq, &sys_errno);
599 tevent_req_error(req, sys_errno);
605 tevent_req_done(req);
608 int tstream_readv_recv(struct tevent_req *req,
611 struct tstream_readv_state *state = tevent_req_data(req,
612 struct tstream_readv_state);
615 ret = tsocket_simple_int_recv(req, perrno);
620 tevent_req_received(req);
624 struct tstream_writev_state {
625 const struct tstream_context_ops *ops;
626 struct tstream_context *stream;
630 static int tstream_writev_destructor(struct tstream_writev_state *state)
633 state->stream->writev_req = NULL;
639 static void tstream_writev_done(struct tevent_req *subreq);
641 struct tevent_req *tstream_writev_send(TALLOC_CTX *mem_ctx,
642 struct tevent_context *ev,
643 struct tstream_context *stream,
644 const struct iovec *vector,
647 struct tevent_req *req;
648 struct tstream_writev_state *state;
649 struct tevent_req *subreq;
653 req = tevent_req_create(mem_ctx, &state,
654 struct tstream_writev_state);
659 state->ops = stream->ops;
660 state->stream = stream;
663 /* first check if the input is ok */
665 if (count > IOV_MAX) {
666 tevent_req_error(req, EMSGSIZE);
671 for (i=0; i < count; i++) {
673 tmp += vector[i].iov_len;
675 if (tmp < to_write) {
676 tevent_req_error(req, EMSGSIZE);
684 tevent_req_error(req, EINVAL);
688 if (stream->writev_req) {
689 tevent_req_error(req, EBUSY);
692 stream->writev_req = req;
694 talloc_set_destructor(state, tstream_writev_destructor);
696 subreq = state->ops->writev_send(state, ev, stream, vector, count);
697 if (tevent_req_nomem(subreq, req)) {
700 tevent_req_set_callback(subreq, tstream_writev_done, req);
705 tevent_req_post(req, ev);
709 static void tstream_writev_done(struct tevent_req *subreq)
711 struct tevent_req *req = tevent_req_callback_data(subreq,
713 struct tstream_writev_state *state = tevent_req_data(req,
714 struct tstream_writev_state);
718 ret = state->ops->writev_recv(subreq, &sys_errno);
720 tevent_req_error(req, sys_errno);
726 tevent_req_done(req);
729 int tstream_writev_recv(struct tevent_req *req,
732 struct tstream_writev_state *state = tevent_req_data(req,
733 struct tstream_writev_state);
736 ret = tsocket_simple_int_recv(req, perrno);
741 tevent_req_received(req);
745 struct tstream_disconnect_state {
746 const struct tstream_context_ops *ops;
749 static void tstream_disconnect_done(struct tevent_req *subreq);
751 struct tevent_req *tstream_disconnect_send(TALLOC_CTX *mem_ctx,
752 struct tevent_context *ev,
753 struct tstream_context *stream)
755 struct tevent_req *req;
756 struct tstream_disconnect_state *state;
757 struct tevent_req *subreq;
759 req = tevent_req_create(mem_ctx, &state,
760 struct tstream_disconnect_state);
765 state->ops = stream->ops;
767 if (stream->readv_req || stream->writev_req) {
768 tevent_req_error(req, EBUSY);
772 subreq = state->ops->disconnect_send(state, ev, stream);
773 if (tevent_req_nomem(subreq, req)) {
776 tevent_req_set_callback(subreq, tstream_disconnect_done, req);
781 tevent_req_post(req, ev);
785 static void tstream_disconnect_done(struct tevent_req *subreq)
787 struct tevent_req *req = tevent_req_callback_data(subreq,
789 struct tstream_disconnect_state *state = tevent_req_data(req,
790 struct tstream_disconnect_state);
794 ret = state->ops->disconnect_recv(subreq, &sys_errno);
796 tevent_req_error(req, sys_errno);
800 tevent_req_done(req);
803 int tstream_disconnect_recv(struct tevent_req *req,
808 ret = tsocket_simple_int_recv(req, perrno);
810 tevent_req_received(req);