Change in libosmocore[master]: socket: do not set SO_REUSEADDR for IPPROTO_UDP

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Thu Aug 2 11:59:37 UTC 2018


Harald Welte has submitted this change and it was merged. ( https://gerrit.osmocom.org/10289 )

Change subject: socket: do not set SO_REUSEADDR for IPPROTO_UDP
......................................................................

socket: do not set SO_REUSEADDR for IPPROTO_UDP

When UDP is used as protocol (proto=IPPROTO_DUP), then we should not set
SO_REUSEADDR in the socket option. Because if we do, we allow two
processes to bind on the same UDP port. The errornous situation will be
undetectable to both applications. So lets only set SO_REUSEADDR when we
do not use UDP.

- Add check if we use UDP, if yes do not set SO_REUSEADDR

Change-Id: I4a8ffb8d598aca88801a4a0322944d7cdd8d4047
Related: OS#3441
---
M src/socket.c
1 file changed, 33 insertions(+), 25 deletions(-)

Approvals:
  Jenkins Builder: Verified
  Harald Welte: Looks good to me, approved



diff --git a/src/socket.c b/src/socket.c
index 0a4e34c..210dbf9 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -209,16 +209,20 @@
 			if (sfd < 0)
 				continue;
 
-			rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,
-							&on, sizeof(on));
-			if (rc < 0) {
-				LOGP(DLGLOBAL, LOGL_ERROR,
-					"cannot setsockopt socket:"
-					" %s:%u: %s\n",
-					local_host, local_port, strerror(errno));
-				close(sfd);
-				continue;
+			if (proto != IPPROTO_UDP) {
+				rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,
+						&on, sizeof(on));
+				if (rc < 0) {
+					LOGP(DLGLOBAL, LOGL_ERROR,
+					     "cannot setsockopt socket:"
+					     " %s:%u: %s\n",
+					     local_host, local_port,
+					     strerror(errno));
+					close(sfd);
+					continue;
+				}
 			}
+
 			if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) {
 				LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: %s:%u: %s\n",
 					local_host, local_port, strerror(errno));
@@ -345,15 +349,17 @@
 				continue;
 			}
 		} else {
-			rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,
-							&on, sizeof(on));
-			if (rc < 0) {
-				LOGP(DLGLOBAL, LOGL_ERROR,
-					"cannot setsockopt socket:"
-					" %s:%u: %s\n",
-					host, port, strerror(errno));
-				close(sfd);
-				continue;
+			if (proto != IPPROTO_UDP) {
+				rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,
+						&on, sizeof(on));
+				if (rc < 0) {
+					LOGP(DLGLOBAL, LOGL_ERROR,
+					     "cannot setsockopt socket:"
+					     " %s:%u: %s\n",
+					     host, port, strerror(errno));
+					close(sfd);
+					continue;
+				}
 			}
 			if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) {
 				LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket:"
@@ -373,13 +379,15 @@
 		return -ENODEV;
 	}
 
-	rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
-	if (rc < 0) {
-		LOGP(DLGLOBAL, LOGL_ERROR,
-		     "cannot setsockopt socket: %s:%u: %s\n", host, port,
-		     strerror(errno));
-		close(sfd);
-		sfd = -1;
+	if (proto != IPPROTO_UDP) {
+		rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+		if (rc < 0) {
+			LOGP(DLGLOBAL, LOGL_ERROR,
+			     "cannot setsockopt socket: %s:%u: %s\n", host,
+			     port, strerror(errno));
+			close(sfd);
+			sfd = -1;
+		}
 	}
 
 	rc = osmo_sock_init_tail(sfd, type, flags);

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

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I4a8ffb8d598aca88801a4a0322944d7cdd8d4047
Gerrit-Change-Number: 10289
Gerrit-PatchSet: 3
Gerrit-Owner: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: daniel <dwillmann at sysmocom.de>
Gerrit-Reviewer: dexter <pmaier at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20180802/64b3289d/attachment.htm>


More information about the gerrit-log mailing list