-
Notifications
You must be signed in to change notification settings - Fork 2
/
SocketDefs.h
113 lines (92 loc) · 3.03 KB
/
SocketDefs.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2015-2019 Josh Blum
// SPDX-License-Identifier: BSL-1.0
// ** Modified from SoapyRemote/common/SoapySocketDefs.hpp
// ** This header should be included first, to avoid compile errors.
// ** At least in the case of the windows header files.
// This header helps to abstract network differences between platforms.
// Including the correct headers for various network APIs.
// And providing various typedefs and definitions when missing.
#pragma once
/***********************************************************************
* Windows socket headers
**********************************************************************/
#ifdef _MSC_VER
#include <winsock2.h> //htonll
#include <ws2tcpip.h> //addrinfo
typedef int socklen_t;
#include <io.h> //read/write
#else
/***********************************************************************
* unix socket headers
**********************************************************************/
#ifndef __has_include
#error "Compiler missing __has_include macro!"
#endif
#if __has_include(<unistd.h>)
#include <unistd.h> //close
#define closesocket close
#endif
#if __has_include(<netdb.h>)
#include <netdb.h> //addrinfo
#endif
#if __has_include(<netinet/in.h>)
#include <netinet/in.h>
#endif
#if __has_include(<netinet/tcp.h>)
#include <netinet/tcp.h>
#endif
#if __has_include(<sys/types.h>)
#include <sys/types.h>
#endif
#if __has_include(<sys/socket.h>)
#include <sys/socket.h>
#endif
#if __has_include(<arpa/inet.h>)
#include <arpa/inet.h> //inet_ntop
#endif
#if __has_include(<ifaddrs.h>)
#include <ifaddrs.h> //getifaddrs
#endif
#if __has_include(<net/if.h>)
#include <net/if.h> //if_nametoindex
#endif
#if __has_include(<fcntl.h>)
#include <fcntl.h> //fcntl and constants
#endif
#endif
/***********************************************************************
* socket type definitions
**********************************************************************/
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif //INVALID_SOCKET
#ifndef _MSC_VER
#define SOCKET int
#endif //SOCKET
/***********************************************************************
* socket errno
**********************************************************************/
#ifdef _MSC_VER
#define SOCKET_ERRNO WSAGetLastError()
#define SOCKET_EINPROGRESS WSAEWOULDBLOCK
#define SOCKET_ETIMEDOUT WSAETIMEDOUT
#define SOCKET_ECONNREFUSED WSAECONNREFUSED
static inline const char *socket_strerror(const int err)
{
static thread_local char buff[1024];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buff, sizeof(buff), NULL);
return buff;
}
#else
#define SOCKET_ERRNO errno
#define SOCKET_EINPROGRESS EINPROGRESS
#define SOCKET_ETIMEDOUT ETIMEDOUT
#define SOCKET_ECONNREFUSED ECONNREFUSED
#define socket_strerror strerror
#endif
/***********************************************************************
* socket flag definitions
**********************************************************************/
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif