diff options
author | Martijn van Duren <martijn@cvs.openbsd.org> | 2021-08-10 06:52:04 +0000 |
---|---|---|
committer | Martijn van Duren <martijn@cvs.openbsd.org> | 2021-08-10 06:52:04 +0000 |
commit | 7bc37e668e20f806c08da9e09c836229239aa1bb (patch) | |
tree | 343cb28c27d3c867f5d93a322bb3fdaae167a51e | |
parent | 53cc0ddd0e105f37f16f76dd555e374fc470d545 (diff) |
Set the SO_REUSEADDR flag on listening sockets.
This way we can have a global listen statement, but add an additional
listener on with different flags on specific interfaces (e.g. allow
snmpv2c on localhost for easier testing)
OK sthen@
-rw-r--r-- | usr.sbin/snmpd/snmpd.c | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/usr.sbin/snmpd/snmpd.c b/usr.sbin/snmpd/snmpd.c index 750ffd7205e..bc32cc96e7c 100644 --- a/usr.sbin/snmpd/snmpd.c +++ b/usr.sbin/snmpd/snmpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpd.c,v 1.45 2021/08/09 18:14:53 martijn Exp $ */ +/* $OpenBSD: snmpd.c,v 1.46 2021/08/10 06:52:03 martijn Exp $ */ /* * Copyright (c) 2007, 2008, 2012 Reyk Floeter <reyk@openbsd.org> @@ -312,8 +312,22 @@ snmpd_dispatch_snmpe(int fd, struct privsep_proc *p, struct imsg *imsg) int snmpd_socket_af(struct sockaddr_storage *ss, int type) { - return socket(ss->ss_family, (type == SOCK_STREAM ? + int fd, serrno; + const int enable = 1; + + fd = socket(ss->ss_family, (type == SOCK_STREAM ? SOCK_STREAM | SOCK_NONBLOCK : SOCK_DGRAM) | SOCK_CLOEXEC, 0); + if (fd == -1) + return -1; + + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, + sizeof(enable)) == -1) { + serrno = errno; + close(fd); + errno = serrno; + return -1; + } + return fd; } u_long |