Set DNS / Nameserver programatically and permanently on Linux

流过昼夜 提交于 2019-12-24 08:58:37

问题


I want to update the nameserver programmatically. In this question programmatically-set-dns-nameserver-on-linux was mentioned to manipulate _res.nsaddr_list -- so I did.

void setNameServer(const std::string& rstr_addr)
{
  struct sockaddr_in x_addr;
  inet_pton(AF_INET,rstr_addr.c_str(),&x_addr.sin_addr);
  res_init();
  _res.nsaddr_list[0] = x_addr;
}

This is not working. getaddrinfo is now returning with error: "Name or service not known"

So there a basically two questions:

  1. What is the correct way to set the nameserver ip programmatically

  2. How to set it permanently in a proper way (ATM I just write it to /etc/resolv.conf by myself)

来源:https://stackoverflow.com/questions/55724014/set-dns-nameserver-programatically-and-permanently-on-linux

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