Programmatically set DNS nameserver on Linux

♀尐吖头ヾ 提交于 2019-12-10 18:06:52

问题


I'd like to be able to add IP addresses for DNS nameservers on Linux from my C/C++ program. I'm on a somewhat embedded platform with a read-only /etc/resolv.conf. This means I can't simply add a "nameserver xxx.xxx.xxx.xxx" line to the file without a little trickery. Are there any clean ways to do this from code?

I could try symlinking /etc/resolv.conf to a file in tmpfs, but that seems hackish and it feels like something I should be able to do without writing to a file.


回答1:


Have a look at nsaddr_list in resolv.conf from your libc.

This question says:

Although not documented, the common way to set the resolver used is to update _res.nsaddr_list.

In uClibc 0.9.31 specifically there is a comment on line 533 in resolv.c describing how this works.



来源:https://stackoverflow.com/questions/5034143/programmatically-set-dns-nameserver-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!