xref: /AOO41X/main/sal/osl/os2/socket.c (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "system.h"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <osl/socket.h>
31*cdf0e10cSrcweir #include <osl/diagnose.h>
32*cdf0e10cSrcweir #include <osl/mutex.h>
33*cdf0e10cSrcweir #include <osl/signal.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <rtl/alloc.h>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <ctype.h>
38*cdf0e10cSrcweir #include <sal/types.h>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include "sockimpl.h"
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir /* defines for poll */
44*cdf0e10cSrcweir #ifdef HAVE_POLL_H
45*cdf0e10cSrcweir #undef HAVE_POLL_H
46*cdf0e10cSrcweir #endif
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #if defined(LINUX) || defined(NETBSD) || defined ( FREEBSD ) || defined (MACOSX)
49*cdf0e10cSrcweir #include <sys/poll.h>
50*cdf0e10cSrcweir #define HAVE_POLL_H
51*cdf0e10cSrcweir #endif /* HAVE_POLL_H */
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir #if defined(SOLARIS)
54*cdf0e10cSrcweir #include <poll.h>
55*cdf0e10cSrcweir #define HAVE_POLL_H
56*cdf0e10cSrcweir #endif /* SOLARIS */
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir #ifndef HAVE_POLL_H
59*cdf0e10cSrcweir #define POLLIN  0x0001
60*cdf0e10cSrcweir #define POLLOUT 0x0002
61*cdf0e10cSrcweir #define POLLPRI 0x0004
62*cdf0e10cSrcweir #endif /* HAVE_POLL_H */
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir /* defines for shutdown */
66*cdf0e10cSrcweir #define SD_RECEIVE 0
67*cdf0e10cSrcweir #define SD_SEND 1
68*cdf0e10cSrcweir #define SD_BOTH 2
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir /*
72*cdf0e10cSrcweir 	oslSocketAddr is a pointer to a Berkeley struct sockaddr.
73*cdf0e10cSrcweir 	I refrained from using sockaddr_in because of possible further
74*cdf0e10cSrcweir 	extensions of this socket-interface (IP-NG?).
75*cdf0e10cSrcweir 	The intention was to hide all Berkeley data-structures from
76*cdf0e10cSrcweir 	direct access past the osl-interface.
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 	The current implementation is internet (IP) centered. All
79*cdf0e10cSrcweir 	the constructor-functions (osl_create...) take parameters
80*cdf0e10cSrcweir 	that will probably make sense only in the IP-environment
81*cdf0e10cSrcweir 	(e.g. because of using the dotted-address-format).
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 	If the interface will be extended to host other protocol-
84*cdf0e10cSrcweir 	families, I expect no externally visible changes in the
85*cdf0e10cSrcweir 	existing functions. You'll probably need only new
86*cdf0e10cSrcweir 	constructor-functions who take the different address
87*cdf0e10cSrcweir 	formats into consideration (maybe a long dotted address
88*cdf0e10cSrcweir 	or whatever).
89*cdf0e10cSrcweir */
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir /* _Note_ that I rely on the fact that oslSocketAddr and struct sockaddr */
92*cdf0e10cSrcweir /* are the same! I don't like it very much but see no other easy way to  */
93*cdf0e10cSrcweir /* conceal the struct sockaddr from the eyes of the user. */
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir #define OSL_INVALID_SOCKET		-1
97*cdf0e10cSrcweir #define OSL_SOCKET_ERROR		-1
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir /* Buffer size for gethostbyname */
101*cdf0e10cSrcweir #define MAX_HOSTBUFFER_SIZE 2048
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir /*****************************************************************************/
104*cdf0e10cSrcweir /* enum oslAddrFamily */
105*cdf0e10cSrcweir /*****************************************************************************/
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir /* map */
108*cdf0e10cSrcweir static unsigned long FamilyMap[]= {
109*cdf0e10cSrcweir 	AF_INET,					/* osl_Socket_FamilyInet    */
110*cdf0e10cSrcweir 	AF_IPX,						/* osl_Socket_FamilyIpx     */
111*cdf0e10cSrcweir 	0							/* osl_Socket_FamilyInvalid */
112*cdf0e10cSrcweir };
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir /* reverse map */
115*cdf0e10cSrcweir static oslAddrFamily osl_AddrFamilyFromNative(sal_uInt32 nativeType)
116*cdf0e10cSrcweir {
117*cdf0e10cSrcweir 	oslAddrFamily i= (oslAddrFamily)0;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 	while(i != osl_Socket_FamilyInvalid)
120*cdf0e10cSrcweir 	{
121*cdf0e10cSrcweir 		if(FamilyMap[i] == nativeType)
122*cdf0e10cSrcweir 			return i;
123*cdf0e10cSrcweir 		i = (oslAddrFamily) ( i + 1 );
124*cdf0e10cSrcweir 	}
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	return i;
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir /* macros */
130*cdf0e10cSrcweir #define FAMILY_FROM_NATIVE(y) osl_AddrFamilyFromNative(y)
131*cdf0e10cSrcweir #define FAMILY_TO_NATIVE(x)	(short)FamilyMap[x]
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir /*****************************************************************************/
134*cdf0e10cSrcweir /* enum oslProtocol */
135*cdf0e10cSrcweir /*****************************************************************************/
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir /* map */
138*cdf0e10cSrcweir static sal_uInt32 ProtocolMap[]= {
139*cdf0e10cSrcweir 	0,							/* osl_Socket_ProtocolIp	  */
140*cdf0e10cSrcweir 	NSPROTO_IPX,				/* osl_Socket_ProtocolIpx	  */
141*cdf0e10cSrcweir 	NSPROTO_SPX,				/* osl_Socket_ProtocolSpx	  */
142*cdf0e10cSrcweir 	NSPROTO_SPXII,				/* osl_Socket_ProtocolSpxII   */
143*cdf0e10cSrcweir 	0							/* osl_Socket_ProtocolInvalid */
144*cdf0e10cSrcweir };
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir /* reverse map */
147*cdf0e10cSrcweir /* mfe: NOT USED
148*cdf0e10cSrcweir static oslProtocol osl_ProtocolFromNative(sal_uInt32 nativeType)
149*cdf0e10cSrcweir {
150*cdf0e10cSrcweir 	oslProtocol i= (oslProtocol)0;
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	while(i != osl_Socket_ProtocolInvalid)
153*cdf0e10cSrcweir 	{
154*cdf0e10cSrcweir 		if(ProtocolMap[i] == nativeType)
155*cdf0e10cSrcweir 			return i;
156*cdf0e10cSrcweir 		i = (oslProtocol) ( i + 1);
157*cdf0e10cSrcweir 	}
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 	return i;
160*cdf0e10cSrcweir }
161*cdf0e10cSrcweir */
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir /* macros */
164*cdf0e10cSrcweir #define PROTOCOL_FROM_NATIVE(y) osl_ProtocolFromNative(y)
165*cdf0e10cSrcweir #define PROTOCOL_TO_NATIVE(x)	ProtocolMap[x]
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir /*****************************************************************************/
169*cdf0e10cSrcweir /* enum oslSocketType */
170*cdf0e10cSrcweir /*****************************************************************************/
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir /* map */
173*cdf0e10cSrcweir static sal_uInt32 TypeMap[]= {
174*cdf0e10cSrcweir 	SOCK_STREAM,				/* osl_Socket_TypeStream    */
175*cdf0e10cSrcweir 	SOCK_DGRAM,					/* osl_Socket_TypeDgram     */
176*cdf0e10cSrcweir 	SOCK_RAW,					/* osl_Socket_TypeRaw       */
177*cdf0e10cSrcweir 	SOCK_RDM,					/* osl_Socket_TypeRdm       */
178*cdf0e10cSrcweir 	SOCK_SEQPACKET,				/* osl_Socket_TypeSeqPacket */
179*cdf0e10cSrcweir 	0							/* osl_Socket_TypeInvalid   */
180*cdf0e10cSrcweir };
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir /* reverse map */
183*cdf0e10cSrcweir static oslSocketType osl_SocketTypeFromNative(sal_uInt32 nativeType)
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir 	oslSocketType i= (oslSocketType)0;
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 	while(i != osl_Socket_TypeInvalid)
188*cdf0e10cSrcweir 	{
189*cdf0e10cSrcweir 		if(TypeMap[i] == nativeType)
190*cdf0e10cSrcweir 			return i;
191*cdf0e10cSrcweir 		i = (oslSocketType)(i + 1);
192*cdf0e10cSrcweir 	}
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 	return i;
195*cdf0e10cSrcweir }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir /* macros */
198*cdf0e10cSrcweir #define TYPE_TO_NATIVE(x)		TypeMap[x]
199*cdf0e10cSrcweir #define TYPE_FROM_NATIVE(y)		osl_SocketTypeFromNative(y)
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir /*****************************************************************************/
203*cdf0e10cSrcweir /* enum oslSocketOption */
204*cdf0e10cSrcweir /*****************************************************************************/
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir /* map */
207*cdf0e10cSrcweir static sal_uInt32 OptionMap[]= {
208*cdf0e10cSrcweir 	SO_DEBUG,					/* osl_Socket_OptionDebug 		*/
209*cdf0e10cSrcweir 	SO_ACCEPTCONN,				/* osl_Socket_OptionAcceptConn  */
210*cdf0e10cSrcweir 	SO_REUSEADDR,				/* osl_Socket_OptionReuseAddr   */
211*cdf0e10cSrcweir 	SO_KEEPALIVE,				/* osl_Socket_OptionKeepAlive   */
212*cdf0e10cSrcweir 	SO_DONTROUTE,				/* osl_Socket_OptionDontRoute   */
213*cdf0e10cSrcweir 	SO_BROADCAST,				/* osl_Socket_OptionBroadcast   */
214*cdf0e10cSrcweir 	SO_USELOOPBACK,				/* osl_Socket_OptionUseLoopback */
215*cdf0e10cSrcweir 	SO_LINGER,					/* osl_Socket_OptionLinger		*/
216*cdf0e10cSrcweir 	SO_OOBINLINE,				/* osl_Socket_OptionOOBinLine   */
217*cdf0e10cSrcweir 	SO_SNDBUF,					/* osl_Socket_OptionSndBuf      */
218*cdf0e10cSrcweir 	SO_RCVBUF,					/* osl_Socket_OptionRcvBuf      */
219*cdf0e10cSrcweir 	SO_SNDLOWAT,				/* osl_Socket_OptionSndLowat    */
220*cdf0e10cSrcweir 	SO_RCVLOWAT,				/* osl_Socket_OptionRcvLowat    */
221*cdf0e10cSrcweir 	SO_SNDTIMEO,				/* osl_Socket_OptionSndTimeo    */
222*cdf0e10cSrcweir 	SO_RCVTIMEO,				/* osl_Socket_OptionRcvTimeo    */
223*cdf0e10cSrcweir 	SO_ERROR,					/* osl_Socket_OptionError       */
224*cdf0e10cSrcweir 	SO_TYPE,					/* osl_Socket_OptionType        */
225*cdf0e10cSrcweir 	TCP_NODELAY,				/* osl_Socket_OptionTcpNoDelay  */
226*cdf0e10cSrcweir 	0							/* osl_Socket_OptionInvalid     */
227*cdf0e10cSrcweir };
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir /* reverse map */
230*cdf0e10cSrcweir /* mfe: NOT USED
231*cdf0e10cSrcweir static oslSocketOption osl_SocketOptionFromNative(sal_uInt32 nativeType)
232*cdf0e10cSrcweir {
233*cdf0e10cSrcweir 	oslSocketOption i= (oslSocketOption)0;
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 	while(i != osl_Socket_OptionInvalid)
236*cdf0e10cSrcweir 	{
237*cdf0e10cSrcweir 		if(OptionMap[i] == nativeType)
238*cdf0e10cSrcweir 			return i;
239*cdf0e10cSrcweir 		i = (oslSocketOption) ( i + 1 );
240*cdf0e10cSrcweir 	}
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir 	return i;
243*cdf0e10cSrcweir }
244*cdf0e10cSrcweir */
245*cdf0e10cSrcweir /* macros */
246*cdf0e10cSrcweir #define OPTION_TO_NATIVE(x)		OptionMap[x]
247*cdf0e10cSrcweir #define OPTION_FROM_NATIVE(y)		osl_SocketOptionFromNative(y)
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir /*****************************************************************************/
251*cdf0e10cSrcweir /* enum oslSocketOptionLevel */
252*cdf0e10cSrcweir /*****************************************************************************/
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir static sal_uInt32 OptionLevelMap[]= {
255*cdf0e10cSrcweir 	SOL_SOCKET,					/* osl_Socket_LevelSocket  */
256*cdf0e10cSrcweir 	IPPROTO_TCP,				/* osl_Socket_LevelTcp     */
257*cdf0e10cSrcweir 	0							/* osl_Socket_LevelInvalid */
258*cdf0e10cSrcweir };
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir /* reverse map */
261*cdf0e10cSrcweir /* mfe: NOT USED
262*cdf0e10cSrcweir static oslSocketOptionLevel osl_SocketOptionLevelFromNative(sal_uInt32 nativeType)
263*cdf0e10cSrcweir {
264*cdf0e10cSrcweir 	oslSocketOptionLevel i= (oslSocketOptionLevel)0;
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir 	while(i != osl_Socket_LevelInvalid)
267*cdf0e10cSrcweir 	{
268*cdf0e10cSrcweir 		if(OptionLevelMap[i] == nativeType)
269*cdf0e10cSrcweir 			return i;
270*cdf0e10cSrcweir 		i = (oslSocketOptionLevel) ( i + 1 );
271*cdf0e10cSrcweir 	}
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir 	return i;
274*cdf0e10cSrcweir }
275*cdf0e10cSrcweir */
276*cdf0e10cSrcweir /* macros */
277*cdf0e10cSrcweir #define OPTION_LEVEL_TO_NATIVE(x)		OptionLevelMap[x]
278*cdf0e10cSrcweir #define OPTION_LEVEL_FROM_NATIVE(y)		osl_SocketOptionLevelFromNative(y)
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir /*****************************************************************************/
281*cdf0e10cSrcweir /* enum oslSocketMsgFlag */
282*cdf0e10cSrcweir /*****************************************************************************/
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir static sal_uInt32 SocketMsgFlagMap[]= {
285*cdf0e10cSrcweir 	0,							/* osl_Socket_MsgNormal    */
286*cdf0e10cSrcweir 	MSG_OOB,					/* osl_Socket_MsgOOB       */
287*cdf0e10cSrcweir 	MSG_PEEK,					/* osl_Socket_MsgPeek      */
288*cdf0e10cSrcweir 	MSG_DONTROUTE,				/* osl_Socket_MsgDontRoute */
289*cdf0e10cSrcweir 	MSG_MAXIOVLEN,				/* osl_Socket_MsgMaxIOVLen */
290*cdf0e10cSrcweir 	0							/* osl_Socket_MsgInvalid   */
291*cdf0e10cSrcweir };
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir /* reverse map */
294*cdf0e10cSrcweir /* mfe: NOT USED
295*cdf0e10cSrcweir static oslSocketMsgFlag osl_SocketMsgFlagFromNative(sal_uInt32 nativeType)
296*cdf0e10cSrcweir {
297*cdf0e10cSrcweir 	oslSocketMsgFlag i= (oslSocketMsgFlag)0;
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir 	while(i != osl_Socket_MsgInvalid)
300*cdf0e10cSrcweir 	{
301*cdf0e10cSrcweir 		if(SocketMsgFlagMap[i] == nativeType)
302*cdf0e10cSrcweir 			return i;
303*cdf0e10cSrcweir 		i = (oslSocketMsgFlag) ( i + 1 );
304*cdf0e10cSrcweir 	}
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir 	return i;
307*cdf0e10cSrcweir }
308*cdf0e10cSrcweir */
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir /* macros */
311*cdf0e10cSrcweir #define MSG_FLAG_TO_NATIVE(x)		SocketMsgFlagMap[x]
312*cdf0e10cSrcweir #define MSG_FLAG_FROM_NATIVE(y)		osl_SocketMsgFlagFromNative(y)
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir /*****************************************************************************/
316*cdf0e10cSrcweir /* enum oslSocketDirection */
317*cdf0e10cSrcweir /*****************************************************************************/
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir static sal_uInt32 SocketDirection[]= {
320*cdf0e10cSrcweir 	SD_RECEIVE,					/* osl_Socket_DirRead      */
321*cdf0e10cSrcweir 	SD_SEND,					/* osl_Socket_DirWrite     */
322*cdf0e10cSrcweir 	SD_BOTH,					/* osl_Socket_DirReadWrite */
323*cdf0e10cSrcweir 	0							/* osl_Socket_DirInvalid   */
324*cdf0e10cSrcweir };
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir /* reverse map */
327*cdf0e10cSrcweir /* mfe: NOT USED
328*cdf0e10cSrcweir static oslSocketDirection osl_SocketDirectionFromNative(sal_uInt32 nativeType)
329*cdf0e10cSrcweir {
330*cdf0e10cSrcweir 	oslSocketDirection i= (oslSocketDirection)0;
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir 	while(i != osl_Socket_DirInvalid)
333*cdf0e10cSrcweir 	{
334*cdf0e10cSrcweir 		if(SocketDirection[i] == nativeType)
335*cdf0e10cSrcweir 			return i;
336*cdf0e10cSrcweir 		i = (oslSocketDirection) ( i + 1 );
337*cdf0e10cSrcweir 	}
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir 	return i;
340*cdf0e10cSrcweir }
341*cdf0e10cSrcweir */
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir /* macros */
344*cdf0e10cSrcweir #define DIRECTION_TO_NATIVE(x)		SocketDirection[x]
345*cdf0e10cSrcweir #define DIRECTION_FROM_NATIVE(y)	osl_SocketDirectionFromNative(y)
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir /*****************************************************************************/
348*cdf0e10cSrcweir /* enum oslSocketError */
349*cdf0e10cSrcweir /*****************************************************************************/
350*cdf0e10cSrcweir 
351*cdf0e10cSrcweir static struct
352*cdf0e10cSrcweir {
353*cdf0e10cSrcweir 	int            errcode;
354*cdf0e10cSrcweir 	oslSocketError error;
355*cdf0e10cSrcweir } SocketError[]= {
356*cdf0e10cSrcweir 	{ 0,			   osl_Socket_E_None 			  }, /* no error */
357*cdf0e10cSrcweir 	{ ENOTSOCK,		   osl_Socket_E_NotSocket 		  }, /* Socket operation on non-socket */
358*cdf0e10cSrcweir 	{ EDESTADDRREQ,	   osl_Socket_E_DestAddrReq 	  }, /* Destination address required */
359*cdf0e10cSrcweir 	{ EMSGSIZE,		   osl_Socket_E_MsgSize 		  }, /* Message too long */
360*cdf0e10cSrcweir 	{ EPROTOTYPE,	   osl_Socket_E_Prototype 		  }, /* Protocol wrong type for socket */
361*cdf0e10cSrcweir 	{ ENOPROTOOPT,	   osl_Socket_E_NoProtocol 		  }, /* Protocol not available */
362*cdf0e10cSrcweir 	{ EPROTONOSUPPORT, osl_Socket_E_ProtocolNoSupport }, /* Protocol not supported */
363*cdf0e10cSrcweir 	{ ESOCKTNOSUPPORT, osl_Socket_E_TypeNoSupport 	  }, /* Socket type not supported */
364*cdf0e10cSrcweir 	{ EOPNOTSUPP,	   osl_Socket_E_OpNotSupport 	  }, /* Operation not supported on socket */
365*cdf0e10cSrcweir 	{ EPFNOSUPPORT,	   osl_Socket_E_PfNoSupport 	  }, /* Protocol family not supported */
366*cdf0e10cSrcweir 	{ EAFNOSUPPORT,	   osl_Socket_E_AfNoSupport 	  }, /* Address family not supported by */
367*cdf0e10cSrcweir 					   									 /* protocol family */
368*cdf0e10cSrcweir 	{ EADDRINUSE,	   osl_Socket_E_AddrInUse 		  }, /* Address already in use */
369*cdf0e10cSrcweir 	{ EADDRNOTAVAIL,   osl_Socket_E_AddrNotAvail 	  }, /* Can't assign requested address */
370*cdf0e10cSrcweir 	{ ENETDOWN,		   osl_Socket_E_NetDown 		  }, /* Network is down */
371*cdf0e10cSrcweir 	{ ENETUNREACH,	   osl_Socket_E_NetUnreachable    }, /* Network is unreachable */
372*cdf0e10cSrcweir 	{ ENETRESET,	   osl_Socket_E_NetReset 		  }, /* Network dropped connection because */
373*cdf0e10cSrcweir 					   								 	 /* of reset */
374*cdf0e10cSrcweir 	{ ECONNABORTED,	   osl_Socket_E_ConnAborted 	  }, /* Software caused connection abort */
375*cdf0e10cSrcweir 	{ ECONNRESET,	   osl_Socket_E_ConnReset 		  }, /* Connection reset by peer */
376*cdf0e10cSrcweir 	{ ENOBUFS,		   osl_Socket_E_NoBufferSpace 	  }, /* No buffer space available */
377*cdf0e10cSrcweir 	{ EISCONN,		   osl_Socket_E_IsConnected 	  }, /* Socket is already connected */
378*cdf0e10cSrcweir 	{ ENOTCONN,		   osl_Socket_E_NotConnected 	  }, /* Socket is not connected */
379*cdf0e10cSrcweir 	{ ESHUTDOWN,	   osl_Socket_E_Shutdown 		  }, /* Can't send after socket shutdown */
380*cdf0e10cSrcweir 	{ ETOOMANYREFS,	   osl_Socket_E_TooManyRefs 	  }, /* Too many references: can't splice */
381*cdf0e10cSrcweir 	{ ETIMEDOUT,	   osl_Socket_E_TimedOut 		  }, /* Connection timed out */
382*cdf0e10cSrcweir 	{ ECONNREFUSED,	   osl_Socket_E_ConnRefused 	  }, /* Connection refused */
383*cdf0e10cSrcweir 	{ EHOSTDOWN,	   osl_Socket_E_HostDown 		  }, /* Host is down */
384*cdf0e10cSrcweir 	{ EHOSTUNREACH,	   osl_Socket_E_HostUnreachable   }, /* No route to host */
385*cdf0e10cSrcweir 	{ EWOULDBLOCK,	   osl_Socket_E_WouldBlock 		  }, /* call would block on non-blocking socket */
386*cdf0e10cSrcweir 	{ EALREADY,		   osl_Socket_E_Already 		  }, /* operation already in progress */
387*cdf0e10cSrcweir 	{ EINPROGRESS,	   osl_Socket_E_InProgress 		  }, /* operation now in progress */
388*cdf0e10cSrcweir 	{ EAGAIN,		   osl_Socket_E_WouldBlock 		  }, /* same as EWOULDBLOCK */
389*cdf0e10cSrcweir 	{ -1,		   	   osl_Socket_E_InvalidError      }
390*cdf0e10cSrcweir };
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir /* map */
393*cdf0e10cSrcweir /* mfe: NOT USED
394*cdf0e10cSrcweir static int osl_NativeFromSocketError(oslSocketError errorCode)
395*cdf0e10cSrcweir {
396*cdf0e10cSrcweir 	int i = 0;
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir 	while ((SocketError[i].error != osl_Socket_E_InvalidError) &&
399*cdf0e10cSrcweir 		   (SocketError[i].error != errorCode)) i++;
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir 	return SocketError[i].errcode;
402*cdf0e10cSrcweir }
403*cdf0e10cSrcweir */
404*cdf0e10cSrcweir 
405*cdf0e10cSrcweir /* reverse map */
406*cdf0e10cSrcweir static oslSocketError osl_SocketErrorFromNative(int nativeType)
407*cdf0e10cSrcweir {
408*cdf0e10cSrcweir 	int i = 0;
409*cdf0e10cSrcweir 
410*cdf0e10cSrcweir 	while ((SocketError[i].error != osl_Socket_E_InvalidError) &&
411*cdf0e10cSrcweir 		   (SocketError[i].errcode != nativeType)) i++;
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir 	return SocketError[i].error;
414*cdf0e10cSrcweir }
415*cdf0e10cSrcweir 
416*cdf0e10cSrcweir /* macros */
417*cdf0e10cSrcweir #define ERROR_TO_NATIVE(x)		osl_NativeFromSocketError(x)
418*cdf0e10cSrcweir #define ERROR_FROM_NATIVE(y)	osl_SocketErrorFromNative(y)
419*cdf0e10cSrcweir 
420*cdf0e10cSrcweir /*****************************************************************************/
421*cdf0e10cSrcweir /* local function prototypes */
422*cdf0e10cSrcweir /*****************************************************************************/
423*cdf0e10cSrcweir 
424*cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_psz_createInetSocketAddr (
425*cdf0e10cSrcweir 	const sal_Char* pszDottedAddr, sal_Int32 Port);
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_psz_createIpxSocketAddr (
428*cdf0e10cSrcweir 	const sal_Char NetNumber[4],
429*cdf0e10cSrcweir 	const sal_Char NodeNumber[6],
430*cdf0e10cSrcweir 	sal_uInt32 SocketNumber);
431*cdf0e10cSrcweir 
432*cdf0e10cSrcweir oslHostAddr SAL_CALL osl_psz_createHostAddr (
433*cdf0e10cSrcweir 	const sal_Char *pszHostname, const oslSocketAddr Addr);
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir oslHostAddr SAL_CALL osl_psz_createHostAddrByName (
436*cdf0e10cSrcweir 	const sal_Char *pszHostname);
437*cdf0e10cSrcweir 
438*cdf0e10cSrcweir const sal_Char* SAL_CALL osl_psz_getHostnameOfHostAddr (
439*cdf0e10cSrcweir 	const oslHostAddr Addr);
440*cdf0e10cSrcweir 
441*cdf0e10cSrcweir oslSocketResult SAL_CALL osl_psz_getLocalHostname (
442*cdf0e10cSrcweir 	sal_Char *pBuffer, sal_uInt32 nBufLen);
443*cdf0e10cSrcweir 
444*cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_psz_resolveHostname (
445*cdf0e10cSrcweir 	const sal_Char* pszHostname);
446*cdf0e10cSrcweir 
447*cdf0e10cSrcweir sal_Int32 SAL_CALL osl_psz_getServicePort (
448*cdf0e10cSrcweir 	const sal_Char* pszServicename, const sal_Char* pszProtocol);
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir oslSocketResult SAL_CALL osl_psz_getHostnameOfSocketAddr (
451*cdf0e10cSrcweir 	oslSocketAddr Addr, sal_Char *pBuffer, sal_uInt32 BufferSize);
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir oslSocketResult	SAL_CALL osl_psz_getDottedInetAddrOfSocketAddr (
454*cdf0e10cSrcweir 	oslSocketAddr Addr, sal_Char *pBuffer, sal_uInt32 BufferSize);
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir void SAL_CALL osl_psz_getLastSocketErrorDescription (
457*cdf0e10cSrcweir 	oslSocket Socket, sal_Char* pBuffer, sal_uInt32 BufferSize);
458*cdf0e10cSrcweir 
459*cdf0e10cSrcweir /*****************************************************************************/
460*cdf0e10cSrcweir /* osl_create/destroy-SocketImpl */
461*cdf0e10cSrcweir /*****************************************************************************/
462*cdf0e10cSrcweir 
463*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
464*cdf0e10cSrcweir static sal_uInt32 g_nSocketImpl = 0;
465*cdf0e10cSrcweir static sal_uInt32 g_nSocketAddr = 0;
466*cdf0e10cSrcweir 
467*cdf0e10cSrcweir /* sorry, must be implemented otherwise */
468*cdf0e10cSrcweir #if 0
469*cdf0e10cSrcweir struct LeakWarning
470*cdf0e10cSrcweir {
471*cdf0e10cSrcweir 	~LeakWarning()
472*cdf0e10cSrcweir 	{
473*cdf0e10cSrcweir 		if( g_nSocketImpl )
474*cdf0e10cSrcweir 			OSL_TRACE( "sal_socket: %d socket instances leak\n" , g_nSocketImpl );
475*cdf0e10cSrcweir 		if( g_nSocketAddr )
476*cdf0e10cSrcweir 			OSL_TRACE( "sal_socket: %d socket address instances leak\n" , g_nSocketAddr );
477*cdf0e10cSrcweir 	}
478*cdf0e10cSrcweir };
479*cdf0e10cSrcweir LeakWarning socketWarning;
480*cdf0e10cSrcweir #endif
481*cdf0e10cSrcweir 
482*cdf0e10cSrcweir #endif /* OSL_DEBUG_LEVEL */
483*cdf0e10cSrcweir 
484*cdf0e10cSrcweir 
485*cdf0e10cSrcweir oslSocket __osl_createSocketImpl(int Socket)
486*cdf0e10cSrcweir {
487*cdf0e10cSrcweir 	oslSocket pSocket;
488*cdf0e10cSrcweir 
489*cdf0e10cSrcweir 	pSocket = (oslSocket)calloc(1, sizeof(struct oslSocketImpl));
490*cdf0e10cSrcweir 
491*cdf0e10cSrcweir 	pSocket->m_Socket = Socket;
492*cdf0e10cSrcweir     pSocket->m_nLastError = 0;
493*cdf0e10cSrcweir     pSocket->m_CloseCallback = 0;
494*cdf0e10cSrcweir     pSocket->m_CallbackArg = 0;
495*cdf0e10cSrcweir 	pSocket->m_nRefCount = 1;
496*cdf0e10cSrcweir 
497*cdf0e10cSrcweir #if defined(LINUX)
498*cdf0e10cSrcweir     pSocket->m_bIsAccepting = sal_False;
499*cdf0e10cSrcweir #endif
500*cdf0e10cSrcweir 
501*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
502*cdf0e10cSrcweir 	g_nSocketImpl ++;
503*cdf0e10cSrcweir #endif
504*cdf0e10cSrcweir 	return pSocket;
505*cdf0e10cSrcweir }
506*cdf0e10cSrcweir 
507*cdf0e10cSrcweir void __osl_destroySocketImpl(oslSocket Socket)
508*cdf0e10cSrcweir {
509*cdf0e10cSrcweir 	if ( Socket != NULL)
510*cdf0e10cSrcweir 		free((struct oslSocketImpl *) Socket);
511*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
512*cdf0e10cSrcweir 	g_nSocketImpl --;
513*cdf0e10cSrcweir #endif
514*cdf0e10cSrcweir }
515*cdf0e10cSrcweir 
516*cdf0e10cSrcweir static oslSocketAddr __osl_createSocketAddr(  )
517*cdf0e10cSrcweir {
518*cdf0e10cSrcweir 	oslSocketAddr pAddr = (oslSocketAddr) rtl_allocateZeroMemory( sizeof( struct oslSocketAddrImpl ));
519*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
520*cdf0e10cSrcweir 	g_nSocketAddr ++;
521*cdf0e10cSrcweir #endif
522*cdf0e10cSrcweir 	return pAddr;
523*cdf0e10cSrcweir }
524*cdf0e10cSrcweir 
525*cdf0e10cSrcweir static oslSocketAddr __osl_createSocketAddrWithFamily(
526*cdf0e10cSrcweir 	oslAddrFamily family, sal_Int32 port, sal_uInt32 nAddr )
527*cdf0e10cSrcweir {
528*cdf0e10cSrcweir 	oslSocketAddr pAddr;
529*cdf0e10cSrcweir 
530*cdf0e10cSrcweir 	OSL_ASSERT( family == osl_Socket_FamilyInet );
531*cdf0e10cSrcweir 
532*cdf0e10cSrcweir 	pAddr = __osl_createSocketAddr();
533*cdf0e10cSrcweir 	switch( family )
534*cdf0e10cSrcweir 	{
535*cdf0e10cSrcweir 	case osl_Socket_FamilyInet:
536*cdf0e10cSrcweir 	{
537*cdf0e10cSrcweir 		struct sockaddr_in* pInetAddr= (struct sockaddr_in*)&(pAddr->m_sockaddr);
538*cdf0e10cSrcweir 
539*cdf0e10cSrcweir 		pInetAddr->sin_family = FAMILY_TO_NATIVE(osl_Socket_FamilyInet);
540*cdf0e10cSrcweir 		pInetAddr->sin_addr.s_addr = nAddr;
541*cdf0e10cSrcweir 		pInetAddr->sin_port = (sal_uInt16)(port&0xffff);
542*cdf0e10cSrcweir 		break;
543*cdf0e10cSrcweir    	}
544*cdf0e10cSrcweir 	default:
545*cdf0e10cSrcweir 		pAddr->m_sockaddr.sa_family = FAMILY_TO_NATIVE(family);
546*cdf0e10cSrcweir 	}
547*cdf0e10cSrcweir 	return pAddr;
548*cdf0e10cSrcweir }
549*cdf0e10cSrcweir 
550*cdf0e10cSrcweir static oslSocketAddr __osl_createSocketAddrFromSystem( struct sockaddr *pSystemSockAddr )
551*cdf0e10cSrcweir {
552*cdf0e10cSrcweir 	oslSocketAddr pAddr = __osl_createSocketAddr();
553*cdf0e10cSrcweir 	memcpy( &(pAddr->m_sockaddr), pSystemSockAddr, sizeof( struct sockaddr ) );
554*cdf0e10cSrcweir 	return pAddr;
555*cdf0e10cSrcweir }
556*cdf0e10cSrcweir 
557*cdf0e10cSrcweir static void __osl_destroySocketAddr( oslSocketAddr addr )
558*cdf0e10cSrcweir {
559*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
560*cdf0e10cSrcweir 	g_nSocketAddr --;
561*cdf0e10cSrcweir #endif
562*cdf0e10cSrcweir 	rtl_freeMemory( addr );
563*cdf0e10cSrcweir }
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir /*****************************************************************************/
566*cdf0e10cSrcweir /* osl_createEmptySocketAddr */
567*cdf0e10cSrcweir /*****************************************************************************/
568*cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_createEmptySocketAddr(oslAddrFamily Family)
569*cdf0e10cSrcweir {
570*cdf0e10cSrcweir 	oslSocketAddr pAddr = 0;
571*cdf0e10cSrcweir 
572*cdf0e10cSrcweir 	/* is it an internet-Addr? */
573*cdf0e10cSrcweir 	if (Family == osl_Socket_FamilyInet)
574*cdf0e10cSrcweir 	{
575*cdf0e10cSrcweir 		pAddr = __osl_createSocketAddrWithFamily(Family, 0 , htonl(INADDR_ANY) );
576*cdf0e10cSrcweir 	}
577*cdf0e10cSrcweir 	else
578*cdf0e10cSrcweir 	{
579*cdf0e10cSrcweir 		pAddr = __osl_createSocketAddrWithFamily( Family , 0 , 0 );
580*cdf0e10cSrcweir 	}
581*cdf0e10cSrcweir 
582*cdf0e10cSrcweir 	return pAddr;
583*cdf0e10cSrcweir }
584*cdf0e10cSrcweir 
585*cdf0e10cSrcweir /*****************************************************************************/
586*cdf0e10cSrcweir /* osl_copySocketAddr */
587*cdf0e10cSrcweir /*****************************************************************************/
588*cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_copySocketAddr(oslSocketAddr Addr)
589*cdf0e10cSrcweir {
590*cdf0e10cSrcweir 	oslSocketAddr pCopy = 0;
591*cdf0e10cSrcweir 	if (Addr)
592*cdf0e10cSrcweir 	{
593*cdf0e10cSrcweir 		pCopy = __osl_createSocketAddr();
594*cdf0e10cSrcweir 
595*cdf0e10cSrcweir 		if (pCopy)
596*cdf0e10cSrcweir 			memcpy(&(pCopy->m_sockaddr),&(Addr->m_sockaddr), sizeof(struct sockaddr));
597*cdf0e10cSrcweir 	}
598*cdf0e10cSrcweir 	return pCopy;
599*cdf0e10cSrcweir }
600*cdf0e10cSrcweir 
601*cdf0e10cSrcweir /*****************************************************************************/
602*cdf0e10cSrcweir /* osl_isEqualSocketAddr */
603*cdf0e10cSrcweir /*****************************************************************************/
604*cdf0e10cSrcweir sal_Bool SAL_CALL osl_isEqualSocketAddr (
605*cdf0e10cSrcweir 	oslSocketAddr Addr1,
606*cdf0e10cSrcweir 	oslSocketAddr Addr2)
607*cdf0e10cSrcweir {
608*cdf0e10cSrcweir 	struct sockaddr* pAddr1= &(Addr1->m_sockaddr);
609*cdf0e10cSrcweir 	struct sockaddr* pAddr2= &(Addr2->m_sockaddr);
610*cdf0e10cSrcweir 
611*cdf0e10cSrcweir 	OSL_ASSERT(pAddr1);
612*cdf0e10cSrcweir 	OSL_ASSERT(pAddr2);
613*cdf0e10cSrcweir 
614*cdf0e10cSrcweir 	if (pAddr1->sa_family == pAddr2->sa_family)
615*cdf0e10cSrcweir 	{
616*cdf0e10cSrcweir 		switch (pAddr1->sa_family)
617*cdf0e10cSrcweir 		{
618*cdf0e10cSrcweir 			case AF_INET:
619*cdf0e10cSrcweir 			{
620*cdf0e10cSrcweir 				struct sockaddr_in* pInetAddr1= (struct sockaddr_in*)pAddr1;
621*cdf0e10cSrcweir 				struct sockaddr_in* pInetAddr2= (struct sockaddr_in*)pAddr2;
622*cdf0e10cSrcweir 
623*cdf0e10cSrcweir 				if ((pInetAddr1->sin_family == pInetAddr2->sin_family) &&
624*cdf0e10cSrcweir 					(pInetAddr1->sin_addr.s_addr == pInetAddr2->sin_addr.s_addr) &&
625*cdf0e10cSrcweir 					(pInetAddr1->sin_port == pInetAddr2->sin_port))
626*cdf0e10cSrcweir 					return (sal_True);
627*cdf0e10cSrcweir 			}
628*cdf0e10cSrcweir 
629*cdf0e10cSrcweir 			default:
630*cdf0e10cSrcweir 			{
631*cdf0e10cSrcweir 				return (memcmp(pAddr1, Addr2, sizeof(struct sockaddr)) == 0);
632*cdf0e10cSrcweir 			}
633*cdf0e10cSrcweir 		}
634*cdf0e10cSrcweir 	}
635*cdf0e10cSrcweir 
636*cdf0e10cSrcweir 	return (sal_False);
637*cdf0e10cSrcweir }
638*cdf0e10cSrcweir 
639*cdf0e10cSrcweir /*****************************************************************************/
640*cdf0e10cSrcweir /* osl_createInetBroadcastAddr */
641*cdf0e10cSrcweir /*****************************************************************************/
642*cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_createInetBroadcastAddr (
643*cdf0e10cSrcweir 	rtl_uString *strDottedAddr,
644*cdf0e10cSrcweir 	sal_Int32    Port)
645*cdf0e10cSrcweir {
646*cdf0e10cSrcweir 	sal_uInt32    nAddr = OSL_INADDR_NONE;
647*cdf0e10cSrcweir 	oslSocketAddr pAddr;
648*cdf0e10cSrcweir 
649*cdf0e10cSrcweir 	if (strDottedAddr && strDottedAddr->length)
650*cdf0e10cSrcweir 	{
651*cdf0e10cSrcweir 		/* Dotted host address for limited broadcast */
652*cdf0e10cSrcweir 		rtl_String *pDottedAddr = NULL;
653*cdf0e10cSrcweir 
654*cdf0e10cSrcweir 		rtl_uString2String (
655*cdf0e10cSrcweir 			&pDottedAddr, strDottedAddr->buffer, strDottedAddr->length,
656*cdf0e10cSrcweir 			RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
657*cdf0e10cSrcweir 
658*cdf0e10cSrcweir 		nAddr = inet_addr (pDottedAddr->buffer);
659*cdf0e10cSrcweir 		rtl_string_release (pDottedAddr);
660*cdf0e10cSrcweir 	}
661*cdf0e10cSrcweir 
662*cdf0e10cSrcweir 	if (nAddr != OSL_INADDR_NONE)
663*cdf0e10cSrcweir 	{
664*cdf0e10cSrcweir 		/* Limited broadcast */
665*cdf0e10cSrcweir 		nAddr = ntohl(nAddr);
666*cdf0e10cSrcweir 		if (IN_CLASSA(nAddr))
667*cdf0e10cSrcweir 		{
668*cdf0e10cSrcweir 			nAddr &= IN_CLASSA_NET;
669*cdf0e10cSrcweir 			nAddr |= IN_CLASSA_HOST;
670*cdf0e10cSrcweir 		}
671*cdf0e10cSrcweir 		else if (IN_CLASSB(nAddr))
672*cdf0e10cSrcweir 		{
673*cdf0e10cSrcweir 			nAddr &= IN_CLASSB_NET;
674*cdf0e10cSrcweir 			nAddr |= IN_CLASSB_HOST;
675*cdf0e10cSrcweir 		}
676*cdf0e10cSrcweir 		else if (IN_CLASSC(nAddr))
677*cdf0e10cSrcweir 		{
678*cdf0e10cSrcweir 			nAddr &= IN_CLASSC_NET;
679*cdf0e10cSrcweir 			nAddr |= IN_CLASSC_HOST;
680*cdf0e10cSrcweir 		}
681*cdf0e10cSrcweir 		else
682*cdf0e10cSrcweir 		{
683*cdf0e10cSrcweir 			/* No broadcast in class D */
684*cdf0e10cSrcweir 			return ((oslSocketAddr)NULL);
685*cdf0e10cSrcweir 		}
686*cdf0e10cSrcweir 		nAddr = htonl(nAddr);
687*cdf0e10cSrcweir 	}
688*cdf0e10cSrcweir 
689*cdf0e10cSrcweir 	pAddr = __osl_createSocketAddrWithFamily( osl_Socket_FamilyInet, htons(Port), nAddr );
690*cdf0e10cSrcweir 	return pAddr;
691*cdf0e10cSrcweir }
692*cdf0e10cSrcweir 
693*cdf0e10cSrcweir /*****************************************************************************/
694*cdf0e10cSrcweir /* osl_createInetSocketAddr */
695*cdf0e10cSrcweir /*****************************************************************************/
696*cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_createInetSocketAddr (
697*cdf0e10cSrcweir 	rtl_uString *ustrDottedAddr,
698*cdf0e10cSrcweir 	sal_Int32    Port)
699*cdf0e10cSrcweir {
700*cdf0e10cSrcweir     rtl_String* strDottedAddr=0;
701*cdf0e10cSrcweir     oslSocketAddr Addr;
702*cdf0e10cSrcweir     sal_Char* pszDottedAddr=0;
703*cdf0e10cSrcweir 
704*cdf0e10cSrcweir     if ( ustrDottedAddr != 0 )
705*cdf0e10cSrcweir     {
706*cdf0e10cSrcweir         rtl_uString2String( &strDottedAddr,
707*cdf0e10cSrcweir                             rtl_uString_getStr(ustrDottedAddr),
708*cdf0e10cSrcweir                             rtl_uString_getLength(ustrDottedAddr),
709*cdf0e10cSrcweir                             RTL_TEXTENCODING_UTF8,
710*cdf0e10cSrcweir                             OUSTRING_TO_OSTRING_CVTFLAGS);
711*cdf0e10cSrcweir         pszDottedAddr = rtl_string_getStr(strDottedAddr);
712*cdf0e10cSrcweir     }
713*cdf0e10cSrcweir 
714*cdf0e10cSrcweir 
715*cdf0e10cSrcweir     Addr = osl_psz_createInetSocketAddr(pszDottedAddr, Port);
716*cdf0e10cSrcweir 
717*cdf0e10cSrcweir     if ( strDottedAddr != 0 )
718*cdf0e10cSrcweir     {
719*cdf0e10cSrcweir         rtl_string_release(strDottedAddr);
720*cdf0e10cSrcweir     }
721*cdf0e10cSrcweir 
722*cdf0e10cSrcweir     return Addr;
723*cdf0e10cSrcweir }
724*cdf0e10cSrcweir 
725*cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_psz_createInetSocketAddr (
726*cdf0e10cSrcweir 	const sal_Char* pszDottedAddr,
727*cdf0e10cSrcweir 	sal_Int32       Port)
728*cdf0e10cSrcweir {
729*cdf0e10cSrcweir 	oslSocketAddr pAddr = 0;
730*cdf0e10cSrcweir 	sal_Int32 Addr = inet_addr(pszDottedAddr);
731*cdf0e10cSrcweir 	if(Addr != -1)
732*cdf0e10cSrcweir 	{
733*cdf0e10cSrcweir 		/* valid dotted addr */
734*cdf0e10cSrcweir 		pAddr = __osl_createSocketAddrWithFamily( osl_Socket_FamilyInet, htons(Port) , Addr );
735*cdf0e10cSrcweir 	}
736*cdf0e10cSrcweir 	return pAddr;
737*cdf0e10cSrcweir }
738*cdf0e10cSrcweir 
739*cdf0e10cSrcweir /*****************************************************************************/
740*cdf0e10cSrcweir /* osl_setAddrOfSocketAddr */
741*cdf0e10cSrcweir /*****************************************************************************/
742*cdf0e10cSrcweir oslSocketResult SAL_CALL osl_setAddrOfSocketAddr( oslSocketAddr pAddr, sal_Sequence *pByteSeq )
743*cdf0e10cSrcweir {
744*cdf0e10cSrcweir 	oslSocketResult res = osl_Socket_Error;
745*cdf0e10cSrcweir 
746*cdf0e10cSrcweir 	OSL_ASSERT( pAddr );
747*cdf0e10cSrcweir 	OSL_ASSERT( pByteSeq );
748*cdf0e10cSrcweir 
749*cdf0e10cSrcweir 	if( pAddr && pByteSeq )
750*cdf0e10cSrcweir 	{
751*cdf0e10cSrcweir 		struct sockaddr_in * pSystemInetAddr;
752*cdf0e10cSrcweir 
753*cdf0e10cSrcweir 		OSL_ASSERT( pAddr->m_sockaddr.sa_family == FAMILY_TO_NATIVE( osl_Socket_FamilyInet ) );
754*cdf0e10cSrcweir 		OSL_ASSERT( pByteSeq->nElements == 4 );
755*cdf0e10cSrcweir 
756*cdf0e10cSrcweir 		pSystemInetAddr = (struct sockaddr_in * ) &(pAddr->m_sockaddr);
757*cdf0e10cSrcweir 		memcpy( &(pSystemInetAddr->sin_addr) , pByteSeq->elements , 4 );
758*cdf0e10cSrcweir 		res = osl_Socket_Ok;
759*cdf0e10cSrcweir 	}
760*cdf0e10cSrcweir 	return res;
761*cdf0e10cSrcweir }
762*cdf0e10cSrcweir 
763*cdf0e10cSrcweir /*****************************************************************************/
764*cdf0e10cSrcweir /* osl_getAddrOfSocketAddr */
765*cdf0e10cSrcweir /*****************************************************************************/
766*cdf0e10cSrcweir oslSocketResult SAL_CALL osl_getAddrOfSocketAddr( oslSocketAddr pAddr, sal_Sequence **ppByteSeq )
767*cdf0e10cSrcweir {
768*cdf0e10cSrcweir 	oslSocketResult res = osl_Socket_Error;
769*cdf0e10cSrcweir 
770*cdf0e10cSrcweir 	OSL_ASSERT( pAddr );
771*cdf0e10cSrcweir 	OSL_ASSERT( ppByteSeq );
772*cdf0e10cSrcweir 
773*cdf0e10cSrcweir 	if( pAddr && ppByteSeq )
774*cdf0e10cSrcweir 	{
775*cdf0e10cSrcweir 		struct sockaddr_in * pSystemInetAddr = (struct sockaddr_in * ) &(pAddr->m_sockaddr);
776*cdf0e10cSrcweir 		rtl_byte_sequence_constructFromArray( ppByteSeq , (sal_Int8 *) &(pSystemInetAddr->sin_addr),4);
777*cdf0e10cSrcweir 		res = osl_Socket_Ok;
778*cdf0e10cSrcweir 	}
779*cdf0e10cSrcweir 	return res;
780*cdf0e10cSrcweir }
781*cdf0e10cSrcweir 
782*cdf0e10cSrcweir 
783*cdf0e10cSrcweir /*****************************************************************************/
784*cdf0e10cSrcweir /* _osl_getFullQualifiedDomainName */
785*cdf0e10cSrcweir /*****************************************************************************/
786*cdf0e10cSrcweir 
787*cdf0e10cSrcweir /** try to figure out a full-qualified hostname, by adding the current domain
788*cdf0e10cSrcweir     as given by the domainname program to the given hostname.
789*cdf0e10cSrcweir 	This function MUST NOT call gethostbyname since pHostName allready points
790*cdf0e10cSrcweir 	to data returned by gethostname and would be garbled: use gethostname_r
791*cdf0e10cSrcweir 	instead!
792*cdf0e10cSrcweir  */
793*cdf0e10cSrcweir 
794*cdf0e10cSrcweir /* wrap around different interfaces to reentrant gethostbyname */
795*cdf0e10cSrcweir static struct hostent* _osl_gethostbyname_r (
796*cdf0e10cSrcweir 	const char *name, struct hostent *result,
797*cdf0e10cSrcweir 	char *buffer, int buflen, int *h_errnop)
798*cdf0e10cSrcweir {
799*cdf0e10cSrcweir 
800*cdf0e10cSrcweir #ifdef LINUX
801*cdf0e10cSrcweir 	struct hostent *__result; /* will be the same as result */
802*cdf0e10cSrcweir 	int __error;
803*cdf0e10cSrcweir 	__error = gethostbyname_r (name, result, buffer, buflen,
804*cdf0e10cSrcweir 				 &__result, h_errnop);
805*cdf0e10cSrcweir 	return __error ? NULL : __result ;
806*cdf0e10cSrcweir #elif defined OS2
807*cdf0e10cSrcweir 	// YD FIXME!!!
808*cdf0e10cSrcweir 	return 0;
809*cdf0e10cSrcweir #else
810*cdf0e10cSrcweir 	return gethostbyname_r( name, result, buffer, buflen, h_errnop);
811*cdf0e10cSrcweir #endif
812*cdf0e10cSrcweir }
813*cdf0e10cSrcweir 
814*cdf0e10cSrcweir static sal_Bool  _osl_getDomainName (sal_Char *buffer, sal_Int32 bufsiz)
815*cdf0e10cSrcweir {
816*cdf0e10cSrcweir 	sal_Bool result;
817*cdf0e10cSrcweir 	int      p[2];
818*cdf0e10cSrcweir 
819*cdf0e10cSrcweir     result = sal_False;
820*cdf0e10cSrcweir 
821*cdf0e10cSrcweir #if 0 // YD 17/04/06 libc panic for fork() from thread!=1
822*cdf0e10cSrcweir 
823*cdf0e10cSrcweir 	if (pipe (p) == 0)
824*cdf0e10cSrcweir 	{
825*cdf0e10cSrcweir 		pid_t pid;
826*cdf0e10cSrcweir         int nStatus;
827*cdf0e10cSrcweir 
828*cdf0e10cSrcweir 		pid = fork();
829*cdf0e10cSrcweir 		if (pid == 0)
830*cdf0e10cSrcweir 		{
831*cdf0e10cSrcweir 			char *argv[] =
832*cdf0e10cSrcweir 			{
833*cdf0e10cSrcweir 				"/bin/domainname",
834*cdf0e10cSrcweir 				NULL
835*cdf0e10cSrcweir 			};
836*cdf0e10cSrcweir 
837*cdf0e10cSrcweir 			close (p[0]);
838*cdf0e10cSrcweir 			dup2  (p[1], 1);
839*cdf0e10cSrcweir 			close (p[1]);
840*cdf0e10cSrcweir 
841*cdf0e10cSrcweir 			execv ("/bin/domainname", argv);
842*cdf0e10cSrcweir             // arriving here means exec failed
843*cdf0e10cSrcweir             _exit(-1);
844*cdf0e10cSrcweir 		}
845*cdf0e10cSrcweir 		else if (pid > 0)
846*cdf0e10cSrcweir 		{
847*cdf0e10cSrcweir 			sal_Int32 k = 0, n = bufsiz;
848*cdf0e10cSrcweir 
849*cdf0e10cSrcweir 			close (p[1]);
850*cdf0e10cSrcweir 			if ((k = read (p[0], buffer, n - 1)) > 0)
851*cdf0e10cSrcweir 			{
852*cdf0e10cSrcweir 				buffer[k] = 0;
853*cdf0e10cSrcweir 				if (buffer[k - 1] == '\n')
854*cdf0e10cSrcweir 					buffer[k - 1] = 0;
855*cdf0e10cSrcweir 				result = sal_True;
856*cdf0e10cSrcweir 			}
857*cdf0e10cSrcweir 			close (p[0]);
858*cdf0e10cSrcweir             waitpid (pid, &nStatus, 0);
859*cdf0e10cSrcweir 		}
860*cdf0e10cSrcweir 		else
861*cdf0e10cSrcweir 		{
862*cdf0e10cSrcweir 			close (p[0]);
863*cdf0e10cSrcweir 			close (p[1]);
864*cdf0e10cSrcweir 		}
865*cdf0e10cSrcweir 	}
866*cdf0e10cSrcweir #endif // 0
867*cdf0e10cSrcweir 
868*cdf0e10cSrcweir 	return (result);
869*cdf0e10cSrcweir }
870*cdf0e10cSrcweir 
871*cdf0e10cSrcweir static sal_Char* _osl_getFullQualifiedDomainName (const sal_Char *pHostName)
872*cdf0e10cSrcweir {
873*cdf0e10cSrcweir #	define DOMAINNAME_LENGTH 512
874*cdf0e10cSrcweir 	sal_uInt32 			nLengthOfHostName;
875*cdf0e10cSrcweir 	static sal_uInt32 	nLengthOfDomainName = 0;
876*cdf0e10cSrcweir 	static sal_Char    *pDomainName = NULL;
877*cdf0e10cSrcweir 
878*cdf0e10cSrcweir 	sal_Char  *pFullQualifiedName;
879*cdf0e10cSrcweir #if 0  /* OBSOLETE */
880*cdf0e10cSrcweir 	FILE      *pPipeToDomainnameExe;
881*cdf0e10cSrcweir #endif /* OBSOLETE */
882*cdf0e10cSrcweir 
883*cdf0e10cSrcweir 	/* get a '\0' terminated domainname */
884*cdf0e10cSrcweir 
885*cdf0e10cSrcweir 	/* read default domainname default from environment */
886*cdf0e10cSrcweir 	if (nLengthOfDomainName == 0)
887*cdf0e10cSrcweir 	{
888*cdf0e10cSrcweir 	    sal_Char *pEnvDomain;
889*cdf0e10cSrcweir 
890*cdf0e10cSrcweir 	    pEnvDomain = getenv ("STAR_OVERRIDE_DOMAINNAME");
891*cdf0e10cSrcweir 		if (pEnvDomain)
892*cdf0e10cSrcweir 		{
893*cdf0e10cSrcweir 		    pDomainName = strdup (pEnvDomain);
894*cdf0e10cSrcweir 		    nLengthOfDomainName = strlen (pDomainName);
895*cdf0e10cSrcweir 		}
896*cdf0e10cSrcweir 	}
897*cdf0e10cSrcweir 
898*cdf0e10cSrcweir #if 1  /* NEW */
899*cdf0e10cSrcweir 	if (nLengthOfDomainName == 0)
900*cdf0e10cSrcweir 	{
901*cdf0e10cSrcweir 		sal_Char pDomainNameBuffer[ DOMAINNAME_LENGTH ];
902*cdf0e10cSrcweir 
903*cdf0e10cSrcweir         pDomainNameBuffer[0] = '\0';
904*cdf0e10cSrcweir 
905*cdf0e10cSrcweir 		if (_osl_getDomainName (pDomainNameBuffer, DOMAINNAME_LENGTH))
906*cdf0e10cSrcweir 		{
907*cdf0e10cSrcweir 			pDomainName = strdup (pDomainNameBuffer);
908*cdf0e10cSrcweir 			nLengthOfDomainName = strlen (pDomainName);
909*cdf0e10cSrcweir 		}
910*cdf0e10cSrcweir 	}
911*cdf0e10cSrcweir 
912*cdf0e10cSrcweir #endif /* NEW */
913*cdf0e10cSrcweir #if 0  /* OBSOLETE */
914*cdf0e10cSrcweir #ifdef SCO
915*cdf0e10cSrcweir 
916*cdf0e10cSrcweir 	/* call 'domainname > /usr/tmp/some-tmp-file', since
917*cdf0e10cSrcweir 	   popen read pclose do block or core-dump,
918*cdf0e10cSrcweir 	   (even the pipe-stuff that comes with pthreads) */
919*cdf0e10cSrcweir 	if (nLengthOfDomainName == 0)
920*cdf0e10cSrcweir 	{
921*cdf0e10cSrcweir 		sal_Char  tmp_name[ L_tmpnam ];
922*cdf0e10cSrcweir         FILE 	 *tmp_file;
923*cdf0e10cSrcweir 		sal_Char  domain_call [ L_tmpnam + 16 ] = "domainname > ";
924*cdf0e10cSrcweir 
925*cdf0e10cSrcweir         tmp_name[0] = '\0';
926*cdf0e10cSrcweir 
927*cdf0e10cSrcweir 		tmpnam ( tmp_name );
928*cdf0e10cSrcweir 		strcat ( domain_call, tmp_name );
929*cdf0e10cSrcweir 		if (   (system ( domain_call ) == 0)
930*cdf0e10cSrcweir 			&& ((tmp_file = fopen( tmp_name, "r" )) != NULL ) )
931*cdf0e10cSrcweir 		{
932*cdf0e10cSrcweir 			sal_Char  pDomainNameBuffer[ DOMAINNAME_LENGTH ];
933*cdf0e10cSrcweir 
934*cdf0e10cSrcweir             pDomainNameBuffer[0] = '\0';
935*cdf0e10cSrcweir 
936*cdf0e10cSrcweir 			if ( fgets ( pDomainNameBuffer, DOMAINNAME_LENGTH, tmp_file ) )
937*cdf0e10cSrcweir 			{
938*cdf0e10cSrcweir 				pDomainName = strdup( pDomainNameBuffer );
939*cdf0e10cSrcweir 				nLengthOfDomainName = strlen( pDomainName );
940*cdf0e10cSrcweir 				if (   ( nLengthOfDomainName > 0 )
941*cdf0e10cSrcweir 					&& ( pDomainName[ nLengthOfDomainName - 1] == '\n' ) )
942*cdf0e10cSrcweir 					pDomainName[ --nLengthOfDomainName ] = '\0';
943*cdf0e10cSrcweir 			}
944*cdf0e10cSrcweir 			fclose ( tmp_file );
945*cdf0e10cSrcweir 		}
946*cdf0e10cSrcweir 		unlink( tmp_name );
947*cdf0e10cSrcweir 	}
948*cdf0e10cSrcweir 
949*cdf0e10cSrcweir #else /* !SCO */
950*cdf0e10cSrcweir 
951*cdf0e10cSrcweir 	/* read the domainname from pipe to the program domainname */
952*cdf0e10cSrcweir 	if (   (nLengthOfDomainName == 0)
953*cdf0e10cSrcweir 		&& (pPipeToDomainnameExe = popen( "domainname", "r")) )
954*cdf0e10cSrcweir 	{
955*cdf0e10cSrcweir 		sal_Char  c;
956*cdf0e10cSrcweir 		sal_Char  pDomainNameBuffer[ DOMAINNAME_LENGTH ];
957*cdf0e10cSrcweir 		sal_Char *pDomainNamePointer;
958*cdf0e10cSrcweir 
959*cdf0e10cSrcweir         pDomainNameBuffer[0] = '\0';
960*cdf0e10cSrcweir 
961*cdf0e10cSrcweir 		pDomainNamePointer = pDomainNameBuffer;
962*cdf0e10cSrcweir     	while (    ((c = getc( pPipeToDomainnameExe )) != EOF)
963*cdf0e10cSrcweir 				&& (nLengthOfDomainName < (DOMAINNAME_LENGTH - 1)) )
964*cdf0e10cSrcweir         {
965*cdf0e10cSrcweir         	if (! isspace(c))
966*cdf0e10cSrcweir 			{
967*cdf0e10cSrcweir 				 nLengthOfDomainName++ ;
968*cdf0e10cSrcweir            		*pDomainNamePointer++ = (sal_Char)c;
969*cdf0e10cSrcweir 			}
970*cdf0e10cSrcweir 		}
971*cdf0e10cSrcweir         *pDomainNamePointer = '\0';
972*cdf0e10cSrcweir 		pDomainName = strdup( pDomainNameBuffer );
973*cdf0e10cSrcweir 
974*cdf0e10cSrcweir 		pclose( pPipeToDomainnameExe );
975*cdf0e10cSrcweir 	}
976*cdf0e10cSrcweir 
977*cdf0e10cSrcweir #endif /* !SCO */
978*cdf0e10cSrcweir #endif /* OBSOLETE */
979*cdf0e10cSrcweir 
980*cdf0e10cSrcweir 	/* compose hostname and domainname */
981*cdf0e10cSrcweir 	nLengthOfHostName = strlen( pHostName );
982*cdf0e10cSrcweir 	pFullQualifiedName = (sal_Char*) malloc( (nLengthOfHostName + 1
983*cdf0e10cSrcweir 							+ nLengthOfDomainName + 1) * sizeof(sal_Char) );
984*cdf0e10cSrcweir 	memcpy( pFullQualifiedName, pHostName,
985*cdf0e10cSrcweir 		(nLengthOfHostName + 1) * sizeof(sal_Char) );
986*cdf0e10cSrcweir 
987*cdf0e10cSrcweir 	if ( nLengthOfDomainName > 0 )
988*cdf0e10cSrcweir 	{
989*cdf0e10cSrcweir 		/* fqdn = hostname + '.' + domainname + '\0' */
990*cdf0e10cSrcweir 		pFullQualifiedName[ nLengthOfHostName ] = '.';
991*cdf0e10cSrcweir 		memcpy( pFullQualifiedName + nLengthOfHostName + 1, pDomainName,
992*cdf0e10cSrcweir 			nLengthOfDomainName + 1 );
993*cdf0e10cSrcweir 	}
994*cdf0e10cSrcweir 
995*cdf0e10cSrcweir 	/* check whether full-qualified name and hostname point to the same host
996*cdf0e10cSrcweir 	 * should almost always be true */
997*cdf0e10cSrcweir 	if ( nLengthOfDomainName > 0 )
998*cdf0e10cSrcweir 	{
999*cdf0e10cSrcweir 		struct hostent *pQualifiedHostByName;
1000*cdf0e10cSrcweir 		struct hostent *pHostByName;
1001*cdf0e10cSrcweir 		sal_Bool        bHostsAreEqual;
1002*cdf0e10cSrcweir 
1003*cdf0e10cSrcweir 		/* buffer for calls to reentrant version of gethostbyname */
1004*cdf0e10cSrcweir 		struct hostent 	aHostByName, aQualifiedHostByName;
1005*cdf0e10cSrcweir 		sal_Char		pHostBuffer[ MAX_HOSTBUFFER_SIZE ];
1006*cdf0e10cSrcweir         sal_Char        pQualifiedHostBuffer[ MAX_HOSTBUFFER_SIZE ];
1007*cdf0e10cSrcweir 		int		nErrorNo;
1008*cdf0e10cSrcweir 
1009*cdf0e10cSrcweir         pHostBuffer[0] = '\0';
1010*cdf0e10cSrcweir         pQualifiedHostBuffer[0] = '\0';
1011*cdf0e10cSrcweir 
1012*cdf0e10cSrcweir         /* get list of addresses */
1013*cdf0e10cSrcweir 		pQualifiedHostByName = _osl_gethostbyname_r (
1014*cdf0e10cSrcweir 			pFullQualifiedName,
1015*cdf0e10cSrcweir 			&aQualifiedHostByName, pQualifiedHostBuffer,
1016*cdf0e10cSrcweir 			sizeof(pQualifiedHostBuffer), &nErrorNo );
1017*cdf0e10cSrcweir 		pHostByName = _osl_gethostbyname_r (
1018*cdf0e10cSrcweir 			pHostName,
1019*cdf0e10cSrcweir 			&aHostByName, pHostBuffer,
1020*cdf0e10cSrcweir 			sizeof(pHostBuffer), &nErrorNo );
1021*cdf0e10cSrcweir 
1022*cdf0e10cSrcweir 		/* compare addresses */
1023*cdf0e10cSrcweir 		bHostsAreEqual = sal_False;
1024*cdf0e10cSrcweir 		if ( pQualifiedHostByName && pHostByName )
1025*cdf0e10cSrcweir 		{
1026*cdf0e10cSrcweir 			sal_Char **p, **q;
1027*cdf0e10cSrcweir 			struct in_addr in;
1028*cdf0e10cSrcweir 
1029*cdf0e10cSrcweir 			/* lists are expected to be (very) short */
1030*cdf0e10cSrcweir 			for ( p = pQualifiedHostByName->h_addr_list; *p != NULL; p++ )
1031*cdf0e10cSrcweir 			{
1032*cdf0e10cSrcweir 				for ( q = pHostByName->h_addr_list; *q != NULL; q++ )
1033*cdf0e10cSrcweir 				{
1034*cdf0e10cSrcweir 					/* in.s_addr may be in_addr_t or uint32_t or heaven knows */
1035*cdf0e10cSrcweir 					if ( memcmp( *p, *q, sizeof(in.s_addr) ) == 0 )
1036*cdf0e10cSrcweir 					{
1037*cdf0e10cSrcweir 						bHostsAreEqual = sal_True;
1038*cdf0e10cSrcweir 						break;
1039*cdf0e10cSrcweir 					}
1040*cdf0e10cSrcweir 				}
1041*cdf0e10cSrcweir 				if ( bHostsAreEqual )
1042*cdf0e10cSrcweir 					break;
1043*cdf0e10cSrcweir 			}
1044*cdf0e10cSrcweir 		}
1045*cdf0e10cSrcweir 
1046*cdf0e10cSrcweir 		/* very strange case, but have to believe it: reduce the
1047*cdf0e10cSrcweir 		 * full qualified name to the unqualified host name */
1048*cdf0e10cSrcweir 		if ( !bHostsAreEqual )
1049*cdf0e10cSrcweir 		{
1050*cdf0e10cSrcweir 			OSL_TRACE("_osl_getFullQualifiedDomainName: "
1051*cdf0e10cSrcweir 					  "suspect FQDN: %s\n", pFullQualifiedName);
1052*cdf0e10cSrcweir 
1053*cdf0e10cSrcweir 			pFullQualifiedName[ nLengthOfHostName ] = '\0';
1054*cdf0e10cSrcweir 			pFullQualifiedName = (sal_Char*)realloc ( pFullQualifiedName,
1055*cdf0e10cSrcweir 								(nLengthOfHostName + 1) * sizeof( sal_Char ));
1056*cdf0e10cSrcweir 		}
1057*cdf0e10cSrcweir 	}
1058*cdf0e10cSrcweir 
1059*cdf0e10cSrcweir 	/* always return a hostname looked up as carefully as possible
1060*cdf0e10cSrcweir 	 * this string must be freed by the caller */
1061*cdf0e10cSrcweir 	return pFullQualifiedName;
1062*cdf0e10cSrcweir }
1063*cdf0e10cSrcweir 
1064*cdf0e10cSrcweir /*****************************************************************************/
1065*cdf0e10cSrcweir /* _osl_isFullQualifiedDomainName */
1066*cdf0e10cSrcweir /*****************************************************************************/
1067*cdf0e10cSrcweir static sal_Bool _osl_isFullQualifiedDomainName (const sal_Char *pHostName)
1068*cdf0e10cSrcweir {
1069*cdf0e10cSrcweir 	/* a FQDN (aka 'hostname.domain.top_level_domain' )
1070*cdf0e10cSrcweir 	 * is a name which contains a dot '.' in it ( would
1071*cdf0e10cSrcweir 	 * match as well for 'hostname.' but is good enough
1072*cdf0e10cSrcweir 	 * for now )*/
1073*cdf0e10cSrcweir 	return (sal_Bool)( strchr( pHostName, (int)'.' ) != NULL );
1074*cdf0e10cSrcweir }
1075*cdf0e10cSrcweir 
1076*cdf0e10cSrcweir /*****************************************************************************/
1077*cdf0e10cSrcweir /* oslHostAddr */
1078*cdf0e10cSrcweir /*****************************************************************************/
1079*cdf0e10cSrcweir struct oslHostAddrImpl
1080*cdf0e10cSrcweir {
1081*cdf0e10cSrcweir 	sal_Char        *pHostName;
1082*cdf0e10cSrcweir 	oslSocketAddr   pSockAddr;
1083*cdf0e10cSrcweir };
1084*cdf0e10cSrcweir 
1085*cdf0e10cSrcweir static oslHostAddr _osl_hostentToHostAddr (const struct hostent *he)
1086*cdf0e10cSrcweir {
1087*cdf0e10cSrcweir 	oslHostAddr pAddr= NULL;
1088*cdf0e10cSrcweir 	oslSocketAddr pSockAddr = 0;
1089*cdf0e10cSrcweir 
1090*cdf0e10cSrcweir 
1091*cdf0e10cSrcweir 	if ((he == NULL) || (he->h_name == NULL) || (he->h_addr_list[0] == NULL))
1092*cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
1093*cdf0e10cSrcweir 
1094*cdf0e10cSrcweir 	//YD 18/06/2006 win32 does this with unicode, see socket.cxx
1095*cdf0e10cSrcweir 	sal_Char        *cn;
1096*cdf0e10cSrcweir 	cn= (sal_Char *)malloc(strlen (he->h_name) + 1);
1097*cdf0e10cSrcweir 	OSL_ASSERT(cn);
1098*cdf0e10cSrcweir 	if (cn == NULL)
1099*cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
1100*cdf0e10cSrcweir 
1101*cdf0e10cSrcweir 	strcpy(cn, he->h_name);
1102*cdf0e10cSrcweir 
1103*cdf0e10cSrcweir #if 0 // YD 17/04/06 win32 doesn't it.
1104*cdf0e10cSrcweir 	if (_osl_isFullQualifiedDomainName(he->h_name))
1105*cdf0e10cSrcweir 	{
1106*cdf0e10cSrcweir 		cn= (sal_Char *)malloc(strlen (he->h_name) + 1);
1107*cdf0e10cSrcweir 		OSL_ASSERT(cn);
1108*cdf0e10cSrcweir 		if (cn == NULL)
1109*cdf0e10cSrcweir 			return ((oslHostAddr)NULL);
1110*cdf0e10cSrcweir 
1111*cdf0e10cSrcweir 		strcpy(cn, he->h_name);
1112*cdf0e10cSrcweir 	}
1113*cdf0e10cSrcweir 	else
1114*cdf0e10cSrcweir 	{
1115*cdf0e10cSrcweir 		cn =_osl_getFullQualifiedDomainName (he->h_name);
1116*cdf0e10cSrcweir 		OSL_ASSERT(cn);
1117*cdf0e10cSrcweir 		if (cn == NULL)
1118*cdf0e10cSrcweir 			return ((oslHostAddr)NULL);
1119*cdf0e10cSrcweir 	}
1120*cdf0e10cSrcweir #endif
1121*cdf0e10cSrcweir 
1122*cdf0e10cSrcweir 	pSockAddr = __osl_createSocketAddr();
1123*cdf0e10cSrcweir 	OSL_ASSERT(pSockAddr);
1124*cdf0e10cSrcweir 	if (pSockAddr == NULL)
1125*cdf0e10cSrcweir 	{
1126*cdf0e10cSrcweir 		free(cn);
1127*cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
1128*cdf0e10cSrcweir 	}
1129*cdf0e10cSrcweir 
1130*cdf0e10cSrcweir 	pSockAddr->m_sockaddr.sa_family= he->h_addrtype;
1131*cdf0e10cSrcweir 	if (pSockAddr->m_sockaddr.sa_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet))
1132*cdf0e10cSrcweir 	{
1133*cdf0e10cSrcweir 		struct sockaddr_in *sin= (struct sockaddr_in *)&(pSockAddr->m_sockaddr);
1134*cdf0e10cSrcweir 		memcpy (
1135*cdf0e10cSrcweir 			&(sin->sin_addr.s_addr),
1136*cdf0e10cSrcweir 			he->h_addr_list[0],
1137*cdf0e10cSrcweir 			he->h_length);
1138*cdf0e10cSrcweir 	}
1139*cdf0e10cSrcweir 	else
1140*cdf0e10cSrcweir 	{
1141*cdf0e10cSrcweir 		/* unknown address family */
1142*cdf0e10cSrcweir 		/* future extensions for new families might be implemented here */
1143*cdf0e10cSrcweir 
1144*cdf0e10cSrcweir 		OSL_TRACE("_osl_hostentToHostAddr: unknown address family.\n");
1145*cdf0e10cSrcweir 		OSL_ASSERT(sal_False);
1146*cdf0e10cSrcweir 
1147*cdf0e10cSrcweir 		__osl_destroySocketAddr( pSockAddr );
1148*cdf0e10cSrcweir 		free (cn);
1149*cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
1150*cdf0e10cSrcweir 	}
1151*cdf0e10cSrcweir 
1152*cdf0e10cSrcweir 	pAddr= (oslHostAddr) malloc(sizeof(struct oslHostAddrImpl));
1153*cdf0e10cSrcweir 	OSL_ASSERT(pAddr);
1154*cdf0e10cSrcweir 	if (pAddr == NULL)
1155*cdf0e10cSrcweir 	{
1156*cdf0e10cSrcweir 		__osl_destroySocketAddr( pSockAddr );
1157*cdf0e10cSrcweir 		free (cn);
1158*cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
1159*cdf0e10cSrcweir 	}
1160*cdf0e10cSrcweir 
1161*cdf0e10cSrcweir 	pAddr->pHostName= cn;
1162*cdf0e10cSrcweir 	pAddr->pSockAddr= pSockAddr;
1163*cdf0e10cSrcweir 
1164*cdf0e10cSrcweir 	return pAddr;
1165*cdf0e10cSrcweir }
1166*cdf0e10cSrcweir 
1167*cdf0e10cSrcweir /*****************************************************************************/
1168*cdf0e10cSrcweir /* osl_createHostAddr */
1169*cdf0e10cSrcweir /*****************************************************************************/
1170*cdf0e10cSrcweir oslHostAddr SAL_CALL osl_createHostAddr (
1171*cdf0e10cSrcweir 	rtl_uString        *ustrHostname,
1172*cdf0e10cSrcweir 	const oslSocketAddr Addr)
1173*cdf0e10cSrcweir {
1174*cdf0e10cSrcweir     oslHostAddr HostAddr;
1175*cdf0e10cSrcweir     rtl_String* strHostname=0;
1176*cdf0e10cSrcweir     sal_Char* pszHostName=0;
1177*cdf0e10cSrcweir 
1178*cdf0e10cSrcweir     if ( ustrHostname != 0 )
1179*cdf0e10cSrcweir     {
1180*cdf0e10cSrcweir         rtl_uString2String( &strHostname,
1181*cdf0e10cSrcweir                             rtl_uString_getStr(ustrHostname),
1182*cdf0e10cSrcweir                             rtl_uString_getLength(ustrHostname),
1183*cdf0e10cSrcweir                             RTL_TEXTENCODING_UTF8,
1184*cdf0e10cSrcweir                             OUSTRING_TO_OSTRING_CVTFLAGS );
1185*cdf0e10cSrcweir         pszHostName = rtl_string_getStr(strHostname);
1186*cdf0e10cSrcweir     }
1187*cdf0e10cSrcweir 
1188*cdf0e10cSrcweir     HostAddr = osl_psz_createHostAddr(pszHostName,Addr);
1189*cdf0e10cSrcweir 
1190*cdf0e10cSrcweir     if ( strHostname != 0 )
1191*cdf0e10cSrcweir     {
1192*cdf0e10cSrcweir         rtl_string_release(strHostname);
1193*cdf0e10cSrcweir     }
1194*cdf0e10cSrcweir 
1195*cdf0e10cSrcweir 
1196*cdf0e10cSrcweir     return HostAddr;
1197*cdf0e10cSrcweir }
1198*cdf0e10cSrcweir 
1199*cdf0e10cSrcweir oslHostAddr SAL_CALL osl_psz_createHostAddr (
1200*cdf0e10cSrcweir 	const sal_Char     *pszHostname,
1201*cdf0e10cSrcweir 	const oslSocketAddr pAddr)
1202*cdf0e10cSrcweir {
1203*cdf0e10cSrcweir 	oslHostAddr pHostAddr;
1204*cdf0e10cSrcweir 	sal_Char            *cn;
1205*cdf0e10cSrcweir 
1206*cdf0e10cSrcweir 	OSL_ASSERT(pszHostname && pAddr);
1207*cdf0e10cSrcweir 	if ((pszHostname == NULL) || (pAddr == NULL))
1208*cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
1209*cdf0e10cSrcweir 
1210*cdf0e10cSrcweir 	cn = (sal_Char *)malloc(strlen (pszHostname) + 1);
1211*cdf0e10cSrcweir 	OSL_ASSERT(cn);
1212*cdf0e10cSrcweir 	if (cn == NULL)
1213*cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
1214*cdf0e10cSrcweir 
1215*cdf0e10cSrcweir 	strcpy (cn, pszHostname);
1216*cdf0e10cSrcweir 
1217*cdf0e10cSrcweir 	pHostAddr= (oslHostAddr) malloc(sizeof(struct oslHostAddrImpl));
1218*cdf0e10cSrcweir 	OSL_ASSERT(pHostAddr);
1219*cdf0e10cSrcweir 	if (pAddr == NULL)
1220*cdf0e10cSrcweir 	{
1221*cdf0e10cSrcweir 		free (cn);
1222*cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
1223*cdf0e10cSrcweir 	}
1224*cdf0e10cSrcweir 
1225*cdf0e10cSrcweir 	pHostAddr->pHostName= cn;
1226*cdf0e10cSrcweir 	pHostAddr->pSockAddr= osl_copySocketAddr( pAddr );
1227*cdf0e10cSrcweir 
1228*cdf0e10cSrcweir 	return pHostAddr;
1229*cdf0e10cSrcweir }
1230*cdf0e10cSrcweir 
1231*cdf0e10cSrcweir /*****************************************************************************/
1232*cdf0e10cSrcweir /* osl_createHostAddrByName */
1233*cdf0e10cSrcweir /*****************************************************************************/
1234*cdf0e10cSrcweir oslHostAddr SAL_CALL osl_createHostAddrByName(rtl_uString *ustrHostname)
1235*cdf0e10cSrcweir {
1236*cdf0e10cSrcweir     oslHostAddr HostAddr;
1237*cdf0e10cSrcweir     rtl_String* strHostname=0;
1238*cdf0e10cSrcweir     sal_Char* pszHostName=0;
1239*cdf0e10cSrcweir 
1240*cdf0e10cSrcweir     if ( ustrHostname != 0 )
1241*cdf0e10cSrcweir     {
1242*cdf0e10cSrcweir         rtl_uString2String( &strHostname,
1243*cdf0e10cSrcweir                             rtl_uString_getStr(ustrHostname),
1244*cdf0e10cSrcweir                             rtl_uString_getLength(ustrHostname),
1245*cdf0e10cSrcweir                             RTL_TEXTENCODING_UTF8,
1246*cdf0e10cSrcweir                             OUSTRING_TO_OSTRING_CVTFLAGS );
1247*cdf0e10cSrcweir         pszHostName=rtl_string_getStr(strHostname);
1248*cdf0e10cSrcweir     }
1249*cdf0e10cSrcweir 
1250*cdf0e10cSrcweir     HostAddr = osl_psz_createHostAddrByName(pszHostName);
1251*cdf0e10cSrcweir 
1252*cdf0e10cSrcweir     if ( strHostname != 0 )
1253*cdf0e10cSrcweir     {
1254*cdf0e10cSrcweir         rtl_string_release(strHostname);
1255*cdf0e10cSrcweir     }
1256*cdf0e10cSrcweir 
1257*cdf0e10cSrcweir     return HostAddr;
1258*cdf0e10cSrcweir }
1259*cdf0e10cSrcweir 
1260*cdf0e10cSrcweir oslHostAddr SAL_CALL osl_psz_createHostAddrByName (const sal_Char *pszHostname)
1261*cdf0e10cSrcweir {
1262*cdf0e10cSrcweir 	struct hostent *he;
1263*cdf0e10cSrcweir         oslHostAddr	addr;
1264*cdf0e10cSrcweir 
1265*cdf0e10cSrcweir 	static oslMutex mutex = NULL;
1266*cdf0e10cSrcweir 
1267*cdf0e10cSrcweir 	if (mutex == NULL)
1268*cdf0e10cSrcweir 		mutex = osl_createMutex();
1269*cdf0e10cSrcweir 
1270*cdf0e10cSrcweir 	osl_acquireMutex(mutex);
1271*cdf0e10cSrcweir 
1272*cdf0e10cSrcweir 	he = gethostbyname((sal_Char *)pszHostname);
1273*cdf0e10cSrcweir 	addr = _osl_hostentToHostAddr (he);
1274*cdf0e10cSrcweir 
1275*cdf0e10cSrcweir 	osl_releaseMutex(mutex);
1276*cdf0e10cSrcweir 
1277*cdf0e10cSrcweir 	return addr;
1278*cdf0e10cSrcweir }
1279*cdf0e10cSrcweir 
1280*cdf0e10cSrcweir /*****************************************************************************/
1281*cdf0e10cSrcweir /* osl_createHostAddrByAddr */
1282*cdf0e10cSrcweir /*****************************************************************************/
1283*cdf0e10cSrcweir oslHostAddr SAL_CALL osl_createHostAddrByAddr (const oslSocketAddr pAddr)
1284*cdf0e10cSrcweir {
1285*cdf0e10cSrcweir 	OSL_ASSERT(pAddr);
1286*cdf0e10cSrcweir 
1287*cdf0e10cSrcweir 	if (pAddr == NULL)
1288*cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
1289*cdf0e10cSrcweir 
1290*cdf0e10cSrcweir 	if (pAddr->m_sockaddr.sa_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet))
1291*cdf0e10cSrcweir 	{
1292*cdf0e10cSrcweir 		const struct sockaddr_in *sin= (const struct sockaddr_in *)&(pAddr->m_sockaddr);
1293*cdf0e10cSrcweir 		struct hostent *he;
1294*cdf0e10cSrcweir 
1295*cdf0e10cSrcweir 		if (sin->sin_addr.s_addr == htonl(INADDR_ANY))
1296*cdf0e10cSrcweir 			return ((oslHostAddr)NULL);
1297*cdf0e10cSrcweir 
1298*cdf0e10cSrcweir 		he= gethostbyaddr((sal_Char *)&(sin->sin_addr),
1299*cdf0e10cSrcweir 						  sizeof (sin->sin_addr),
1300*cdf0e10cSrcweir 						  sin->sin_family);
1301*cdf0e10cSrcweir 		return _osl_hostentToHostAddr (he);
1302*cdf0e10cSrcweir 	}
1303*cdf0e10cSrcweir 
1304*cdf0e10cSrcweir 	return ((oslHostAddr)NULL);
1305*cdf0e10cSrcweir }
1306*cdf0e10cSrcweir 
1307*cdf0e10cSrcweir /*****************************************************************************/
1308*cdf0e10cSrcweir /* osl_copyHostAddr */
1309*cdf0e10cSrcweir /*****************************************************************************/
1310*cdf0e10cSrcweir oslHostAddr SAL_CALL osl_copyHostAddr (const oslHostAddr pAddr)
1311*cdf0e10cSrcweir {
1312*cdf0e10cSrcweir 	OSL_ASSERT(pAddr);
1313*cdf0e10cSrcweir 
1314*cdf0e10cSrcweir 	if (pAddr)
1315*cdf0e10cSrcweir 		return osl_psz_createHostAddr (pAddr->pHostName, pAddr->pSockAddr);
1316*cdf0e10cSrcweir 	else
1317*cdf0e10cSrcweir 		return ((oslHostAddr)NULL);
1318*cdf0e10cSrcweir }
1319*cdf0e10cSrcweir 
1320*cdf0e10cSrcweir /*****************************************************************************/
1321*cdf0e10cSrcweir /* osl_getHostnameOfHostAddr */
1322*cdf0e10cSrcweir /*****************************************************************************/
1323*cdf0e10cSrcweir void SAL_CALL osl_getHostnameOfHostAddr (
1324*cdf0e10cSrcweir 	const oslHostAddr   Addr,
1325*cdf0e10cSrcweir 	rtl_uString       **ustrHostname)
1326*cdf0e10cSrcweir {
1327*cdf0e10cSrcweir     const sal_Char* pHostname=0;
1328*cdf0e10cSrcweir 
1329*cdf0e10cSrcweir     pHostname = osl_psz_getHostnameOfHostAddr(Addr);
1330*cdf0e10cSrcweir 
1331*cdf0e10cSrcweir     rtl_uString_newFromAscii (ustrHostname, pHostname);
1332*cdf0e10cSrcweir 
1333*cdf0e10cSrcweir     return;
1334*cdf0e10cSrcweir }
1335*cdf0e10cSrcweir 
1336*cdf0e10cSrcweir const sal_Char* SAL_CALL osl_psz_getHostnameOfHostAddr (const oslHostAddr pAddr)
1337*cdf0e10cSrcweir {
1338*cdf0e10cSrcweir 	OSL_ASSERT(pAddr);
1339*cdf0e10cSrcweir 
1340*cdf0e10cSrcweir 	if (pAddr)
1341*cdf0e10cSrcweir 		return pAddr->pHostName;
1342*cdf0e10cSrcweir 	else
1343*cdf0e10cSrcweir 		return NULL;
1344*cdf0e10cSrcweir }
1345*cdf0e10cSrcweir 
1346*cdf0e10cSrcweir /*****************************************************************************/
1347*cdf0e10cSrcweir /* osl_getSocketAddrOfHostAddr */
1348*cdf0e10cSrcweir /*****************************************************************************/
1349*cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_getSocketAddrOfHostAddr (const oslHostAddr pAddr)
1350*cdf0e10cSrcweir {
1351*cdf0e10cSrcweir 	OSL_ASSERT(pAddr);
1352*cdf0e10cSrcweir 
1353*cdf0e10cSrcweir 	if (pAddr)
1354*cdf0e10cSrcweir 		return ((oslSocketAddr)(pAddr->pSockAddr));
1355*cdf0e10cSrcweir 	else
1356*cdf0e10cSrcweir 		return NULL;
1357*cdf0e10cSrcweir }
1358*cdf0e10cSrcweir 
1359*cdf0e10cSrcweir /*****************************************************************************/
1360*cdf0e10cSrcweir /* osl_destroyHostAddr */
1361*cdf0e10cSrcweir /*****************************************************************************/
1362*cdf0e10cSrcweir void SAL_CALL osl_destroyHostAddr (oslHostAddr pAddr)
1363*cdf0e10cSrcweir {
1364*cdf0e10cSrcweir 	if (pAddr)
1365*cdf0e10cSrcweir 	{
1366*cdf0e10cSrcweir 		if (pAddr->pHostName)
1367*cdf0e10cSrcweir 			free (pAddr->pHostName);
1368*cdf0e10cSrcweir 		if (pAddr->pSockAddr)
1369*cdf0e10cSrcweir 			osl_destroySocketAddr (pAddr->pSockAddr);
1370*cdf0e10cSrcweir 		free (pAddr);
1371*cdf0e10cSrcweir 	}
1372*cdf0e10cSrcweir }
1373*cdf0e10cSrcweir 
1374*cdf0e10cSrcweir /*****************************************************************************/
1375*cdf0e10cSrcweir /* osl_getLocalHostname */
1376*cdf0e10cSrcweir /*****************************************************************************/
1377*cdf0e10cSrcweir oslSocketResult SAL_CALL osl_getLocalHostname(rtl_uString **ustrLocalHostname)
1378*cdf0e10cSrcweir {
1379*cdf0e10cSrcweir     oslSocketResult Result;
1380*cdf0e10cSrcweir     sal_Char pszHostname[1024];
1381*cdf0e10cSrcweir 
1382*cdf0e10cSrcweir     pszHostname[0] = '\0';
1383*cdf0e10cSrcweir 
1384*cdf0e10cSrcweir     Result = osl_psz_getLocalHostname(pszHostname,sizeof(pszHostname));
1385*cdf0e10cSrcweir 
1386*cdf0e10cSrcweir     rtl_uString_newFromAscii(ustrLocalHostname,pszHostname);
1387*cdf0e10cSrcweir 
1388*cdf0e10cSrcweir     return Result;
1389*cdf0e10cSrcweir }
1390*cdf0e10cSrcweir 
1391*cdf0e10cSrcweir oslSocketResult SAL_CALL osl_psz_getLocalHostname (
1392*cdf0e10cSrcweir 	sal_Char *pBuffer, sal_uInt32 nBufLen)
1393*cdf0e10cSrcweir {
1394*cdf0e10cSrcweir 	static sal_Char LocalHostname[256] = "";
1395*cdf0e10cSrcweir 
1396*cdf0e10cSrcweir 	if (strlen(LocalHostname) == 0)
1397*cdf0e10cSrcweir 	{
1398*cdf0e10cSrcweir 		const sal_Char *pStr;
1399*cdf0e10cSrcweir 
1400*cdf0e10cSrcweir #ifdef SYSV
1401*cdf0e10cSrcweir 		struct utsname uts;
1402*cdf0e10cSrcweir 
1403*cdf0e10cSrcweir 		if (uname(&uts) < 0)
1404*cdf0e10cSrcweir 			return osl_Socket_Error;
1405*cdf0e10cSrcweir 
1406*cdf0e10cSrcweir 		if ((strlen(uts.nodename) + 1) > nBufLen)
1407*cdf0e10cSrcweir 			return osl_Socket_Error;
1408*cdf0e10cSrcweir 
1409*cdf0e10cSrcweir 		strncpy(LocalHostname, uts.nodename, sizeof( LocalHostname ));
1410*cdf0e10cSrcweir #else  /* BSD compatible */
1411*cdf0e10cSrcweir 
1412*cdf0e10cSrcweir 		if (gethostname(LocalHostname, sizeof(LocalHostname)-1) != 0)
1413*cdf0e10cSrcweir 			return osl_Socket_Error;
1414*cdf0e10cSrcweir         LocalHostname[sizeof(LocalHostname)-1] = 0;
1415*cdf0e10cSrcweir #endif /* SYSV */
1416*cdf0e10cSrcweir 
1417*cdf0e10cSrcweir 		/* check if we have an FQDN */
1418*cdf0e10cSrcweir     	if (strchr(LocalHostname, '.') == NULL)
1419*cdf0e10cSrcweir         {
1420*cdf0e10cSrcweir 			oslHostAddr Addr;
1421*cdf0e10cSrcweir 
1422*cdf0e10cSrcweir 			/* no, determine it via dns */
1423*cdf0e10cSrcweir 			Addr = osl_psz_createHostAddrByName(LocalHostname);
1424*cdf0e10cSrcweir 
1425*cdf0e10cSrcweir 			if (Addr && (pStr = osl_psz_getHostnameOfHostAddr(Addr)) != NULL)
1426*cdf0e10cSrcweir 			{
1427*cdf0e10cSrcweir #if 0  /* OBSOLETE */
1428*cdf0e10cSrcweir 				sal_Char* pChr;
1429*cdf0e10cSrcweir #endif /* OBSOLETE */
1430*cdf0e10cSrcweir 				strcpy(LocalHostname, pStr);
1431*cdf0e10cSrcweir 
1432*cdf0e10cSrcweir #if 0  /* OBSOLETE */
1433*cdf0e10cSrcweir 				/* already done by _osl_getFullQualifiedDomainName() with
1434*cdf0e10cSrcweir 				   much better heuristics, so this may be contraproductive */
1435*cdf0e10cSrcweir 
1436*cdf0e10cSrcweir 				/* no FQDN, last try append domain name */
1437*cdf0e10cSrcweir 		    	if ((pChr = strchr(LocalHostname, '.')) == NULL)
1438*cdf0e10cSrcweir 		        {
1439*cdf0e10cSrcweir                     FILE *fp;
1440*cdf0e10cSrcweir 
1441*cdf0e10cSrcweir 					pChr = &LocalHostname[strlen(LocalHostname)];
1442*cdf0e10cSrcweir 
1443*cdf0e10cSrcweir                     if ( (fp = popen("domainname", "r")) != 0 )
1444*cdf0e10cSrcweir                     {
1445*cdf0e10cSrcweir                         int c;
1446*cdf0e10cSrcweir 
1447*cdf0e10cSrcweir 						*pChr++ = '.';
1448*cdf0e10cSrcweir 
1449*cdf0e10cSrcweir                         while ((c = getc(fp)) != EOF)
1450*cdf0e10cSrcweir                         {
1451*cdf0e10cSrcweir                             if (! isspace(c))
1452*cdf0e10cSrcweir                             	*pChr++ = (sal_Char)c;
1453*cdf0e10cSrcweir                         }
1454*cdf0e10cSrcweir 
1455*cdf0e10cSrcweir                         *pChr = '\0';
1456*cdf0e10cSrcweir 
1457*cdf0e10cSrcweir                         fclose(fp);
1458*cdf0e10cSrcweir                     }
1459*cdf0e10cSrcweir                     else
1460*cdf0e10cSrcweir 						LocalHostname[0] = '\0';
1461*cdf0e10cSrcweir 				}
1462*cdf0e10cSrcweir #endif /* OBSOLETE */
1463*cdf0e10cSrcweir 
1464*cdf0e10cSrcweir 			}
1465*cdf0e10cSrcweir 			if (Addr)
1466*cdf0e10cSrcweir 				osl_destroyHostAddr(Addr);
1467*cdf0e10cSrcweir 		}
1468*cdf0e10cSrcweir 	}
1469*cdf0e10cSrcweir 
1470*cdf0e10cSrcweir 	if (strlen(LocalHostname) > 0)
1471*cdf0e10cSrcweir 	{
1472*cdf0e10cSrcweir 		strncpy(pBuffer, LocalHostname, nBufLen);
1473*cdf0e10cSrcweir 		pBuffer[nBufLen - 1] = '\0';
1474*cdf0e10cSrcweir 
1475*cdf0e10cSrcweir 		return osl_Socket_Ok;
1476*cdf0e10cSrcweir 	}
1477*cdf0e10cSrcweir 
1478*cdf0e10cSrcweir 	return osl_Socket_Error;
1479*cdf0e10cSrcweir }
1480*cdf0e10cSrcweir 
1481*cdf0e10cSrcweir /*****************************************************************************/
1482*cdf0e10cSrcweir /* osl_resolveHostname */
1483*cdf0e10cSrcweir /*****************************************************************************/
1484*cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_resolveHostname(rtl_uString *ustrHostname)
1485*cdf0e10cSrcweir {
1486*cdf0e10cSrcweir     oslSocketAddr Addr;
1487*cdf0e10cSrcweir     rtl_String* strHostname=0;
1488*cdf0e10cSrcweir     sal_Char* pszHostName=0;
1489*cdf0e10cSrcweir 
1490*cdf0e10cSrcweir     if ( ustrHostname != 0 )
1491*cdf0e10cSrcweir     {
1492*cdf0e10cSrcweir         rtl_uString2String( &strHostname,
1493*cdf0e10cSrcweir                             rtl_uString_getStr(ustrHostname),
1494*cdf0e10cSrcweir                             rtl_uString_getLength(ustrHostname),
1495*cdf0e10cSrcweir                             RTL_TEXTENCODING_UTF8,
1496*cdf0e10cSrcweir                             OUSTRING_TO_OSTRING_CVTFLAGS );
1497*cdf0e10cSrcweir         pszHostName = rtl_string_getStr(strHostname);
1498*cdf0e10cSrcweir     }
1499*cdf0e10cSrcweir 
1500*cdf0e10cSrcweir 
1501*cdf0e10cSrcweir     Addr = osl_psz_resolveHostname(pszHostName);
1502*cdf0e10cSrcweir 
1503*cdf0e10cSrcweir     if ( strHostname != 0 )
1504*cdf0e10cSrcweir     {
1505*cdf0e10cSrcweir         rtl_string_release(strHostname);
1506*cdf0e10cSrcweir     }
1507*cdf0e10cSrcweir 
1508*cdf0e10cSrcweir 
1509*cdf0e10cSrcweir     return Addr;
1510*cdf0e10cSrcweir }
1511*cdf0e10cSrcweir 
1512*cdf0e10cSrcweir 
1513*cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_psz_resolveHostname(const sal_Char* pszHostname)
1514*cdf0e10cSrcweir {
1515*cdf0e10cSrcweir 	struct oslHostAddrImpl *pAddr = (oslHostAddr)osl_psz_createHostAddrByName(pszHostname);
1516*cdf0e10cSrcweir 
1517*cdf0e10cSrcweir 	if (pAddr)
1518*cdf0e10cSrcweir 	{
1519*cdf0e10cSrcweir 		oslSocketAddr SockAddr = osl_copySocketAddr(pAddr->pSockAddr);
1520*cdf0e10cSrcweir 
1521*cdf0e10cSrcweir 		osl_destroyHostAddr(pAddr);
1522*cdf0e10cSrcweir 
1523*cdf0e10cSrcweir 		return (SockAddr);
1524*cdf0e10cSrcweir 	}
1525*cdf0e10cSrcweir 
1526*cdf0e10cSrcweir 	return ((oslSocketAddr)NULL);
1527*cdf0e10cSrcweir }
1528*cdf0e10cSrcweir 
1529*cdf0e10cSrcweir /*****************************************************************************/
1530*cdf0e10cSrcweir /* osl_getServicePort */
1531*cdf0e10cSrcweir /*****************************************************************************/
1532*cdf0e10cSrcweir sal_Int32 SAL_CALL osl_getServicePort(rtl_uString *ustrServicename, rtl_uString *ustrProtocol)
1533*cdf0e10cSrcweir {
1534*cdf0e10cSrcweir     sal_Int32 nPort;
1535*cdf0e10cSrcweir     rtl_String* strServicename=0;
1536*cdf0e10cSrcweir     rtl_String* strProtocol=0;
1537*cdf0e10cSrcweir     sal_Char* pszServiceName=0;
1538*cdf0e10cSrcweir     sal_Char* pszProtocol=0;
1539*cdf0e10cSrcweir 
1540*cdf0e10cSrcweir     if ( ustrServicename != 0 )
1541*cdf0e10cSrcweir     {
1542*cdf0e10cSrcweir         rtl_uString2String( &strServicename,
1543*cdf0e10cSrcweir                             rtl_uString_getStr(ustrServicename),
1544*cdf0e10cSrcweir                             rtl_uString_getLength(ustrServicename),
1545*cdf0e10cSrcweir                             RTL_TEXTENCODING_UTF8,
1546*cdf0e10cSrcweir                             OUSTRING_TO_OSTRING_CVTFLAGS );
1547*cdf0e10cSrcweir         pszServiceName = rtl_string_getStr(strServicename);
1548*cdf0e10cSrcweir     }
1549*cdf0e10cSrcweir 
1550*cdf0e10cSrcweir     if ( ustrProtocol != 0 )
1551*cdf0e10cSrcweir     {
1552*cdf0e10cSrcweir         rtl_uString2String( &strProtocol,
1553*cdf0e10cSrcweir                             rtl_uString_getStr(ustrProtocol),
1554*cdf0e10cSrcweir                             rtl_uString_getLength(ustrProtocol),
1555*cdf0e10cSrcweir                             RTL_TEXTENCODING_UTF8,
1556*cdf0e10cSrcweir                             OUSTRING_TO_OSTRING_CVTFLAGS );
1557*cdf0e10cSrcweir         pszProtocol = rtl_string_getStr(strProtocol);
1558*cdf0e10cSrcweir     }
1559*cdf0e10cSrcweir 
1560*cdf0e10cSrcweir     nPort = osl_psz_getServicePort(pszServiceName,pszProtocol);
1561*cdf0e10cSrcweir 
1562*cdf0e10cSrcweir     if ( strServicename != 0 )
1563*cdf0e10cSrcweir     {
1564*cdf0e10cSrcweir         rtl_string_release(strServicename);
1565*cdf0e10cSrcweir     }
1566*cdf0e10cSrcweir 
1567*cdf0e10cSrcweir     if ( strProtocol != 0 )
1568*cdf0e10cSrcweir     {
1569*cdf0e10cSrcweir         rtl_string_release(strProtocol);
1570*cdf0e10cSrcweir     }
1571*cdf0e10cSrcweir 
1572*cdf0e10cSrcweir 
1573*cdf0e10cSrcweir     return nPort;
1574*cdf0e10cSrcweir }
1575*cdf0e10cSrcweir 
1576*cdf0e10cSrcweir 
1577*cdf0e10cSrcweir sal_Int32 SAL_CALL osl_psz_getServicePort(const sal_Char* pszServicename,
1578*cdf0e10cSrcweir 						const sal_Char* pszProtocol)
1579*cdf0e10cSrcweir {
1580*cdf0e10cSrcweir 	struct servent* ps;
1581*cdf0e10cSrcweir 
1582*cdf0e10cSrcweir 	ps= getservbyname(pszServicename, pszProtocol);
1583*cdf0e10cSrcweir 
1584*cdf0e10cSrcweir 	if (ps != 0)
1585*cdf0e10cSrcweir 		return ntohs(ps->s_port);
1586*cdf0e10cSrcweir 
1587*cdf0e10cSrcweir 	return OSL_INVALID_PORT;
1588*cdf0e10cSrcweir }
1589*cdf0e10cSrcweir 
1590*cdf0e10cSrcweir /*****************************************************************************/
1591*cdf0e10cSrcweir /* osl_destroySocketAddr */
1592*cdf0e10cSrcweir /*****************************************************************************/
1593*cdf0e10cSrcweir void SAL_CALL osl_destroySocketAddr(oslSocketAddr pAddr)
1594*cdf0e10cSrcweir {
1595*cdf0e10cSrcweir 	__osl_destroySocketAddr( pAddr );
1596*cdf0e10cSrcweir }
1597*cdf0e10cSrcweir 
1598*cdf0e10cSrcweir /*****************************************************************************/
1599*cdf0e10cSrcweir /* osl_getFamilyOfSocketAddr */
1600*cdf0e10cSrcweir /*****************************************************************************/
1601*cdf0e10cSrcweir oslAddrFamily SAL_CALL osl_getFamilyOfSocketAddr(oslSocketAddr pAddr)
1602*cdf0e10cSrcweir {
1603*cdf0e10cSrcweir 	OSL_ASSERT(pAddr);
1604*cdf0e10cSrcweir 
1605*cdf0e10cSrcweir 	if (pAddr)
1606*cdf0e10cSrcweir 		return FAMILY_FROM_NATIVE(pAddr->m_sockaddr.sa_family);
1607*cdf0e10cSrcweir 	else
1608*cdf0e10cSrcweir 		return osl_Socket_FamilyInvalid;
1609*cdf0e10cSrcweir }
1610*cdf0e10cSrcweir 
1611*cdf0e10cSrcweir /*****************************************************************************/
1612*cdf0e10cSrcweir /* osl_getInetPortOfSocketAddr */
1613*cdf0e10cSrcweir /*****************************************************************************/
1614*cdf0e10cSrcweir sal_Int32 SAL_CALL osl_getInetPortOfSocketAddr(oslSocketAddr pAddr)
1615*cdf0e10cSrcweir {
1616*cdf0e10cSrcweir 	OSL_ASSERT(pAddr);
1617*cdf0e10cSrcweir 	if( pAddr )
1618*cdf0e10cSrcweir 	{
1619*cdf0e10cSrcweir 		struct sockaddr_in* pSystemInetAddr= (struct sockaddr_in*)&(pAddr->m_sockaddr);
1620*cdf0e10cSrcweir 
1621*cdf0e10cSrcweir 		if ( pSystemInetAddr->sin_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet))
1622*cdf0e10cSrcweir 			return ntohs(pSystemInetAddr->sin_port);
1623*cdf0e10cSrcweir 	}
1624*cdf0e10cSrcweir 	return OSL_INVALID_PORT;
1625*cdf0e10cSrcweir }
1626*cdf0e10cSrcweir 
1627*cdf0e10cSrcweir /*****************************************************************************/
1628*cdf0e10cSrcweir /* osl_setInetPortOfSocketAddr */
1629*cdf0e10cSrcweir /*****************************************************************************/
1630*cdf0e10cSrcweir sal_Bool SAL_CALL osl_setInetPortOfSocketAddr(oslSocketAddr pAddr, sal_Int32 Port)
1631*cdf0e10cSrcweir {
1632*cdf0e10cSrcweir 	OSL_ASSERT(pAddr);
1633*cdf0e10cSrcweir 	if( pAddr )
1634*cdf0e10cSrcweir 	{
1635*cdf0e10cSrcweir 		struct sockaddr_in* pSystemInetAddr= (struct sockaddr_in*)&(pAddr->m_sockaddr);
1636*cdf0e10cSrcweir 		if ( pSystemInetAddr->sin_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet))
1637*cdf0e10cSrcweir 		{
1638*cdf0e10cSrcweir 			pSystemInetAddr->sin_port= htons((short)Port);
1639*cdf0e10cSrcweir 			return sal_True;
1640*cdf0e10cSrcweir 		}
1641*cdf0e10cSrcweir 	}
1642*cdf0e10cSrcweir 
1643*cdf0e10cSrcweir 	/* this is not a inet-addr => can't set port */
1644*cdf0e10cSrcweir 	return sal_False;
1645*cdf0e10cSrcweir }
1646*cdf0e10cSrcweir 
1647*cdf0e10cSrcweir /*****************************************************************************/
1648*cdf0e10cSrcweir /* osl_getHostnameOfSocketAddr */
1649*cdf0e10cSrcweir /*****************************************************************************/
1650*cdf0e10cSrcweir oslSocketResult	SAL_CALL osl_getHostnameOfSocketAddr(oslSocketAddr Addr, rtl_uString **ustrHostname)
1651*cdf0e10cSrcweir {
1652*cdf0e10cSrcweir     oslSocketResult Result;
1653*cdf0e10cSrcweir     sal_Char pszHostname[1024];
1654*cdf0e10cSrcweir 
1655*cdf0e10cSrcweir     pszHostname[0] = '\0';
1656*cdf0e10cSrcweir 
1657*cdf0e10cSrcweir     Result = osl_psz_getHostnameOfSocketAddr(Addr,pszHostname,sizeof(pszHostname));
1658*cdf0e10cSrcweir 
1659*cdf0e10cSrcweir     rtl_uString_newFromAscii(ustrHostname,pszHostname);
1660*cdf0e10cSrcweir 
1661*cdf0e10cSrcweir     return Result;
1662*cdf0e10cSrcweir }
1663*cdf0e10cSrcweir 
1664*cdf0e10cSrcweir 
1665*cdf0e10cSrcweir oslSocketResult SAL_CALL osl_psz_getHostnameOfSocketAddr(oslSocketAddr pAddr,
1666*cdf0e10cSrcweir                                             sal_Char *pBuffer, sal_uInt32 BufferSize)
1667*cdf0e10cSrcweir {
1668*cdf0e10cSrcweir 	oslHostAddr pHostAddr= (oslHostAddr )osl_createHostAddrByAddr(pAddr);
1669*cdf0e10cSrcweir 
1670*cdf0e10cSrcweir 	if (pHostAddr)
1671*cdf0e10cSrcweir 	{
1672*cdf0e10cSrcweir 		strncpy(pBuffer, pHostAddr->pHostName, BufferSize);
1673*cdf0e10cSrcweir 
1674*cdf0e10cSrcweir 		pBuffer[BufferSize - 1] = '\0';
1675*cdf0e10cSrcweir 
1676*cdf0e10cSrcweir 		osl_destroyHostAddr(pHostAddr);
1677*cdf0e10cSrcweir 
1678*cdf0e10cSrcweir 		return osl_Socket_Ok;
1679*cdf0e10cSrcweir 	}
1680*cdf0e10cSrcweir 
1681*cdf0e10cSrcweir 	return osl_Socket_Error;
1682*cdf0e10cSrcweir }
1683*cdf0e10cSrcweir 
1684*cdf0e10cSrcweir /*****************************************************************************/
1685*cdf0e10cSrcweir /* osl_getDottedInetAddrOfSocketAddr */
1686*cdf0e10cSrcweir /*****************************************************************************/
1687*cdf0e10cSrcweir oslSocketResult	SAL_CALL osl_getDottedInetAddrOfSocketAddr(oslSocketAddr Addr, rtl_uString **ustrDottedInetAddr)
1688*cdf0e10cSrcweir {
1689*cdf0e10cSrcweir     oslSocketResult Result;
1690*cdf0e10cSrcweir     sal_Char pszDottedInetAddr[1024];
1691*cdf0e10cSrcweir 
1692*cdf0e10cSrcweir     pszDottedInetAddr[0] = '\0';
1693*cdf0e10cSrcweir 
1694*cdf0e10cSrcweir     Result = osl_psz_getDottedInetAddrOfSocketAddr(Addr,pszDottedInetAddr,sizeof(pszDottedInetAddr));
1695*cdf0e10cSrcweir 
1696*cdf0e10cSrcweir     rtl_uString_newFromAscii(ustrDottedInetAddr,pszDottedInetAddr);
1697*cdf0e10cSrcweir 
1698*cdf0e10cSrcweir     return Result;
1699*cdf0e10cSrcweir 
1700*cdf0e10cSrcweir }
1701*cdf0e10cSrcweir 
1702*cdf0e10cSrcweir oslSocketResult	SAL_CALL osl_psz_getDottedInetAddrOfSocketAddr(oslSocketAddr pAddr,
1703*cdf0e10cSrcweir 												  sal_Char *pBuffer, sal_uInt32 BufferSize)
1704*cdf0e10cSrcweir {
1705*cdf0e10cSrcweir 	OSL_ASSERT(pAddr);
1706*cdf0e10cSrcweir 
1707*cdf0e10cSrcweir 	if( pAddr )
1708*cdf0e10cSrcweir 	{
1709*cdf0e10cSrcweir 		struct sockaddr_in* pSystemInetAddr = ( struct sockaddr_in * ) &(pAddr->m_sockaddr);
1710*cdf0e10cSrcweir 
1711*cdf0e10cSrcweir 		if (pSystemInetAddr->sin_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet))
1712*cdf0e10cSrcweir 		{
1713*cdf0e10cSrcweir 			strncpy(pBuffer, inet_ntoa(pSystemInetAddr->sin_addr), BufferSize);
1714*cdf0e10cSrcweir 			pBuffer[BufferSize - 1] = '\0';
1715*cdf0e10cSrcweir 
1716*cdf0e10cSrcweir 			return osl_Socket_Ok;
1717*cdf0e10cSrcweir 		}
1718*cdf0e10cSrcweir 	}
1719*cdf0e10cSrcweir 
1720*cdf0e10cSrcweir 	return osl_Socket_Error;
1721*cdf0e10cSrcweir }
1722*cdf0e10cSrcweir 
1723*cdf0e10cSrcweir #if 0  /* OBSOLETE */
1724*cdf0e10cSrcweir /*****************************************************************************/
1725*cdf0e10cSrcweir /* osl_getIpxNetNumber  */
1726*cdf0e10cSrcweir /*****************************************************************************/
1727*cdf0e10cSrcweir oslSocketResult SAL_CALL osl_getIpxNetNumber(oslSocketAddr Addr,
1728*cdf0e10cSrcweir                                     oslSocketIpxNetNumber NetNumber)
1729*cdf0e10cSrcweir 
1730*cdf0e10cSrcweir {
1731*cdf0e10cSrcweir 	struct sockaddr_ipx* pAddr;
1732*cdf0e10cSrcweir 
1733*cdf0e10cSrcweir 	pAddr= (struct sockaddr_ipx*)Addr;
1734*cdf0e10cSrcweir 
1735*cdf0e10cSrcweir 	OSL_ASSERT(pAddr);
1736*cdf0e10cSrcweir 
1737*cdf0e10cSrcweir 	if (pAddr && (pAddr->sa_family == FAMILY_TO_NATIVE(osl_Socket_FamilyIpx)))
1738*cdf0e10cSrcweir  	{
1739*cdf0e10cSrcweir  		memcpy(NetNumber, pAddr->sa_netnum, sizeof(NetNumber));
1740*cdf0e10cSrcweir 
1741*cdf0e10cSrcweir   		return osl_Socket_Ok;
1742*cdf0e10cSrcweir   	}
1743*cdf0e10cSrcweir   	else
1744*cdf0e10cSrcweir   		return osl_Socket_Error;
1745*cdf0e10cSrcweir }
1746*cdf0e10cSrcweir 
1747*cdf0e10cSrcweir 
1748*cdf0e10cSrcweir /*****************************************************************************/
1749*cdf0e10cSrcweir /* osl_getIpxNodeNumber  */
1750*cdf0e10cSrcweir /*****************************************************************************/
1751*cdf0e10cSrcweir oslSocketResult SAL_CALL osl_getIpxNodeNumber(oslSocketAddr Addr,
1752*cdf0e10cSrcweir                                      oslSocketIpxNodeNumber NodeNumber)
1753*cdf0e10cSrcweir 
1754*cdf0e10cSrcweir {
1755*cdf0e10cSrcweir   	struct sockaddr_ipx* pAddr;
1756*cdf0e10cSrcweir 
1757*cdf0e10cSrcweir   	pAddr= (struct sockaddr_ipx*)Addr;
1758*cdf0e10cSrcweir 
1759*cdf0e10cSrcweir   	OSL_ASSERT(pAddr);
1760*cdf0e10cSrcweir 
1761*cdf0e10cSrcweir   	if (pAddr && (pAddr->sa_family == FAMILY_TO_NATIVE(osl_Socket_FamilyIpx)))
1762*cdf0e10cSrcweir   	{
1763*cdf0e10cSrcweir   		memcpy(NodeNumber, pAddr->sa_nodenum, sizeof(NodeNumber));
1764*cdf0e10cSrcweir 
1765*cdf0e10cSrcweir   		return osl_Socket_Ok;
1766*cdf0e10cSrcweir   	}
1767*cdf0e10cSrcweir   	else
1768*cdf0e10cSrcweir   		return osl_Socket_Error;
1769*cdf0e10cSrcweir }
1770*cdf0e10cSrcweir 
1771*cdf0e10cSrcweir 
1772*cdf0e10cSrcweir /*****************************************************************************/
1773*cdf0e10cSrcweir /* osl_getIpxSocketNumber  */
1774*cdf0e10cSrcweir /*****************************************************************************/
1775*cdf0e10cSrcweir sal_Int32 SAL_CALL osl_getIpxSocketNumber(oslSocketAddr Addr)
1776*cdf0e10cSrcweir {
1777*cdf0e10cSrcweir 	struct sockaddr_ipx* pAddr= (struct sockaddr_ipx*)Addr;
1778*cdf0e10cSrcweir 	OSL_ASSERT(pAddr);
1779*cdf0e10cSrcweir 
1780*cdf0e10cSrcweir  	if (pAddr && (pAddr->sa_family == FAMILY_TO_NATIVE(osl_Socket_FamilyIpx)))
1781*cdf0e10cSrcweir   		return pAddr->sa_socket;
1782*cdf0e10cSrcweir   	else
1783*cdf0e10cSrcweir   		return OSL_INVALID_IPX_SOCKET_NO;
1784*cdf0e10cSrcweir }
1785*cdf0e10cSrcweir 
1786*cdf0e10cSrcweir #endif /* OBSOLETE */
1787*cdf0e10cSrcweir 
1788*cdf0e10cSrcweir /*****************************************************************************/
1789*cdf0e10cSrcweir /* osl_createSocket  */
1790*cdf0e10cSrcweir /*****************************************************************************/
1791*cdf0e10cSrcweir oslSocket SAL_CALL osl_createSocket(oslAddrFamily	Family,
1792*cdf0e10cSrcweir 						   oslSocketType	Type,
1793*cdf0e10cSrcweir 						   oslProtocol		Protocol)
1794*cdf0e10cSrcweir {
1795*cdf0e10cSrcweir 	int 		   Flags;
1796*cdf0e10cSrcweir 	oslSocket pSocket;
1797*cdf0e10cSrcweir 
1798*cdf0e10cSrcweir 	/* alloc memory */
1799*cdf0e10cSrcweir 	pSocket= __osl_createSocketImpl(OSL_INVALID_SOCKET);
1800*cdf0e10cSrcweir 
1801*cdf0e10cSrcweir 	/* create socket */
1802*cdf0e10cSrcweir 	pSocket->m_Socket= socket(FAMILY_TO_NATIVE(Family),
1803*cdf0e10cSrcweir 								TYPE_TO_NATIVE(Type),
1804*cdf0e10cSrcweir 								PROTOCOL_TO_NATIVE(Protocol));
1805*cdf0e10cSrcweir 
1806*cdf0e10cSrcweir 	/* creation failed => free memory */
1807*cdf0e10cSrcweir 	if(pSocket->m_Socket == OSL_INVALID_SOCKET)
1808*cdf0e10cSrcweir 	{
1809*cdf0e10cSrcweir 	    OSL_TRACE("osl_createSocket failed. Errno: %d; %s\n",
1810*cdf0e10cSrcweir 			      errno,
1811*cdf0e10cSrcweir 			      strerror(errno));
1812*cdf0e10cSrcweir 
1813*cdf0e10cSrcweir 		__osl_destroySocketImpl((pSocket));
1814*cdf0e10cSrcweir 		pSocket= 0;
1815*cdf0e10cSrcweir 	}
1816*cdf0e10cSrcweir 	else
1817*cdf0e10cSrcweir 	{
1818*cdf0e10cSrcweir 		/* set close-on-exec flag */
1819*cdf0e10cSrcweir 		if ((Flags = fcntl(pSocket->m_Socket, F_GETFD, 0)) != -1)
1820*cdf0e10cSrcweir 		{
1821*cdf0e10cSrcweir 			Flags |= FD_CLOEXEC;
1822*cdf0e10cSrcweir 			if (fcntl(pSocket->m_Socket, F_SETFD, Flags) == -1)
1823*cdf0e10cSrcweir             {
1824*cdf0e10cSrcweir                 pSocket->m_nLastError=errno;
1825*cdf0e10cSrcweir 				OSL_TRACE("osl_createSocket failed changing socket flags. Errno: %d; %s\n",
1826*cdf0e10cSrcweir                           errno,
1827*cdf0e10cSrcweir                           strerror(errno));
1828*cdf0e10cSrcweir             }
1829*cdf0e10cSrcweir 		}
1830*cdf0e10cSrcweir         else
1831*cdf0e10cSrcweir         {
1832*cdf0e10cSrcweir             pSocket->m_nLastError=errno;
1833*cdf0e10cSrcweir         }
1834*cdf0e10cSrcweir 
1835*cdf0e10cSrcweir 
1836*cdf0e10cSrcweir 		pSocket->m_CloseCallback 	= NULL;
1837*cdf0e10cSrcweir 		pSocket->m_CallbackArg	= NULL;
1838*cdf0e10cSrcweir 	}
1839*cdf0e10cSrcweir 
1840*cdf0e10cSrcweir 	return pSocket;
1841*cdf0e10cSrcweir }
1842*cdf0e10cSrcweir 
1843*cdf0e10cSrcweir void SAL_CALL osl_acquireSocket(oslSocket pSocket)
1844*cdf0e10cSrcweir {
1845*cdf0e10cSrcweir 	osl_incrementInterlockedCount( &(pSocket->m_nRefCount ) );
1846*cdf0e10cSrcweir }
1847*cdf0e10cSrcweir 
1848*cdf0e10cSrcweir void SAL_CALL osl_releaseSocket( oslSocket pSocket )
1849*cdf0e10cSrcweir {
1850*cdf0e10cSrcweir 	if( pSocket && 0 == osl_decrementInterlockedCount( &(pSocket->m_nRefCount) ) )
1851*cdf0e10cSrcweir 	{
1852*cdf0e10cSrcweir #if defined(LINUX)
1853*cdf0e10cSrcweir     if ( pSocket->m_bIsAccepting == sal_True )
1854*cdf0e10cSrcweir     {
1855*cdf0e10cSrcweir         OSL_ENSURE(0, "osl_destroySocket : attempt to destroy socket while accepting\n");
1856*cdf0e10cSrcweir         return;
1857*cdf0e10cSrcweir     }
1858*cdf0e10cSrcweir #endif /* LINUX */
1859*cdf0e10cSrcweir 		osl_closeSocket( pSocket );
1860*cdf0e10cSrcweir 		__osl_destroySocketImpl( pSocket );
1861*cdf0e10cSrcweir 	}
1862*cdf0e10cSrcweir }
1863*cdf0e10cSrcweir 
1864*cdf0e10cSrcweir 
1865*cdf0e10cSrcweir 
1866*cdf0e10cSrcweir /*****************************************************************************/
1867*cdf0e10cSrcweir /* osl_closeSocket  */
1868*cdf0e10cSrcweir /*****************************************************************************/
1869*cdf0e10cSrcweir void SAL_CALL osl_closeSocket(oslSocket pSocket)
1870*cdf0e10cSrcweir {
1871*cdf0e10cSrcweir     int nRet;
1872*cdf0e10cSrcweir     int nFD;
1873*cdf0e10cSrcweir 
1874*cdf0e10cSrcweir     /* socket already invalid */
1875*cdf0e10cSrcweir 	if(pSocket==0)
1876*cdf0e10cSrcweir 		return;
1877*cdf0e10cSrcweir 
1878*cdf0e10cSrcweir     pSocket->m_nLastError=0;
1879*cdf0e10cSrcweir     nFD = pSocket->m_Socket;
1880*cdf0e10cSrcweir 
1881*cdf0e10cSrcweir     pSocket->m_Socket = OSL_INVALID_SOCKET;
1882*cdf0e10cSrcweir 
1883*cdf0e10cSrcweir #if defined(LINUX)
1884*cdf0e10cSrcweir     pSocket->m_bIsInShutdown = sal_True;
1885*cdf0e10cSrcweir 
1886*cdf0e10cSrcweir     if ( pSocket->m_bIsAccepting == sal_True )
1887*cdf0e10cSrcweir     {
1888*cdf0e10cSrcweir         int nConnFD;
1889*cdf0e10cSrcweir         struct sockaddr aSockAddr;
1890*cdf0e10cSrcweir         socklen_t nSockLen = sizeof(aSockAddr);
1891*cdf0e10cSrcweir 
1892*cdf0e10cSrcweir         nRet = getsockname(nFD, &aSockAddr, &nSockLen);
1893*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
1894*cdf0e10cSrcweir         if ( nRet < 0 )
1895*cdf0e10cSrcweir         {
1896*cdf0e10cSrcweir             perror("getsockname");
1897*cdf0e10cSrcweir         }
1898*cdf0e10cSrcweir #endif /* OSL_DEBUG_LEVEL */
1899*cdf0e10cSrcweir 
1900*cdf0e10cSrcweir         if ( aSockAddr.sa_family == AF_INET )
1901*cdf0e10cSrcweir         {
1902*cdf0e10cSrcweir             struct sockaddr_in* pSockAddrIn = (struct sockaddr_in*) &aSockAddr;
1903*cdf0e10cSrcweir 
1904*cdf0e10cSrcweir             if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )
1905*cdf0e10cSrcweir             {
1906*cdf0e10cSrcweir                 pSockAddrIn->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1907*cdf0e10cSrcweir             }
1908*cdf0e10cSrcweir 
1909*cdf0e10cSrcweir             nConnFD = socket(AF_INET, SOCK_STREAM, 0);
1910*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
1911*cdf0e10cSrcweir             if ( nConnFD < 0 )
1912*cdf0e10cSrcweir             {
1913*cdf0e10cSrcweir                 perror("socket");
1914*cdf0e10cSrcweir             }
1915*cdf0e10cSrcweir #endif /* OSL_DEBUG_LEVEL */
1916*cdf0e10cSrcweir 
1917*cdf0e10cSrcweir             nRet = connect(nConnFD, &aSockAddr, sizeof(aSockAddr));
1918*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
1919*cdf0e10cSrcweir             if ( nRet < 0 )
1920*cdf0e10cSrcweir             {
1921*cdf0e10cSrcweir                 perror("connect");
1922*cdf0e10cSrcweir             }
1923*cdf0e10cSrcweir #endif /* OSL_DEBUG_LEVEL */
1924*cdf0e10cSrcweir             close(nConnFD);
1925*cdf0e10cSrcweir         }
1926*cdf0e10cSrcweir     }
1927*cdf0e10cSrcweir #endif /* LINUX */
1928*cdf0e10cSrcweir 
1929*cdf0e10cSrcweir 	/* registrierten Callback ausfuehren */
1930*cdf0e10cSrcweir 	if (pSocket->m_CloseCallback != NULL)
1931*cdf0e10cSrcweir 	{
1932*cdf0e10cSrcweir 		pSocket->m_CloseCallback(pSocket->m_CallbackArg);
1933*cdf0e10cSrcweir 	}
1934*cdf0e10cSrcweir 
1935*cdf0e10cSrcweir     nRet=close(nFD);
1936*cdf0e10cSrcweir     if ( nRet != 0 )
1937*cdf0e10cSrcweir     {
1938*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
1939*cdf0e10cSrcweir         OSL_TRACE("closeSocket close error '%s'\n",strerror(errno));
1940*cdf0e10cSrcweir     }
1941*cdf0e10cSrcweir 
1942*cdf0e10cSrcweir     pSocket->m_Socket = OSL_INVALID_SOCKET;
1943*cdf0e10cSrcweir }
1944*cdf0e10cSrcweir 
1945*cdf0e10cSrcweir /*****************************************************************************/
1946*cdf0e10cSrcweir /* osl_getLocalAddrOfSocket  */
1947*cdf0e10cSrcweir /* Note that I rely on the fact that oslSocketAddr and struct sockaddr */
1948*cdf0e10cSrcweir /* are the same! I don't like it very much but see no other easy way to conceal */
1949*cdf0e10cSrcweir /* the struct sockaddr from the eyes of the user. */
1950*cdf0e10cSrcweir /*****************************************************************************/
1951*cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_getLocalAddrOfSocket(oslSocket pSocket)
1952*cdf0e10cSrcweir {
1953*cdf0e10cSrcweir #if defined(LINUX) || defined(FREEBSD)
1954*cdf0e10cSrcweir 	socklen_t AddrLen;
1955*cdf0e10cSrcweir #else
1956*cdf0e10cSrcweir 	/* mfe: Solaris 'cc +w' means Addrlen should be signed! */
1957*cdf0e10cSrcweir 	/*      it's really defined as 'int*' in /usr/include/sys/socket.h! */
1958*cdf0e10cSrcweir 	/*      the man page says it expects a 'size_t' */
1959*cdf0e10cSrcweir 	int AddrLen;
1960*cdf0e10cSrcweir #endif
1961*cdf0e10cSrcweir 	struct sockaddr Addr;
1962*cdf0e10cSrcweir 	oslSocketAddr  pAddr;
1963*cdf0e10cSrcweir 
1964*cdf0e10cSrcweir 	if (pSocket == NULL) /* ENOTSOCK */
1965*cdf0e10cSrcweir 		return ((oslSocketAddr)NULL);
1966*cdf0e10cSrcweir 
1967*cdf0e10cSrcweir 	AddrLen= sizeof(struct sockaddr);
1968*cdf0e10cSrcweir 
1969*cdf0e10cSrcweir 	if (getsockname(pSocket->m_Socket, &Addr, PTR_SIZE_T(AddrLen)) == OSL_SOCKET_ERROR)
1970*cdf0e10cSrcweir 		return ((oslSocketAddr)NULL);
1971*cdf0e10cSrcweir 
1972*cdf0e10cSrcweir 	pAddr = __osl_createSocketAddrFromSystem( &Addr );
1973*cdf0e10cSrcweir 	return pAddr;
1974*cdf0e10cSrcweir }
1975*cdf0e10cSrcweir 
1976*cdf0e10cSrcweir /*****************************************************************************/
1977*cdf0e10cSrcweir /* osl_getPeerAddrOfSocket  */
1978*cdf0e10cSrcweir /*****************************************************************************/
1979*cdf0e10cSrcweir oslSocketAddr SAL_CALL osl_getPeerAddrOfSocket(oslSocket pSocket)
1980*cdf0e10cSrcweir {
1981*cdf0e10cSrcweir 	sal_uInt32 AddrLen;
1982*cdf0e10cSrcweir 	struct sockaddr Addr;
1983*cdf0e10cSrcweir 
1984*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
1985*cdf0e10cSrcweir 	if ( pSocket == 0 )
1986*cdf0e10cSrcweir 	{
1987*cdf0e10cSrcweir 		return 0;
1988*cdf0e10cSrcweir 	}
1989*cdf0e10cSrcweir 
1990*cdf0e10cSrcweir     pSocket->m_nLastError=0;
1991*cdf0e10cSrcweir 	AddrLen= sizeof(struct sockaddr);
1992*cdf0e10cSrcweir 
1993*cdf0e10cSrcweir 	if(getpeername(pSocket->m_Socket, &Addr, (int*)PTR_SIZE_T(AddrLen)) == OSL_SOCKET_ERROR)
1994*cdf0e10cSrcweir     {
1995*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
1996*cdf0e10cSrcweir 		return 0;
1997*cdf0e10cSrcweir     }
1998*cdf0e10cSrcweir 	return __osl_createSocketAddrFromSystem( &Addr );
1999*cdf0e10cSrcweir }
2000*cdf0e10cSrcweir 
2001*cdf0e10cSrcweir /*****************************************************************************/
2002*cdf0e10cSrcweir /* osl_bindAddrToSocket  */
2003*cdf0e10cSrcweir /*****************************************************************************/
2004*cdf0e10cSrcweir sal_Bool SAL_CALL osl_bindAddrToSocket(oslSocket pSocket,
2005*cdf0e10cSrcweir 							 oslSocketAddr pAddr)
2006*cdf0e10cSrcweir {
2007*cdf0e10cSrcweir     int nRet;
2008*cdf0e10cSrcweir 
2009*cdf0e10cSrcweir 	OSL_ASSERT(pSocket && pAddr );
2010*cdf0e10cSrcweir 	if ( pSocket == 0 || pAddr == 0 )
2011*cdf0e10cSrcweir 	{
2012*cdf0e10cSrcweir 		return sal_False;
2013*cdf0e10cSrcweir 	}
2014*cdf0e10cSrcweir 
2015*cdf0e10cSrcweir     pSocket->m_nLastError=0;
2016*cdf0e10cSrcweir 
2017*cdf0e10cSrcweir     nRet = bind(pSocket->m_Socket, &(pAddr->m_sockaddr), sizeof(struct sockaddr));
2018*cdf0e10cSrcweir 
2019*cdf0e10cSrcweir     if ( nRet == OSL_SOCKET_ERROR)
2020*cdf0e10cSrcweir     {
2021*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
2022*cdf0e10cSrcweir         return sal_False;
2023*cdf0e10cSrcweir     }
2024*cdf0e10cSrcweir 
2025*cdf0e10cSrcweir 	return sal_True;
2026*cdf0e10cSrcweir }
2027*cdf0e10cSrcweir 
2028*cdf0e10cSrcweir 
2029*cdf0e10cSrcweir /*****************************************************************************/
2030*cdf0e10cSrcweir /* osl_listenOnSocket  */
2031*cdf0e10cSrcweir /*****************************************************************************/
2032*cdf0e10cSrcweir sal_Bool SAL_CALL osl_listenOnSocket(oslSocket pSocket,
2033*cdf0e10cSrcweir 						   sal_Int32 MaxPendingConnections)
2034*cdf0e10cSrcweir {
2035*cdf0e10cSrcweir     int nRet;
2036*cdf0e10cSrcweir 
2037*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2038*cdf0e10cSrcweir 	if ( pSocket == 0 )
2039*cdf0e10cSrcweir 	{
2040*cdf0e10cSrcweir 		return sal_False;
2041*cdf0e10cSrcweir 	}
2042*cdf0e10cSrcweir 
2043*cdf0e10cSrcweir     pSocket->m_nLastError=0;
2044*cdf0e10cSrcweir 
2045*cdf0e10cSrcweir     nRet = listen(pSocket->m_Socket,
2046*cdf0e10cSrcweir                   MaxPendingConnections == -1 ?
2047*cdf0e10cSrcweir                   SOMAXCONN :
2048*cdf0e10cSrcweir                   MaxPendingConnections);
2049*cdf0e10cSrcweir     if ( nRet == OSL_SOCKET_ERROR)
2050*cdf0e10cSrcweir     {
2051*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
2052*cdf0e10cSrcweir         return sal_False;
2053*cdf0e10cSrcweir     }
2054*cdf0e10cSrcweir 
2055*cdf0e10cSrcweir     return sal_True;
2056*cdf0e10cSrcweir }
2057*cdf0e10cSrcweir 
2058*cdf0e10cSrcweir 
2059*cdf0e10cSrcweir /*****************************************************************************/
2060*cdf0e10cSrcweir /* osl_connectSocketTo  */
2061*cdf0e10cSrcweir /*****************************************************************************/
2062*cdf0e10cSrcweir oslSocketResult SAL_CALL osl_connectSocketTo(oslSocket pSocket,
2063*cdf0e10cSrcweir 									oslSocketAddr pAddr,
2064*cdf0e10cSrcweir 									const TimeValue* pTimeout)
2065*cdf0e10cSrcweir {
2066*cdf0e10cSrcweir 	fd_set   WriteSet;
2067*cdf0e10cSrcweir 	fd_set   ExcptSet;
2068*cdf0e10cSrcweir 	int      ReadyHandles;
2069*cdf0e10cSrcweir     struct timeval  tv;
2070*cdf0e10cSrcweir 	oslSocketResult Result= osl_Socket_Ok;
2071*cdf0e10cSrcweir 
2072*cdf0e10cSrcweir 	OSL_PRECOND(pSocket, "osl_connectSocketTo(): need a valid socket!\n");
2073*cdf0e10cSrcweir 
2074*cdf0e10cSrcweir     if ( pSocket == 0 )
2075*cdf0e10cSrcweir     {
2076*cdf0e10cSrcweir         return osl_Socket_Error;
2077*cdf0e10cSrcweir     }
2078*cdf0e10cSrcweir 
2079*cdf0e10cSrcweir     pSocket->m_nLastError=0;
2080*cdf0e10cSrcweir 
2081*cdf0e10cSrcweir 	if (osl_isNonBlockingMode(pSocket))
2082*cdf0e10cSrcweir     {
2083*cdf0e10cSrcweir 		if (connect(pSocket->m_Socket,
2084*cdf0e10cSrcweir 				    &(pAddr->m_sockaddr),
2085*cdf0e10cSrcweir 				    sizeof(struct sockaddr)) != OSL_SOCKET_ERROR)
2086*cdf0e10cSrcweir 			return osl_Socket_Ok;
2087*cdf0e10cSrcweir 		else
2088*cdf0e10cSrcweir 			if (errno == EWOULDBLOCK || errno == EINPROGRESS)
2089*cdf0e10cSrcweir             {
2090*cdf0e10cSrcweir                 pSocket->m_nLastError=EINPROGRESS;
2091*cdf0e10cSrcweir 				return osl_Socket_InProgress;
2092*cdf0e10cSrcweir             }
2093*cdf0e10cSrcweir 
2094*cdf0e10cSrcweir 
2095*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
2096*cdf0e10cSrcweir         OSL_TRACE("can't connect : '%s'",strerror(errno));
2097*cdf0e10cSrcweir 		return osl_Socket_Error;
2098*cdf0e10cSrcweir 	}
2099*cdf0e10cSrcweir 
2100*cdf0e10cSrcweir 	/* set socket temporarily to non-blocking */
2101*cdf0e10cSrcweir 	OSL_VERIFY(osl_enableNonBlockingMode(pSocket, sal_True));
2102*cdf0e10cSrcweir 
2103*cdf0e10cSrcweir 	/* initiate connect */
2104*cdf0e10cSrcweir 	if(connect(pSocket->m_Socket,
2105*cdf0e10cSrcweir 			   &(pAddr->m_sockaddr),
2106*cdf0e10cSrcweir 			   sizeof(struct sockaddr)) != OSL_SOCKET_ERROR)
2107*cdf0e10cSrcweir 	{
2108*cdf0e10cSrcweir 	   /* immediate connection */
2109*cdf0e10cSrcweir 		osl_enableNonBlockingMode(pSocket, sal_False);
2110*cdf0e10cSrcweir 
2111*cdf0e10cSrcweir 		return osl_Socket_Ok;
2112*cdf0e10cSrcweir     }
2113*cdf0e10cSrcweir 	else
2114*cdf0e10cSrcweir 	{
2115*cdf0e10cSrcweir 	    /* really an error or just delayed? */
2116*cdf0e10cSrcweir 	    if (errno != EINPROGRESS)
2117*cdf0e10cSrcweir 	    {
2118*cdf0e10cSrcweir             pSocket->m_nLastError=errno;
2119*cdf0e10cSrcweir 			OSL_TRACE(
2120*cdf0e10cSrcweir 				"osl_connectSocketTo(): connect failed: errno: %d (%s)\n",
2121*cdf0e10cSrcweir 				errno, strerror(errno));
2122*cdf0e10cSrcweir 
2123*cdf0e10cSrcweir 			osl_enableNonBlockingMode(pSocket, sal_False);
2124*cdf0e10cSrcweir 			return osl_Socket_Error;
2125*cdf0e10cSrcweir 	    }
2126*cdf0e10cSrcweir 	}
2127*cdf0e10cSrcweir 
2128*cdf0e10cSrcweir 
2129*cdf0e10cSrcweir 	/* prepare select set for socket  */
2130*cdf0e10cSrcweir 	FD_ZERO(&WriteSet);
2131*cdf0e10cSrcweir 	FD_ZERO(&ExcptSet);
2132*cdf0e10cSrcweir 	FD_SET(pSocket->m_Socket, &WriteSet);
2133*cdf0e10cSrcweir 	FD_SET(pSocket->m_Socket, &ExcptSet);
2134*cdf0e10cSrcweir 
2135*cdf0e10cSrcweir 	/* prepare timeout */
2136*cdf0e10cSrcweir 	if (pTimeout)
2137*cdf0e10cSrcweir 	{
2138*cdf0e10cSrcweir 		/* divide milliseconds into seconds and microseconds */
2139*cdf0e10cSrcweir 		tv.tv_sec=	pTimeout->Seconds;
2140*cdf0e10cSrcweir 		tv.tv_usec=	pTimeout->Nanosec / 1000L;
2141*cdf0e10cSrcweir 	}
2142*cdf0e10cSrcweir 
2143*cdf0e10cSrcweir 	/* select */
2144*cdf0e10cSrcweir     ReadyHandles= select(pSocket->m_Socket+1,
2145*cdf0e10cSrcweir 						 0,
2146*cdf0e10cSrcweir 						 PTR_FD_SET(WriteSet),
2147*cdf0e10cSrcweir 						 PTR_FD_SET(ExcptSet),
2148*cdf0e10cSrcweir 						 (pTimeout) ? &tv : 0);
2149*cdf0e10cSrcweir 
2150*cdf0e10cSrcweir 	if (ReadyHandles > 0)  /* connected */
2151*cdf0e10cSrcweir 	{
2152*cdf0e10cSrcweir 		if ( FD_ISSET(pSocket->m_Socket, &WriteSet ) )
2153*cdf0e10cSrcweir 		{
2154*cdf0e10cSrcweir 			int nErrorCode = 0;
2155*cdf0e10cSrcweir #ifdef SOLARIS
2156*cdf0e10cSrcweir /*  mfe: Solaris 'cc +w' means 5th argument should be a 'int*'!
2157*cdf0e10cSrcweir 	     it's really defined as 'int*' in /usr/include/sys/socket.h!
2158*cdf0e10cSrcweir 	     the man page says it expects a 'size_t*'
2159*cdf0e10cSrcweir */
2160*cdf0e10cSrcweir 			int nErrorSize = sizeof( nErrorCode );
2161*cdf0e10cSrcweir #else
2162*cdf0e10cSrcweir 			size_t nErrorSize = sizeof( nErrorCode );
2163*cdf0e10cSrcweir #endif
2164*cdf0e10cSrcweir 
2165*cdf0e10cSrcweir 			int nSockOpt;
2166*cdf0e10cSrcweir 
2167*cdf0e10cSrcweir 			nSockOpt = getsockopt ( pSocket->m_Socket, SOL_SOCKET, SO_ERROR,
2168*cdf0e10cSrcweir #ifdef SOLARIS
2169*cdf0e10cSrcweir /*  mfe: Solaris 'cc +w' means 4th argument should be a 'char*'!
2170*cdf0e10cSrcweir 	     it's really defined as 'char*' in /usr/include/sys/socket.h!
2171*cdf0e10cSrcweir 	     the man page says it expects a 'void*'
2172*cdf0e10cSrcweir */
2173*cdf0e10cSrcweir 									(char*)
2174*cdf0e10cSrcweir #endif
2175*cdf0e10cSrcweir 									&nErrorCode, (int*)&nErrorSize );
2176*cdf0e10cSrcweir 			if ( (nSockOpt == 0) && (nErrorCode == 0))
2177*cdf0e10cSrcweir 				Result = osl_Socket_Ok;
2178*cdf0e10cSrcweir 			else
2179*cdf0e10cSrcweir 				Result = osl_Socket_Error;
2180*cdf0e10cSrcweir 		}
2181*cdf0e10cSrcweir 		else
2182*cdf0e10cSrcweir 		{
2183*cdf0e10cSrcweir 			Result= osl_Socket_Error;
2184*cdf0e10cSrcweir 		}
2185*cdf0e10cSrcweir 	}
2186*cdf0e10cSrcweir 	else if (ReadyHandles < 0)  /* error */
2187*cdf0e10cSrcweir 	{
2188*cdf0e10cSrcweir 	    if (errno == EBADF) /* most probably interrupted by close() */
2189*cdf0e10cSrcweir 		{
2190*cdf0e10cSrcweir 		    /* do not access pSockImpl because it is about to be or */
2191*cdf0e10cSrcweir 		    /* already destroyed */
2192*cdf0e10cSrcweir 		    return osl_Socket_Interrupted;
2193*cdf0e10cSrcweir 		}
2194*cdf0e10cSrcweir 		else
2195*cdf0e10cSrcweir         {
2196*cdf0e10cSrcweir             pSocket->m_nLastError=errno;
2197*cdf0e10cSrcweir 		    Result= osl_Socket_Error;
2198*cdf0e10cSrcweir         }
2199*cdf0e10cSrcweir 	}
2200*cdf0e10cSrcweir 	else    /* timeout */
2201*cdf0e10cSrcweir 	{
2202*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
2203*cdf0e10cSrcweir 	    Result= osl_Socket_TimedOut;
2204*cdf0e10cSrcweir 	}
2205*cdf0e10cSrcweir 
2206*cdf0e10cSrcweir 	osl_enableNonBlockingMode(pSocket, sal_False);
2207*cdf0e10cSrcweir 
2208*cdf0e10cSrcweir 	return Result;
2209*cdf0e10cSrcweir }
2210*cdf0e10cSrcweir 
2211*cdf0e10cSrcweir 
2212*cdf0e10cSrcweir /*****************************************************************************/
2213*cdf0e10cSrcweir /* osl_acceptConnectionOnSocket  */
2214*cdf0e10cSrcweir /*****************************************************************************/
2215*cdf0e10cSrcweir oslSocket SAL_CALL osl_acceptConnectionOnSocket(oslSocket pSocket,
2216*cdf0e10cSrcweir 						oslSocketAddr* ppAddr)
2217*cdf0e10cSrcweir {
2218*cdf0e10cSrcweir 	struct sockaddr Addr;
2219*cdf0e10cSrcweir 	int Connection, Flags;
2220*cdf0e10cSrcweir 	sal_uInt32 AddrLen = sizeof(struct sockaddr);
2221*cdf0e10cSrcweir 	oslSocket pConnectionSockImpl;
2222*cdf0e10cSrcweir 
2223*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2224*cdf0e10cSrcweir 	if ( pSocket == 0 )
2225*cdf0e10cSrcweir 	{
2226*cdf0e10cSrcweir 		return 0;
2227*cdf0e10cSrcweir 	}
2228*cdf0e10cSrcweir 
2229*cdf0e10cSrcweir     pSocket->m_nLastError=0;
2230*cdf0e10cSrcweir #if defined(LINUX)
2231*cdf0e10cSrcweir     pSocket->m_bIsAccepting = sal_True;
2232*cdf0e10cSrcweir #endif /* LINUX */
2233*cdf0e10cSrcweir 
2234*cdf0e10cSrcweir 	if( ppAddr && *ppAddr )
2235*cdf0e10cSrcweir 	{
2236*cdf0e10cSrcweir 		osl_destroySocketAddr( *ppAddr );
2237*cdf0e10cSrcweir 		*ppAddr = 0;
2238*cdf0e10cSrcweir 	}
2239*cdf0e10cSrcweir 
2240*cdf0e10cSrcweir     /* prevent Linux EINTR behaviour */
2241*cdf0e10cSrcweir     do
2242*cdf0e10cSrcweir     {
2243*cdf0e10cSrcweir         Connection = accept(pSocket->m_Socket, &Addr, (int*)PTR_SIZE_T(AddrLen));
2244*cdf0e10cSrcweir     } while (Connection == -1 && errno == EINTR);
2245*cdf0e10cSrcweir 
2246*cdf0e10cSrcweir 
2247*cdf0e10cSrcweir     /* accept failed? */
2248*cdf0e10cSrcweir     if( Connection == OSL_SOCKET_ERROR )
2249*cdf0e10cSrcweir     {
2250*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
2251*cdf0e10cSrcweir 		OSL_TRACE("osl_acceptConnectionOnSocket : accept error '%s'\n",strerror(errno));
2252*cdf0e10cSrcweir 
2253*cdf0e10cSrcweir #if defined(LINUX)
2254*cdf0e10cSrcweir         pSocket->m_bIsAccepting = sal_False;
2255*cdf0e10cSrcweir #endif /* LINUX */
2256*cdf0e10cSrcweir         return 0;
2257*cdf0e10cSrcweir     }
2258*cdf0e10cSrcweir 
2259*cdf0e10cSrcweir     OSL_ASSERT(AddrLen == sizeof(struct sockaddr));
2260*cdf0e10cSrcweir 
2261*cdf0e10cSrcweir 
2262*cdf0e10cSrcweir #if defined(LINUX)
2263*cdf0e10cSrcweir     if ( pSocket->m_bIsInShutdown == sal_True )
2264*cdf0e10cSrcweir     {
2265*cdf0e10cSrcweir         close(Connection);
2266*cdf0e10cSrcweir 		OSL_TRACE("osl_acceptConnectionOnSocket : close while accept\n");
2267*cdf0e10cSrcweir         return 0;
2268*cdf0e10cSrcweir     }
2269*cdf0e10cSrcweir #endif /* LINUX */
2270*cdf0e10cSrcweir 
2271*cdf0e10cSrcweir 
2272*cdf0e10cSrcweir 	if(ppAddr)
2273*cdf0e10cSrcweir 	{
2274*cdf0e10cSrcweir 		*ppAddr= __osl_createSocketAddrFromSystem(&Addr);
2275*cdf0e10cSrcweir     }
2276*cdf0e10cSrcweir 
2277*cdf0e10cSrcweir 	/* alloc memory */
2278*cdf0e10cSrcweir 	pConnectionSockImpl= __osl_createSocketImpl(OSL_INVALID_SOCKET);
2279*cdf0e10cSrcweir 
2280*cdf0e10cSrcweir 	/* set close-on-exec flag */
2281*cdf0e10cSrcweir 	if ((Flags = fcntl(Connection, F_GETFD, 0)) != -1)
2282*cdf0e10cSrcweir 	{
2283*cdf0e10cSrcweir 		Flags |= FD_CLOEXEC;
2284*cdf0e10cSrcweir 		if (fcntl(Connection, F_SETFD, Flags) == -1)
2285*cdf0e10cSrcweir         {
2286*cdf0e10cSrcweir             pSocket->m_nLastError=errno;
2287*cdf0e10cSrcweir 			OSL_TRACE("osl_acceptConnectionOnSocket failed changing socket flags. Errno: %d (%s)\n",
2288*cdf0e10cSrcweir                       errno,
2289*cdf0e10cSrcweir                       strerror(errno));
2290*cdf0e10cSrcweir         }
2291*cdf0e10cSrcweir 
2292*cdf0e10cSrcweir 	}
2293*cdf0e10cSrcweir 
2294*cdf0e10cSrcweir 	pConnectionSockImpl->m_Socket			= Connection;
2295*cdf0e10cSrcweir 	pConnectionSockImpl->m_nLastError		= 0;
2296*cdf0e10cSrcweir 	pConnectionSockImpl->m_CloseCallback	= NULL;
2297*cdf0e10cSrcweir 	pConnectionSockImpl->m_CallbackArg		= NULL;
2298*cdf0e10cSrcweir #if defined(LINUX)
2299*cdf0e10cSrcweir 	pConnectionSockImpl->m_bIsAccepting		= sal_False;
2300*cdf0e10cSrcweir 
2301*cdf0e10cSrcweir     pSocket->m_bIsAccepting = sal_False;
2302*cdf0e10cSrcweir #endif /* LINUX */
2303*cdf0e10cSrcweir 	return pConnectionSockImpl;
2304*cdf0e10cSrcweir }
2305*cdf0e10cSrcweir 
2306*cdf0e10cSrcweir /*****************************************************************************/
2307*cdf0e10cSrcweir /* osl_receiveSocket  */
2308*cdf0e10cSrcweir /*****************************************************************************/
2309*cdf0e10cSrcweir sal_Int32 SAL_CALL osl_receiveSocket(oslSocket pSocket,
2310*cdf0e10cSrcweir 					      void* pBuffer,
2311*cdf0e10cSrcweir 					      sal_uInt32 BytesToRead,
2312*cdf0e10cSrcweir 					      oslSocketMsgFlag Flag)
2313*cdf0e10cSrcweir {
2314*cdf0e10cSrcweir     int nRead;
2315*cdf0e10cSrcweir 
2316*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2317*cdf0e10cSrcweir 	if ( pSocket == 0 )
2318*cdf0e10cSrcweir 	{
2319*cdf0e10cSrcweir         OSL_TRACE("osl_receiveSocket : Invalid socket");
2320*cdf0e10cSrcweir 		return -1;
2321*cdf0e10cSrcweir 	}
2322*cdf0e10cSrcweir 
2323*cdf0e10cSrcweir     pSocket->m_nLastError=0;
2324*cdf0e10cSrcweir 
2325*cdf0e10cSrcweir     do
2326*cdf0e10cSrcweir     {
2327*cdf0e10cSrcweir         nRead =  recv(pSocket->m_Socket,
2328*cdf0e10cSrcweir                       (sal_Char*)pBuffer,
2329*cdf0e10cSrcweir                       BytesToRead,
2330*cdf0e10cSrcweir                       MSG_FLAG_TO_NATIVE(Flag));
2331*cdf0e10cSrcweir     } while ( nRead < 0 && errno == EINTR );
2332*cdf0e10cSrcweir 
2333*cdf0e10cSrcweir     if ( nRead < 0 )
2334*cdf0e10cSrcweir     {
2335*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
2336*cdf0e10cSrcweir         OSL_TRACE("osl_receiveSocket failed : %i '%s'",nRead,strerror(errno));
2337*cdf0e10cSrcweir     }
2338*cdf0e10cSrcweir     else if ( nRead == 0 )
2339*cdf0e10cSrcweir     {
2340*cdf0e10cSrcweir         OSL_TRACE("osl_receiveSocket failed : %i '%s'",nRead,"EOL");
2341*cdf0e10cSrcweir     }
2342*cdf0e10cSrcweir 
2343*cdf0e10cSrcweir     return nRead;
2344*cdf0e10cSrcweir }
2345*cdf0e10cSrcweir 
2346*cdf0e10cSrcweir 
2347*cdf0e10cSrcweir /*****************************************************************************/
2348*cdf0e10cSrcweir /* osl_receiveFromSocket  */
2349*cdf0e10cSrcweir /*****************************************************************************/
2350*cdf0e10cSrcweir sal_Int32 SAL_CALL osl_receiveFromSocket(oslSocket pSocket,
2351*cdf0e10cSrcweir 						      oslSocketAddr pSenderAddr,
2352*cdf0e10cSrcweir 						      void* pBuffer,
2353*cdf0e10cSrcweir 						      sal_uInt32 BufferSize,
2354*cdf0e10cSrcweir 						      oslSocketMsgFlag Flag)
2355*cdf0e10cSrcweir {
2356*cdf0e10cSrcweir     int nRead;
2357*cdf0e10cSrcweir 	struct sockaddr *pSystemSockAddr = 0;
2358*cdf0e10cSrcweir 	int AddrLen = 0;
2359*cdf0e10cSrcweir 	if( pSenderAddr )
2360*cdf0e10cSrcweir 	{
2361*cdf0e10cSrcweir 		AddrLen = sizeof( struct sockaddr );
2362*cdf0e10cSrcweir 		pSystemSockAddr = &(pSenderAddr->m_sockaddr);
2363*cdf0e10cSrcweir 	}
2364*cdf0e10cSrcweir 
2365*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2366*cdf0e10cSrcweir 	if ( pSocket == 0 )
2367*cdf0e10cSrcweir 	{
2368*cdf0e10cSrcweir         OSL_TRACE("osl_receiveFromSocket : Invalid socket");
2369*cdf0e10cSrcweir         return -1;
2370*cdf0e10cSrcweir 	}
2371*cdf0e10cSrcweir 
2372*cdf0e10cSrcweir     pSocket->m_nLastError=0;
2373*cdf0e10cSrcweir 
2374*cdf0e10cSrcweir     nRead = recvfrom(pSocket->m_Socket,
2375*cdf0e10cSrcweir 					 (sal_Char*)pBuffer,
2376*cdf0e10cSrcweir 					 BufferSize,
2377*cdf0e10cSrcweir 					 MSG_FLAG_TO_NATIVE(Flag),
2378*cdf0e10cSrcweir 					 pSystemSockAddr,
2379*cdf0e10cSrcweir 					 PTR_SIZE_T(AddrLen));
2380*cdf0e10cSrcweir 
2381*cdf0e10cSrcweir     if ( nRead < 0 )
2382*cdf0e10cSrcweir     {
2383*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
2384*cdf0e10cSrcweir         OSL_TRACE("osl_receiveFromSocket failed : %i '%s'",nRead,strerror(errno));
2385*cdf0e10cSrcweir     }
2386*cdf0e10cSrcweir     else if ( nRead == 0 )
2387*cdf0e10cSrcweir     {
2388*cdf0e10cSrcweir         OSL_TRACE("osl_receiveSocket failed : %i '%s'",nRead,"EOL");
2389*cdf0e10cSrcweir     }
2390*cdf0e10cSrcweir 
2391*cdf0e10cSrcweir 	return nRead;
2392*cdf0e10cSrcweir }
2393*cdf0e10cSrcweir 
2394*cdf0e10cSrcweir 
2395*cdf0e10cSrcweir /*****************************************************************************/
2396*cdf0e10cSrcweir /* osl_sendSocket  */
2397*cdf0e10cSrcweir /*****************************************************************************/
2398*cdf0e10cSrcweir sal_Int32 SAL_CALL osl_sendSocket(oslSocket pSocket,
2399*cdf0e10cSrcweir 				       const void* pBuffer,
2400*cdf0e10cSrcweir 				       sal_uInt32 BytesToSend,
2401*cdf0e10cSrcweir 				       oslSocketMsgFlag Flag)
2402*cdf0e10cSrcweir {
2403*cdf0e10cSrcweir     int nWritten;
2404*cdf0e10cSrcweir 
2405*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2406*cdf0e10cSrcweir 	if ( pSocket == 0 )
2407*cdf0e10cSrcweir 	{
2408*cdf0e10cSrcweir         OSL_TRACE("osl_sendSocket : Invalid socket");
2409*cdf0e10cSrcweir 		return -1;
2410*cdf0e10cSrcweir 	}
2411*cdf0e10cSrcweir 
2412*cdf0e10cSrcweir     pSocket->m_nLastError=0;
2413*cdf0e10cSrcweir 
2414*cdf0e10cSrcweir     do
2415*cdf0e10cSrcweir     {
2416*cdf0e10cSrcweir         nWritten = send(pSocket->m_Socket,
2417*cdf0e10cSrcweir                         (sal_Char*)pBuffer,
2418*cdf0e10cSrcweir                         BytesToSend,
2419*cdf0e10cSrcweir                         MSG_FLAG_TO_NATIVE(Flag));
2420*cdf0e10cSrcweir     } while ( nWritten < 0 && errno == EINTR );
2421*cdf0e10cSrcweir 
2422*cdf0e10cSrcweir 
2423*cdf0e10cSrcweir     if ( nWritten < 0 )
2424*cdf0e10cSrcweir     {
2425*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
2426*cdf0e10cSrcweir         OSL_TRACE("osl_sendSocket failed : %i '%s'",nWritten,strerror(errno));
2427*cdf0e10cSrcweir     }
2428*cdf0e10cSrcweir     else if ( nWritten == 0 )
2429*cdf0e10cSrcweir     {
2430*cdf0e10cSrcweir         OSL_TRACE("osl_sendSocket failed : %i '%s'",nWritten,"EOL");
2431*cdf0e10cSrcweir     }
2432*cdf0e10cSrcweir 
2433*cdf0e10cSrcweir 	return nWritten;
2434*cdf0e10cSrcweir }
2435*cdf0e10cSrcweir 
2436*cdf0e10cSrcweir /*****************************************************************************/
2437*cdf0e10cSrcweir /* osl_sendToSocket  */
2438*cdf0e10cSrcweir /*****************************************************************************/
2439*cdf0e10cSrcweir sal_Int32 SAL_CALL osl_sendToSocket(oslSocket pSocket,
2440*cdf0e10cSrcweir 					     oslSocketAddr ReceiverAddr,
2441*cdf0e10cSrcweir 					     const void* pBuffer,
2442*cdf0e10cSrcweir 					     sal_uInt32 BytesToSend,
2443*cdf0e10cSrcweir 					     oslSocketMsgFlag Flag)
2444*cdf0e10cSrcweir {
2445*cdf0e10cSrcweir     int nWritten;
2446*cdf0e10cSrcweir 
2447*cdf0e10cSrcweir 	struct sockaddr *pSystemSockAddr = 0;
2448*cdf0e10cSrcweir 	int AddrLen = 0;
2449*cdf0e10cSrcweir 	if( ReceiverAddr )
2450*cdf0e10cSrcweir 	{
2451*cdf0e10cSrcweir 		pSystemSockAddr = &(ReceiverAddr->m_sockaddr);
2452*cdf0e10cSrcweir 		AddrLen = sizeof( struct sockaddr );
2453*cdf0e10cSrcweir 	}
2454*cdf0e10cSrcweir 
2455*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2456*cdf0e10cSrcweir 	if ( pSocket == 0 )
2457*cdf0e10cSrcweir 	{
2458*cdf0e10cSrcweir         OSL_TRACE("osl_sendToSocket : Invalid socket");
2459*cdf0e10cSrcweir 		return -1;
2460*cdf0e10cSrcweir 	}
2461*cdf0e10cSrcweir 
2462*cdf0e10cSrcweir     pSocket->m_nLastError=0;
2463*cdf0e10cSrcweir 
2464*cdf0e10cSrcweir 	/* ReceiverAddr might be 0 when used on a connected socket. */
2465*cdf0e10cSrcweir 	/* Then sendto should behave like send. */
2466*cdf0e10cSrcweir 
2467*cdf0e10cSrcweir     nWritten = sendto(pSocket->m_Socket,
2468*cdf0e10cSrcweir                       (sal_Char*)pBuffer,
2469*cdf0e10cSrcweir                       BytesToSend,
2470*cdf0e10cSrcweir                       MSG_FLAG_TO_NATIVE(Flag),
2471*cdf0e10cSrcweir                       pSystemSockAddr,
2472*cdf0e10cSrcweir                       AddrLen);
2473*cdf0e10cSrcweir 
2474*cdf0e10cSrcweir     if ( nWritten < 0 )
2475*cdf0e10cSrcweir     {
2476*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
2477*cdf0e10cSrcweir         OSL_TRACE("osl_sendToSocket failed : %i '%s'",nWritten,strerror(errno));
2478*cdf0e10cSrcweir     }
2479*cdf0e10cSrcweir     else if ( nWritten == 0 )
2480*cdf0e10cSrcweir     {
2481*cdf0e10cSrcweir         OSL_TRACE("osl_sendToSocket failed : %i '%s'",nWritten,"EOL");
2482*cdf0e10cSrcweir     }
2483*cdf0e10cSrcweir 
2484*cdf0e10cSrcweir 	return nWritten;
2485*cdf0e10cSrcweir }
2486*cdf0e10cSrcweir 
2487*cdf0e10cSrcweir /*****************************************************************************/
2488*cdf0e10cSrcweir /* osl_readSocket  */
2489*cdf0e10cSrcweir /*****************************************************************************/
2490*cdf0e10cSrcweir sal_Int32 SAL_CALL osl_readSocket (
2491*cdf0e10cSrcweir 	oslSocket pSocket, void *pBuffer, sal_Int32 n )
2492*cdf0e10cSrcweir {
2493*cdf0e10cSrcweir 	sal_uInt8 * Ptr = (sal_uInt8 *)pBuffer;
2494*cdf0e10cSrcweir 	sal_uInt32 BytesRead= 0;
2495*cdf0e10cSrcweir 	sal_uInt32 BytesToRead= n;
2496*cdf0e10cSrcweir 
2497*cdf0e10cSrcweir 	OSL_ASSERT( pSocket);
2498*cdf0e10cSrcweir 
2499*cdf0e10cSrcweir 	/* loop until all desired bytes were read or an error occured */
2500*cdf0e10cSrcweir 	while (BytesToRead > 0)
2501*cdf0e10cSrcweir 	{
2502*cdf0e10cSrcweir 		sal_Int32 RetVal;
2503*cdf0e10cSrcweir 		RetVal= osl_receiveSocket(pSocket,
2504*cdf0e10cSrcweir 								   Ptr,
2505*cdf0e10cSrcweir 								   BytesToRead,
2506*cdf0e10cSrcweir 								   osl_Socket_MsgNormal);
2507*cdf0e10cSrcweir 
2508*cdf0e10cSrcweir 		/* error occured? */
2509*cdf0e10cSrcweir 		if(RetVal <= 0)
2510*cdf0e10cSrcweir 		{
2511*cdf0e10cSrcweir 			break;
2512*cdf0e10cSrcweir 		}
2513*cdf0e10cSrcweir 
2514*cdf0e10cSrcweir 		BytesToRead -= RetVal;
2515*cdf0e10cSrcweir 		BytesRead += RetVal;
2516*cdf0e10cSrcweir 		Ptr += RetVal;
2517*cdf0e10cSrcweir 	}
2518*cdf0e10cSrcweir 
2519*cdf0e10cSrcweir 	return BytesRead;
2520*cdf0e10cSrcweir }
2521*cdf0e10cSrcweir 
2522*cdf0e10cSrcweir /*****************************************************************************/
2523*cdf0e10cSrcweir /* osl_writeSocket  */
2524*cdf0e10cSrcweir /*****************************************************************************/
2525*cdf0e10cSrcweir sal_Int32 SAL_CALL osl_writeSocket(
2526*cdf0e10cSrcweir 	oslSocket pSocket, const void *pBuffer, sal_Int32 n )
2527*cdf0e10cSrcweir {
2528*cdf0e10cSrcweir 	/* loop until all desired bytes were send or an error occured */
2529*cdf0e10cSrcweir 	sal_uInt32 BytesSend= 0;
2530*cdf0e10cSrcweir 	sal_uInt32 BytesToSend= n;
2531*cdf0e10cSrcweir 	sal_uInt8 *Ptr = ( sal_uInt8 * )pBuffer;
2532*cdf0e10cSrcweir 
2533*cdf0e10cSrcweir 	OSL_ASSERT( pSocket );
2534*cdf0e10cSrcweir 
2535*cdf0e10cSrcweir 	while (BytesToSend > 0)
2536*cdf0e10cSrcweir 	{
2537*cdf0e10cSrcweir 		sal_Int32 RetVal;
2538*cdf0e10cSrcweir 
2539*cdf0e10cSrcweir 		RetVal= osl_sendSocket( pSocket,Ptr,BytesToSend,osl_Socket_MsgNormal);
2540*cdf0e10cSrcweir 
2541*cdf0e10cSrcweir 		/* error occured? */
2542*cdf0e10cSrcweir 		if(RetVal <= 0)
2543*cdf0e10cSrcweir 		{
2544*cdf0e10cSrcweir 			break;
2545*cdf0e10cSrcweir 		}
2546*cdf0e10cSrcweir 
2547*cdf0e10cSrcweir 		BytesToSend -= RetVal;
2548*cdf0e10cSrcweir 		BytesSend += RetVal;
2549*cdf0e10cSrcweir 		Ptr += RetVal;
2550*cdf0e10cSrcweir 
2551*cdf0e10cSrcweir 	}
2552*cdf0e10cSrcweir 	return BytesSend;
2553*cdf0e10cSrcweir }
2554*cdf0e10cSrcweir 
2555*cdf0e10cSrcweir /*****************************************************************************/
2556*cdf0e10cSrcweir /* __osl_socket_poll */
2557*cdf0e10cSrcweir /*****************************************************************************/
2558*cdf0e10cSrcweir 
2559*cdf0e10cSrcweir #ifdef HAVE_POLL_H /* poll() */
2560*cdf0e10cSrcweir 
2561*cdf0e10cSrcweir sal_Bool __osl_socket_poll (
2562*cdf0e10cSrcweir 	oslSocket        pSocket,
2563*cdf0e10cSrcweir 	const TimeValue* pTimeout,
2564*cdf0e10cSrcweir 	short            nEvent)
2565*cdf0e10cSrcweir {
2566*cdf0e10cSrcweir 	struct pollfd fds;
2567*cdf0e10cSrcweir 	int           timeout;
2568*cdf0e10cSrcweir 	int           result;
2569*cdf0e10cSrcweir 
2570*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2571*cdf0e10cSrcweir 	pSocket->m_nLastError = 0;
2572*cdf0e10cSrcweir 
2573*cdf0e10cSrcweir 	fds.fd      = pSocket->m_Socket;
2574*cdf0e10cSrcweir 	fds.events  = nEvent;
2575*cdf0e10cSrcweir 	fds.revents = 0;
2576*cdf0e10cSrcweir 
2577*cdf0e10cSrcweir 	timeout = -1;
2578*cdf0e10cSrcweir 	if (pTimeout)
2579*cdf0e10cSrcweir 	{
2580*cdf0e10cSrcweir 		/* Convert to [ms] */
2581*cdf0e10cSrcweir 		timeout  = pTimeout->Seconds * 1000;
2582*cdf0e10cSrcweir 		timeout += pTimeout->Nanosec / (1000 * 1000);
2583*cdf0e10cSrcweir 	}
2584*cdf0e10cSrcweir 
2585*cdf0e10cSrcweir 	result = poll (&fds, 1, timeout);
2586*cdf0e10cSrcweir 	if (result < 0)
2587*cdf0e10cSrcweir 	{
2588*cdf0e10cSrcweir 		pSocket->m_nLastError = errno;
2589*cdf0e10cSrcweir 		OSL_TRACE("__osl_socket_poll(): poll error: %d (%s)",
2590*cdf0e10cSrcweir 				  errno, strerror(errno));
2591*cdf0e10cSrcweir 		return sal_False;
2592*cdf0e10cSrcweir 	}
2593*cdf0e10cSrcweir 	if (result == 0)
2594*cdf0e10cSrcweir 	{
2595*cdf0e10cSrcweir 		/* Timeout */
2596*cdf0e10cSrcweir 		return sal_False;
2597*cdf0e10cSrcweir 	}
2598*cdf0e10cSrcweir 
2599*cdf0e10cSrcweir 	return ((fds.revents & nEvent) == nEvent);
2600*cdf0e10cSrcweir }
2601*cdf0e10cSrcweir 
2602*cdf0e10cSrcweir #else  /* select() */
2603*cdf0e10cSrcweir 
2604*cdf0e10cSrcweir sal_Bool __osl_socket_poll (
2605*cdf0e10cSrcweir 	oslSocket        pSocket,
2606*cdf0e10cSrcweir 	const TimeValue* pTimeout,
2607*cdf0e10cSrcweir 	short            nEvent)
2608*cdf0e10cSrcweir {
2609*cdf0e10cSrcweir 	fd_set         fds;
2610*cdf0e10cSrcweir 	struct timeval tv;
2611*cdf0e10cSrcweir 	int            result;
2612*cdf0e10cSrcweir 
2613*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2614*cdf0e10cSrcweir 	pSocket->m_nLastError = 0;
2615*cdf0e10cSrcweir 
2616*cdf0e10cSrcweir 	FD_ZERO(&fds);
2617*cdf0e10cSrcweir 	FD_SET(pSocket->m_Socket, &fds);
2618*cdf0e10cSrcweir 
2619*cdf0e10cSrcweir 	if (pTimeout)
2620*cdf0e10cSrcweir 	{
2621*cdf0e10cSrcweir 		/* Convert to 'timeval' */
2622*cdf0e10cSrcweir 		tv.tv_sec  = pTimeout->Seconds;
2623*cdf0e10cSrcweir 		tv.tv_usec = pTimeout->Nanosec / 1000;
2624*cdf0e10cSrcweir 	}
2625*cdf0e10cSrcweir 
2626*cdf0e10cSrcweir 	result = select (
2627*cdf0e10cSrcweir 		pSocket->m_Socket + 1,
2628*cdf0e10cSrcweir 		(nEvent == POLLIN ) ? PTR_FD_SET(fds) : NULL,
2629*cdf0e10cSrcweir 		(nEvent == POLLOUT) ? PTR_FD_SET(fds) : NULL,
2630*cdf0e10cSrcweir 		(nEvent == POLLPRI) ? PTR_FD_SET(fds) : NULL,
2631*cdf0e10cSrcweir 		(pTimeout)          ? &tv             : NULL);
2632*cdf0e10cSrcweir 
2633*cdf0e10cSrcweir 	if (result < 0)
2634*cdf0e10cSrcweir 	{
2635*cdf0e10cSrcweir 		pSocket->m_nLastError = errno;
2636*cdf0e10cSrcweir         OSL_TRACE("__osl_socket_poll(): select error: %d (%s)",
2637*cdf0e10cSrcweir 				  errno, strerror(errno));
2638*cdf0e10cSrcweir 		return sal_False;
2639*cdf0e10cSrcweir 	}
2640*cdf0e10cSrcweir 	if (result == 0)
2641*cdf0e10cSrcweir 	{
2642*cdf0e10cSrcweir 		/* Timeout */
2643*cdf0e10cSrcweir 		return sal_False;
2644*cdf0e10cSrcweir 	}
2645*cdf0e10cSrcweir 
2646*cdf0e10cSrcweir 	return (FD_ISSET(pSocket->m_Socket, &fds) ? sal_True : sal_False);
2647*cdf0e10cSrcweir }
2648*cdf0e10cSrcweir 
2649*cdf0e10cSrcweir #endif /* HAVE_POLL_H */
2650*cdf0e10cSrcweir 
2651*cdf0e10cSrcweir /*****************************************************************************/
2652*cdf0e10cSrcweir /* osl_isReceiveReady  */
2653*cdf0e10cSrcweir /*****************************************************************************/
2654*cdf0e10cSrcweir sal_Bool SAL_CALL osl_isReceiveReady (
2655*cdf0e10cSrcweir 	oslSocket pSocket, const TimeValue* pTimeout)
2656*cdf0e10cSrcweir {
2657*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2658*cdf0e10cSrcweir 	if (pSocket == NULL)
2659*cdf0e10cSrcweir 	{
2660*cdf0e10cSrcweir 		/* ENOTSOCK */
2661*cdf0e10cSrcweir 		return sal_False;
2662*cdf0e10cSrcweir 	}
2663*cdf0e10cSrcweir 
2664*cdf0e10cSrcweir 	return __osl_socket_poll (pSocket, pTimeout, POLLIN);
2665*cdf0e10cSrcweir }
2666*cdf0e10cSrcweir 
2667*cdf0e10cSrcweir /*****************************************************************************/
2668*cdf0e10cSrcweir /* osl_isSendReady  */
2669*cdf0e10cSrcweir /*****************************************************************************/
2670*cdf0e10cSrcweir sal_Bool SAL_CALL osl_isSendReady (
2671*cdf0e10cSrcweir 	oslSocket pSocket, const TimeValue* pTimeout)
2672*cdf0e10cSrcweir {
2673*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2674*cdf0e10cSrcweir 	if (pSocket == NULL)
2675*cdf0e10cSrcweir 	{
2676*cdf0e10cSrcweir 		/* ENOTSOCK */
2677*cdf0e10cSrcweir 		return sal_False;
2678*cdf0e10cSrcweir 	}
2679*cdf0e10cSrcweir 
2680*cdf0e10cSrcweir 	return __osl_socket_poll (pSocket, pTimeout, POLLOUT);
2681*cdf0e10cSrcweir }
2682*cdf0e10cSrcweir 
2683*cdf0e10cSrcweir /*****************************************************************************/
2684*cdf0e10cSrcweir /* osl_isExceptionPending  */
2685*cdf0e10cSrcweir /*****************************************************************************/
2686*cdf0e10cSrcweir sal_Bool SAL_CALL osl_isExceptionPending (
2687*cdf0e10cSrcweir 	oslSocket pSocket, const TimeValue* pTimeout)
2688*cdf0e10cSrcweir {
2689*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2690*cdf0e10cSrcweir 	if (pSocket == NULL)
2691*cdf0e10cSrcweir 	{
2692*cdf0e10cSrcweir 		/* ENOTSOCK */
2693*cdf0e10cSrcweir 		return sal_False;
2694*cdf0e10cSrcweir 	}
2695*cdf0e10cSrcweir 
2696*cdf0e10cSrcweir 	return __osl_socket_poll (pSocket, pTimeout, POLLPRI);
2697*cdf0e10cSrcweir }
2698*cdf0e10cSrcweir 
2699*cdf0e10cSrcweir /*****************************************************************************/
2700*cdf0e10cSrcweir /* osl_shutdownSocket  */
2701*cdf0e10cSrcweir /*****************************************************************************/
2702*cdf0e10cSrcweir sal_Bool SAL_CALL osl_shutdownSocket(oslSocket pSocket,
2703*cdf0e10cSrcweir 						   oslSocketDirection Direction)
2704*cdf0e10cSrcweir {
2705*cdf0e10cSrcweir     int nRet;
2706*cdf0e10cSrcweir 
2707*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2708*cdf0e10cSrcweir 	if ( pSocket == 0 )
2709*cdf0e10cSrcweir 	{
2710*cdf0e10cSrcweir 		return sal_False;
2711*cdf0e10cSrcweir 	}
2712*cdf0e10cSrcweir 
2713*cdf0e10cSrcweir     pSocket->m_nLastError=0;
2714*cdf0e10cSrcweir 
2715*cdf0e10cSrcweir     nRet=shutdown(pSocket->m_Socket, DIRECTION_TO_NATIVE(Direction));
2716*cdf0e10cSrcweir     if (nRet != 0 )
2717*cdf0e10cSrcweir     {
2718*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
2719*cdf0e10cSrcweir         OSL_TRACE("shutdown error '%s'\n",strerror(errno));
2720*cdf0e10cSrcweir     }
2721*cdf0e10cSrcweir     return (nRet==0);
2722*cdf0e10cSrcweir }
2723*cdf0e10cSrcweir 
2724*cdf0e10cSrcweir 
2725*cdf0e10cSrcweir /*****************************************************************************/
2726*cdf0e10cSrcweir /* osl_getSocketOption  */
2727*cdf0e10cSrcweir /*****************************************************************************/
2728*cdf0e10cSrcweir sal_Int32 SAL_CALL osl_getSocketOption(oslSocket pSocket,
2729*cdf0e10cSrcweir 					    	oslSocketOptionLevel	Level,
2730*cdf0e10cSrcweir 							oslSocketOption			Option,
2731*cdf0e10cSrcweir 							void*					pBuffer,
2732*cdf0e10cSrcweir 							sal_uInt32  				BufferLen)
2733*cdf0e10cSrcweir {
2734*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2735*cdf0e10cSrcweir 	if ( pSocket == 0 )
2736*cdf0e10cSrcweir 	{
2737*cdf0e10cSrcweir 		return -1;
2738*cdf0e10cSrcweir 	}
2739*cdf0e10cSrcweir 
2740*cdf0e10cSrcweir     pSocket->m_nLastError=0;
2741*cdf0e10cSrcweir 
2742*cdf0e10cSrcweir 	if(getsockopt(pSocket->m_Socket,
2743*cdf0e10cSrcweir 				  OPTION_LEVEL_TO_NATIVE(Level),
2744*cdf0e10cSrcweir 				  OPTION_TO_NATIVE(Option),
2745*cdf0e10cSrcweir 				  (sal_Char*)pBuffer,
2746*cdf0e10cSrcweir 				  (int*)PTR_SIZE_T(BufferLen)) == -1)
2747*cdf0e10cSrcweir 	{
2748*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
2749*cdf0e10cSrcweir 		return -1;
2750*cdf0e10cSrcweir 	}
2751*cdf0e10cSrcweir 
2752*cdf0e10cSrcweir 	return BufferLen;
2753*cdf0e10cSrcweir }
2754*cdf0e10cSrcweir 
2755*cdf0e10cSrcweir /*****************************************************************************/
2756*cdf0e10cSrcweir /* osl_setSocketOption  */
2757*cdf0e10cSrcweir /*****************************************************************************/
2758*cdf0e10cSrcweir sal_Bool SAL_CALL osl_setSocketOption(oslSocket pSocket,
2759*cdf0e10cSrcweir 							oslSocketOptionLevel	Level,
2760*cdf0e10cSrcweir 							oslSocketOption			Option,
2761*cdf0e10cSrcweir 							void*					pBuffer,
2762*cdf0e10cSrcweir 							sal_uInt32					BufferLen)
2763*cdf0e10cSrcweir {
2764*cdf0e10cSrcweir     int nRet;
2765*cdf0e10cSrcweir 
2766*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2767*cdf0e10cSrcweir 	if ( pSocket == 0 )
2768*cdf0e10cSrcweir 	{
2769*cdf0e10cSrcweir 		return sal_False;
2770*cdf0e10cSrcweir 	}
2771*cdf0e10cSrcweir 
2772*cdf0e10cSrcweir     pSocket->m_nLastError=0;
2773*cdf0e10cSrcweir 
2774*cdf0e10cSrcweir     nRet = setsockopt(pSocket->m_Socket,
2775*cdf0e10cSrcweir 					  OPTION_LEVEL_TO_NATIVE(Level),
2776*cdf0e10cSrcweir 					  OPTION_TO_NATIVE(Option),
2777*cdf0e10cSrcweir 					  (sal_Char*)pBuffer,
2778*cdf0e10cSrcweir 					  BufferLen);
2779*cdf0e10cSrcweir 
2780*cdf0e10cSrcweir     if ( nRet < 0 )
2781*cdf0e10cSrcweir     {
2782*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
2783*cdf0e10cSrcweir         return sal_False;
2784*cdf0e10cSrcweir     }
2785*cdf0e10cSrcweir 
2786*cdf0e10cSrcweir 	return sal_True;
2787*cdf0e10cSrcweir }
2788*cdf0e10cSrcweir 
2789*cdf0e10cSrcweir /*****************************************************************************/
2790*cdf0e10cSrcweir /* osl_enableNonBlockingMode  */
2791*cdf0e10cSrcweir /*****************************************************************************/
2792*cdf0e10cSrcweir sal_Bool SAL_CALL osl_enableNonBlockingMode(oslSocket pSocket,
2793*cdf0e10cSrcweir 								  sal_Bool On)
2794*cdf0e10cSrcweir {
2795*cdf0e10cSrcweir 	int flags;
2796*cdf0e10cSrcweir     int nRet;
2797*cdf0e10cSrcweir 
2798*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2799*cdf0e10cSrcweir 	if ( pSocket == 0 )
2800*cdf0e10cSrcweir 	{
2801*cdf0e10cSrcweir 		return sal_False;
2802*cdf0e10cSrcweir 	}
2803*cdf0e10cSrcweir 
2804*cdf0e10cSrcweir     pSocket->m_nLastError=0;
2805*cdf0e10cSrcweir 
2806*cdf0e10cSrcweir 	flags = fcntl(pSocket->m_Socket, F_GETFL, 0);
2807*cdf0e10cSrcweir 
2808*cdf0e10cSrcweir 	if (On)
2809*cdf0e10cSrcweir 		flags |= O_NONBLOCK;
2810*cdf0e10cSrcweir 	else
2811*cdf0e10cSrcweir 		flags &= ~(O_NONBLOCK);
2812*cdf0e10cSrcweir 
2813*cdf0e10cSrcweir     nRet = fcntl(pSocket->m_Socket, F_SETFL, flags);
2814*cdf0e10cSrcweir 
2815*cdf0e10cSrcweir     if  ( nRet < 0 )
2816*cdf0e10cSrcweir     {
2817*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
2818*cdf0e10cSrcweir         return sal_False;
2819*cdf0e10cSrcweir     }
2820*cdf0e10cSrcweir 
2821*cdf0e10cSrcweir     return sal_True;
2822*cdf0e10cSrcweir }
2823*cdf0e10cSrcweir 
2824*cdf0e10cSrcweir /*****************************************************************************/
2825*cdf0e10cSrcweir /* osl_isNonBlockingMode  */
2826*cdf0e10cSrcweir /*****************************************************************************/
2827*cdf0e10cSrcweir sal_Bool SAL_CALL osl_isNonBlockingMode(oslSocket pSocket)
2828*cdf0e10cSrcweir {
2829*cdf0e10cSrcweir 	int flags;
2830*cdf0e10cSrcweir 
2831*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2832*cdf0e10cSrcweir 	if ( pSocket == 0 )
2833*cdf0e10cSrcweir 	{
2834*cdf0e10cSrcweir 		return sal_False;
2835*cdf0e10cSrcweir 	}
2836*cdf0e10cSrcweir 
2837*cdf0e10cSrcweir     pSocket->m_nLastError=0;
2838*cdf0e10cSrcweir 
2839*cdf0e10cSrcweir 	flags = fcntl(pSocket->m_Socket, F_GETFL, 0);
2840*cdf0e10cSrcweir 
2841*cdf0e10cSrcweir 	if (flags == -1 || !(flags & O_NONBLOCK))
2842*cdf0e10cSrcweir 		return sal_False;
2843*cdf0e10cSrcweir 	else
2844*cdf0e10cSrcweir 		return sal_True;
2845*cdf0e10cSrcweir }
2846*cdf0e10cSrcweir 
2847*cdf0e10cSrcweir /*****************************************************************************/
2848*cdf0e10cSrcweir /* osl_getSocketType  */
2849*cdf0e10cSrcweir /*****************************************************************************/
2850*cdf0e10cSrcweir oslSocketType SAL_CALL osl_getSocketType(oslSocket pSocket)
2851*cdf0e10cSrcweir {
2852*cdf0e10cSrcweir 	int	Type=0;
2853*cdf0e10cSrcweir 	sal_uInt32 TypeSize= sizeof(Type);
2854*cdf0e10cSrcweir 
2855*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2856*cdf0e10cSrcweir 	if ( pSocket == 0 )
2857*cdf0e10cSrcweir 	{
2858*cdf0e10cSrcweir 		return osl_Socket_TypeInvalid;
2859*cdf0e10cSrcweir 	}
2860*cdf0e10cSrcweir 
2861*cdf0e10cSrcweir     pSocket->m_nLastError=0;
2862*cdf0e10cSrcweir 
2863*cdf0e10cSrcweir 	if(getsockopt(pSocket->m_Socket,
2864*cdf0e10cSrcweir 				  OPTION_LEVEL_TO_NATIVE(osl_Socket_LevelSocket),
2865*cdf0e10cSrcweir 				  OPTION_TO_NATIVE(osl_Socket_OptionType),
2866*cdf0e10cSrcweir 				  (sal_Char*)&Type,
2867*cdf0e10cSrcweir 				  (int*)PTR_SIZE_T(TypeSize)) == -1)
2868*cdf0e10cSrcweir 	{
2869*cdf0e10cSrcweir 		/* error */
2870*cdf0e10cSrcweir         pSocket->m_nLastError=errno;
2871*cdf0e10cSrcweir 		return osl_Socket_TypeInvalid;
2872*cdf0e10cSrcweir 	}
2873*cdf0e10cSrcweir 
2874*cdf0e10cSrcweir 	return TYPE_FROM_NATIVE(Type);
2875*cdf0e10cSrcweir 
2876*cdf0e10cSrcweir }
2877*cdf0e10cSrcweir 
2878*cdf0e10cSrcweir /*****************************************************************************/
2879*cdf0e10cSrcweir /* osl_getLastSocketErrorDescription  */
2880*cdf0e10cSrcweir /*****************************************************************************/
2881*cdf0e10cSrcweir void SAL_CALL osl_getLastSocketErrorDescription(oslSocket Socket, rtl_uString **ustrError)
2882*cdf0e10cSrcweir {
2883*cdf0e10cSrcweir     sal_Char pszError[1024];
2884*cdf0e10cSrcweir 
2885*cdf0e10cSrcweir     pszError[0] = '\0';
2886*cdf0e10cSrcweir 
2887*cdf0e10cSrcweir     osl_psz_getLastSocketErrorDescription(Socket,pszError,sizeof(pszError));
2888*cdf0e10cSrcweir 
2889*cdf0e10cSrcweir     rtl_uString_newFromAscii(ustrError,pszError);
2890*cdf0e10cSrcweir 
2891*cdf0e10cSrcweir     return;
2892*cdf0e10cSrcweir }
2893*cdf0e10cSrcweir 
2894*cdf0e10cSrcweir 
2895*cdf0e10cSrcweir void SAL_CALL osl_psz_getLastSocketErrorDescription(oslSocket pSocket, sal_Char* pBuffer, sal_uInt32 BufferSize)
2896*cdf0e10cSrcweir {
2897*cdf0e10cSrcweir 	/* make shure pBuffer will be a zero-terminated string even when strncpy has to cut */
2898*cdf0e10cSrcweir 	pBuffer[BufferSize-1]= '\0';
2899*cdf0e10cSrcweir 
2900*cdf0e10cSrcweir     if ( pSocket == 0 )
2901*cdf0e10cSrcweir     {
2902*cdf0e10cSrcweir         strncpy(pBuffer, strerror(EINVAL), BufferSize-1);
2903*cdf0e10cSrcweir         return;
2904*cdf0e10cSrcweir     }
2905*cdf0e10cSrcweir 
2906*cdf0e10cSrcweir     strncpy(pBuffer, strerror(pSocket->m_nLastError), BufferSize-1);
2907*cdf0e10cSrcweir     return;
2908*cdf0e10cSrcweir }
2909*cdf0e10cSrcweir 
2910*cdf0e10cSrcweir /*****************************************************************************/
2911*cdf0e10cSrcweir /* osl_getLastSocketError  */
2912*cdf0e10cSrcweir /*****************************************************************************/
2913*cdf0e10cSrcweir oslSocketError SAL_CALL osl_getLastSocketError(oslSocket pSocket)
2914*cdf0e10cSrcweir {
2915*cdf0e10cSrcweir     if ( pSocket == 0 )
2916*cdf0e10cSrcweir     {
2917*cdf0e10cSrcweir         return ERROR_FROM_NATIVE(EINVAL);
2918*cdf0e10cSrcweir     }
2919*cdf0e10cSrcweir 
2920*cdf0e10cSrcweir 	return ERROR_FROM_NATIVE(pSocket->m_nLastError);
2921*cdf0e10cSrcweir }
2922*cdf0e10cSrcweir 
2923*cdf0e10cSrcweir /*****************************************************************************/
2924*cdf0e10cSrcweir /* SocketSet                                                                 */
2925*cdf0e10cSrcweir /*****************************************************************************/
2926*cdf0e10cSrcweir typedef struct _TSocketSetImpl
2927*cdf0e10cSrcweir {
2928*cdf0e10cSrcweir 	int		m_MaxHandle;	/* for select(), the largest descriptor in the set */
2929*cdf0e10cSrcweir 	fd_set	m_Set;			/* the set of descriptors */
2930*cdf0e10cSrcweir 
2931*cdf0e10cSrcweir } TSocketSetImpl;
2932*cdf0e10cSrcweir 
2933*cdf0e10cSrcweir /*****************************************************************************/
2934*cdf0e10cSrcweir /* osl_createSocketSet  */
2935*cdf0e10cSrcweir /*****************************************************************************/
2936*cdf0e10cSrcweir oslSocketSet SAL_CALL osl_createSocketSet()
2937*cdf0e10cSrcweir {
2938*cdf0e10cSrcweir 	TSocketSetImpl* pSet;
2939*cdf0e10cSrcweir 
2940*cdf0e10cSrcweir 	pSet= (TSocketSetImpl*)malloc(sizeof(TSocketSetImpl));
2941*cdf0e10cSrcweir 
2942*cdf0e10cSrcweir 	OSL_ASSERT(pSet);
2943*cdf0e10cSrcweir 
2944*cdf0e10cSrcweir 	if(pSet)
2945*cdf0e10cSrcweir 	{
2946*cdf0e10cSrcweir 		pSet->m_MaxHandle= 0;
2947*cdf0e10cSrcweir 		FD_ZERO(&pSet->m_Set);
2948*cdf0e10cSrcweir 	}
2949*cdf0e10cSrcweir 
2950*cdf0e10cSrcweir 	return (oslSocketSet)pSet;
2951*cdf0e10cSrcweir }
2952*cdf0e10cSrcweir 
2953*cdf0e10cSrcweir /*****************************************************************************/
2954*cdf0e10cSrcweir /* osl_destroySocketSet  */
2955*cdf0e10cSrcweir /*****************************************************************************/
2956*cdf0e10cSrcweir void SAL_CALL osl_destroySocketSet(oslSocketSet Set)
2957*cdf0e10cSrcweir {
2958*cdf0e10cSrcweir 	if(Set)
2959*cdf0e10cSrcweir 		free(Set);
2960*cdf0e10cSrcweir }
2961*cdf0e10cSrcweir 
2962*cdf0e10cSrcweir /*****************************************************************************/
2963*cdf0e10cSrcweir /* osl_clearSocketSet  */
2964*cdf0e10cSrcweir /*****************************************************************************/
2965*cdf0e10cSrcweir void SAL_CALL osl_clearSocketSet(oslSocketSet Set)
2966*cdf0e10cSrcweir {
2967*cdf0e10cSrcweir 	TSocketSetImpl* pSet;
2968*cdf0e10cSrcweir 	OSL_ASSERT(Set);
2969*cdf0e10cSrcweir 	if ( Set == 0 )
2970*cdf0e10cSrcweir 	{
2971*cdf0e10cSrcweir 		return;
2972*cdf0e10cSrcweir 	}
2973*cdf0e10cSrcweir 
2974*cdf0e10cSrcweir 	pSet= (TSocketSetImpl*)Set;
2975*cdf0e10cSrcweir 	pSet->m_MaxHandle= 0;
2976*cdf0e10cSrcweir 
2977*cdf0e10cSrcweir 	FD_ZERO(&pSet->m_Set);
2978*cdf0e10cSrcweir }
2979*cdf0e10cSrcweir 
2980*cdf0e10cSrcweir /*****************************************************************************/
2981*cdf0e10cSrcweir /* osl_addToSocketSet  */
2982*cdf0e10cSrcweir /*****************************************************************************/
2983*cdf0e10cSrcweir void SAL_CALL osl_addToSocketSet(oslSocketSet Set, oslSocket pSocket)
2984*cdf0e10cSrcweir {
2985*cdf0e10cSrcweir 	TSocketSetImpl* pSet;
2986*cdf0e10cSrcweir 
2987*cdf0e10cSrcweir 	OSL_ASSERT(Set);
2988*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
2989*cdf0e10cSrcweir 
2990*cdf0e10cSrcweir 	if ( Set == 0 || pSocket == 0)
2991*cdf0e10cSrcweir 	{
2992*cdf0e10cSrcweir 		return;
2993*cdf0e10cSrcweir 	}
2994*cdf0e10cSrcweir 
2995*cdf0e10cSrcweir 	pSet= (TSocketSetImpl*)Set;
2996*cdf0e10cSrcweir 
2997*cdf0e10cSrcweir 	/* correct max handle */
2998*cdf0e10cSrcweir 	if(pSocket->m_Socket > pSet->m_MaxHandle)
2999*cdf0e10cSrcweir 		pSet->m_MaxHandle= pSocket->m_Socket;
3000*cdf0e10cSrcweir 	FD_SET(pSocket->m_Socket, &pSet->m_Set);
3001*cdf0e10cSrcweir 
3002*cdf0e10cSrcweir }
3003*cdf0e10cSrcweir 
3004*cdf0e10cSrcweir /*****************************************************************************/
3005*cdf0e10cSrcweir /* osl_removeFromSocketSet  */
3006*cdf0e10cSrcweir /*****************************************************************************/
3007*cdf0e10cSrcweir void SAL_CALL osl_removeFromSocketSet(oslSocketSet Set, oslSocket pSocket)
3008*cdf0e10cSrcweir {
3009*cdf0e10cSrcweir 	TSocketSetImpl* pSet;
3010*cdf0e10cSrcweir 
3011*cdf0e10cSrcweir 	OSL_ASSERT(Set);
3012*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
3013*cdf0e10cSrcweir 
3014*cdf0e10cSrcweir 	if ( Set == 0 || pSocket == 0)
3015*cdf0e10cSrcweir 	{
3016*cdf0e10cSrcweir 		return;
3017*cdf0e10cSrcweir 	}
3018*cdf0e10cSrcweir 
3019*cdf0e10cSrcweir 	pSet= (TSocketSetImpl*)Set;
3020*cdf0e10cSrcweir 
3021*cdf0e10cSrcweir 	/* correct max handle */
3022*cdf0e10cSrcweir 	if(pSocket->m_Socket == pSet->m_MaxHandle)
3023*cdf0e10cSrcweir 	{
3024*cdf0e10cSrcweir 		/* not optimal, since the next used descriptor might be */
3025*cdf0e10cSrcweir 		/* much smaller than m_Socket-1, but it will do */
3026*cdf0e10cSrcweir 		pSet->m_MaxHandle--;
3027*cdf0e10cSrcweir 		if(pSet->m_MaxHandle < 0)
3028*cdf0e10cSrcweir 		{
3029*cdf0e10cSrcweir 			pSet->m_MaxHandle= 0;	/* avoid underflow */
3030*cdf0e10cSrcweir 		}
3031*cdf0e10cSrcweir 	}
3032*cdf0e10cSrcweir 
3033*cdf0e10cSrcweir 	FD_CLR(pSocket->m_Socket, &pSet->m_Set);
3034*cdf0e10cSrcweir }
3035*cdf0e10cSrcweir 
3036*cdf0e10cSrcweir /*****************************************************************************/
3037*cdf0e10cSrcweir /* osl_isInSocketSet  */
3038*cdf0e10cSrcweir /*****************************************************************************/
3039*cdf0e10cSrcweir sal_Bool SAL_CALL osl_isInSocketSet(oslSocketSet Set, oslSocket pSocket)
3040*cdf0e10cSrcweir {
3041*cdf0e10cSrcweir 	TSocketSetImpl* pSet;
3042*cdf0e10cSrcweir 
3043*cdf0e10cSrcweir 	OSL_ASSERT(Set);
3044*cdf0e10cSrcweir 	OSL_ASSERT(pSocket);
3045*cdf0e10cSrcweir 	if ( Set == 0 || pSocket == 0 )
3046*cdf0e10cSrcweir 	{
3047*cdf0e10cSrcweir 		return sal_False;
3048*cdf0e10cSrcweir 	}
3049*cdf0e10cSrcweir 
3050*cdf0e10cSrcweir 	pSet= (TSocketSetImpl*)Set;
3051*cdf0e10cSrcweir 
3052*cdf0e10cSrcweir 	return (FD_ISSET(pSocket->m_Socket, &pSet->m_Set) != 0);
3053*cdf0e10cSrcweir }
3054*cdf0e10cSrcweir 
3055*cdf0e10cSrcweir /*****************************************************************************/
3056*cdf0e10cSrcweir /* osl_demultiplexSocketEvents  */
3057*cdf0e10cSrcweir /*****************************************************************************/
3058*cdf0e10cSrcweir sal_Int32 SAL_CALL osl_demultiplexSocketEvents(oslSocketSet IncomingSet,
3059*cdf0e10cSrcweir 								    oslSocketSet OutgoingSet,
3060*cdf0e10cSrcweir 								    oslSocketSet OutOfBandSet,
3061*cdf0e10cSrcweir 								    const TimeValue* pTimeout)
3062*cdf0e10cSrcweir {
3063*cdf0e10cSrcweir 	int MaxHandle= 0;
3064*cdf0e10cSrcweir 	struct timeval 	tv;
3065*cdf0e10cSrcweir 	TSocketSetImpl* pInSet;
3066*cdf0e10cSrcweir 	TSocketSetImpl* pOutSet;
3067*cdf0e10cSrcweir 	TSocketSetImpl* pOOBSet;
3068*cdf0e10cSrcweir 
3069*cdf0e10cSrcweir 	if (pTimeout)
3070*cdf0e10cSrcweir 	{
3071*cdf0e10cSrcweir 	    /* non-blocking call */
3072*cdf0e10cSrcweir 	    tv.tv_sec  = pTimeout->Seconds;
3073*cdf0e10cSrcweir 	    tv.tv_usec = pTimeout->Nanosec / 1000L;
3074*cdf0e10cSrcweir 	}
3075*cdf0e10cSrcweir 
3076*cdf0e10cSrcweir 	/* map opaque data to impl-types */
3077*cdf0e10cSrcweir 	pInSet=  (TSocketSetImpl*)IncomingSet;
3078*cdf0e10cSrcweir 	pOutSet= (TSocketSetImpl*)OutgoingSet;
3079*cdf0e10cSrcweir 	pOOBSet= (TSocketSetImpl*)OutOfBandSet;
3080*cdf0e10cSrcweir 
3081*cdf0e10cSrcweir 	/* get max handle from all sets */
3082*cdf0e10cSrcweir 	if (pInSet)
3083*cdf0e10cSrcweir 		MaxHandle= pInSet->m_MaxHandle;
3084*cdf0e10cSrcweir 
3085*cdf0e10cSrcweir 	if (pOutSet && (pOutSet->m_MaxHandle > MaxHandle))
3086*cdf0e10cSrcweir 		MaxHandle= pOutSet->m_MaxHandle;
3087*cdf0e10cSrcweir 
3088*cdf0e10cSrcweir 	if (pOOBSet && (pOOBSet->m_MaxHandle > MaxHandle))
3089*cdf0e10cSrcweir 		MaxHandle= pOOBSet->m_MaxHandle;
3090*cdf0e10cSrcweir 
3091*cdf0e10cSrcweir 	return select(MaxHandle+1,
3092*cdf0e10cSrcweir 				  pInSet  ? PTR_FD_SET(pInSet->m_Set)  : 0,
3093*cdf0e10cSrcweir 				  pOutSet ? PTR_FD_SET(pOutSet->m_Set) : 0,
3094*cdf0e10cSrcweir 				  pOOBSet ? PTR_FD_SET(pOOBSet->m_Set) : 0,
3095*cdf0e10cSrcweir 				  pTimeout ? &tv : 0);
3096*cdf0e10cSrcweir }
3097*cdf0e10cSrcweir 
3098