WinStd
Windows Win32 API using Standard C++
Loading...
Searching...
No Matches
WinSock2.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2024 Amebis
4 Copyright © 2016 GÉANT
5*/
6
8
9#pragma once
10
11#include "Common.h"
12#include <WinSock2.h>
13#include <ws2def.h>
14#include <WS2tcpip.h>
15
16namespace winstd
17{
20
25 {
26 public:
33 {}
34
41 ws2_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<int>(num, msg + ": " + message(num))
42 {}
43
50 ws2_runtime_error(_In_ error_type num, _In_z_ const char *msg) : num_runtime_error<int>(num, std::string(msg) + ": " + message(num))
51 {}
52
56 ws2_runtime_error() : num_runtime_error<int>(WSAGetLastError(), message(WSAGetLastError()))
57 {}
58
64 ws2_runtime_error(_In_ const std::string& msg) : num_runtime_error<int>(WSAGetLastError(), msg + ": " + message(WSAGetLastError()))
65 {}
66
72 ws2_runtime_error(_In_z_ const char *msg) : num_runtime_error<int>(WSAGetLastError(), std::string(msg) + ": " + message(WSAGetLastError()))
73 {}
74
75 protected:
81 static std::string message(_In_ error_type num, _In_opt_ DWORD dwLanguageId = 0)
82 {
83 std::wstring wstr;
84 if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, num, dwLanguageId, wstr, NULL)) {
85 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
86 wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1);
87 }
88 else
89 sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num);
90 std::string str;
91 WideCharToMultiByte(CP_UTF8, 0, wstr, str, NULL, NULL);
92 return str;
93 }
94 };
95
97
100
101#if (NTDDI_VERSION >= NTDDI_WINXPSP2) || (_WIN32_WINNT >= 0x0502)
102
108 class addrinfo : public handle<PADDRINFOA, NULL>
109 {
110 WINSTD_HANDLE_IMPL(addrinfo, PADDRINFOA, NULL)
111
112 public:
118 virtual ~addrinfo()
119 {
120 if (m_h != invalid)
122 }
123
124 protected:
130 void free_internal() noexcept override
131 {
132 FreeAddrInfoA(m_h);
133 }
134 };
135
141 class waddrinfo : public handle<PADDRINFOW, NULL>
142 {
143 WINSTD_HANDLE_IMPL(waddrinfo, PADDRINFOW, NULL)
144
145 public:
151 virtual ~waddrinfo()
152 {
153 if (m_h != invalid)
155 }
156
157 protected:
163 void free_internal() noexcept override
164 {
165 FreeAddrInfoW(m_h);
166 }
167 };
168
172#ifdef _UNICODE
173 typedef waddrinfo taddrinfo;
174#else
176#endif
177
178#endif
179
181}
182
185
186#pragma warning(push)
187#pragma warning(disable: 4505) // Don't warn on unused code
188
190static INT GetAddrInfoA(
191 _In_opt_ PCSTR pNodeName,
192 _In_opt_ PCSTR pServiceName,
193 _In_opt_ const ADDRINFOA *pHints,
194 _Inout_ winstd::addrinfo &result)
195{
196 PADDRINFOA h;
197 INT iResult = GetAddrInfoA(pNodeName, pServiceName, pHints, &h);
198 if (iResult == 0)
199 result.attach(h);
200 return iResult;
201}
202
208static INT GetAddrInfoW(
209 _In_opt_ PCWSTR pNodeName,
210 _In_opt_ PCWSTR pServiceName,
211 _In_opt_ const ADDRINFOW *pHints,
212 _Inout_ winstd::waddrinfo &result)
213{
214 PADDRINFOW h;
215 INT iResult = GetAddrInfoW(pNodeName, pServiceName, pHints, &h);
216 if (iResult == 0)
217 result.attach(h);
218 return iResult;
219}
220
221#pragma warning(pop)
222
ADDRINFOA wrapper class.
Definition WinSock2.h:109
void free_internal() noexcept override
Frees address information.
Definition WinSock2.h:130
virtual ~addrinfo()
Frees address information.
Definition WinSock2.h:118
Base abstract template class to support generic object handle keeping.
Definition Common.h:1024
handle_type m_h
Definition Common.h:1276
Numerical runtime error.
Definition Common.h:1481
int error_type
Definition Common.h:1483
ADDRINFOW wrapper class.
Definition WinSock2.h:142
virtual ~waddrinfo()
Frees address information.
Definition WinSock2.h:151
void free_internal() noexcept override
Frees address information.
Definition WinSock2.h:163
WinSock2 runtime error.
Definition WinSock2.h:25
ws2_runtime_error(error_type num)
Constructs an exception.
Definition WinSock2.h:32
ws2_runtime_error()
Constructs an exception using WSAGetLastError()
Definition WinSock2.h:56
ws2_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition WinSock2.h:41
ws2_runtime_error(error_type num, const char *msg)
Constructs an exception.
Definition WinSock2.h:50
ws2_runtime_error(const std::string &msg)
Constructs an exception using WSAGetLastError()
Definition WinSock2.h:64
ws2_runtime_error(const char *msg)
Constructs an exception using WSAGetLastError()
Definition WinSock2.h:72
static std::string message(error_type num, DWORD dwLanguageId=0)
Returns a user-readable Windows error message.
Definition WinSock2.h:81
addrinfo taddrinfo
Multi-byte / Wide-character ADDRINFO wrapper class (according to _UNICODE)
Definition WinSock2.h:175
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:208
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:190
static DWORD FormatMessageW(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, std::basic_string< wchar_t, _Traits, _Ax > &str, va_list *Arguments)
Formats a message string.
Definition Common.h:703
static int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, std::basic_string< char, _Traits, _Ax > &sMultiByteStr, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar) noexcept
Maps a UTF-16 (wide character) string to a std::string. The new character string is not necessarily f...
Definition Common.h:348
static int sprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format,...)
Formats string using printf().
Definition Common.h:333
#define WINSTD_HANDLE_IMPL(C, T, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition Common.h:164
static const PADDRINFOA invalid
Definition Common.h:1034