E-MailRelay
gnameservers_unix.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gnameservers_unix.cpp
19///
20
21#include "gdef.h"
22#include "gnameservers.h"
23#include "gstr.h"
24#include "gstringview.h"
25#include "gstringtoken.h"
26#include <fstream>
27
28std::vector<GNet::Address> GNet::nameservers( unsigned int port )
29{
30 std::vector<GNet::Address> result ;
31 std::string line ;
32 std::ifstream f( "/etc/resolv.conf" ) ;
33 while( G::Str::readLine( f , line ) )
34 {
35 std::string_view sv( line ) ;
36 G::StringTokenView t( sv , " \t" ) ;
37 if( t.valid() && G::Str::imatch(t(),"nameserver") )
38 {
39 ++t ;
40 if( t.valid() && GNet::Address::validStrings(G::sv_to_string(t()),"0") )
41 result.push_back( GNet::Address::parse( G::sv_to_string(t()) , port ) ) ;
42 }
43 }
44 return result ;
45}
46
static bool validStrings(std::string_view ip, std::string_view port_string, std::string *reason=nullptr)
Returns true if the combined network-address string and port string is valid.
Definition: gaddress.cpp:398
static Address parse(std::string_view display_string)
Factory function for any address family.
Definition: gaddress.cpp:178
static bool imatch(char, char) noexcept
Returns true if the two characters are the same, ignoring seven-bit case.
Definition: gstr.cpp:1415
static std::istream & readLine(std::istream &stream, std::string &result, std::string_view eol={}, bool pre_erase_result=true, std::size_t limit=0U)
Reads a line from the stream using the given line terminator, which may be multi-character.
Definition: gstr.cpp:958
A zero-copy string token iterator where the token separators are runs of whitespace characters,...
Definition: gstringtoken.h:54