inetcomm: Unfold headers.
[wine] / dlls / inetcomm / internettransport.c
1 /*
2  * Internet Messaging Transport Base Class
3  *
4  * Copyright 2006 Robert Shearman for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnt.h"
29 #include "winsock2.h"
30 #include "ws2tcpip.h"
31
32 #include "wine/debug.h"
33
34 #include "inetcomm_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(inetcomm);
37
38 static const WCHAR wszClassName[] = {'T','h','o','r','C','o','n','n','W','n','d','C','l','a','s','s',0};
39
40 #define IX_READ     (WM_USER + 0)
41 #define IX_READLINE (WM_USER + 1)
42 #define IX_WRITE    (WM_USER + 2)
43
44 HRESULT InternetTransport_Init(InternetTransport *This)
45 {
46     This->pCallback = NULL;
47     This->Status = IXP_DISCONNECTED;
48     This->Socket = -1;
49     This->fCommandLogging = FALSE;
50     This->fnCompletion = NULL;
51
52     return S_OK;
53 }
54
55 HRESULT InternetTransport_GetServerInfo(InternetTransport *This, LPINETSERVER pInetServer)
56 {
57     if (This->Status == IXP_DISCONNECTED)
58         return IXP_E_NOT_CONNECTED;
59
60     memcpy(pInetServer, &This->ServerInfo, sizeof(*pInetServer));
61     return S_OK;
62 }
63
64 HRESULT InternetTransport_InetServerFromAccount(InternetTransport *This,
65     IImnAccount *pAccount, LPINETSERVER pInetServer)
66 {
67     FIXME("(%p, %p): stub\n", pAccount, pInetServer);
68     return E_NOTIMPL;
69 }
70
71 HRESULT InternetTransport_Connect(InternetTransport *This,
72     LPINETSERVER pInetServer, boolean fAuthenticate, boolean fCommandLogging)
73 {
74     struct addrinfo *ai;
75     struct addrinfo *ai_cur;
76     struct addrinfo hints;
77     int ret;
78     char szPort[10];
79
80     if (This->Status != IXP_DISCONNECTED)
81         return IXP_E_ALREADY_CONNECTED;
82
83     memcpy(&This->ServerInfo, pInetServer, sizeof(This->ServerInfo));
84     This->fCommandLogging = fCommandLogging;
85
86     This->hwnd = CreateWindowW(wszClassName, wszClassName, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0);
87     if (!This->hwnd)
88         return HRESULT_FROM_WIN32(GetLastError());
89     SetWindowLongPtrW(This->hwnd, GWLP_USERDATA, (LONG_PTR)This);
90
91     hints.ai_flags          = 0;
92     hints.ai_family         = PF_UNSPEC;
93     hints.ai_socktype       = SOCK_STREAM;
94     hints.ai_protocol       = IPPROTO_TCP;
95     hints.ai_addrlen        = 0;
96     hints.ai_addr           = NULL;
97     hints.ai_canonname      = NULL;
98     hints.ai_next           = NULL;
99
100     snprintf(szPort, sizeof(szPort), "%d", (unsigned short)pInetServer->dwPort);
101
102     InternetTransport_ChangeStatus(This, IXP_FINDINGHOST);
103
104     ret = getaddrinfo(pInetServer->szServerName, szPort, &hints, &ai);
105     if (ret)
106     {
107         ERR("getaddrinfo failed: %d\n", ret);
108         return IXP_E_CANT_FIND_HOST;
109     }
110
111     for (ai_cur = ai; ai_cur; ai_cur = ai->ai_next)
112     {
113         int so;
114
115         if (TRACE_ON(inetcomm))
116         {
117             char host[256];
118             char service[256];
119             getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
120                 host, sizeof(host), service, sizeof(service),
121                 NI_NUMERICHOST | NI_NUMERICSERV);
122             TRACE("trying %s:%s\n", host, service);
123         }
124
125         InternetTransport_ChangeStatus(This, IXP_CONNECTING);
126
127         so = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
128         if (so == -1)
129         {
130             WARN("socket() failed\n");
131             continue;
132         }
133         This->Socket = so;
134
135         /* FIXME: set to async */
136
137         if (0 > connect(This->Socket, ai_cur->ai_addr, ai_cur->ai_addrlen))
138         {
139             WARN("connect() failed\n");
140             closesocket(This->Socket);
141             continue;
142         }
143         InternetTransport_ChangeStatus(This, IXP_CONNECTED);
144
145         /* FIXME: call WSAAsyncSelect */
146
147         freeaddrinfo(ai);
148         TRACE("connected\n");
149         return S_OK;
150     }
151
152     freeaddrinfo(ai);
153
154     return IXP_E_CANT_FIND_HOST;
155 }
156
157 HRESULT InternetTransport_HandsOffCallback(InternetTransport *This)
158 {
159     if (!This->pCallback)
160         return S_FALSE;
161
162     ITransportCallback_Release(This->pCallback);
163     This->pCallback = NULL;
164
165     return S_OK;
166 }
167
168 HRESULT InternetTransport_DropConnection(InternetTransport *This)
169 {
170     int ret;
171
172     if (This->Status == IXP_DISCONNECTED)
173         return IXP_E_NOT_CONNECTED;
174
175     ret = shutdown(This->Socket, SD_BOTH);
176
177     ret = closesocket(This->Socket);
178
179     DestroyWindow(This->hwnd);
180     This->hwnd = NULL;
181
182     InternetTransport_ChangeStatus(This, IXP_DISCONNECTED);
183
184     return S_OK;
185 }
186
187 HRESULT InternetTransport_GetStatus(InternetTransport *This,
188     IXPSTATUS *pCurrentStatus)
189 {
190     *pCurrentStatus = This->Status;
191     return S_OK;
192 }
193
194 HRESULT InternetTransport_ChangeStatus(InternetTransport *This, IXPSTATUS Status)
195 {
196     This->Status = Status;
197     if (This->pCallback)
198         ITransportCallback_OnStatus(This->pCallback, Status,
199             (IInternetTransport *)&This->u.vtbl);
200     return S_OK;
201 }
202
203 HRESULT InternetTransport_Read(InternetTransport *This, int cbBuffer,
204     INETXPORT_COMPLETION_FUNCTION fnCompletion)
205 {
206     if (This->Status == IXP_DISCONNECTED)
207         return IXP_E_NOT_CONNECTED;
208
209     if (This->fnCompletion)
210         return IXP_E_BUSY;
211
212     This->fnCompletion = fnCompletion;
213
214     This->cbBuffer = cbBuffer;
215     This->pBuffer = HeapAlloc(GetProcessHeap(), 0, This->cbBuffer);
216     This->iCurrentBufferOffset = 0;
217
218     if (WSAAsyncSelect(This->Socket, This->hwnd, IX_READ, FD_READ) == SOCKET_ERROR)
219     {
220         ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
221         /* FIXME: handle error */
222     }
223     return S_OK;
224 }
225
226 HRESULT InternetTransport_ReadLine(InternetTransport *This,
227     INETXPORT_COMPLETION_FUNCTION fnCompletion)
228 {
229     if (This->Status == IXP_DISCONNECTED)
230         return IXP_E_NOT_CONNECTED;
231
232     if (This->fnCompletion)
233         return IXP_E_BUSY;
234
235     This->fnCompletion = fnCompletion;
236
237     This->cbBuffer = 1024;
238     This->pBuffer = HeapAlloc(GetProcessHeap(), 0, This->cbBuffer);
239     This->iCurrentBufferOffset = 0;
240
241     if (WSAAsyncSelect(This->Socket, This->hwnd, IX_READLINE, FD_READ) == SOCKET_ERROR)
242     {
243         ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
244         /* FIXME: handle error */
245     }
246     return S_OK;
247 }
248
249 HRESULT InternetTransport_Write(InternetTransport *This, const char *pvData,
250     int cbSize, INETXPORT_COMPLETION_FUNCTION fnCompletion)
251 {
252     int ret;
253
254     if (This->Status == IXP_DISCONNECTED)
255         return IXP_E_NOT_CONNECTED;
256
257     if (This->fnCompletion)
258         return IXP_E_BUSY;
259
260     /* FIXME: do this asynchronously */
261     ret = send(This->Socket, pvData, cbSize, 0);
262     if (ret == SOCKET_ERROR)
263     {
264         ERR("send failed with error %d\n", WSAGetLastError());
265         /* FIXME: handle error */
266     }
267
268     fnCompletion((IInternetTransport *)&This->u.vtbl, NULL, 0);
269
270     return S_OK;
271 }
272
273 static LRESULT CALLBACK InternetTransport_WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
274 {
275     if (uMsg == IX_READ)
276     {
277         InternetTransport *This = (InternetTransport *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
278
279         /* no work to do */
280         if (!This->fnCompletion)
281             return 0;
282
283         while (This->iCurrentBufferOffset < This->cbBuffer)
284         {
285             if (recv(This->Socket, &This->pBuffer[This->iCurrentBufferOffset], 1, 0) <= 0)
286             {
287                 if (WSAGetLastError() == WSAEWOULDBLOCK)
288                     break;
289
290                 ERR("recv failed with error %d\n", WSAGetLastError());
291                 /* FIXME: handle error */
292             }
293
294             This->iCurrentBufferOffset++;
295         }
296         if (This->iCurrentBufferOffset == This->cbBuffer)
297         {
298             INETXPORT_COMPLETION_FUNCTION fnCompletion = This->fnCompletion;
299             char *pBuffer;
300
301             This->fnCompletion = NULL;
302             pBuffer = This->pBuffer;
303             This->pBuffer = NULL;
304             fnCompletion((IInternetTransport *)&This->u.vtbl, pBuffer,
305                 This->iCurrentBufferOffset);
306             HeapFree(GetProcessHeap(), 0, pBuffer);
307             return 0;
308         }
309
310         if (WSAAsyncSelect(This->Socket, hwnd, uMsg, FD_READ) == SOCKET_ERROR)
311         {
312             ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
313             /* FIXME: handle error */
314         }
315         return 0;
316     }
317     else if (uMsg == IX_READLINE)
318     {
319         InternetTransport *This = (InternetTransport *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
320
321         /* no work to do */
322         if (!This->fnCompletion)
323             return 0;
324
325         while (This->iCurrentBufferOffset < This->cbBuffer - 1)
326         {
327             struct timeval tv;
328             fd_set infd;
329
330             if (recv(This->Socket, &This->pBuffer[This->iCurrentBufferOffset], 1, 0) <= 0)
331             {
332                 if (WSAGetLastError() == WSAEWOULDBLOCK)
333                     break;
334
335                 ERR("recv failed with error %d\n", WSAGetLastError());
336                 /* FIXME: handle error */
337                 return 0;
338             }
339
340             if (This->pBuffer[This->iCurrentBufferOffset] == '\n')
341             {
342                 INETXPORT_COMPLETION_FUNCTION fnCompletion = This->fnCompletion;
343                 char *pBuffer;
344
345                 This->fnCompletion = NULL;
346                 This->pBuffer[This->iCurrentBufferOffset++] = '\0';
347                 pBuffer = This->pBuffer;
348                 This->pBuffer = NULL;
349
350                 fnCompletion((IInternetTransport *)&This->u.vtbl, pBuffer,
351                     This->iCurrentBufferOffset);
352
353                 HeapFree(GetProcessHeap(), 0, pBuffer);
354                 return 0;
355             }
356             if (This->pBuffer[This->iCurrentBufferOffset] != '\r')
357                 This->iCurrentBufferOffset++;
358
359             FD_ZERO(&infd);
360             FD_SET(This->Socket, &infd);
361             tv.tv_sec = 0;
362             tv.tv_usec = 0;
363         }
364         if (This->iCurrentBufferOffset == This->cbBuffer - 1)
365             return 0;
366
367         if (WSAAsyncSelect(This->Socket, hwnd, uMsg, FD_READ) == SOCKET_ERROR)
368         {
369             ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
370             /* FIXME: handle error */
371         }
372         return 0;
373     }
374     else
375         return DefWindowProcW(hwnd, uMsg, wParam, lParam);
376 }
377
378 BOOL InternetTransport_RegisterClass(HINSTANCE hInstance)
379 {
380     WNDCLASSW cls;
381     WSADATA wsadata;
382
383     if (WSAStartup(MAKEWORD(2, 2), &wsadata))
384         return FALSE;
385
386     memset(&cls, 0, sizeof(cls));
387     cls.hInstance     = hInstance;
388     cls.lpfnWndProc   = InternetTransport_WndProc;
389     cls.lpszClassName = wszClassName;
390
391     return RegisterClassW(&cls);
392 }
393
394 void InternetTransport_UnregisterClass(HINSTANCE hInstance)
395 {
396     UnregisterClassW(wszClassName, hInstance);
397     WSACleanup();
398 }