diff options
Diffstat (limited to 'src/compat/win32.cpp')
-rw-r--r-- | src/compat/win32.cpp | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/src/compat/win32.cpp b/src/compat/win32.cpp new file mode 100644 index 0000000..6fcac15 --- /dev/null +++ b/src/compat/win32.cpp | |||
@@ -0,0 +1,166 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2010 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/compat/win32.h" | ||
9 | |||
10 | #ifdef WIN32 | ||
11 | |||
12 | #define deffunc( name ) \ | ||
13 | Bu::Winsock2::FNDEF_DYN_ ##name Bu::Winsock2::_fnptr_ ##name = NULL | ||
14 | |||
15 | char Bu::Winsock2::scode[15]; | ||
16 | |||
17 | deffunc( WSAStartup ); | ||
18 | deffunc( WSACleanup ); | ||
19 | deffunc( WSAGetLastError ); | ||
20 | deffunc( inet_ntoa ); | ||
21 | deffunc( inet_addr ); | ||
22 | deffunc( select ); | ||
23 | deffunc( socket ); | ||
24 | deffunc( shutdown ); | ||
25 | deffunc( ioctlsocket ); | ||
26 | deffunc( htons ); | ||
27 | deffunc( htonl ); | ||
28 | deffunc( gethostbyname ); | ||
29 | deffunc( freeaddrinfo ); | ||
30 | deffunc( getaddrinfo ); | ||
31 | deffunc( connect ); | ||
32 | deffunc( getpeername ); | ||
33 | deffunc( setsockopt ); | ||
34 | deffunc( bind ); | ||
35 | deffunc( listen ); | ||
36 | deffunc( accept ); | ||
37 | deffunc( recv ); | ||
38 | deffunc( send ); | ||
39 | deffunc( __WSAFDIsSet ); | ||
40 | |||
41 | // Safely get a function from the library | ||
42 | #define getfunc( name ) \ | ||
43 | Bu::Winsock2::_fnptr_ ##name = (FNDEF_DYN_ ##name) \ | ||
44 | GetProcAddress( Ws2_32, #name ); \ | ||
45 | if( Bu::Winsock2::_fnptr_ ##name == NULL ) { \ | ||
46 | throw Bu::ExceptionBase("Error loading function " #name " from dll.");\ | ||
47 | } (void)0 | ||
48 | |||
49 | Bu::Winsock2::Winsock2() | ||
50 | { | ||
51 | Ws2_32 = LoadLibrary(TEXT("WS2_32.DLL")); | ||
52 | |||
53 | getfunc( WSAStartup ); | ||
54 | getfunc( WSACleanup ); | ||
55 | getfunc( WSAGetLastError ); | ||
56 | getfunc( inet_ntoa ); | ||
57 | getfunc( inet_addr ); | ||
58 | getfunc( select ); | ||
59 | getfunc( socket ); | ||
60 | getfunc( shutdown ); | ||
61 | getfunc( ioctlsocket ); | ||
62 | getfunc( htons ); | ||
63 | getfunc( htonl ); | ||
64 | getfunc( gethostbyname ); | ||
65 | getfunc( freeaddrinfo ); | ||
66 | getfunc( getaddrinfo ); | ||
67 | getfunc( connect ); | ||
68 | getfunc( getpeername ); | ||
69 | getfunc( setsockopt ); | ||
70 | getfunc( bind ); | ||
71 | getfunc( listen ); | ||
72 | getfunc( accept ); | ||
73 | getfunc( recv ); | ||
74 | getfunc( send ); | ||
75 | getfunc( __WSAFDIsSet ); | ||
76 | |||
77 | Bu::Winsock2::WSAStartup( MAKEWORD(2, 2), &wsaData ); | ||
78 | } | ||
79 | |||
80 | Bu::Winsock2::~Winsock2() | ||
81 | { | ||
82 | Bu::Winsock2::WSACleanup(); | ||
83 | FreeLibrary( Ws2_32 ); | ||
84 | } | ||
85 | |||
86 | char *Bu::Winsock2::gai_strerror( int iCode ) | ||
87 | { | ||
88 | sprintf( scode, "%d", Bu::Winsock2::WSAGetLastError() ); | ||
89 | return scode; | ||
90 | } | ||
91 | |||
92 | int Bu::Winsock2::WSAStartup( WORD a, LPWSADATA b ) { | ||
93 | return (*Bu::Winsock2::_fnptr_WSAStartup)( a, b ); | ||
94 | } | ||
95 | int Bu::Winsock2::WSACleanup( ) { | ||
96 | return (*Bu::Winsock2::_fnptr_WSACleanup)(); | ||
97 | } | ||
98 | int Bu::Winsock2::WSAGetLastError( ) { | ||
99 | return (*Bu::Winsock2::_fnptr_WSAGetLastError)(); | ||
100 | } | ||
101 | char * Bu::Winsock2::inet_ntoa( struct in_addr a ) { | ||
102 | return (*Bu::Winsock2::_fnptr_inet_ntoa)( a ); | ||
103 | } | ||
104 | unsigned long Bu::Winsock2::inet_addr( const char *s_in ) { | ||
105 | return (*Bu::Winsock2::_fnptr_inet_addr)( s_in ); | ||
106 | } | ||
107 | int Bu::Winsock2::select( int a, fd_set *b, fd_set *c, fd_set *d, | ||
108 | const struct timeval *e ) { | ||
109 | return (*Bu::Winsock2::_fnptr_select)( a, b, c, d, e ); | ||
110 | } | ||
111 | SOCKET Bu::Winsock2::socket( int domain, int type, int protocol ) { | ||
112 | return (*Bu::Winsock2::_fnptr_socket)( domain, type, protocol ); | ||
113 | } | ||
114 | int Bu::Winsock2::shutdown( SOCKET s, int how ) { | ||
115 | return (*Bu::Winsock2::_fnptr_shutdown)( s, how ); | ||
116 | } | ||
117 | int Bu::Winsock2::ioctlsocket( SOCKET s, long cmd, u_long *argp ) { | ||
118 | return (*Bu::Winsock2::_fnptr_ioctlsocket)( s, cmd, argp ); | ||
119 | } | ||
120 | u_short Bu::Winsock2::htons( u_short in ) { | ||
121 | return (*Bu::Winsock2::_fnptr_htons)( in ); | ||
122 | } | ||
123 | u_long Bu::Winsock2::htonl( u_long in ) { | ||
124 | return (*Bu::Winsock2::_fnptr_htonl)( in ); | ||
125 | } | ||
126 | struct hostent * Bu::Winsock2::gethostbyname( const char *name ) { | ||
127 | return (*Bu::Winsock2::_fnptr_gethostbyname)( name ); | ||
128 | } | ||
129 | void Bu::Winsock2::freeaddrinfo( struct addrinfo *ai ) { | ||
130 | return (*Bu::Winsock2::_fnptr_freeaddrinfo)( ai ); | ||
131 | } | ||
132 | int Bu::Winsock2::getaddrinfo( const char *a, const char *b, | ||
133 | const struct addrinfo *c, struct addrinfo **d ) { | ||
134 | return (*Bu::Winsock2::_fnptr_getaddrinfo)( a, b, c, d ); | ||
135 | } | ||
136 | int Bu::Winsock2::connect( SOCKET s, const struct sockaddr *a, int b ) { | ||
137 | return (*Bu::Winsock2::_fnptr_connect)( s, a, b ); | ||
138 | } | ||
139 | int Bu::Winsock2::getpeername( SOCKET s, struct sockaddr *a, int *b ) { | ||
140 | return (*Bu::Winsock2::_fnptr_getpeername)( s, a, b); | ||
141 | } | ||
142 | int Bu::Winsock2::setsockopt( SOCKET s, int a, int b, | ||
143 | const char *c, int d ) { | ||
144 | return (*Bu::Winsock2::_fnptr_setsockopt)( s, a, b, c, d ); | ||
145 | } | ||
146 | int Bu::Winsock2::bind( SOCKET s, const struct sockaddr *a, int b ) { | ||
147 | return (*Bu::Winsock2::_fnptr_bind)( s, a, b ); | ||
148 | } | ||
149 | int Bu::Winsock2::listen( SOCKET s, int backlog ) { | ||
150 | return (*Bu::Winsock2::_fnptr_listen)( s, backlog ); | ||
151 | } | ||
152 | SOCKET Bu::Winsock2::accept( SOCKET s, struct sockaddr *a, int *b ) { | ||
153 | return (*Bu::Winsock2::_fnptr_accept)( s, a, b ); | ||
154 | } | ||
155 | int Bu::Winsock2::recv( SOCKET s, char *buf, int len, int flags ) { | ||
156 | return (*Bu::Winsock2::_fnptr_recv)( s, buf, len, flags ); | ||
157 | } | ||
158 | int Bu::Winsock2::send( SOCKET s, const char *buf, int len, int flags ) { | ||
159 | return (*Bu::Winsock2::_fnptr_send)( s, buf, len, flags ); | ||
160 | } | ||
161 | int Bu::Winsock2::__WSAFDIsSet( SOCKET s, fd_set *set ) { | ||
162 | return (*Bu::Winsock2::_fnptr___WSAFDIsSet)( s, set ); | ||
163 | } | ||
164 | |||
165 | #endif | ||
166 | |||