push d1f5df181c120dbe494f7c89b454752c2e0dcc04
[wine/hacks.git] / dlls / inetcomm / internettransport.c
blobf0499bc19a1fb85fcff9284dafc0a8e621c9de1c
1 /*
2 * Internet Messaging Transport Base Class
4 * Copyright 2006 Robert Shearman for CodeWeavers
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.
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.
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
21 #define COBJMACROS
23 #include <stdarg.h>
24 #include <stdio.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnt.h"
29 #include "winuser.h"
30 #include "winsock2.h"
31 #include "ws2tcpip.h"
32 #include "objbase.h"
33 #include "ole2.h"
34 #include "mimeole.h"
36 #include "wine/debug.h"
38 #include "inetcomm_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(inetcomm);
42 static const WCHAR wszClassName[] = {'T','h','o','r','C','o','n','n','W','n','d','C','l','a','s','s',0};
44 #define IX_READ (WM_USER + 0)
45 #define IX_READLINE (WM_USER + 1)
46 #define IX_WRITE (WM_USER + 2)
48 HRESULT InternetTransport_Init(InternetTransport *This)
50 This->pCallback = NULL;
51 This->Status = IXP_DISCONNECTED;
52 This->Socket = -1;
53 This->fCommandLogging = FALSE;
54 This->fnCompletion = NULL;
56 return S_OK;
59 HRESULT InternetTransport_GetServerInfo(InternetTransport *This, LPINETSERVER pInetServer)
61 if (This->Status == IXP_DISCONNECTED)
62 return IXP_E_NOT_CONNECTED;
64 *pInetServer = This->ServerInfo;
65 return S_OK;
68 HRESULT InternetTransport_InetServerFromAccount(InternetTransport *This,
69 IImnAccount *pAccount, LPINETSERVER pInetServer)
71 FIXME("(%p, %p): stub\n", pAccount, pInetServer);
72 return E_NOTIMPL;
75 HRESULT InternetTransport_Connect(InternetTransport *This,
76 LPINETSERVER pInetServer, boolean fAuthenticate, boolean fCommandLogging)
78 struct addrinfo *ai;
79 struct addrinfo *ai_cur;
80 struct addrinfo hints;
81 int ret;
82 char szPort[10];
84 if (This->Status != IXP_DISCONNECTED)
85 return IXP_E_ALREADY_CONNECTED;
87 This->ServerInfo = *pInetServer;
88 This->fCommandLogging = fCommandLogging;
90 This->hwnd = CreateWindowW(wszClassName, wszClassName, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0);
91 if (!This->hwnd)
92 return HRESULT_FROM_WIN32(GetLastError());
93 SetWindowLongPtrW(This->hwnd, GWLP_USERDATA, (LONG_PTR)This);
95 hints.ai_flags = 0;
96 hints.ai_family = PF_UNSPEC;
97 hints.ai_socktype = SOCK_STREAM;
98 hints.ai_protocol = IPPROTO_TCP;
99 hints.ai_addrlen = 0;
100 hints.ai_addr = NULL;
101 hints.ai_canonname = NULL;
102 hints.ai_next = NULL;
104 snprintf(szPort, sizeof(szPort), "%d", (unsigned short)pInetServer->dwPort);
106 InternetTransport_ChangeStatus(This, IXP_FINDINGHOST);
108 ret = getaddrinfo(pInetServer->szServerName, szPort, &hints, &ai);
109 if (ret)
111 ERR("getaddrinfo failed: %d\n", ret);
112 return IXP_E_CANT_FIND_HOST;
115 for (ai_cur = ai; ai_cur; ai_cur = ai->ai_next)
117 int so;
119 if (TRACE_ON(inetcomm))
121 char host[256];
122 char service[256];
123 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
124 host, sizeof(host), service, sizeof(service),
125 NI_NUMERICHOST | NI_NUMERICSERV);
126 TRACE("trying %s:%s\n", host, service);
129 InternetTransport_ChangeStatus(This, IXP_CONNECTING);
131 so = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
132 if (so == -1)
134 WARN("socket() failed\n");
135 continue;
137 This->Socket = so;
139 /* FIXME: set to async */
141 if (0 > connect(This->Socket, ai_cur->ai_addr, ai_cur->ai_addrlen))
143 WARN("connect() failed\n");
144 closesocket(This->Socket);
145 continue;
147 InternetTransport_ChangeStatus(This, IXP_CONNECTED);
149 /* FIXME: call WSAAsyncSelect */
151 freeaddrinfo(ai);
152 TRACE("connected\n");
153 return S_OK;
156 freeaddrinfo(ai);
158 return IXP_E_CANT_FIND_HOST;
161 HRESULT InternetTransport_HandsOffCallback(InternetTransport *This)
163 if (!This->pCallback)
164 return S_FALSE;
166 ITransportCallback_Release(This->pCallback);
167 This->pCallback = NULL;
169 return S_OK;
172 HRESULT InternetTransport_DropConnection(InternetTransport *This)
174 int ret;
176 if (This->Status == IXP_DISCONNECTED)
177 return IXP_E_NOT_CONNECTED;
179 ret = shutdown(This->Socket, SD_BOTH);
181 ret = closesocket(This->Socket);
183 DestroyWindow(This->hwnd);
184 This->hwnd = NULL;
186 InternetTransport_ChangeStatus(This, IXP_DISCONNECTED);
188 return S_OK;
191 HRESULT InternetTransport_GetStatus(InternetTransport *This,
192 IXPSTATUS *pCurrentStatus)
194 *pCurrentStatus = This->Status;
195 return S_OK;
198 HRESULT InternetTransport_ChangeStatus(InternetTransport *This, IXPSTATUS Status)
200 This->Status = Status;
201 if (This->pCallback)
202 ITransportCallback_OnStatus(This->pCallback, Status,
203 (IInternetTransport *)&This->u.vtbl);
204 return S_OK;
207 HRESULT InternetTransport_ReadLine(InternetTransport *This,
208 INETXPORT_COMPLETION_FUNCTION fnCompletion)
210 if (This->Status == IXP_DISCONNECTED)
211 return IXP_E_NOT_CONNECTED;
213 if (This->fnCompletion)
214 return IXP_E_BUSY;
216 This->fnCompletion = fnCompletion;
218 This->cbBuffer = 1024;
219 This->pBuffer = HeapAlloc(GetProcessHeap(), 0, This->cbBuffer);
220 This->iCurrentBufferOffset = 0;
222 if (WSAAsyncSelect(This->Socket, This->hwnd, IX_READLINE, FD_READ) == SOCKET_ERROR)
224 ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
225 /* FIXME: handle error */
227 return S_OK;
230 HRESULT InternetTransport_Write(InternetTransport *This, const char *pvData,
231 int cbSize, INETXPORT_COMPLETION_FUNCTION fnCompletion)
233 int ret;
235 if (This->Status == IXP_DISCONNECTED)
236 return IXP_E_NOT_CONNECTED;
238 if (This->fnCompletion)
239 return IXP_E_BUSY;
241 /* FIXME: do this asynchronously */
242 ret = send(This->Socket, pvData, cbSize, 0);
243 if (ret == SOCKET_ERROR)
245 ERR("send failed with error %d\n", WSAGetLastError());
246 /* FIXME: handle error */
249 fnCompletion((IInternetTransport *)&This->u.vtbl, NULL, 0);
251 return S_OK;
254 HRESULT InternetTransport_DoCommand(InternetTransport *This,
255 LPCSTR pszCommand, INETXPORT_COMPLETION_FUNCTION fnCompletion)
257 if (This->Status == IXP_DISCONNECTED)
258 return IXP_E_NOT_CONNECTED;
260 if (This->fnCompletion)
261 return IXP_E_BUSY;
263 if (This->pCallback && This->fCommandLogging)
265 ITransportCallback_OnCommand(This->pCallback, CMD_SEND, (LPSTR)pszCommand, 0,
266 (IInternetTransport *)&This->u.vtbl);
268 return InternetTransport_Write(This, pszCommand, strlen(pszCommand), fnCompletion);
271 static LRESULT CALLBACK InternetTransport_WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
273 if (uMsg == IX_READ)
275 InternetTransport *This = (InternetTransport *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
277 /* no work to do */
278 if (!This->fnCompletion)
279 return 0;
281 while (This->iCurrentBufferOffset < This->cbBuffer)
283 if (recv(This->Socket, &This->pBuffer[This->iCurrentBufferOffset], 1, 0) <= 0)
285 if (WSAGetLastError() == WSAEWOULDBLOCK)
286 break;
288 ERR("recv failed with error %d\n", WSAGetLastError());
289 /* FIXME: handle error */
292 This->iCurrentBufferOffset++;
294 if (This->iCurrentBufferOffset == This->cbBuffer)
296 INETXPORT_COMPLETION_FUNCTION fnCompletion = This->fnCompletion;
297 char *pBuffer;
299 This->fnCompletion = NULL;
300 pBuffer = This->pBuffer;
301 This->pBuffer = NULL;
302 fnCompletion((IInternetTransport *)&This->u.vtbl, pBuffer,
303 This->iCurrentBufferOffset);
304 HeapFree(GetProcessHeap(), 0, pBuffer);
305 return 0;
308 if (WSAAsyncSelect(This->Socket, hwnd, uMsg, FD_READ) == SOCKET_ERROR)
310 ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
311 /* FIXME: handle error */
313 return 0;
315 else if (uMsg == IX_READLINE)
317 InternetTransport *This = (InternetTransport *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
319 /* no work to do */
320 if (!This->fnCompletion)
321 return 0;
323 while (This->iCurrentBufferOffset < This->cbBuffer - 1)
325 fd_set infd;
327 if (recv(This->Socket, &This->pBuffer[This->iCurrentBufferOffset], 1, 0) <= 0)
329 if (WSAGetLastError() == WSAEWOULDBLOCK)
330 break;
332 ERR("recv failed with error %d\n", WSAGetLastError());
333 /* FIXME: handle error */
334 return 0;
337 if (This->pBuffer[This->iCurrentBufferOffset] == '\n')
339 INETXPORT_COMPLETION_FUNCTION fnCompletion = This->fnCompletion;
340 char *pBuffer;
342 This->fnCompletion = NULL;
343 This->pBuffer[This->iCurrentBufferOffset++] = '\0';
344 pBuffer = This->pBuffer;
345 This->pBuffer = NULL;
347 fnCompletion((IInternetTransport *)&This->u.vtbl, pBuffer,
348 This->iCurrentBufferOffset);
350 HeapFree(GetProcessHeap(), 0, pBuffer);
351 return 0;
353 if (This->pBuffer[This->iCurrentBufferOffset] != '\r')
354 This->iCurrentBufferOffset++;
356 FD_ZERO(&infd);
357 FD_SET(This->Socket, &infd);
359 if (This->iCurrentBufferOffset == This->cbBuffer - 1)
360 return 0;
362 if (WSAAsyncSelect(This->Socket, hwnd, uMsg, FD_READ) == SOCKET_ERROR)
364 ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
365 /* FIXME: handle error */
367 return 0;
369 else
370 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
373 BOOL InternetTransport_RegisterClass(HINSTANCE hInstance)
375 WNDCLASSW cls;
376 WSADATA wsadata;
378 if (WSAStartup(MAKEWORD(2, 2), &wsadata))
379 return FALSE;
381 memset(&cls, 0, sizeof(cls));
382 cls.hInstance = hInstance;
383 cls.lpfnWndProc = InternetTransport_WndProc;
384 cls.lpszClassName = wszClassName;
386 return RegisterClassW(&cls);
389 void InternetTransport_UnregisterClass(HINSTANCE hInstance)
391 UnregisterClassW(wszClassName, hInstance);
392 WSACleanup();