svn cleanup
[anytun.git] / Sockets / socket_include.h
blob213c8b35bc7c515b03e2e4338cfcfebcdf9f64fd
1 /** \file socket_include.h
2 ** \date 2005-04-12
3 ** \author grymse@alhem.net
4 **/
5 /*
6 Copyright (C) 2004-2007 Anders Hedstrom
8 This library is made available under the terms of the GNU GPL.
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.
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.
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.
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.
30 #ifndef _SOCKETS_socket_include_H
31 #define _SOCKETS_socket_include_H
32 #include "sockets-config.h"
34 #ifdef _MSC_VER
35 #pragma warning(disable:4514)
36 #endif
38 // common defines affecting library and applications using library
40 /* Define SOCKETS_DYNAMIC_TEMP to use dynamically allocated buffers
41 in read operations - helps on ECOS */
42 #define SOCKETS_DYNAMIC_TEMP
45 // platform specific stuff
46 #if (defined(__unix__) || defined(unix)) && !defined(USG)
47 #include <sys/param.h>
48 #endif
49 #include <list>
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
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>
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
80 #ifdef SOCKETS_NAMESPACE
81 namespace SOCKETS_NAMESPACE {
82 #endif
85 // WIN32 adapt
86 #define closesocket close
87 #define INVALID_SOCKET -1
88 #define SOCKET_ERROR -1
90 #ifndef INADDR_NONE
91 #define INADDR_NONE ((unsigned long) -1)
92 #endif // INADDR_NONE
94 #ifdef SOCKETS_NAMESPACE
96 #endif
98 #endif // !_WIN32
101 // ----------------------------------------
102 // Generic
103 #ifndef SOL_IP
104 #define SOL_IP IPPROTO_IP
105 #endif
108 // ----------------------------------------
109 // OS specific adaptions
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
120 #ifdef SOCKETS_NAMESPACE
122 #endif
124 #define s6_addr16 _S6_un._S6_u8
125 #define MSG_NOSIGNAL 0
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
143 #ifdef SOCKETS_NAMESPACE
145 #endif
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
153 #elif defined MACOSX
154 // ----------------------------------------
155 // Mac OS X
156 #include <string.h>
157 #include <mach/port.h>
158 typedef unsigned long ipaddr_t;
159 #ifdef SOCKETS_NAMESPACE
160 namespace SOCKETS_NAMESPACE {
161 #endif
162 // no defs
164 #ifdef SOCKETS_NAMESPACE
166 #endif
168 #define s6_addr16 __u6_addr.__u6_addr16
169 #define MSG_NOSIGNAL 0 // oops - thanks Derek
170 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
171 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
173 #elif defined _WIN32
174 // ----------------------------------------
175 // Win32
176 #ifdef _MSC_VER
177 #pragma comment(lib, "wsock32.lib")
178 #endif
179 #define strcasecmp _stricmp
181 typedef unsigned long ipaddr_t;
182 typedef unsigned short port_t;
183 typedef int socklen_t;
184 #ifdef SOCKETS_NAMESPACE
185 namespace SOCKETS_NAMESPACE {
186 #endif
187 // no defs
189 #ifdef SOCKETS_NAMESPACE
191 #endif
193 // 1.8.6: define FD_SETSIZE to something bigger than 64 if there are a lot of
194 // simultaneous connections (must be done before including winsock.h)
195 //#define FD_SETSIZE 1024
197 // windows 2000 with ipv6 preview installed:
198 // http://msdn.microsoft.com/downloads/sdks/platform/tpipv6.asp
199 // see the FAQ on how to install
200 #define WIN32_LEAN_AND_MEAN
201 #include <winsock2.h>
202 #include <ws2tcpip.h>
203 #if _MSC_VER < 1200
204 #ifndef __CYGWIN__
205 #ifdef ENABLE_IPV6
206 #include <tpipv6.h> // For IPv6 Tech Preview.
207 #endif
208 #endif
209 #endif // _MSC_VER < 1200
212 #define MSG_NOSIGNAL 0
213 //#define SHUT_RDWR 2
214 #define SHUT_WR 1
216 #define Errno WSAGetLastError()
217 const char *StrError(int x);
219 #ifdef SOCKETS_NAMESPACE
220 namespace SOCKETS_NAMESPACE {
221 #endif
224 // class WSAInitializer is a part of the Socket class (on win32)
225 // as a static instance - so whenever an application uses a Socket,
226 // winsock is initialized
227 class WSAInitializer // Winsock Initializer
229 public:
230 WSAInitializer() {
231 if (WSAStartup(0x101,&m_wsadata))
233 exit(-1);
236 ~WSAInitializer() {
237 WSACleanup();
239 private:
240 WSADATA m_wsadata;
243 #ifdef SOCKETS_NAMESPACE
245 #endif
247 #else
248 // ----------------------------------------
249 // LINUX
250 typedef unsigned long ipaddr_t;
251 typedef unsigned short port_t;
252 #ifdef SOCKETS_NAMESPACE
253 namespace SOCKETS_NAMESPACE {
254 #endif
255 // no defs
257 #ifdef SOCKETS_NAMESPACE
259 #endif
262 #endif
264 #ifdef SOCKETS_NAMESPACE
265 namespace SOCKETS_NAMESPACE {
266 #endif
267 /** List type containing file descriptors. */
268 typedef std::list<SOCKET> socket_v;
271 #ifdef SOCKETS_NAMESPACE
273 #endif
276 // getaddrinfo / getnameinfo replacements
277 #ifdef NO_GETADDRINFO
278 #ifndef AI_NUMERICHOST
279 #define AI_NUMERICHOST 1
280 #endif
281 #ifndef NI_NUMERICHOST
282 #define NI_NUMERICHOST 1
283 #endif
284 #endif
287 #endif // _SOCKETS_socket_include_H