aboutsummaryrefslogtreecommitdiff
path: root/src/win32_compatibility.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/win32_compatibility.cpp434
1 files changed, 117 insertions, 317 deletions
diff --git a/src/win32_compatibility.cpp b/src/win32_compatibility.cpp
index 54fd56c..b85e869 100644
--- a/src/win32_compatibility.cpp
+++ b/src/win32_compatibility.cpp
@@ -2,352 +2,152 @@
2 2
3#ifdef WIN32 3#ifdef WIN32
4 4
5typedef int (__cdecl *FNDEF_DYN_WSAStartup)(WORD,LPWSADATA); 5#define deffunc( name ) \
6int DynamicWinsock2::WSAStartup( 6 Bu::Winsock2::FNDEF_DYN_ ##name Bu::Winsock2::_fnptr_ ##name = NULL
7 WORD wVersionRequested,LPWSADATA lpWSAData) 7
8char Bu::Winsock2::scode[15];
9
10deffunc( WSAStartup );
11deffunc( WSACleanup );
12deffunc( WSAGetLastError );
13deffunc( inet_ntoa );
14deffunc( inet_addr );
15deffunc( select );
16deffunc( socket );
17deffunc( ioctlsocket );
18deffunc( htons );
19deffunc( htonl );
20deffunc( gethostbyname );
21deffunc( freeaddrinfo );
22deffunc( getaddrinfo );
23deffunc( connect );
24deffunc( getpeername );
25deffunc( setsockopt );
26deffunc( bind );
27deffunc( listen );
28deffunc( accept );
29deffunc( recv );
30deffunc( send );
31deffunc( __WSAFDIsSet );
32
33// Safely get a function from the library
34#define getfunc( name ) \
35 Bu::Winsock2::_fnptr_ ##name = (FNDEF_DYN_ ##name) \
36 GetProcAddress( Ws2_32, #name ); \
37 if( Bu::Winsock2::_fnptr_ ##name == NULL ) { \
38 throw Bu::ExceptionBase("Error loading function " #name " from dll.");\
39 } (void)0
40
41Bu::Winsock2::Winsock2()
8{ 42{
9 int out=0; 43 Ws2_32 = LoadLibrary(TEXT("WS2_32.DLL"));
10 HINSTANCE Ws2_32 = LoadLibrary(TEXT("WS2_32.DLL")); 44
11 if( Ws2_32 != NULL ) 45 getfunc( WSAStartup );
12 { 46 getfunc( WSACleanup );
13 FNDEF_DYN_WSAStartup fn = (FNDEF_DYN_WSAStartup) 47 getfunc( WSAGetLastError );
14 GetProcAddress( Ws2_32, "WSAStartup" ); 48 getfunc( inet_ntoa );
15 if( fn != NULL ) 49 getfunc( inet_addr );
16 out = (fn)(wVersionRequested,lpWSAData); 50 getfunc( select );
17 } 51 getfunc( socket );
18 return out; 52 getfunc( ioctlsocket );
19} 53 getfunc( htons );
54 getfunc( htonl );
55 getfunc( gethostbyname );
56 getfunc( freeaddrinfo );
57 getfunc( getaddrinfo );
58 getfunc( connect );
59 getfunc( getpeername );
60 getfunc( setsockopt );
61 getfunc( bind );
62 getfunc( listen );
63 getfunc( accept );
64 getfunc( recv );
65 getfunc( send );
66 getfunc( __WSAFDIsSet );
20 67
21typedef int (__cdecl *FNDEF_DYN_WSACleanup)(); 68 Bu::Winsock2::WSAStartup( MAKEWORD(2, 2), &wsaData );
22int DynamicWinsock2::WSACleanup()
23{
24 int out=0;
25 HINSTANCE Ws2_32 = LoadLibrary(TEXT("WS2_32.DLL"));
26 if( Ws2_32 != NULL )
27 {
28 FNDEF_DYN_WSACleanup fn = (FNDEF_DYN_WSACleanup)
29 GetProcAddress( Ws2_32, "WSACleanup" );
30 if( fn != NULL )
31 out = (fn)();
32 }
33 return out;
34} 69}
35 70
36//typedef char * (__cdecl *FNDEF_DYN_gai_strerrorA)( int ecode ); 71Bu::Winsock2::~Winsock2()
37typedef int (__cdecl *FNDEF_DYN_WSAGetLastError)(void);
38int DynamicWinsock2::WSAGetLastError()
39{ 72{
40 int out=0; 73 Bu::Winsock2::WSACleanup();
41 HINSTANCE Ws2_32 = LoadLibrary(TEXT("WS2_32.DLL")); 74 FreeLibrary( Ws2_32 );
42 if( Ws2_32 != NULL )
43 {
44 FNDEF_DYN_WSAGetLastError fn = (FNDEF_DYN_WSAGetLastError)
45 GetProcAddress( Ws2_32, "WSAGetLastError" );
46 if( fn != NULL )
47 out = (fn)();
48 }
49 return out;
50} 75}
51 76
52typedef char * (__cdecl *FNDEF_DYN_inet_ntoa)( struct in_addr ); 77char *Bu::Winsock2::gai_strerror( int iCode )
53void DynamicWinsock2::inet_ntoa( Bu::FString &out, struct in_addr addr_in )
54{ 78{
55 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32")); 79 sprintf( scode, "%d", Bu::Winsock2::WSAGetLastError() );
56 if( Ws2_32 != NULL ) 80 return scode;
57 {
58 FNDEF_DYN_inet_ntoa fn = (FNDEF_DYN_inet_ntoa)
59 GetProcAddress( Ws2_32, "inet_ntoa" );
60 if( fn != NULL )
61 out = (fn)( addr_in );
62
63 //We will let windows clean up our dll imports on exit
64 //FreeLibrary( Ws2_32 );
65 }
66} 81}
67 82
68typedef unsigned long (__cdecl *FNDEF_DYN_inet_addr)( const char * ); 83int Bu::Winsock2::WSAStartup( WORD a, LPWSADATA b ) {
69unsigned long DynamicWinsock2::inet_addr( const char *s_in ) 84 return (*Bu::Winsock2::_fnptr_WSAStartup)( a, b );
70{
71 unsigned long out = 0;
72 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
73 if( Ws2_32 != NULL )
74 {
75 FNDEF_DYN_inet_addr fn = (FNDEF_DYN_inet_addr)
76 GetProcAddress( Ws2_32, "inet_addr" );
77 if( fn != NULL )
78 out = (fn)( s_in );
79 }
80 return out;
81} 85}
82 86int Bu::Winsock2::WSACleanup( ) {
83typedef int (__cdecl *FNDEF_DYN_select)( 87 return (*Bu::Winsock2::_fnptr_WSACleanup)();
84 int nfds,fd_set*,fd_set*,fd_set*,const struct timeval*);
85int DynamicWinsock2::select(int nfds, fd_set *readfds, fd_set *writefds,
86 fd_set *exceptfds, const struct timeval *timeout)
87{
88 int out = 0;
89 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
90 if( Ws2_32 != NULL )
91 {
92 FNDEF_DYN_select fn = (FNDEF_DYN_select)
93 GetProcAddress( Ws2_32, "select" );
94 if( fn != NULL )
95 out = (fn)( nfds, readfds, writefds, exceptfds, timeout );
96 }
97 return out;
98} 88}
99 89int Bu::Winsock2::WSAGetLastError( ) {
100typedef SOCKET (__cdecl *FNDEF_DYN_socket)(int,int,int); 90 return (*Bu::Winsock2::_fnptr_WSAGetLastError)();
101SOCKET DynamicWinsock2::socket(int domain, int type, int protocol)
102{
103 SOCKET out = 0;
104 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
105 if( Ws2_32 != NULL )
106 {
107 FNDEF_DYN_socket fn = (FNDEF_DYN_socket)
108 GetProcAddress( Ws2_32, "socket" );
109 if( fn != NULL )
110 out = (fn)( domain, type, protocol );
111 }
112 return out;
113} 91}
114 92char * Bu::Winsock2::inet_ntoa( struct in_addr a ) {
115typedef int (__cdecl *FNDEF_DYN_ioctlsocket)(SOCKET,long,u_long *); 93 return (*Bu::Winsock2::_fnptr_inet_ntoa)( a );
116int DynamicWinsock2::ioctlsocket(SOCKET s, long cmd, u_long *argp)
117{
118 int out = 0;
119 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
120 if( Ws2_32 != NULL )
121 {
122 FNDEF_DYN_ioctlsocket fn = (FNDEF_DYN_ioctlsocket)
123 GetProcAddress( Ws2_32, "ioctlsocket" );
124 if( fn != NULL )
125 out = (fn)( s, cmd, argp );
126 }
127 return out;
128} 94}
129 95unsigned long Bu::Winsock2::inet_addr( const char *s_in ) {
130typedef u_short (__cdecl *FNDEF_DYN_htons)(u_short); 96 return (*Bu::Winsock2::_fnptr_inet_addr)( s_in );
131u_short DynamicWinsock2::htons(u_short in)
132{
133 u_short out = 0;
134 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
135 if( Ws2_32 != NULL )
136 {
137 FNDEF_DYN_htons fn = (FNDEF_DYN_htons)
138 GetProcAddress( Ws2_32, "htons" );
139 if( fn != NULL )
140 out = (fn)( in );
141 }
142 return out;
143} 97}
144 98int Bu::Winsock2::select( int a, fd_set *b, fd_set *c, fd_set *d,
145typedef u_long (__cdecl *FNDEF_DYN_htonl)(u_long); 99 const struct timeval *e ) {
146u_long DynamicWinsock2::htonl(u_long in) 100 return (*Bu::Winsock2::_fnptr_select)( a, b, c, d, e );
147{
148 u_long out = 0;
149 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
150 if( Ws2_32 != NULL )
151 {
152 FNDEF_DYN_htonl fn = (FNDEF_DYN_htonl)
153 GetProcAddress( Ws2_32, "htonl" );
154 if( fn != NULL )
155 out = (fn)( in );
156 }
157 return out;
158} 101}
159 102SOCKET Bu::Winsock2::socket( int domain, int type, int protocol ) {
160typedef struct hostent * (__cdecl *FNDEF_DYN_gethostbyname)(const char *); 103 return (*Bu::Winsock2::_fnptr_socket)( domain, type, protocol );
161struct hostent *DynamicWinsock2::gethostbyname(const char *name)
162{
163 hostent *out = NULL;
164 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
165 if( Ws2_32 != NULL )
166 {
167 FNDEF_DYN_gethostbyname fn = (FNDEF_DYN_gethostbyname)
168 GetProcAddress( Ws2_32, "gethostbyname" );
169 if( fn != NULL )
170 out = (fn)( name );
171 }
172 return out;
173} 104}
174 105int Bu::Winsock2::ioctlsocket( SOCKET s, long cmd, u_long *argp ) {
175typedef void (__cdecl *FNDEF_DYN_freeaddrinfo)(struct addrinfo *); 106 return (*Bu::Winsock2::_fnptr_ioctlsocket)( s, cmd, argp );
176void DynamicWinsock2::freeaddrinfo(struct addrinfo *ai)
177{
178 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
179 if( Ws2_32 != NULL )
180 {
181 FNDEF_DYN_freeaddrinfo fn = (FNDEF_DYN_freeaddrinfo)
182 GetProcAddress( Ws2_32, "freeaddrinfo" );
183 if( fn != NULL )
184 (fn)( ai );
185 }
186} 107}
187 108u_short Bu::Winsock2::htons( u_short in ) {
188typedef int (__cdecl *FNDEF_DYN_getaddrinfo)( 109 return (*Bu::Winsock2::_fnptr_htons)( in );
189 const char*,const char*,const struct addrinfo*,struct addrinfo**); 110}
190int DynamicWinsock2::getaddrinfo( 111u_long Bu::Winsock2::htonl( u_long in ) {
191 const char *nodename, const char *servname, 112 return (*Bu::Winsock2::_fnptr_htonl)( in );
192 const struct addrinfo *hints, struct addrinfo **res )
193{
194 int out = 0;
195 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
196 if( Ws2_32 != NULL )
197 {
198 FNDEF_DYN_getaddrinfo fn = (FNDEF_DYN_getaddrinfo)
199 GetProcAddress( Ws2_32, "getaddrinfo" );
200 if( fn != NULL )
201 out = (fn)( nodename, servname, hints, res );
202 }
203 return out;
204} 113}
205 114struct hostent * Bu::Winsock2::gethostbyname( const char *name ) {
206typedef int (__cdecl *FNDEF_DYN_connect)(SOCKET,const struct sockaddr*,int); 115 return (*Bu::Winsock2::_fnptr_gethostbyname)( name );
207int DynamicWinsock2::connect(
208 SOCKET s, const struct sockaddr *serv_addr, int addrlen)
209{
210 int out = 0;
211 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
212 if( Ws2_32 != NULL )
213 {
214 FNDEF_DYN_connect fn = (FNDEF_DYN_connect)
215 GetProcAddress( Ws2_32, "connect" );
216 if( fn != NULL )
217 out = (fn)( s, serv_addr, addrlen );
218 }
219 return out;
220} 116}
221 117void Bu::Winsock2::freeaddrinfo( struct addrinfo *ai ) {
222typedef int (__cdecl *FNDEF_DYN_getpeername)(SOCKET,struct sockaddr*,int*); 118 return (*Bu::Winsock2::_fnptr_freeaddrinfo)( ai );
223int DynamicWinsock2::getpeername(SOCKET s, struct sockaddr *name, int *namelen)
224{
225 int out = 0;
226 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
227 if( Ws2_32 != NULL )
228 {
229 FNDEF_DYN_getpeername fn = (FNDEF_DYN_getpeername)
230 GetProcAddress( Ws2_32, "getpeername" );
231 if( fn != NULL )
232 out = (fn)( s, name, namelen );
233 }
234 return out;
235} 119}
236 120int Bu::Winsock2::getaddrinfo( const char *a, const char *b,
237typedef int (__cdecl *FNDEF_DYN_setsockopt)(SOCKET,int,int,const char*,int); 121 const struct addrinfo *c, struct addrinfo **d ) {
238int DynamicWinsock2::setsockopt(SOCKET s, int level, int optname, 122 return (*Bu::Winsock2::_fnptr_getaddrinfo)( a, b, c, d );
239 const char *optval, int optlen)
240{
241 int out = 0;
242 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
243 if( Ws2_32 != NULL )
244 {
245 FNDEF_DYN_setsockopt fn = (FNDEF_DYN_setsockopt)
246 GetProcAddress( Ws2_32, "setsockopt" );
247 if( fn != NULL )
248 out = (fn)( s, level, optname, optval, optlen );
249 }
250 return out;
251} 123}
252 124int Bu::Winsock2::connect( SOCKET s, const struct sockaddr *a, int b ) {
253typedef int (__cdecl *FNDEF_DYN_bind)(SOCKET,const struct sockaddr*,int); 125 return (*Bu::Winsock2::_fnptr_connect)( s, a, b );
254int DynamicWinsock2::bind(SOCKET s, const struct sockaddr *my_addr, int addrlen)
255{
256 int out = 0;
257 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
258 if( Ws2_32 != NULL )
259 {
260 FNDEF_DYN_bind fn = (FNDEF_DYN_bind)
261 GetProcAddress( Ws2_32, "bind" );
262 if( fn != NULL )
263 out = (fn)( s, my_addr, addrlen );
264 }
265 return out;
266} 126}
267 127int Bu::Winsock2::getpeername( SOCKET s, struct sockaddr *a, int *b ) {
268typedef int (__cdecl *FNDEF_DYN_listen)(SOCKET,int); 128 return (*Bu::Winsock2::_fnptr_getpeername)( s, a, b);
269int DynamicWinsock2::listen(SOCKET s, int backlog)
270{
271 int out = 0;
272 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
273 if( Ws2_32 != NULL )
274 {
275 FNDEF_DYN_listen fn = (FNDEF_DYN_listen)
276 GetProcAddress( Ws2_32, "listen" );
277 if( fn != NULL )
278 out = (fn)( s, backlog );
279 }
280 return out;
281} 129}
282 130int Bu::Winsock2::setsockopt( SOCKET s, int a, int b,
283typedef SOCKET (__cdecl *FNDEF_DYN_accept)(SOCKET,struct sockaddr*,int*); 131 const char *c, int d ) {
284SOCKET DynamicWinsock2::accept(SOCKET s, struct sockaddr *addr, int *addrlen) 132 return (*Bu::Winsock2::_fnptr_setsockopt)( s, a, b, c, d );
285{
286 SOCKET out = 0;
287 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
288 if( Ws2_32 != NULL )
289 {
290 FNDEF_DYN_accept fn = (FNDEF_DYN_accept)
291 GetProcAddress( Ws2_32, "accept" );
292 if( fn != NULL )
293 out = (fn)( s, addr, addrlen );
294 }
295 return out;
296} 133}
297 134int Bu::Winsock2::bind( SOCKET s, const struct sockaddr *a, int b ) {
298typedef int (__cdecl *FNDEF_DYN_recv)(SOCKET,char*,int,int); 135 return (*Bu::Winsock2::_fnptr_bind)( s, a, b );
299int DynamicWinsock2::recv( SOCKET s, char *buf, int len, int flags )
300{
301 int out = 0;
302 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
303 if( Ws2_32 != NULL )
304 {
305 FNDEF_DYN_recv fn = (FNDEF_DYN_recv)
306 GetProcAddress( Ws2_32, "recv" );
307 if( fn != NULL )
308 out = (fn)( s, buf, len, flags );
309 }
310 return out;
311} 136}
312 137int Bu::Winsock2::listen( SOCKET s, int backlog ) {
313typedef int (__cdecl *FNDEF_DYN_send)(SOCKET,const char*,int,int); 138 return (*Bu::Winsock2::_fnptr_listen)( s, backlog );
314int DynamicWinsock2::send( SOCKET s, const char *buf, int len, int flags )
315{
316 int out = 0;
317 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
318 if( Ws2_32 != NULL )
319 {
320 FNDEF_DYN_send fn = (FNDEF_DYN_send)
321 GetProcAddress( Ws2_32, "send" );
322 if( fn != NULL )
323 out = (fn)( s, buf, len, flags );
324 }
325 return out;
326} 139}
327 140SOCKET Bu::Winsock2::accept( SOCKET s, struct sockaddr *a, int *b ) {
328typedef int (__cdecl *FNDEF_DYN__WSAFDIsSet)(SOCKET,fd_set*); 141 return (*Bu::Winsock2::_fnptr_accept)( s, a, b );
329int DynamicWinsock2::DYN_FD_ISSET(SOCKET s, fd_set *set)
330{
331 int out = 0;
332 HINSTANCE Ws2_32 = LoadLibrary(TEXT("Ws2_32"));
333 if( Ws2_32 != NULL )
334 {
335 FNDEF_DYN__WSAFDIsSet fn = (FNDEF_DYN__WSAFDIsSet)
336 GetProcAddress( Ws2_32, "__WSAFDIsSet" );
337 if( fn != NULL )
338 out = (fn)( s, set );
339 }
340 return out;
341} 142}
342 143int Bu::Winsock2::recv( SOCKET s, char *buf, int len, int flags ) {
343DynamicWinsock2::Winsock2::Winsock2() 144 return (*Bu::Winsock2::_fnptr_recv)( s, buf, len, flags );
344{
345 DynamicWinsock2::WSAStartup( MAKEWORD(2, 2), &wsaData );
346} 145}
347 146int Bu::Winsock2::send( SOCKET s, const char *buf, int len, int flags ) {
348DynamicWinsock2::Winsock2::~Winsock2() 147 return (*Bu::Winsock2::_fnptr_send)( s, buf, len, flags );
349{ 148}
350 DynamicWinsock2::WSACleanup(); 149int Bu::Winsock2::__WSAFDIsSet( SOCKET s, fd_set *set ) {
150 return (*Bu::Winsock2::_fnptr___WSAFDIsSet)( s, set );
351} 151}
352 152
353#endif 153#endif