Skip to content

Instantly share code, notes, and snippets.

@inaz2
Created August 21, 2014 08:03
Show Gist options
  • Save inaz2/7a8e87e575aaaa4ef704 to your computer and use it in GitHub Desktop.
Save inaz2/7a8e87e575aaaa4ef704 to your computer and use it in GitHub Desktop.
ライブラリ関数のhook
/*
compile:
$ gcc -shared -fPIC -o hook.so hook.c -ldl
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
static int (*getaddrinfo_0)(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
extern int h_errno;
static struct hostent *(*gethostbyname_0)(const char *name);
void __attribute__((constructor))
hook_init()
{
getaddrinfo_0 = dlsym(RTLD_NEXT, "getaddrinfo");
gethostbyname_0 = dlsym(RTLD_NEXT, "gethostbyname");
}
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
{
fprintf(stderr, "getaddrinfo: node=%s, service=%s\n", node, service);
return (*getaddrinfo_0)(node, service, hints, res);
}
struct hostent *gethostbyname(const char *name)
{
fprintf(stderr, "gethostbyname: name=%s\n", name);
return (*gethostbyname_0)(name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment