aboutsummaryrefslogtreecommitdiff
path: root/src/compat
diff options
context:
space:
mode:
Diffstat (limited to 'src/compat')
-rw-r--r--src/compat/linux.h31
-rw-r--r--src/compat/osx.h27
-rw-r--r--src/compat/win32.cpp166
-rw-r--r--src/compat/win32.h131
4 files changed, 355 insertions, 0 deletions
diff --git a/src/compat/linux.h b/src/compat/linux.h
new file mode 100644
index 0000000..ccc8536
--- /dev/null
+++ b/src/compat/linux.h
@@ -0,0 +1,31 @@
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#ifdef __linux__
9
10#define bu_inet_ntoa inet_ntoa
11#define bu_inet_addr inet_addr
12#define bu_select select
13#define bu_socket socket
14#define bu_shutdown shutdown
15#define bu_htons htons
16#define bu_htonl htonl
17#define bu_gethostbyname gethostbyname
18#define bu_freeaddrinfo freeaddrinfo
19#define bu_getaddrinfo getaddrinfo
20#define bu_connect connect
21#define bu_getpeername getpeername
22#define bu_setsockopt setsockopt
23#define bu_bind bind
24#define bu_listen listen
25#define bu_accept accept
26#define bu_send send
27#define bu_recv recv
28
29#define bu_gai_strerror gai_strerror
30
31#endif
diff --git a/src/compat/osx.h b/src/compat/osx.h
new file mode 100644
index 0000000..7169d7e
--- /dev/null
+++ b/src/compat/osx.h
@@ -0,0 +1,27 @@
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#ifndef OSX_COMPATIBILITY__H
9#define OSX_COMPATIBILITY__H
10
11#ifdef __APPLE__
12
13#ifndef TEMP_FAILURE_RETRY
14#define TEMP_FAILURE_RETRY(expression) \
15 (__extension__ \
16 ({ long int __result; \
17 do __result = (long int) (expression); \
18 while (__result == -1L && errno == EINTR); \
19 __result; }))
20#endif
21
22#include <sched.h>
23
24#define pthread_yield() sched_yield()
25#endif /* __APPLE__ */
26#endif
27
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
15char Bu::Winsock2::scode[15];
16
17deffunc( WSAStartup );
18deffunc( WSACleanup );
19deffunc( WSAGetLastError );
20deffunc( inet_ntoa );
21deffunc( inet_addr );
22deffunc( select );
23deffunc( socket );
24deffunc( shutdown );
25deffunc( ioctlsocket );
26deffunc( htons );
27deffunc( htonl );
28deffunc( gethostbyname );
29deffunc( freeaddrinfo );
30deffunc( getaddrinfo );
31deffunc( connect );
32deffunc( getpeername );
33deffunc( setsockopt );
34deffunc( bind );
35deffunc( listen );
36deffunc( accept );
37deffunc( recv );
38deffunc( send );
39deffunc( __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
49Bu::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
80Bu::Winsock2::~Winsock2()
81{
82 Bu::Winsock2::WSACleanup();
83 FreeLibrary( Ws2_32 );
84}
85
86char *Bu::Winsock2::gai_strerror( int iCode )
87{
88 sprintf( scode, "%d", Bu::Winsock2::WSAGetLastError() );
89 return scode;
90}
91
92int Bu::Winsock2::WSAStartup( WORD a, LPWSADATA b ) {
93 return (*Bu::Winsock2::_fnptr_WSAStartup)( a, b );
94}
95int Bu::Winsock2::WSACleanup( ) {
96 return (*Bu::Winsock2::_fnptr_WSACleanup)();
97}
98int Bu::Winsock2::WSAGetLastError( ) {
99 return (*Bu::Winsock2::_fnptr_WSAGetLastError)();
100}
101char * Bu::Winsock2::inet_ntoa( struct in_addr a ) {
102 return (*Bu::Winsock2::_fnptr_inet_ntoa)( a );
103}
104unsigned long Bu::Winsock2::inet_addr( const char *s_in ) {
105 return (*Bu::Winsock2::_fnptr_inet_addr)( s_in );
106}
107int 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}
111SOCKET Bu::Winsock2::socket( int domain, int type, int protocol ) {
112 return (*Bu::Winsock2::_fnptr_socket)( domain, type, protocol );
113}
114int Bu::Winsock2::shutdown( SOCKET s, int how ) {
115 return (*Bu::Winsock2::_fnptr_shutdown)( s, how );
116}
117int Bu::Winsock2::ioctlsocket( SOCKET s, long cmd, u_long *argp ) {
118 return (*Bu::Winsock2::_fnptr_ioctlsocket)( s, cmd, argp );
119}
120u_short Bu::Winsock2::htons( u_short in ) {
121 return (*Bu::Winsock2::_fnptr_htons)( in );
122}
123u_long Bu::Winsock2::htonl( u_long in ) {
124 return (*Bu::Winsock2::_fnptr_htonl)( in );
125}
126struct hostent * Bu::Winsock2::gethostbyname( const char *name ) {
127 return (*Bu::Winsock2::_fnptr_gethostbyname)( name );
128}
129void Bu::Winsock2::freeaddrinfo( struct addrinfo *ai ) {
130 return (*Bu::Winsock2::_fnptr_freeaddrinfo)( ai );
131}
132int 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}
136int Bu::Winsock2::connect( SOCKET s, const struct sockaddr *a, int b ) {
137 return (*Bu::Winsock2::_fnptr_connect)( s, a, b );
138}
139int Bu::Winsock2::getpeername( SOCKET s, struct sockaddr *a, int *b ) {
140 return (*Bu::Winsock2::_fnptr_getpeername)( s, a, b);
141}
142int 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}
146int Bu::Winsock2::bind( SOCKET s, const struct sockaddr *a, int b ) {
147 return (*Bu::Winsock2::_fnptr_bind)( s, a, b );
148}
149int Bu::Winsock2::listen( SOCKET s, int backlog ) {
150 return (*Bu::Winsock2::_fnptr_listen)( s, backlog );
151}
152SOCKET Bu::Winsock2::accept( SOCKET s, struct sockaddr *a, int *b ) {
153 return (*Bu::Winsock2::_fnptr_accept)( s, a, b );
154}
155int Bu::Winsock2::recv( SOCKET s, char *buf, int len, int flags ) {
156 return (*Bu::Winsock2::_fnptr_recv)( s, buf, len, flags );
157}
158int Bu::Winsock2::send( SOCKET s, const char *buf, int len, int flags ) {
159 return (*Bu::Winsock2::_fnptr_send)( s, buf, len, flags );
160}
161int Bu::Winsock2::__WSAFDIsSet( SOCKET s, fd_set *set ) {
162 return (*Bu::Winsock2::_fnptr___WSAFDIsSet)( s, set );
163}
164
165#endif
166
diff --git a/src/compat/win32.h b/src/compat/win32.h
new file mode 100644
index 0000000..6304d4c
--- /dev/null
+++ b/src/compat/win32.h
@@ -0,0 +1,131 @@
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#ifndef WIN32_COMPATIBILITY__H
9#define WIN32_COMPATIBILITY__H
10
11#ifdef WIN32
12
13#ifdef __cplusplus
14extern "C"
15{
16#include <Winsock2.h>
17#include <ws2tcpip.h>
18}
19#endif
20
21#include "bu/fstring.h"
22#include "bu/singleton.h"
23
24#ifndef TEMP_FAILURE_RETRY
25#define TEMP_FAILURE_RETRY(expression) \
26 (__extension__ \
27 ({ long int __result; \
28 do __result = (long int) (expression); \
29 while (__result == -1L && errno == EINTR); \
30 __result; }))
31#endif
32
33#define decltype( ret, name, ... ) \
34 typedef ret (__cdecl *FNDEF_DYN_ ##name)( __VA_ARGS__ ); \
35 static FNDEF_DYN_ ##name _fnptr_ ##name; \
36 static ret name( __VA_ARGS__ )
37
38__extension__ typedef int socklen_t;
39
40#ifdef gai_strerror
41#undef gai_strerror
42#endif
43
44namespace Bu
45{
46 class Winsock2 : public Bu::Singleton<Winsock2>
47 {
48 friend class Bu::Singleton<Winsock2>;
49 private:
50 Winsock2();
51 virtual ~Winsock2();
52
53 WSADATA wsaData;
54 HINSTANCE Ws2_32;
55
56 public:
57 // decltype( return type, function name<, optional parameters> )
58 decltype( int, WSAStartup, WORD, LPWSADATA );
59 decltype( int, WSACleanup );
60 decltype( int, WSAGetLastError );
61 decltype( char *, inet_ntoa, struct in_addr );
62 decltype( unsigned long, inet_addr, const char *s_in );
63 decltype( int, select, int nfds, fd_set *readfds, fd_set *writefds,
64 fd_set *exceptfds, const struct timeval *timeout );
65 decltype( SOCKET, socket, int domain, int type, int protocol );
66 decltype( int, shutdown, SOCKET s, int how );
67 decltype( int, ioctlsocket, SOCKET s, long cmd, u_long *argp );
68 decltype( u_short, htons, u_short in );
69 decltype( u_long, htonl, u_long in );
70 decltype( struct hostent *, gethostbyname, const char *name );
71 decltype( void, freeaddrinfo, struct addrinfo *ai );
72 decltype( int, getaddrinfo, const char *nodename, const char *servname,
73 const struct addrinfo *hints, struct addrinfo **res );
74 decltype( int, connect, SOCKET s, const struct sockaddr *serv_addr,
75 int addrlen );
76 decltype( int, getpeername, SOCKET s, struct sockaddr *name,
77 int *namelen );
78 decltype( int, setsockopt, SOCKET s, int level, int optname,
79 const char *optval, int optlen );
80 decltype( int, bind, SOCKET s, const struct sockaddr *my_addr,
81 int addrlen );
82 decltype( int, listen, SOCKET s, int backlog );
83 decltype( SOCKET, accept, SOCKET s, struct sockaddr *addr,
84 int *addrlen);
85 decltype( int, recv, SOCKET s, char *buf, int len, int flags );
86 decltype( int, send, SOCKET s, const char *buf, int len, int flags );
87 decltype( int, __WSAFDIsSet, SOCKET s, fd_set *set );
88
89 static char scode[15];
90 static char *gai_strerror( int iCode );
91 };
92};
93
94#ifdef FD_ISSET
95#undef FD_ISSET
96#endif
97
98#define bu_WSAStartup (*Bu::Winsock2::WSAStartup)
99#define bu_WSACleanup (*Bu::Winsock2::WSACleanup)
100#define bu_WSAGetLastError (*Bu::Winsock2::WSAGetLastError)
101#define bu_inet_ntoa (*Bu::Winsock2::inet_ntoa)
102#define bu_inet_addr (*Bu::Winsock2::inet_addr)
103#define bu_select (*Bu::Winsock2::select)
104#define bu_socket (*Bu::Winsock2::socket)
105#define bu_shutdown (*Bu::Winsock2::shutdown)
106#define bu_ioctlsocket (*Bu::Winsock2::ioctlsocket)
107#define bu_htons (*Bu::Winsock2::htons)
108#define bu_htonl (*Bu::Winsock2::htonl)
109#define bu_gethostbyname (*Bu::Winsock2::gethostbyname)
110#define bu_freeaddrinfo (*Bu::Winsock2::freeaddrinfo)
111#define bu_getaddrinfo (*Bu::Winsock2::getaddrinfo)
112#define bu_connect (*Bu::Winsock2::connect)
113#define bu_getpeername (*Bu::Winsock2::getpeername)
114#define bu_setsockopt (*Bu::Winsock2::setsockopt)
115#define bu_bind (*Bu::Winsock2::bind)
116#define bu_listen (*Bu::Winsock2::listen)
117#define bu_accept (*Bu::Winsock2::accept)
118#define bu_recv (*Bu::Winsock2::recv)
119#define bu_send (*Bu::Winsock2::send)
120#define bu___WSAFDIsSet (*Bu::Winsock2::__WSAFDIsSet)
121
122#define FD_ISSET (*Bu::Winsock2::__WSAFDIsSet)
123#define bu_gai_strerror Bu::Winsock2::gai_strerror
124
125#undef decltype
126
127#undef USE_64BIT_IO
128
129#endif /* WIN32 */
130#endif
131