/*	Copyright 2015-2024 Rivoreo

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
	IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
	DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
	OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
	THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#define _GNU_SOURCE	/* For RTLD_NEXT on GNU */
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

static int (*get_next_socket())(int, int, int) {
	static int (*f)(int, int, int);
	if(f) return f;
	*(void **)&f = dlsym(RTLD_NEXT, "socket");
	if(!f) {
		perror("dlsym: socket");
		abort();
	}
	return f;
}

// Return boolean
static int run_external_helper(int fd, const char *interface) {
	pid_t pid = fork();
	if(pid < 0) {
		perror("fork");
		return 0;
	}
	if(pid) {
		int status;
		while(waitpid(pid, &status, 0) < 0) {
			if(errno == EINTR) continue;
			if(errno == ECHILD) return 1;	// Why?
			perror("waitpid");
			return 0;
		}
		return status == 0;
	} else {
		char fd_s[10];
		snprintf(fd_s, sizeof fd_s, "%d", fd);
#ifdef HELPER_PROGRAM_PATH
		execl(HELPER_PROGRAM_PATH, HELPER_PROGRAM_PATH,
#else
		execlp("bind-socket-to-device", "bind-socket-to-device",
#endif
			fd_s, interface, (char *)NULL);
#ifdef HELPER_PROGRAM_PATH
		perror(HELPER_PROGRAM_PATH);
#else
		perror("bind-socket-to-device");
#endif
		_exit(127);
	}
}

int socket(int domain, int type, int protocol) {
	int fd = get_next_socket()(domain, type, protocol);
	if(fd != -1 && (domain == AF_INET || domain == AF_INET6)) {
		const char *bind_interface = getenv("BIND_INTERFACE");
		if(bind_interface && *bind_interface) {
			const char *verbose = getenv("BIND_INTERFACE_VERBOSE");
			if(verbose && *verbose) fprintf(stderr, "Binding socket %d to '%s'\n", fd, bind_interface);
			if(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_interface, strlen(bind_interface)) < 0) {
				perror("setsockopt: SO_BINDTODEVICE");
				fputs("Trying external helper program\n", stderr);
				if(!run_external_helper(fd, bind_interface)) {
					fputs("Failed\n", stderr);
				}
			}
		}
	}
	return fd;
}
