From 7bc37e668e20f806c08da9e09c836229239aa1bb Mon Sep 17 00:00:00 2001 From: Martijn van Duren Date: Tue, 10 Aug 2021 06:52:04 +0000 Subject: 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@ --- usr.sbin/snmpd/snmpd.c | 18 ++++++++++++++++-- 1 file 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 @@ -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 -- cgit v1.2.3