E-MailRelay
gnameservers_win32.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_win32.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 "gscope.h"
27#include "gbuffer.h"
28#include "glog.h"
29#include <iphlpapi.h>
30#include <fstream>
31#include <cstdlib>
32
33std::vector<GNet::Address> GNet::nameservers( unsigned int port )
34{
35 std::vector<GNet::Address> result ;
36
37 G::Buffer<char> info_buffer( sizeof(FIXED_INFO) ) ;
38 FIXED_INFO * info = G::buffer_cast<FIXED_INFO*>( info_buffer ) ;
39
40 ULONG size = sizeof(FIXED_INFO) ;
41 auto rc = GetNetworkParams( info , &size ) ;
42 if( rc == ERROR_BUFFER_OVERFLOW )
43 {
44 info_buffer.resize( size == ULONG(0) ? std::size_t(1U) : static_cast<std::size_t>(size) ) ;
45 info = G::buffer_cast<FIXED_INFO*>( info_buffer ) ;
46 rc = GetNetworkParams( info , &size ) ;
47 }
48 if( rc == NO_ERROR )
49 {
50 const char * p = info->DnsServerList.IpAddress.String ;
51 if( GNet::Address::validStrings( p?p:"" , "0" ) )
52 result.push_back( GNet::Address::parse( p?p:"" , port ) ) ;
53
54 for( const IP_ADDR_STRING * addr = info->DnsServerList.Next ; addr ; addr = addr->Next )
55 {
56 p = addr->IpAddress.String ;
57 if( GNet::Address::validStrings( p?p:"" , "0" ) )
58 result.push_back( GNet::Address::parse( p?p:"" , port ) ) ;
59 }
60 }
61 return result ;
62}
63
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
A substitute for std::vector<char> that has more useful alignment guarantees and explicitly avoids de...
Definition: gbuffer.h:63