vbscript: Fix memory leak in owned safearray iterator.
[wine.git] / dlls / inetcomm / internettransport.c
blobf7060abe9e75ec13edff14a0cea47be5ed3287df
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 "ws2tcpip.h"
24 #include "windef.h"
25 #include "winnt.h"
26 #include "objbase.h"
27 #include "ole2.h"
28 #include "mimeole.h"
30 #include <stdio.h>
32 #include "wine/debug.h"
34 #include "inetcomm_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(inetcomm);
38 static const WCHAR wszClassName[] = L"ThorConnWndClass";
40 #define IX_READ (WM_USER + 0)
41 #define IX_READLINE (WM_USER + 1)
42 #define IX_WRITE (WM_USER + 2)
44 HRESULT InternetTransport_Init(InternetTransport *This)
46 This->pCallback = NULL;
47 This->Status = IXP_DISCONNECTED;
48 This->Socket = -1;
49 This->fCommandLogging = FALSE;
50 This->fnCompletion = NULL;
52 return S_OK;
55 HRESULT InternetTransport_GetServerInfo(InternetTransport *This, LPINETSERVER pInetServer)
57 if (This->Status == IXP_DISCONNECTED)
58 return IXP_E_NOT_CONNECTED;
60 *pInetServer = This->ServerInfo;
61 return S_OK;
64 HRESULT InternetTransport_InetServerFromAccount(InternetTransport *This,
65 IImnAccount *pAccount, LPINETSERVER pInetServer)
67 FIXME("(%p, %p): stub\n", pAccount, pInetServer);
68 return E_NOTIMPL;
71 HRESULT InternetTransport_Connect(InternetTransport *This,
72 LPINETSERVER pInetServer, boolean fAuthenticate, boolean fCommandLogging)
74 struct addrinfo *ai;
75 struct addrinfo *ai_cur;
76 struct addrinfo hints;
77 int ret;
78 char szPort[10];
80 if (This->Status != IXP_DISCONNECTED)
81 return IXP_E_ALREADY_CONNECTED;
83 This->ServerInfo = *pInetServer;
84 This->fCommandLogging = fCommandLogging;
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);
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;
100 snprintf(szPort, sizeof(szPort), "%d", (unsigned short)pInetServer->dwPort);
102 InternetTransport_ChangeStatus(This, IXP_FINDINGHOST);
104 ret = getaddrinfo(pInetServer->szServerName, szPort, &hints, &ai);
105 if (ret)
107 ERR("getaddrinfo failed: %d\n", ret);
108 return IXP_E_CANT_FIND_HOST;
111 for (ai_cur = ai; ai_cur; ai_cur = ai->ai_next)
113 int so;
115 if (TRACE_ON(inetcomm))
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);
125 InternetTransport_ChangeStatus(This, IXP_CONNECTING);
127 so = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
128 if (so == -1)
130 WARN("socket() failed\n");
131 continue;
133 This->Socket = so;
135 /* FIXME: set to async */
137 if (0 > connect(This->Socket, ai_cur->ai_addr, ai_cur->ai_addrlen))
139 WARN("connect() failed\n");
140 closesocket(This->Socket);
141 continue;
143 InternetTransport_ChangeStatus(This, IXP_CONNECTED);
145 /* FIXME: call WSAAsyncSelect */
147 freeaddrinfo(ai);
148 TRACE("connected\n");
149 return S_OK;
152 freeaddrinfo(ai);
154 return IXP_E_CANT_FIND_HOST;
157 HRESULT InternetTransport_HandsOffCallback(InternetTransport *This)
159 if (!This->pCallback)
160 return S_FALSE;
162 ITransportCallback_Release(This->pCallback);
163 This->pCallback = NULL;
165 return S_OK;
168 HRESULT InternetTransport_DropConnection(InternetTransport *This)
170 if (This->Status == IXP_DISCONNECTED)
171 return IXP_E_NOT_CONNECTED;
173 shutdown(This->Socket, SD_BOTH);
175 closesocket(This->Socket);
177 DestroyWindow(This->hwnd);
178 This->hwnd = NULL;
180 InternetTransport_ChangeStatus(This, IXP_DISCONNECTED);
182 return S_OK;
185 HRESULT InternetTransport_GetStatus(InternetTransport *This,
186 IXPSTATUS *pCurrentStatus)
188 *pCurrentStatus = This->Status;
189 return S_OK;
192 HRESULT InternetTransport_ChangeStatus(InternetTransport *This, IXPSTATUS Status)
194 This->Status = Status;
195 if (This->pCallback)
196 ITransportCallback_OnStatus(This->pCallback, Status,
197 (IInternetTransport *)&This->u.vtbl);
198 return S_OK;
201 HRESULT InternetTransport_ReadLine(InternetTransport *This,
202 INETXPORT_COMPLETION_FUNCTION fnCompletion)
204 if (This->Status == IXP_DISCONNECTED)
205 return IXP_E_NOT_CONNECTED;
207 if (This->fnCompletion)
208 return IXP_E_BUSY;
210 This->fnCompletion = fnCompletion;
212 This->cbBuffer = 1024;
213 This->pBuffer = HeapAlloc(GetProcessHeap(), 0, This->cbBuffer);
214 This->iCurrentBufferOffset = 0;
216 if (WSAAsyncSelect(This->Socket, This->hwnd, IX_READLINE, FD_READ) == SOCKET_ERROR)
218 ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
219 /* FIXME: handle error */
221 return S_OK;
224 HRESULT InternetTransport_Write(InternetTransport *This, const char *pvData,
225 int cbSize, INETXPORT_COMPLETION_FUNCTION fnCompletion)
227 int ret;
229 if (This->Status == IXP_DISCONNECTED)
230 return IXP_E_NOT_CONNECTED;
232 if (This->fnCompletion)
233 return IXP_E_BUSY;
235 /* FIXME: do this asynchronously */
236 ret = send(This->Socket, pvData, cbSize, 0);
237 if (ret == SOCKET_ERROR)
239 ERR("send failed with error %d\n", WSAGetLastError());
240 /* FIXME: handle error */
243 fnCompletion((IInternetTransport *)&This->u.vtbl, NULL, 0);
245 return S_OK;
248 HRESULT InternetTransport_DoCommand(InternetTransport *This,
249 LPCSTR pszCommand, INETXPORT_COMPLETION_FUNCTION fnCompletion)
251 if (This->Status == IXP_DISCONNECTED)
252 return IXP_E_NOT_CONNECTED;
254 if (This->fnCompletion)
255 return IXP_E_BUSY;
257 if (This->pCallback && This->fCommandLogging)
259 ITransportCallback_OnCommand(This->pCallback, CMD_SEND, (LPSTR)pszCommand, 0,
260 (IInternetTransport *)&This->u.vtbl);
262 return InternetTransport_Write(This, pszCommand, strlen(pszCommand), fnCompletion);
265 static LRESULT CALLBACK InternetTransport_WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
267 if (uMsg == IX_READ)
269 InternetTransport *This = (InternetTransport *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
271 /* no work to do */
272 if (!This->fnCompletion)
273 return 0;
275 while (This->iCurrentBufferOffset < This->cbBuffer)
277 if (recv(This->Socket, &This->pBuffer[This->iCurrentBufferOffset], 1, 0) <= 0)
279 if (WSAGetLastError() == WSAEWOULDBLOCK)
280 break;
282 ERR("recv failed with error %d\n", WSAGetLastError());
283 /* FIXME: handle error */
286 This->iCurrentBufferOffset++;
288 if (This->iCurrentBufferOffset == This->cbBuffer)
290 INETXPORT_COMPLETION_FUNCTION fnCompletion = This->fnCompletion;
291 char *pBuffer;
293 This->fnCompletion = NULL;
294 pBuffer = This->pBuffer;
295 This->pBuffer = NULL;
296 fnCompletion((IInternetTransport *)&This->u.vtbl, pBuffer,
297 This->iCurrentBufferOffset);
298 HeapFree(GetProcessHeap(), 0, pBuffer);
299 return 0;
302 if (WSAAsyncSelect(This->Socket, hwnd, uMsg, FD_READ) == SOCKET_ERROR)
304 ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
305 /* FIXME: handle error */
307 return 0;
309 else if (uMsg == IX_READLINE)
311 InternetTransport *This = (InternetTransport *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
313 /* no work to do */
314 if (!This->fnCompletion)
315 return 0;
317 while (This->iCurrentBufferOffset < This->cbBuffer - 1)
319 fd_set infd;
321 if (recv(This->Socket, &This->pBuffer[This->iCurrentBufferOffset], 1, 0) <= 0)
323 if (WSAGetLastError() == WSAEWOULDBLOCK)
324 break;
326 ERR("recv failed with error %d\n", WSAGetLastError());
327 /* FIXME: handle error */
328 return 0;
331 if (This->pBuffer[This->iCurrentBufferOffset] == '\n')
333 INETXPORT_COMPLETION_FUNCTION fnCompletion = This->fnCompletion;
334 char *pBuffer;
336 This->fnCompletion = NULL;
337 This->pBuffer[This->iCurrentBufferOffset++] = '\0';
338 pBuffer = This->pBuffer;
339 This->pBuffer = NULL;
341 fnCompletion((IInternetTransport *)&This->u.vtbl, pBuffer,
342 This->iCurrentBufferOffset);
344 HeapFree(GetProcessHeap(), 0, pBuffer);
345 return 0;
347 if (This->pBuffer[This->iCurrentBufferOffset] != '\r')
348 This->iCurrentBufferOffset++;
350 FD_ZERO(&infd);
351 FD_SET(This->Socket, &infd);
353 if (This->iCurrentBufferOffset == This->cbBuffer - 1)
354 return 0;
356 if (WSAAsyncSelect(This->Socket, hwnd, uMsg, FD_READ) == SOCKET_ERROR)
358 ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
359 /* FIXME: handle error */
361 return 0;
363 else
364 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
367 BOOL InternetTransport_RegisterClass(HINSTANCE hInstance)
369 WNDCLASSW cls;
370 WSADATA wsadata;
372 if (WSAStartup(MAKEWORD(2, 2), &wsadata))
373 return FALSE;
375 memset(&cls, 0, sizeof(cls));
376 cls.hInstance = hInstance;
377 cls.lpfnWndProc = InternetTransport_WndProc;
378 cls.lpszClassName = wszClassName;
380 return RegisterClassW(&cls);
383 void InternetTransport_UnregisterClass(HINSTANCE hInstance)
385 UnregisterClassW(wszClassName, hInstance);
386 WSACleanup();