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 static int tsocket_context_destructor(struct tsocket_context *sock)
31 tsocket_disconnect(sock);
35 struct tsocket_context *_tsocket_context_create(TALLOC_CTX *mem_ctx,
36 const struct tsocket_context_ops *ops,
42 void **ppstate = (void **)pstate;
43 struct tsocket_context *sock;
45 sock = talloc_zero(mem_ctx, struct tsocket_context);
50 sock->location = location;
51 sock->private_data = talloc_size(sock, psize);
52 if (!sock->private_data) {
56 talloc_set_name_const(sock->private_data, type);
58 talloc_set_destructor(sock, tsocket_context_destructor);
60 *ppstate = sock->private_data;
64 int tsocket_set_event_context(struct tsocket_context *sock,
65 struct tevent_context *ev)
67 return sock->ops->set_event_context(sock, ev);
70 int tsocket_set_readable_handler(struct tsocket_context *sock,
71 tsocket_event_handler_t handler,
74 return sock->ops->set_read_handler(sock, handler, private_data);
77 int tsocket_set_writeable_handler(struct tsocket_context *sock,
78 tsocket_event_handler_t handler,
81 return sock->ops->set_write_handler(sock, handler, private_data);
84 int tsocket_connect(struct tsocket_context *sock,
85 const struct tsocket_address *remote_addr)
87 return sock->ops->connect_to(sock, remote_addr);
90 int tsocket_listen(struct tsocket_context *sock,
93 return sock->ops->listen_on(sock, queue_size);
96 int _tsocket_accept(struct tsocket_context *sock,
98 struct tsocket_context **new_sock,
101 return sock->ops->accept_new(sock, mem_ctx, new_sock, location);
104 ssize_t tsocket_pending(struct tsocket_context *sock)
106 return sock->ops->pending_data(sock);
109 int tsocket_readv(struct tsocket_context *sock,
110 const struct iovec *vector, size_t count)
112 return sock->ops->readv_data(sock, vector, count);
115 int tsocket_writev(struct tsocket_context *sock,
116 const struct iovec *vector, size_t count)
118 return sock->ops->writev_data(sock, vector, count);
121 ssize_t tsocket_recvfrom(struct tsocket_context *sock,
122 uint8_t *data, size_t len,
123 TALLOC_CTX *addr_ctx,
124 struct tsocket_address **src_addr)
126 return sock->ops->recvfrom_data(sock, data, len, addr_ctx, src_addr);
129 ssize_t tsocket_sendto(struct tsocket_context *sock,
130 const uint8_t *data, size_t len,
131 const struct tsocket_address *dest_addr)
133 return sock->ops->sendto_data(sock, data, len, dest_addr);
136 int tsocket_get_status(const struct tsocket_context *sock)
138 return sock->ops->get_status(sock);
141 int _tsocket_get_local_address(const struct tsocket_context *sock,
143 struct tsocket_address **local_addr,
144 const char *location)
146 return sock->ops->get_local_address(sock, mem_ctx,
147 local_addr, location);
150 int _tsocket_get_remote_address(const struct tsocket_context *sock,
152 struct tsocket_address **remote_addr,
153 const char *location)
155 return sock->ops->get_remote_address(sock, mem_ctx,
156 remote_addr, location);
159 int tsocket_get_option(const struct tsocket_context *sock,
164 return sock->ops->get_option(sock, option, mem_ctx, value);
167 int tsocket_set_option(const struct tsocket_context *sock,
172 return sock->ops->set_option(sock, option, force, value);
175 void tsocket_disconnect(struct tsocket_context *sock)
177 sock->ops->disconnect(sock);
180 struct tsocket_address *_tsocket_address_create(TALLOC_CTX *mem_ctx,
181 const struct tsocket_address_ops *ops,
185 const char *location)
187 void **ppstate = (void **)pstate;
188 struct tsocket_address *addr;
190 addr = talloc_zero(mem_ctx, struct tsocket_address);
195 addr->location = location;
196 addr->private_data = talloc_size(addr, psize);
197 if (!addr->private_data) {
201 talloc_set_name_const(addr->private_data, type);
203 *ppstate = addr->private_data;
207 char *tsocket_address_string(const struct tsocket_address *addr,
211 return talloc_strdup(mem_ctx, "NULL");
213 return addr->ops->string(addr, mem_ctx);
216 struct tsocket_address *_tsocket_address_copy(const struct tsocket_address *addr,
218 const char *location)
220 return addr->ops->copy(addr, mem_ctx, location);
223 int _tsocket_address_create_socket(const struct tsocket_address *addr,
224 enum tsocket_type type,
226 struct tsocket_context **sock,
227 const char *location)
229 return addr->ops->create_socket(addr, type, mem_ctx, sock, location);
232 struct tdgram_context {
233 const char *location;
234 const struct tdgram_context_ops *ops;
238 struct tdgram_context *_tdgram_context_create(TALLOC_CTX *mem_ctx,
239 const struct tdgram_context_ops *ops,
243 const char *location)
245 struct tdgram_context *dgram;
246 void **ppstate = (void **)pstate;
249 dgram = talloc(mem_ctx, struct tdgram_context);
253 dgram->location = location;
256 state = talloc_size(dgram, psize);
261 talloc_set_name_const(state, type);
263 dgram->private_data = state;
269 void *_tdgram_context_data(struct tdgram_context *dgram)
271 return dgram->private_data;
274 struct tdgram_recvfrom_state {
275 const struct tdgram_context_ops *ops;
278 struct tsocket_address *src;
281 static void tdgram_recvfrom_done(struct tevent_req *subreq);
283 struct tevent_req *tdgram_recvfrom_send(TALLOC_CTX *mem_ctx,
284 struct tevent_context *ev,
285 struct tdgram_context *dgram)
287 struct tevent_req *req;
288 struct tdgram_recvfrom_state *state;
289 struct tevent_req *subreq;
291 req = tevent_req_create(mem_ctx, &state,
292 struct tdgram_recvfrom_state);
297 state->ops = dgram->ops;
299 subreq = state->ops->recvfrom_send(state, ev, dgram);
300 if (tevent_req_nomem(subreq, req)) {
303 tevent_req_set_callback(subreq, tdgram_recvfrom_done, req);
308 tevent_req_post(req, ev);
312 static void tdgram_recvfrom_done(struct tevent_req *subreq)
314 struct tevent_req *req = tevent_req_callback_data(subreq,
316 struct tdgram_recvfrom_state *state = tevent_req_data(req,
317 struct tdgram_recvfrom_state);
321 ret = state->ops->recvfrom_recv(subreq, &sys_errno, state,
322 &state->buf, &state->src);
324 tevent_req_error(req, sys_errno);
330 tevent_req_done(req);
333 ssize_t tdgram_recvfrom_recv(struct tevent_req *req,
337 struct tsocket_address **src)
339 struct tdgram_recvfrom_state *state = tevent_req_data(req,
340 struct tdgram_recvfrom_state);
343 ret = tsocket_simple_int_recv(req, perrno);
345 *buf = talloc_move(mem_ctx, &state->buf);
348 *src = talloc_move(mem_ctx, &state->src);
352 tevent_req_received(req);
356 struct tdgram_sendto_state {
357 const struct tdgram_context_ops *ops;
361 static void tdgram_sendto_done(struct tevent_req *subreq);
363 struct tevent_req *tdgram_sendto_send(TALLOC_CTX *mem_ctx,
364 struct tevent_context *ev,
365 struct tdgram_context *dgram,
366 const uint8_t *buf, size_t len,
367 const struct tsocket_address *dst)
369 struct tevent_req *req;
370 struct tdgram_sendto_state *state;
371 struct tevent_req *subreq;
373 req = tevent_req_create(mem_ctx, &state,
374 struct tdgram_sendto_state);
379 state->ops = dgram->ops;
382 subreq = state->ops->sendto_send(state, ev, dgram,
384 if (tevent_req_nomem(subreq, req)) {
387 tevent_req_set_callback(subreq, tdgram_sendto_done, req);
392 tevent_req_post(req, ev);
396 static void tdgram_sendto_done(struct tevent_req *subreq)
398 struct tevent_req *req = tevent_req_callback_data(subreq,
400 struct tdgram_sendto_state *state = tevent_req_data(req,
401 struct tdgram_sendto_state);
405 ret = state->ops->sendto_recv(subreq, &sys_errno);
407 tevent_req_error(req, sys_errno);
413 tevent_req_done(req);
416 ssize_t tdgram_sendto_recv(struct tevent_req *req,
419 struct tdgram_sendto_state *state = tevent_req_data(req,
420 struct tdgram_sendto_state);
423 ret = tsocket_simple_int_recv(req, perrno);
428 tevent_req_received(req);
432 struct tdgram_disconnect_state {
433 const struct tdgram_context_ops *ops;
436 static void tdgram_disconnect_done(struct tevent_req *subreq);
438 struct tevent_req *tdgram_disconnect_send(TALLOC_CTX *mem_ctx,
439 struct tevent_context *ev,
440 struct tdgram_context *dgram)
442 struct tevent_req *req;
443 struct tdgram_disconnect_state *state;
444 struct tevent_req *subreq;
446 req = tevent_req_create(mem_ctx, &state,
447 struct tdgram_disconnect_state);
452 state->ops = dgram->ops;
454 subreq = state->ops->disconnect_send(state, ev, dgram);
455 if (tevent_req_nomem(subreq, req)) {
458 tevent_req_set_callback(subreq, tdgram_disconnect_done, req);
463 tevent_req_post(req, ev);
467 static void tdgram_disconnect_done(struct tevent_req *subreq)
469 struct tevent_req *req = tevent_req_callback_data(subreq,
471 struct tdgram_disconnect_state *state = tevent_req_data(req,
472 struct tdgram_disconnect_state);
476 ret = state->ops->disconnect_recv(subreq, &sys_errno);
478 tevent_req_error(req, sys_errno);
482 tevent_req_done(req);
485 int tdgram_disconnect_recv(struct tevent_req *req,
490 ret = tsocket_simple_int_recv(req, perrno);
492 tevent_req_received(req);