2

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)

florgeng
  • 856
  • 1
  • 9
  • 17
  • For Systemd-based systems see [`hostnamectl.c`](https://github.com/systemd/systemd/blob/master/src/hostname/hostnamectl.c). I believe systemd has an API, but I am not sure if hostname is exposed. – jww Apr 17 '19 at 10:45
  • 1
    `struct sockaddr_in` is not the same as `struct in_addr`. – stark Apr 17 '19 at 17:29
  • @stark absolutely correct, thanks, I corrected the code - but I still have the same problem – florgeng Apr 17 '19 at 20:47

0 Answers0