From 8b5fe54d9f46d1c2d16f97a727339f281d4139d3 Mon Sep 17 00:00:00 2001 From: Sean Bright Date: Fri, 17 Mar 2017 21:43:48 -0400 Subject: [PATCH] http: getprotobyname() is not thread safe Change-Id: I9dfc785d453053668b97886a8b25d202751d8f15 --- main/http.c | 15 +++------------ main/manager.c | 14 ++++---------- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/main/http.c b/main/http.c index ac5aae1..1ad9d94 100644 --- a/main/http.c +++ b/main/http.c @@ -1915,8 +1915,7 @@ static int httpd_process_request(struct ast_tcptls_session_instance *ser) static void *httpd_helper_thread(void *data) { struct ast_tcptls_session_instance *ser = data; - struct protoent *p; - int flags; + int flags = 1; int timeout; if (!ser || !ser->f) { @@ -1936,16 +1935,8 @@ static void *httpd_helper_thread(void *data) * This is necessary to prevent delays (caused by buffering) as we * write to the socket in bits and pieces. */ - p = getprotobyname("tcp"); - if (p) { - int arg = 1; - - if (setsockopt(ser->fd, p->p_proto, TCP_NODELAY, (char *) &arg, sizeof(arg) ) < 0) { - ast_log(LOG_WARNING, "Failed to set TCP_NODELAY on HTTP connection: %s\n", strerror(errno)); - ast_log(LOG_WARNING, "Some HTTP requests may be slow to respond.\n"); - } - } else { - ast_log(LOG_WARNING, "Failed to set TCP_NODELAY on HTTP connection, getprotobyname(\"tcp\") failed\n"); + if (setsockopt(ser->fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flags, sizeof(flags)) < 0) { + ast_log(LOG_WARNING, "Failed to set TCP_NODELAY on HTTP connection: %s\n", strerror(errno)); ast_log(LOG_WARNING, "Some HTTP requests may be slow to respond.\n"); } diff --git a/main/manager.c b/main/manager.c index 0ca84b2..86328c2 100644 --- a/main/manager.c +++ b/main/manager.c @@ -6577,10 +6577,9 @@ static void *session_do(void *data) struct mansession s = { .tcptls_session = data, }; - int flags; + int flags = 1; int res; struct ast_sockaddr ser_remote_address_tmp; - struct protoent *p; if (ast_atomic_fetchadd_int(&unauth_sessions, +1) >= authlimit) { fclose(ser->f); @@ -6600,14 +6599,9 @@ static void *session_do(void *data) /* here we set TCP_NODELAY on the socket to disable Nagle's algorithm. * This is necessary to prevent delays (caused by buffering) as we * write to the socket in bits and pieces. */ - p = getprotobyname("tcp"); - if (p) { - int arg = 1; - if( setsockopt(ser->fd, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) { - ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY mode: %s\nSome manager actions may be slow to respond.\n", strerror(errno)); - } - } else { - ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY, getprotobyname(\"tcp\") failed\nSome manager actions may be slow to respond.\n"); + if (setsockopt(ser->fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flags, sizeof(flags)) < 0) { + ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY mode: %s\n", strerror(errno)); + ast_log(LOG_WARNING, "Some manager actions may be slow to respond.\n"); } /* make sure socket is non-blocking */ -- 2.7.4