From 99f706d3369d1e3faf6a886b7d6fe1d3145ac15c Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Wed, 1 Aug 2018 12:40:36 +0200 Subject: 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 --- src/socket.c | 58 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/socket.c b/src/socket.c index 0a4e34cd..210dbf96 100644 --- a/src/socket.c +++ b/src/socket.c @@ -209,16 +209,20 @@ int osmo_sock_init2(uint16_t family, uint16_t type, uint8_t proto, 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 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, 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 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, 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); -- cgit v1.2.3