Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
DNS.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_IO_Network_DNS_h_
5#define _Stroika_Foundation_IO_Network_DNS_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <optional>
10
12#include "Stroika/Foundation/Containers/Sequence.h"
14
15/**
16 * \file
17 *
18 * \note Code-Status: <a href="Code-Status.md#Alpha">Alpha</a>
19 *
20 * TODO:
21 * @todo DNS::Default() vs DNS::kThe???
22 *
23 * @todo How to add/integrate alternate dns implementations (maybe rename this to network-name-resolver???)
24 *
25 * @todo add CTOR args for DNS resolve instances with params like timeouts.
26 *
27 * @todo add ctor params for prefer IPV4, IPV6 or both
28 *
29 * @todo Possibly support async lookup - getaddrinfo_a() or Windows variation doing async lookup
30 */
31
33
34 using Characters::String;
35 using Containers::Sequence;
36
37 /**
38 * DNS (Domain Name Service) Resolver.
39 *
40 * \note This code used to return Collection<String>/Collection<InternetAddress> but I noticed that
41 * http://man7.org/linux/man-pages/man3/getaddrinfo.3.html says "the application should try using
42 * the addresses in the order in which they are returned" so i switched to Sequence<> to preserve
43 * the order.
44 */
45 class DNS {
46 public:
47 /**
48 * Returns the default dns resolver.
49 */
50 static DNS Default ();
51
52 public:
53 /**
54 * Returns the default DNS resolver.
55 *
56 * \note NOTE - future versions of Stroika may allow specification
57 * of alternate DNS resolvers, somehow, probably by just constructing them and using a backend rep.
58 */
59 static const DNS kThe;
60
61 public:
62 DNS ();
63
64 public:
65 /**
66 */
67 struct HostEntry {
68 Sequence<InternetAddress> fAddressList;
69 Sequence<String> fAliases;
70 String fCanonicalName; // aka hostname?
71 };
72
73 public:
74 /**
75 * The argument can be either a hostname (following DNS name restrictions - see https://tools.ietf.org/html/rfc1123#page-13.
76 * Also, see https://tools.ietf.org/html/rfc2732 for 'Literal IPv6 Address Format in URL's Syntax'
77 * Or an IP address (e.g. 192.168.2.2, or fe80::3d83:d6d5:3823:33ea). Note - IP addresses (numeric) maybe wrapped in [] (required by SMTP)
78 * and if so, those names are automatically interpretted correctly (as numeric IP addresses).
79 */
80 nonvirtual HostEntry GetHostEntry (const String& hostNameOrAddress) const;
81
82 public:
83 /**
84 * Lookup the dns name associated with the given ip address. This uses internet
85 * DNS PTR records.
86 */
87 nonvirtual optional<String> ReverseLookup (const InternetAddress& address) const;
88
89 public:
90 /**
91 * Lookup the dns name associated with the given ip address. This uses internet
92 * DNS PTR records, but don't through except in extreme (low memory) circumstances. Just return missing.
93 */
94 nonvirtual optional<String> QuietReverseLookup (const InternetAddress& address) const;
95
96 public:
97 /**
98 * \brief simple wrapper on GetHostEntry - looking up the hostname/ip address and returning the list of associated ip addresses (or the argument ip address).
99 */
100 nonvirtual Sequence<InternetAddress> GetHostAddresses (const String& hostNameOrAddress) const;
101 nonvirtual Sequence<InternetAddress> GetHostAddresses (const String& hostNameOrAddress, InternetAddress::AddressFamily family) const;
102
103 public:
104 /**
105 * \brief simple wrapper on GetHostEntry.
106 *
107 * Return the primary host address for the given argument hostnameOrAddress. Throws exception if
108 * it somehow cannot (e.g. if there are zero addresses associated with the given argument or a network
109 * error retrieving the information).
110 */
111 nonvirtual InternetAddress GetHostAddress (const String& hostNameOrAddress) const;
112 nonvirtual InternetAddress GetHostAddress (const String& hostNameOrAddress, InternetAddress::AddressFamily family) const;
113 };
114 inline const DNS DNS::kThe;
115
116}
117
118/*
119 ********************************************************************************
120 ***************************** Implementation Details ***************************
121 ********************************************************************************
122 */
123#include "DNS.inl"
124
125#endif /*_Stroika_Foundation_IO_Network_DNS_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
A generalization of a vector: a container whose elements are keyed by the natural numbers.
Definition Sequence.h:187
nonvirtual optional< String > ReverseLookup(const InternetAddress &address) const
Definition DNS.cpp:184
nonvirtual optional< String > QuietReverseLookup(const InternetAddress &address) const
Definition DNS.cpp:212
nonvirtual InternetAddress GetHostAddress(const String &hostNameOrAddress) const
simple wrapper on GetHostEntry.
Definition DNS.cpp:261
nonvirtual HostEntry GetHostEntry(const String &hostNameOrAddress) const
Definition DNS.cpp:115
nonvirtual Sequence< InternetAddress > GetHostAddresses(const String &hostNameOrAddress) const
simple wrapper on GetHostEntry - looking up the hostname/ip address and returning the list of associa...
Definition DNS.cpp:238