[Asterisk-code-review] Use non-blocking socket() and pipe() wrappers (asterisk[13])

Sean Bright asteriskteam at digium.com
Tue Dec 11 11:26:59 CST 2018


Sean Bright has uploaded this change for review. ( https://gerrit.asterisk.org/10796


Change subject: Use non-blocking socket() and pipe() wrappers
......................................................................

Use non-blocking socket() and pipe() wrappers

Change-Id: I050ceffe5a133d5add2dab46687209813d58f597
---
M main/tcptls.c
M main/udptl.c
M res/res_agi.c
M res/res_rtp_asterisk.c
M res/res_timing_pthread.c
5 files changed, 12 insertions(+), 27 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/96/10796/1

diff --git a/main/tcptls.c b/main/tcptls.c
index c5ab0b6..d32b91f 100644
--- a/main/tcptls.c
+++ b/main/tcptls.c
@@ -1277,7 +1277,7 @@
 		return;
 	}
 
-	desc->accept_fd = socket(ast_sockaddr_is_ipv6(&desc->local_address) ?
+	desc->accept_fd = ast_socket_nonblock(ast_sockaddr_is_ipv6(&desc->local_address) ?
 				 AF_INET6 : AF_INET, SOCK_STREAM, 0);
 	if (desc->accept_fd < 0) {
 		ast_log(LOG_ERROR, "Unable to allocate socket for %s: %s\n", desc->name, strerror(errno));
@@ -1296,7 +1296,6 @@
 		ast_log(LOG_ERROR, "Unable to listen for %s!\n", desc->name);
 		goto error;
 	}
-	ast_fd_set_flags(desc->accept_fd, O_NONBLOCK);
 	if (ast_pthread_create_background(&desc->master, NULL, desc->accept_fn, desc)) {
 		ast_log(LOG_ERROR, "Unable to launch thread for %s on %s: %s\n",
 			desc->name,
diff --git a/main/udptl.c b/main/udptl.c
index 83989f7..59fccf4 100644
--- a/main/udptl.c
+++ b/main/udptl.c
@@ -1038,13 +1038,12 @@
 		udptl->tx[i].buf_len = -1;
 	}
 
-	if ((udptl->fd = socket(ast_sockaddr_is_ipv6(addr) ?
+	if ((udptl->fd = ast_socket_nonblock(ast_sockaddr_is_ipv6(addr) ?
 					AF_INET6 : AF_INET, SOCK_DGRAM, 0)) < 0) {
 		ast_free(udptl);
 		ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
 		return NULL;
 	}
-	ast_fd_set_flags(udptl->fd, O_NONBLOCK);
 
 #ifdef SO_NO_CHECK
 	if (cfg->general->nochecksums)
diff --git a/res/res_agi.c b/res/res_agi.c
index f19303f..270c269 100644
--- a/res/res_agi.c
+++ b/res/res_agi.c
@@ -2073,16 +2073,11 @@
 			ast_sockaddr_set_port(&addrs[i], AGI_PORT);
 		}
 
-		if ((s = socket(addrs[i].ss.ss_family, SOCK_STREAM, IPPROTO_TCP)) < 0) {
+		if ((s = ast_socket_nonblock(addrs[i].ss.ss_family, SOCK_STREAM, IPPROTO_TCP)) < 0) {
 			ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
 			continue;
 		}
 
-		if (ast_fd_set_flags(s, O_NONBLOCK)) {
-			close(s);
-			continue;
-		}
-
 		if (ast_connect(s, &addrs[i]) && errno == EINPROGRESS) {
 
 			if (handle_connection(agiurl, addrs[i], s)) {
diff --git a/res/res_rtp_asterisk.c b/res/res_rtp_asterisk.c
index 4586ca6..d94dd16 100644
--- a/res/res_rtp_asterisk.c
+++ b/res/res_rtp_asterisk.c
@@ -2792,22 +2792,19 @@
 
 static int create_new_socket(const char *type, int af)
 {
-	int sock = socket(af, SOCK_DGRAM, 0);
+	int sock = ast_socket_nonblock(af, SOCK_DGRAM, 0);
 
 	if (sock < 0) {
-		if (!type) {
-			type = "RTP/RTCP";
-		}
 		ast_log(LOG_WARNING, "Unable to allocate %s socket: %s\n", type, strerror(errno));
-	} else {
-		ast_fd_set_flags(sock, O_NONBLOCK);
-#ifdef SO_NO_CHECK
-		if (nochecksums) {
-			setsockopt(sock, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
-		}
-#endif
+		return sock;
 	}
 
+#ifdef SO_NO_CHECK
+	if (nochecksums) {
+		setsockopt(sock, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
+	}
+#endif
+
 	return sock;
 }
 
diff --git a/res/res_timing_pthread.c b/res/res_timing_pthread.c
index eee0749..cefe6a7 100644
--- a/res/res_timing_pthread.c
+++ b/res/res_timing_pthread.c
@@ -116,7 +116,6 @@
 static void *pthread_timer_open(void)
 {
 	struct pthread_timer *timer;
-	int i;
 
 	if (!(timer = ao2_alloc(sizeof(*timer), pthread_timer_destructor))) {
 		errno = ENOMEM;
@@ -126,15 +125,11 @@
 	timer->pipe[PIPE_READ] = timer->pipe[PIPE_WRITE] = -1;
 	timer->state = TIMER_STATE_IDLE;
 
-	if (pipe(timer->pipe)) {
+	if (ast_pipe_nonblock(timer->pipe)) {
 		ao2_ref(timer, -1);
 		return NULL;
 	}
 
-	for (i = 0; i < ARRAY_LEN(timer->pipe); ++i) {
-		ast_fd_set_flags(timer->pipe[i], O_NONBLOCK);
-	}
-
 	ao2_lock(pthread_timers);
 	if (!ao2_container_count(pthread_timers)) {
 		ast_mutex_lock(&timing_thread.lock);

-- 
To view, visit https://gerrit.asterisk.org/10796
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-MessageType: newchange
Gerrit-Change-Id: I050ceffe5a133d5add2dab46687209813d58f597
Gerrit-Change-Number: 10796
Gerrit-PatchSet: 1
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20181211/f3887bd3/attachment.html>


More information about the asterisk-code-review mailing list