xref: /aoo42x/main/sal/osl/w32/socket.cxx (revision 87d2adbc)
1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*87d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*87d2adbcSAndrew Rist  * distributed with this work for additional information
6*87d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*87d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*87d2adbcSAndrew Rist  *
11*87d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist  *
13*87d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist  * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*87d2adbcSAndrew Rist  * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist  * under the License.
19*87d2adbcSAndrew Rist  *
20*87d2adbcSAndrew Rist  *************************************************************/
21*87d2adbcSAndrew Rist 
22*87d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "system.h"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <osl/socket.h>
30cdf0e10cSrcweir #include <osl/diagnose.h>
31cdf0e10cSrcweir #include <rtl/alloc.h>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include "sockimpl.h"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir extern "C" {
36cdf0e10cSrcweir 
37cdf0e10cSrcweir /* defines for shutdown */
38cdf0e10cSrcweir #ifdef GCC
39cdf0e10cSrcweir #	define SD_RECEIVE 0
40cdf0e10cSrcweir #	define SD_SEND 1
41cdf0e10cSrcweir #	define SD_BOTH 2
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir 
44cdf0e10cSrcweir /*
45cdf0e10cSrcweir 	oslSocketAddr is a pointer to a Berkeley struct sockaddr.
46cdf0e10cSrcweir 	I refrained from using sockaddr_in because of possible further
47cdf0e10cSrcweir 	extensions of this socket-interface (IP-NG?).
48cdf0e10cSrcweir 	The intention was to hide all Berkeley data-structures from
49cdf0e10cSrcweir 	direct access past the osl-interface.
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	The current implementation is internet (IP) centered. All
52cdf0e10cSrcweir 	the constructor-functions (osl_create...) take parameters
53cdf0e10cSrcweir 	that will probably make sense only in the IP-environment
54cdf0e10cSrcweir 	(e.g. because of using the dotted-Addr-format).
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 	If the interface will be extended to host other protocol-
57cdf0e10cSrcweir 	families, I expect no externally visible changes in the
58cdf0e10cSrcweir 	existing functions. You'll probably need only new
59cdf0e10cSrcweir 	constructor-functions who take the different Addr
60cdf0e10cSrcweir 	formats into consideration (maybe a long dotted Addr
61cdf0e10cSrcweir 	or whatever).
62cdf0e10cSrcweir */
63cdf0e10cSrcweir 
64cdf0e10cSrcweir /*
65cdf0e10cSrcweir   _Note_ that I rely on the fact that oslSocketAddr and struct sockaddr
66cdf0e10cSrcweir   are the same! I don't like it very much but see no other easy way to
67cdf0e10cSrcweir   conceal the struct sockaddr from the eyes of the user.
68cdf0e10cSrcweir */
69cdf0e10cSrcweir 
70cdf0e10cSrcweir #define OSL_INVALID_SOCKET		INVALID_SOCKET			/* WIN32 */
71cdf0e10cSrcweir #define OSL_SOCKET_ERROR		SOCKET_ERROR			/* WIN32 */
72cdf0e10cSrcweir 
73cdf0e10cSrcweir /*****************************************************************************/
74cdf0e10cSrcweir /* enum oslAddrFamily */
75cdf0e10cSrcweir /*****************************************************************************/
76cdf0e10cSrcweir 
77cdf0e10cSrcweir /* map */
78cdf0e10cSrcweir static DWORD FamilyMap[]= {
79cdf0e10cSrcweir 	AF_INET,					/* osl_Socket_FamilyInet */
80cdf0e10cSrcweir 	AF_IPX,						/* osl_Socket_FamilyIpx */
81cdf0e10cSrcweir 	0							/* osl_Socket_FamilyInvalid */
82cdf0e10cSrcweir };
83cdf0e10cSrcweir 
84cdf0e10cSrcweir /* reverse map */
85cdf0e10cSrcweir static oslAddrFamily osl_AddrFamilyFromNative(DWORD nativeType)
86cdf0e10cSrcweir {
87cdf0e10cSrcweir 	oslAddrFamily i= (oslAddrFamily) 0;
88cdf0e10cSrcweir 	while(i != osl_Socket_FamilyInvalid)
89cdf0e10cSrcweir 	{
90cdf0e10cSrcweir 		if(FamilyMap[i] == nativeType)
91cdf0e10cSrcweir 			return i;
92cdf0e10cSrcweir 		i = (oslAddrFamily) ( (int)i + 1);
93cdf0e10cSrcweir 	}
94cdf0e10cSrcweir 	return i;
95cdf0e10cSrcweir }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir /* macros */
98cdf0e10cSrcweir #define FAMILY_FROM_NATIVE(y) osl_AddrFamilyFromNative(y)
99cdf0e10cSrcweir #define FAMILY_TO_NATIVE(x)	(short)FamilyMap[x]
100cdf0e10cSrcweir 
101cdf0e10cSrcweir /*****************************************************************************/
102cdf0e10cSrcweir /* enum oslProtocol */
103cdf0e10cSrcweir /*****************************************************************************/
104cdf0e10cSrcweir 
105cdf0e10cSrcweir /* map */
106cdf0e10cSrcweir static DWORD ProtocolMap[]= {
107cdf0e10cSrcweir 	0,							/* osl_Socket_FamilyInet */
108cdf0e10cSrcweir 	NSPROTO_IPX,				/* osl_Socket_FamilyIpx */
109cdf0e10cSrcweir 	NSPROTO_SPX,				/* osl_Socket_ProtocolSpx */
110cdf0e10cSrcweir 	NSPROTO_SPXII,				/* osl_Socket_ProtocolSpx_ii */
111cdf0e10cSrcweir 	0							/* osl_Socket_ProtocolInvalid */
112cdf0e10cSrcweir };
113cdf0e10cSrcweir 
114cdf0e10cSrcweir /* macros */
115cdf0e10cSrcweir #define PROTOCOL_FROM_NATIVE(y) osl_ProtocolFromNative(y)
116cdf0e10cSrcweir #define PROTOCOL_TO_NATIVE(x)	ProtocolMap[x]
117cdf0e10cSrcweir 
118cdf0e10cSrcweir /*****************************************************************************/
119cdf0e10cSrcweir /* enum oslSocketType */
120cdf0e10cSrcweir /*****************************************************************************/
121cdf0e10cSrcweir 
122cdf0e10cSrcweir /* map */
123cdf0e10cSrcweir static DWORD TypeMap[]= {
124cdf0e10cSrcweir 	SOCK_STREAM,				/* osl_Socket_TypeStream */
125cdf0e10cSrcweir 	SOCK_DGRAM,					/* osl_Socket_TypeDgram  */
126cdf0e10cSrcweir 	SOCK_RAW,					/* osl_Socket_TypeRaw */
127cdf0e10cSrcweir 	SOCK_RDM,					/* osl_Socket_TypeRdm */
128cdf0e10cSrcweir 	SOCK_SEQPACKET,				/* osl_Socket_TypeSeqPacket */
129cdf0e10cSrcweir 	0							/* osl_Socket_TypeInvalid */
130cdf0e10cSrcweir };
131cdf0e10cSrcweir 
132cdf0e10cSrcweir /* reverse map */
133cdf0e10cSrcweir static oslSocketType osl_SocketTypeFromNative(DWORD nativeType)
134cdf0e10cSrcweir {
135cdf0e10cSrcweir 	oslSocketType i= (oslSocketType)0;
136cdf0e10cSrcweir 	while(i != osl_Socket_TypeInvalid)
137cdf0e10cSrcweir 	{
138cdf0e10cSrcweir 		if(TypeMap[i] == nativeType)
139cdf0e10cSrcweir 			return i;
140cdf0e10cSrcweir 		i = (oslSocketType)((int)i+1);
141cdf0e10cSrcweir 	}
142cdf0e10cSrcweir 	return i;
143cdf0e10cSrcweir }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir /* macros */
146cdf0e10cSrcweir #define TYPE_TO_NATIVE(x)		TypeMap[x]
147cdf0e10cSrcweir #define TYPE_FROM_NATIVE(y)		osl_SocketTypeFromNative(y)
148cdf0e10cSrcweir 
149cdf0e10cSrcweir /*****************************************************************************/
150cdf0e10cSrcweir /* enum oslSocketOption */
151cdf0e10cSrcweir /*****************************************************************************/
152cdf0e10cSrcweir 
153cdf0e10cSrcweir /* map */
154cdf0e10cSrcweir static DWORD OptionMap[]= {
155cdf0e10cSrcweir 	SO_DEBUG,					/* osl_Socket_OptionDebug */
156cdf0e10cSrcweir 	SO_ACCEPTCONN,				/* osl_Socket_OptionAcceptConn */
157cdf0e10cSrcweir 	SO_REUSEADDR,				/* osl_Socket_OptionReuseAddr */
158cdf0e10cSrcweir 	SO_KEEPALIVE,				/* osl_Socket_OptionKeepAlive */
159cdf0e10cSrcweir 	SO_DONTROUTE,				/* osl_Socket_OptionDontRoute */
160cdf0e10cSrcweir 	SO_BROADCAST,				/* osl_Socket_OptionBroadcast */
161cdf0e10cSrcweir 	SO_USELOOPBACK,				/* osl_Socket_OptionUseLoopback */
162cdf0e10cSrcweir 	SO_LINGER,					/* osl_Socket_OptionLinger */
163cdf0e10cSrcweir 	SO_OOBINLINE,				/* osl_Socket_OptionOOBinLine */
164cdf0e10cSrcweir 	SO_SNDBUF,					/* osl_Socket_OptionSndBuf */
165cdf0e10cSrcweir 	SO_RCVBUF,					/* osl_Socket_OptionRcvBuf */
166cdf0e10cSrcweir 	SO_SNDLOWAT,				/* osl_Socket_OptionSndLowat */
167cdf0e10cSrcweir 	SO_RCVLOWAT,				/* osl_Socket_OptionRcvLowat */
168cdf0e10cSrcweir 	SO_SNDTIMEO,				/* osl_Socket_OptionSndTimeo */
169cdf0e10cSrcweir 	SO_RCVTIMEO,				/* osl_Socket_OptionRcvTimeo */
170cdf0e10cSrcweir 	SO_ERROR,					/* osl_Socket_OptionError */
171cdf0e10cSrcweir 	SO_TYPE,					/* osl_Socket_OptionType */
172cdf0e10cSrcweir 	TCP_NODELAY,				/* osl_Socket_OptionTcpNoDelay */
173cdf0e10cSrcweir 	0							/* osl_Socket_OptionInvalid */
174cdf0e10cSrcweir };
175cdf0e10cSrcweir 
176cdf0e10cSrcweir /* macros */
177cdf0e10cSrcweir #define OPTION_TO_NATIVE(x)		OptionMap[x]
178cdf0e10cSrcweir #define OPTION_FROM_NATIVE(y)	osl_SocketOptionFromNative(y)
179cdf0e10cSrcweir 
180cdf0e10cSrcweir /*****************************************************************************/
181cdf0e10cSrcweir /* enum oslSocketOptionLevel */
182cdf0e10cSrcweir /*****************************************************************************/
183cdf0e10cSrcweir 
184cdf0e10cSrcweir static DWORD OptionLevelMap[]= {
185cdf0e10cSrcweir 	SOL_SOCKET,					/* osl_Socket_LevelSocket */
186cdf0e10cSrcweir 	IPPROTO_TCP,				/* osl_Socket_LevelTcp */
187cdf0e10cSrcweir 	0							/* osl_invalid_SocketLevel */
188cdf0e10cSrcweir };
189cdf0e10cSrcweir 
190cdf0e10cSrcweir /* macros */
191cdf0e10cSrcweir #define OPTION_LEVEL_TO_NATIVE(x)		OptionLevelMap[x]
192cdf0e10cSrcweir #define OPTION_LEVEL_FROM_NATIVE(y)		osl_SocketOptionLevelFromNative(y)
193cdf0e10cSrcweir 
194cdf0e10cSrcweir /*****************************************************************************/
195cdf0e10cSrcweir /* enum oslSocketMsgFlag */
196cdf0e10cSrcweir /*****************************************************************************/
197cdf0e10cSrcweir 
198cdf0e10cSrcweir static DWORD SocketMsgFlagMap[]= {
199cdf0e10cSrcweir 	0,							/* osl_Socket_MsgNormal */
200cdf0e10cSrcweir 	MSG_OOB,					/* osl_Socket_MsgOOB */
201cdf0e10cSrcweir 	MSG_PEEK,					/* osl_Socket_MsgPeek */
202cdf0e10cSrcweir 	MSG_DONTROUTE,				/* osl_Socket_MsgDontRoute */
203cdf0e10cSrcweir 	MSG_MAXIOVLEN				/* osl_Socket_MsgMaxIOVLen */
204cdf0e10cSrcweir };
205cdf0e10cSrcweir 
206cdf0e10cSrcweir /* macros */
207cdf0e10cSrcweir #define MSG_FLAG_TO_NATIVE(x)		SocketMsgFlagMap[x]
208cdf0e10cSrcweir #define MSG_FLAG_FROM_NATIVE(y)		osl_SocketMsgFlagFromNative(y)
209cdf0e10cSrcweir 
210cdf0e10cSrcweir /*****************************************************************************/
211cdf0e10cSrcweir /* enum oslSocketDirection */
212cdf0e10cSrcweir /*****************************************************************************/
213cdf0e10cSrcweir 
214cdf0e10cSrcweir static DWORD SocketDirection[]= {
215cdf0e10cSrcweir 	SD_RECEIVE,					/* osl_Socket_DirRead */
216cdf0e10cSrcweir 	SD_SEND,					/* osl_Socket_DirWrite */
217cdf0e10cSrcweir 	SD_BOTH						/* osl_Socket_DirReadwrite */
218cdf0e10cSrcweir };
219cdf0e10cSrcweir 
220cdf0e10cSrcweir /* macros */
221cdf0e10cSrcweir #define DIRECTION_TO_NATIVE(x)		SocketDirection[x]
222cdf0e10cSrcweir #define DIRECTION_FROM_NATIVE(y)	osl_SocketDirectionFromNative(y)
223cdf0e10cSrcweir 
224cdf0e10cSrcweir /*****************************************************************************/
225cdf0e10cSrcweir /* enum oslSocketError */
226cdf0e10cSrcweir /*****************************************************************************/
227cdf0e10cSrcweir 
228cdf0e10cSrcweir static int SocketError[]= {
229cdf0e10cSrcweir 
230cdf0e10cSrcweir 	0,					/* no error */
231cdf0e10cSrcweir 	WSAENOTSOCK,			/* Socket operation on non-socket */
232cdf0e10cSrcweir 	WSAEDESTADDRREQ,		/* Destination address required */
233cdf0e10cSrcweir 	WSAEMSGSIZE,			/* Message too long */
234cdf0e10cSrcweir 	WSAEPROTOTYPE,			/* Protocol wrong type for socket */
235cdf0e10cSrcweir 	WSAENOPROTOOPT,		/* Protocol not available */
236cdf0e10cSrcweir 	WSAEPROTONOSUPPORT,	/* Protocol not supported */
237cdf0e10cSrcweir 	WSAESOCKTNOSUPPORT,	/* Socket type not supported */
238cdf0e10cSrcweir 	WSAEOPNOTSUPP,			/* Operation not supported on socket */
239cdf0e10cSrcweir 	WSAEPFNOSUPPORT,		/* Protocol family not supported */
240cdf0e10cSrcweir 	WSAEAFNOSUPPORT,		/* Address family not supported by */
241cdf0e10cSrcweir 							/* protocol family */
242cdf0e10cSrcweir 	WSAEADDRINUSE,			/* Address already in use */
243cdf0e10cSrcweir 	WSAEADDRNOTAVAIL,		/* Can't assign requested address */
244cdf0e10cSrcweir 	WSAENETDOWN,			/* Network is down */
245cdf0e10cSrcweir 	WSAENETUNREACH,		/* Network is unreachable */
246cdf0e10cSrcweir 	WSAENETRESET,			/* Network dropped connection because */
247cdf0e10cSrcweir 							/* of reset */
248cdf0e10cSrcweir 	WSAECONNABORTED,		/* Software caused connection abort */
249cdf0e10cSrcweir 	WSAECONNRESET,			/* Connection reset by peer */
250cdf0e10cSrcweir 	WSAENOBUFS,			/* No buffer space available */
251cdf0e10cSrcweir 	WSAEISCONN,			/* Socket is already connected */
252cdf0e10cSrcweir 	WSAENOTCONN,			/* Socket is not connected */
253cdf0e10cSrcweir 	WSAESHUTDOWN,			/* Can't send after socket shutdown */
254cdf0e10cSrcweir 	WSAETOOMANYREFS,		/* Too many references: can't splice */
255cdf0e10cSrcweir 	WSAETIMEDOUT,			/* Connection timed out */
256cdf0e10cSrcweir 	WSAECONNREFUSED,		/* Connection refused */
257cdf0e10cSrcweir 	WSAEHOSTDOWN,			/* Host is down */
258cdf0e10cSrcweir 	WSAEHOSTUNREACH,		/* No route to host */
259cdf0e10cSrcweir 	WSAEWOULDBLOCK,		/* call would block on non-blocking socket */
260cdf0e10cSrcweir 	WSAEALREADY,			/* operation already in progress */
261cdf0e10cSrcweir 	WSAEINPROGRESS		/* operation now in progress */
262cdf0e10cSrcweir };
263cdf0e10cSrcweir 
264cdf0e10cSrcweir /* reverse map */
265cdf0e10cSrcweir static oslSocketError osl_SocketErrorFromNative(int nativeType)
266cdf0e10cSrcweir {
267cdf0e10cSrcweir 	oslSocketError i= (oslSocketError)0;
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 	while(i != osl_Socket_E_InvalidError)
270cdf0e10cSrcweir 	{
271cdf0e10cSrcweir 		if(SocketError[i] == nativeType)
272cdf0e10cSrcweir 			return i;
273cdf0e10cSrcweir 
274cdf0e10cSrcweir 		i = (oslSocketError)( (int) i + 1);
275cdf0e10cSrcweir 	}
276cdf0e10cSrcweir 	return i;
277cdf0e10cSrcweir }
278cdf0e10cSrcweir 
279cdf0e10cSrcweir /* macros */
280cdf0e10cSrcweir #define ERROR_TO_NATIVE(x)		SocketError[x]
281cdf0e10cSrcweir #define ERROR_FROM_NATIVE(y)	osl_SocketErrorFromNative(y)
282cdf0e10cSrcweir 
283cdf0e10cSrcweir /*****************************************************************************/
284cdf0e10cSrcweir /* oslSocketDialupImpl */
285cdf0e10cSrcweir /*****************************************************************************/
286cdf0e10cSrcweir static oslSocketDialupImpl *pDialupImpl = NULL;
287cdf0e10cSrcweir 
288cdf0e10cSrcweir #if 0  /* INTERNAL DEBUG ONLY */
289cdf0e10cSrcweir BOOL WINAPI __osl_autodial_Impl (DWORD dwFlags, DWORD dwReserved)
290cdf0e10cSrcweir {
291cdf0e10cSrcweir 	return 0;
292cdf0e10cSrcweir }
293cdf0e10cSrcweir 
294cdf0e10cSrcweir BOOL WINAPI __osl_autodialHangup_Impl (DWORD dwReserved)
295cdf0e10cSrcweir {
296cdf0e10cSrcweir 	return 1;
297cdf0e10cSrcweir }
298cdf0e10cSrcweir 
299cdf0e10cSrcweir BOOL WINAPI __osl_getConnectedState_Impl (LPDWORD lpdwFlags, DWORD dwReserved)
300cdf0e10cSrcweir {
301cdf0e10cSrcweir 	if (lpdwFlags)
302cdf0e10cSrcweir 		*lpdwFlags = 0;
303cdf0e10cSrcweir 	return 0;
304cdf0e10cSrcweir }
305cdf0e10cSrcweir #endif /* INTERNAL DEBUG ONLY */
306cdf0e10cSrcweir 
307cdf0e10cSrcweir /*
308cdf0e10cSrcweir  * __osl_createSocketDialupImpl.
309cdf0e10cSrcweir  */
310cdf0e10cSrcweir static oslSocketDialupImpl* __osl_createSocketDialupImpl (void)
311cdf0e10cSrcweir {
312cdf0e10cSrcweir 	oslSocketDialupImpl *pImpl;
313cdf0e10cSrcweir 	pImpl = (oslSocketDialupImpl*)rtl_allocateZeroMemory( sizeof (oslSocketDialupImpl));
314cdf0e10cSrcweir 
315cdf0e10cSrcweir 	InitializeCriticalSection (&pImpl->m_hMutex);
316cdf0e10cSrcweir 
317cdf0e10cSrcweir 	return (pImpl);
318cdf0e10cSrcweir }
319cdf0e10cSrcweir 
320cdf0e10cSrcweir /*
321cdf0e10cSrcweir  * __osl_initSocketDialupImpl.
322cdf0e10cSrcweir  */
323cdf0e10cSrcweir static void __osl_initSocketDialupImpl (oslSocketDialupImpl *pImpl)
324cdf0e10cSrcweir {
325cdf0e10cSrcweir #ifdef SOCKET_USE_AUTODIAL
326cdf0e10cSrcweir 	if (pImpl)
327cdf0e10cSrcweir 	{
328cdf0e10cSrcweir 		HINSTANCE hModule;
329cdf0e10cSrcweir 
330cdf0e10cSrcweir 		EnterCriticalSection (&pImpl->m_hMutex);
331cdf0e10cSrcweir 
332cdf0e10cSrcweir 		hModule = LoadLibrary (INTERNET_MODULE_NAME);
333cdf0e10cSrcweir 		if (!(hModule <= (HINSTANCE)HINSTANCE_ERROR))
334cdf0e10cSrcweir 		{
335cdf0e10cSrcweir 			pImpl->m_pfnAttemptConnect = (INTERNETATTEMPTCONNECT)
336cdf0e10cSrcweir 				(GetProcAddress (hModule, "InternetAttemptConnect"));
337cdf0e10cSrcweir 			pImpl->m_pfnAutodial = (INTERNETAUTODIAL)
338cdf0e10cSrcweir 				(GetProcAddress (hModule, "InternetAutodial"));
339cdf0e10cSrcweir 			pImpl->m_pfnAutodialHangup = (INTERNETAUTODIALHANGUP)
340cdf0e10cSrcweir 				(GetProcAddress (hModule, "InternetAutodialHangup"));
341cdf0e10cSrcweir 			pImpl->m_pfnGetConnectedState = (INTERNETGETCONNECTEDSTATE)
342cdf0e10cSrcweir 				(GetProcAddress (hModule, "InternetGetConnectedState"));
343cdf0e10cSrcweir 			pImpl->m_hModule = hModule;
344cdf0e10cSrcweir 		}
345cdf0e10cSrcweir 
346cdf0e10cSrcweir 		LeaveCriticalSection (&pImpl->m_hMutex);
347cdf0e10cSrcweir 	}
348cdf0e10cSrcweir #else
349cdf0e10cSrcweir     pImpl = pImpl; /* avoid warnings */
350cdf0e10cSrcweir #endif
351cdf0e10cSrcweir }
352cdf0e10cSrcweir 
353cdf0e10cSrcweir /*
354cdf0e10cSrcweir  * __osl_destroySocketDialupImpl.
355cdf0e10cSrcweir  */
356cdf0e10cSrcweir static void __osl_destroySocketDialupImpl (oslSocketDialupImpl *pImpl)
357cdf0e10cSrcweir {
358cdf0e10cSrcweir 	if (pImpl)
359cdf0e10cSrcweir 	{
360cdf0e10cSrcweir 		EnterCriticalSection (&pImpl->m_hMutex);
361cdf0e10cSrcweir 
362cdf0e10cSrcweir 		if (pImpl->m_dwFlags & INTERNET_CONNECTION_HANGUP)
363cdf0e10cSrcweir 		{
364cdf0e10cSrcweir 			if (pImpl->m_pfnAutodialHangup)
365cdf0e10cSrcweir 			{
366cdf0e10cSrcweir 				(pImpl->m_pfnAutodialHangup)(0);
367cdf0e10cSrcweir 				pImpl->m_dwFlags &= ~INTERNET_CONNECTION_HANGUP;
368cdf0e10cSrcweir 			}
369cdf0e10cSrcweir 		}
370cdf0e10cSrcweir 
371cdf0e10cSrcweir 		if (pImpl->m_hModule)
372cdf0e10cSrcweir 			FreeLibrary (pImpl->m_hModule);
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 		LeaveCriticalSection (&pImpl->m_hMutex);
375cdf0e10cSrcweir 		DeleteCriticalSection (&pImpl->m_hMutex);
376cdf0e10cSrcweir 
377cdf0e10cSrcweir 		rtl_freeMemory (pImpl);
378cdf0e10cSrcweir 	}
379cdf0e10cSrcweir }
380cdf0e10cSrcweir 
381cdf0e10cSrcweir /*
382cdf0e10cSrcweir  * __osl_querySocketDialupImpl.
383cdf0e10cSrcweir  */
384cdf0e10cSrcweir static sal_Bool __osl_querySocketDialupImpl (void)
385cdf0e10cSrcweir {
386cdf0e10cSrcweir 	sal_Bool result;
387cdf0e10cSrcweir 
388cdf0e10cSrcweir 	if (pDialupImpl == NULL)
389cdf0e10cSrcweir 	{
390cdf0e10cSrcweir 		pDialupImpl = __osl_createSocketDialupImpl();
391cdf0e10cSrcweir 		__osl_initSocketDialupImpl (pDialupImpl);
392cdf0e10cSrcweir 	}
393cdf0e10cSrcweir 
394cdf0e10cSrcweir 	EnterCriticalSection (&pDialupImpl->m_hMutex);
395cdf0e10cSrcweir 
396cdf0e10cSrcweir 	result = sal_True;
397cdf0e10cSrcweir 	if (pDialupImpl->m_pfnGetConnectedState)
398cdf0e10cSrcweir 	{
399cdf0e10cSrcweir 		DWORD dwFlags = 0;
400cdf0e10cSrcweir 
401cdf0e10cSrcweir 		result = (sal_Bool)(pDialupImpl->m_pfnGetConnectedState)(&dwFlags, 0);
402cdf0e10cSrcweir 		pDialupImpl->m_dwFlags |= dwFlags;
403cdf0e10cSrcweir 	}
404cdf0e10cSrcweir 
405cdf0e10cSrcweir 	LeaveCriticalSection (&pDialupImpl->m_hMutex);
406cdf0e10cSrcweir 	return (result);
407cdf0e10cSrcweir }
408cdf0e10cSrcweir 
409cdf0e10cSrcweir /*
410cdf0e10cSrcweir  * __osl_attemptSocketDialupImpl.
411cdf0e10cSrcweir  */
412cdf0e10cSrcweir static sal_Bool __osl_attemptSocketDialupImpl (void)
413cdf0e10cSrcweir {
414cdf0e10cSrcweir 	sal_Bool result;
415cdf0e10cSrcweir 
416cdf0e10cSrcweir 	if (pDialupImpl == NULL)
417cdf0e10cSrcweir 	{
418cdf0e10cSrcweir 		pDialupImpl = __osl_createSocketDialupImpl();
419cdf0e10cSrcweir 		__osl_initSocketDialupImpl (pDialupImpl);
420cdf0e10cSrcweir 	}
421cdf0e10cSrcweir 
422cdf0e10cSrcweir 	EnterCriticalSection (&pDialupImpl->m_hMutex);
423cdf0e10cSrcweir 
424cdf0e10cSrcweir 	result = __osl_querySocketDialupImpl();
425cdf0e10cSrcweir 	if (!result)
426cdf0e10cSrcweir 	{
427cdf0e10cSrcweir 		result = sal_True;
428cdf0e10cSrcweir 		if (pDialupImpl->m_pfnAutodial)
429cdf0e10cSrcweir 		{
430cdf0e10cSrcweir 			result = (sal_Bool)(pDialupImpl->m_pfnAutodial)(0, 0);
431cdf0e10cSrcweir 			if (result)
432cdf0e10cSrcweir 				pDialupImpl->m_dwFlags |= INTERNET_CONNECTION_HANGUP;
433cdf0e10cSrcweir 			else
434cdf0e10cSrcweir 				WSASetLastError (WSAENETDOWN);
435cdf0e10cSrcweir 		}
436cdf0e10cSrcweir 	}
437cdf0e10cSrcweir 
438cdf0e10cSrcweir 	LeaveCriticalSection (&pDialupImpl->m_hMutex);
439cdf0e10cSrcweir 	return (result);
440cdf0e10cSrcweir }
441cdf0e10cSrcweir 
442cdf0e10cSrcweir /*****************************************************************************/
443cdf0e10cSrcweir /* oslSocketImpl */
444cdf0e10cSrcweir /*****************************************************************************/
445cdf0e10cSrcweir static sal_uInt32 g_nSocketImpl = 0;
446cdf0e10cSrcweir 
447cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
448cdf0e10cSrcweir static sal_uInt32 g_nSocketAddr = 0;
449cdf0e10cSrcweir struct LeakWarning
450cdf0e10cSrcweir {
451cdf0e10cSrcweir 	~LeakWarning()
452cdf0e10cSrcweir 	{
453cdf0e10cSrcweir 		if( g_nSocketImpl )
454cdf0e10cSrcweir 			OSL_TRACE( "sal_socket: %d socket instances leak\n" , g_nSocketImpl );
455cdf0e10cSrcweir 		if( g_nSocketAddr )
456cdf0e10cSrcweir 			OSL_TRACE( "sal_socket: %d socket address instances leak\n" , g_nSocketAddr );
457cdf0e10cSrcweir 	}
458cdf0e10cSrcweir };
459cdf0e10cSrcweir LeakWarning socketWarning;
460cdf0e10cSrcweir #endif
461cdf0e10cSrcweir 
462cdf0e10cSrcweir /*
463cdf0e10cSrcweir  * __osl_createSocketImpl.
464cdf0e10cSrcweir  */
465cdf0e10cSrcweir oslSocket __osl_createSocketImpl(SOCKET Socket)
466cdf0e10cSrcweir {
467cdf0e10cSrcweir 	oslSocket pSockImpl = (oslSocket) rtl_allocateZeroMemory( sizeof(struct oslSocketImpl));
468cdf0e10cSrcweir 	pSockImpl->m_Socket = Socket;
469cdf0e10cSrcweir 	pSockImpl->m_nRefCount = 1;
470cdf0e10cSrcweir 
471cdf0e10cSrcweir 	g_nSocketImpl++;
472cdf0e10cSrcweir 
473cdf0e10cSrcweir 	return (pSockImpl);
474cdf0e10cSrcweir }
475cdf0e10cSrcweir 
476cdf0e10cSrcweir /*
477cdf0e10cSrcweir  * __osl_destroySocketImpl.
478cdf0e10cSrcweir  */
479cdf0e10cSrcweir void __osl_destroySocketImpl(oslSocketImpl *pImpl)
480cdf0e10cSrcweir {
481cdf0e10cSrcweir 	if (pImpl)
482cdf0e10cSrcweir 	{
483cdf0e10cSrcweir 		if (--g_nSocketImpl == 0)
484cdf0e10cSrcweir 		{
485cdf0e10cSrcweir 			__osl_destroySocketDialupImpl (pDialupImpl);
486cdf0e10cSrcweir 			pDialupImpl = NULL;
487cdf0e10cSrcweir 		}
488cdf0e10cSrcweir 		rtl_freeMemory (pImpl);
489cdf0e10cSrcweir 	}
490cdf0e10cSrcweir }
491cdf0e10cSrcweir /*****************************************************************************/
492cdf0e10cSrcweir static oslSocketAddr __osl_createSocketAddr(  )
493cdf0e10cSrcweir {
494cdf0e10cSrcweir 	oslSocketAddr pAddr = (oslSocketAddr) rtl_allocateZeroMemory( sizeof( struct oslSocketAddrImpl ));
495cdf0e10cSrcweir 	pAddr->m_nRefCount = 1;
496cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
497cdf0e10cSrcweir 	g_nSocketAddr ++;
498cdf0e10cSrcweir #endif
499cdf0e10cSrcweir 	return pAddr;
500cdf0e10cSrcweir }
501cdf0e10cSrcweir 
502cdf0e10cSrcweir static oslSocketAddr __osl_createSocketAddrWithFamily(
503cdf0e10cSrcweir 	oslAddrFamily family, sal_Int32 port, sal_uInt32 nAddr )
504cdf0e10cSrcweir {
505cdf0e10cSrcweir 	OSL_ASSERT( family == osl_Socket_FamilyInet );
506cdf0e10cSrcweir 
507cdf0e10cSrcweir 	oslSocketAddr pAddr = __osl_createSocketAddr();
508cdf0e10cSrcweir 	switch( family )
509cdf0e10cSrcweir 	{
510cdf0e10cSrcweir 	case osl_Socket_FamilyInet:
511cdf0e10cSrcweir 	{
512cdf0e10cSrcweir 		struct sockaddr_in* pInetAddr= (struct sockaddr_in*)&(pAddr->m_sockaddr);
513cdf0e10cSrcweir 
514cdf0e10cSrcweir 		pInetAddr->sin_family = FAMILY_TO_NATIVE(osl_Socket_FamilyInet);
515cdf0e10cSrcweir 		pInetAddr->sin_addr.s_addr = nAddr;
516cdf0e10cSrcweir 		pInetAddr->sin_port = (sal_uInt16)(port&0xffff);
517cdf0e10cSrcweir 		break;
518cdf0e10cSrcweir    	}
519cdf0e10cSrcweir 	default:
520cdf0e10cSrcweir 		pAddr->m_sockaddr.sa_family = FAMILY_TO_NATIVE(family);
521cdf0e10cSrcweir 	}
522cdf0e10cSrcweir 	return pAddr;
523cdf0e10cSrcweir }
524cdf0e10cSrcweir 
525cdf0e10cSrcweir static oslSocketAddr __osl_createSocketAddrFromSystem( struct sockaddr *pSystemSockAddr )
526cdf0e10cSrcweir {
527cdf0e10cSrcweir 	oslSocketAddr pAddr = __osl_createSocketAddr();
528cdf0e10cSrcweir 	memcpy( &(pAddr->m_sockaddr), pSystemSockAddr, sizeof( sockaddr ) );
529cdf0e10cSrcweir 	return pAddr;
530cdf0e10cSrcweir }
531cdf0e10cSrcweir 
532cdf0e10cSrcweir static void __osl_destroySocketAddr( oslSocketAddr addr )
533cdf0e10cSrcweir {
534cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
535cdf0e10cSrcweir 	g_nSocketAddr --;
536cdf0e10cSrcweir #endif
537cdf0e10cSrcweir 	rtl_freeMemory( addr );
538cdf0e10cSrcweir }
539cdf0e10cSrcweir /*****************************************************************************/
540cdf0e10cSrcweir /* osl_createEmptySocketAddr */
541cdf0e10cSrcweir /*****************************************************************************/
542cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_createEmptySocketAddr(oslAddrFamily Family)
543cdf0e10cSrcweir {
544cdf0e10cSrcweir 	oslSocketAddr pAddr = 0;
545cdf0e10cSrcweir 
546cdf0e10cSrcweir 	/* is it an internet-Addr? */
547cdf0e10cSrcweir 	if (Family == osl_Socket_FamilyInet)
548cdf0e10cSrcweir 	{
549cdf0e10cSrcweir 		pAddr = __osl_createSocketAddrWithFamily(Family, 0 , htonl(INADDR_ANY) );
550cdf0e10cSrcweir 	}
551cdf0e10cSrcweir 	else
552cdf0e10cSrcweir 	{
553cdf0e10cSrcweir 		pAddr = __osl_createSocketAddrWithFamily( Family , 0 , 0 );
554cdf0e10cSrcweir 	}
555cdf0e10cSrcweir 
556cdf0e10cSrcweir 	return pAddr;
557cdf0e10cSrcweir }
558cdf0e10cSrcweir 
559cdf0e10cSrcweir /*****************************************************************************/
560cdf0e10cSrcweir /* osl_copySocketAddr */
561cdf0e10cSrcweir /*****************************************************************************/
562cdf0e10cSrcweir // @deprecated, to be removed
563cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_copySocketAddr(oslSocketAddr Addr)
564cdf0e10cSrcweir {
565cdf0e10cSrcweir 	oslSocketAddr pCopy = 0;
566cdf0e10cSrcweir 	if (Addr)
567cdf0e10cSrcweir 	{
568cdf0e10cSrcweir 		pCopy = __osl_createSocketAddr();
569cdf0e10cSrcweir 
570cdf0e10cSrcweir 		if (pCopy)
571cdf0e10cSrcweir 			memcpy(&(pCopy->m_sockaddr),&(Addr->m_sockaddr), sizeof(struct sockaddr));
572cdf0e10cSrcweir 	}
573cdf0e10cSrcweir 	return pCopy;
574cdf0e10cSrcweir }
575cdf0e10cSrcweir 
576cdf0e10cSrcweir /*****************************************************************************/
577cdf0e10cSrcweir /* osl_isEqualSocketAddr */
578cdf0e10cSrcweir /*****************************************************************************/
579cdf0e10cSrcweir sal_Bool SAL_CALL osl_isEqualSocketAddr(oslSocketAddr Addr1, oslSocketAddr Addr2)
580cdf0e10cSrcweir {
581cdf0e10cSrcweir 	struct sockaddr* pAddr1= &(Addr1->m_sockaddr);
582cdf0e10cSrcweir 	struct sockaddr* pAddr2= &(Addr2->m_sockaddr);
583cdf0e10cSrcweir 
584cdf0e10cSrcweir 	OSL_ASSERT(pAddr1);
585cdf0e10cSrcweir 	OSL_ASSERT(pAddr2);
586cdf0e10cSrcweir 
587cdf0e10cSrcweir 	if (pAddr1->sa_family == pAddr2->sa_family)
588cdf0e10cSrcweir 	{
589cdf0e10cSrcweir 		switch (pAddr1->sa_family)
590cdf0e10cSrcweir 		{
591cdf0e10cSrcweir 			case AF_INET:
592cdf0e10cSrcweir 			{
593cdf0e10cSrcweir 				struct sockaddr_in* pInetAddr1= (struct sockaddr_in*)pAddr1;
594cdf0e10cSrcweir 				struct sockaddr_in* pInetAddr2= (struct sockaddr_in*)pAddr2;
595cdf0e10cSrcweir 
596cdf0e10cSrcweir 				if ((pInetAddr1->sin_family == pInetAddr2->sin_family) &&
597cdf0e10cSrcweir 					(pInetAddr1->sin_addr.s_addr == pInetAddr2->sin_addr.s_addr) &&
598cdf0e10cSrcweir 					(pInetAddr1->sin_port == pInetAddr2->sin_port))
599cdf0e10cSrcweir 					return (sal_True);
600cdf0e10cSrcweir 			}
601cdf0e10cSrcweir 
602cdf0e10cSrcweir 			default:
603cdf0e10cSrcweir 			{
604cdf0e10cSrcweir 				return (memcmp(pAddr1, Addr2, sizeof(struct sockaddr)) == 0);
605cdf0e10cSrcweir 			}
606cdf0e10cSrcweir 		}
607cdf0e10cSrcweir 	}
608cdf0e10cSrcweir 
609cdf0e10cSrcweir 	return (sal_False);
610cdf0e10cSrcweir }
611cdf0e10cSrcweir 
612cdf0e10cSrcweir /*****************************************************************************/
613cdf0e10cSrcweir /* osl_createInetBroadcastAddr */
614cdf0e10cSrcweir /*****************************************************************************/
615cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_createInetBroadcastAddr (
616cdf0e10cSrcweir 	rtl_uString *strDottedAddr,
617cdf0e10cSrcweir 	sal_Int32    Port)
618cdf0e10cSrcweir {
619cdf0e10cSrcweir 	sal_uInt32          nAddr = OSL_INADDR_NONE;
620cdf0e10cSrcweir 
621cdf0e10cSrcweir 	if (strDottedAddr && strDottedAddr->length)
622cdf0e10cSrcweir 	{
623cdf0e10cSrcweir 		/* Dotted host address for limited broadcast */
624cdf0e10cSrcweir 		rtl_String *pDottedAddr = NULL;
625cdf0e10cSrcweir 
626cdf0e10cSrcweir 		rtl_uString2String (
627cdf0e10cSrcweir 			&pDottedAddr, strDottedAddr->buffer, strDottedAddr->length,
628cdf0e10cSrcweir 			RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
629cdf0e10cSrcweir 
630cdf0e10cSrcweir 		nAddr = inet_addr (pDottedAddr->buffer);
631cdf0e10cSrcweir 		rtl_string_release (pDottedAddr);
632cdf0e10cSrcweir 	}
633cdf0e10cSrcweir 
634cdf0e10cSrcweir 	if (nAddr != OSL_INADDR_NONE)
635cdf0e10cSrcweir 	{
636cdf0e10cSrcweir 		/* Limited broadcast */
637cdf0e10cSrcweir 		nAddr = ntohl(nAddr);
638cdf0e10cSrcweir 		if (IN_CLASSA(nAddr))
639cdf0e10cSrcweir 		{
640cdf0e10cSrcweir 			nAddr &= IN_CLASSA_NET;
641cdf0e10cSrcweir 			nAddr |= IN_CLASSA_HOST;
642cdf0e10cSrcweir 		}
643cdf0e10cSrcweir 		else if (IN_CLASSB(nAddr))
644cdf0e10cSrcweir 		{
645cdf0e10cSrcweir 			nAddr &= IN_CLASSB_NET;
646cdf0e10cSrcweir 			nAddr |= IN_CLASSB_HOST;
647cdf0e10cSrcweir 		}
648cdf0e10cSrcweir 		else if (IN_CLASSC(nAddr))
649cdf0e10cSrcweir 		{
650cdf0e10cSrcweir 			nAddr &= IN_CLASSC_NET;
651cdf0e10cSrcweir 			nAddr |= IN_CLASSC_HOST;
652cdf0e10cSrcweir 		}
653cdf0e10cSrcweir 		else
654cdf0e10cSrcweir 		{
655cdf0e10cSrcweir 			/* No broadcast in class D */
656cdf0e10cSrcweir 			return ((oslSocketAddr)NULL);
657cdf0e10cSrcweir 		}
658cdf0e10cSrcweir 		nAddr = htonl(nAddr);
659cdf0e10cSrcweir 	}
660cdf0e10cSrcweir 
661cdf0e10cSrcweir 	oslSocketAddr pAddr =
662cdf0e10cSrcweir 		__osl_createSocketAddrWithFamily( osl_Socket_FamilyInet, htons( (sal_uInt16) Port), nAddr );
663cdf0e10cSrcweir 	return pAddr;
664cdf0e10cSrcweir }
665cdf0e10cSrcweir 
666cdf0e10cSrcweir /*****************************************************************************/
667cdf0e10cSrcweir /* osl_createInetSocketAddr */
668cdf0e10cSrcweir /*****************************************************************************/
669cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_createInetSocketAddr (
670cdf0e10cSrcweir 	rtl_uString *strDottedAddr,
671cdf0e10cSrcweir 	sal_Int32    Port)
672cdf0e10cSrcweir {
673cdf0e10cSrcweir 	DWORD Addr;
674cdf0e10cSrcweir 	rtl_String	*pDottedAddr=NULL;
675cdf0e10cSrcweir 
676cdf0e10cSrcweir 	rtl_uString2String(
677cdf0e10cSrcweir 		&pDottedAddr, strDottedAddr->buffer, strDottedAddr->length,
678cdf0e10cSrcweir 		RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
679cdf0e10cSrcweir 
680cdf0e10cSrcweir 	Addr= inet_addr (pDottedAddr->buffer);
681cdf0e10cSrcweir 	rtl_string_release (pDottedAddr);
682cdf0e10cSrcweir 
683cdf0e10cSrcweir 	oslSocketAddr pAddr = 0;
684cdf0e10cSrcweir 	if(Addr != -1)
685cdf0e10cSrcweir 	{
686cdf0e10cSrcweir 		pAddr = __osl_createSocketAddrWithFamily( osl_Socket_FamilyInet, htons( (sal_uInt16)Port), Addr );
687cdf0e10cSrcweir 	}
688cdf0e10cSrcweir 	return pAddr;
689cdf0e10cSrcweir }
690cdf0e10cSrcweir 
691cdf0e10cSrcweir oslSocketResult SAL_CALL osl_setAddrOfSocketAddr( oslSocketAddr pAddr, sal_Sequence *pByteSeq )
692cdf0e10cSrcweir {
693cdf0e10cSrcweir 	OSL_ASSERT( pAddr );
694cdf0e10cSrcweir 	OSL_ASSERT( pByteSeq );
695cdf0e10cSrcweir 
696cdf0e10cSrcweir 	oslSocketResult res = osl_Socket_Error;
697cdf0e10cSrcweir 	if( pAddr && pByteSeq )
698cdf0e10cSrcweir 	{
699cdf0e10cSrcweir 		OSL_ASSERT( pAddr->m_sockaddr.sa_family == FAMILY_TO_NATIVE( osl_Socket_FamilyInet ) );
700cdf0e10cSrcweir 		OSL_ASSERT( pByteSeq->nElements == 4 );
701cdf0e10cSrcweir 		struct sockaddr_in * pSystemInetAddr = (struct sockaddr_in * ) &(pAddr->m_sockaddr);
702cdf0e10cSrcweir 		memcpy( &(pSystemInetAddr->sin_addr) , pByteSeq->elements , 4 );
703cdf0e10cSrcweir 		res = osl_Socket_Ok;
704cdf0e10cSrcweir 	}
705cdf0e10cSrcweir 	return res;
706cdf0e10cSrcweir }
707cdf0e10cSrcweir 
708cdf0e10cSrcweir /** Returns the addr field in the struct sockaddr. ppByteSeq is in network byteorder. *ppByteSeq may
709cdf0e10cSrcweir 	either be 0 or contain a constructed sal_Sequence.
710cdf0e10cSrcweir  */
711cdf0e10cSrcweir oslSocketResult SAL_CALL osl_getAddrOfSocketAddr( oslSocketAddr pAddr, sal_Sequence **ppByteSeq )
712cdf0e10cSrcweir {
713cdf0e10cSrcweir 	OSL_ASSERT( pAddr );
714cdf0e10cSrcweir 	OSL_ASSERT( ppByteSeq );
715cdf0e10cSrcweir 
716cdf0e10cSrcweir 	oslSocketResult res = osl_Socket_Error;
717cdf0e10cSrcweir 	if( pAddr && ppByteSeq )
718cdf0e10cSrcweir 	{
719cdf0e10cSrcweir 		struct sockaddr_in * pSystemInetAddr = (struct sockaddr_in * ) &(pAddr->m_sockaddr);
720cdf0e10cSrcweir 		rtl_byte_sequence_constructFromArray( ppByteSeq , (sal_Int8 *) &(pSystemInetAddr->sin_addr),4);
721cdf0e10cSrcweir 		res = osl_Socket_Ok;
722cdf0e10cSrcweir 	}
723cdf0e10cSrcweir 	return res;
724cdf0e10cSrcweir }
725cdf0e10cSrcweir 
726cdf0e10cSrcweir /*****************************************************************************/
727cdf0e10cSrcweir /* oslHostAddr */
728cdf0e10cSrcweir /*****************************************************************************/
729cdf0e10cSrcweir struct oslHostAddrImpl {
730cdf0e10cSrcweir 	rtl_uString     *pHostName;
731cdf0e10cSrcweir 	oslSocketAddr   pSockAddr;
732cdf0e10cSrcweir } ;
733cdf0e10cSrcweir 
734cdf0e10cSrcweir static oslHostAddr __osl_hostentToHostAddr (const struct hostent *he)
735cdf0e10cSrcweir {
736cdf0e10cSrcweir 	oslHostAddr pAddr= NULL;
737cdf0e10cSrcweir 	oslSocketAddr pSocketAddr = 0;
738cdf0e10cSrcweir 
739cdf0e10cSrcweir 	rtl_uString     *cn= NULL;
740cdf0e10cSrcweir 
741cdf0e10cSrcweir 	if ((he == NULL) || (he->h_name == NULL) || (he->h_addr_list[0] == NULL))
742cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
743cdf0e10cSrcweir 
744cdf0e10cSrcweir 	rtl_string2UString(
745cdf0e10cSrcweir 		&cn, he->h_name, strlen(he->h_name),
746cdf0e10cSrcweir 		RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
747cdf0e10cSrcweir     OSL_ASSERT(cn != 0);
748cdf0e10cSrcweir 
749cdf0e10cSrcweir 	pSocketAddr = __osl_createSocketAddr();
750cdf0e10cSrcweir 
751cdf0e10cSrcweir 	if (pSocketAddr == NULL)
752cdf0e10cSrcweir 	{
753cdf0e10cSrcweir 		rtl_uString_release(cn);
754cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
755cdf0e10cSrcweir 	}
756cdf0e10cSrcweir 
757cdf0e10cSrcweir 	pSocketAddr->m_sockaddr.sa_family = he->h_addrtype;
758cdf0e10cSrcweir 	if (pSocketAddr->m_sockaddr.sa_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet))
759cdf0e10cSrcweir 	{
760cdf0e10cSrcweir 		struct sockaddr_in *sin= (struct sockaddr_in *)&(pSocketAddr->m_sockaddr);
761cdf0e10cSrcweir 		memcpy (
762cdf0e10cSrcweir 			&(sin->sin_addr.s_addr),
763cdf0e10cSrcweir 			he->h_addr_list[0],
764cdf0e10cSrcweir 			he->h_length);
765cdf0e10cSrcweir 	}
766cdf0e10cSrcweir 	else
767cdf0e10cSrcweir 	{
768cdf0e10cSrcweir 		/* unknown address family */
769cdf0e10cSrcweir 		/* future extensions for new families might be implemented here */
770cdf0e10cSrcweir 
771cdf0e10cSrcweir         OSL_TRACE("_osl_hostentToHostAddr(): unknown address family.\n");
772cdf0e10cSrcweir 		OSL_ASSERT(sal_False);
773cdf0e10cSrcweir 
774cdf0e10cSrcweir 		__osl_destroySocketAddr( pSocketAddr );
775cdf0e10cSrcweir 		rtl_uString_release(cn);
776cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
777cdf0e10cSrcweir 	}
778cdf0e10cSrcweir 
779cdf0e10cSrcweir 	pAddr= (oslHostAddr )rtl_allocateMemory (sizeof (struct oslHostAddrImpl));
780cdf0e10cSrcweir 
781cdf0e10cSrcweir 	if (pAddr == NULL)
782cdf0e10cSrcweir 	{
783cdf0e10cSrcweir 		__osl_destroySocketAddr( pSocketAddr );
784cdf0e10cSrcweir 		rtl_uString_release(cn);
785cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
786cdf0e10cSrcweir 	}
787cdf0e10cSrcweir 
788cdf0e10cSrcweir 	pAddr->pHostName= cn;
789cdf0e10cSrcweir 	pAddr->pSockAddr= pSocketAddr;
790cdf0e10cSrcweir 
791cdf0e10cSrcweir 	return pAddr;
792cdf0e10cSrcweir }
793cdf0e10cSrcweir 
794cdf0e10cSrcweir /*****************************************************************************/
795cdf0e10cSrcweir /* osl_createHostAddr */
796cdf0e10cSrcweir /*****************************************************************************/
797cdf0e10cSrcweir oslHostAddr SAL_CALL osl_createHostAddr (
798cdf0e10cSrcweir 	rtl_uString         *strHostname,
799cdf0e10cSrcweir 	const oslSocketAddr  pSocketAddr)
800cdf0e10cSrcweir {
801cdf0e10cSrcweir 	oslHostAddr pAddr;
802cdf0e10cSrcweir 	rtl_uString     *cn= NULL;
803cdf0e10cSrcweir 
804cdf0e10cSrcweir 	if ((strHostname == NULL)  || (strHostname->length == 0) || (pSocketAddr == NULL))
805cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
806cdf0e10cSrcweir 
807cdf0e10cSrcweir 	rtl_uString_newFromString( &cn, strHostname);
808cdf0e10cSrcweir 
809cdf0e10cSrcweir 	if ( ! pSocketAddr )
810cdf0e10cSrcweir 	{
811cdf0e10cSrcweir 		rtl_uString_release(cn);
812cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
813cdf0e10cSrcweir 	}
814cdf0e10cSrcweir 
815cdf0e10cSrcweir 	pAddr= (oslHostAddr)rtl_allocateMemory (sizeof (struct oslHostAddrImpl));
816cdf0e10cSrcweir 
817cdf0e10cSrcweir 	if (pAddr == NULL)
818cdf0e10cSrcweir 	{
819cdf0e10cSrcweir 		rtl_uString_release(cn);
820cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
821cdf0e10cSrcweir 	}
822cdf0e10cSrcweir 
823cdf0e10cSrcweir 	pAddr->pHostName= cn;
824cdf0e10cSrcweir 	pAddr->pSockAddr= osl_copySocketAddr( pSocketAddr );
825cdf0e10cSrcweir 
826cdf0e10cSrcweir 	return ((oslHostAddr)pAddr);
827cdf0e10cSrcweir }
828cdf0e10cSrcweir 
829cdf0e10cSrcweir /*****************************************************************************/
830cdf0e10cSrcweir /* osl_createHostAddrByName */
831cdf0e10cSrcweir /*****************************************************************************/
832cdf0e10cSrcweir oslHostAddr SAL_CALL osl_createHostAddrByName(rtl_uString *strHostname)
833cdf0e10cSrcweir {
834cdf0e10cSrcweir 	if ((strHostname == NULL) || (strHostname->length == 0))
835cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
836cdf0e10cSrcweir 
837cdf0e10cSrcweir 	if (__osl_attemptSocketDialupImpl())
838cdf0e10cSrcweir 	{
839cdf0e10cSrcweir 		struct hostent *he;
840cdf0e10cSrcweir 		rtl_String     *Hostname= NULL;
841cdf0e10cSrcweir 
842cdf0e10cSrcweir 		rtl_uString2String(
843cdf0e10cSrcweir 			&Hostname, strHostname->buffer, strHostname->length,
844cdf0e10cSrcweir 			RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
845cdf0e10cSrcweir 
846cdf0e10cSrcweir 		he= gethostbyname (Hostname->buffer);
847cdf0e10cSrcweir 
848cdf0e10cSrcweir 		rtl_string_release (Hostname);
849cdf0e10cSrcweir 		return __osl_hostentToHostAddr (he);
850cdf0e10cSrcweir 	}
851cdf0e10cSrcweir 	return ((oslHostAddr)NULL);
852cdf0e10cSrcweir }
853cdf0e10cSrcweir 
854cdf0e10cSrcweir /*****************************************************************************/
855cdf0e10cSrcweir /* osl_createHostAddrByAddr */
856cdf0e10cSrcweir /*****************************************************************************/
857cdf0e10cSrcweir oslHostAddr SAL_CALL osl_createHostAddrByAddr(const oslSocketAddr pAddr)
858cdf0e10cSrcweir {
859cdf0e10cSrcweir 	if (pAddr == NULL)
860cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
861cdf0e10cSrcweir 
862cdf0e10cSrcweir 	if (pAddr->m_sockaddr.sa_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet))
863cdf0e10cSrcweir 	{
864cdf0e10cSrcweir 		const struct sockaddr_in *sin= (const struct sockaddr_in *)&(pAddr->m_sockaddr);
865cdf0e10cSrcweir 
866cdf0e10cSrcweir 		if (sin->sin_addr.s_addr == htonl(INADDR_ANY))
867cdf0e10cSrcweir 			return ((oslHostAddr)NULL);
868cdf0e10cSrcweir 
869cdf0e10cSrcweir 		if (__osl_attemptSocketDialupImpl())
870cdf0e10cSrcweir 		{
871cdf0e10cSrcweir 			struct hostent *he;
872cdf0e10cSrcweir 			he= gethostbyaddr ((const sal_Char *)&(sin->sin_addr),
873cdf0e10cSrcweir 							   sizeof (sin->sin_addr),
874cdf0e10cSrcweir 							   sin->sin_family);
875cdf0e10cSrcweir 			return __osl_hostentToHostAddr (he);
876cdf0e10cSrcweir 		}
877cdf0e10cSrcweir 	}
878cdf0e10cSrcweir 
879cdf0e10cSrcweir 	return ((oslHostAddr)NULL);
880cdf0e10cSrcweir }
881cdf0e10cSrcweir 
882cdf0e10cSrcweir /*****************************************************************************/
883cdf0e10cSrcweir /* osl_copyHostAddr */
884cdf0e10cSrcweir /*****************************************************************************/
885cdf0e10cSrcweir oslHostAddr SAL_CALL osl_copyHostAddr(const oslHostAddr Addr)
886cdf0e10cSrcweir {
887cdf0e10cSrcweir 	oslHostAddr pAddr= (oslHostAddr)Addr;
888cdf0e10cSrcweir 
889cdf0e10cSrcweir 	if (pAddr)
890cdf0e10cSrcweir 		return osl_createHostAddr (pAddr->pHostName, pAddr->pSockAddr);
891cdf0e10cSrcweir 	else
892cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
893cdf0e10cSrcweir }
894cdf0e10cSrcweir 
895cdf0e10cSrcweir /*****************************************************************************/
896cdf0e10cSrcweir /* osl_getHostnameOfHostAddr */
897cdf0e10cSrcweir /*****************************************************************************/
898cdf0e10cSrcweir void SAL_CALL osl_getHostnameOfHostAddr(
899cdf0e10cSrcweir 	const oslHostAddr pAddr, rtl_uString **strHostname)
900cdf0e10cSrcweir {
901cdf0e10cSrcweir 	if (pAddr)
902cdf0e10cSrcweir 		rtl_uString_assign (strHostname, pAddr->pHostName);
903cdf0e10cSrcweir 	else
904cdf0e10cSrcweir 		rtl_uString_new (strHostname);
905cdf0e10cSrcweir }
906cdf0e10cSrcweir 
907cdf0e10cSrcweir /*****************************************************************************/
908cdf0e10cSrcweir /* osl_getSocketAddrOfHostAddr */
909cdf0e10cSrcweir /*****************************************************************************/
910cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_getSocketAddrOfHostAddr(const oslHostAddr pAddr)
911cdf0e10cSrcweir {
912cdf0e10cSrcweir 	if (pAddr)
913cdf0e10cSrcweir 		return (const oslSocketAddr)(pAddr->pSockAddr);
914cdf0e10cSrcweir 	else
915cdf0e10cSrcweir 		return NULL;
916cdf0e10cSrcweir }
917cdf0e10cSrcweir 
918cdf0e10cSrcweir /*****************************************************************************/
919cdf0e10cSrcweir /* osl_destroyHostAddr */
920cdf0e10cSrcweir /*****************************************************************************/
921cdf0e10cSrcweir void SAL_CALL osl_destroyHostAddr(oslHostAddr pAddr)
922cdf0e10cSrcweir {
923cdf0e10cSrcweir 	if (pAddr)
924cdf0e10cSrcweir 	{
925cdf0e10cSrcweir 		if (pAddr->pHostName)
926cdf0e10cSrcweir 			rtl_uString_release (pAddr->pHostName);
927cdf0e10cSrcweir 		if (pAddr->pSockAddr)
928cdf0e10cSrcweir 			osl_destroySocketAddr( pAddr->pSockAddr );
929cdf0e10cSrcweir 
930cdf0e10cSrcweir 		rtl_freeMemory (pAddr);
931cdf0e10cSrcweir 	}
932cdf0e10cSrcweir }
933cdf0e10cSrcweir 
934cdf0e10cSrcweir /*****************************************************************************/
935cdf0e10cSrcweir /* osl_getLocalHostname */
936cdf0e10cSrcweir /*****************************************************************************/
937cdf0e10cSrcweir oslSocketResult SAL_CALL osl_getLocalHostname (rtl_uString **strLocalHostname)
938cdf0e10cSrcweir {
939cdf0e10cSrcweir 	static sal_Unicode LocalHostname[256] = {0};
940cdf0e10cSrcweir 
941cdf0e10cSrcweir 	if (rtl_ustr_getLength(LocalHostname) == 0)
942cdf0e10cSrcweir 	{
943cdf0e10cSrcweir 		sal_Char Host[256]= "";
944cdf0e10cSrcweir 		if (gethostname(Host, sizeof(Host)) == 0)
945cdf0e10cSrcweir 		{
946cdf0e10cSrcweir 			/* check if we have an FQDN */
947cdf0e10cSrcweir 	    	if (strchr(Host, '.') == NULL)
948cdf0e10cSrcweir 	        {
949cdf0e10cSrcweir 				oslHostAddr pAddr;
950cdf0e10cSrcweir 				rtl_uString     *hostName= NULL;
951cdf0e10cSrcweir 
952cdf0e10cSrcweir 				rtl_string2UString(
953cdf0e10cSrcweir 					&hostName, Host, strlen(Host),
954cdf0e10cSrcweir 					RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
955cdf0e10cSrcweir                 OSL_ASSERT(hostName != 0);
956cdf0e10cSrcweir 
957cdf0e10cSrcweir 				/* no, determine it via dns */
958cdf0e10cSrcweir 				pAddr = osl_createHostAddrByName(hostName);
959cdf0e10cSrcweir 				rtl_uString_release (hostName);
960cdf0e10cSrcweir 
961cdf0e10cSrcweir 				if (pAddr && pAddr->pHostName)
962cdf0e10cSrcweir 					memcpy(LocalHostname, pAddr->pHostName->buffer, sizeof(sal_Unicode)*(rtl_ustr_getLength(pAddr->pHostName->buffer)+1));
963cdf0e10cSrcweir 				else
964cdf0e10cSrcweir 					memset(LocalHostname, 0, sizeof(LocalHostname));
965cdf0e10cSrcweir 
966cdf0e10cSrcweir 				osl_destroyHostAddr ((oslHostAddr)pAddr);
967cdf0e10cSrcweir 			}
968cdf0e10cSrcweir 		}
969cdf0e10cSrcweir 	}
970cdf0e10cSrcweir 
971cdf0e10cSrcweir 	if (rtl_ustr_getLength(LocalHostname) > 0)
972cdf0e10cSrcweir 	{
973cdf0e10cSrcweir 		rtl_uString_newFromStr (strLocalHostname, LocalHostname);
974cdf0e10cSrcweir 		return osl_Socket_Ok;
975cdf0e10cSrcweir 	}
976cdf0e10cSrcweir 
977cdf0e10cSrcweir 	return osl_Socket_Error;
978cdf0e10cSrcweir }
979cdf0e10cSrcweir 
980cdf0e10cSrcweir /*****************************************************************************/
981cdf0e10cSrcweir /* osl_resolveHostname */
982cdf0e10cSrcweir /*****************************************************************************/
983cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_resolveHostname(rtl_uString* strHostname)
984cdf0e10cSrcweir {
985cdf0e10cSrcweir 	oslHostAddr pAddr=
986cdf0e10cSrcweir 		(oslHostAddr )osl_createHostAddrByName (strHostname);
987cdf0e10cSrcweir 	if (pAddr)
988cdf0e10cSrcweir 	{
989cdf0e10cSrcweir 		oslSocketAddr SockAddr = osl_copySocketAddr( pAddr->pSockAddr );
990cdf0e10cSrcweir 		osl_destroyHostAddr(pAddr);
991cdf0e10cSrcweir 		return (SockAddr);
992cdf0e10cSrcweir 	}
993cdf0e10cSrcweir 	return ((oslSocketAddr)NULL);
994cdf0e10cSrcweir }
995cdf0e10cSrcweir 
996cdf0e10cSrcweir /*****************************************************************************/
997cdf0e10cSrcweir /* osl_getServicePort */
998cdf0e10cSrcweir /*****************************************************************************/
999cdf0e10cSrcweir sal_Int32 SAL_CALL osl_getServicePort (
1000cdf0e10cSrcweir 	rtl_uString* strServicename,
1001cdf0e10cSrcweir 	rtl_uString* strProtocol)
1002cdf0e10cSrcweir {
1003cdf0e10cSrcweir 	struct servent* ps;
1004cdf0e10cSrcweir 
1005cdf0e10cSrcweir 	rtl_String *str_Servicename=NULL;
1006cdf0e10cSrcweir 	rtl_String *str_Protocol=NULL;
1007cdf0e10cSrcweir 
1008cdf0e10cSrcweir 	rtl_uString2String(
1009cdf0e10cSrcweir 		&str_Servicename,
1010cdf0e10cSrcweir 		rtl_uString_getStr(strServicename),
1011cdf0e10cSrcweir 		rtl_uString_getLength(strServicename),
1012cdf0e10cSrcweir 		RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
1013cdf0e10cSrcweir 	rtl_uString2String(
1014cdf0e10cSrcweir 		&str_Protocol,
1015cdf0e10cSrcweir 		rtl_uString_getStr(strProtocol),
1016cdf0e10cSrcweir 		rtl_uString_getLength(strProtocol),
1017cdf0e10cSrcweir 		RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
1018cdf0e10cSrcweir 
1019cdf0e10cSrcweir 	ps= getservbyname(
1020cdf0e10cSrcweir 		rtl_string_getStr(str_Servicename),
1021cdf0e10cSrcweir 		rtl_string_getStr(str_Protocol));
1022cdf0e10cSrcweir 
1023cdf0e10cSrcweir 	rtl_string_release( str_Servicename );
1024cdf0e10cSrcweir 	rtl_string_release( str_Protocol );
1025cdf0e10cSrcweir 
1026cdf0e10cSrcweir 	if (ps != 0)
1027cdf0e10cSrcweir 		return ntohs(ps->s_port);
1028cdf0e10cSrcweir 
1029cdf0e10cSrcweir 	return OSL_INVALID_PORT;
1030cdf0e10cSrcweir }
1031cdf0e10cSrcweir 
1032cdf0e10cSrcweir /*****************************************************************************/
1033cdf0e10cSrcweir /* osl_destroySocketAddr */
1034cdf0e10cSrcweir /*****************************************************************************/
1035cdf0e10cSrcweir void SAL_CALL osl_destroySocketAddr(oslSocketAddr pAddr)
1036cdf0e10cSrcweir {
1037cdf0e10cSrcweir 	__osl_destroySocketAddr( pAddr );
1038cdf0e10cSrcweir }
1039cdf0e10cSrcweir 
1040cdf0e10cSrcweir /*****************************************************************************/
1041cdf0e10cSrcweir /* osl_getFamilyOfSocketAddr */
1042cdf0e10cSrcweir /*****************************************************************************/
1043cdf0e10cSrcweir oslAddrFamily SAL_CALL osl_getFamilyOfSocketAddr(oslSocketAddr pAddr)
1044cdf0e10cSrcweir {
1045cdf0e10cSrcweir 	if (pAddr)
1046cdf0e10cSrcweir 		return FAMILY_FROM_NATIVE(pAddr->m_sockaddr.sa_family);
1047cdf0e10cSrcweir 	else
1048cdf0e10cSrcweir 		return osl_Socket_FamilyInvalid;
1049cdf0e10cSrcweir }
1050cdf0e10cSrcweir 
1051cdf0e10cSrcweir /*****************************************************************************/
1052cdf0e10cSrcweir /* osl_getInetPortOfSocketAddr */
1053cdf0e10cSrcweir /*****************************************************************************/
1054cdf0e10cSrcweir sal_Int32 SAL_CALL osl_getInetPortOfSocketAddr(oslSocketAddr pAddr)
1055cdf0e10cSrcweir {
1056cdf0e10cSrcweir 	if( pAddr )
1057cdf0e10cSrcweir 	{
1058cdf0e10cSrcweir 		struct sockaddr_in* pSystemInetAddr= (struct sockaddr_in*)&(pAddr->m_sockaddr);
1059cdf0e10cSrcweir 
1060cdf0e10cSrcweir 		if ( (pSystemInetAddr->sin_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet)))
1061cdf0e10cSrcweir 			return ntohs(pSystemInetAddr->sin_port);
1062cdf0e10cSrcweir 	}
1063cdf0e10cSrcweir 	return OSL_INVALID_PORT;
1064cdf0e10cSrcweir }
1065cdf0e10cSrcweir 
1066cdf0e10cSrcweir /*****************************************************************************/
1067cdf0e10cSrcweir /* osl_setInetPortOfSocketAddr */
1068cdf0e10cSrcweir /*****************************************************************************/
1069cdf0e10cSrcweir sal_Bool SAL_CALL osl_setInetPortOfSocketAddr (
1070cdf0e10cSrcweir 	oslSocketAddr pAddr,
1071cdf0e10cSrcweir 	sal_Int32     Port)
1072cdf0e10cSrcweir {
1073cdf0e10cSrcweir 	if (pAddr == NULL)
1074cdf0e10cSrcweir 		return sal_False;
1075cdf0e10cSrcweir 
1076cdf0e10cSrcweir 	struct sockaddr_in* pSystemInetAddr= (struct sockaddr_in*)&(pAddr->m_sockaddr);
1077cdf0e10cSrcweir 
1078cdf0e10cSrcweir 	if (pSystemInetAddr->sin_family != FAMILY_TO_NATIVE(osl_Socket_FamilyInet))
1079cdf0e10cSrcweir 		return sal_False;
1080cdf0e10cSrcweir 
1081cdf0e10cSrcweir 	pSystemInetAddr->sin_port= htons((short)Port);
1082cdf0e10cSrcweir 	return sal_True;
1083cdf0e10cSrcweir }
1084cdf0e10cSrcweir 
1085cdf0e10cSrcweir /*****************************************************************************/
1086cdf0e10cSrcweir /* osl_getHostnameOfSocketAddr */
1087cdf0e10cSrcweir /*****************************************************************************/
1088cdf0e10cSrcweir oslSocketResult SAL_CALL osl_getHostnameOfSocketAddr (
1089cdf0e10cSrcweir 	oslSocketAddr   Addr,
1090cdf0e10cSrcweir 	rtl_uString   **strHostName)
1091cdf0e10cSrcweir {
1092cdf0e10cSrcweir 	oslHostAddr pAddr= osl_createHostAddrByAddr (Addr);
1093cdf0e10cSrcweir 
1094cdf0e10cSrcweir 	if (pAddr)
1095cdf0e10cSrcweir 	{
1096cdf0e10cSrcweir 		rtl_uString_newFromString(strHostName, pAddr->pHostName);
1097cdf0e10cSrcweir 
1098cdf0e10cSrcweir 		osl_destroyHostAddr(pAddr);
1099cdf0e10cSrcweir 
1100cdf0e10cSrcweir 		return osl_Socket_Ok;
1101cdf0e10cSrcweir 	}
1102cdf0e10cSrcweir 
1103cdf0e10cSrcweir 	return osl_Socket_Error;
1104cdf0e10cSrcweir }
1105cdf0e10cSrcweir 
1106cdf0e10cSrcweir /*****************************************************************************/
1107cdf0e10cSrcweir /* osl_getDottedInetAddrOfSocketAddr */
1108cdf0e10cSrcweir /*****************************************************************************/
1109cdf0e10cSrcweir oslSocketResult	SAL_CALL osl_getDottedInetAddrOfSocketAddr (
1110cdf0e10cSrcweir 	oslSocketAddr   pAddr,
1111cdf0e10cSrcweir 	rtl_uString   **strDottedInetAddr)
1112cdf0e10cSrcweir {
1113cdf0e10cSrcweir 	sal_Char           *pDotted;
1114cdf0e10cSrcweir 
1115cdf0e10cSrcweir 	if (pAddr == NULL)
1116cdf0e10cSrcweir 		return osl_Socket_Error;
1117cdf0e10cSrcweir 
1118cdf0e10cSrcweir 	struct sockaddr_in *pSystemInetAddr = (struct sockaddr_in*) &(pAddr->m_sockaddr);
1119cdf0e10cSrcweir 	if (pSystemInetAddr->sin_family != FAMILY_TO_NATIVE(osl_Socket_FamilyInet))
1120cdf0e10cSrcweir 		return osl_Socket_Error;
1121cdf0e10cSrcweir 
1122cdf0e10cSrcweir 	pDotted = inet_ntoa (pSystemInetAddr->sin_addr);
1123cdf0e10cSrcweir 	rtl_string2UString(
1124cdf0e10cSrcweir 		strDottedInetAddr, pDotted, strlen (pDotted),
1125cdf0e10cSrcweir 		RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
1126cdf0e10cSrcweir     OSL_ASSERT(*strDottedInetAddr != 0);
1127cdf0e10cSrcweir 
1128cdf0e10cSrcweir 	return osl_Socket_Ok;
1129cdf0e10cSrcweir }
1130cdf0e10cSrcweir 
1131cdf0e10cSrcweir /*****************************************************************************/
1132cdf0e10cSrcweir /* osl_createSocket  */
1133cdf0e10cSrcweir /*****************************************************************************/
1134cdf0e10cSrcweir oslSocket SAL_CALL osl_createSocket (
1135cdf0e10cSrcweir 	oslAddrFamily Family,
1136cdf0e10cSrcweir 	oslSocketType Type,
1137cdf0e10cSrcweir 	oslProtocol   Protocol)
1138cdf0e10cSrcweir {
1139cdf0e10cSrcweir 	/* alloc memory */
1140cdf0e10cSrcweir 	oslSocket pSocket = __osl_createSocketImpl(0);
1141cdf0e10cSrcweir 
1142cdf0e10cSrcweir 	if (pSocket == NULL)
1143cdf0e10cSrcweir 		return 0;
1144cdf0e10cSrcweir 
1145cdf0e10cSrcweir 	/* create socket */
1146cdf0e10cSrcweir 	pSocket->m_Socket= socket(FAMILY_TO_NATIVE(Family),
1147cdf0e10cSrcweir 								TYPE_TO_NATIVE(Type),
1148cdf0e10cSrcweir 								PROTOCOL_TO_NATIVE(Protocol));
1149cdf0e10cSrcweir 
1150cdf0e10cSrcweir 	/* creation failed => free memory */
1151cdf0e10cSrcweir 	if(pSocket->m_Socket == OSL_INVALID_SOCKET)
1152cdf0e10cSrcweir 	{
1153cdf0e10cSrcweir 		__osl_destroySocketImpl(pSocket);
1154cdf0e10cSrcweir 		pSocket= 0;
1155cdf0e10cSrcweir 	}
1156cdf0e10cSrcweir 	else
1157cdf0e10cSrcweir 	{
1158cdf0e10cSrcweir 		pSocket->m_Flags			= 0;
1159cdf0e10cSrcweir 		pSocket->m_CloseCallback	= NULL;
1160cdf0e10cSrcweir 		pSocket->m_CallbackArg	= NULL;
1161cdf0e10cSrcweir 	}
1162cdf0e10cSrcweir 
1163cdf0e10cSrcweir 	return pSocket;
1164cdf0e10cSrcweir }
1165cdf0e10cSrcweir 
1166cdf0e10cSrcweir void SAL_CALL osl_acquireSocket( oslSocket pSocket )
1167cdf0e10cSrcweir {
1168cdf0e10cSrcweir 	osl_incrementInterlockedCount( &(pSocket->m_nRefCount) );
1169cdf0e10cSrcweir }
1170cdf0e10cSrcweir 
1171cdf0e10cSrcweir void SAL_CALL osl_releaseSocket( oslSocket pSocket )
1172cdf0e10cSrcweir {
1173cdf0e10cSrcweir 	if( pSocket && 0 == osl_decrementInterlockedCount( &(pSocket->m_nRefCount) ) )
1174cdf0e10cSrcweir 	{
1175cdf0e10cSrcweir 		osl_closeSocket( pSocket );
1176cdf0e10cSrcweir 		__osl_destroySocketImpl( pSocket );
1177cdf0e10cSrcweir 	}
1178cdf0e10cSrcweir }
1179cdf0e10cSrcweir 
1180cdf0e10cSrcweir /*****************************************************************************/
1181cdf0e10cSrcweir /* osl_closeSocket  */
1182cdf0e10cSrcweir /*****************************************************************************/
1183cdf0e10cSrcweir void SAL_CALL osl_closeSocket(oslSocket pSocket)
1184cdf0e10cSrcweir {
1185cdf0e10cSrcweir 	/* socket already invalid */
1186cdf0e10cSrcweir 	if(pSocket==0)
1187cdf0e10cSrcweir 		return;
1188cdf0e10cSrcweir 
1189cdf0e10cSrcweir 	/* close */
1190cdf0e10cSrcweir 	closesocket(pSocket->m_Socket);
1191cdf0e10cSrcweir 
1192cdf0e10cSrcweir 	pSocket->m_Socket = OSL_INVALID_SOCKET;
1193cdf0e10cSrcweir 
1194cdf0e10cSrcweir 	/* registrierten Callback ausfuehren */
1195cdf0e10cSrcweir 	if (pSocket->m_CloseCallback != NULL)
1196cdf0e10cSrcweir 	{
1197cdf0e10cSrcweir 		pSocket->m_CloseCallback(pSocket->m_CallbackArg);
1198cdf0e10cSrcweir 	}
1199cdf0e10cSrcweir }
1200cdf0e10cSrcweir 
1201cdf0e10cSrcweir /*****************************************************************************/
1202cdf0e10cSrcweir /* osl_getLocalAddrOfSocket  */
1203cdf0e10cSrcweir /* Note that I rely on the fact that oslSocketAddr and struct sockaddr */
1204cdf0e10cSrcweir /* are the same! I don't like it very much but see no other easy way */
1205cdf0e10cSrcweir /* to conceal the struct sockaddr from the eyes of the user. */
1206cdf0e10cSrcweir /*****************************************************************************/
1207cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_getLocalAddrOfSocket(oslSocket pSocket)
1208cdf0e10cSrcweir {
1209cdf0e10cSrcweir 	struct sockaddr Addr;
1210cdf0e10cSrcweir 	int             AddrLen;
1211cdf0e10cSrcweir 
1212cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1213cdf0e10cSrcweir 		return ((oslSocketAddr)NULL);
1214cdf0e10cSrcweir 
1215cdf0e10cSrcweir 	AddrLen= sizeof(struct sockaddr);
1216cdf0e10cSrcweir 
1217cdf0e10cSrcweir 	if (getsockname(pSocket->m_Socket, &Addr, &AddrLen) == OSL_SOCKET_ERROR)
1218cdf0e10cSrcweir 		return ((oslSocketAddr)NULL);
1219cdf0e10cSrcweir 
1220cdf0e10cSrcweir 	oslSocketAddr pAddr = __osl_createSocketAddrFromSystem( &Addr );
1221cdf0e10cSrcweir 	return pAddr;
1222cdf0e10cSrcweir }
1223cdf0e10cSrcweir 
1224cdf0e10cSrcweir /*****************************************************************************/
1225cdf0e10cSrcweir /* osl_getPeerAddrOfSocket  */
1226cdf0e10cSrcweir /*****************************************************************************/
1227cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_getPeerAddrOfSocket(oslSocket pSocket)
1228cdf0e10cSrcweir {
1229cdf0e10cSrcweir 	struct sockaddr Addr;
1230cdf0e10cSrcweir 	int             AddrLen;
1231cdf0e10cSrcweir 
1232cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1233cdf0e10cSrcweir 		return ((oslSocketAddr)NULL);
1234cdf0e10cSrcweir 
1235cdf0e10cSrcweir 	AddrLen= sizeof(struct sockaddr);
1236cdf0e10cSrcweir 
1237cdf0e10cSrcweir 	if (getpeername(pSocket->m_Socket, &Addr, &AddrLen) == OSL_SOCKET_ERROR)
1238cdf0e10cSrcweir 		return ((oslSocketAddr)NULL);
1239cdf0e10cSrcweir 
1240cdf0e10cSrcweir 	oslSocketAddr pAddr = __osl_createSocketAddrFromSystem( &Addr );
1241cdf0e10cSrcweir 	return pAddr;
1242cdf0e10cSrcweir }
1243cdf0e10cSrcweir 
1244cdf0e10cSrcweir /*****************************************************************************/
1245cdf0e10cSrcweir /* osl_bindAddrToSocket  */
1246cdf0e10cSrcweir /*****************************************************************************/
1247cdf0e10cSrcweir sal_Bool SAL_CALL osl_bindAddrToSocket ( oslSocket pSocket, oslSocketAddr pAddr)
1248cdf0e10cSrcweir {
1249cdf0e10cSrcweir 	OSL_ASSERT( pAddr );
1250cdf0e10cSrcweir 
1251cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1252cdf0e10cSrcweir 		return sal_False;
1253cdf0e10cSrcweir 
1254cdf0e10cSrcweir 	return (bind(pSocket->m_Socket,
1255cdf0e10cSrcweir 				 &(pAddr->m_sockaddr),
1256cdf0e10cSrcweir 				 sizeof(struct sockaddr)) != OSL_SOCKET_ERROR);
1257cdf0e10cSrcweir }
1258cdf0e10cSrcweir 
1259cdf0e10cSrcweir /*****************************************************************************/
1260cdf0e10cSrcweir /* osl_connectSocketTo  */
1261cdf0e10cSrcweir /*****************************************************************************/
1262cdf0e10cSrcweir oslSocketResult SAL_CALL osl_connectSocketTo (
1263cdf0e10cSrcweir 	oslSocket        pSocket,
1264cdf0e10cSrcweir 	oslSocketAddr    pAddr,
1265cdf0e10cSrcweir 	const TimeValue* pTimeout)
1266cdf0e10cSrcweir {
1267cdf0e10cSrcweir 
1268cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1269cdf0e10cSrcweir 		return osl_Socket_Error;
1270cdf0e10cSrcweir 
1271cdf0e10cSrcweir 	if (pAddr == NULL) /* EDESTADDRREQ */
1272cdf0e10cSrcweir 		return osl_Socket_Error;
1273cdf0e10cSrcweir 
1274cdf0e10cSrcweir 	if (!__osl_attemptSocketDialupImpl()) /* ENETDOWN */
1275cdf0e10cSrcweir 		return osl_Socket_Error;
1276cdf0e10cSrcweir 
1277cdf0e10cSrcweir 	if (pTimeout == NULL)
1278cdf0e10cSrcweir 	{
1279cdf0e10cSrcweir 		if(connect(pSocket->m_Socket,
1280cdf0e10cSrcweir 				   &(pAddr->m_sockaddr),
1281cdf0e10cSrcweir 					sizeof(struct sockaddr)) == OSL_SOCKET_ERROR)
1282cdf0e10cSrcweir 			return osl_Socket_Error;
1283cdf0e10cSrcweir 		else
1284cdf0e10cSrcweir 			return osl_Socket_Ok;
1285cdf0e10cSrcweir 	}
1286cdf0e10cSrcweir 	else
1287cdf0e10cSrcweir 	{
1288cdf0e10cSrcweir 		fd_set          fds;
1289cdf0e10cSrcweir 		int	            error;
1290cdf0e10cSrcweir 	    struct timeval  tv;
1291cdf0e10cSrcweir 		unsigned long   Param;
1292cdf0e10cSrcweir 		oslSocketResult Result= osl_Socket_Ok;
1293cdf0e10cSrcweir 
1294cdf0e10cSrcweir 		if (pSocket->m_Flags & OSL_SOCKET_FLAGS_NONBLOCKING)
1295cdf0e10cSrcweir 	    {
1296cdf0e10cSrcweir 			if (connect(pSocket->m_Socket,
1297cdf0e10cSrcweir 			    	    &(pAddr->m_sockaddr),
1298cdf0e10cSrcweir 						sizeof(struct sockaddr)) == OSL_SOCKET_ERROR)
1299cdf0e10cSrcweir 			{
1300cdf0e10cSrcweir 			    switch (WSAGetLastError())
1301cdf0e10cSrcweir 			    {
1302cdf0e10cSrcweir 					case WSAEWOULDBLOCK:
1303cdf0e10cSrcweir 					case WSAEINPROGRESS:
1304cdf0e10cSrcweir 						return osl_Socket_InProgress;
1305cdf0e10cSrcweir 
1306cdf0e10cSrcweir 					default:
1307cdf0e10cSrcweir 						return osl_Socket_Error;
1308cdf0e10cSrcweir 				}
1309cdf0e10cSrcweir 			}
1310cdf0e10cSrcweir 			else
1311cdf0e10cSrcweir 				return osl_Socket_Ok;
1312cdf0e10cSrcweir 		}
1313cdf0e10cSrcweir 
1314cdf0e10cSrcweir 		/* set socket temporarily to non-blocking */
1315cdf0e10cSrcweir 		Param= 1;
1316cdf0e10cSrcweir 		OSL_VERIFY(ioctlsocket(
1317cdf0e10cSrcweir 			pSocket->m_Socket, FIONBIO, &Param) != OSL_SOCKET_ERROR);
1318cdf0e10cSrcweir 
1319cdf0e10cSrcweir 		/* initiate connect */
1320cdf0e10cSrcweir 		if (connect(pSocket->m_Socket,
1321cdf0e10cSrcweir 		     	    &(pAddr->m_sockaddr),
1322cdf0e10cSrcweir 				    sizeof(struct sockaddr)) != OSL_SOCKET_ERROR)
1323cdf0e10cSrcweir 		{
1324cdf0e10cSrcweir 		   /* immediate connection */
1325cdf0e10cSrcweir 
1326cdf0e10cSrcweir 			Param= 0;
1327cdf0e10cSrcweir 			ioctlsocket(pSocket->m_Socket, FIONBIO, &Param);
1328cdf0e10cSrcweir 
1329cdf0e10cSrcweir 			return osl_Socket_Ok;
1330cdf0e10cSrcweir 	    }
1331cdf0e10cSrcweir 		else
1332cdf0e10cSrcweir 		{
1333cdf0e10cSrcweir 			error = WSAGetLastError();
1334cdf0e10cSrcweir 
1335cdf0e10cSrcweir 		    /* really an error or just delayed? */
1336cdf0e10cSrcweir 		    if (error != WSAEWOULDBLOCK && error != WSAEINPROGRESS)
1337cdf0e10cSrcweir 		    {
1338cdf0e10cSrcweir 			     Param= 0;
1339cdf0e10cSrcweir 			     ioctlsocket(pSocket->m_Socket, FIONBIO, &Param);
1340cdf0e10cSrcweir 
1341cdf0e10cSrcweir 			     return osl_Socket_Error;
1342cdf0e10cSrcweir 		    }
1343cdf0e10cSrcweir 		}
1344cdf0e10cSrcweir 
1345cdf0e10cSrcweir 		/* prepare select set for socket  */
1346cdf0e10cSrcweir 		FD_ZERO(&fds);
1347cdf0e10cSrcweir 		FD_SET(pSocket->m_Socket, &fds);
1348cdf0e10cSrcweir 
1349cdf0e10cSrcweir 		/* divide milliseconds into seconds and microseconds */
1350cdf0e10cSrcweir 		tv.tv_sec=	pTimeout->Seconds;
1351cdf0e10cSrcweir 		tv.tv_usec=	pTimeout->Nanosec / 1000L;
1352cdf0e10cSrcweir 
1353cdf0e10cSrcweir 		/* select */
1354cdf0e10cSrcweir 	    error= select(pSocket->m_Socket+1,
1355cdf0e10cSrcweir 		 		      0,
1356cdf0e10cSrcweir 					  &fds,
1357cdf0e10cSrcweir 					  0,
1358cdf0e10cSrcweir 					  &tv);
1359cdf0e10cSrcweir 
1360cdf0e10cSrcweir 		if (error > 0)  /* connected */
1361cdf0e10cSrcweir 		{
1362cdf0e10cSrcweir 			OSL_POSTCOND(
1363cdf0e10cSrcweir 				FD_ISSET(pSocket->m_Socket, &fds),
1364cdf0e10cSrcweir 				"osl_connectSocketTo(): select returned but socket not set\n");
1365cdf0e10cSrcweir 
1366cdf0e10cSrcweir 			Result= osl_Socket_Ok;
1367cdf0e10cSrcweir 
1368cdf0e10cSrcweir 		}
1369cdf0e10cSrcweir 		else if(error < 0)  /* error */
1370cdf0e10cSrcweir 		{
1371cdf0e10cSrcweir 		    /* errno == EBADF: most probably interrupted by close() */
1372cdf0e10cSrcweir 		    if(WSAGetLastError() == WSAEBADF)
1373cdf0e10cSrcweir 			{
1374cdf0e10cSrcweir 			    /* do not access pSockImpl because it is about to be or */
1375cdf0e10cSrcweir 			    /* already destroyed */
1376cdf0e10cSrcweir 			    return osl_Socket_Interrupted;
1377cdf0e10cSrcweir 			}
1378cdf0e10cSrcweir 			else
1379cdf0e10cSrcweir 			    Result= osl_Socket_Error;
1380cdf0e10cSrcweir 
1381cdf0e10cSrcweir 		}
1382cdf0e10cSrcweir 		else    /* timeout */
1383cdf0e10cSrcweir 			Result= osl_Socket_TimedOut;
1384cdf0e10cSrcweir 
1385cdf0e10cSrcweir 
1386cdf0e10cSrcweir 		/* clean up */
1387cdf0e10cSrcweir 		Param= 0;
1388cdf0e10cSrcweir 		ioctlsocket(pSocket->m_Socket, FIONBIO, &Param);
1389cdf0e10cSrcweir 
1390cdf0e10cSrcweir 		return Result;
1391cdf0e10cSrcweir 	}
1392cdf0e10cSrcweir }
1393cdf0e10cSrcweir 
1394cdf0e10cSrcweir /*****************************************************************************/
1395cdf0e10cSrcweir /* osl_listenOnSocket  */
1396cdf0e10cSrcweir /*****************************************************************************/
1397cdf0e10cSrcweir sal_Bool SAL_CALL osl_listenOnSocket (
1398cdf0e10cSrcweir 	oslSocket  pSocket,
1399cdf0e10cSrcweir 	sal_Int32  MaxPendingConnections)
1400cdf0e10cSrcweir {
1401cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1402cdf0e10cSrcweir 		return sal_False;
1403cdf0e10cSrcweir 
1404cdf0e10cSrcweir 	return (listen(pSocket->m_Socket,
1405cdf0e10cSrcweir 				   MaxPendingConnections == -1 ?
1406cdf0e10cSrcweir 				   SOMAXCONN :
1407cdf0e10cSrcweir 				   MaxPendingConnections) != OSL_SOCKET_ERROR);
1408cdf0e10cSrcweir }
1409cdf0e10cSrcweir 
1410cdf0e10cSrcweir /*****************************************************************************/
1411cdf0e10cSrcweir /* osl_acceptConnectionOnSocket  */
1412cdf0e10cSrcweir /*****************************************************************************/
1413cdf0e10cSrcweir oslSocket SAL_CALL osl_acceptConnectionOnSocket (
1414cdf0e10cSrcweir 	oslSocket      pSocket,
1415cdf0e10cSrcweir 	oslSocketAddr* ppAddr)
1416cdf0e10cSrcweir {
1417cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1418cdf0e10cSrcweir 		return ((oslSocket)NULL);
1419cdf0e10cSrcweir 
1420cdf0e10cSrcweir 	SOCKET          Connection;
1421cdf0e10cSrcweir 	if(ppAddr)
1422cdf0e10cSrcweir 	{
1423cdf0e10cSrcweir 		if( *ppAddr )
1424cdf0e10cSrcweir 		{
1425cdf0e10cSrcweir 			osl_destroySocketAddr( *ppAddr );
1426cdf0e10cSrcweir 			*ppAddr = 0;
1427cdf0e10cSrcweir 		}
1428cdf0e10cSrcweir 		int AddrLen= sizeof(struct sockaddr);
1429cdf0e10cSrcweir 
1430cdf0e10cSrcweir 		/* user wants to know peer Addr */
1431cdf0e10cSrcweir 		struct sockaddr Addr;
1432cdf0e10cSrcweir 
1433cdf0e10cSrcweir 		Connection=	accept(pSocket->m_Socket, &Addr, &AddrLen);
1434cdf0e10cSrcweir 		OSL_ASSERT(AddrLen == sizeof(struct sockaddr));
1435cdf0e10cSrcweir 
1436cdf0e10cSrcweir 		if(Connection != OSL_SOCKET_ERROR)
1437cdf0e10cSrcweir 			*ppAddr= __osl_createSocketAddrFromSystem(&Addr);
1438cdf0e10cSrcweir 		else
1439cdf0e10cSrcweir 			*ppAddr = NULL;
1440cdf0e10cSrcweir 	}
1441cdf0e10cSrcweir 	else
1442cdf0e10cSrcweir 	{
1443cdf0e10cSrcweir 		/* user is not interested in peer-addr */
1444cdf0e10cSrcweir 		Connection=	accept(pSocket->m_Socket, 0, 0);
1445cdf0e10cSrcweir 	}
1446cdf0e10cSrcweir 
1447cdf0e10cSrcweir 	/* accept failed? */
1448cdf0e10cSrcweir 	if(Connection == OSL_SOCKET_ERROR)
1449cdf0e10cSrcweir 		return ((oslSocket)NULL);
1450cdf0e10cSrcweir 
1451cdf0e10cSrcweir 	/* alloc memory */
1452cdf0e10cSrcweir 	oslSocket  pConnectionSocket;
1453cdf0e10cSrcweir 	pConnectionSocket= __osl_createSocketImpl(Connection);
1454cdf0e10cSrcweir 
1455cdf0e10cSrcweir 	pConnectionSocket->m_Flags			= 0;
1456cdf0e10cSrcweir 	pConnectionSocket->m_CloseCallback	= NULL;
1457cdf0e10cSrcweir 	pConnectionSocket->m_CallbackArg	= NULL;
1458cdf0e10cSrcweir 
1459cdf0e10cSrcweir 	return pConnectionSocket;
1460cdf0e10cSrcweir }
1461cdf0e10cSrcweir 
1462cdf0e10cSrcweir /*****************************************************************************/
1463cdf0e10cSrcweir /* osl_receiveSocket  */
1464cdf0e10cSrcweir /*****************************************************************************/
1465cdf0e10cSrcweir sal_Int32 SAL_CALL osl_receiveSocket (
1466cdf0e10cSrcweir 	oslSocket        pSocket,
1467cdf0e10cSrcweir 	void*            pBuffer,
1468cdf0e10cSrcweir 	sal_uInt32       BytesToRead,
1469cdf0e10cSrcweir 	oslSocketMsgFlag Flag)
1470cdf0e10cSrcweir {
1471cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1472cdf0e10cSrcweir 		return osl_Socket_Error;
1473cdf0e10cSrcweir 
1474cdf0e10cSrcweir 	return recv(pSocket->m_Socket,
1475cdf0e10cSrcweir 				(sal_Char*)pBuffer,
1476cdf0e10cSrcweir 				BytesToRead,
1477cdf0e10cSrcweir 				MSG_FLAG_TO_NATIVE(Flag));
1478cdf0e10cSrcweir }
1479cdf0e10cSrcweir 
1480cdf0e10cSrcweir /*****************************************************************************/
1481cdf0e10cSrcweir /* osl_receiveFromSocket  */
1482cdf0e10cSrcweir /*****************************************************************************/
1483cdf0e10cSrcweir sal_Int32 SAL_CALL osl_receiveFromSocket (
1484cdf0e10cSrcweir 	oslSocket        pSocket,
1485cdf0e10cSrcweir 	oslSocketAddr    SenderAddr,
1486cdf0e10cSrcweir 	void*            pBuffer,
1487cdf0e10cSrcweir 	sal_uInt32       BufferSize,
1488cdf0e10cSrcweir 	oslSocketMsgFlag Flag)
1489cdf0e10cSrcweir {
1490cdf0e10cSrcweir 	struct sockaddr *pSystemSockAddr = 0;
1491cdf0e10cSrcweir 	int AddrLen = 0;
1492cdf0e10cSrcweir 	if( SenderAddr )
1493cdf0e10cSrcweir 	{
1494cdf0e10cSrcweir 		AddrLen = sizeof( struct sockaddr );
1495cdf0e10cSrcweir 		pSystemSockAddr = &(SenderAddr->m_sockaddr);
1496cdf0e10cSrcweir 	}
1497cdf0e10cSrcweir 
1498cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1499cdf0e10cSrcweir 		return osl_Socket_Error;
1500cdf0e10cSrcweir 
1501cdf0e10cSrcweir 	return recvfrom(pSocket->m_Socket,
1502cdf0e10cSrcweir 					 (sal_Char*)pBuffer,
1503cdf0e10cSrcweir 					 BufferSize,
1504cdf0e10cSrcweir 					 MSG_FLAG_TO_NATIVE(Flag),
1505cdf0e10cSrcweir 					 pSystemSockAddr,
1506cdf0e10cSrcweir 					 &AddrLen);
1507cdf0e10cSrcweir }
1508cdf0e10cSrcweir 
1509cdf0e10cSrcweir /*****************************************************************************/
1510cdf0e10cSrcweir /* osl_sendSocket  */
1511cdf0e10cSrcweir /*****************************************************************************/
1512cdf0e10cSrcweir sal_Int32 SAL_CALL osl_sendSocket (
1513cdf0e10cSrcweir 	oslSocket        pSocket,
1514cdf0e10cSrcweir 	const void*      pBuffer,
1515cdf0e10cSrcweir 	sal_uInt32       BytesToSend,
1516cdf0e10cSrcweir 	oslSocketMsgFlag Flag)
1517cdf0e10cSrcweir {
1518cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1519cdf0e10cSrcweir 		return osl_Socket_Error;
1520cdf0e10cSrcweir 
1521cdf0e10cSrcweir 	return send(pSocket->m_Socket,
1522cdf0e10cSrcweir 				(sal_Char*)pBuffer,
1523cdf0e10cSrcweir 				BytesToSend,
1524cdf0e10cSrcweir 				MSG_FLAG_TO_NATIVE(Flag));
1525cdf0e10cSrcweir }
1526cdf0e10cSrcweir 
1527cdf0e10cSrcweir /*****************************************************************************/
1528cdf0e10cSrcweir /* osl_sendToSocket  */
1529cdf0e10cSrcweir /*****************************************************************************/
1530cdf0e10cSrcweir sal_Int32 SAL_CALL osl_sendToSocket (
1531cdf0e10cSrcweir 	oslSocket        pSocket,
1532cdf0e10cSrcweir 	oslSocketAddr    ReceiverAddr,
1533cdf0e10cSrcweir 	const void*      pBuffer,
1534cdf0e10cSrcweir 	sal_uInt32       BytesToSend,
1535cdf0e10cSrcweir 	oslSocketMsgFlag Flag)
1536cdf0e10cSrcweir {
1537cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1538cdf0e10cSrcweir 		return osl_Socket_Error;
1539cdf0e10cSrcweir 
1540cdf0e10cSrcweir 	/* ReceiverAddr might be 0 when used on a connected socket. */
1541cdf0e10cSrcweir 	/* Then sendto should behave like send. */
1542cdf0e10cSrcweir 
1543cdf0e10cSrcweir 	struct sockaddr *pSystemSockAddr = 0;
1544cdf0e10cSrcweir 	if( ReceiverAddr )
1545cdf0e10cSrcweir 		pSystemSockAddr = &(ReceiverAddr->m_sockaddr);
1546cdf0e10cSrcweir 
1547cdf0e10cSrcweir 	return sendto(pSocket->m_Socket,
1548cdf0e10cSrcweir 				  (sal_Char*)pBuffer,
1549cdf0e10cSrcweir 				  BytesToSend,
1550cdf0e10cSrcweir 				  MSG_FLAG_TO_NATIVE(Flag),
1551cdf0e10cSrcweir 				  pSystemSockAddr,
1552cdf0e10cSrcweir 				  pSystemSockAddr == 0 ? 0 : sizeof(struct sockaddr));
1553cdf0e10cSrcweir }
1554cdf0e10cSrcweir 
1555cdf0e10cSrcweir /*****************************************************************************/
1556cdf0e10cSrcweir /* osl_readSocket  */
1557cdf0e10cSrcweir /*****************************************************************************/
1558cdf0e10cSrcweir sal_Int32 SAL_CALL osl_readSocket( oslSocket pSocket, void *pBuffer, sal_Int32 n )
1559cdf0e10cSrcweir {
1560cdf0e10cSrcweir 	sal_uInt8 * Ptr = (sal_uInt8 *)pBuffer;
1561cdf0e10cSrcweir 
1562cdf0e10cSrcweir 	OSL_ASSERT( pSocket);
1563cdf0e10cSrcweir 
1564cdf0e10cSrcweir 	/* loop until all desired bytes were read or an error occured */
1565cdf0e10cSrcweir 	sal_uInt32 BytesRead= 0;
1566cdf0e10cSrcweir 	sal_uInt32 BytesToRead= n;
1567cdf0e10cSrcweir 	while (BytesToRead > 0)
1568cdf0e10cSrcweir 	{
1569cdf0e10cSrcweir 		sal_Int32 RetVal;
1570cdf0e10cSrcweir 		RetVal= osl_receiveSocket(pSocket,
1571cdf0e10cSrcweir 								   Ptr,
1572cdf0e10cSrcweir 								   BytesToRead,
1573cdf0e10cSrcweir 								   osl_Socket_MsgNormal);
1574cdf0e10cSrcweir 
1575cdf0e10cSrcweir 		/* error occured? */
1576cdf0e10cSrcweir 		if(RetVal <= 0)
1577cdf0e10cSrcweir 		{
1578cdf0e10cSrcweir 			break;
1579cdf0e10cSrcweir 		}
1580cdf0e10cSrcweir 
1581cdf0e10cSrcweir 		BytesToRead -= RetVal;
1582cdf0e10cSrcweir 		BytesRead += RetVal;
1583cdf0e10cSrcweir 		Ptr += RetVal;
1584cdf0e10cSrcweir 	}
1585cdf0e10cSrcweir 
1586cdf0e10cSrcweir 	return BytesRead;
1587cdf0e10cSrcweir }
1588cdf0e10cSrcweir 
1589cdf0e10cSrcweir /*****************************************************************************/
1590cdf0e10cSrcweir /* osl_writeSocket  */
1591cdf0e10cSrcweir /*****************************************************************************/
1592cdf0e10cSrcweir sal_Int32 SAL_CALL osl_writeSocket( oslSocket pSocket, const void *pBuffer, sal_Int32 n )
1593cdf0e10cSrcweir {
1594cdf0e10cSrcweir     OSL_ASSERT( pSocket );
1595cdf0e10cSrcweir 
1596cdf0e10cSrcweir 	/* loop until all desired bytes were send or an error occured */
1597cdf0e10cSrcweir 	sal_uInt32 BytesSend= 0;
1598cdf0e10cSrcweir 	sal_uInt32 BytesToSend= n;
1599cdf0e10cSrcweir 	sal_uInt8 *Ptr = ( sal_uInt8 * )pBuffer;
1600cdf0e10cSrcweir 	while (BytesToSend > 0)
1601cdf0e10cSrcweir 	{
1602cdf0e10cSrcweir 		sal_Int32 RetVal;
1603cdf0e10cSrcweir 
1604cdf0e10cSrcweir 		RetVal= osl_sendSocket( pSocket,Ptr,BytesToSend,osl_Socket_MsgNormal);
1605cdf0e10cSrcweir 
1606cdf0e10cSrcweir 		/* error occured? */
1607cdf0e10cSrcweir 		if(RetVal <= 0)
1608cdf0e10cSrcweir 		{
1609cdf0e10cSrcweir 			break;
1610cdf0e10cSrcweir 		}
1611cdf0e10cSrcweir 
1612cdf0e10cSrcweir 		BytesToSend -= RetVal;
1613cdf0e10cSrcweir 		BytesSend += RetVal;
1614cdf0e10cSrcweir 		Ptr += RetVal;
1615cdf0e10cSrcweir 
1616cdf0e10cSrcweir 	}
1617cdf0e10cSrcweir 	return BytesSend;
1618cdf0e10cSrcweir }
1619cdf0e10cSrcweir 
1620cdf0e10cSrcweir 
1621cdf0e10cSrcweir /*****************************************************************************/
1622cdf0e10cSrcweir /* osl_isReceiveReady  */
1623cdf0e10cSrcweir /*****************************************************************************/
1624cdf0e10cSrcweir sal_Bool SAL_CALL osl_isReceiveReady (
1625cdf0e10cSrcweir 	oslSocket        pSocket,
1626cdf0e10cSrcweir 	const TimeValue* pTimeout)
1627cdf0e10cSrcweir {
1628cdf0e10cSrcweir 	fd_set         fds;
1629cdf0e10cSrcweir 	struct timeval tv;
1630cdf0e10cSrcweir 
1631cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1632cdf0e10cSrcweir 		return sal_False;
1633cdf0e10cSrcweir 
1634cdf0e10cSrcweir 	FD_ZERO(&fds);
1635cdf0e10cSrcweir 	FD_SET(pSocket->m_Socket, &fds);
1636cdf0e10cSrcweir 
1637cdf0e10cSrcweir 	if (pTimeout)
1638cdf0e10cSrcweir 	{
1639cdf0e10cSrcweir 		tv.tv_sec  = pTimeout->Seconds;
1640cdf0e10cSrcweir 		tv.tv_usec = pTimeout->Nanosec / 1000L;
1641cdf0e10cSrcweir 	}
1642cdf0e10cSrcweir 
1643cdf0e10cSrcweir 	return (select(pSocket->m_Socket + 1,		/* no of sockets to monitor */
1644cdf0e10cSrcweir 				   &fds,						/* check read operations */
1645cdf0e10cSrcweir 				   0,							/* check write ops */
1646cdf0e10cSrcweir 				   0,							/* ckeck for OOB */
1647cdf0e10cSrcweir 				   (pTimeout) ? &tv : 0)==1);	/* use timeout? */
1648cdf0e10cSrcweir }
1649cdf0e10cSrcweir 
1650cdf0e10cSrcweir /*****************************************************************************/
1651cdf0e10cSrcweir /* osl_isSendReady  */
1652cdf0e10cSrcweir /*****************************************************************************/
1653cdf0e10cSrcweir sal_Bool SAL_CALL osl_isSendReady (
1654cdf0e10cSrcweir 	oslSocket        pSocket,
1655cdf0e10cSrcweir 	const TimeValue* pTimeout)
1656cdf0e10cSrcweir {
1657cdf0e10cSrcweir 	fd_set         fds;
1658cdf0e10cSrcweir 	struct timeval tv;
1659cdf0e10cSrcweir 
1660cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1661cdf0e10cSrcweir 		return sal_False;
1662cdf0e10cSrcweir 
1663cdf0e10cSrcweir 	FD_ZERO(&fds);
1664cdf0e10cSrcweir 	FD_SET(pSocket->m_Socket, &fds);
1665cdf0e10cSrcweir 
1666cdf0e10cSrcweir 	if (pTimeout)
1667cdf0e10cSrcweir 	{
1668cdf0e10cSrcweir 		tv.tv_sec  = pTimeout->Seconds;
1669cdf0e10cSrcweir 		tv.tv_usec = pTimeout->Nanosec / 1000L;
1670cdf0e10cSrcweir 	}
1671cdf0e10cSrcweir 
1672cdf0e10cSrcweir 	return (select(pSocket->m_Socket + 1,		/* no of sockets to monitor */
1673cdf0e10cSrcweir 				   0,							/* check read operations */
1674cdf0e10cSrcweir 				   &fds,						/* check write ops */
1675cdf0e10cSrcweir 				   0,							/* ckeck for OOB */
1676cdf0e10cSrcweir 				   (pTimeout) ? &tv : 0)==1);	/* use timeout? */
1677cdf0e10cSrcweir }
1678cdf0e10cSrcweir 
1679cdf0e10cSrcweir /*****************************************************************************/
1680cdf0e10cSrcweir /* osl_isExceptionPending  */
1681cdf0e10cSrcweir /*****************************************************************************/
1682cdf0e10cSrcweir sal_Bool SAL_CALL osl_isExceptionPending (
1683cdf0e10cSrcweir 	oslSocket        pSocket,
1684cdf0e10cSrcweir 	const TimeValue* pTimeout)
1685cdf0e10cSrcweir {
1686cdf0e10cSrcweir 	fd_set         fds;
1687cdf0e10cSrcweir 	struct timeval tv;
1688cdf0e10cSrcweir 
1689cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1690cdf0e10cSrcweir 		return sal_False;
1691cdf0e10cSrcweir 
1692cdf0e10cSrcweir 	FD_ZERO(&fds);
1693cdf0e10cSrcweir 	FD_SET(pSocket->m_Socket, &fds);
1694cdf0e10cSrcweir 
1695cdf0e10cSrcweir 	if (pTimeout)
1696cdf0e10cSrcweir 	{
1697cdf0e10cSrcweir 		tv.tv_sec  = pTimeout->Seconds;
1698cdf0e10cSrcweir 		tv.tv_usec = pTimeout->Nanosec / 1000L;
1699cdf0e10cSrcweir 	}
1700cdf0e10cSrcweir 
1701cdf0e10cSrcweir 	return (select(pSocket->m_Socket + 1,	    /* no of sockets to monitor */
1702cdf0e10cSrcweir 				   0,							/* check read operations */
1703cdf0e10cSrcweir 				   0,							/* check write ops */
1704cdf0e10cSrcweir 				   &fds,						/* ckeck for OOB */
1705cdf0e10cSrcweir 				   (pTimeout) ? &tv : 0)==1);	/* use timeout? */
1706cdf0e10cSrcweir }
1707cdf0e10cSrcweir 
1708cdf0e10cSrcweir /*****************************************************************************/
1709cdf0e10cSrcweir /* osl_shutdownSocket  */
1710cdf0e10cSrcweir /*****************************************************************************/
1711cdf0e10cSrcweir sal_Bool SAL_CALL osl_shutdownSocket (
1712cdf0e10cSrcweir 	oslSocket          pSocket,
1713cdf0e10cSrcweir 	oslSocketDirection Direction)
1714cdf0e10cSrcweir {
1715cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1716cdf0e10cSrcweir 		return sal_False;
1717cdf0e10cSrcweir 
1718cdf0e10cSrcweir 	return (shutdown(pSocket->m_Socket, DIRECTION_TO_NATIVE(Direction))==0);
1719cdf0e10cSrcweir }
1720cdf0e10cSrcweir 
1721cdf0e10cSrcweir /*****************************************************************************/
1722cdf0e10cSrcweir /* osl_getSocketOption  */
1723cdf0e10cSrcweir /*****************************************************************************/
1724cdf0e10cSrcweir sal_Int32 SAL_CALL osl_getSocketOption (
1725cdf0e10cSrcweir 	oslSocket            pSocket,
1726cdf0e10cSrcweir 	oslSocketOptionLevel Level,
1727cdf0e10cSrcweir 	oslSocketOption      Option,
1728cdf0e10cSrcweir 	void*                pBuffer,
1729cdf0e10cSrcweir 	sal_uInt32           BufferLen)
1730cdf0e10cSrcweir {
1731cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1732cdf0e10cSrcweir 		return osl_Socket_Error;
1733cdf0e10cSrcweir 
1734cdf0e10cSrcweir 	if (getsockopt(pSocket->m_Socket,
1735cdf0e10cSrcweir 	     		   OPTION_LEVEL_TO_NATIVE(Level),
1736cdf0e10cSrcweir 				   OPTION_TO_NATIVE(Option),
1737cdf0e10cSrcweir 				   (sal_Char*)pBuffer,
1738cdf0e10cSrcweir 				   (int*)&BufferLen) == -1)
1739cdf0e10cSrcweir 	{
1740cdf0e10cSrcweir 		return -1;
1741cdf0e10cSrcweir 	}
1742cdf0e10cSrcweir 
1743cdf0e10cSrcweir 	return (sal_Int32)BufferLen;
1744cdf0e10cSrcweir }
1745cdf0e10cSrcweir 
1746cdf0e10cSrcweir /*****************************************************************************/
1747cdf0e10cSrcweir /* osl_setSocketOption  */
1748cdf0e10cSrcweir /*****************************************************************************/
1749cdf0e10cSrcweir sal_Bool SAL_CALL osl_setSocketOption (
1750cdf0e10cSrcweir 	oslSocket            pSocket,
1751cdf0e10cSrcweir 	oslSocketOptionLevel Level,
1752cdf0e10cSrcweir 	oslSocketOption      Option,
1753cdf0e10cSrcweir 	void*                pBuffer,
1754cdf0e10cSrcweir 	sal_uInt32           BufferLen)
1755cdf0e10cSrcweir {
1756cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1757cdf0e10cSrcweir 		return sal_False;
1758cdf0e10cSrcweir 
1759cdf0e10cSrcweir 	return(setsockopt(pSocket->m_Socket,
1760cdf0e10cSrcweir 					  OPTION_LEVEL_TO_NATIVE(Level),
1761cdf0e10cSrcweir 					  OPTION_TO_NATIVE(Option),
1762cdf0e10cSrcweir 					  (sal_Char*)pBuffer,
1763cdf0e10cSrcweir 					  BufferLen) == 0);
1764cdf0e10cSrcweir }
1765cdf0e10cSrcweir 
1766cdf0e10cSrcweir /*****************************************************************************/
1767cdf0e10cSrcweir /* osl_enableNonBlockingMode  */
1768cdf0e10cSrcweir /*****************************************************************************/
1769cdf0e10cSrcweir sal_Bool SAL_CALL osl_enableNonBlockingMode ( oslSocket pSocket, sal_Bool  On)
1770cdf0e10cSrcweir {
1771cdf0e10cSrcweir 	unsigned long  Param= On ? 1 : 0;
1772cdf0e10cSrcweir 
1773cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1774cdf0e10cSrcweir 		return sal_False;
1775cdf0e10cSrcweir 
1776cdf0e10cSrcweir 	pSocket->m_Flags = Param ?
1777cdf0e10cSrcweir 		(pSocket->m_Flags |  OSL_SOCKET_FLAGS_NONBLOCKING) :
1778cdf0e10cSrcweir 		(pSocket->m_Flags & ~OSL_SOCKET_FLAGS_NONBLOCKING) ;
1779cdf0e10cSrcweir 
1780cdf0e10cSrcweir 	return (
1781cdf0e10cSrcweir 		ioctlsocket(pSocket->m_Socket, FIONBIO, &Param) != OSL_SOCKET_ERROR);
1782cdf0e10cSrcweir }
1783cdf0e10cSrcweir 
1784cdf0e10cSrcweir /*****************************************************************************/
1785cdf0e10cSrcweir /* osl_isNonBlockingMode  */
1786cdf0e10cSrcweir /*****************************************************************************/
1787cdf0e10cSrcweir sal_Bool SAL_CALL osl_isNonBlockingMode(oslSocket pSocket)
1788cdf0e10cSrcweir {
1789cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1790cdf0e10cSrcweir 		return sal_False;
1791cdf0e10cSrcweir 
1792cdf0e10cSrcweir 	return (sal_Bool)((pSocket->m_Flags & OSL_SOCKET_FLAGS_NONBLOCKING) != 0);
1793cdf0e10cSrcweir }
1794cdf0e10cSrcweir 
1795cdf0e10cSrcweir /*****************************************************************************/
1796cdf0e10cSrcweir /* osl_getSocketType  */
1797cdf0e10cSrcweir /*****************************************************************************/
1798cdf0e10cSrcweir oslSocketType SAL_CALL osl_getSocketType(oslSocket pSocket)
1799cdf0e10cSrcweir {
1800cdf0e10cSrcweir 	int            Type=0;
1801cdf0e10cSrcweir 	int            TypeSize= sizeof(Type);
1802cdf0e10cSrcweir 
1803cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1804cdf0e10cSrcweir 		return osl_Socket_TypeInvalid;
1805cdf0e10cSrcweir 
1806cdf0e10cSrcweir 	if(getsockopt(pSocket->m_Socket,
1807cdf0e10cSrcweir 				  OPTION_LEVEL_TO_NATIVE(osl_Socket_LevelSocket),
1808cdf0e10cSrcweir 				  OPTION_TO_NATIVE(osl_Socket_OptionType),
1809cdf0e10cSrcweir 				  (sal_Char *)&Type,
1810cdf0e10cSrcweir 				  &TypeSize) == -1)
1811cdf0e10cSrcweir 	{
1812cdf0e10cSrcweir 		/* error */
1813cdf0e10cSrcweir 		return osl_Socket_TypeInvalid;
1814cdf0e10cSrcweir 	}
1815cdf0e10cSrcweir 
1816cdf0e10cSrcweir 	return TYPE_FROM_NATIVE(Type);
1817cdf0e10cSrcweir }
1818cdf0e10cSrcweir 
1819cdf0e10cSrcweir /*****************************************************************************/
1820cdf0e10cSrcweir /* osl_getLastSocketErrorDescription  */
1821cdf0e10cSrcweir /*****************************************************************************/
1822cdf0e10cSrcweir void SAL_CALL osl_getLastSocketErrorDescription (
1823cdf0e10cSrcweir 	oslSocket  /*Socket*/,
1824cdf0e10cSrcweir 	rtl_uString	**strError)
1825cdf0e10cSrcweir {
1826cdf0e10cSrcweir 	int error;
1827cdf0e10cSrcweir 
1828cdf0e10cSrcweir 	switch(error = WSAGetLastError())
1829cdf0e10cSrcweir 	{
1830cdf0e10cSrcweir 		case WSAENOTSOCK:
1831cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAENOTSOCK, Socket operation on non-socket. A socket created in one process is used by another process.");
1832cdf0e10cSrcweir             break;
1833cdf0e10cSrcweir 
1834cdf0e10cSrcweir         case WSAEDESTADDRREQ:
1835cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEDESTADDRREQ, Destination Addr required");
1836cdf0e10cSrcweir             break;
1837cdf0e10cSrcweir 
1838cdf0e10cSrcweir         case WSAEMSGSIZE:
1839cdf0e10cSrcweir             rtl_uString_newFromAscii (strError, "WSAEMSGSIZE, Message too long");
1840cdf0e10cSrcweir             break;
1841cdf0e10cSrcweir 
1842cdf0e10cSrcweir         case WSAEPROTOTYPE:
1843cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEPROTOTYPE, Protocol wrong type for socket");
1844cdf0e10cSrcweir             break;
1845cdf0e10cSrcweir 
1846cdf0e10cSrcweir         case WSAENOPROTOOPT:
1847cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAENOPROTOOPT, Protocol not available");
1848cdf0e10cSrcweir             break;
1849cdf0e10cSrcweir 
1850cdf0e10cSrcweir         case WSAEPROTONOSUPPORT:
1851cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEPROTONOSUPPORT, Protocol not supported");
1852cdf0e10cSrcweir             break;
1853cdf0e10cSrcweir 
1854cdf0e10cSrcweir         case WSAESOCKTNOSUPPORT:
1855cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAESOCKTNOSUPPORT, Socket type not supported");
1856cdf0e10cSrcweir             break;
1857cdf0e10cSrcweir 
1858cdf0e10cSrcweir         case WSAEOPNOTSUPP:
1859cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEOPNOTSUPP, Operation not supported on socket");
1860cdf0e10cSrcweir             break;
1861cdf0e10cSrcweir 
1862cdf0e10cSrcweir         case WSAEPFNOSUPPORT:
1863cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEPFNOSUPPORT, Protocol family not supported");
1864cdf0e10cSrcweir             break;
1865cdf0e10cSrcweir 
1866cdf0e10cSrcweir         case WSAEAFNOSUPPORT:
1867cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSEAFNOSUPPORT, Addr family not supported by protocol family");
1868cdf0e10cSrcweir             break;
1869cdf0e10cSrcweir 
1870cdf0e10cSrcweir         case WSAEADDRINUSE:
1871cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEADDRINUSE, Triggered by bind() because a process went down without closing a socket.");
1872cdf0e10cSrcweir             break;
1873cdf0e10cSrcweir 
1874cdf0e10cSrcweir         case WSAEADDRNOTAVAIL:
1875cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEADDRNOTAVAIL, Can't assign requested Addr");
1876cdf0e10cSrcweir             break;
1877cdf0e10cSrcweir 
1878cdf0e10cSrcweir         case WSAENETDOWN:
1879cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAENETDOWN, Network is down");
1880cdf0e10cSrcweir             break;
1881cdf0e10cSrcweir 
1882cdf0e10cSrcweir         case WSAENETUNREACH:
1883cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAENETUNREACH, Network is unreachable");
1884cdf0e10cSrcweir             break;
1885cdf0e10cSrcweir 
1886cdf0e10cSrcweir         case WSAENETRESET:
1887cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAENETRESET, Network dropped connection or reset");
1888cdf0e10cSrcweir             break;
1889cdf0e10cSrcweir 
1890cdf0e10cSrcweir         case WSAECONNABORTED:
1891cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAECONNABORTED, Software caused connection abort");
1892cdf0e10cSrcweir             break;
1893cdf0e10cSrcweir 
1894cdf0e10cSrcweir         case WSAECONNRESET:
1895cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAECONNRESET, Connection reset by peer");
1896cdf0e10cSrcweir             break;
1897cdf0e10cSrcweir 
1898cdf0e10cSrcweir         case WSAENOBUFS:
1899cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAENOBUFS, No buffer space available.");
1900cdf0e10cSrcweir             break;
1901cdf0e10cSrcweir 
1902cdf0e10cSrcweir         case WSAEISCONN:
1903cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEISCONN, Socket is already connected");
1904cdf0e10cSrcweir             break;
1905cdf0e10cSrcweir 
1906cdf0e10cSrcweir         case WSAENOTCONN:
1907cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAENOTCONN, Socket is not connected");
1908cdf0e10cSrcweir             break;
1909cdf0e10cSrcweir 
1910cdf0e10cSrcweir         case WSAESHUTDOWN:
1911cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAESHUTDOWN, Can't send after socket shutdown");
1912cdf0e10cSrcweir             break;
1913cdf0e10cSrcweir 
1914cdf0e10cSrcweir         case WSAETIMEDOUT:
1915cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAETIMEDOUT, Connection timed out");
1916cdf0e10cSrcweir             break;
1917cdf0e10cSrcweir 
1918cdf0e10cSrcweir         case WSAECONNREFUSED:
1919cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAECONNREFUSED, Connection refused");
1920cdf0e10cSrcweir             break;
1921cdf0e10cSrcweir 
1922cdf0e10cSrcweir         case WSAEHOSTDOWN:
1923cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEHOSTDOWN, Networking subsystem not started");
1924cdf0e10cSrcweir             break;
1925cdf0e10cSrcweir 
1926cdf0e10cSrcweir         case WSAEHOSTUNREACH:
1927cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEHOSTUNREACH, No route to host");
1928cdf0e10cSrcweir             break;
1929cdf0e10cSrcweir 
1930cdf0e10cSrcweir         case WSAEWOULDBLOCK:
1931cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEWOULDBLOCK, Operation would block");
1932cdf0e10cSrcweir             break;
1933cdf0e10cSrcweir 
1934cdf0e10cSrcweir         case WSAEINPROGRESS:
1935cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEINPROGRESS, Operation now in progress");
1936cdf0e10cSrcweir             break;
1937cdf0e10cSrcweir 
1938cdf0e10cSrcweir         case WSAEALREADY:
1939cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEALREADY, Operation already in progress");
1940cdf0e10cSrcweir             break;
1941cdf0e10cSrcweir 
1942cdf0e10cSrcweir         case WSAEINTR:
1943cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEALREADY, Operation was interrupted");
1944cdf0e10cSrcweir             break;
1945cdf0e10cSrcweir 
1946cdf0e10cSrcweir         case WSAEBADF:
1947cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEBADF, Bad file number");
1948cdf0e10cSrcweir             break;
1949cdf0e10cSrcweir 
1950cdf0e10cSrcweir         case WSAEACCES:
1951cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEACCES, Access is denied");
1952cdf0e10cSrcweir             break;
1953cdf0e10cSrcweir 
1954cdf0e10cSrcweir         case WSAEFAULT:
1955cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEFAULT, Bad memory Addr");
1956cdf0e10cSrcweir             break;
1957cdf0e10cSrcweir 
1958cdf0e10cSrcweir         case WSAEINVAL:
1959cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEINVAL, The socket has not been bound with bind() or is already connected");
1960cdf0e10cSrcweir             break;
1961cdf0e10cSrcweir 
1962cdf0e10cSrcweir         case WSAEMFILE:
1963cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEMFILE, No more file descriptors are available");
1964cdf0e10cSrcweir             break;
1965cdf0e10cSrcweir 
1966cdf0e10cSrcweir         case WSAETOOMANYREFS:
1967cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAETOOMANYREFS, Undocumented WinSock error");
1968cdf0e10cSrcweir             break;
1969cdf0e10cSrcweir 
1970cdf0e10cSrcweir         case WSAENAMETOOLONG:
1971cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAENAMETOOLONG, Undocumented WinSock error");
1972cdf0e10cSrcweir             break;
1973cdf0e10cSrcweir 
1974cdf0e10cSrcweir         case WSAENOTEMPTY:
1975cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAENOTEMPTY, Undocumented WinSock error");
1976cdf0e10cSrcweir             break;
1977cdf0e10cSrcweir 
1978cdf0e10cSrcweir         case WSAEPROCLIM:
1979cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEPROCLIM, Undocumented WinSock error");
1980cdf0e10cSrcweir             break;
1981cdf0e10cSrcweir 
1982cdf0e10cSrcweir         case WSAEUSERS:
1983cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEUSERS, Undocumented WinSock error");
1984cdf0e10cSrcweir             break;
1985cdf0e10cSrcweir 
1986cdf0e10cSrcweir         case WSAEDQUOT:
1987cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEDQUOT, Undocumented WinSock error");
1988cdf0e10cSrcweir             break;
1989cdf0e10cSrcweir 
1990cdf0e10cSrcweir         case WSAESTALE:
1991cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAESTALE, Undocumented WinSock error");
1992cdf0e10cSrcweir             break;
1993cdf0e10cSrcweir 
1994cdf0e10cSrcweir         case WSAEREMOTE:
1995cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEREMOTE, Undocumented WinSock error");
1996cdf0e10cSrcweir             break;
1997cdf0e10cSrcweir 
1998cdf0e10cSrcweir         case WSAEDISCON:
1999cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAEDISCON, Circuit was gracefully terminated");
2000cdf0e10cSrcweir             break;
2001cdf0e10cSrcweir 
2002cdf0e10cSrcweir         case WSASYSNOTREADY:
2003cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSASYSNOTREADY, The underlying network subsystem is not ready for network communication");
2004cdf0e10cSrcweir             break;
2005cdf0e10cSrcweir 
2006cdf0e10cSrcweir         case WSAVERNOTSUPPORTED:
2007cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAVERNOTSUPPORTED, The version of Windows Sockets API support requested is not provided by this particular Windows Sockets implementation");
2008cdf0e10cSrcweir             break;
2009cdf0e10cSrcweir 
2010cdf0e10cSrcweir         case WSANOTINITIALISED:
2011cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSANOTINITIALISED, WSAStartup() has not been called");
2012cdf0e10cSrcweir             break;
2013cdf0e10cSrcweir 
2014cdf0e10cSrcweir         case WSAHOST_NOT_FOUND:
2015cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSAHOST_NOT_FOUND, Authoritative answer host not found");
2016cdf0e10cSrcweir             break;
2017cdf0e10cSrcweir 
2018cdf0e10cSrcweir         case WSATRY_AGAIN:
2019cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSATRY_AGAIN, Non-authoritative answer host not found or SERVERFAIL");
2020cdf0e10cSrcweir             break;
2021cdf0e10cSrcweir 
2022cdf0e10cSrcweir         case WSANO_RECOVERY:
2023cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSANO_RECOVERY, Non recoverable errors, FORMERR, REFUSED, NOTIMP");
2024cdf0e10cSrcweir             break;
2025cdf0e10cSrcweir 
2026cdf0e10cSrcweir         case WSANO_DATA:
2027cdf0e10cSrcweir 			rtl_uString_newFromAscii (strError, "WSANO_DATA or WSANO_ADDRESS, Valid name, no data record of requested type");
2028cdf0e10cSrcweir             break;
2029cdf0e10cSrcweir 
2030cdf0e10cSrcweir 		default:
2031cdf0e10cSrcweir 		{
2032cdf0e10cSrcweir 			sal_Unicode message[128];
2033cdf0e10cSrcweir 
2034cdf0e10cSrcweir             wsprintfW(reinterpret_cast<LPWSTR>(message), L"Unknown WinSock Error Number %d", error);
2035cdf0e10cSrcweir 			rtl_uString_newFromStr (strError, message);
2036cdf0e10cSrcweir         }
2037cdf0e10cSrcweir 
2038cdf0e10cSrcweir         return;
2039cdf0e10cSrcweir 
2040cdf0e10cSrcweir 	}
2041cdf0e10cSrcweir }
2042cdf0e10cSrcweir 
2043cdf0e10cSrcweir /*****************************************************************************/
2044cdf0e10cSrcweir /* osl_getLastSocketError  */
2045cdf0e10cSrcweir /*****************************************************************************/
2046cdf0e10cSrcweir oslSocketError SAL_CALL osl_getLastSocketError(oslSocket /*Socket*/)
2047cdf0e10cSrcweir {
2048cdf0e10cSrcweir 	return ERROR_FROM_NATIVE(WSAGetLastError());
2049cdf0e10cSrcweir }
2050cdf0e10cSrcweir 
2051cdf0e10cSrcweir /*****************************************************************************/
2052cdf0e10cSrcweir /* SocketSet                                                                 */
2053cdf0e10cSrcweir /*****************************************************************************/
2054cdf0e10cSrcweir typedef struct _TSocketSetImpl
2055cdf0e10cSrcweir {
2056cdf0e10cSrcweir 	fd_set	m_Set;			/* the set of descriptors */
2057cdf0e10cSrcweir 
2058cdf0e10cSrcweir } TSocketSetImpl;
2059cdf0e10cSrcweir 
2060cdf0e10cSrcweir /*****************************************************************************/
2061cdf0e10cSrcweir /* osl_createSocketSet  */
2062cdf0e10cSrcweir /*****************************************************************************/
2063cdf0e10cSrcweir oslSocketSet SAL_CALL osl_createSocketSet()
2064cdf0e10cSrcweir {
2065cdf0e10cSrcweir 	TSocketSetImpl* pSet;
2066cdf0e10cSrcweir 
2067cdf0e10cSrcweir 	pSet = (TSocketSetImpl*) rtl_allocateMemory(sizeof(TSocketSetImpl));
2068cdf0e10cSrcweir 
2069cdf0e10cSrcweir 	if(pSet)
2070cdf0e10cSrcweir 	{
2071cdf0e10cSrcweir 		FD_ZERO(&pSet->m_Set);
2072cdf0e10cSrcweir 	}
2073cdf0e10cSrcweir 
2074cdf0e10cSrcweir 	return (oslSocketSet)pSet;
2075cdf0e10cSrcweir }
2076cdf0e10cSrcweir 
2077cdf0e10cSrcweir /*****************************************************************************/
2078cdf0e10cSrcweir /* osl_destroySocketSet  */
2079cdf0e10cSrcweir /*****************************************************************************/
2080cdf0e10cSrcweir void SAL_CALL osl_destroySocketSet (oslSocketSet Set)
2081cdf0e10cSrcweir {
2082cdf0e10cSrcweir 	if(Set)
2083cdf0e10cSrcweir 		rtl_freeMemory(Set);
2084cdf0e10cSrcweir }
2085cdf0e10cSrcweir 
2086cdf0e10cSrcweir /*****************************************************************************/
2087cdf0e10cSrcweir /* osl_clearSocketSet  */
2088cdf0e10cSrcweir /*****************************************************************************/
2089cdf0e10cSrcweir void SAL_CALL osl_clearSocketSet (oslSocketSet Set)
2090cdf0e10cSrcweir {
2091cdf0e10cSrcweir 	TSocketSetImpl* pSet;
2092cdf0e10cSrcweir 
2093cdf0e10cSrcweir 	pSet= (TSocketSetImpl*)Set;
2094cdf0e10cSrcweir 
2095cdf0e10cSrcweir 	if (pSet)
2096cdf0e10cSrcweir 		FD_ZERO(&pSet->m_Set);
2097cdf0e10cSrcweir }
2098cdf0e10cSrcweir 
2099cdf0e10cSrcweir /*****************************************************************************/
2100cdf0e10cSrcweir /* osl_addToSocketSet  */
2101cdf0e10cSrcweir /*****************************************************************************/
2102cdf0e10cSrcweir void SAL_CALL osl_addToSocketSet (
2103cdf0e10cSrcweir 	oslSocketSet Set,
2104cdf0e10cSrcweir 	oslSocket    Socket)
2105cdf0e10cSrcweir {
2106cdf0e10cSrcweir 	TSocketSetImpl* pSet;
2107cdf0e10cSrcweir 	oslSocketImpl*  pSockImpl;
2108cdf0e10cSrcweir 
2109cdf0e10cSrcweir 	pSet= (TSocketSetImpl*)Set;
2110cdf0e10cSrcweir 	pSockImpl= (oslSocketImpl*)Socket;
2111cdf0e10cSrcweir 
2112cdf0e10cSrcweir 	if (pSet && pSockImpl)
2113cdf0e10cSrcweir 		FD_SET(pSockImpl->m_Socket, &pSet->m_Set);
2114cdf0e10cSrcweir }
2115cdf0e10cSrcweir 
2116cdf0e10cSrcweir /*****************************************************************************/
2117cdf0e10cSrcweir /* osl_removeFromSocketSet  */
2118cdf0e10cSrcweir /*****************************************************************************/
2119cdf0e10cSrcweir void SAL_CALL osl_removeFromSocketSet (
2120cdf0e10cSrcweir 	oslSocketSet Set,
2121cdf0e10cSrcweir 	oslSocket    Socket)
2122cdf0e10cSrcweir {
2123cdf0e10cSrcweir 	TSocketSetImpl* pSet;
2124cdf0e10cSrcweir 	oslSocketImpl*  pSockImpl;
2125cdf0e10cSrcweir 
2126cdf0e10cSrcweir 	pSet= (TSocketSetImpl*)Set;
2127cdf0e10cSrcweir 	pSockImpl= (oslSocketImpl*)Socket;
2128cdf0e10cSrcweir 
2129cdf0e10cSrcweir 	if (pSet && pSockImpl)
2130cdf0e10cSrcweir 		FD_CLR(pSockImpl->m_Socket, &pSet->m_Set);
2131cdf0e10cSrcweir }
2132cdf0e10cSrcweir 
2133cdf0e10cSrcweir /*****************************************************************************/
2134cdf0e10cSrcweir /* osl_isInSocketSet  */
2135cdf0e10cSrcweir /*****************************************************************************/
2136cdf0e10cSrcweir sal_Bool SAL_CALL osl_isInSocketSet (
2137cdf0e10cSrcweir 	oslSocketSet Set,
2138cdf0e10cSrcweir 	oslSocket    Socket)
2139cdf0e10cSrcweir {
2140cdf0e10cSrcweir 	TSocketSetImpl* pSet;
2141cdf0e10cSrcweir 	oslSocketImpl*  pSockImpl;
2142cdf0e10cSrcweir 
2143cdf0e10cSrcweir 	pSet= (TSocketSetImpl*)Set;
2144cdf0e10cSrcweir 	pSockImpl= (oslSocketImpl*)Socket;
2145cdf0e10cSrcweir 
2146cdf0e10cSrcweir 	if (pSet && pSockImpl)
2147cdf0e10cSrcweir 		return (FD_ISSET(pSockImpl->m_Socket, &pSet->m_Set) != 0);
2148cdf0e10cSrcweir 	else
2149cdf0e10cSrcweir 		return sal_False;
2150cdf0e10cSrcweir }
2151cdf0e10cSrcweir 
2152cdf0e10cSrcweir /*****************************************************************************/
2153cdf0e10cSrcweir /* osl_demultiplexSocketEvents  */
2154cdf0e10cSrcweir /*****************************************************************************/
2155cdf0e10cSrcweir sal_Int32 SAL_CALL osl_demultiplexSocketEvents (
2156cdf0e10cSrcweir 	oslSocketSet IncomingSet,
2157cdf0e10cSrcweir 	oslSocketSet OutgoingSet,
2158cdf0e10cSrcweir 	oslSocketSet OutOfBandSet,
2159cdf0e10cSrcweir 	const TimeValue* pTimeout)
2160cdf0e10cSrcweir {
2161cdf0e10cSrcweir 	int             MaxHandle= 0;
2162cdf0e10cSrcweir 	struct timeval  tv;
2163cdf0e10cSrcweir 	TSocketSetImpl* pInSet;
2164cdf0e10cSrcweir 	TSocketSetImpl* pOutSet;
2165cdf0e10cSrcweir 	TSocketSetImpl* pOOBSet;
2166cdf0e10cSrcweir 
2167cdf0e10cSrcweir 	if(pTimeout)
2168cdf0e10cSrcweir 	{
2169cdf0e10cSrcweir 		/* divide milliseconds into seconds and microseconds */
2170cdf0e10cSrcweir 		tv.tv_sec  = pTimeout->Seconds;
2171cdf0e10cSrcweir 		tv.tv_usec = pTimeout->Nanosec / 1000L;
2172cdf0e10cSrcweir 	}
2173cdf0e10cSrcweir 
2174cdf0e10cSrcweir 	/* map opaque data to impl-types */
2175cdf0e10cSrcweir 	pInSet= (TSocketSetImpl*)IncomingSet;
2176cdf0e10cSrcweir 	pOutSet= (TSocketSetImpl*)OutgoingSet;
2177cdf0e10cSrcweir 	pOOBSet= (TSocketSetImpl*)OutOfBandSet;
2178cdf0e10cSrcweir 
2179cdf0e10cSrcweir 	return select(MaxHandle,				/* redundant in WIN32 */
2180cdf0e10cSrcweir 				  pInSet ? &pInSet->m_Set : 0,
2181cdf0e10cSrcweir 				  pOutSet ? &pOutSet->m_Set : 0,
2182cdf0e10cSrcweir 				  pOOBSet ? &pOOBSet->m_Set : 0,
2183cdf0e10cSrcweir 				  pTimeout ? &tv : 0);
2184cdf0e10cSrcweir }
2185cdf0e10cSrcweir 
2186cdf0e10cSrcweir }
2187