From cc429b68fadc502b981fd0acd64a5ff6e2ae1d15 Mon Sep 17 00:00:00 2001 From: psychocrypt Date: Fri, 29 Sep 2017 20:32:31 +0200 Subject: group files - move source code to `src` - categorize files and move to group folder - change upper case class files to lower case - change C++ header to `*.hpp` --- xmrstak/net/socks.hpp | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 xmrstak/net/socks.hpp (limited to 'xmrstak/net/socks.hpp') diff --git a/xmrstak/net/socks.hpp b/xmrstak/net/socks.hpp new file mode 100644 index 0000000..82bfa2f --- /dev/null +++ b/xmrstak/net/socks.hpp @@ -0,0 +1,97 @@ +#pragma once +#ifdef _WIN32 +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x0601 /* Windows 7 */ +#endif +#include +#include +#include + +inline void sock_init() +{ + static bool bWSAInit = false; + + if (!bWSAInit) + { + WSADATA wsaData; + WSAStartup(MAKEWORD(2, 2), &wsaData); + bWSAInit = true; + } +} + +inline void sock_close(SOCKET s) +{ + shutdown(s, SD_BOTH); + closesocket(s); +} + +inline const char* sock_strerror(char* buf, size_t len) +{ + buf[0] = '\0'; + + FormatMessageA( + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK, + NULL, WSAGetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPSTR)buf, len, NULL); + + return buf; +} + +inline const char* sock_gai_strerror(int err, char* buf, size_t len) +{ + buf[0] = '\0'; + + FormatMessageA( + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK, + NULL, (DWORD)err, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPSTR)buf, len, NULL); + + return buf; +} + +#else + +/* Assume that any non-Windows platform uses POSIX-style sockets instead. */ +#include +#include +#include /* Needed for getaddrinfo() and freeaddrinfo() */ +#include /* Needed for close() */ +#include +#include +#if defined(__FreeBSD__) +#include /* Needed for IPPROTO_TCP */ +#endif + +inline void sock_init() {} +typedef int SOCKET; + +#define INVALID_SOCKET (-1) +#define SOCKET_ERROR (-1) + +inline void sock_close(SOCKET s) +{ + shutdown(s, SHUT_RDWR); + close(s); +} + +inline const char* sock_strerror(char* buf, size_t len) +{ + buf[0] = '\0'; + +#if defined(__APPLE__) || defined(__FreeBSD__) || !defined(_GNU_SOURCE) || !defined(__GLIBC__) + + strerror_r(errno, buf, len); + return buf; +#else + return strerror_r(errno, buf, len); +#endif +} + +inline const char* sock_gai_strerror(int err, char* buf, size_t len) +{ + buf[0] = '\0'; + return gai_strerror(err); +} +#endif -- cgit v1.1