OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
default_dns_list_builder.cc
Go to the documentation of this file.
1 #include "../dns_proto.h"
2 #include <fstream>
3 
5  DefaultServerList ip_list;
6 
7  if (agent()->test_mode()) {
8  // Mocking a list with one ip address for test purposes.
9  boost::system::error_code ec;
10  IpAddress ip = IpAddress::from_string("127.0.0.1", ec);
11  if (!ec.value()) {
12  ip_list.push_back(ip);
13  }
14  return ip_list;
15  }
16 
17  std::ifstream fd;
18  fd.open("/etc/resolv.conf");
19  if (!fd.is_open()) {
20  return ip_list;
21  }
22 
23  std::string line;
24  while (getline(fd, line)) {
25  std::size_t pos = line.find_first_of("#");
26  std::stringstream ss(line.substr(0, pos));
27  std::string key;
28  ss >> key;
29  if (key == "nameserver") {
30  std::string ip_str;
31  ss >> ip_str;
32  boost::system::error_code ec;
33  IpAddress ip = IpAddress::from_string(ip_str, ec);
34  if (!ec.value()) {
35  ip_list.push_back(ip);
36  }
37  }
38  }
39 
40  fd.close();
41 
42  return ip_list;
43 }
boost::asio::ip::address IpAddress
Definition: address.h:13
DefaultServerList BuildDefaultServerListImpl()
std::vector< IpAddress > DefaultServerList
Definition: dns_proto.h:150