From 3a8b7b0518b33b016d2dbb8dd23d35ef1c6aaa5c Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Thu, 21 May 2015 11:37:06 +0200 Subject: [PATCH] lib/tsocket: add tdgram_bsd_existing_socket() helper function This is similar to tstream_bsd_existing_socket(). Both help to migrate strange code path to using the tstream or tdgram abstractions. BUG: https://bugzilla.samba.org/show_bug.cgi?id=11316 Signed-off-by: Stefan Metzmacher Reviewed-by: Volker Lendecke Reviewed-by: Jeremy Allison --- lib/tsocket/tsocket.h | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/tsocket/tsocket_bsd.c | 24 ++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/lib/tsocket/tsocket.h b/lib/tsocket/tsocket.h index 6b0eef6d799..296c7c555ae 100644 --- a/lib/tsocket/tsocket.h +++ b/lib/tsocket/tsocket.h @@ -627,6 +627,48 @@ int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx, char *tsocket_address_unix_path(const struct tsocket_address *addr, TALLOC_CTX *mem_ctx); +#ifdef DOXYGEN +/** + * @brief Wrap an existing file descriptors into the tdgram abstraction. + * + * You can use this function to wrap an existing file descriptors into the + * tdgram abstraction. After that you're not able to use this file descriptor + * for anything else. The file descriptor will be closed when the stream gets + * freed. If you still want to use the fd you have have to create a duplicate. + * + * @param[in] mem_ctx The talloc memory context to use. + * + * @param[in] fd The non blocking fd to use! + * + * @param[out] dgram A pointer to store an allocated tdgram_context. + * + * @return 0 on success, -1 on error. + * + * Example: + * @code + * fd2 = dup(fd); + * rc = tdgram_bsd_existing_socket(mem_ctx, fd2, &tdgram); + * if (rc < 0) { + * return; + * } + * @endcode + * + * @warning This is an internal function. You should read the code to fully + * understand it if you plan to use it. + */ +int tdgram_bsd_existing_socket(TALLOC_CTX *mem_ctx, + int fd, + struct tdgram_context **dgram); +#else +int _tdgram_bsd_existing_socket(TALLOC_CTX *mem_ctx, + int fd, + struct tdgram_context **_dgram, + const char *location); +#define tdgram_bsd_existing_socket(mem_ctx, fd, dgram) \ + _tdgram_bsd_existing_socket(mem_ctx, fd, dgram, \ + __location__) +#endif + /** * @brief Request a syscall optimization for tdgram_recvfrom_send() * diff --git a/lib/tsocket/tsocket_bsd.c b/lib/tsocket/tsocket_bsd.c index 5d8f80cc223..9ddbc061b94 100644 --- a/lib/tsocket/tsocket_bsd.c +++ b/lib/tsocket/tsocket_bsd.c @@ -1388,6 +1388,30 @@ static int tdgram_bsd_dgram_socket(const struct tsocket_address *local, return 0; } +int _tdgram_bsd_existing_socket(TALLOC_CTX *mem_ctx, + int fd, + struct tdgram_context **_dgram, + const char *location) +{ + struct tdgram_context *dgram; + struct tdgram_bsd *bsds; + + dgram = tdgram_context_create(mem_ctx, + &tdgram_bsd_ops, + &bsds, + struct tdgram_bsd, + location); + if (!dgram) { + return -1; + } + ZERO_STRUCTP(bsds); + bsds->fd = fd; + talloc_set_destructor(bsds, tdgram_bsd_destructor); + + *_dgram = dgram; + return 0; +} + int _tdgram_inet_udp_socket(const struct tsocket_address *local, const struct tsocket_address *remote, TALLOC_CTX *mem_ctx, -- 2.11.4.GIT