Index | Thread | Search

From:
David Gwynne <david@gwynne.id.au>
Subject:
dhcpd(8): integrate sync into the event loop properly
To:
tech@openbsd.org
Cc:
beck@openbsd.org, sthen@openbsd.org
Date:
Tue, 20 May 2025 14:44:46 +1000

Download raw body.

Thread
  • David Gwynne:

    dhcpd(8): integrate sync into the event loop properly

at the moment the event loop code handles the syncfd directly. it could
add it's own event handler like all the other fds integrated into the
event loop with add_protocol.

could someone test this for me?

Index: dhcpd.c
===================================================================
RCS file: /cvs/src/usr.sbin/dhcpd/dhcpd.c,v
diff -u -p -r1.60 dhcpd.c
--- dhcpd.c	21 Aug 2024 09:19:55 -0000	1.60
+++ dhcpd.c	20 May 2025 04:40:59 -0000
@@ -204,8 +204,7 @@ main(int argc, char *argv[])
 			fatal("setrtable");
 
 	if (syncsend || syncrecv) {
-		syncfd = sync_init(sync_iface, sync_baddr, sync_port);
-		if (syncfd == -1)
+		if (sync_init(sync_iface, sync_baddr, sync_port) == -1)
 			err(1, "sync init");
 	}
 
Index: dispatch.c
===================================================================
RCS file: /cvs/src/usr.sbin/dhcpd/dispatch.c,v
diff -u -p -r1.45 dispatch.c
--- dispatch.c	2 Sep 2023 10:18:45 -0000	1.45
+++ dispatch.c	20 May 2025 04:40:59 -0000
@@ -67,8 +67,6 @@
 #include "log.h"
 #include "sync.h"
 
-extern int syncfd;
-
 struct interface_info *interfaces;
 struct protocol *protocols;
 struct dhcpd_timeout *timeouts;
@@ -309,8 +307,6 @@ dispatch(void)
 
 	for (nfds = 0, l = protocols; l; l = l->next)
 		nfds++;
-	if (syncfd != -1)
-		nfds++;
 	if (nfds > nfds_max) {
 		fds = reallocarray(fds, nfds, sizeof(struct pollfd));
 		if (fds == NULL)
@@ -363,12 +359,6 @@ another:
 		if (i == 0)
 			fatalx("No live interfaces to poll on - exiting.");
 
-		if (syncfd != -1) {
-			/* add syncer */
-			fds[i].fd = syncfd;
-			fds[i].events = POLLIN;
-		}
-
 		/* Wait for a packet or a timeout... */
 		switch (poll(fds, nfds, to_msec)) {
 		case -1:
@@ -392,8 +382,6 @@ another:
 			}
 			++i;
 		}
-		if ((syncfd != -1) && (fds[i].revents & (POLLIN | POLLHUP)))
-			sync_recv();
 		interfaces_invalidated = 0;
 	}
 }
Index: sync.c
===================================================================
RCS file: /cvs/src/usr.sbin/dhcpd/sync.c,v
diff -u -p -r1.25 sync.c
--- sync.c	24 Aug 2024 08:35:24 -0000	1.25
+++ sync.c	20 May 2025 04:40:59 -0000
@@ -61,6 +61,7 @@ struct sync_host {
 };
 LIST_HEAD(synchosts, sync_host) sync_hosts = LIST_HEAD_INITIALIZER(sync_hosts);
 
+void	 sync_recv(struct protocol *);
 void	 sync_send(struct iovec *, int);
 
 int
@@ -223,6 +224,8 @@ sync_init(const char *iface, const char 
 		    sendmcast ? "" : "receive ",
 		    ttl, inet_ntoa(sync_out.sin_addr), port);
 
+	add_protocol("sync", syncfd, sync_recv, NULL);
+
 	return (syncfd);
 
  fail:
@@ -231,7 +234,7 @@ sync_init(const char *iface, const char 
 }
 
 void
-sync_recv(void)
+sync_recv(struct protocol *protocol)
 {
 	struct dhcp_synchdr *hdr;
 	struct sockaddr_in addr;
Index: sync.h
===================================================================
RCS file: /cvs/src/usr.sbin/dhcpd/sync.h,v
diff -u -p -r1.5 sync.h
--- sync.h	4 Oct 2016 22:47:51 -0000	1.5
+++ sync.h	20 May 2025 04:40:59 -0000
@@ -67,9 +67,7 @@ struct dhcp_synctlv_lease {
 #define DHCP_SYNC_END		0x0000
 #define DHCP_SYNC_LEASE		0x0001
 
-extern int	 syncfd;
 extern int	 sync_init(const char *, const char *, u_short);
 extern int	 sync_addhost(const char *, u_short);
-extern void	 sync_recv(void);
 extern void	 sync_lease(struct lease *);
 #endif /* _DHCPD_SYNC */