TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
networkHelpers.h
Go to the documentation of this file.
1 // Included by NetworkDevice.cpp and TCPConduit.cpp to provide a platform-independent networking base
2 
3 #include <cstring>
4 #include <stdlib.h>
5 #include <time.h>
6 
7 #if defined(G3D_LINUX) || defined(G3D_OSX) || defined(G3D_FREEBSD)
8 # include <sys/types.h>
9 # include <sys/socket.h>
10 # include <ifaddrs.h>
11 # include <netinet/in.h>
12 # include <net/if.h>
13 # ifdef __linux__
14 # include <sys/ioctl.h>
15 # include <netinet/in.h>
16 # include <unistd.h>
17 # include <string.h>
18 // Match Linux to FreeBSD
19 # define AF_LINK AF_PACKET
20 # else
21 # include <net/if_dl.h>
22 # include <sys/sockio.h>
23 # endif
24 
25  #include <unistd.h>
26  #include <errno.h>
27  #include <sys/socket.h>
28  #include <netinet/in.h>
29  #include <arpa/inet.h>
30  #include <netdb.h>
31  #include <netinet/tcp.h>
32  #include <sys/ioctl.h>
33  #include <netinet/if_ether.h>
34  #include <net/ethernet.h>
35  #include <net/if.h>
36 
37  #include <sys/types.h>
38 
39  #define _alloca alloca
40 
42  int WSAGetLastError() {
43  return -1;
44  }
45 
46  #define SOCKET_ERROR -1
47 
48  static std::string socketErrorCode(int code) {
49  return G3D::format("CODE %d: %s\n", code, strerror(code));
50  }
51 
52  static std::string socketErrorCode() {
53  return socketErrorCode(errno);
54  }
55 
56  static const int WSAEWOULDBLOCK = -100;
57 
58  typedef int SOCKET;
59  typedef struct sockaddr_in SOCKADDR_IN;
60 
61 #else
62 
63  // Windows
64  static std::string socketErrorCode(int code) {
65  LPTSTR formatMsg = NULL;
66 
67  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
68  FORMAT_MESSAGE_IGNORE_INSERTS |
69  FORMAT_MESSAGE_FROM_SYSTEM,
70  NULL,
71  code,
72  0,
73  (LPTSTR)&formatMsg,
74  0,
75  NULL);
76 
77  return G3D::format("CODE %d: %s\n", code, formatMsg);
78  }
79 
80  static std::string socketErrorCode() {
81  return socketErrorCode(GetLastError());
82  }
83 
84 #endif
85 
86 
87 #ifndef _SOCKLEN_T
88 # if defined(G3D_WINDOWS) || defined(G3D_OSX)
89  typedef int socklen_t;
90 # endif
91 #endif
static std::string socketErrorCode(int code)
Definition: networkHelpers.h:64
arena_t NULL
Definition: jemalloc_internal.h:624
#define SOCKADDR_IN
Definition: netheaders.h:17
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS
Definition: inftrees.h:24
int GetLastError()
Definition: Common.cpp:70
#define SOCKET
Definition: netheaders.h:20