libvisiontransfer  6.1.0
networking.h
1 /*******************************************************************************
2  * Copyright (c) 2018 Nerian Vision GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 /*******************************************************************************
16  * This header file contains include statements and definitions for simplifying
17  * cross platform network development
18 *******************************************************************************/
19 
20 #ifndef VISIONTRANSFER_NETWORKING_H
21 #define VISIONTRANSFER_NETWORKING_H
22 
23 #ifdef _MSC_VER
24  // Visual studio does not come with snprintf
25  #define snprintf _snprintf_s
26 #endif
27 
28 // Network headers
29 #ifdef _WIN32
30  #ifndef _WIN32_WINNT
31  #define _WIN32_WINNT 0x501
32  #endif
33  #define _WINSOCK_DEPRECATED_NO_WARNINGS
34 
35  #ifndef NOMINMAX
36  #define NOMINMAX
37  #endif
38 
39  #include <winsock2.h>
40  #include <ws2tcpip.h>
41 
42  #include <Ipmib.h>
43  #include <Iprtrmib.h>
44  #include <Iphlpapi.h>
45 
46  // Some defines to make windows socket look more like
47  // posix sockets.
48  #ifdef EWOULDBLOCK
49  #undef EWOULDBLOCK
50  #endif
51  #ifdef ECONNRESET
52  #undef ECONNRESET
53  #endif
54  #ifdef ETIMEDOUT
55  #undef ETIMEDOUT
56  #endif
57  #ifdef EPIPE
58  #undef EPIPE
59  #endif
60 
61  #define EWOULDBLOCK WSAEWOULDBLOCK
62  #define ECONNRESET WSAECONNRESET
63  #define ETIMEDOUT WSAETIMEDOUT
64  #define EPIPE WSAECONNABORTED
65  #define MSG_DONTWAIT 0
66  #define SHUT_WR SD_BOTH
67 
68  #define close closesocket
69 
70  // Emulate posix errno. Does not work in a throw
71  // statement (WTF?)
72  #undef errno
73  #define errno WSAGetLastError()
74  #define strerror win_strerror
75 
76  inline std::string win_strerror(unsigned long error) {
77  char* str = nullptr;
78  if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
79  FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
80  nullptr, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
81  (LPSTR)&str, 0, nullptr) == 0 || str == nullptr) {
82  return "Unknown error";
83  } else {
84  char buffer[512];
85  snprintf(buffer, sizeof(buffer), "%s (%lu)", str, error);
86  LocalFree(str);
87  return std::string(buffer);
88  }
89  }
90 
91 
92  typedef int socklen_t;
93 
94 #else
95  #include <arpa/inet.h>
96  #include <netinet/tcp.h>
97  #include <sys/types.h>
98  #include <sys/socket.h>
99  #include <sys/select.h>
100  #include <netdb.h>
101  #include <netinet/in.h>
102  #include <errno.h>
103  #include <unistd.h>
104  #include <signal.h>
105  #include <ifaddrs.h>
106 
107  // Unfortunately we have to use a winsock like socket type
108  typedef int SOCKET;
109  #define INVALID_SOCKET -1
110 
111  // Also we need some additional winsock defines
112  #define WSA_IO_PENDING 0
113  #define WSAECONNRESET 0
114 #endif
115 
116 #endif
Nerian Vision Technologies