Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::SocketDNS Struct Reference

Synchronous DNS Resolution. More...

#include <Socket.h>

Static Public Member Functions

static Result resolveDNS (StringSpan host, Span< char > &ipAddress)
 Resolve an host string to an ip address (blocking until DNS response arrives)
 

Detailed Description

Synchronous DNS Resolution.

Example:

char buffer[256] = {0};
Span<char> ipAddress = {buffer};
SC_TEST_EXPECT(SocketDNS::resolveDNS("localhost", ipAddress));
StringView ipString = StringView(ipAddress, true, StringEncoding::Ascii);
SC_TEST_EXPECT(ipString == "127.0.0.1" or ipString == "::1");
const char utf8Localhost[] = "localhost";
ipAddress = {buffer};
SC_TEST_EXPECT(SocketDNS::resolveDNS(
StringSpan({utf8Localhost, sizeof(utf8Localhost) - 1}, true, StringEncoding::Utf8), ipAddress));
const char nonAsciiHost[] = {
'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't', static_cast<char>(0xc3), static_cast<char>(0xa9)};
ipAddress = {buffer};
SC_TEST_EXPECT(not SocketDNS::resolveDNS(
StringSpan({nonAsciiHost, sizeof(nonAsciiHost)}, false, StringEncoding::Utf8), ipAddress));

Member Function Documentation

◆ resolveDNS()

static Result SC::SocketDNS::resolveDNS ( StringSpan host,
Span< char > & ipAddress )
staticnodiscard

Resolve an host string to an ip address (blocking until DNS response arrives)

Parameters
[in]hostThe ASCII encoded host string (example.com)
[out]ipAddressHost ip address (ASCII encoded and null-terminated)
Returns
Valid Result if ip address for the passed host has been successfully resolved

Example:

char buffer[256] = {0};
Span<char> ipAddress = {buffer};
SC_TEST_EXPECT(SocketDNS::resolveDNS("localhost", ipAddress));
StringView ipString = StringView(ipAddress, true, StringEncoding::Ascii);
SC_TEST_EXPECT(ipString == "127.0.0.1" or ipString == "::1");
const char utf8Localhost[] = "localhost";
ipAddress = {buffer};
SC_TEST_EXPECT(SocketDNS::resolveDNS(
StringSpan({utf8Localhost, sizeof(utf8Localhost) - 1}, true, StringEncoding::Utf8), ipAddress));
const char nonAsciiHost[] = {
'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't', static_cast<char>(0xc3), static_cast<char>(0xa9)};
ipAddress = {buffer};
SC_TEST_EXPECT(not SocketDNS::resolveDNS(
StringSpan({nonAsciiHost, sizeof(nonAsciiHost)}, false, StringEncoding::Utf8), ipAddress));

The documentation for this struct was generated from the following file: