| /* Copyright 2015-2023 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. |
| */ |
| |
| #include <arpa/inet.h> |
| #include <sys/wait.h> |
| #include <sys/socket.h> |
| #include <netinet/in.h> |
| #include <netipx/ipx.h> |
| #include <fcntl.h> |
| #include <dlfcn.h> |
| #include <unistd.h> |
| #include <string.h> |
| #include <stdlib.h> |
| #include <stdio.h> |
| #include <errno.h> |
| |
| #ifndef LIBC_PATH |
| // Default to a common GNU/Linux i386 path |
| #define LIBC_PATH "/lib/libc.so.6" |
| #warning "You may need to define macro LIBC_PATH" |
| #endif |
| |
| #ifndef LIBC_ERRNO_FUNCTION_NAME |
| #define LIBC_ERRNO_FUNCTION_NAME "__errno_location" |
| #endif |
| |
| static void *get_libc() { |
| static void *libc; |
| if(!libc) libc = dlopen(LIBC_PATH, RTLD_LAZY); |
| return libc; |
| } |
| |
| static int (*get_libc_bind())(int, const struct sockaddr *, socklen_t) { |
| static int (*f)(int, const struct sockaddr *, socklen_t); |
| if(f) return f; |
| void *libc = get_libc(); |
| if(!libc) { |
| perror("dlopen"); |
| abort(); |
| } |
| *(void **)&f = dlsym(libc, "bind"); |
| if(!f) { |
| perror("dlsym: bind"); |
| abort(); |
| } |
| return f; |
| } |
| |
| static int *get_libc_errno() { |
| static int *(*f)(void); |
| if(f) return f(); |
| void *libc = get_libc(); |
| if(!libc) { |
| perror("dlopen"); |
| abort(); |
| } |
| *(void **)&f = dlsym(libc, LIBC_ERRNO_FUNCTION_NAME); |
| if(!f) { |
| perror("dlsym: " LIBC_ERRNO_FUNCTION_NAME); |
| abort(); |
| } |
| return f(); |
| } |
| |
| #ifdef __linux__ |
| // Return boolean |
| static int bind_to_device_via_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 BIND_TO_DEVICE_HELPER_PROGRAM_PATH |
| execl(BIND_TO_DEVICE_HELPER_PROGRAM_PATH, BIND_TO_DEVICE_HELPER_PROGRAM_PATH, |
| #else |
| execlp("bind-socket-to-device", "bind-socket-to-device", |
| #endif |
| fd_s, interface, (char *)NULL); |
| #ifdef BIND_TO_DEVICE_HELPER_PROGRAM_PATH |
| perror(BIND_TO_DEVICE_HELPER_PROGRAM_PATH); |
| #else |
| perror("bind-socket-to-device"); |
| #endif |
| _exit(127); |
| } |
| } |
| #endif |
| |
| int bind(int fd, const struct sockaddr *addr, socklen_t addr_len) { |
| fprintf(stderr, "function: bind(%d, %p<sa_family=%hu>, %u)\n", |
| fd, addr, addr ? addr->sa_family : -1, (unsigned int)addr_len); |
| int r = get_libc_bind()(fd, addr, addr_len); |
| int e = r < 0 ? *get_libc_errno() : 0; |
| #ifdef __linux__ |
| if(r == 0 && addr->sa_family == AF_INET) { |
| const struct sockaddr_in *insa = (const struct sockaddr_in *)addr; |
| if(!insa->sin_addr.s_addr) { |
| unsigned int port = ntohs(insa->sin_port); |
| fprintf(stderr, "Socket %d bound to *:%u\n", fd, port); |
| if(port == 5000 || (port & 0x40) == 0x40) { |
| fputs("IPX wrapper guessed\n", stderr); |
| const char *bind_interface = getenv("BIND_INTERFACE"); |
| if(bind_interface && *bind_interface) { |
| 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(!bind_to_device_via_external_helper(fd, bind_interface)) { |
| fputs("Failed\n", stderr); |
| } |
| } |
| } |
| } |
| } |
| } else |
| #endif |
| if(r < 0 && e == EACCES && addr->sa_family == AF_IPX) { |
| const struct sockaddr_ipx *ipxsa = (const struct sockaddr_ipx *)addr; |
| uint16_t port = ntohs(ipxsa->sipx_port); |
| unsigned char network_number[4]; |
| #ifdef __linux__ |
| memcpy(network_number, &ipxsa->sipx_network, 4); |
| #else |
| #define network_number ((struct sockaddr_ipx *)&ifr->ifr_addr)->sipx_addr.x_net.c_net |
| #define sipx_node sipx_addr.x_host.c_host |
| #endif |
| pid_t pid = fork(); |
| if(pid < 0) { |
| perror("fork"); |
| } else if(pid) { |
| int status; |
| while(waitpid(pid, &status, 0) < 0) { |
| if(errno == EINTR) continue; |
| if(errno == ECHILD) return 1; // Why? |
| perror("waitpid"); |
| return 0; |
| } |
| if(status == 0) r = 0; |
| } else { |
| char fd_s[10], network_s[12], node_s[18], port_s[6]; |
| snprintf(fd_s, sizeof fd_s, "%d", fd); |
| snprintf(network_s, sizeof network_s, "%02hhx:%02hhx:%02hhx:%02hhx", |
| network_number[0], network_number[1], network_number[2], network_number[3]); |
| snprintf(node_s, sizeof node_s, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
| ipxsa->sipx_node[0], ipxsa->sipx_node[1], ipxsa->sipx_node[2], ipxsa->sipx_node[3], ipxsa->sipx_node[4], ipxsa->sipx_node[5]); |
| snprintf(port_s, sizeof port_s, "%u", (unsigned int)port); |
| #ifdef IPXBIND_HELPER_PROGRAM_PATH |
| execl(IPXBIND_HELPER_PROGRAM_PATH, IPXBIND_HELPER_PROGRAM_PATH, |
| #else |
| execlp("ipxbind", "ipxbind", |
| #endif |
| fd_s, network_s, node_s, port_s, (char *)NULL); |
| #ifdef IPXBIND_HELPER_PROGRAM_PATH |
| perror(IPXBIND_HELPER_PROGRAM_PATH); |
| #else |
| perror("ipxbind"); |
| #endif |
| _exit(127); |
| } |
| } |
| if(r < 0) errno = e; |
| return r; |
| } |