<p>Joshua Colp <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/17720">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Joshua Colp: Looks good to me, but someone else must approve; Approved for Submit
George Joseph: Looks good to me, approved
</div><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.<br><br>This patch makes the client socket non-blocking while connecting,<br>ensures a connect does not immediately fail due to EINTR "errors",<br>and adds a connect timeout option.<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, 100 insertions(+), 14 deletions(-)<br><br></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 1e66201..1b1b56c 100644</span><br><span>--- a/include/asterisk/tcptls.h</span><br><span>+++ b/include/asterisk/tcptls.h</span><br><span>@@ -164,8 +164,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..b2756d1 100644</span><br><span>--- a/main/tcptls.c</span><br><span>+++ b/main/tcptls.c</span><br><span>@@ -582,20 +582,82 @@</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(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 style="color: hsl(120, 100%, 40%);">+</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> ast_fd_clear_flags(desc->accept_fd, O_NONBLOCK);</span><br><span>@@ -606,10 +668,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 +689,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 +736,7 @@</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%);">+</span><br><span> return tcptls_session;</span><br><span> </span><br><span> error:</span><br><span></span><br></pre><div style="white-space:pre-wrap"></div><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/17720">change 17720</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/+/17720"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: I907571843a83e43c0742b95a64785f4411f02671 </div>
<div style="display:none"> Gerrit-Change-Number: 17720 </div>
<div style="display:none"> Gerrit-PatchSet: 4 </div>
<div style="display:none"> Gerrit-Owner: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-Reviewer: Sean Bright <sean@seanbright.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>