Skip to content

Instantly share code, notes, and snippets.

@mio-19
Forked from winny-/force-inet4-or-inet6.c
Created May 7, 2024 07:20
Show Gist options
  • Save mio-19/d7b17e2ccc144c1bfab47ea5db415d24 to your computer and use it in GitHub Desktop.
Save mio-19/d7b17e2ccc144c1bfab47ea5db415d24 to your computer and use it in GitHub Desktop.
Force IPv4 or IPv6 in applications such as Lynx
/*
force-inet4-or-inet6.c - hack to force applications to use IPv4 or IPv6 exclusively
compilation: cc -fPIC -shared -DUSE_INET4 -o force-inet4.so force-inet4-or-inet6.c
cc -fPIC -shared -DUSE_INET6 -o force-inet6.so force-inet4-or-inet6.c
usage: LD_PRELOAD=/path/to/library.so program args ...
This file is in the public domain.
*/
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <dlfcn.h>
int
getaddrinfo(const char *hostname, const char *servname,
const struct addrinfo *hints, struct addrinfo **res)
{
int (*getaddrinfo_original)();
struct addrinfo *hints2 = hints;
getaddrinfo_original = dlsym(RTLD_NEXT, "getaddrinfo");
#if defined USE_INET4
hints2->ai_family = PF_INET;
#elif defined USE_INET6
hints2->ai_family = PF_INET6;
#else
#error You must define USE_INET4 or USE_INET6.
#endif
return (getaddrinfo_original(hostname, servname, hints2, res));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment