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_recvfrom_state {
30 /* this structs are owned by the caller */
32 struct tsocket_context *sock;
37 struct tsocket_address *src;
40 static int tsocket_recvfrom_state_destructor(struct tsocket_recvfrom_state *state)
42 if (state->caller.sock) {
43 tsocket_set_readable_handler(state->caller.sock, NULL, NULL);
45 ZERO_STRUCT(state->caller);
50 static void tsocket_recvfrom_handler(struct tsocket_context *sock,
53 struct tevent_req *tsocket_recvfrom_send(struct tsocket_context *sock,
56 struct tevent_req *req;
57 struct tsocket_recvfrom_state *state;
62 req = tevent_req_create(mem_ctx, &state,
63 struct tsocket_recvfrom_state);
68 state->caller.sock = sock;
73 talloc_set_destructor(state, tsocket_recvfrom_state_destructor);
75 ret = tsocket_set_readable_handler(sock,
76 tsocket_recvfrom_handler,
78 err = tsocket_error_from_errno(ret, errno, &dummy);
79 if (tevent_req_error(req, err)) {
86 return tevent_req_post(req, sock->event.ctx);
89 static void tsocket_recvfrom_handler(struct tsocket_context *sock,
92 struct tevent_req *req = talloc_get_type(private_data,
94 struct tsocket_recvfrom_state *state = tevent_req_data(req,
95 struct tsocket_recvfrom_state);
100 ret = tsocket_pending(state->caller.sock);
105 err = tsocket_error_from_errno(ret, errno, &retry);
110 if (tevent_req_error(req, err)) {
114 state->buf = talloc_array(state, uint8_t, ret);
115 if (tevent_req_nomem(state->buf, req)) {
120 ret = tsocket_recvfrom(state->caller.sock,
125 err = tsocket_error_from_errno(ret, errno, &retry);
130 if (tevent_req_error(req, err)) {
134 if (ret != state->len) {
135 tevent_req_error(req, EIO);
139 tevent_req_done(req);
142 ssize_t tsocket_recvfrom_recv(struct tevent_req *req,
146 struct tsocket_address **src)
148 struct tsocket_recvfrom_state *state = tevent_req_data(req,
149 struct tsocket_recvfrom_state);
152 ret = tsocket_simple_int_recv(req, perrno);
154 *buf = talloc_move(mem_ctx, &state->buf);
157 *src = talloc_move(mem_ctx, &state->src);
161 tevent_req_received(req);