<p>Kevin Harwell has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/17472">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">tcptls.c: refactor client connection to be more robust<br><br>The current TCP client connect code, blocks and does not handle EINTR<br>error case. As well the underlying file descriptor is set to not poll<br>on reads, which can also cause inadvertent errors.<br><br>This patch makes the client socket non-blocking, ensures a connect<br>does not immediately fail due to EINTR "errors", adds a connect<br>timeout option, and sets the underyling iostream to poll on read.<br><br>The original client start call sets the new timeout option to<br>"infinite", thus making sure old, orginal behavior is retained.<br><br>ASTERISK-29746 #close<br><br>Change-Id: I907571843a83e43c0742b95a64785f4411f02671<br>---<br>M include/asterisk/tcptls.h<br>M main/tcptls.c<br>2 files changed, 101 insertions(+), 16 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/72/17472/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/tcptls.h b/include/asterisk/tcptls.h</span><br><span>index 0b943ce..576bdb0 100644</span><br><span>--- a/include/asterisk/tcptls.h</span><br><span>+++ b/include/asterisk/tcptls.h</span><br><span>@@ -163,8 +163,30 @@</span><br><span> };</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(0, 100%, 40%);">- * \brief attempts to connect and start tcptls session, on error the tcptls_session's</span><br><span style="color: hsl(0, 100%, 40%);">- * ref count is decremented, fd and file are closed, and NULL is returned.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Attempt to connect, and start a tcptls session within the given timeout</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Note, on error the tcptls_session's ref count is decremented,* fd and file</span><br><span style="color: hsl(120, 100%, 40%);">+ * are closed, and NULL is returned.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param tcptls_session The session instance to connect and start</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param timeout How long (in milliseconds) to attempt to connect (-1 equals infinite)</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return The tcptls_session, or NULL on error</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_tcptls_session_instance *ast_tcptls_client_start_timeout(</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_tcptls_session_instance *tcptls_session, int timeout);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Attempt to connect, and start a tcptls session</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Blocks until a connection is established, or an error occurs.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Note, on error the tcptls_session's ref count is decremented,* fd and file</span><br><span style="color: hsl(120, 100%, 40%);">+ * are closed, and NULL is returned.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param tcptls_session The session instance to connect and start</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return The tcptls_session, or NULL on error</span><br><span> */</span><br><span> struct ast_tcptls_session_instance *ast_tcptls_client_start(struct ast_tcptls_session_instance *tcptls_session);</span><br><span> </span><br><span>diff --git a/main/tcptls.c b/main/tcptls.c</span><br><span>index 45eb29b..8d85a29 100644</span><br><span>--- a/main/tcptls.c</span><br><span>+++ b/main/tcptls.c</span><br><span>@@ -582,23 +582,83 @@</span><br><span> #endif</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-struct ast_tcptls_session_instance *ast_tcptls_client_start(struct ast_tcptls_session_instance *tcptls_session)</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \internal</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Connect a socket</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Attempt to connect to a given address for up to 'timeout' milliseconds. A negative</span><br><span style="color: hsl(120, 100%, 40%);">+ * timeout value equates to an infinite wait time.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * A -1 is returned on error, and an appropriate errno value is set based on the</span><br><span style="color: hsl(120, 100%, 40%);">+ * type of error.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param sockfd The socket file descriptor</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param addr The address to connect to</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param timeout How long, in milliseconds, to attempt to connect</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return 0 if successfully connected, -1 otherwise</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static int socket_connect(int sockfd, const struct ast_sockaddr *addr, int timeout)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ int optval = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ socklen_t optlen = sizeof(int);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ errno = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_connect(sockfd, addr)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ int res;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /*</span><br><span style="color: hsl(120, 100%, 40%);">+ * A connect failure could mean things are still in progress.</span><br><span style="color: hsl(120, 100%, 40%);">+ * If so wait for it to complete.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (errno != EINPROGRESS) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((res = ast_wait_for_output(sockfd, timeout)) != 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (res == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ errno = ETIMEDOUT;</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (errno != EINTR) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Check the status to ensure it actually connected successfully */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, &optlen) < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (optval) {</span><br><span style="color: hsl(120, 100%, 40%);">+ errno = optval;</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_tcptls_session_instance *ast_tcptls_client_start_timeout(</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_tcptls_session_instance *tcptls_session, int timeout)</span><br><span> {</span><br><span> struct ast_tcptls_session_args *desc;</span><br><span> </span><br><span> if (!(desc = tcptls_session->parent)) {</span><br><span style="color: hsl(0, 100%, 40%);">- goto client_start_error;</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(tcptls_session, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_connect(desc->accept_fd, &desc->remote_address)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Unable to connect %s to %s: %s\n",</span><br><span style="color: hsl(0, 100%, 40%);">- desc->name,</span><br><span style="color: hsl(0, 100%, 40%);">- ast_sockaddr_stringify(&desc->remote_address),</span><br><span style="color: hsl(0, 100%, 40%);">- strerror(errno));</span><br><span style="color: hsl(0, 100%, 40%);">- goto client_start_error;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (socket_connect(desc->accept_fd, &desc->remote_address, timeout)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Unable to connect %s to %s: %s\n", desc->name,</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sockaddr_stringify(&desc->remote_address), strerror(errno));</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- ast_fd_clear_flags(desc->accept_fd, O_NONBLOCK);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(tcptls_session, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span> if (desc->tls_cfg) {</span><br><span> desc->tls_cfg->enabled = 1;</span><br><span>@@ -606,10 +666,11 @@</span><br><span> }</span><br><span> </span><br><span> return handle_tcptls_connection(tcptls_session);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-client_start_error:</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(tcptls_session, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_tcptls_session_instance *ast_tcptls_client_start(struct ast_tcptls_session_instance *tcptls_session)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ return ast_tcptls_client_start_timeout(tcptls_session, -1);</span><br><span> }</span><br><span> </span><br><span> struct ast_tcptls_session_instance *ast_tcptls_client_create(struct ast_tcptls_session_args *desc)</span><br><span>@@ -626,7 +687,7 @@</span><br><span> /* If we return early, there is no connection */</span><br><span> ast_sockaddr_setnull(&desc->old_address);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- fd = desc->accept_fd = socket(ast_sockaddr_is_ipv6(&desc->remote_address) ?</span><br><span style="color: hsl(120, 100%, 40%);">+ fd = desc->accept_fd = ast_socket_nonblock(ast_sockaddr_is_ipv6(&desc->remote_address) ?</span><br><span> AF_INET6 : AF_INET, SOCK_STREAM, IPPROTO_TCP);</span><br><span> if (desc->accept_fd < 0) {</span><br><span> ast_log(LOG_ERROR, "Unable to allocate socket for %s: %s\n",</span><br><span>@@ -673,6 +734,8 @@</span><br><span> </span><br><span> /* Set current info */</span><br><span> ast_sockaddr_copy(&desc->old_address, &desc->remote_address);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Wait for input on socket reads */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_iostream_set_exclusive_input(tcptls_session->stream, 1);</span><br><span> return tcptls_session;</span><br><span> </span><br><span> error:</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/17472">change 17472</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/17472"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I907571843a83e43c0742b95a64785f4411f02671 </div>
<div style="display:none"> Gerrit-Change-Number: 17472 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>