Skip to content

Instantly share code, notes, and snippets.

@intika
Forked from winny-/force-inet4-or-inet6.c
Last active February 1, 2019 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intika/4e154606686a0e139d02fe9d00a8ed0d to your computer and use it in GitHub Desktop.
Save intika/4e154606686a0e139d02fe9d00a8ed0d 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