Msg : Prev 4 / 222 Next -3
1999-09-13 10:24:05

To  :


Re: LD_PRELOAD - anonymous

On Mon, 13 Sep 1999, Adam Morrison wrote: 

W>connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) 
W> int (*real_connect)(int, const struct sockaddr *, socklen_t) = 
W>        dlsym(RTLD_NEXT, "connect"); 
W> printf("woop!\n"); 
W> return (*real_connect)(sockfd, addr, addrlen); 

it's ok, but slow. mine version does the same thing using a varaible, which 
is set once. 

1. you cannot use your solution in speed-critical applications, 
2. it don't need the name of dynamic library, which is, of course, better, 
3. it's not 100% clean: this one uses prototype redefinition in assigment 
and uses socklen_t, look at the comment in my source. 

the best solution of this problem is therefore to combine both approaches. 

[email protected][78, 578]: ~$ cat drb.c 
#include <stdio.h> 
#include <dlfcn.h> 
#include <signal.h> 

#include <sys/types.h> 
#include <sys/socket.h> 

NOTE from my man page: 
The third argument of connect is in reality an int (and this is what BSD 
4.* and libc4 and libc5 have). Some POSIX confusion resulted in the present 
socklen_t. The draft standard has not been adopted yet, but glibc2 
already follows it and also has socklen_t. See also accept(2). 
#define socklen_t int 

static int (*real_connect)(int, const struct sockaddr *, socklen_t); 

void _init() 
real_connect = (int (*)(int, const struct sockaddr *, socklen_t)) 
(dlsym(RTLD_NEXT, "connect")); 

void _fini() 

int connect(int i1, const struct sockaddr * p1, socklen_t i2) 
return real_connect(i1, p1, i2); 
[email protected][79, 579]: ~$ gcc -Wall -nostdlib -shared -o drb.c -ldl 
[email protected][85, 585]: ~$ LD_PRELOAD=./ telnet 0 2345 
telnet: Unable to connect to remote host: Connection refused 
[email protected][86, 586]: ~$ 

Milan Pikula 

-- Milan Pikula, WWW. Finger me for Geek Code., [email protected] .. dajte mi pevnu 
linku a pohnem zemegulou .. 
VX Heavens - collection of viruses,sources and articles.
Длинный список? Alt + Home - в начало, Alt + End - в конец
Пользовательского поиска