]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
thread safety: Don't use getprotobyname()
authorSean Bright <sean.bright@gmail.com>
Sat, 18 Mar 2017 17:30:32 +0000 (13:30 -0400)
committerSean Bright <sean.bright@gmail.com>
Mon, 20 Mar 2017 12:55:05 +0000 (08:55 -0400)
POSIX does not require getprotobyname() to be thread safe and some
implementations use static memory which causes issues when multiple
threads are used.

Further, our usage of it today is just to ultimately get IPPROTO_TCP
for calls to setsockopt(). So instead we just use IPPROTO_TCP directly.

Change-Id: I2e14e58674808f7ce99b2f5e900d0f90d0d8da48

channels/chan_skinny.c
include/asterisk/network.h
main/http.c
main/manager.c

index a3a2f87fbe670af0e8ba8323eabd73115a392099..d1c2b927a61385f7733657c0363adf8fb9a9c673 100644 (file)
@@ -7639,7 +7639,6 @@ static void *accept_thread(void *ignore)
        struct sockaddr_in sin;
        socklen_t sinlen;
        struct skinnysession *s;
-       struct protoent *p;
        int arg = 1;
 
        for (;;) {
@@ -7656,12 +7655,10 @@ static void *accept_thread(void *ignore)
                        continue;
                }
 
-               p = getprotobyname("tcp");
-               if(p) {
-                       if( setsockopt(as, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
-                               ast_log(LOG_WARNING, "Failed to set Skinny tcp connection to TCP_NODELAY mode: %s\n", strerror(errno));
-                       }
+               if (setsockopt(as, IPPROTO_TCP, TCP_NODELAY, (char *) &arg, sizeof(arg)) < 0) {
+                       ast_log(LOG_WARNING, "Failed to set TCP_NODELAY on Skinny TCP connection: %s\n", strerror(errno));
                }
+
                if (!(s = ast_calloc(1, sizeof(struct skinnysession)))) {
                        close(as);
                        ast_atomic_fetchadd_int(&unauth_sessions, -1);
index 3371e58952f7e60978234d8f19cbbd2143ebfd04..5216f4c61a2bf124cf51f24d72fa99a08a8019c1 100644 (file)
@@ -86,6 +86,11 @@ const char *ast_inet_ntoa(struct in_addr ia);
 #endif
 #define inet_ntoa __dont__use__inet_ntoa__use__ast_inet_ntoa__instead__
 
+#ifdef getprotobyname
+#undef getprotobyname
+#endif
+#define getprotobyname __getprotobyname_is_not_threadsafe__do_not_use__
+
 /*! \brief Compares the source address and port of two sockaddr_in */
 static force_inline int inaddrcmp(const struct sockaddr_in *sin1, const struct sockaddr_in *sin2)
 {
index 0db6ee7b66fbcba18073e25b174ec6b496865fd4..ea85a282364a5508f161e709333861b90bbb281b 100644 (file)
@@ -1917,9 +1917,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.
         */
-       if (setsockopt(ast_iostream_get_fd(ser->stream), IPPROTO_TCP, TCP_NODELAY, (char *) &arg, sizeof(arg) ) < 0) {
+       if (setsockopt(ast_iostream_get_fd(ser->stream), IPPROTO_TCP, 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");
        }
        ast_iostream_nonblock(ser->stream);
 
index eae1ca52afdaac2acf7ee4552dd7fb483bcc3fd8..c1d73dce7634e10afdb6a175e9958f37969ded68 100644 (file)
@@ -6647,8 +6647,8 @@ 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. */
-       if (setsockopt(ast_iostream_get_fd(ser->stream), IPPROTO_TCP, 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));
+       if (setsockopt(ast_iostream_get_fd(ser->stream), IPPROTO_TCP, TCP_NODELAY, (char *) &arg, sizeof(arg)) < 0) {
+               ast_log(LOG_WARNING, "Failed to set TCP_NODELAY on manager connection: %s\n", strerror(errno));
        }
        ast_iostream_nonblock(ser->stream);