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

To  :

Subj:

Re: LD_PRELOAD - anonymous


On Mon, 13 Sep 1999, Adam Morrison wrote: 

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

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) 
{ 
puts("connect"); 
return real_connect(i1, p1, i2); 
} 
[email protected][79, 579]: ~$ gcc -Wall -nostdlib -shared -o drb.so drb.c -ldl 
[email protected][85, 585]: ~$ LD_PRELOAD=./drb.so telnet 0 2345 
Trying 0.0.0.0... 
connect 
telnet: Unable to connect to remote host: Connection refused 
[email protected][86, 586]: ~$ 

Milan Pikula 

-- Milan Pikula, WWW. Finger me for Geek Code. 
http://fornax.elf.stuba.sk/~www, [email protected] .. dajte mi pevnu 
linku a pohnem zemegulou .. 
VX Heavens - collection of viruses,sources and articles.
Сообщения можно выбирать стрелками Enter
Пользовательского поиска