# WinStd: include/WinStd/WinSock2.h Source File

WinSock2.h
/*
SPDX-License-Identifier: MIT
Copyright © 1991-2022 Amebis
Copyright © 2016 GÉANT
*/
#pragma once
#include "Common.h"
#include <WinSock2.h>
#include <ws2def.h>
#include <WS2tcpip.h>
namespace winstd
{
class ws2_runtime_error : public num_runtime_error<int>
{
public:
ws2_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<int>(num, msg)
{
}
ws2_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) : num_runtime_error<int>(num, msg)
{
}
ws2_runtime_error(_In_ const std::string& msg) : num_runtime_error<int>(WSAGetLastError(), msg)
{
}
ws2_runtime_error(_In_opt_z_ const char *msg = nullptr) : num_runtime_error<int>(WSAGetLastError(), msg)
{
}
tstring msg(_In_opt_ DWORD dwLanguageId = 0) const
{
tstring str;
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, m_num, dwLanguageId, str, NULL)) {
// Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
str.erase(str.find_last_not_of(_T(" \t\n\r\f\v")) + 1);
} else
sprintf(str, m_num >= 0x10000 ? _T("Error 0x%X") : _T("Error %u"), m_num);
return str;
}
};
#if (NTDDI_VERSION >= NTDDI_WINXPSP2) || (_WIN32_WINNT >= 0x0502)
class addrinfo : public handle<PADDRINFOA, NULL>
{
WINSTD_HANDLE_IMPL(addrinfo, NULL)
public:
virtual ~addrinfo()
{
if (m_h != invalid)
free_internal();
}
protected:
void free_internal() noexcept override
{
FreeAddrInfoA(m_h);
}
};
class waddrinfo : public handle<PADDRINFOW, NULL>
{
WINSTD_HANDLE_IMPL(waddrinfo, NULL)
public:
virtual ~waddrinfo()
{
if (m_h != invalid)
free_internal();
}
protected:
void free_internal() noexcept override
{
FreeAddrInfoW(m_h);
}
};
#ifdef _UNICODE
typedef waddrinfo taddrinfo;
#else
typedef addrinfo taddrinfo;
#endif
#endif
}
#pragma warning(push)
#pragma warning(disable: 4505) // Don't warn on unused code
static INT GetAddrInfoA(
_In_opt_ PCSTR pNodeName,
_In_opt_ PCSTR pServiceName,
_In_opt_ const ADDRINFOA *pHints,
_Inout_ winstd::addrinfo &result)
{
PADDRINFOA h;
INT iResult = GetAddrInfoA(pNodeName, pServiceName, pHints, &h);
if (iResult == 0)
result.attach(h);
return iResult;
}
static INT GetAddrInfoW(
_In_opt_ PCWSTR pNodeName,
_In_opt_ PCWSTR pServiceName,
_In_opt_ const ADDRINFOW *pHints,
_Inout_ winstd::waddrinfo &result)
{
PADDRINFOW h;
INT iResult = GetAddrInfoW(pNodeName, pServiceName, pHints, &h);
if (iResult == 0)
result.attach(h);
return iResult;
}
#pragma warning(pop)
winstd::addrinfo
SID wrapper class.
Definition: WinSock2.h:95
winstd::addrinfo::free_internal
void free_internal() noexcept override
Frees address information.
Definition: WinSock2.h:116
winstd::addrinfo::~addrinfo
virtual ~addrinfo()
Frees address information.
Definition: WinSock2.h:104
winstd::handle
Base abstract template class to support generic object handle keeping.
Definition: Common.h:607
winstd::handle< PADDRINFOA, NULL >::m_h
handle_type m_h
Object handle.
Definition: Common.h:858
winstd::num_runtime_error
Numerical runtime error.
Definition: Common.h:1003
winstd::num_runtime_error< int >::error_type
int error_type
Error number type.
Definition: Common.h:1005
winstd::num_runtime_error< int >::m_num
error_type m_num
Numeric error code.
Definition: Common.h:1041
winstd::waddrinfo
SID wrapper class.
Definition: WinSock2.h:128
winstd::waddrinfo::~waddrinfo
virtual ~waddrinfo()
Frees address information.
Definition: WinSock2.h:137
winstd::waddrinfo::free_internal
void free_internal() noexcept override
Frees address information.
Definition: WinSock2.h:149
winstd::ws2_runtime_error
WinSock2 runtime error.
Definition: WinSock2.h:25
winstd::ws2_runtime_error::ws2_runtime_error
ws2_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition: WinSock2.h:43
winstd::ws2_runtime_error::ws2_runtime_error
ws2_runtime_error(const char *msg=nullptr)
Constructs an exception using WSAGetLastError()
Definition: WinSock2.h:61
winstd::ws2_runtime_error::ws2_runtime_error
ws2_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition: WinSock2.h:33
winstd::ws2_runtime_error::ws2_runtime_error
ws2_runtime_error(const std::string &msg)
Constructs an exception using WSAGetLastError()
Definition: WinSock2.h:52
winstd::ws2_runtime_error::msg
tstring msg(DWORD dwLanguageId=0) const
Returns a user-readable Windows error message.
Definition: WinSock2.h:70
winstd::taddrinfo
addrinfo taddrinfo
Multi-byte / Wide-character SID wrapper class (according to _UNICODE)
Definition: WinSock2.h:161
GetAddrInfoW
static INT GetAddrInfoW(PCWSTR pNodeName, PCWSTR pServiceName, const ADDRINFOW *pHints, winstd::waddrinfo &result)
Provides protocol-independent translation from a host name to an address.
Definition: WinSock2.h:194
GetAddrInfoA
static INT GetAddrInfoA(PCSTR pNodeName, PCSTR pServiceName, const ADDRINFOA *pHints, winstd::addrinfo &result)
Provides protocol-independent translation from a host name to an address.
Definition: WinSock2.h:176
winstd::tstring
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition: Common.h:338
FormatMessage
static DWORD FormatMessage(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, std::basic_string< char, _Traits, _Ax > &str, va_list *Arguments)
Formats a message string.
Definition: Common.h:299
sprintf
static int sprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format,...)
Formats string using printf().
Definition: Common.h:284
WINSTD_HANDLE_IMPL
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:163
winstd::handle< PADDRINFOA, NULL >::invalid
static const PADDRINFOA invalid
Invalid handle value.
Definition: Common.h:617