aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilipp Maier <pmaier@sysmocom.de>2018-08-01 12:40:36 +0200
committerPhilipp Maier <pmaier@sysmocom.de>2018-08-02 11:19:38 +0200
commit99f706d3369d1e3faf6a886b7d6fe1d3145ac15c (patch)
treea6bd15e52e82ad31ec85a92e87ab74b38d15491b
parent0659c5d2f3e7653668838a2162ec72efb67ca68e (diff)
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
-rw-r--r--src/socket.c58
1 files changed, 33 insertions, 25 deletions
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 @@ 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);