1 |
/*
|
2 |
* dynsockets.cpp - ip router
|
3 |
*
|
4 |
* Basilisk II (C) 1997-2008 Christian Bauer
|
5 |
*
|
6 |
* Windows platform specific code copyright (C) Lauri Pesonen
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or modify
|
9 |
* it under the terms of the GNU General Public License as published by
|
10 |
* the Free Software Foundation; either version 2 of the License, or
|
11 |
* (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
21 |
*/
|
22 |
|
23 |
#include "sysdeps.h"
|
24 |
#include "dynsockets.h"
|
25 |
#include "dump.h"
|
26 |
#include "main.h"
|
27 |
|
28 |
|
29 |
|
30 |
#if DEBUG
|
31 |
#pragma optimize("",off)
|
32 |
#endif
|
33 |
|
34 |
|
35 |
#include "debug.h"
|
36 |
|
37 |
|
38 |
/*
|
39 |
Cannot link statically to winsock. We need ws2, but there are
|
40 |
Win95 b2 users who can't (or won't) upgrade.
|
41 |
*/
|
42 |
|
43 |
static const char *wslib = "WS2_32.DLL";
|
44 |
|
45 |
static HMODULE hWinsock32 = 0;
|
46 |
static WSADATA WSAData;
|
47 |
|
48 |
int (WSAAPI *_WSAStartup) (WORD, LPWSADATA) = 0;
|
49 |
int (WSAAPI *_WSACleanup) (void) = 0;
|
50 |
int (WSAAPI *_gethostname) (char *, int) = 0;
|
51 |
char * (WSAAPI *_inet_ntoa) (struct in_addr) = 0;
|
52 |
struct hostent * (WSAAPI *_gethostbyname) (const char *) = 0;
|
53 |
int (WSAAPI *_send) (SOCKET, const char *, int, int) = 0;
|
54 |
int (WSAAPI *_sendto) (SOCKET, const char *, int, int, const struct sockaddr *, int) = 0;
|
55 |
int (WSAAPI *_recv) (SOCKET, char *, int, int) = 0;
|
56 |
int (WSAAPI *_recvfrom) (SOCKET, char *, int, int, struct sockaddr *, int *) = 0;
|
57 |
int (WSAAPI *_listen) (SOCKET, int) = 0;
|
58 |
SOCKET (WSAAPI *_accept) (SOCKET, struct sockaddr *, int *) = 0;
|
59 |
SOCKET (WSAAPI *_socket) (int, int, int) = 0;
|
60 |
int (WSAAPI *_bind) (SOCKET, const struct sockaddr *, int) = 0;
|
61 |
int (WSAAPI *_WSAAsyncSelect) (SOCKET, HWND, u_int, long) = 0;
|
62 |
int (WSAAPI *_closesocket) (SOCKET) = 0;
|
63 |
int (WSAAPI *_getsockname) (SOCKET, struct sockaddr *, int *) = 0;
|
64 |
int (WSAAPI *_WSARecvFrom) (SOCKET, LPWSABUF, DWORD, LPDWORD, LPDWORD, struct sockaddr *, LPINT, LPWSAOVERLAPPED, LPWSAOVERLAPPED_COMPLETION_ROUTINE) = 0;
|
65 |
int (WSAAPI *_WSAGetLastError) (void) = 0;
|
66 |
int (WSAAPI *_WSAConnect) (SOCKET, const struct sockaddr *, int, LPWSABUF, LPWSABUF, LPQOS, LPQOS) = 0;
|
67 |
int (WSAAPI *_setsockopt) (SOCKET, int, int, const char *, int) = 0;
|
68 |
int (WSAAPI *_WSAEventSelect) (SOCKET, WSAEVENT, long) = 0;
|
69 |
WSAEVENT (WSAAPI *_WSACreateEvent) (void) = 0;
|
70 |
BOOL (WSAAPI *_WSACloseEvent) (WSAEVENT) = 0;
|
71 |
BOOL (WSAAPI *_WSAResetEvent) (WSAEVENT) = 0;
|
72 |
int (WSAAPI *_WSAEnumNetworkEvents) (SOCKET, WSAEVENT, LPWSANETWORKEVENTS) = 0;
|
73 |
int (WSAAPI *_shutdown) (SOCKET, int) = 0;
|
74 |
int (WSAAPI *_WSASend) (SOCKET, LPWSABUF, DWORD, LPDWORD, DWORD, LPWSAOVERLAPPED, LPWSAOVERLAPPED_COMPLETION_ROUTINE) = 0;
|
75 |
int (WSAAPI *_WSARecv) (SOCKET, LPWSABUF, DWORD, LPDWORD, LPDWORD, LPWSAOVERLAPPED, LPWSAOVERLAPPED_COMPLETION_ROUTINE) = 0;
|
76 |
unsigned long (WSAAPI *_inet_addr) (const char *) = 0;
|
77 |
|
78 |
static bool load_sockets()
|
79 |
{
|
80 |
bool result = false;
|
81 |
|
82 |
hWinsock32 = LoadLibrary( wslib );
|
83 |
if(!hWinsock32) {
|
84 |
ErrorAlert("Could not load Winsock libraries; router module is not available. Please install Windows sockets 2.");
|
85 |
} else {
|
86 |
_WSAStartup = (int (WSAAPI *)(WORD, LPWSADATA))GetProcAddress( hWinsock32, "WSAStartup" );
|
87 |
_WSACleanup = (int (WSAAPI *)(void))GetProcAddress( hWinsock32, "WSACleanup" );
|
88 |
_gethostname = (int (WSAAPI *)(char *, int))GetProcAddress( hWinsock32, "gethostname" );
|
89 |
_inet_ntoa = (char * (WSAAPI *)(struct in_addr))GetProcAddress( hWinsock32, "inet_ntoa" );
|
90 |
_gethostbyname = (struct hostent * (WSAAPI *)(const char *))GetProcAddress( hWinsock32, "gethostbyname" );
|
91 |
_send = (int (WSAAPI *)(SOCKET, const char *, int, int))GetProcAddress( hWinsock32, "send" );
|
92 |
_sendto = (int (WSAAPI *)(SOCKET, const char *, int, int, const struct sockaddr *, int))GetProcAddress( hWinsock32, "sendto" );
|
93 |
_recv = (int (WSAAPI *)(SOCKET, char *, int, int))GetProcAddress( hWinsock32, "recv" );
|
94 |
_recvfrom = (int (WSAAPI *)(SOCKET, char *, int, int, struct sockaddr *, int *))GetProcAddress( hWinsock32, "recvfrom" );
|
95 |
_listen = (int (WSAAPI *)(SOCKET, int))GetProcAddress( hWinsock32, "listen" );
|
96 |
_accept = (SOCKET (WSAAPI *)(SOCKET, struct sockaddr *, int *))GetProcAddress( hWinsock32, "accept" );
|
97 |
_socket = (SOCKET (WSAAPI *)(int, int, int))GetProcAddress( hWinsock32, "socket" );
|
98 |
_bind = (int (WSAAPI *)(SOCKET, const struct sockaddr *, int))GetProcAddress( hWinsock32, "bind" );
|
99 |
_WSAAsyncSelect = (int (WSAAPI *)(SOCKET, HWND, u_int, long))GetProcAddress( hWinsock32, "WSAAsyncSelect" );
|
100 |
_closesocket = (int (WSAAPI *)(SOCKET))GetProcAddress( hWinsock32, "closesocket" );
|
101 |
_getsockname = (int (WSAAPI *)(SOCKET, struct sockaddr *, int *))GetProcAddress( hWinsock32, "getsockname" );
|
102 |
_WSARecvFrom = (int (WSAAPI *)(SOCKET, LPWSABUF, DWORD, LPDWORD, LPDWORD, struct sockaddr *, LPINT, LPWSAOVERLAPPED, LPWSAOVERLAPPED_COMPLETION_ROUTINE))GetProcAddress( hWinsock32, "WSARecvFrom" );
|
103 |
_WSAGetLastError = (int (WSAAPI *)(void))GetProcAddress( hWinsock32, "WSAGetLastError" );
|
104 |
_WSAConnect = (int (WSAAPI *)(SOCKET, const struct sockaddr *, int, LPWSABUF, LPWSABUF, LPQOS, LPQOS))GetProcAddress( hWinsock32, "WSAConnect" );
|
105 |
_setsockopt = (int (WSAAPI *)(SOCKET, int, int, const char *, int))GetProcAddress( hWinsock32, "setsockopt" );
|
106 |
_WSAEventSelect = (int (WSAAPI *)(SOCKET, WSAEVENT, long))GetProcAddress( hWinsock32, "WSAEventSelect" );
|
107 |
_WSACreateEvent = (WSAEVENT (WSAAPI *)(void))GetProcAddress( hWinsock32, "WSACreateEvent" );
|
108 |
_WSACloseEvent = (BOOL (WSAAPI *)(WSAEVENT))GetProcAddress( hWinsock32, "WSACloseEvent" );
|
109 |
_WSAResetEvent = (BOOL (WSAAPI *)(WSAEVENT))GetProcAddress( hWinsock32, "WSAResetEvent" );
|
110 |
_WSAEnumNetworkEvents = (BOOL (WSAAPI *)(SOCKET, WSAEVENT, LPWSANETWORKEVENTS))GetProcAddress( hWinsock32, "WSAEnumNetworkEvents" );
|
111 |
_shutdown = (int (WSAAPI *)(SOCKET, int))GetProcAddress( hWinsock32, "shutdown" );
|
112 |
_WSASend = (int (WSAAPI *)(SOCKET, LPWSABUF, DWORD, LPDWORD, DWORD, LPWSAOVERLAPPED, LPWSAOVERLAPPED_COMPLETION_ROUTINE))GetProcAddress( hWinsock32, "WSASend" );
|
113 |
_WSARecv = (int (WSAAPI *)(SOCKET, LPWSABUF, DWORD, LPDWORD, LPDWORD, LPWSAOVERLAPPED, LPWSAOVERLAPPED_COMPLETION_ROUTINE))GetProcAddress( hWinsock32, "WSARecv" );
|
114 |
_inet_addr = (unsigned long (WSAAPI *)(const char *))GetProcAddress( hWinsock32, "inet_addr" );
|
115 |
|
116 |
if( _WSAStartup && _WSACleanup && _gethostname && _inet_ntoa && _gethostbyname &&
|
117 |
_send && _sendto && _recv && _recvfrom && _listen && _accept && _socket && _bind &&
|
118 |
_WSAAsyncSelect && _closesocket && _getsockname && _WSARecvFrom && _WSAGetLastError &&
|
119 |
_WSAConnect && _setsockopt && _WSAEventSelect && _WSACreateEvent && _WSACloseEvent &&
|
120 |
_WSAResetEvent && _WSAEnumNetworkEvents && _shutdown && _WSASend && _WSARecv && _inet_addr
|
121 |
)
|
122 |
{
|
123 |
result = true;
|
124 |
} else {
|
125 |
ErrorAlert("Could not find required entry points; router module is not available. Please install Windows sockets 2.");
|
126 |
}
|
127 |
}
|
128 |
|
129 |
return result;
|
130 |
}
|
131 |
|
132 |
bool dynsockets_init(void)
|
133 |
{
|
134 |
bool result = false;
|
135 |
if(load_sockets()) {
|
136 |
if( (_WSAStartup(MAKEWORD(2,0), &WSAData)) != 0 ||
|
137 |
LOBYTE( WSAData.wVersion ) != 2 ||
|
138 |
HIBYTE( WSAData.wVersion ) != 0 )
|
139 |
{
|
140 |
ErrorAlert("Could not start Windows sockets version 2.");
|
141 |
} else {
|
142 |
result = true;
|
143 |
}
|
144 |
}
|
145 |
return result;
|
146 |
}
|
147 |
|
148 |
void dynsockets_final(void)
|
149 |
{
|
150 |
if(hWinsock32) {
|
151 |
_WSACleanup();
|
152 |
FreeLibrary( hWinsock32 );
|
153 |
hWinsock32 = 0;
|
154 |
}
|
155 |
_WSAStartup = 0;
|
156 |
_WSACleanup = 0;
|
157 |
_gethostname = 0;
|
158 |
_inet_ntoa = 0;
|
159 |
_gethostbyname = 0;
|
160 |
_send = 0;
|
161 |
_sendto = 0;
|
162 |
_recv = 0;
|
163 |
_recvfrom = 0;
|
164 |
_listen = 0;
|
165 |
_accept = 0;
|
166 |
_socket = 0;
|
167 |
_bind = 0;
|
168 |
_WSAAsyncSelect = 0;
|
169 |
_closesocket = 0;
|
170 |
_getsockname = 0;
|
171 |
_WSARecvFrom = 0;
|
172 |
_WSAGetLastError = 0;
|
173 |
_WSAConnect = 0;
|
174 |
_setsockopt = 0;
|
175 |
_WSAEventSelect = 0;
|
176 |
_WSACreateEvent = 0;
|
177 |
_WSACloseEvent = 0;
|
178 |
_WSAResetEvent = 0;
|
179 |
_WSAEnumNetworkEvents = 0;
|
180 |
_shutdown = 0;
|
181 |
_WSASend = 0;
|
182 |
_WSARecv = 0;
|
183 |
_inet_addr = 0;
|
184 |
}
|