-
Notifications
You must be signed in to change notification settings - Fork 0
/
socks.h
97 lines (79 loc) · 1.91 KB
/
socks.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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