OSDN Git Service

Fix bugs of UTF-8 to UTF-16 API bridge.
[ffftp/ffftp.git] / socketwrapper.c
1 // socketwrapper.cpp
2 // Copyright (C) 2011 Suguru Kawamoto
3 // \83\\83P\83b\83g\83\89\83b\83p\81[
4 // socket\8aÖ\98A\8aÖ\90\94\82ðOpenSSL\97p\82É\92u\8a·
5 // \83R\83\93\83p\83C\83\8b\82É\82ÍOpenSSL\82Ì\83w\83b\83_\81[\83t\83@\83C\83\8b\82ª\95K\97v
6 // \8eÀ\8ds\82É\82ÍOpenSSL\82ÌDLL\82ª\95K\97v
7
8 #include <windows.h>
9 #include <mmsystem.h>
10 #include <openssl/ssl.h>
11
12 #include "socketwrapper.h"
13
14 typedef void (__stdcall* _SSL_load_error_strings)();
15 typedef int (__stdcall* _SSL_library_init)();
16 typedef SSL_METHOD* (__stdcall* _SSLv23_method)();
17 typedef SSL_CTX* (__stdcall* _SSL_CTX_new)(SSL_METHOD*);
18 typedef void (__stdcall* _SSL_CTX_free)(SSL_CTX*);
19 typedef SSL* (__stdcall* _SSL_new)(SSL_CTX*);
20 typedef void (__stdcall* _SSL_free)(SSL*);
21 typedef int (__stdcall* _SSL_shutdown)(SSL*);
22 typedef int (__stdcall* _SSL_get_fd)(SSL*);
23 typedef int (__stdcall* _SSL_set_fd)(SSL*, int);
24 typedef int (__stdcall* _SSL_accept)(SSL*);
25 typedef int (__stdcall* _SSL_connect)(SSL*);
26 typedef int (__stdcall* _SSL_write)(SSL*, const void*, int);
27 typedef int (__stdcall* _SSL_peek)(SSL*, void*, int);
28 typedef int (__stdcall* _SSL_read)(SSL*, void*, int);
29 typedef int (__stdcall* _SSL_get_error)(SSL*, int);
30
31 _SSL_load_error_strings pSSL_load_error_strings;
32 _SSL_library_init pSSL_library_init;
33 _SSLv23_method pSSLv23_method;
34 _SSL_CTX_new pSSL_CTX_new;
35 _SSL_CTX_free pSSL_CTX_free;
36 _SSL_new pSSL_new;
37 _SSL_free pSSL_free;
38 _SSL_shutdown pSSL_shutdown;
39 _SSL_get_fd pSSL_get_fd;
40 _SSL_set_fd pSSL_set_fd;
41 _SSL_accept pSSL_accept;
42 _SSL_connect pSSL_connect;
43 _SSL_write pSSL_write;
44 _SSL_peek pSSL_peek;
45 _SSL_read pSSL_read;
46 _SSL_get_error pSSL_get_error;
47
48 #define MAX_SSL_SOCKET 16
49
50 BOOL g_bOpenSSLLoaded;
51 HMODULE g_hOpenSSL;
52 CRITICAL_SECTION g_OpenSSLLock;
53 DWORD g_OpenSSLTimeout;
54 LPSSLTIMEOUTCALLBACK g_pOpenSSLTimeoutCallback;
55 SSL_CTX* g_pOpenSSLCTX;
56 SSL* g_pOpenSSLHandle[MAX_SSL_SOCKET];
57
58 BOOL __stdcall DefaultSSLTimeoutCallback()
59 {
60         Sleep(100);
61         return FALSE;
62 }
63
64 BOOL LoadOpenSSL()
65 {
66         if(g_bOpenSSLLoaded)
67                 return FALSE;
68         g_hOpenSSL = LoadLibrary("ssleay32.dll");
69         if(!g_hOpenSSL)
70                 g_hOpenSSL = LoadLibrary("libssl32.dll");
71         if(!g_hOpenSSL
72                 || !(pSSL_load_error_strings = (_SSL_load_error_strings)GetProcAddress(g_hOpenSSL, "SSL_load_error_strings"))
73                 || !(pSSL_library_init = (_SSL_library_init)GetProcAddress(g_hOpenSSL, "SSL_library_init"))
74                 || !(pSSLv23_method = (_SSLv23_method)GetProcAddress(g_hOpenSSL, "SSLv23_method"))
75                 || !(pSSL_CTX_new = (_SSL_CTX_new)GetProcAddress(g_hOpenSSL, "SSL_CTX_new"))
76                 || !(pSSL_CTX_free = (_SSL_CTX_free)GetProcAddress(g_hOpenSSL, "SSL_CTX_free"))
77                 || !(pSSL_new = (_SSL_new)GetProcAddress(g_hOpenSSL, "SSL_new"))
78                 || !(pSSL_free = (_SSL_free)GetProcAddress(g_hOpenSSL, "SSL_free"))
79                 || !(pSSL_shutdown = (_SSL_shutdown)GetProcAddress(g_hOpenSSL, "SSL_shutdown"))
80                 || !(pSSL_get_fd = (_SSL_get_fd)GetProcAddress(g_hOpenSSL, "SSL_get_fd"))
81                 || !(pSSL_set_fd = (_SSL_set_fd)GetProcAddress(g_hOpenSSL, "SSL_set_fd"))
82                 || !(pSSL_accept = (_SSL_accept)GetProcAddress(g_hOpenSSL, "SSL_accept"))
83                 || !(pSSL_connect = (_SSL_connect)GetProcAddress(g_hOpenSSL, "SSL_connect"))
84                 || !(pSSL_write = (_SSL_write)GetProcAddress(g_hOpenSSL, "SSL_write"))
85                 || !(pSSL_peek = (_SSL_peek)GetProcAddress(g_hOpenSSL, "SSL_peek"))
86                 || !(pSSL_read = (_SSL_read)GetProcAddress(g_hOpenSSL, "SSL_read"))
87                 || !(pSSL_get_error = (_SSL_get_error)GetProcAddress(g_hOpenSSL, "SSL_get_error")))
88         {
89                 FreeLibrary(g_hOpenSSL);
90                 g_hOpenSSL = NULL;
91                 return FALSE;
92         }
93         InitializeCriticalSection(&g_OpenSSLLock);
94         pSSL_load_error_strings();
95         pSSL_library_init();
96         SetSSLTimeoutCallback(60000, DefaultSSLTimeoutCallback);
97         g_bOpenSSLLoaded = TRUE;
98         return TRUE;
99 }
100
101 void FreeOpenSSL()
102 {
103         int i;
104         if(!g_bOpenSSLLoaded)
105                 return;
106         EnterCriticalSection(&g_OpenSSLLock);
107         for(i = 0; i < MAX_SSL_SOCKET; i++)
108         {
109                 if(g_pOpenSSLHandle[i])
110                 {
111                         pSSL_shutdown(g_pOpenSSLHandle[i]);
112                         pSSL_free(g_pOpenSSLHandle[i]);
113                         g_pOpenSSLHandle[i] = NULL;
114                 }
115         }
116         if(g_pOpenSSLCTX)
117                 pSSL_CTX_free(g_pOpenSSLCTX);
118         g_pOpenSSLCTX = NULL;
119         FreeLibrary(g_hOpenSSL);
120         g_hOpenSSL = NULL;
121         LeaveCriticalSection(&g_OpenSSLLock);
122         DeleteCriticalSection(&g_OpenSSLLock);
123         g_bOpenSSLLoaded = FALSE;
124 }
125
126 BOOL IsOpenSSLLoaded()
127 {
128         return g_bOpenSSLLoaded;
129 }
130
131 SSL** GetUnusedSSLPointer()
132 {
133         int i;
134         for(i = 0; i < MAX_SSL_SOCKET; i++)
135         {
136                 if(!g_pOpenSSLHandle[i])
137                         return &g_pOpenSSLHandle[i];
138         }
139         return NULL;
140 }
141
142 SSL** FindSSLPointerFromSocket(SOCKET s)
143 {
144         int i;
145         for(i = 0; i < MAX_SSL_SOCKET; i++)
146         {
147                 if(g_pOpenSSLHandle[i])
148                 {
149                         if(pSSL_get_fd(g_pOpenSSLHandle[i]) == s)
150                                 return &g_pOpenSSLHandle[i];
151                 }
152         }
153         return NULL;
154 }
155
156 void SetSSLTimeoutCallback(DWORD Timeout, LPSSLTIMEOUTCALLBACK pCallback)
157 {
158         EnterCriticalSection(&g_OpenSSLLock);
159         g_OpenSSLTimeout = Timeout;
160         g_pOpenSSLTimeoutCallback = pCallback;
161         LeaveCriticalSection(&g_OpenSSLLock);
162 }
163
164 BOOL AttachSSL(SOCKET s)
165 {
166         BOOL r;
167         DWORD Time;
168         SSL** ppSSL;
169         r = FALSE;
170         Time = timeGetTime();
171         EnterCriticalSection(&g_OpenSSLLock);
172         if(!g_pOpenSSLCTX)
173                 g_pOpenSSLCTX = pSSL_CTX_new(pSSLv23_method());
174         if(g_pOpenSSLCTX)
175         {
176                 if(ppSSL = GetUnusedSSLPointer())
177                 {
178                         if(*ppSSL = pSSL_new(g_pOpenSSLCTX))
179                         {
180                                 if(pSSL_set_fd(*ppSSL, s) != 0)
181                                 {
182                                         r = TRUE;
183                                         // SSL\82Ì\83l\83S\83V\83G\81[\83V\83\87\83\93\82É\82Í\8e\9e\8aÔ\82ª\82©\82©\82é\8fê\8d\87\82ª\82 \82é
184                                         while(pSSL_connect(*ppSSL) != 1)
185                                         {
186                                                 LeaveCriticalSection(&g_OpenSSLLock);
187                                                 if(g_pOpenSSLTimeoutCallback() || timeGetTime() - Time >= g_OpenSSLTimeout)
188                                                 {
189                                                         DetachSSL(s);
190                                                         r = FALSE;
191                                                         EnterCriticalSection(&g_OpenSSLLock);
192                                                         break;
193                                                 }
194                                                 EnterCriticalSection(&g_OpenSSLLock);
195                                         }
196                                 }
197                                 else
198                                         DetachSSL(s);
199                         }
200                 }
201         }
202         LeaveCriticalSection(&g_OpenSSLLock);
203         return r;
204 }
205
206 BOOL DetachSSL(SOCKET s)
207 {
208         BOOL r;
209         SSL** ppSSL;
210         r = FALSE;
211         EnterCriticalSection(&g_OpenSSLLock);
212         if(ppSSL = FindSSLPointerFromSocket(s))
213         {
214                 pSSL_shutdown(*ppSSL);
215                 pSSL_free(*ppSSL);
216                 *ppSSL = NULL;
217                 r = TRUE;
218         }
219         LeaveCriticalSection(&g_OpenSSLLock);
220         return r;
221 }
222
223 BOOL IsSSLAttached(SOCKET s)
224 {
225         SSL** ppSSL;
226         EnterCriticalSection(&g_OpenSSLLock);
227         ppSSL = FindSSLPointerFromSocket(s);
228         LeaveCriticalSection(&g_OpenSSLLock);
229         if(!ppSSL)
230                 return TRUE;
231         return TRUE;
232 }
233
234 SOCKET socketS(int af, int type, int protocol)
235 {
236         return socket(af, type, protocol);
237 }
238
239 int bindS(SOCKET s, const struct sockaddr *addr, int namelen)
240 {
241         return bind(s, addr, namelen);
242 }
243
244 int listenS(SOCKET s, int backlog)
245 {
246         return listen(s, backlog);
247 }
248
249 SOCKET acceptS(SOCKET s, struct sockaddr *addr, int *addrlen)
250 {
251         SOCKET r;
252         r = accept(s, addr, addrlen);
253         if(!AttachSSL(r))
254         {
255                 closesocket(r);
256                 return INVALID_SOCKET;
257         }
258         return r;
259 }
260
261 int connectS(SOCKET s, const struct sockaddr *name, int namelen)
262 {
263         int r;
264         r = connect(s, name, namelen);
265         if(!AttachSSL(r))
266                 return SOCKET_ERROR;
267         return r;
268 }
269
270 int closesocketS(SOCKET s)
271 {
272         DetachSSL(s);
273         return closesocket(s);
274 }
275
276 int sendS(SOCKET s, const char * buf, int len, int flags)
277 {
278         SSL** ppSSL;
279         EnterCriticalSection(&g_OpenSSLLock);
280         ppSSL = FindSSLPointerFromSocket(s);
281         LeaveCriticalSection(&g_OpenSSLLock);
282         if(!ppSSL)
283                 return send(s, buf, len, flags);
284         return pSSL_write(*ppSSL, buf, len);
285 }
286
287 int recvS(SOCKET s, char * buf, int len, int flags)
288 {
289         SSL** ppSSL;
290         EnterCriticalSection(&g_OpenSSLLock);
291         ppSSL = FindSSLPointerFromSocket(s);
292         LeaveCriticalSection(&g_OpenSSLLock);
293         if(!ppSSL)
294                 return recv(s, buf, len, flags);
295         if(flags & MSG_PEEK)
296                 return pSSL_peek(*ppSSL, buf, len);
297         return pSSL_read(*ppSSL, buf, len);
298 }
299