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;
77 struct tevent_req *recvfrom_req;
78 struct tevent_req *sendto_req;
81 static int tdgram_context_destructor(struct tdgram_context *dgram)
83 if (dgram->recvfrom_req) {
84 tevent_req_received(dgram->recvfrom_req);
87 if (dgram->sendto_req) {
88 tevent_req_received(dgram->sendto_req);
94 struct tdgram_context *_tdgram_context_create(TALLOC_CTX *mem_ctx,
95 const struct tdgram_context_ops *ops,
101 struct tdgram_context *dgram;
102 void **ppstate = (void **)pstate;
105 dgram = talloc(mem_ctx, struct tdgram_context);
109 dgram->location = location;
111 dgram->recvfrom_req = NULL;
112 dgram->sendto_req = NULL;
114 state = talloc_size(dgram, psize);
119 talloc_set_name_const(state, type);
121 dgram->private_data = state;
123 talloc_set_destructor(dgram, tdgram_context_destructor);
129 void *_tdgram_context_data(struct tdgram_context *dgram)
131 return dgram->private_data;
134 struct tdgram_recvfrom_state {
135 const struct tdgram_context_ops *ops;
136 struct tdgram_context *dgram;
139 struct tsocket_address *src;
142 static int tdgram_recvfrom_destructor(struct tdgram_recvfrom_state *state)
145 state->dgram->recvfrom_req = NULL;
151 static void tdgram_recvfrom_done(struct tevent_req *subreq);
153 struct tevent_req *tdgram_recvfrom_send(TALLOC_CTX *mem_ctx,
154 struct tevent_context *ev,
155 struct tdgram_context *dgram)
157 struct tevent_req *req;
158 struct tdgram_recvfrom_state *state;
159 struct tevent_req *subreq;
161 req = tevent_req_create(mem_ctx, &state,
162 struct tdgram_recvfrom_state);
167 state->ops = dgram->ops;
168 state->dgram = dgram;
173 if (dgram->recvfrom_req) {
174 tevent_req_error(req, EBUSY);
177 dgram->recvfrom_req = req;
179 talloc_set_destructor(state, tdgram_recvfrom_destructor);
181 subreq = state->ops->recvfrom_send(state, ev, dgram);
182 if (tevent_req_nomem(subreq, req)) {
185 tevent_req_set_callback(subreq, tdgram_recvfrom_done, req);
190 tevent_req_post(req, ev);
194 static void tdgram_recvfrom_done(struct tevent_req *subreq)
196 struct tevent_req *req = tevent_req_callback_data(subreq,
198 struct tdgram_recvfrom_state *state = tevent_req_data(req,
199 struct tdgram_recvfrom_state);
203 ret = state->ops->recvfrom_recv(subreq, &sys_errno, state,
204 &state->buf, &state->src);
206 tevent_req_error(req, sys_errno);
212 tevent_req_done(req);
215 ssize_t tdgram_recvfrom_recv(struct tevent_req *req,
219 struct tsocket_address **src)
221 struct tdgram_recvfrom_state *state = tevent_req_data(req,
222 struct tdgram_recvfrom_state);
225 ret = tsocket_simple_int_recv(req, perrno);
227 *buf = talloc_move(mem_ctx, &state->buf);
230 *src = talloc_move(mem_ctx, &state->src);
234 tevent_req_received(req);
238 struct tdgram_sendto_state {
239 const struct tdgram_context_ops *ops;
240 struct tdgram_context *dgram;
244 static int tdgram_sendto_destructor(struct tdgram_sendto_state *state)
247 state->dgram->sendto_req = NULL;
253 static void tdgram_sendto_done(struct tevent_req *subreq);
255 struct tevent_req *tdgram_sendto_send(TALLOC_CTX *mem_ctx,
256 struct tevent_context *ev,
257 struct tdgram_context *dgram,
258 const uint8_t *buf, size_t len,
259 const struct tsocket_address *dst)
261 struct tevent_req *req;
262 struct tdgram_sendto_state *state;
263 struct tevent_req *subreq;
265 req = tevent_req_create(mem_ctx, &state,
266 struct tdgram_sendto_state);
271 state->ops = dgram->ops;
272 state->dgram = dgram;
276 tevent_req_error(req, EINVAL);
280 if (dgram->sendto_req) {
281 tevent_req_error(req, EBUSY);
284 dgram->sendto_req = req;
286 talloc_set_destructor(state, tdgram_sendto_destructor);
288 subreq = state->ops->sendto_send(state, ev, dgram,
290 if (tevent_req_nomem(subreq, req)) {
293 tevent_req_set_callback(subreq, tdgram_sendto_done, req);
298 tevent_req_post(req, ev);
302 static void tdgram_sendto_done(struct tevent_req *subreq)
304 struct tevent_req *req = tevent_req_callback_data(subreq,
306 struct tdgram_sendto_state *state = tevent_req_data(req,
307 struct tdgram_sendto_state);
311 ret = state->ops->sendto_recv(subreq, &sys_errno);
313 tevent_req_error(req, sys_errno);
319 tevent_req_done(req);
322 ssize_t tdgram_sendto_recv(struct tevent_req *req,
325 struct tdgram_sendto_state *state = tevent_req_data(req,
326 struct tdgram_sendto_state);
329 ret = tsocket_simple_int_recv(req, perrno);
334 tevent_req_received(req);
338 struct tdgram_disconnect_state {
339 const struct tdgram_context_ops *ops;
342 static void tdgram_disconnect_done(struct tevent_req *subreq);
344 struct tevent_req *tdgram_disconnect_send(TALLOC_CTX *mem_ctx,
345 struct tevent_context *ev,
346 struct tdgram_context *dgram)
348 struct tevent_req *req;
349 struct tdgram_disconnect_state *state;
350 struct tevent_req *subreq;
352 req = tevent_req_create(mem_ctx, &state,
353 struct tdgram_disconnect_state);
358 state->ops = dgram->ops;
360 if (dgram->recvfrom_req || dgram->sendto_req) {
361 tevent_req_error(req, EBUSY);
365 subreq = state->ops->disconnect_send(state, ev, dgram);
366 if (tevent_req_nomem(subreq, req)) {
369 tevent_req_set_callback(subreq, tdgram_disconnect_done, req);
374 tevent_req_post(req, ev);
378 static void tdgram_disconnect_done(struct tevent_req *subreq)
380 struct tevent_req *req = tevent_req_callback_data(subreq,
382 struct tdgram_disconnect_state *state = tevent_req_data(req,
383 struct tdgram_disconnect_state);
387 ret = state->ops->disconnect_recv(subreq, &sys_errno);
389 tevent_req_error(req, sys_errno);
393 tevent_req_done(req);
396 int tdgram_disconnect_recv(struct tevent_req *req,
401 ret = tsocket_simple_int_recv(req, perrno);
403 tevent_req_received(req);