blob: f4824791023bb89a863195e61b0536583719cd2c [file] [log] [blame] [raw]
/* 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 */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <dlfcn.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;
}
static int (*get_next_bind())(int, const struct sockaddr *, socklen_t) {
static int (*f)(int, const struct sockaddr *, socklen_t);
if(f) return f;
*(void **)&f = dlsym(RTLD_NEXT, "bind");
if(!f) {
perror("dlsym: bind");
abort();
}
return f;
}
static int (*get_next_connect())(int, const struct sockaddr *, socklen_t) {
static int (*f)(int, const struct sockaddr *, socklen_t);
if(f) return f;
*(void **)&f = dlsym(RTLD_NEXT, "connect");
if(!f) {
perror("dlsym: connect");
abort();
}
return f;
}
int socket(int domain, int type, int protocol) {
if(domain == AF_INET6) {
errno = EAFNOSUPPORT;
return -1;
}
return get_next_socket()(domain, type, protocol);
}
int bind(int fd, const struct sockaddr *addr, socklen_t addr_len) {
if(addr->sa_family == AF_INET6) {
errno = EADDRNOTAVAIL;
return -1;
}
return get_next_bind()(fd, addr, addr_len);
}
int connect(int fd, const struct sockaddr *addr, socklen_t addr_len) {
if(addr->sa_family == AF_INET6) {
errno = EADDRNOTAVAIL;
return -1;
}
return get_next_connect()(fd, addr, addr_len);
}