1 | /** \file socket_include.h |
---|
2 | ** \date 2005-04-12 |
---|
3 | ** \author grymse@alhem.net |
---|
4 | **/ |
---|
5 | /* |
---|
6 | Copyright (C) 2004-2007 Anders Hedstrom |
---|
7 | |
---|
8 | This library is made available under the terms of the GNU GPL. |
---|
9 | |
---|
10 | If you would like to use this library in a closed-source application, |
---|
11 | a separate license agreement is available. For information about |
---|
12 | the closed-source license agreement for the C++ sockets library, |
---|
13 | please visit http://www.alhem.net/Sockets/license.html and/or |
---|
14 | email license@alhem.net. |
---|
15 | |
---|
16 | This program is free software; you can redistribute it and/or |
---|
17 | modify it under the terms of the GNU General Public License |
---|
18 | as published by the Free Software Foundation; either version 2 |
---|
19 | of the License, or (at your option) any later version. |
---|
20 | |
---|
21 | This program is distributed in the hope that it will be useful, |
---|
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
24 | GNU General Public License for more details. |
---|
25 | |
---|
26 | You should have received a copy of the GNU General Public License |
---|
27 | along with this program; if not, write to the Free Software |
---|
28 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
29 | */ |
---|
30 | #ifndef _SOCKETS_socket_include_H |
---|
31 | #define _SOCKETS_socket_include_H |
---|
32 | #include "sockets-config.h" |
---|
33 | |
---|
34 | #ifdef _MSC_VER |
---|
35 | #pragma warning(disable:4514) |
---|
36 | #endif |
---|
37 | |
---|
38 | // common defines affecting library and applications using library |
---|
39 | |
---|
40 | /* Define SOCKETS_DYNAMIC_TEMP to use dynamically allocated buffers |
---|
41 | in read operations - helps on ECOS */ |
---|
42 | #define SOCKETS_DYNAMIC_TEMP |
---|
43 | |
---|
44 | |
---|
45 | // platform specific stuff |
---|
46 | #if (defined(__unix__) || defined(unix)) && !defined(USG) |
---|
47 | #include <sys/param.h> |
---|
48 | #endif |
---|
49 | #include <list> |
---|
50 | |
---|
51 | // int64 |
---|
52 | #ifdef _WIN32 |
---|
53 | typedef unsigned __int64 uint64_t; |
---|
54 | #else |
---|
55 | #include <stdlib.h> |
---|
56 | #ifdef SOLARIS |
---|
57 | # include <sys/types.h> |
---|
58 | #else |
---|
59 | # include <stdint.h> |
---|
60 | #endif |
---|
61 | #endif |
---|
62 | |
---|
63 | #ifndef _WIN32 |
---|
64 | // ---------------------------------------- |
---|
65 | // common unix includes / defines |
---|
66 | #include <unistd.h> |
---|
67 | #include <sys/time.h> |
---|
68 | #include <sys/types.h> |
---|
69 | #include <sys/socket.h> |
---|
70 | #include <netinet/in.h> |
---|
71 | #include <arpa/inet.h> |
---|
72 | //#include <netdb.h> |
---|
73 | |
---|
74 | // all typedefs in this file will be declared outside the sockets namespace, |
---|
75 | // because some os's will already have one or more of the type defined. |
---|
76 | typedef int SOCKET; |
---|
77 | #define Errno errno |
---|
78 | #define StrError strerror |
---|
79 | |
---|
80 | #ifdef SOCKETS_NAMESPACE |
---|
81 | namespace SOCKETS_NAMESPACE { |
---|
82 | #endif |
---|
83 | |
---|
84 | |
---|
85 | // WIN32 adapt |
---|
86 | #define closesocket close |
---|
87 | #define INVALID_SOCKET -1 |
---|
88 | #define SOCKET_ERROR -1 |
---|
89 | |
---|
90 | #ifndef INADDR_NONE |
---|
91 | #define INADDR_NONE ((unsigned long) -1) |
---|
92 | #endif // INADDR_NONE |
---|
93 | |
---|
94 | #ifdef SOCKETS_NAMESPACE |
---|
95 | } |
---|
96 | #endif |
---|
97 | |
---|
98 | #endif // !_WIN32 |
---|
99 | |
---|
100 | |
---|
101 | // ---------------------------------------- |
---|
102 | // Generic |
---|
103 | #ifndef SOL_IP |
---|
104 | #define SOL_IP IPPROTO_IP |
---|
105 | #endif |
---|
106 | |
---|
107 | |
---|
108 | // ---------------------------------------- |
---|
109 | // OS specific adaptions |
---|
110 | |
---|
111 | #ifdef SOLARIS |
---|
112 | // ---------------------------------------- |
---|
113 | // Solaris |
---|
114 | typedef unsigned short port_t; |
---|
115 | #ifdef SOCKETS_NAMESPACE |
---|
116 | namespace SOCKETS_NAMESPACE { |
---|
117 | #endif |
---|
118 | // no defs |
---|
119 | |
---|
120 | #ifdef SOCKETS_NAMESPACE |
---|
121 | } |
---|
122 | #endif |
---|
123 | |
---|
124 | #define s6_addr16 _S6_un._S6_u8 |
---|
125 | #define MSG_NOSIGNAL 0 |
---|
126 | |
---|
127 | #elif defined __FreeBSD__ |
---|
128 | // ---------------------------------------- |
---|
129 | // FreeBSD |
---|
130 | # if __FreeBSD_version >= 400014 |
---|
131 | # define s6_addr16 __u6_addr.__u6_addr16 |
---|
132 | # if !defined(MSG_NOSIGNAL) |
---|
133 | # define MSG_NOSIGNAL 0 |
---|
134 | # endif |
---|
135 | # include <netinet/in.h> |
---|
136 | typedef in_addr_t ipaddr_t; |
---|
137 | typedef in_port_t port_t; |
---|
138 | #ifdef SOCKETS_NAMESPACE |
---|
139 | namespace SOCKETS_NAMESPACE { |
---|
140 | #endif |
---|
141 | // no defs |
---|
142 | |
---|
143 | #ifdef SOCKETS_NAMESPACE |
---|
144 | } |
---|
145 | #endif |
---|
146 | |
---|
147 | # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP |
---|
148 | # define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP |
---|
149 | # else |
---|
150 | # error FreeBSD versions prior to 400014 does not support ipv6 |
---|
151 | # endif |
---|
152 | |
---|
153 | #elif defined __NetBSD__ |
---|
154 | # if !defined(MSG_NOSIGNAL) |
---|
155 | # define MSG_NOSIGNAL 0 |
---|
156 | # endif |
---|
157 | # include <netinet/in.h> |
---|
158 | typedef in_addr_t ipaddr_t; |
---|
159 | typedef in_port_t port_t; |
---|
160 | #elif defined MACOSX |
---|
161 | // ---------------------------------------- |
---|
162 | // Mac OS X |
---|
163 | #include <string.h> |
---|
164 | #ifdef __DARWIN_UNIX03 |
---|
165 | typedef unsigned short port_t; |
---|
166 | #else |
---|
167 | #include <mach/port.h> |
---|
168 | #endif // __DARWIN_UNIX03 |
---|
169 | typedef unsigned long ipaddr_t; |
---|
170 | #ifdef SOCKETS_NAMESPACE |
---|
171 | namespace SOCKETS_NAMESPACE { |
---|
172 | #endif |
---|
173 | // no defs |
---|
174 | |
---|
175 | #ifdef SOCKETS_NAMESPACE |
---|
176 | } |
---|
177 | #endif |
---|
178 | |
---|
179 | #define s6_addr16 __u6_addr.__u6_addr16 |
---|
180 | #define MSG_NOSIGNAL 0 // oops - thanks Derek |
---|
181 | #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP |
---|
182 | #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP |
---|
183 | |
---|
184 | #elif defined _WIN32 |
---|
185 | // ---------------------------------------- |
---|
186 | // Win32 |
---|
187 | #ifdef _MSC_VER |
---|
188 | #pragma comment(lib, "wsock32.lib") |
---|
189 | #endif |
---|
190 | #define strcasecmp _stricmp |
---|
191 | |
---|
192 | typedef unsigned long ipaddr_t; |
---|
193 | typedef unsigned short port_t; |
---|
194 | typedef int socklen_t; |
---|
195 | #ifdef SOCKETS_NAMESPACE |
---|
196 | namespace SOCKETS_NAMESPACE { |
---|
197 | #endif |
---|
198 | // no defs |
---|
199 | |
---|
200 | #ifdef SOCKETS_NAMESPACE |
---|
201 | } |
---|
202 | #endif |
---|
203 | |
---|
204 | // 1.8.6: define FD_SETSIZE to something bigger than 64 if there are a lot of |
---|
205 | // simultaneous connections (must be done before including winsock.h) |
---|
206 | #define FD_SETSIZE 1024 |
---|
207 | |
---|
208 | // windows 2000 with ipv6 preview installed: |
---|
209 | // http://msdn.microsoft.com/downloads/sdks/platform/tpipv6.asp |
---|
210 | // see the FAQ on how to install |
---|
211 | #define WIN32_LEAN_AND_MEAN |
---|
212 | #include <winsock2.h> |
---|
213 | #include <ws2tcpip.h> |
---|
214 | #if _MSC_VER < 1200 |
---|
215 | #ifndef __CYGWIN__ |
---|
216 | #ifdef ENABLE_IPV6 |
---|
217 | #include <tpipv6.h> // For IPv6 Tech Preview. |
---|
218 | #endif |
---|
219 | #endif |
---|
220 | #endif // _MSC_VER < 1200 |
---|
221 | |
---|
222 | |
---|
223 | #define MSG_NOSIGNAL 0 |
---|
224 | //#define SHUT_RDWR 2 |
---|
225 | #define SHUT_WR 1 |
---|
226 | |
---|
227 | #define Errno WSAGetLastError() |
---|
228 | const char *StrError(int x); |
---|
229 | |
---|
230 | #ifdef SOCKETS_NAMESPACE |
---|
231 | namespace SOCKETS_NAMESPACE { |
---|
232 | #endif |
---|
233 | |
---|
234 | |
---|
235 | // class WSAInitializer is a part of the Socket class (on win32) |
---|
236 | // as a static instance - so whenever an application uses a Socket, |
---|
237 | // winsock is initialized |
---|
238 | class WSAInitializer // Winsock Initializer |
---|
239 | { |
---|
240 | public: |
---|
241 | WSAInitializer() { |
---|
242 | if (WSAStartup(0x101,&m_wsadata)) |
---|
243 | { |
---|
244 | exit(-1); |
---|
245 | } |
---|
246 | } |
---|
247 | ~WSAInitializer() { |
---|
248 | WSACleanup(); |
---|
249 | } |
---|
250 | private: |
---|
251 | WSADATA m_wsadata; |
---|
252 | }; |
---|
253 | |
---|
254 | #ifdef SOCKETS_NAMESPACE |
---|
255 | } |
---|
256 | #endif |
---|
257 | |
---|
258 | #else |
---|
259 | // ---------------------------------------- |
---|
260 | // LINUX |
---|
261 | typedef unsigned long ipaddr_t; |
---|
262 | typedef unsigned short port_t; |
---|
263 | #ifdef SOCKETS_NAMESPACE |
---|
264 | namespace SOCKETS_NAMESPACE { |
---|
265 | #endif |
---|
266 | // no defs |
---|
267 | |
---|
268 | #ifdef SOCKETS_NAMESPACE |
---|
269 | } |
---|
270 | #endif |
---|
271 | |
---|
272 | |
---|
273 | #endif |
---|
274 | |
---|
275 | #ifdef SOCKETS_NAMESPACE |
---|
276 | namespace SOCKETS_NAMESPACE { |
---|
277 | #endif |
---|
278 | /** List type containing file descriptors. */ |
---|
279 | typedef std::list<SOCKET> socket_v; |
---|
280 | |
---|
281 | |
---|
282 | #ifdef SOCKETS_NAMESPACE |
---|
283 | } |
---|
284 | #endif |
---|
285 | |
---|
286 | |
---|
287 | // getaddrinfo / getnameinfo replacements |
---|
288 | #ifdef NO_GETADDRINFO |
---|
289 | #ifndef AI_NUMERICHOST |
---|
290 | #define AI_NUMERICHOST 1 |
---|
291 | #endif |
---|
292 | #ifndef NI_NUMERICHOST |
---|
293 | #define NI_NUMERICHOST 1 |
---|
294 | #endif |
---|
295 | #endif |
---|
296 | |
---|
297 | |
---|
298 | #endif // _SOCKETS_socket_include_H |
---|
299 | |
---|