2013-01-30 21:33:52 +01:00
|
|
|
/*
|
2021-01-11 22:29:39 +01:00
|
|
|
* Copyright 2015-2021 Max Kellermann <max.kellermann@gmail.com>
|
2013-01-30 21:33:52 +01:00
|
|
|
*
|
2021-01-11 22:29:39 +01:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
2013-01-30 21:33:52 +01:00
|
|
|
*
|
2021-01-11 22:29:39 +01:00
|
|
|
* - Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2013-01-30 21:33:52 +01:00
|
|
|
*
|
2021-01-11 22:29:39 +01:00
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
2013-01-30 21:33:52 +01:00
|
|
|
*/
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "SocketError.hxx"
|
2019-08-03 13:10:49 +02:00
|
|
|
|
|
|
|
#include <iterator>
|
2013-01-30 21:33:52 +01:00
|
|
|
|
2013-12-15 18:30:25 +01:00
|
|
|
#include <string.h>
|
2013-09-04 23:36:30 +02:00
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2013-09-04 23:36:30 +02:00
|
|
|
|
2017-05-08 14:44:49 +02:00
|
|
|
SocketErrorMessage::SocketErrorMessage(socket_error_t code) noexcept
|
2013-09-04 23:36:30 +02:00
|
|
|
{
|
2015-02-25 16:01:46 +01:00
|
|
|
#ifdef _UNICODE
|
2020-09-22 20:40:18 +02:00
|
|
|
wchar_t buffer[msg_size];
|
2015-02-25 16:01:46 +01:00
|
|
|
#else
|
|
|
|
auto *buffer = msg;
|
|
|
|
#endif
|
|
|
|
|
2013-09-04 23:36:30 +02:00
|
|
|
DWORD nbytes = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS |
|
|
|
|
FORMAT_MESSAGE_MAX_WIDTH_MASK,
|
2015-02-25 16:01:46 +01:00
|
|
|
nullptr, code, 0,
|
2020-09-22 20:40:18 +02:00
|
|
|
buffer, msg_size, nullptr);
|
2015-02-25 16:01:46 +01:00
|
|
|
if (nbytes == 0) {
|
2013-09-04 23:36:30 +02:00
|
|
|
strcpy(msg, "Unknown error");
|
2015-02-25 16:01:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _UNICODE
|
|
|
|
auto length = WideCharToMultiByte(CP_UTF8, 0, buffer, -1,
|
2019-08-03 13:10:49 +02:00
|
|
|
msg, std::size(msg),
|
2015-02-25 16:01:46 +01:00
|
|
|
nullptr, nullptr);
|
|
|
|
if (length <= 0) {
|
|
|
|
strcpy(msg, "WideCharToMultiByte() error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2013-09-04 23:36:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-05-08 14:44:49 +02:00
|
|
|
SocketErrorMessage::SocketErrorMessage(socket_error_t code) noexcept
|
2013-12-15 18:30:25 +01:00
|
|
|
:msg(strerror(code)) {}
|
2013-09-04 23:36:30 +02:00
|
|
|
|
|
|
|
#endif
|