git: 03a9755147ac - main - net/socketbind: unbreak the library

From: Eugene Grosbein <eugen_at_FreeBSD.org>
Date: Thu, 05 Oct 2023 13:16:57 UTC
The branch main has been updated by eugen:

URL: https://cgit.FreeBSD.org/ports/commit/?id=03a9755147acbd1933f5c991ce33ae1bc375746d

commit 03a9755147acbd1933f5c991ce33ae1bc375746d
Author:     Eugene Grosbein <eugen@FreeBSD.org>
AuthorDate: 2023-10-05 13:05:10 +0000
Commit:     Eugene Grosbein <eugen@FreeBSD.org>
CommitDate: 2023-10-05 13:16:51 +0000

    net/socketbind: unbreak the library
    
    The code has multiple issues making it unusable with modern FreeBSD:
    
    * it uses long gone ascii2addr() function removed in 2007;
    * it passes hardcoded "/usr/lib/libc.so" path to dlopen(),
      but now it is plain text hint file;
    * this IPv4-only code neglects to check passed domain for PF_INET
      messing with sockets in other domains (like PF_INET6, PF_LOCAL).
    
    Still, it is very useful while dealing with software like Nagios
    that does not support binding to specific IPv4 address
    for outgoing connections. The library solves the problem with single line
    in /etc/rc.conf:
    
    nagios_env="LD_PRELOAD=/usr/local/lib/libsocketbind.so.1 BINDTO=192.168.6.1"
    
    This changes unbreaks the library making it usable again.
---
 net/socketbind/Makefile           |  1 +
 net/socketbind/files/socketbind.c | 15 +++++++--------
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/net/socketbind/Makefile b/net/socketbind/Makefile
index 3725a84f17b0..bf17612645ba 100644
--- a/net/socketbind/Makefile
+++ b/net/socketbind/Makefile
@@ -1,5 +1,6 @@
 PORTNAME=	socketbind
 PORTVERSION=	1
+PORTREVISION=	1
 CATEGORIES=	net
 MASTER_SITES=	# none
 DISTFILES=	# none
diff --git a/net/socketbind/files/socketbind.c b/net/socketbind/files/socketbind.c
index 51d929d9b794..340083adaefb 100644
--- a/net/socketbind/files/socketbind.c
+++ b/net/socketbind/files/socketbind.c
@@ -2,32 +2,31 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
 #include <dlfcn.h>
+#include <stdlib.h>
 
-static void *socket_p, *dl_handle;
+static void *socket_p = NULL;
 
 static struct sockaddr_in bind_addr;
 static int	   do_bind;
 
 int socket(int domain, int type, int protocol) {
 	auto int res;
-	if (dl_handle == NULL) {
+	if (socket_p == NULL) {
 		char *str;
-		dl_handle = dlopen("/usr/lib/libc.so", RTLD_LAZY);
-		if (dl_handle == NULL) 
-			return -1;
-		socket_p = dlsym(dl_handle, "socket");
+		socket_p = dlsym(RTLD_NEXT, "socket");
 		if (!socket_p)
 			return -1;
 #ifdef DEBUG
 		printf("Loaded socket %x\n", socket_p);
 #endif
 
-		if ((str = getenv("BINDTO")) != NULL) {
+		if ((domain == PF_INET) && (str = getenv("BINDTO")) != NULL) {
 #ifdef DEBUG
 			printf("Thinking about bind\n");
 #endif
-			if (ascii2addr(AF_INET, str, &bind_addr.sin_addr)) {
+			if (inet_aton(str, &bind_addr.sin_addr)) {
 				do_bind = 1;
 				bind_addr.sin_len = INET_ADDRSTRLEN;
 				bind_addr.sin_family = AF_INET;