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
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/>.
29 #endif /* HAVE_CONFIG_H */
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
39 #include <arpa/inet.h>
41 #include <atalk/talloc.h>
42 #include <atalk/tevent.h>
43 #include <atalk/util.h>
46 struct tsocket_address;
47 struct tdgram_context;
48 struct tstream_context;
54 * The tsocket abstraction is an API ...
58 * @defgroup tsocket The tsocket API
60 * The tsocket abstraction is split into two different kinds of
61 * communication interfaces.
63 * There's the "tstream_context" interface with abstracts the communication
64 * through a bidirectional byte stream between two endpoints.
66 * And there's the "tdgram_context" interface with abstracts datagram based
67 * communication between any number of endpoints.
69 * Both interfaces share the "tsocket_address" abstraction for endpoint
72 * The whole library is based on the talloc(3) and 'tevent' libraries and
73 * provides "tevent_req" based "foo_send()"/"foo_recv()" functions pairs for
74 * all abstracted methods that need to be async.
76 * @section vsock Virtual Sockets
78 * The abstracted layout of tdgram_context and tstream_context allow
79 * implementations around virtual sockets for encrypted tunnels (like TLS,
80 * SASL or GSSAPI) or named pipes over smb.
82 * @section npa Named Pipe Auth (NPA) Sockets
84 * Samba has an implementation to abstract named pipes over smb (within the
85 * server side). See libcli/named_pipe_auth/npa_tstream.[ch] for the core code.
86 * The current callers are located in source4/ntvfs/ipc/vfs_ipc.c and
87 * source4/rpc_server/service_rpc.c for the users.
91 * @defgroup tsocket_address The tsocket_address abstraction
94 * The tsocket_address represents an socket endpoint genericly.
95 * As it's like an abstract class it has no specific constructor.
96 * The specific constructors are descripted in later sections.
102 * @brief Get a string representaion of the endpoint.
104 * This function creates a string representation of the endpoint for debugging.
105 * The output will look as followed:
106 * prefix:address:port
109 * ipv4:192.168.1.1:143
111 * Callers should not try to parse the string! The should use additional methods
112 * of the specific tsocket_address implemention to get more details.
114 * @param[in] addr The address to convert.
116 * @param[in] mem_ctx The talloc memory context to allocate the memory.
118 * @return The address as a string representation, NULL on error.
120 * @see tsocket_address_inet_addr_string()
121 * @see tsocket_address_inet_port()
123 char *tsocket_address_string(const struct tsocket_address *addr,
124 TALLOC_CTX *mem_ctx);
128 * @brief This creates a copy of a tsocket_address.
130 * This is useful when before doing modifications to a socket via additional
131 * methods of the specific tsocket_address implementation.
133 * @param[in] addr The address to create the copy from.
135 * @param[in] mem_ctx The talloc memory context to use.
137 * @return A newly allocated copy of addr (tsocket_address *), NULL
140 struct tsocket_address *tsocket_address_copy(const struct tsocket_address *addr,
141 TALLOC_CTX *mem_ctx);
143 struct tsocket_address *_tsocket_address_copy(const struct tsocket_address *addr,
145 const char *location);
147 #define tsocket_address_copy(addr, mem_ctx) \
148 _tsocket_address_copy(addr, mem_ctx, __location__)
156 * @defgroup tdgram_context The tdgram_context abstraction
159 * The tdgram_context is like an abstract class for datagram based sockets. The
160 * interface provides async 'tevent_req' based functions on top functionality
161 * is similar to the recvfrom(2)/sendto(2)/close(2) syscalls.
163 * @note You can always use talloc_free(tdgram) to cleanup the resources
164 * of the tdgram_context on a fatal error.
169 * @brief Ask for next available datagram on the abstracted tdgram_context.
171 * It returns a 'tevent_req' handle, where the caller can register
172 * a callback with tevent_req_set_callback(). The callback is triggered
173 * when a datagram is available or an error happened.
175 * @param[in] mem_ctx The talloc memory context to use.
177 * @param[in] ev The tevent_context to run on.
179 * @param[in] dgram The dgram context to work on.
181 * @return Returns a 'tevent_req' handle, where the caller can
182 * register a callback with tevent_req_set_callback().
183 * NULL on fatal error.
185 * @see tdgram_inet_udp_socket()
186 * @see tdgram_unix_socket()
188 struct tevent_req *tdgram_recvfrom_send(TALLOC_CTX *mem_ctx,
189 struct tevent_context *ev,
190 struct tdgram_context *dgram);
193 * @brief Receive the next available datagram on the abstracted tdgram_context.
195 * This function should be called by the callback when a datagram is available
196 * or an error happened.
198 * The caller can only have one outstanding tdgram_recvfrom_send() at a time
199 * otherwise the caller will get '*perrno = EBUSY'.
201 * @param[in] req The tevent request from tdgram_recvfrom_send().
203 * @param[out] perrno The error number, set if an error occurred.
205 * @param[in] mem_ctx The memory context to use.
207 * @param[out] buf This will hold the buffer of the datagram.
209 * @param[out] src The abstracted tsocket_address of the sender of the
212 * @return The length of the datagram (0 is never returned!),
213 * -1 on error with perrno set to the actual errno.
215 * @see tdgram_recvfrom_send()
217 ssize_t tdgram_recvfrom_recv(struct tevent_req *req,
221 struct tsocket_address **src);
224 * @brief Send a datagram to a destination endpoint.
226 * The function can be called to send a datagram (specified by a buf/len) to a
227 * destination endpoint (specified by dst). It's not allowed for len to be 0.
229 * It returns a 'tevent_req' handle, where the caller can register a callback
230 * with tevent_req_set_callback(). The callback is triggered when the specific
231 * implementation (assumes it) has delivered the datagram to the "wire".
233 * The callback is then supposed to get the result by calling
234 * tdgram_sendto_recv() on the 'tevent_req'.
236 * @param[in] mem_ctx The talloc memory context to use.
238 * @param[in] ev The tevent_context to run on.
240 * @param[in] dgram The dgram context to work on.
242 * @param[in] buf The buffer to send.
244 * @param[in] len The length of the buffer to send. It has to be bigger
247 * @param[in] dst The destination to send the datagram to in form of a
250 * @return Returns a 'tevent_req' handle, where the caller can
251 * register a callback with tevent_req_set_callback().
252 * NULL on fatal error.
254 * @see tdgram_inet_udp_socket()
255 * @see tdgram_unix_socket()
256 * @see tdgram_sendto_recv()
258 struct tevent_req *tdgram_sendto_send(TALLOC_CTX *mem_ctx,
259 struct tevent_context *ev,
260 struct tdgram_context *dgram,
261 const uint8_t *buf, size_t len,
262 const struct tsocket_address *dst);
265 * @brief Receive the result of the sent datagram.
267 * The caller can only have one outstanding tdgram_sendto_send() at a time
268 * otherwise the caller will get '*perrno = EBUSY'.
270 * @param[in] req The tevent request from tdgram_sendto_send().
272 * @param[out] perrno The error number, set if an error occurred.
274 * @return The length of the datagram (0 is never returned!), -1 on
275 * error with perrno set to the actual errno.
277 * @see tdgram_sendto_send()
279 ssize_t tdgram_sendto_recv(struct tevent_req *req,
283 * @brief Shutdown/close an abstracted socket.
285 * It returns a 'tevent_req' handle, where the caller can register a callback
286 * with tevent_req_set_callback(). The callback is triggered when the specific
287 * implementation (assumes it) has delivered the datagram to the "wire".
289 * The callback is then supposed to get the result by calling
290 * tdgram_sendto_recv() on the 'tevent_req'.
292 * @param[in] mem_ctx The talloc memory context to use.
294 * @param[in] ev The tevent_context to run on.
296 * @param[in] dgram The dgram context diconnect from.
298 * @return Returns a 'tevent_req' handle, where the caller can
299 * register a callback with tevent_req_set_callback().
300 * NULL on fatal error.
302 * @see tdgram_disconnect_recv()
304 struct tevent_req *tdgram_disconnect_send(TALLOC_CTX *mem_ctx,
305 struct tevent_context *ev,
306 struct tdgram_context *dgram);
309 * @brief Receive the result from a tdgram_disconnect_send() request.
311 * The caller should make sure there're no outstanding tdgram_recvfrom_send()
312 * and tdgram_sendto_send() calls otherwise the caller will get
315 * @param[in] req The tevent request from tdgram_disconnect_send().
317 * @param[out] perrno The error number, set if an error occurred.
319 * @return The length of the datagram (0 is never returned!), -1 on
320 * error with perrno set to the actual errno.
322 * @see tdgram_disconnect_send()
324 int tdgram_disconnect_recv(struct tevent_req *req,
332 * @defgroup tstream_context The tstream_context abstraction
335 * The tstream_context is like an abstract class for stream based sockets. The
336 * interface provides async 'tevent_req' based functions on top functionality
337 * is similar to the readv(2)/writev(2)/close(2) syscalls.
339 * @note You can always use talloc_free(tstream) to cleanup the resources
340 * of the tstream_context on a fatal error.
346 * @brief Report the number of bytes received but not consumed yet.
348 * The tstream_pending_bytes() function reports how much bytes of the incoming
349 * stream have been received but not consumed yet.
351 * @param[in] stream The tstream_context to check for pending bytes.
353 * @return The number of bytes received, -1 on error with errno
356 ssize_t tstream_pending_bytes(struct tstream_context *stream);
359 * @brief Read a specific amount of bytes from a stream socket.
361 * The function can be called to read for a specific amount of bytes from the
362 * stream into given buffers. The caller has to preallocate the buffers.
364 * The caller might need to use tstream_pending_bytes() if the protocol doesn't
365 * have a fixed pdu header containing the pdu size.
367 * @param[in] mem_ctx The talloc memory context to use.
369 * @param[in] ev The tevent_context to run on.
371 * @param[in] stream The tstream context to work on.
373 * @param[out] vector A preallocated iovec to store the data to read.
375 * @param[in] count The number of buffers in the vector allocated.
377 * @return A 'tevent_req' handle, where the caller can register
378 * a callback with tevent_req_set_callback(). NULL on
381 * @see tstream_unix_connect_send()
382 * @see tstream_inet_tcp_connect_send()
384 struct tevent_req *tstream_readv_send(TALLOC_CTX *mem_ctx,
385 struct tevent_context *ev,
386 struct tstream_context *stream,
387 struct iovec *vector,
391 * @brief Get the result of a tstream_readv_send().
393 * The caller can only have one outstanding tstream_readv_send()
394 * at a time otherwise the caller will get *perrno = EBUSY.
396 * @param[in] req The tevent request from tstream_readv_send().
398 * @param[out] perrno The error number, set if an error occurred.
400 * @return The length of the stream (0 is never returned!), -1 on
401 * error with perrno set to the actual errno.
403 int tstream_readv_recv(struct tevent_req *req,
407 * @brief Write buffers from a vector into a stream socket.
409 * The function can be called to write buffers from a given vector
410 * to a stream socket.
412 * You have to ensure that the vector is not empty.
414 * @param[in] mem_ctx The talloc memory context to use.
416 * @param[in] ev The tevent_context to run on.
418 * @param[in] stream The tstream context to work on.
420 * @param[in] vector The iovec vector with data to write on a stream socket.
422 * @param[in] count The number of buffers in the vector to write.
424 * @return A 'tevent_req' handle, where the caller can register
425 * a callback with tevent_req_set_callback(). NULL on
428 struct tevent_req *tstream_writev_send(TALLOC_CTX *mem_ctx,
429 struct tevent_context *ev,
430 struct tstream_context *stream,
431 const struct iovec *vector,
435 * @brief Get the result of a tstream_writev_send().
437 * The caller can only have one outstanding tstream_writev_send()
438 * at a time otherwise the caller will get *perrno = EBUSY.
440 * @param[in] req The tevent request from tstream_writev_send().
442 * @param[out] perrno The error number, set if an error occurred.
444 * @return The length of the stream (0 is never returned!), -1 on
445 * error with perrno set to the actual errno.
447 int tstream_writev_recv(struct tevent_req *req,
451 * @brief Shutdown/close an abstracted socket.
453 * It returns a 'tevent_req' handle, where the caller can register a callback
454 * with tevent_req_set_callback(). The callback is triggered when the specific
455 * implementation (assumes it) has delivered the stream to the "wire".
457 * The callback is then supposed to get the result by calling
458 * tdgram_sendto_recv() on the 'tevent_req'.
460 * @param[in] mem_ctx The talloc memory context to use.
462 * @param[in] ev The tevent_context to run on.
464 * @param[in] stream The tstream context to work on.
466 * @return A 'tevent_req' handle, where the caller can register
467 * a callback with tevent_req_set_callback(). NULL on
470 struct tevent_req *tstream_disconnect_send(TALLOC_CTX *mem_ctx,
471 struct tevent_context *ev,
472 struct tstream_context *stream);
475 * @brief Get the result of a tstream_disconnect_send().
477 * The caller can only have one outstanding tstream_writev_send()
478 * at a time otherwise the caller will get *perrno = EBUSY.
480 * @param[in] req The tevent request from tstream_disconnect_send().
482 * @param[out] perrno The error number, set if an error occurred.
484 * @return The length of the stream (0 is never returned!), -1 on
485 * error with perrno set to the actual errno.
487 int tstream_disconnect_recv(struct tevent_req *req,
496 * @defgroup tsocket_bsd tsocket_bsd - inet, inet6 and unix
499 * The main tsocket library comes with implentations for BSD style ipv4, ipv6
507 * @brief Create a tsocket_address for ipv4 and ipv6 endpoint addresses.
509 * @param[in] mem_ctx The talloc memory context to use.
511 * @param[in] fam The family can be can be "ipv4", "ipv6" or "ip". With
512 * "ip" is autodetects "ipv4" or "ipv6" based on the
515 * @param[in] addr A valid ip address string based on the selected family
516 * (dns names are not allowed!). It's valid to pass NULL,
517 * which gets mapped to "0.0.0.0" or "::".
519 * @param[in] port A valid port number.
521 * @param[out] _addr A tsocket_address pointer to store the information.
523 * @return 0 on success, -1 on error with errno set.
525 int tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
529 struct tsocket_address **_addr);
531 int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
535 struct tsocket_address **_addr,
536 const char *location);
538 #define tsocket_address_inet_from_strings(mem_ctx, fam, addr, port, _addr) \
539 _tsocket_address_inet_from_strings(mem_ctx, fam, addr, port, _addr, \
544 * @brief Get the address of an 'inet' tsocket_address as a string.
546 * @param[in] addr The address to convert to a string.
548 * @param[in] mem_ctx The talloc memory context to use.
550 * @return A newly allocated string of the address, NULL on error
553 char *tsocket_address_inet_addr_string(const struct tsocket_address *addr,
554 TALLOC_CTX *mem_ctx);
557 * @brief Get the port number as an integer from an 'inet' tsocket_address.
559 * @param[in] addr The tsocket address to use.
561 * @return The port number, 0 on error with errno set.
563 uint16_t tsocket_address_inet_port(const struct tsocket_address *addr);
566 * @brief Set the port number of an existing 'inet' tsocket_address.
568 * @param[in] addr The existing tsocket_address to use.
570 * @param[in] port The valid port number to set.
572 * @return 0 on success, -1 on error with errno set.
574 int tsocket_address_inet_set_port(struct tsocket_address *addr,
579 * @brief Create a tsocket_address for a unix domain endpoint addresses.
581 * @param[in] mem_ctx The talloc memory context to use.
583 * @param[in] path The filesystem path, NULL will map "".
585 * @param[in] _addr The tsocket_address pointer to store the information.
587 * @return 0 on success, -1 on error with errno set.
589 int tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
591 struct tsocket_address **_addr);
593 int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
595 struct tsocket_address **_addr,
596 const char *location);
598 #define tsocket_address_unix_from_path(mem_ctx, path, _addr) \
599 _tsocket_address_unix_from_path(mem_ctx, path, _addr, \
604 * @brief Get the address of an 'unix' tsocket_address.
606 * @param[in] addr A valid 'unix' tsocket_address.
608 * @param[in] mem_ctx The talloc memory context to use.
610 * @return The path of the unix domain socket, NULL on error or if
611 * the tsocket_address doesn't represent an unix domain
614 char *tsocket_address_unix_path(const struct tsocket_address *addr,
615 TALLOC_CTX *mem_ctx);
619 * @brief Create a tdgram_context for a ipv4 or ipv6 UDP communication.
621 * @param[in] local An 'inet' tsocket_address for the local endpoint.
623 * @param[in] remote An 'inet' tsocket_address for the remote endpoint or
624 * NULL (??? to create a listener?).
626 * @param[in] mem_ctx The talloc memory context to use.
628 * @param[in] dgram The tdgram_context pointer to setup the udp
629 * communication. The function will allocate the memory.
631 * @return 0 on success, -1 on error with errno set.
633 int tdgram_inet_udp_socket(const struct tsocket_address *local,
634 const struct tsocket_address *remote,
636 struct tdgram_context **dgram);
638 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
639 const struct tsocket_address *remote,
641 struct tdgram_context **dgram,
642 const char *location);
643 #define tdgram_inet_udp_socket(local, remote, mem_ctx, dgram) \
644 _tdgram_inet_udp_socket(local, remote, mem_ctx, dgram, __location__)
649 * @brief Create a tdgram_context for unix domain datagram communication.
651 * @param[in] local An 'unix' tsocket_address for the local endpoint.
653 * @param[in] remote An 'unix' tsocket_address for the remote endpoint or
654 * NULL (??? to create a listener?).
656 * @param[in] mem_ctx The talloc memory context to use.
658 * @param[in] dgram The tdgram_context pointer to setup the udp
659 * communication. The function will allocate the memory.
661 * @return 0 on success, -1 on error with errno set.
663 int tdgram_unix_socket(const struct tsocket_address *local,
664 const struct tsocket_address *remote,
666 struct tdgram_context **dgram);
668 int _tdgram_unix_socket(const struct tsocket_address *local,
669 const struct tsocket_address *remote,
671 struct tdgram_context **dgram,
672 const char *location);
674 #define tdgram_unix_socket(local, remote, mem_ctx, dgram) \
675 _tdgram_unix_socket(local, remote, mem_ctx, dgram, __location__)
679 * @brief Connect async to a TCP endpoint and create a tstream_context for the
680 * stream based communication.
682 * Use this function to connenct asynchronously to a remote ipv4 or ipv6 TCP
683 * endpoint and create a tstream_context for the stream based communication.
685 * @param[in] mem_ctx The talloc memory context to use.
687 * @param[in] ev The tevent_context to run on.
689 * @param[in] local An 'inet' tsocket_address for the local endpoint.
691 * @param[in] remote An 'inet' tsocket_address for the remote endpoint.
693 * @return A 'tevent_req' handle, where the caller can register a
694 * callback with tevent_req_set_callback(). NULL on a fatal
697 * @see tstream_inet_tcp_connect_recv()
699 struct tevent_req *tstream_inet_tcp_connect_send(TALLOC_CTX *mem_ctx,
700 struct tevent_context *ev,
701 const struct tsocket_address *local,
702 const struct tsocket_address *remote);
706 * @brief Receive the result from a tstream_inet_tcp_connect_send().
708 * @param[in] req The tevent request from tstream_inet_tcp_connect_send().
710 * @param[out] perrno The error number, set if an error occurred.
712 * @param[in] mem_ctx The talloc memory context to use.
714 * @param[in] stream A tstream_context pointer to setup the tcp communication
715 * on. This function will allocate the memory.
717 * @return 0 on success, -1 on error with perrno set.
719 int tstream_inet_tcp_connect_recv(struct tevent_req *req,
722 struct tstream_context **stream);
724 int _tstream_inet_tcp_connect_recv(struct tevent_req *req,
727 struct tstream_context **stream,
728 const char *location);
729 #define tstream_inet_tcp_connect_recv(req, perrno, mem_ctx, stream) \
730 _tstream_inet_tcp_connect_recv(req, perrno, mem_ctx, stream, \
735 * @brief Connect async to a unix domain endpoint and create a tstream_context
736 * for the stream based communication.
738 * Use this function to connenct asynchronously to a unix domainendpoint and
739 * create a tstream_context for the stream based communication.
741 * The callback is triggered when a socket is connected and ready for IO or an
744 * @param[in] mem_ctx The talloc memory context to use.
746 * @param[in] ev The tevent_context to run on.
748 * @param[in] local An 'unix' tsocket_address for the local endpoint.
750 * @param[in] remote An 'unix' tsocket_address for the remote endpoint.
752 * @return A 'tevent_req' handle, where the caller can register a
753 * callback with tevent_req_set_callback(). NULL on a falal
756 * @see tstream_unix_connect_recv()
758 struct tevent_req * tstream_unix_connect_send(TALLOC_CTX *mem_ctx,
759 struct tevent_context *ev,
760 const struct tsocket_address *local,
761 const struct tsocket_address *remote);
765 * @brief Receive the result from a tstream_unix_connect_send().
767 * @param[in] req The tevent request from tstream_inet_tcp_connect_send().
769 * @param[out] perrno The error number, set if an error occurred.
771 * @param[in] mem_ctx The talloc memory context to use.
773 * @param[in] stream The tstream context to work on.
775 * @return 0 on success, -1 on error with perrno set.
777 int tstream_unix_connect_recv(struct tevent_req *req,
780 struct tstream_context **stream);
782 int _tstream_unix_connect_recv(struct tevent_req *req,
785 struct tstream_context **stream,
786 const char *location);
787 #define tstream_unix_connect_recv(req, perrno, mem_ctx, stream) \
788 _tstream_unix_connect_recv(req, perrno, mem_ctx, stream, \
794 * @brief Create two connected 'unix' tsocket_contexts for stream based
797 * @param[in] mem_ctx1 The talloc memory context to use for stream1.
799 * @param[in] stream1 The first stream to connect.
801 * @param[in] mem_ctx2 The talloc memory context to use for stream2.
803 * @param[in] stream2 The second stream to connect.
805 * @return 0 on success, -1 on error with errno set.
807 int tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
808 struct tstream_context **stream1,
809 TALLOC_CTX *mem_ctx2,
810 struct tstream_context **stream2);
812 int _tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
813 struct tstream_context **_stream1,
814 TALLOC_CTX *mem_ctx2,
815 struct tstream_context **_stream2,
816 const char *location);
818 #define tstream_unix_socketpair(mem_ctx1, stream1, mem_ctx2, stream2) \
819 _tstream_unix_socketpair(mem_ctx1, stream1, mem_ctx2, stream2, \
827 * @brief Convert a tsocket address to a bsd socket address.
829 * @param[in] mem_ctx The talloc memory context to use.
831 * @param[in] sa The sockaddr structure to convert.
833 * @param[in] sa_socklen The lenth of the sockaddr sturucte.
835 * @param[out] addr The tsocket pointer to allocate and fill.
837 * @return 0 on success, -1 on error with errno set.
839 int tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx,
842 struct tsocket_address **addr);
844 int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx,
847 struct tsocket_address **_addr,
848 const char *location);
850 #define tsocket_address_bsd_from_sockaddr(mem_ctx, sa, sa_socklen, _addr) \
851 _tsocket_address_bsd_from_sockaddr(mem_ctx, sa, sa_socklen, _addr, \
856 * @brief Fill a bsd sockaddr structure.
858 * @param[in] addr The tsocket address structure to use.
860 * @param[in] sa The bsd sockaddr structure to fill out.
862 * @param[in] sa_socklen The length of the bsd sockaddr structure to fill out.
864 * @return The actual size of the sockaddr structure, -1 on error
865 * with errno set. The size could differ from sa_socklen.
869 * struct sockaddr_storage ss;
871 * socklen = tsocket_address_bsd_sockaddr(taddr,
872 * (struct sockaddr *) &ss,
873 * sizeof(struct sockaddr_storage));
879 ssize_t tsocket_address_bsd_sockaddr(const struct tsocket_address *addr,
885 * @brief Wrap an existing file descriptors into the tstream abstraction.
887 * You can use this function to wrap an existing file descriptors into the
888 * tstream abstraction. After that you're not able to use this file descriptor
889 * for anything else. The file descriptor will be closed when the stream gets
890 * freed. If you still want to use the fd you have have to create a duplicate.
892 * @param[in] mem_ctx The talloc memory context to use.
894 * @param[in] fd The non blocking fd to use!
896 * @param[in] stream The filed tstream_context you allocated before.
898 * @return 0 on success, -1 on error with errno set.
900 * @warning You should read the tsocket_bsd.c code and unterstand it in order
903 int tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
905 struct tstream_context **stream);
907 int _tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
909 struct tstream_context **_stream,
910 const char *location);
911 #define tstream_bsd_existing_socket(mem_ctx, fd, stream) \
912 _tstream_bsd_existing_socket(mem_ctx, fd, stream, \
921 * @defgroup tsocket_helper Queue and PDU helpers
924 * In order to make the live easier for callers which want to implement a
925 * function to receive a full PDU with a single async function pair, there're
926 * some helper functions.
928 * There're some cases where the caller wants doesn't care about the order of
929 * doing IO on the abstracted sockets.
935 * @brief Queue a dgram blob for sending through the socket.
937 * This function queues a blob for sending to destination through an existing
938 * dgram socket. The async callback is triggered when the whole blob is
939 * delivered to the underlying system socket.
941 * The caller needs to make sure that all non-scalar input parameters hang
942 * around for the whole lifetime of the request.
944 * @param[in] mem_ctx The memory context for the result.
946 * @param[in] ev The event context the operation should work on.
948 * @param[in] dgram The tdgram_context to send the message buffer.
950 * @param[in] queue The existing dgram queue.
952 * @param[in] buf The message buffer to send.
954 * @param[in] len The message length.
956 * @param[in] dst The destination socket address.
958 * @return The async request handle. NULL on fatal error.
960 * @see tdgram_sendto_queue_recv()
962 struct tevent_req *tdgram_sendto_queue_send(TALLOC_CTX *mem_ctx,
963 struct tevent_context *ev,
964 struct tdgram_context *dgram,
965 struct tevent_queue *queue,
968 struct tsocket_address *dst);
971 * @brief Receive the result of the sent dgram blob.
973 * @param[in] req The tevent request from tdgram_sendto_queue_send().
975 * @param[out] perrno The error set to the actual errno.
977 * @return The length of the datagram (0 is never returned!), -1 on
978 * error with perrno set to the actual errno.
980 ssize_t tdgram_sendto_queue_recv(struct tevent_req *req, int *perrno);
982 typedef int (*tstream_readv_pdu_next_vector_t)(struct tstream_context *stream,
985 struct iovec **vector,
988 struct tevent_req *tstream_readv_pdu_send(TALLOC_CTX *mem_ctx,
989 struct tevent_context *ev,
990 struct tstream_context *stream,
991 tstream_readv_pdu_next_vector_t next_vector_fn,
992 void *next_vector_private);
993 int tstream_readv_pdu_recv(struct tevent_req *req, int *perrno);
996 * @brief Queue a read request for a PDU on the socket.
998 * This function queues a read request for a PDU on a stream socket. The async
999 * callback is triggered when a full PDU has been read from the socket.
1001 * The caller needs to make sure that all non-scalar input parameters hang
1002 * around for the whole lifetime of the request.
1004 * @param[in] mem_ctx The memory context for the result
1006 * @param[in] ev The tevent_context to run on
1008 * @param[in] stream The stream to send data through
1010 * @param[in] queue The existing send queue
1012 * @param[in] next_vector_fn The next vector function
1014 * @param[in] next_vector_private The private_data of the next vector function
1016 * @return The async request handle. NULL on fatal error.
1018 * @see tstream_readv_pdu_queue_recv()
1020 struct tevent_req *tstream_readv_pdu_queue_send(TALLOC_CTX *mem_ctx,
1021 struct tevent_context *ev,
1022 struct tstream_context *stream,
1023 struct tevent_queue *queue,
1024 tstream_readv_pdu_next_vector_t next_vector_fn,
1025 void *next_vector_private);
1028 * @brief Receive the PDU blob read from the stream.
1030 * @param[in] req The tevent request from tstream_readv_pdu_queue_send().
1032 * @param[out] perrno The error set to the actual errno.
1034 * @return The number of bytes read on success, -1 on error with
1035 * perrno set to the actual errno.
1037 int tstream_readv_pdu_queue_recv(struct tevent_req *req, int *perrno);
1040 * @brief Queue an iovector for sending through the socket
1042 * This function queues an iovector for sending to destination through an
1043 * existing stream socket. The async callback is triggered when the whole
1044 * vectror has been delivered to the underlying system socket.
1046 * The caller needs to make sure that all non-scalar input parameters hang
1047 * around for the whole lifetime of the request.
1049 * @param[in] mem_ctx The memory context for the result.
1051 * @param[in] ev The tevent_context to run on.
1053 * @param[in] stream The stream to send data through.
1055 * @param[in] queue The existing send queue.
1057 * @param[in] vector The iovec vector so write.
1059 * @param[in] count The size of the vector.
1061 * @return The async request handle. NULL on fatal error.
1063 struct tevent_req *tstream_writev_queue_send(TALLOC_CTX *mem_ctx,
1064 struct tevent_context *ev,
1065 struct tstream_context *stream,
1066 struct tevent_queue *queue,
1067 const struct iovec *vector,
1071 * @brief Receive the result of the sent iovector.
1073 * @param[in] req The tevent request from tstream_writev_queue_send().
1075 * @param[out] perrno The error set to the actual errno.
1077 * @return The length of the iovector (0 is never returned!), -1 on
1078 * error with perrno set to the actual errno.
1080 int tstream_writev_queue_recv(struct tevent_req *req, int *perrno);
1086 #endif /* _TSOCKET_H */