ECLIPSE + PyDev Project
[Samba/kamenim.git] / lib / tsocket / tsocket.h
blobd983325c4540935a1fd59c3f355584a6887013d4
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2009
6 ** NOTE! The following LGPL license applies to the tsocket
7 ** library. This does NOT imply that all of Samba is released
8 ** under the LGPL
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/>.
24 #ifndef _TSOCKET_H
25 #define _TSOCKET_H
27 #include <talloc.h>
28 #include <tevent.h>
30 struct tsocket_address;
31 struct tdgram_context;
32 struct tstream_context;
33 struct iovec;
35 /**
36 * @mainpage
38 * The tsocket abstraction is an API ...
41 /**
42 * @defgroup tsocket The tsocket API
44 * The tsocket abstraction is split into two different kinds of
45 * communication interfaces.
47 * There's the "tstream_context" interface with abstracts the communication
48 * through a bidirectional byte stream between two endpoints.
50 * And there's the "tdgram_context" interface with abstracts datagram based
51 * communication between any number of endpoints.
53 * Both interfaces share the "tsocket_address" abstraction for endpoint
54 * addresses.
56 * The whole library is based on the talloc(3) and 'tevent' libraries and
57 * provides "tevent_req" based "foo_send()"/"foo_recv()" functions pairs for
58 * all abstracted methods that need to be async.
60 * @section vsock Virtual Sockets
62 * The abstracted layout of tdgram_context and tstream_context allow
63 * implementations around virtual sockets for encrypted tunnels (like TLS,
64 * SASL or GSSAPI) or named pipes over smb.
66 * @section npa Named Pipe Auth (NPA) Sockets
68 * Samba has an implementation to abstract named pipes over smb (within the
69 * server side). See libcli/named_pipe_auth/npa_tstream.[ch] for the core code.
70 * The current callers are located in source4/ntvfs/ipc/vfs_ipc.c and
71 * source4/rpc_server/service_rpc.c for the users.
74 /**
75 * @defgroup tsocket_address The tsocket_address abstraction
76 * @ingroup tsocket
78 * The tsocket_address represents an socket endpoint genericly.
79 * As it's like an abstract class it has no specific constructor.
80 * The specific constructors are descripted in later sections.
82 * @{
85 /**
86 * @brief Get a string representaion of the endpoint.
88 * This function creates a string representation of the endpoint for debugging.
89 * The output will look as followed:
90 * prefix:address:port
92 * e.g.
93 * ipv4:192.168.1.1:143
95 * Callers should not try to parse the string! The should use additional methods
96 * of the specific tsocket_address implemention to get more details.
98 * @param[in] addr The address to convert.
100 * @param[in] mem_ctx The talloc memory context to allocate the memory.
102 * @return The address as a string representation, NULL on error.
104 * @see tsocket_address_inet_addr_string()
105 * @see tsocket_address_inet_port()
107 char *tsocket_address_string(const struct tsocket_address *addr,
108 TALLOC_CTX *mem_ctx);
110 #ifdef DOXYGEN
112 * @brief This creates a copy of a tsocket_address.
114 * This is useful when before doing modifications to a socket via additional
115 * methods of the specific tsocket_address implementation.
117 * @param[in] addr The address to create the copy from.
119 * @param[in] mem_ctx The talloc memory context to use.
121 * @return A newly allocated copy of addr (tsocket_address *), NULL
122 * on error.
124 struct tsocket_address *tsocket_address_copy(const struct tsocket_address *addr,
125 TALLOC_CTX *mem_ctx);
126 #else
127 struct tsocket_address *_tsocket_address_copy(const struct tsocket_address *addr,
128 TALLOC_CTX *mem_ctx,
129 const char *location);
131 #define tsocket_address_copy(addr, mem_ctx) \
132 _tsocket_address_copy(addr, mem_ctx, __location__)
133 #endif
136 * @}
140 * @defgroup tdgram_context The tdgram_context abstraction
141 * @ingroup tsocket
143 * The tdgram_context is like an abstract class for datagram based sockets. The
144 * interface provides async 'tevent_req' based functions on top functionality
145 * is similar to the recvfrom(2)/sendto(2)/close(2) syscalls.
147 * @note You can always use talloc_free(tdgram) to cleanup the resources
148 * of the tdgram_context on a fatal error.
149 * @{
153 * @brief Ask for next available datagram on the abstracted tdgram_context.
155 * It returns a 'tevent_req' handle, where the caller can register
156 * a callback with tevent_req_set_callback(). The callback is triggered
157 * when a datagram is available or an error happened.
159 * @param[in] mem_ctx The talloc memory context to use.
161 * @param[in] ev The tevent_context to run on.
163 * @param[in] dgram The dgram context to work on.
165 * @return Returns a 'tevent_req' handle, where the caller can
166 * register a callback with tevent_req_set_callback().
167 * NULL on fatal error.
169 * @see tdgram_inet_udp_socket()
170 * @see tdgram_unix_socket()
172 struct tevent_req *tdgram_recvfrom_send(TALLOC_CTX *mem_ctx,
173 struct tevent_context *ev,
174 struct tdgram_context *dgram);
177 * @brief Receive the next available datagram on the abstracted tdgram_context.
179 * This function should be called by the callback when a datagram is available
180 * or an error happened.
182 * The caller can only have one outstanding tdgram_recvfrom_send() at a time
183 * otherwise the caller will get '*perrno = EBUSY'.
185 * @param[in] req The tevent request from tdgram_recvfrom_send().
187 * @param[out] perrno The error number, set if an error occurred.
189 * @param[in] mem_ctx The memory context to use.
191 * @param[out] buf This will hold the buffer of the datagram.
193 * @param[out] src The abstracted tsocket_address of the sender of the
194 * received datagram.
196 * @return The length of the datagram (0 is never returned!),
197 * -1 on error with perrno set to the actual errno.
199 * @see tdgram_recvfrom_send()
201 ssize_t tdgram_recvfrom_recv(struct tevent_req *req,
202 int *perrno,
203 TALLOC_CTX *mem_ctx,
204 uint8_t **buf,
205 struct tsocket_address **src);
208 * @brief Send a datagram to a destination endpoint.
210 * The function can be called to send a datagram (specified by a buf/len) to a
211 * destination endpoint (specified by dst). It's not allowed for len to be 0.
213 * It returns a 'tevent_req' handle, where the caller can register a callback
214 * with tevent_req_set_callback(). The callback is triggered when the specific
215 * implementation (assumes it) has delivered the datagram to the "wire".
217 * The callback is then supposed to get the result by calling
218 * tdgram_sendto_recv() on the 'tevent_req'.
220 * @param[in] mem_ctx The talloc memory context to use.
222 * @param[in] ev The tevent_context to run on.
224 * @param[in] dgram The dgram context to work on.
226 * @param[in] buf The buffer to send.
228 * @param[in] len The length of the buffer to send. It has to be bigger
229 * than 0.
231 * @param[in] dst The destination to send the datagram to in form of a
232 * tsocket_address.
234 * @return Returns a 'tevent_req' handle, where the caller can
235 * register a callback with tevent_req_set_callback().
236 * NULL on fatal error.
238 * @see tdgram_inet_udp_socket()
239 * @see tdgram_unix_socket()
240 * @see tdgram_sendto_recv()
242 struct tevent_req *tdgram_sendto_send(TALLOC_CTX *mem_ctx,
243 struct tevent_context *ev,
244 struct tdgram_context *dgram,
245 const uint8_t *buf, size_t len,
246 const struct tsocket_address *dst);
249 * @brief Receive the result of the sent datagram.
251 * The caller can only have one outstanding tdgram_sendto_send() at a time
252 * otherwise the caller will get '*perrno = EBUSY'.
254 * @param[in] req The tevent request from tdgram_sendto_send().
256 * @param[out] perrno The error number, set if an error occurred.
258 * @return The length of the datagram (0 is never returned!), -1 on
259 * error with perrno set to the actual errno.
261 * @see tdgram_sendto_send()
263 ssize_t tdgram_sendto_recv(struct tevent_req *req,
264 int *perrno);
267 * @brief Shutdown/close an abstracted socket.
269 * It returns a 'tevent_req' handle, where the caller can register a callback
270 * with tevent_req_set_callback(). The callback is triggered when the specific
271 * implementation (assumes it) has delivered the datagram to the "wire".
273 * The callback is then supposed to get the result by calling
274 * tdgram_sendto_recv() on the 'tevent_req'.
276 * @param[in] mem_ctx The talloc memory context to use.
278 * @param[in] ev The tevent_context to run on.
280 * @param[in] dgram The dgram context diconnect from.
282 * @return Returns a 'tevent_req' handle, where the caller can
283 * register a callback with tevent_req_set_callback().
284 * NULL on fatal error.
286 * @see tdgram_disconnect_recv()
288 struct tevent_req *tdgram_disconnect_send(TALLOC_CTX *mem_ctx,
289 struct tevent_context *ev,
290 struct tdgram_context *dgram);
293 * @brief Receive the result from a tdgram_disconnect_send() request.
295 * The caller should make sure there're no outstanding tdgram_recvfrom_send()
296 * and tdgram_sendto_send() calls otherwise the caller will get
297 * '*perrno = EBUSY'.
299 * @param[in] req The tevent request from tdgram_disconnect_send().
301 * @param[out] perrno The error number, set if an error occurred.
303 * @return The length of the datagram (0 is never returned!), -1 on
304 * error with perrno set to the actual errno.
306 * @see tdgram_disconnect_send()
308 int tdgram_disconnect_recv(struct tevent_req *req,
309 int *perrno);
312 * @}
316 * @defgroup tstream_context The tstream_context abstraction
317 * @ingroup tsocket
319 * The tstream_context is like an abstract class for stream based sockets. The
320 * interface provides async 'tevent_req' based functions on top functionality
321 * is similar to the readv(2)/writev(2)/close(2) syscalls.
323 * @note You can always use talloc_free(tstream) to cleanup the resources
324 * of the tstream_context on a fatal error.
326 * @{
330 * @brief Report the number of bytes received but not consumed yet.
332 * The tstream_pending_bytes() function reports how much bytes of the incoming
333 * stream have been received but not consumed yet.
335 * @param[in] stream The tstream_context to check for pending bytes.
337 * @return The number of bytes received, -1 on error with errno
338 * set.
340 ssize_t tstream_pending_bytes(struct tstream_context *stream);
343 * @brief Read a specific amount of bytes from a stream socket.
345 * The function can be called to read for a specific amount of bytes from the
346 * stream into given buffers. The caller has to preallocate the buffers.
348 * The caller might need to use tstream_pending_bytes() if the protocol doesn't
349 * have a fixed pdu header containing the pdu size.
351 * @param[in] mem_ctx The talloc memory context to use.
353 * @param[in] ev The tevent_context to run on.
355 * @param[in] stream The tstream context to work on.
357 * @param[out] vector A preallocated iovec to store the data to read.
359 * @param[in] count The number of buffers in the vector allocated.
361 * @return A 'tevent_req' handle, where the caller can register
362 * a callback with tevent_req_set_callback(). NULL on
363 * fatal error.
365 * @see tstream_unix_connect_send()
366 * @see tstream_inet_tcp_connect_send()
368 struct tevent_req *tstream_readv_send(TALLOC_CTX *mem_ctx,
369 struct tevent_context *ev,
370 struct tstream_context *stream,
371 struct iovec *vector,
372 size_t count);
375 * @brief Get the result of a tstream_readv_send().
377 * The caller can only have one outstanding tstream_readv_send()
378 * at a time otherwise the caller will get *perrno = EBUSY.
380 * @param[in] req The tevent request from tstream_readv_send().
382 * @param[out] perrno The error number, set if an error occurred.
384 * @return The length of the stream (0 is never returned!), -1 on
385 * error with perrno set to the actual errno.
387 int tstream_readv_recv(struct tevent_req *req,
388 int *perrno);
391 * @brief Write buffers from a vector into a stream socket.
393 * The function can be called to write buffers from a given vector
394 * to a stream socket.
396 * You have to ensure that the vector is not empty.
398 * @param[in] mem_ctx The talloc memory context to use.
400 * @param[in] ev The tevent_context to run on.
402 * @param[in] stream The tstream context to work on.
404 * @param[in] vector The iovec vector with data to write on a stream socket.
406 * @param[in] count The number of buffers in the vector to write.
408 * @return A 'tevent_req' handle, where the caller can register
409 * a callback with tevent_req_set_callback(). NULL on
410 * fatal error.
412 struct tevent_req *tstream_writev_send(TALLOC_CTX *mem_ctx,
413 struct tevent_context *ev,
414 struct tstream_context *stream,
415 const struct iovec *vector,
416 size_t count);
419 * @brief Get the result of a tstream_writev_send().
421 * The caller can only have one outstanding tstream_writev_send()
422 * at a time otherwise the caller will get *perrno = EBUSY.
424 * @param[in] req The tevent request from tstream_writev_send().
426 * @param[out] perrno The error number, set if an error occurred.
428 * @return The length of the stream (0 is never returned!), -1 on
429 * error with perrno set to the actual errno.
431 int tstream_writev_recv(struct tevent_req *req,
432 int *perrno);
435 * @brief Shutdown/close an abstracted socket.
437 * It returns a 'tevent_req' handle, where the caller can register a callback
438 * with tevent_req_set_callback(). The callback is triggered when the specific
439 * implementation (assumes it) has delivered the stream to the "wire".
441 * The callback is then supposed to get the result by calling
442 * tdgram_sendto_recv() on the 'tevent_req'.
444 * @param[in] mem_ctx The talloc memory context to use.
446 * @param[in] ev The tevent_context to run on.
448 * @param[in] stream The tstream context to work on.
450 * @return A 'tevent_req' handle, where the caller can register
451 * a callback with tevent_req_set_callback(). NULL on
452 * fatal error.
454 struct tevent_req *tstream_disconnect_send(TALLOC_CTX *mem_ctx,
455 struct tevent_context *ev,
456 struct tstream_context *stream);
459 * @brief Get the result of a tstream_disconnect_send().
461 * The caller can only have one outstanding tstream_writev_send()
462 * at a time otherwise the caller will get *perrno = EBUSY.
464 * @param[in] req The tevent request from tstream_disconnect_send().
466 * @param[out] perrno The error number, set if an error occurred.
468 * @return The length of the stream (0 is never returned!), -1 on
469 * error with perrno set to the actual errno.
471 int tstream_disconnect_recv(struct tevent_req *req,
472 int *perrno);
475 * @}
480 * @defgroup tsocket_bsd tsocket_bsd - inet, inet6 and unix
481 * @ingroup tsocket
483 * The main tsocket library comes with implentations for BSD style ipv4, ipv6
484 * and unix sockets.
486 * @{
489 #if DOXYGEN
491 * @brief Create a tsocket_address for ipv4 and ipv6 endpoint addresses.
493 * @param[in] mem_ctx The talloc memory context to use.
495 * @param[in] fam The family can be can be "ipv4", "ipv6" or "ip". With
496 * "ip" is autodetects "ipv4" or "ipv6" based on the
497 * addr.
499 * @param[in] addr A valid ip address string based on the selected family
500 * (dns names are not allowed!). It's valid to pass NULL,
501 * which gets mapped to "0.0.0.0" or "::".
503 * @param[in] port A valid port number.
505 * @param[out] _addr A tsocket_address pointer to store the information.
507 * @return 0 on success, -1 on error with errno set.
509 int tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
510 const char *fam,
511 const char *addr,
512 uint16_t port,
513 struct tsocket_address **_addr);
514 #else
515 int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
516 const char *fam,
517 const char *addr,
518 uint16_t port,
519 struct tsocket_address **_addr,
520 const char *location);
522 #define tsocket_address_inet_from_strings(mem_ctx, fam, addr, port, _addr) \
523 _tsocket_address_inet_from_strings(mem_ctx, fam, addr, port, _addr, \
524 __location__)
525 #endif
528 * @brief Get the address of an 'inet' tsocket_address as a string.
530 * @param[in] addr The address to convert to a string.
532 * @param[in] mem_ctx The talloc memory context to use.
534 * @return A newly allocated string of the address, NULL on error
535 * with errno set.
537 char *tsocket_address_inet_addr_string(const struct tsocket_address *addr,
538 TALLOC_CTX *mem_ctx);
541 * @brief Get the port number as an integer from an 'inet' tsocket_address.
543 * @param[in] addr The tsocket address to use.
545 * @return The port number, 0 on error with errno set.
547 uint16_t tsocket_address_inet_port(const struct tsocket_address *addr);
550 * @brief Set the port number of an existing 'inet' tsocket_address.
552 * @param[in] addr The existing tsocket_address to use.
554 * @param[in] port The valid port number to set.
556 * @return 0 on success, -1 on error with errno set.
558 int tsocket_address_inet_set_port(struct tsocket_address *addr,
559 uint16_t port);
561 #ifdef DOXYGEN
563 * @brief Create a tsocket_address for a unix domain endpoint addresses.
565 * @param[in] mem_ctx The talloc memory context to use.
567 * @param[in] path The filesystem path, NULL will map "".
569 * @param[in] _addr The tsocket_address pointer to store the information.
571 * @return 0 on success, -1 on error with errno set.
573 int tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
574 const char *path,
575 struct tsocket_address **_addr);
576 #else
577 int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
578 const char *path,
579 struct tsocket_address **_addr,
580 const char *location);
582 #define tsocket_address_unix_from_path(mem_ctx, path, _addr) \
583 _tsocket_address_unix_from_path(mem_ctx, path, _addr, \
584 __location__)
585 #endif
588 * @brief Get the address of an 'unix' tsocket_address.
590 * @param[in] addr A valid 'unix' tsocket_address.
592 * @param[in] mem_ctx The talloc memory context to use.
594 * @return The path of the unix domain socket, NULL on error or if
595 * the tsocket_address doesn't represent an unix domain
596 * endpoint path.
598 char *tsocket_address_unix_path(const struct tsocket_address *addr,
599 TALLOC_CTX *mem_ctx);
601 #ifdef DOXYGEN
603 * @brief Create a tdgram_context for a ipv4 or ipv6 UDP communication.
605 * @param[in] local An 'inet' tsocket_address for the local endpoint.
607 * @param[in] remote An 'inet' tsocket_address for the remote endpoint or
608 * NULL (??? to create a listener?).
610 * @param[in] mem_ctx The talloc memory context to use.
612 * @param[in] dgram The tdgram_context pointer to setup the udp
613 * communication. The function will allocate the memory.
615 * @return 0 on success, -1 on error with errno set.
617 int tdgram_inet_udp_socket(const struct tsocket_address *local,
618 const struct tsocket_address *remote,
619 TALLOC_CTX *mem_ctx,
620 struct tdgram_context **dgram);
621 #else
622 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
623 const struct tsocket_address *remote,
624 TALLOC_CTX *mem_ctx,
625 struct tdgram_context **dgram,
626 const char *location);
627 #define tdgram_inet_udp_socket(local, remote, mem_ctx, dgram) \
628 _tdgram_inet_udp_socket(local, remote, mem_ctx, dgram, __location__)
629 #endif
631 #ifdef DOXYGEN
633 * @brief Create a tdgram_context for unix domain datagram communication.
635 * @param[in] local An 'unix' tsocket_address for the local endpoint.
637 * @param[in] remote An 'unix' tsocket_address for the remote endpoint or
638 * NULL (??? to create a listener?).
640 * @param[in] mem_ctx The talloc memory context to use.
642 * @param[in] dgram The tdgram_context pointer to setup the udp
643 * communication. The function will allocate the memory.
645 * @return 0 on success, -1 on error with errno set.
647 int tdgram_unix_socket(const struct tsocket_address *local,
648 const struct tsocket_address *remote,
649 TALLOC_CTX *mem_ctx,
650 struct tdgram_context **dgram);
651 #else
652 int _tdgram_unix_socket(const struct tsocket_address *local,
653 const struct tsocket_address *remote,
654 TALLOC_CTX *mem_ctx,
655 struct tdgram_context **dgram,
656 const char *location);
658 #define tdgram_unix_socket(local, remote, mem_ctx, dgram) \
659 _tdgram_unix_socket(local, remote, mem_ctx, dgram, __location__)
660 #endif
663 * @brief Connect async to a TCP endpoint and create a tstream_context for the
664 * stream based communication.
666 * Use this function to connenct asynchronously to a remote ipv4 or ipv6 TCP
667 * endpoint and create a tstream_context for the stream based communication.
669 * @param[in] mem_ctx The talloc memory context to use.
671 * @param[in] ev The tevent_context to run on.
673 * @param[in] local An 'inet' tsocket_address for the local endpoint.
675 * @param[in] remote An 'inet' tsocket_address for the remote endpoint.
677 * @return A 'tevent_req' handle, where the caller can register a
678 * callback with tevent_req_set_callback(). NULL on a fatal
679 * error.
681 * @see tstream_inet_tcp_connect_recv()
683 struct tevent_req *tstream_inet_tcp_connect_send(TALLOC_CTX *mem_ctx,
684 struct tevent_context *ev,
685 const struct tsocket_address *local,
686 const struct tsocket_address *remote);
688 #ifdef DOXYGEN
690 * @brief Receive the result from a tstream_inet_tcp_connect_send().
692 * @param[in] req The tevent request from tstream_inet_tcp_connect_send().
694 * @param[out] perrno The error number, set if an error occurred.
696 * @param[in] mem_ctx The talloc memory context to use.
698 * @param[in] stream A tstream_context pointer to setup the tcp communication
699 * on. This function will allocate the memory.
701 * @return 0 on success, -1 on error with perrno set.
703 int tstream_inet_tcp_connect_recv(struct tevent_req *req,
704 int *perrno,
705 TALLOC_CTX *mem_ctx,
706 struct tstream_context **stream);
707 #else
708 int _tstream_inet_tcp_connect_recv(struct tevent_req *req,
709 int *perrno,
710 TALLOC_CTX *mem_ctx,
711 struct tstream_context **stream,
712 const char *location);
713 #define tstream_inet_tcp_connect_recv(req, perrno, mem_ctx, stream) \
714 _tstream_inet_tcp_connect_recv(req, perrno, mem_ctx, stream, \
715 __location__)
716 #endif
719 * @brief Connect async to a unix domain endpoint and create a tstream_context
720 * for the stream based communication.
722 * Use this function to connenct asynchronously to a unix domainendpoint and
723 * create a tstream_context for the stream based communication.
725 * The callback is triggered when a socket is connected and ready for IO or an
726 * error happened.
728 * @param[in] mem_ctx The talloc memory context to use.
730 * @param[in] ev The tevent_context to run on.
732 * @param[in] local An 'unix' tsocket_address for the local endpoint.
734 * @param[in] remote An 'unix' tsocket_address for the remote endpoint.
736 * @return A 'tevent_req' handle, where the caller can register a
737 * callback with tevent_req_set_callback(). NULL on a falal
738 * error.
740 * @see tstream_unix_connect_recv()
742 struct tevent_req * tstream_unix_connect_send(TALLOC_CTX *mem_ctx,
743 struct tevent_context *ev,
744 const struct tsocket_address *local,
745 const struct tsocket_address *remote);
747 #ifdef DOXYGEN
749 * @brief Receive the result from a tstream_unix_connect_send().
751 * @param[in] req The tevent request from tstream_inet_tcp_connect_send().
753 * @param[out] perrno The error number, set if an error occurred.
755 * @param[in] mem_ctx The talloc memory context to use.
757 * @param[in] stream The tstream context to work on.
759 * @return 0 on success, -1 on error with perrno set.
761 int tstream_unix_connect_recv(struct tevent_req *req,
762 int *perrno,
763 TALLOC_CTX *mem_ctx,
764 struct tstream_context **stream);
765 #else
766 int _tstream_unix_connect_recv(struct tevent_req *req,
767 int *perrno,
768 TALLOC_CTX *mem_ctx,
769 struct tstream_context **stream,
770 const char *location);
771 #define tstream_unix_connect_recv(req, perrno, mem_ctx, stream) \
772 _tstream_unix_connect_recv(req, perrno, mem_ctx, stream, \
773 __location__)
774 #endif
776 #ifdef DOXYGEN
778 * @brief Create two connected 'unix' tsocket_contexts for stream based
779 * communication.
781 * @param[in] mem_ctx1 The talloc memory context to use for stream1.
783 * @param[in] stream1 The first stream to connect.
785 * @param[in] mem_ctx2 The talloc memory context to use for stream2.
787 * @param[in] stream2 The second stream to connect.
789 * @return 0 on success, -1 on error with errno set.
791 int tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
792 struct tstream_context **stream1,
793 TALLOC_CTX *mem_ctx2,
794 struct tstream_context **stream2);
795 #else
796 int _tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
797 struct tstream_context **_stream1,
798 TALLOC_CTX *mem_ctx2,
799 struct tstream_context **_stream2,
800 const char *location);
802 #define tstream_unix_socketpair(mem_ctx1, stream1, mem_ctx2, stream2) \
803 _tstream_unix_socketpair(mem_ctx1, stream1, mem_ctx2, stream2, \
804 __location__)
805 #endif
807 struct sockaddr;
809 #ifdef DOXYGEN
811 * @brief Convert a tsocket address to a bsd socket address.
813 * @param[in] mem_ctx The talloc memory context to use.
815 * @param[in] sa The sockaddr structure to convert.
817 * @param[in] sa_socklen The lenth of the sockaddr sturucte.
819 * @param[out] addr The tsocket pointer to allocate and fill.
821 * @return 0 on success, -1 on error with errno set.
823 int tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx,
824 struct sockaddr *sa,
825 size_t sa_socklen,
826 struct tsocket_address **addr);
827 #else
828 int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx,
829 struct sockaddr *sa,
830 size_t sa_socklen,
831 struct tsocket_address **_addr,
832 const char *location);
834 #define tsocket_address_bsd_from_sockaddr(mem_ctx, sa, sa_socklen, _addr) \
835 _tsocket_address_bsd_from_sockaddr(mem_ctx, sa, sa_socklen, _addr, \
836 __location__)
837 #endif
840 * @brief Fill a bsd sockaddr structure.
842 * @param[in] addr The tsocket address structure to use.
844 * @param[in] sa The bsd sockaddr structure to fill out.
846 * @param[in] sa_socklen The length of the bsd sockaddr structure to fill out.
848 * @return The actual size of the sockaddr structure, -1 on error
849 * with errno set. The size could differ from sa_socklen.
851 * @code
852 * ssize_t socklen;
853 * struct sockaddr_storage ss;
855 * socklen = tsocket_address_bsd_sockaddr(taddr,
856 * (struct sockaddr *) &ss,
857 * sizeof(struct sockaddr_storage));
858 * if (socklen < 0) {
859 * return -1;
861 * @endcode
863 ssize_t tsocket_address_bsd_sockaddr(const struct tsocket_address *addr,
864 struct sockaddr *sa,
865 size_t sa_socklen);
867 #ifdef DOXYGEN
869 * @brief Wrap an existing file descriptors into the tstream abstraction.
871 * You can use this function to wrap an existing file descriptors into the
872 * tstream abstraction. After that you're not able to use this file descriptor
873 * for anything else. The file descriptor will be closed when the stream gets
874 * freed. If you still want to use the fd you have have to create a duplicate.
876 * @param[in] mem_ctx The talloc memory context to use.
878 * @param[in] fd The non blocking fd to use!
880 * @param[in] stream The filed tstream_context you allocated before.
882 * @return 0 on success, -1 on error with errno set.
884 * @warning You should read the tsocket_bsd.c code and unterstand it in order
885 * use this function.
887 int tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
888 int fd,
889 struct tstream_context **stream);
890 #else
891 int _tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
892 int fd,
893 struct tstream_context **_stream,
894 const char *location);
895 #define tstream_bsd_existing_socket(mem_ctx, fd, stream) \
896 _tstream_bsd_existing_socket(mem_ctx, fd, stream, \
897 __location__)
898 #endif
901 * @}
905 * @defgroup tsocket_helper Queue and PDU helpers
906 * @ingroup tsocket
908 * In order to make the live easier for callers which want to implement a
909 * function to receive a full PDU with a single async function pair, there're
910 * some helper functions.
912 * There're some cases where the caller wants doesn't care about the order of
913 * doing IO on the abstracted sockets.
915 * @{
919 * @brief Queue a dgram blob for sending through the socket.
921 * This function queues a blob for sending to destination through an existing
922 * dgram socket. The async callback is triggered when the whole blob is
923 * delivered to the underlying system socket.
925 * The caller needs to make sure that all non-scalar input parameters hang
926 * around for the whole lifetime of the request.
928 * @param[in] mem_ctx The memory context for the result.
930 * @param[in] ev The event context the operation should work on.
932 * @param[in] dgram The tdgram_context to send the message buffer.
934 * @param[in] queue The existing dgram queue.
936 * @param[in] buf The message buffer to send.
938 * @param[in] len The message length.
940 * @param[in] dst The destination socket address.
942 * @return The async request handle. NULL on fatal error.
944 * @see tdgram_sendto_queue_recv()
946 struct tevent_req *tdgram_sendto_queue_send(TALLOC_CTX *mem_ctx,
947 struct tevent_context *ev,
948 struct tdgram_context *dgram,
949 struct tevent_queue *queue,
950 const uint8_t *buf,
951 size_t len,
952 struct tsocket_address *dst);
955 * @brief Receive the result of the sent dgram blob.
957 * @param[in] req The tevent request from tdgram_sendto_queue_send().
959 * @param[out] perrno The error set to the actual errno.
961 * @return The length of the datagram (0 is never returned!), -1 on
962 * error with perrno set to the actual errno.
964 ssize_t tdgram_sendto_queue_recv(struct tevent_req *req, int *perrno);
966 typedef int (*tstream_readv_pdu_next_vector_t)(struct tstream_context *stream,
967 void *private_data,
968 TALLOC_CTX *mem_ctx,
969 struct iovec **vector,
970 size_t *count);
972 struct tevent_req *tstream_readv_pdu_send(TALLOC_CTX *mem_ctx,
973 struct tevent_context *ev,
974 struct tstream_context *stream,
975 tstream_readv_pdu_next_vector_t next_vector_fn,
976 void *next_vector_private);
977 int tstream_readv_pdu_recv(struct tevent_req *req, int *perrno);
980 * @brief Queue a read request for a PDU on the socket.
982 * This function queues a read request for a PDU on a stream socket. The async
983 * callback is triggered when a full PDU has been read from the socket.
985 * The caller needs to make sure that all non-scalar input parameters hang
986 * around for the whole lifetime of the request.
988 * @param[in] mem_ctx The memory context for the result
990 * @param[in] ev The tevent_context to run on
992 * @param[in] stream The stream to send data through
994 * @param[in] queue The existing send queue
996 * @param[in] next_vector_fn The next vector function
998 * @param[in] next_vector_private The private_data of the next vector function
1000 * @return The async request handle. NULL on fatal error.
1002 * @see tstream_readv_pdu_queue_recv()
1004 struct tevent_req *tstream_readv_pdu_queue_send(TALLOC_CTX *mem_ctx,
1005 struct tevent_context *ev,
1006 struct tstream_context *stream,
1007 struct tevent_queue *queue,
1008 tstream_readv_pdu_next_vector_t next_vector_fn,
1009 void *next_vector_private);
1012 * @brief Receive the PDU blob read from the stream.
1014 * @param[in] req The tevent request from tstream_readv_pdu_queue_send().
1016 * @param[out] perrno The error set to the actual errno.
1018 * @return The number of bytes read on success, -1 on error with
1019 * perrno set to the actual errno.
1021 int tstream_readv_pdu_queue_recv(struct tevent_req *req, int *perrno);
1024 * @brief Queue an iovector for sending through the socket
1026 * This function queues an iovector for sending to destination through an
1027 * existing stream socket. The async callback is triggered when the whole
1028 * vectror has been delivered to the underlying system socket.
1030 * The caller needs to make sure that all non-scalar input parameters hang
1031 * around for the whole lifetime of the request.
1033 * @param[in] mem_ctx The memory context for the result.
1035 * @param[in] ev The tevent_context to run on.
1037 * @param[in] stream The stream to send data through.
1039 * @param[in] queue The existing send queue.
1041 * @param[in] vector The iovec vector so write.
1043 * @param[in] count The size of the vector.
1045 * @return The async request handle. NULL on fatal error.
1047 struct tevent_req *tstream_writev_queue_send(TALLOC_CTX *mem_ctx,
1048 struct tevent_context *ev,
1049 struct tstream_context *stream,
1050 struct tevent_queue *queue,
1051 const struct iovec *vector,
1052 size_t count);
1055 * @brief Receive the result of the sent iovector.
1057 * @param[in] req The tevent request from tstream_writev_queue_send().
1059 * @param[out] perrno The error set to the actual errno.
1061 * @return The length of the iovector (0 is never returned!), -1 on
1062 * error with perrno set to the actual errno.
1064 int tstream_writev_queue_recv(struct tevent_req *req, int *perrno);
1067 * @}
1070 #endif /* _TSOCKET_H */