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.
This commit is contained in:
Eugene Grosbein 2023-10-05 20:05:10 +07:00
parent 3d9993d8ac
commit 03a9755147
No known key found for this signature in database
GPG Key ID: C8960FF146564C9A
2 changed files with 8 additions and 8 deletions

View File

@ -1,5 +1,6 @@
PORTNAME= socketbind
PORTVERSION= 1
PORTREVISION= 1
CATEGORIES= net
MASTER_SITES= # none
DISTFILES= # none

View File

@ -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;