WinStd
Additional templates and function helpers for Microsoft Windows using Standard C++ classes
WinSock2.h
Go to the documentation of this file.
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4 Copyright © 2016 GÉANT
5*/
6
12
13#pragma once
14
15#include "Common.h"
16#include <WinSock2.h>
17#include <ws2def.h>
18#include <WS2tcpip.h>
19
20namespace winstd
21{
24
29 {
30 public:
37 ws2_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<int>(num, msg)
38 {
39 }
40
47 ws2_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) : num_runtime_error<int>(num, msg)
48 {
49 }
50
56 ws2_runtime_error(_In_ const std::string& msg) : num_runtime_error<int>(WSAGetLastError(), msg)
57 {
58 }
59
65 ws2_runtime_error(_In_opt_z_ const char *msg = nullptr) : num_runtime_error<int>(WSAGetLastError(), msg)
66 {
67 }
68
74 tstring msg(_In_opt_ DWORD dwLanguageId = 0) const
75 {
76 tstring str;
77 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, m_num, dwLanguageId, str, NULL)) {
78 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
79 str.erase(str.find_last_not_of(_T(" \t\n\r\f\v")) + 1);
80 } else
81 sprintf(str, m_num >= 0x10000 ? _T("Error 0x%X") : _T("Error %u"), m_num);
82 return str;
83 }
84 };
85
87
90
91#if (NTDDI_VERSION >= NTDDI_WINXPSP2) || (_WIN32_WINNT >= 0x0502)
92
98 class addrinfo : public handle<PADDRINFOA, NULL>
99 {
101
102 public:
108 virtual ~addrinfo()
109 {
110 if (m_h != invalid)
112 }
113
114 protected:
120 void free_internal() noexcept override
121 {
122 FreeAddrInfoA(m_h);
123 }
124 };
125
131 class waddrinfo : public handle<PADDRINFOW, NULL>
132 {
134
135 public:
141 virtual ~waddrinfo()
142 {
143 if (m_h != invalid)
145 }
146
147 protected:
153 void free_internal() noexcept override
154 {
155 FreeAddrInfoW(m_h);
156 }
157 };
158
162#ifdef _UNICODE
163 typedef waddrinfo taddrinfo;
164#else
166#endif
167
168#endif
169
171}
172
175
176#pragma warning(push)
177#pragma warning(disable: 4505) // Don't warn on unused code
178
180static INT GetAddrInfoA(
181 _In_opt_ PCSTR pNodeName,
182 _In_opt_ PCSTR pServiceName,
183 _In_opt_ const ADDRINFOA *pHints,
184 _Inout_ winstd::addrinfo &result)
185{
186 PADDRINFOA h;
187 INT iResult = GetAddrInfoA(pNodeName, pServiceName, pHints, &h);
188 if (iResult == 0)
189 result.attach(h);
190 return iResult;
191}
192
198static INT GetAddrInfoW(
199 _In_opt_ PCWSTR pNodeName,
200 _In_opt_ PCWSTR pServiceName,
201 _In_opt_ const ADDRINFOW *pHints,
202 _Inout_ winstd::waddrinfo &result)
203{
204 PADDRINFOW h;
205 INT iResult = GetAddrInfoW(pNodeName, pServiceName, pHints, &h);
206 if (iResult == 0)
207 result.attach(h);
208 return iResult;
209}
210
211#pragma warning(pop)
212
General API.
Integrates WinStd classes with Microsoft WinSock2 API.
SID wrapper class.
Definition: WinSock2.h:99
void free_internal() noexcept override
Frees address information.
Definition: WinSock2.h:120
virtual ~addrinfo()
Frees address information.
Definition: WinSock2.h:108
Base abstract template class to support generic object handle keeping.
Definition: Common.h:615
handle_type m_h
Object handle.
Definition: Common.h:866
Numerical runtime error.
Definition: Common.h:1011
int error_type
Error number type.
Definition: Common.h:1013
error_type m_num
Numeric error code.
Definition: Common.h:1049
SID wrapper class.
Definition: WinSock2.h:132
virtual ~waddrinfo()
Frees address information.
Definition: WinSock2.h:141
void free_internal() noexcept override
Frees address information.
Definition: WinSock2.h:153
WinSock2 runtime error.
Definition: WinSock2.h:29
ws2_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition: WinSock2.h:47
ws2_runtime_error(const char *msg=nullptr)
Constructs an exception using WSAGetLastError()
Definition: WinSock2.h:65
ws2_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition: WinSock2.h:37
ws2_runtime_error(const std::string &msg)
Constructs an exception using WSAGetLastError()
Definition: WinSock2.h:56
tstring msg(DWORD dwLanguageId=0) const
Returns a user-readable Windows error message.
Definition: WinSock2.h:74
addrinfo taddrinfo
Multi-byte / Wide-character SID wrapper class (according to _UNICODE)
Definition: WinSock2.h:165
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:198
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:180
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition: Common.h:346
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:307
static int sprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format,...)
Formats string using printf().
Definition: Common.h:292
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:171
static const PADDRINFOA invalid
Invalid handle value.
Definition: Common.h:625