修改gethostbyname (res_search)源代码- Linux Ubuntu 14.04 LTS

时间:2022-12-04 16:26:11

I am trying to modify the behaviour of the DNS lookup functions in Linux for my project by setting it to write a random string in a file as a test.

我试图修改我的项目在Linux中DNS查找函数的行为,将其设置为在文件中编写一个随机字符串作为测试。

Linux use some DNS resolver functions, mainly gethostbyname. Looking forward, I found out that the resolver functions are within the glibc6 library. So, I downloaded it, compiled, generating the libresolv.so, libnss_dns.so dynamic libraries. Then, I replaced the existing ones on my system, at /usr/lib/x86_64-linux-gnu/.

Linux使用一些DNS解析器函数,主要是gethostbyname。展望未来,我发现解析器函数在glibc6库中。我下载了它,编译了,生成了libresolv。所以,libnss_dns。所以动态库。然后,在/usr/lib/x86_64 linux-gnu/上替换系统上的现有代码。

Note: I found out these libraries are the ones that resolves queries by modifying gethostbyname code and compiling again. Then, I saw which dynamic libraries changed.

注意:我发现这些库是通过修改gethostbyname代码并再次编译来解决查询的库。然后,我看到哪些动态库发生了变化。

By creating a program that uses res_query directly (a resolver function) and compiling with -lresolv, it works (I used ldd command and it uses the resolver library I created). But, using gethostbyname directly from the code, using wget or browsing the web I can't get it right.

通过创建一个直接使用res_query(解析器函数)并使用-lresolv编译的程序,它可以工作(我使用了ldd命令,它使用了我创建的resolver库)。但是,直接从代码中使用gethostbyname,使用wget或浏览web,我都做不到。

What am I doing wrong?

我做错了什么?

1 个解决方案

#1


2  

I discovered what should I do:

我发现我应该做什么:

Firstly, the function called isn't gethostbyname. It is _nss_dns_gethostbyname3_r, defined in resolv/nss_dns/dns-host.c. Looking at the source I realized that this function called another one to resolve DNS names, __libc_res_nsearch, defined in resolv/res-query.c. So this is the function! I added some file writting commands there and it worked like a charm. I called it from C code using gethostbyname, used wget and Firefox, all worked. When you compile the code you should replace the libresolv.so of your system by the new one.

首先,调用的函数不是gethostbyname。它是_nss_dns_gethostbyname3_r,在resolv/nss_dns/dns-host.c中定义。查看源代码时,我意识到这个函数调用了另一个解析DNS名称的函数__libc_res_nsearch,它在resolv/res-query.c中定义。这就是函数!我在那里添加了一些文件书写命令,效果非常棒。我使用gethostbyname从C代码调用它,使用wget和Firefox,所有这些都有效。在编译代码时,应该替换libresolv。新系统也是如此。

Note: my glibc version is 2.19.

注意:我的glibc版本是2.19。

I hope this helps someone.

我希望这能帮助某人。

#1


2  

I discovered what should I do:

我发现我应该做什么:

Firstly, the function called isn't gethostbyname. It is _nss_dns_gethostbyname3_r, defined in resolv/nss_dns/dns-host.c. Looking at the source I realized that this function called another one to resolve DNS names, __libc_res_nsearch, defined in resolv/res-query.c. So this is the function! I added some file writting commands there and it worked like a charm. I called it from C code using gethostbyname, used wget and Firefox, all worked. When you compile the code you should replace the libresolv.so of your system by the new one.

首先,调用的函数不是gethostbyname。它是_nss_dns_gethostbyname3_r,在resolv/nss_dns/dns-host.c中定义。查看源代码时,我意识到这个函数调用了另一个解析DNS名称的函数__libc_res_nsearch,它在resolv/res-query.c中定义。这就是函数!我在那里添加了一些文件书写命令,效果非常棒。我使用gethostbyname从C代码调用它,使用wget和Firefox,所有这些都有效。在编译代码时,应该替换libresolv。新系统也是如此。

Note: my glibc version is 2.19.

注意:我的glibc版本是2.19。

I hope this helps someone.

我希望这能帮助某人。