summaryrefslogtreecommitdiffstats
path: root/xmrstak/net/socks.hpp
diff options
context:
space:
mode:
authorpsychocrypt <psychocrypt@users.noreply.github.com>2017-09-29 20:32:31 +0200
committerpsychocrypt <psychocrypt@users.noreply.github.com>2017-09-30 23:46:08 +0200
commitcc429b68fadc502b981fd0acd64a5ff6e2ae1d15 (patch)
tree3fb23fc4db15dbdd08af4c7ea20134b9d82e58fd /xmrstak/net/socks.hpp
parente5b0319d5a9f58762fa934ad700113908940cb31 (diff)
downloadxmr-stak-cc429b68fadc502b981fd0acd64a5ff6e2ae1d15.zip
xmr-stak-cc429b68fadc502b981fd0acd64a5ff6e2ae1d15.tar.gz
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`
Diffstat (limited to 'xmrstak/net/socks.hpp')
-rw-r--r--xmrstak/net/socks.hpp97
1 files changed, 97 insertions, 0 deletions
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 <winsock2.h>
+#include <ws2tcpip.h>
+#include <windows.h>
+
+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 <sys/socket.h>
+#include <arpa/inet.h>
+#include <netdb.h> /* Needed for getaddrinfo() and freeaddrinfo() */
+#include <unistd.h> /* Needed for close() */
+#include <errno.h>
+#include <string.h>
+#if defined(__FreeBSD__)
+#include <netinet/in.h> /* 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
OpenPOWER on IntegriCloud