Set DNS / Nameserver programatically and permanent

2019-07-26 11:48发布

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)

0条回答
登录 后发表回答