WinStd
Additional templates and function helpers for Microsoft Windows using Standard C++ classes
WinSock2.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4 Copyright © 2016 GÉANT
5*/
6
7#pragma once
8
9#include "Common.h"
10#include <WinSock2.h>
11#include <ws2def.h>
12#include <WS2tcpip.h>
13
14namespace winstd
15{
21
26 {
27 public:
34 ws2_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<int>(num, msg)
35 {
36 }
37
44 ws2_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) : num_runtime_error<int>(num, msg)
45 {
46 }
47
53 ws2_runtime_error(_In_ const std::string& msg) : num_runtime_error<int>(WSAGetLastError(), msg)
54 {
55 }
56
62 ws2_runtime_error(_In_opt_z_ const char *msg = nullptr) : num_runtime_error<int>(WSAGetLastError(), msg)
63 {
64 }
65
71 tstring msg(_In_opt_ DWORD dwLanguageId = 0) const
72 {
73 tstring str;
74 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, m_num, dwLanguageId, str, NULL)) {
75 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
76 str.erase(str.find_last_not_of(_T(" \t\n\r\f\v")) + 1);
77 } else
78 sprintf(str, m_num >= 0x10000 ? _T("Error 0x%X") : _T("Error %u"), m_num);
79 return str;
80 }
81 };
82
84
87
88#if (NTDDI_VERSION >= NTDDI_WINXPSP2) || (_WIN32_WINNT >= 0x0502)
89
93 class addrinfo : public handle<PADDRINFOA, NULL>
94 {
96
97 public:
103 __declspec(deprecated("Use GetAddrInfoA"))
104 bool get(
105 _In_opt_ PCSTR pNodeName,
106 _In_opt_ PCSTR pServiceName,
107 _In_opt_ const ADDRINFOA *pHints)
108 {
109 handle_type h;
110 if (GetAddrInfoA(pNodeName, pServiceName, pHints, &h) == 0) {
111 attach(h);
112 return true;
113 } else
114 return false;
115 }
116
122 virtual ~addrinfo()
123 {
124 if (m_h != invalid)
126 }
127
128 protected:
134 void free_internal() noexcept override
135 {
136 FreeAddrInfoA(m_h);
137 }
138 };
139
143 class waddrinfo : public handle<PADDRINFOW, NULL>
144 {
146
147 public:
153 __declspec(deprecated("Use GetAddrInfoW"))
154 bool get(
155 _In_opt_ PCWSTR pNodeName,
156 _In_opt_ PCWSTR pServiceName,
157 _In_opt_ const ADDRINFOW *pHints)
158 {
159 handle_type h;
160 if (GetAddrInfoW(pNodeName, pServiceName, pHints, &h) == 0) {
161 attach(h);
162 return true;
163 } else
164 return false;
165 }
166
172 virtual ~waddrinfo()
173 {
174 if (m_h != invalid)
176 }
177
178 protected:
184 void free_internal() noexcept override
185 {
186 FreeAddrInfoW(m_h);
187 }
188 };
189
193#ifdef _UNICODE
194 typedef waddrinfo taddrinfo;
195#else
197#endif
198
199#endif
200
202}
203
206
207#pragma warning(push)
208#pragma warning(disable: 4505) // Don't warn on unused code
209
211static INT GetAddrInfoA(
212 _In_opt_ PCSTR pNodeName,
213 _In_opt_ PCSTR pServiceName,
214 _In_opt_ const ADDRINFOA *pHints,
215 _Inout_ winstd::addrinfo &result)
216{
217 PADDRINFOA h;
218 INT iResult = GetAddrInfoA(pNodeName, pServiceName, pHints, &h);
219 if (iResult == 0)
220 result.attach(h);
221 return iResult;
222}
223
229static INT GetAddrInfoW(
230 _In_opt_ PCWSTR pNodeName,
231 _In_opt_ PCWSTR pServiceName,
232 _In_opt_ const ADDRINFOW *pHints,
233 _Inout_ winstd::waddrinfo &result)
234{
235 PADDRINFOW h;
236 INT iResult = GetAddrInfoW(pNodeName, pServiceName, pHints, &h);
237 if (iResult == 0)
238 result.attach(h);
239 return iResult;
240}
241
242#pragma warning(pop)
243
SID wrapper class.
Definition: WinSock2.h:94
void free_internal() noexcept override
Frees address information.
Definition: WinSock2.h:134
virtual ~addrinfo()
Frees address information.
Definition: WinSock2.h:122
__declspec(deprecated("Use GetAddrInfoA")) bool get(PCSTR pNodeName
Provides protocol-independent translation from a host name to an address.
Base abstract template class to support generic object handle keeping.
Definition: Common.h:603
PADDRINFOA handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:608
handle_type m_h
Object handle.
Definition: Common.h:854
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:817
Numerical runtime error.
Definition: Common.h:1002
int error_type
Error number type.
Definition: Common.h:1004
error_type m_num
Numeric error code.
Definition: Common.h:1040
SID wrapper class.
Definition: WinSock2.h:144
virtual ~waddrinfo()
Frees address information.
Definition: WinSock2.h:172
void free_internal() noexcept override
Frees address information.
Definition: WinSock2.h:184
__declspec(deprecated("Use GetAddrInfoW")) bool get(PCWSTR pNodeName
Provides protocol-independent translation from a host name to an address.
WinSock2 runtime error.
Definition: WinSock2.h:26
ws2_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition: WinSock2.h:44
ws2_runtime_error(const char *msg=nullptr)
Constructs an exception using WSAGetLastError()
Definition: WinSock2.h:62
ws2_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition: WinSock2.h:34
ws2_runtime_error(const std::string &msg)
Constructs an exception using WSAGetLastError()
Definition: WinSock2.h:53
tstring msg(DWORD dwLanguageId=0) const
Returns a user-readable Windows error message.
Definition: WinSock2.h:71
addrinfo taddrinfo
Multi-byte / Wide-character SID wrapper class (according to _UNICODE)
Definition: WinSock2.h:196
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition: Common.h:334
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:161
static const PADDRINFOA invalid
Invalid handle value.
Definition: Common.h:613