lib/tsocket: add tdgram_bsd_existing_socket() helper function
[Samba.git] / lib / tsocket / tsocket.h
blob296c7c555ae907b51fd7854eff9e9ed3b239989a
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 representation 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_is_inet()
105 * @see tsocket_address_inet_addr_string()
106 * @see tsocket_address_inet_port()
108 char *tsocket_address_string(const struct tsocket_address *addr,
109 TALLOC_CTX *mem_ctx);
111 #ifdef DOXYGEN
113 * @brief This creates a copy of a tsocket_address.
115 * This is useful when before doing modifications to a socket via additional
116 * methods of the specific tsocket_address implementation.
118 * @param[in] addr The address to create the copy from.
120 * @param[in] mem_ctx The talloc memory context to use.
122 * @return A newly allocated copy of addr (tsocket_address *), NULL
123 * on error.
125 struct tsocket_address *tsocket_address_copy(const struct tsocket_address *addr,
126 TALLOC_CTX *mem_ctx);
127 #else
128 struct tsocket_address *_tsocket_address_copy(const struct tsocket_address *addr,
129 TALLOC_CTX *mem_ctx,
130 const char *location);
132 #define tsocket_address_copy(addr, mem_ctx) \
133 _tsocket_address_copy(addr, mem_ctx, __location__)
134 #endif
137 * @}
141 * @defgroup tdgram_context The tdgram_context abstraction
142 * @ingroup tsocket
144 * The tdgram_context is like an abstract class for datagram based sockets. The
145 * interface provides async 'tevent_req' based functions on top functionality
146 * is similar to the recvfrom(2)/sendto(2)/close(2) syscalls.
148 * @note You can always use talloc_free(tdgram) to cleanup the resources
149 * of the tdgram_context on a fatal error.
150 * @{
154 * @brief Ask for next available datagram on the abstracted tdgram_context.
156 * It returns a 'tevent_req' handle, where the caller can register
157 * a callback with tevent_req_set_callback(). The callback is triggered
158 * when a datagram is available or an error happened.
160 * @param[in] mem_ctx The talloc memory context to use.
162 * @param[in] ev The tevent_context to run on.
164 * @param[in] dgram The dgram context to work on.
166 * @return Returns a 'tevent_req' handle, where the caller can
167 * register a callback with tevent_req_set_callback().
168 * NULL on fatal error.
170 * @see tdgram_inet_udp_socket()
171 * @see tdgram_unix_socket()
173 struct tevent_req *tdgram_recvfrom_send(TALLOC_CTX *mem_ctx,
174 struct tevent_context *ev,
175 struct tdgram_context *dgram);
178 * @brief Receive the next available datagram on the abstracted tdgram_context.
180 * This function should be called by the callback when a datagram is available
181 * or an error happened.
183 * The caller can only have one outstanding tdgram_recvfrom_send() at a time
184 * otherwise the caller will get '*perrno = EBUSY'.
186 * @param[in] req The tevent request from tdgram_recvfrom_send().
188 * @param[out] perrno The error number, set if an error occurred.
190 * @param[in] mem_ctx The memory context to use.
192 * @param[out] buf This will hold the buffer of the datagram.
194 * @param[out] src The abstracted tsocket_address of the sender of the
195 * received datagram.
197 * @return The length of the datagram (0 is never returned!),
198 * -1 on error with perrno set to the actual errno.
200 * @see tdgram_recvfrom_send()
202 ssize_t tdgram_recvfrom_recv(struct tevent_req *req,
203 int *perrno,
204 TALLOC_CTX *mem_ctx,
205 uint8_t **buf,
206 struct tsocket_address **src);
209 * @brief Send a datagram to a destination endpoint.
211 * The function can be called to send a datagram (specified by a buf/len) to a
212 * destination endpoint (specified by dst). It's not allowed for len to be 0.
214 * It returns a 'tevent_req' handle, where the caller can register a callback
215 * with tevent_req_set_callback(). The callback is triggered when the specific
216 * implementation (assumes it) has delivered the datagram to the "wire".
218 * The callback is then supposed to get the result by calling
219 * tdgram_sendto_recv() on the 'tevent_req'.
221 * @param[in] mem_ctx The talloc memory context to use.
223 * @param[in] ev The tevent_context to run on.
225 * @param[in] dgram The dgram context to work on.
227 * @param[in] buf The buffer to send.
229 * @param[in] len The length of the buffer to send. It has to be bigger
230 * than 0.
232 * @param[in] dst The destination to send the datagram to in form of a
233 * tsocket_address.
235 * @return Returns a 'tevent_req' handle, where the caller can
236 * register a callback with tevent_req_set_callback().
237 * NULL on fatal error.
239 * @see tdgram_inet_udp_socket()
240 * @see tdgram_unix_socket()
241 * @see tdgram_sendto_recv()
243 struct tevent_req *tdgram_sendto_send(TALLOC_CTX *mem_ctx,
244 struct tevent_context *ev,
245 struct tdgram_context *dgram,
246 const uint8_t *buf, size_t len,
247 const struct tsocket_address *dst);
250 * @brief Receive the result of the sent datagram.
252 * The caller can only have one outstanding tdgram_sendto_send() at a time
253 * otherwise the caller will get '*perrno = EBUSY'.
255 * @param[in] req The tevent request from tdgram_sendto_send().
257 * @param[out] perrno The error number, set if an error occurred.
259 * @return The length of the datagram (0 is never returned!), -1 on
260 * error with perrno set to the actual errno.
262 * @see tdgram_sendto_send()
264 ssize_t tdgram_sendto_recv(struct tevent_req *req,
265 int *perrno);
268 * @brief Shutdown/close an abstracted socket.
270 * It returns a 'tevent_req' handle, where the caller can register a callback
271 * with tevent_req_set_callback(). The callback is triggered when the specific
272 * implementation (assumes it) has delivered the datagram to the "wire".
274 * The callback is then supposed to get the result by calling
275 * tdgram_sendto_recv() on the 'tevent_req'.
277 * @param[in] mem_ctx The talloc memory context to use.
279 * @param[in] ev The tevent_context to run on.
281 * @param[in] dgram The dgram context diconnect from.
283 * @return Returns a 'tevent_req' handle, where the caller can
284 * register a callback with tevent_req_set_callback().
285 * NULL on fatal error.
287 * @see tdgram_disconnect_recv()
289 struct tevent_req *tdgram_disconnect_send(TALLOC_CTX *mem_ctx,
290 struct tevent_context *ev,
291 struct tdgram_context *dgram);
294 * @brief Receive the result from a tdgram_disconnect_send() request.
296 * The caller should make sure there're no outstanding tdgram_recvfrom_send()
297 * and tdgram_sendto_send() calls otherwise the caller will get
298 * '*perrno = EBUSY'.
300 * @param[in] req The tevent request from tdgram_disconnect_send().
302 * @param[out] perrno The error number, set if an error occurred.
304 * @return The length of the datagram (0 is never returned!), -1 on
305 * error with perrno set to the actual errno.
307 * @see tdgram_disconnect_send()
309 int tdgram_disconnect_recv(struct tevent_req *req,
310 int *perrno);
313 * @}
317 * @defgroup tstream_context The tstream_context abstraction
318 * @ingroup tsocket
320 * The tstream_context is like an abstract class for stream based sockets. The
321 * interface provides async 'tevent_req' based functions on top functionality
322 * is similar to the readv(2)/writev(2)/close(2) syscalls.
324 * @note You can always use talloc_free(tstream) to cleanup the resources
325 * of the tstream_context on a fatal error.
327 * @{
331 * @brief Report the number of bytes received but not consumed yet.
333 * The tstream_pending_bytes() function reports how much bytes of the incoming
334 * stream have been received but not consumed yet.
336 * @param[in] stream The tstream_context to check for pending bytes.
338 * @return The number of bytes received, -1 on error with errno
339 * set.
341 ssize_t tstream_pending_bytes(struct tstream_context *stream);
344 * @brief Read a specific amount of bytes from a stream socket.
346 * The function can be called to read for a specific amount of bytes from the
347 * stream into given buffers. The caller has to preallocate the buffers.
349 * The caller might need to use tstream_pending_bytes() if the protocol doesn't
350 * have a fixed pdu header containing the pdu size.
352 * @param[in] mem_ctx The talloc memory context to use.
354 * @param[in] ev The tevent_context to run on.
356 * @param[in] stream The tstream context to work on.
358 * @param[out] vector A preallocated iovec to store the data to read.
360 * @param[in] count The number of buffers in the vector allocated.
362 * @return A 'tevent_req' handle, where the caller can register
363 * a callback with tevent_req_set_callback(). NULL on
364 * fatal error.
366 * @see tstream_unix_connect_send()
367 * @see tstream_inet_tcp_connect_send()
369 struct tevent_req *tstream_readv_send(TALLOC_CTX *mem_ctx,
370 struct tevent_context *ev,
371 struct tstream_context *stream,
372 struct iovec *vector,
373 size_t count);
376 * @brief Get the result of a tstream_readv_send().
378 * The caller can only have one outstanding tstream_readv_send()
379 * at a time otherwise the caller will get *perrno = EBUSY.
381 * @param[in] req The tevent request from tstream_readv_send().
383 * @param[out] perrno The error number, set if an error occurred.
385 * @return The length of the stream (0 is never returned!), -1 on
386 * error with perrno set to the actual errno.
388 int tstream_readv_recv(struct tevent_req *req,
389 int *perrno);
392 * @brief Write buffers from a vector into a stream socket.
394 * The function can be called to write buffers from a given vector
395 * to a stream socket.
397 * You have to ensure that the vector is not empty.
399 * @param[in] mem_ctx The talloc memory context to use.
401 * @param[in] ev The tevent_context to run on.
403 * @param[in] stream The tstream context to work on.
405 * @param[in] vector The iovec vector with data to write on a stream socket.
407 * @param[in] count The number of buffers in the vector to write.
409 * @return A 'tevent_req' handle, where the caller can register
410 * a callback with tevent_req_set_callback(). NULL on
411 * fatal error.
413 struct tevent_req *tstream_writev_send(TALLOC_CTX *mem_ctx,
414 struct tevent_context *ev,
415 struct tstream_context *stream,
416 const struct iovec *vector,
417 size_t count);
420 * @brief Get the result of a tstream_writev_send().
422 * The caller can only have one outstanding tstream_writev_send()
423 * at a time otherwise the caller will get *perrno = EBUSY.
425 * @param[in] req The tevent request from tstream_writev_send().
427 * @param[out] perrno The error number, set if an error occurred.
429 * @return The length of the stream (0 is never returned!), -1 on
430 * error with perrno set to the actual errno.
432 int tstream_writev_recv(struct tevent_req *req,
433 int *perrno);
436 * @brief Shutdown/close an abstracted socket.
438 * It returns a 'tevent_req' handle, where the caller can register a callback
439 * with tevent_req_set_callback(). The callback is triggered when the specific
440 * implementation (assumes it) has delivered the stream to the "wire".
442 * The callback is then supposed to get the result by calling
443 * tdgram_sendto_recv() on the 'tevent_req'.
445 * @param[in] mem_ctx The talloc memory context to use.
447 * @param[in] ev The tevent_context to run on.
449 * @param[in] stream The tstream context to work on.
451 * @return A 'tevent_req' handle, where the caller can register
452 * a callback with tevent_req_set_callback(). NULL on
453 * fatal error.
455 struct tevent_req *tstream_disconnect_send(TALLOC_CTX *mem_ctx,
456 struct tevent_context *ev,
457 struct tstream_context *stream);
460 * @brief Get the result of a tstream_disconnect_send().
462 * The caller can only have one outstanding tstream_writev_send()
463 * at a time otherwise the caller will get *perrno = EBUSY.
465 * @param[in] req The tevent request from tstream_disconnect_send().
467 * @param[out] perrno The error number, set if an error occurred.
469 * @return The length of the stream (0 is never returned!), -1 on
470 * error with perrno set to the actual errno.
472 int tstream_disconnect_recv(struct tevent_req *req,
473 int *perrno);
476 * @}
481 * @defgroup tsocket_bsd tsocket_bsd - inet, inet6 and unix
482 * @ingroup tsocket
484 * The main tsocket library comes with implentations for BSD style ipv4, ipv6
485 * and unix sockets.
487 * @{
491 * @brief Find out if the tsocket_address represents an ipv4 or ipv6 endpoint.
493 * @param[in] addr The tsocket_address pointer
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 * @return true if addr represents an address of the given family,
500 * otherwise false.
502 bool tsocket_address_is_inet(const struct tsocket_address *addr, const char *fam);
504 #if DOXYGEN
506 * @brief Create a tsocket_address for ipv4 and ipv6 endpoint addresses.
508 * @param[in] mem_ctx The talloc memory context to use.
510 * @param[in] fam The family can be can be "ipv4", "ipv6" or "ip". With
511 * "ip" is autodetects "ipv4" or "ipv6" based on the
512 * addr.
514 * @param[in] addr A valid ip address string based on the selected family
515 * (dns names are not allowed!). It's valid to pass NULL,
516 * which gets mapped to "0.0.0.0" or "::".
518 * @param[in] port A valid port number.
520 * @param[out] _addr A tsocket_address pointer to store the information.
522 * @return 0 on success, -1 on error with errno set.
524 int tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
525 const char *fam,
526 const char *addr,
527 uint16_t port,
528 struct tsocket_address **_addr);
529 #else
530 int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
531 const char *fam,
532 const char *addr,
533 uint16_t port,
534 struct tsocket_address **_addr,
535 const char *location);
537 #define tsocket_address_inet_from_strings(mem_ctx, fam, addr, port, _addr) \
538 _tsocket_address_inet_from_strings(mem_ctx, fam, addr, port, _addr, \
539 __location__)
540 #endif
543 * @brief Get the address of an 'inet' tsocket_address as a string.
545 * @param[in] addr The address to convert to a string.
547 * @param[in] mem_ctx The talloc memory context to use.
549 * @return A newly allocated string of the address, NULL on error
550 * with errno set.
552 * @see tsocket_address_is_inet()
554 char *tsocket_address_inet_addr_string(const struct tsocket_address *addr,
555 TALLOC_CTX *mem_ctx);
558 * @brief Get the port number as an integer from an 'inet' tsocket_address.
560 * @param[in] addr The tsocket address to use.
562 * @return The port number, 0 on error with errno set.
564 uint16_t tsocket_address_inet_port(const struct tsocket_address *addr);
567 * @brief Set the port number of an existing 'inet' tsocket_address.
569 * @param[in] addr The existing tsocket_address to use.
571 * @param[in] port The valid port number to set.
573 * @return 0 on success, -1 on error with errno set.
575 int tsocket_address_inet_set_port(struct tsocket_address *addr,
576 uint16_t port);
579 * @brief Find out if the tsocket_address represents an unix domain endpoint.
581 * @param[in] addr The tsocket_address pointer
583 * @return true if addr represents an unix domain endpoint,
584 * otherwise false.
586 bool tsocket_address_is_unix(const struct tsocket_address *addr);
588 #ifdef DOXYGEN
590 * @brief Create a tsocket_address for a unix domain endpoint addresses.
592 * @param[in] mem_ctx The talloc memory context to use.
594 * @param[in] path The filesystem path, NULL will map "".
596 * @param[in] _addr The tsocket_address pointer to store the information.
598 * @return 0 on success, -1 on error with errno set.
600 * @see tsocket_address_is_unix()
602 int tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
603 const char *path,
604 struct tsocket_address **_addr);
605 #else
606 int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
607 const char *path,
608 struct tsocket_address **_addr,
609 const char *location);
611 #define tsocket_address_unix_from_path(mem_ctx, path, _addr) \
612 _tsocket_address_unix_from_path(mem_ctx, path, _addr, \
613 __location__)
614 #endif
617 * @brief Get the address of an 'unix' tsocket_address.
619 * @param[in] addr A valid 'unix' tsocket_address.
621 * @param[in] mem_ctx The talloc memory context to use.
623 * @return The path of the unix domain socket, NULL on error or if
624 * the tsocket_address doesn't represent an unix domain
625 * endpoint path.
627 char *tsocket_address_unix_path(const struct tsocket_address *addr,
628 TALLOC_CTX *mem_ctx);
630 #ifdef DOXYGEN
632 * @brief Wrap an existing file descriptors into the tdgram abstraction.
634 * You can use this function to wrap an existing file descriptors into the
635 * tdgram abstraction. After that you're not able to use this file descriptor
636 * for anything else. The file descriptor will be closed when the stream gets
637 * freed. If you still want to use the fd you have have to create a duplicate.
639 * @param[in] mem_ctx The talloc memory context to use.
641 * @param[in] fd The non blocking fd to use!
643 * @param[out] dgram A pointer to store an allocated tdgram_context.
645 * @return 0 on success, -1 on error.
647 * Example:
648 * @code
649 * fd2 = dup(fd);
650 * rc = tdgram_bsd_existing_socket(mem_ctx, fd2, &tdgram);
651 * if (rc < 0) {
652 * return;
654 * @endcode
656 * @warning This is an internal function. You should read the code to fully
657 * understand it if you plan to use it.
659 int tdgram_bsd_existing_socket(TALLOC_CTX *mem_ctx,
660 int fd,
661 struct tdgram_context **dgram);
662 #else
663 int _tdgram_bsd_existing_socket(TALLOC_CTX *mem_ctx,
664 int fd,
665 struct tdgram_context **_dgram,
666 const char *location);
667 #define tdgram_bsd_existing_socket(mem_ctx, fd, dgram) \
668 _tdgram_bsd_existing_socket(mem_ctx, fd, dgram, \
669 __location__)
670 #endif
673 * @brief Request a syscall optimization for tdgram_recvfrom_send()
675 * This function is only used to reduce the amount of syscalls and
676 * optimize performance. You should only use this if you know
677 * what you're doing.
679 * The optimization is off by default.
681 * @param[in] dgram The tdgram_context of a bsd socket, if this
682 * not a bsd socket the function does nothing.
684 * @param[in] on The boolean value to turn the optimization on and off.
686 * @return The old boolean value.
688 * @see tdgram_recvfrom_send()
690 bool tdgram_bsd_optimize_recvfrom(struct tdgram_context *dgram,
691 bool on);
693 #ifdef DOXYGEN
695 * @brief Create a tdgram_context for a ipv4 or ipv6 UDP communication.
697 * @param[in] local An 'inet' tsocket_address for the local endpoint.
699 * @param[in] remote An 'inet' tsocket_address for the remote endpoint or
700 * NULL (??? to create a listener?).
702 * @param[in] mem_ctx The talloc memory context to use.
704 * @param[in] dgram The tdgram_context pointer to setup the udp
705 * communication. The function will allocate the memory.
707 * @return 0 on success, -1 on error with errno set.
709 int tdgram_inet_udp_socket(const struct tsocket_address *local,
710 const struct tsocket_address *remote,
711 TALLOC_CTX *mem_ctx,
712 struct tdgram_context **dgram);
713 #else
714 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
715 const struct tsocket_address *remote,
716 TALLOC_CTX *mem_ctx,
717 struct tdgram_context **dgram,
718 const char *location);
719 #define tdgram_inet_udp_socket(local, remote, mem_ctx, dgram) \
720 _tdgram_inet_udp_socket(local, remote, mem_ctx, dgram, __location__)
721 #endif
723 #ifdef DOXYGEN
725 * @brief Create a tdgram_context for unix domain datagram communication.
727 * @param[in] local An 'unix' tsocket_address for the local endpoint.
729 * @param[in] remote An 'unix' tsocket_address for the remote endpoint or
730 * NULL (??? to create a listener?).
732 * @param[in] mem_ctx The talloc memory context to use.
734 * @param[in] dgram The tdgram_context pointer to setup the udp
735 * communication. The function will allocate the memory.
737 * @return 0 on success, -1 on error with errno set.
739 int tdgram_unix_socket(const struct tsocket_address *local,
740 const struct tsocket_address *remote,
741 TALLOC_CTX *mem_ctx,
742 struct tdgram_context **dgram);
743 #else
744 int _tdgram_unix_socket(const struct tsocket_address *local,
745 const struct tsocket_address *remote,
746 TALLOC_CTX *mem_ctx,
747 struct tdgram_context **dgram,
748 const char *location);
750 #define tdgram_unix_socket(local, remote, mem_ctx, dgram) \
751 _tdgram_unix_socket(local, remote, mem_ctx, dgram, __location__)
752 #endif
755 * @brief Request a syscall optimization for tstream_readv_send()
757 * This function is only used to reduce the amount of syscalls and
758 * optimize performance. You should only use this if you know
759 * what you're doing.
761 * The optimization is off by default.
763 * @param[in] stream The tstream_context of a bsd socket, if this
764 * not a bsd socket the function does nothing.
766 * @param[in] on The boolean value to turn the optimization on and off.
768 * @return The old boolean value.
770 * @see tstream_readv_send()
772 bool tstream_bsd_optimize_readv(struct tstream_context *stream,
773 bool on);
776 * @brief Connect async to a TCP endpoint and create a tstream_context for the
777 * stream based communication.
779 * Use this function to connenct asynchronously to a remote ipv4 or ipv6 TCP
780 * endpoint and create a tstream_context for the stream based communication.
782 * @param[in] mem_ctx The talloc memory context to use.
784 * @param[in] ev The tevent_context to run on.
786 * @param[in] local An 'inet' tsocket_address for the local endpoint.
788 * @param[in] remote An 'inet' tsocket_address for the remote endpoint.
790 * @return A 'tevent_req' handle, where the caller can register a
791 * callback with tevent_req_set_callback(). NULL on a fatal
792 * error.
794 * @see tstream_inet_tcp_connect_recv()
796 struct tevent_req *tstream_inet_tcp_connect_send(TALLOC_CTX *mem_ctx,
797 struct tevent_context *ev,
798 const struct tsocket_address *local,
799 const struct tsocket_address *remote);
801 #ifdef DOXYGEN
803 * @brief Receive the result from a tstream_inet_tcp_connect_send().
805 * @param[in] req The tevent request from tstream_inet_tcp_connect_send().
807 * @param[out] perrno The error number, set if an error occurred.
809 * @param[in] mem_ctx The talloc memory context to use.
811 * @param[out] stream A tstream_context pointer to setup the tcp communication
812 * on. This function will allocate the memory.
814 * @param[out] local The real 'inet' tsocket_address of the local endpoint.
815 * This parameter is optional and can be NULL.
817 * @return 0 on success, -1 on error with perrno set.
819 int tstream_inet_tcp_connect_recv(struct tevent_req *req,
820 int *perrno,
821 TALLOC_CTX *mem_ctx,
822 struct tstream_context **stream,
823 struct tsocket_address **local)
824 #else
825 int _tstream_inet_tcp_connect_recv(struct tevent_req *req,
826 int *perrno,
827 TALLOC_CTX *mem_ctx,
828 struct tstream_context **stream,
829 struct tsocket_address **local,
830 const char *location);
831 #define tstream_inet_tcp_connect_recv(req, perrno, mem_ctx, stream, local) \
832 _tstream_inet_tcp_connect_recv(req, perrno, mem_ctx, stream, local, \
833 __location__)
834 #endif
837 * @brief Connect async to a unix domain endpoint and create a tstream_context
838 * for the stream based communication.
840 * Use this function to connenct asynchronously to a unix domainendpoint and
841 * create a tstream_context for the stream based communication.
843 * The callback is triggered when a socket is connected and ready for IO or an
844 * error happened.
846 * @param[in] mem_ctx The talloc memory context to use.
848 * @param[in] ev The tevent_context to run on.
850 * @param[in] local An 'unix' tsocket_address for the local endpoint.
852 * @param[in] remote An 'unix' tsocket_address for the remote endpoint.
854 * @return A 'tevent_req' handle, where the caller can register a
855 * callback with tevent_req_set_callback(). NULL on a falal
856 * error.
858 * @see tstream_unix_connect_recv()
860 struct tevent_req * tstream_unix_connect_send(TALLOC_CTX *mem_ctx,
861 struct tevent_context *ev,
862 const struct tsocket_address *local,
863 const struct tsocket_address *remote);
865 #ifdef DOXYGEN
867 * @brief Receive the result from a tstream_unix_connect_send().
869 * @param[in] req The tevent request from tstream_inet_tcp_connect_send().
871 * @param[out] perrno The error number, set if an error occurred.
873 * @param[in] mem_ctx The talloc memory context to use.
875 * @param[in] stream The tstream context to work on.
877 * @return 0 on success, -1 on error with perrno set.
879 int tstream_unix_connect_recv(struct tevent_req *req,
880 int *perrno,
881 TALLOC_CTX *mem_ctx,
882 struct tstream_context **stream);
883 #else
884 int _tstream_unix_connect_recv(struct tevent_req *req,
885 int *perrno,
886 TALLOC_CTX *mem_ctx,
887 struct tstream_context **stream,
888 const char *location);
889 #define tstream_unix_connect_recv(req, perrno, mem_ctx, stream) \
890 _tstream_unix_connect_recv(req, perrno, mem_ctx, stream, \
891 __location__)
892 #endif
894 #ifdef DOXYGEN
896 * @brief Create two connected 'unix' tsocket_contexts for stream based
897 * communication.
899 * @param[in] mem_ctx1 The talloc memory context to use for stream1.
901 * @param[in] stream1 The first stream to connect.
903 * @param[in] mem_ctx2 The talloc memory context to use for stream2.
905 * @param[in] stream2 The second stream to connect.
907 * @return 0 on success, -1 on error with errno set.
909 int tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
910 struct tstream_context **stream1,
911 TALLOC_CTX *mem_ctx2,
912 struct tstream_context **stream2);
913 #else
914 int _tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
915 struct tstream_context **_stream1,
916 TALLOC_CTX *mem_ctx2,
917 struct tstream_context **_stream2,
918 const char *location);
920 #define tstream_unix_socketpair(mem_ctx1, stream1, mem_ctx2, stream2) \
921 _tstream_unix_socketpair(mem_ctx1, stream1, mem_ctx2, stream2, \
922 __location__)
923 #endif
925 struct sockaddr;
927 #ifdef DOXYGEN
929 * @brief Convert a tsocket address to a bsd socket address.
931 * @param[in] mem_ctx The talloc memory context to use.
933 * @param[in] sa The sockaddr structure to convert.
935 * @param[in] sa_socklen The lenth of the sockaddr sturucte.
937 * @param[out] addr The tsocket pointer to allocate and fill.
939 * @return 0 on success, -1 on error with errno set.
941 int tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx,
942 const struct sockaddr *sa,
943 size_t sa_socklen,
944 struct tsocket_address **addr);
945 #else
946 int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx,
947 const struct sockaddr *sa,
948 size_t sa_socklen,
949 struct tsocket_address **_addr,
950 const char *location);
952 #define tsocket_address_bsd_from_sockaddr(mem_ctx, sa, sa_socklen, _addr) \
953 _tsocket_address_bsd_from_sockaddr(mem_ctx, sa, sa_socklen, _addr, \
954 __location__)
955 #endif
958 * @brief Fill a bsd sockaddr structure.
960 * @param[in] addr The tsocket address structure to use.
962 * @param[in] sa The bsd sockaddr structure to fill out.
964 * @param[in] sa_socklen The length of the bsd sockaddr structure to fill out.
966 * @return The actual size of the sockaddr structure, -1 on error
967 * with errno set. The size could differ from sa_socklen.
969 * @code
970 * ssize_t socklen;
971 * struct sockaddr_storage ss;
973 * socklen = tsocket_address_bsd_sockaddr(taddr,
974 * (struct sockaddr *) &ss,
975 * sizeof(struct sockaddr_storage));
976 * if (socklen < 0) {
977 * return -1;
979 * @endcode
981 ssize_t tsocket_address_bsd_sockaddr(const struct tsocket_address *addr,
982 struct sockaddr *sa,
983 size_t sa_socklen);
985 #ifdef DOXYGEN
987 * @brief Wrap an existing file descriptors into the tstream abstraction.
989 * You can use this function to wrap an existing file descriptors into the
990 * tstream abstraction. After that you're not able to use this file descriptor
991 * for anything else. The file descriptor will be closed when the stream gets
992 * freed. If you still want to use the fd you have have to create a duplicate.
994 * @param[in] mem_ctx The talloc memory context to use.
996 * @param[in] fd The non blocking fd to use!
998 * @param[out] stream A pointer to store an allocated tstream_context.
1000 * @return 0 on success, -1 on error.
1002 * Example:
1003 * @code
1004 * fd2 = dup(fd);
1005 * rc = tstream_bsd_existing_socket(mem_ctx, fd2, &tstream);
1006 * if (rc < 0) {
1007 * stream_terminate_connection(conn, "named_pipe_accept: out of memory");
1008 * return;
1010 * @endcode
1012 * @warning This is an internal function. You should read the code to fully
1013 * understand it if you plan to use it.
1015 int tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
1016 int fd,
1017 struct tstream_context **stream);
1018 #else
1019 int _tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
1020 int fd,
1021 struct tstream_context **_stream,
1022 const char *location);
1023 #define tstream_bsd_existing_socket(mem_ctx, fd, stream) \
1024 _tstream_bsd_existing_socket(mem_ctx, fd, stream, \
1025 __location__)
1026 #endif
1029 * @}
1033 * @defgroup tsocket_helper Queue and PDU helpers
1034 * @ingroup tsocket
1036 * In order to make the live easier for callers which want to implement a
1037 * function to receive a full PDU with a single async function pair, there're
1038 * some helper functions.
1040 * There're some cases where the caller wants doesn't care about the order of
1041 * doing IO on the abstracted sockets.
1043 * @{
1047 * @brief Queue a dgram blob for sending through the socket.
1049 * This function queues a blob for sending to destination through an existing
1050 * dgram socket. The async callback is triggered when the whole blob is
1051 * delivered to the underlying system socket.
1053 * The caller needs to make sure that all non-scalar input parameters hang
1054 * around for the whole lifetime of the request.
1056 * @param[in] mem_ctx The memory context for the result.
1058 * @param[in] ev The event context the operation should work on.
1060 * @param[in] dgram The tdgram_context to send the message buffer.
1062 * @param[in] queue The existing dgram queue.
1064 * @param[in] buf The message buffer to send.
1066 * @param[in] len The message length.
1068 * @param[in] dst The destination socket address.
1070 * @return The async request handle. NULL on fatal error.
1072 * @see tdgram_sendto_queue_recv()
1074 struct tevent_req *tdgram_sendto_queue_send(TALLOC_CTX *mem_ctx,
1075 struct tevent_context *ev,
1076 struct tdgram_context *dgram,
1077 struct tevent_queue *queue,
1078 const uint8_t *buf,
1079 size_t len,
1080 struct tsocket_address *dst);
1083 * @brief Receive the result of the sent dgram blob.
1085 * @param[in] req The tevent request from tdgram_sendto_queue_send().
1087 * @param[out] perrno The error set to the actual errno.
1089 * @return The length of the datagram (0 is never returned!), -1 on
1090 * error with perrno set to the actual errno.
1092 ssize_t tdgram_sendto_queue_recv(struct tevent_req *req, int *perrno);
1094 typedef int (*tstream_readv_pdu_next_vector_t)(struct tstream_context *stream,
1095 void *private_data,
1096 TALLOC_CTX *mem_ctx,
1097 struct iovec **vector,
1098 size_t *count);
1100 struct tevent_req *tstream_readv_pdu_send(TALLOC_CTX *mem_ctx,
1101 struct tevent_context *ev,
1102 struct tstream_context *stream,
1103 tstream_readv_pdu_next_vector_t next_vector_fn,
1104 void *next_vector_private);
1105 int tstream_readv_pdu_recv(struct tevent_req *req, int *perrno);
1108 * @brief Queue a read request for a PDU on the socket.
1110 * This function queues a read request for a PDU on a stream socket. The async
1111 * callback is triggered when a full PDU has been read from the socket.
1113 * The caller needs to make sure that all non-scalar input parameters hang
1114 * around for the whole lifetime of the request.
1116 * @param[in] mem_ctx The memory context for the result
1118 * @param[in] ev The tevent_context to run on
1120 * @param[in] stream The stream to send data through
1122 * @param[in] queue The existing send queue
1124 * @param[in] next_vector_fn The next vector function
1126 * @param[in] next_vector_private The private_data of the next vector function
1128 * @return The async request handle. NULL on fatal error.
1130 * @see tstream_readv_pdu_queue_recv()
1132 struct tevent_req *tstream_readv_pdu_queue_send(TALLOC_CTX *mem_ctx,
1133 struct tevent_context *ev,
1134 struct tstream_context *stream,
1135 struct tevent_queue *queue,
1136 tstream_readv_pdu_next_vector_t next_vector_fn,
1137 void *next_vector_private);
1140 * @brief Receive the PDU blob read from the stream.
1142 * @param[in] req The tevent request from tstream_readv_pdu_queue_send().
1144 * @param[out] perrno The error set to the actual errno.
1146 * @return The number of bytes read on success, -1 on error with
1147 * perrno set to the actual errno.
1149 int tstream_readv_pdu_queue_recv(struct tevent_req *req, int *perrno);
1152 * @brief Queue an iovector for sending through the socket
1154 * This function queues an iovector for sending to destination through an
1155 * existing stream socket. The async callback is triggered when the whole
1156 * vectror has been delivered to the underlying system socket.
1158 * The caller needs to make sure that all non-scalar input parameters hang
1159 * around for the whole lifetime of the request.
1161 * @param[in] mem_ctx The memory context for the result.
1163 * @param[in] ev The tevent_context to run on.
1165 * @param[in] stream The stream to send data through.
1167 * @param[in] queue The existing send queue.
1169 * @param[in] vector The iovec vector so write.
1171 * @param[in] count The size of the vector.
1173 * @return The async request handle. NULL on fatal error.
1175 struct tevent_req *tstream_writev_queue_send(TALLOC_CTX *mem_ctx,
1176 struct tevent_context *ev,
1177 struct tstream_context *stream,
1178 struct tevent_queue *queue,
1179 const struct iovec *vector,
1180 size_t count);
1183 * @brief Receive the result of the sent iovector.
1185 * @param[in] req The tevent request from tstream_writev_queue_send().
1187 * @param[out] perrno The error set to the actual errno.
1189 * @return The length of the iovector (0 is never returned!), -1 on
1190 * error with perrno set to the actual errno.
1192 int tstream_writev_queue_recv(struct tevent_req *req, int *perrno);
1195 * @}
1198 #endif /* _TSOCKET_H */