1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 #include <osl/socket.h>
31 #include <osl/diagnose.h>
32 #include <osl/mutex.h>
33 #include <osl/signal.h>
35 #include <rtl/alloc.h>
38 #include <sal/types.h>
43 /* defines for poll */
48 #if defined(LINUX) || defined(NETBSD) || defined ( FREEBSD ) || defined (MACOSX)
51 #endif /* HAVE_POLL_H */
60 #define POLLOUT 0x0002
61 #define POLLPRI 0x0004
62 #endif /* HAVE_POLL_H */
65 /* defines for shutdown */
72 oslSocketAddr is a pointer to a Berkeley struct sockaddr.
73 I refrained from using sockaddr_in because of possible further
74 extensions of this socket-interface (IP-NG?).
75 The intention was to hide all Berkeley data-structures from
76 direct access past the osl-interface.
78 The current implementation is internet (IP) centered. All
79 the constructor-functions (osl_create...) take parameters
80 that will probably make sense only in the IP-environment
81 (e.g. because of using the dotted-address-format).
83 If the interface will be extended to host other protocol-
84 families, I expect no externally visible changes in the
85 existing functions. You'll probably need only new
86 constructor-functions who take the different address
87 formats into consideration (maybe a long dotted address
91 /* _Note_ that I rely on the fact that oslSocketAddr and struct sockaddr */
92 /* are the same! I don't like it very much but see no other easy way to */
93 /* conceal the struct sockaddr from the eyes of the user. */
96 #define OSL_INVALID_SOCKET -1
97 #define OSL_SOCKET_ERROR -1
100 /* Buffer size for gethostbyname */
101 #define MAX_HOSTBUFFER_SIZE 2048
103 /*****************************************************************************/
104 /* enum oslAddrFamily */
105 /*****************************************************************************/
108 static unsigned long FamilyMap
[]= {
109 AF_INET
, /* osl_Socket_FamilyInet */
110 AF_IPX
, /* osl_Socket_FamilyIpx */
111 0 /* osl_Socket_FamilyInvalid */
115 static oslAddrFamily
osl_AddrFamilyFromNative(sal_uInt32 nativeType
)
117 oslAddrFamily i
= (oslAddrFamily
)0;
119 while(i
!= osl_Socket_FamilyInvalid
)
121 if(FamilyMap
[i
] == nativeType
)
123 i
= (oslAddrFamily
) ( i
+ 1 );
130 #define FAMILY_FROM_NATIVE(y) osl_AddrFamilyFromNative(y)
131 #define FAMILY_TO_NATIVE(x) (short)FamilyMap[x]
133 /*****************************************************************************/
134 /* enum oslProtocol */
135 /*****************************************************************************/
138 static sal_uInt32 ProtocolMap
[]= {
139 0, /* osl_Socket_ProtocolIp */
140 NSPROTO_IPX
, /* osl_Socket_ProtocolIpx */
141 NSPROTO_SPX
, /* osl_Socket_ProtocolSpx */
142 NSPROTO_SPXII
, /* osl_Socket_ProtocolSpxII */
143 0 /* osl_Socket_ProtocolInvalid */
148 static oslProtocol osl_ProtocolFromNative(sal_uInt32 nativeType)
150 oslProtocol i= (oslProtocol)0;
152 while(i != osl_Socket_ProtocolInvalid)
154 if(ProtocolMap[i] == nativeType)
156 i = (oslProtocol) ( i + 1);
164 #define PROTOCOL_FROM_NATIVE(y) osl_ProtocolFromNative(y)
165 #define PROTOCOL_TO_NATIVE(x) ProtocolMap[x]
168 /*****************************************************************************/
169 /* enum oslSocketType */
170 /*****************************************************************************/
173 static sal_uInt32 TypeMap
[]= {
174 SOCK_STREAM
, /* osl_Socket_TypeStream */
175 SOCK_DGRAM
, /* osl_Socket_TypeDgram */
176 SOCK_RAW
, /* osl_Socket_TypeRaw */
177 SOCK_RDM
, /* osl_Socket_TypeRdm */
178 SOCK_SEQPACKET
, /* osl_Socket_TypeSeqPacket */
179 0 /* osl_Socket_TypeInvalid */
183 static oslSocketType
osl_SocketTypeFromNative(sal_uInt32 nativeType
)
185 oslSocketType i
= (oslSocketType
)0;
187 while(i
!= osl_Socket_TypeInvalid
)
189 if(TypeMap
[i
] == nativeType
)
191 i
= (oslSocketType
)(i
+ 1);
198 #define TYPE_TO_NATIVE(x) TypeMap[x]
199 #define TYPE_FROM_NATIVE(y) osl_SocketTypeFromNative(y)
202 /*****************************************************************************/
203 /* enum oslSocketOption */
204 /*****************************************************************************/
207 static sal_uInt32 OptionMap
[]= {
208 SO_DEBUG
, /* osl_Socket_OptionDebug */
209 SO_ACCEPTCONN
, /* osl_Socket_OptionAcceptConn */
210 SO_REUSEADDR
, /* osl_Socket_OptionReuseAddr */
211 SO_KEEPALIVE
, /* osl_Socket_OptionKeepAlive */
212 SO_DONTROUTE
, /* osl_Socket_OptionDontRoute */
213 SO_BROADCAST
, /* osl_Socket_OptionBroadcast */
214 SO_USELOOPBACK
, /* osl_Socket_OptionUseLoopback */
215 SO_LINGER
, /* osl_Socket_OptionLinger */
216 SO_OOBINLINE
, /* osl_Socket_OptionOOBinLine */
217 SO_SNDBUF
, /* osl_Socket_OptionSndBuf */
218 SO_RCVBUF
, /* osl_Socket_OptionRcvBuf */
219 SO_SNDLOWAT
, /* osl_Socket_OptionSndLowat */
220 SO_RCVLOWAT
, /* osl_Socket_OptionRcvLowat */
221 SO_SNDTIMEO
, /* osl_Socket_OptionSndTimeo */
222 SO_RCVTIMEO
, /* osl_Socket_OptionRcvTimeo */
223 SO_ERROR
, /* osl_Socket_OptionError */
224 SO_TYPE
, /* osl_Socket_OptionType */
225 TCP_NODELAY
, /* osl_Socket_OptionTcpNoDelay */
226 0 /* osl_Socket_OptionInvalid */
231 static oslSocketOption osl_SocketOptionFromNative(sal_uInt32 nativeType)
233 oslSocketOption i= (oslSocketOption)0;
235 while(i != osl_Socket_OptionInvalid)
237 if(OptionMap[i] == nativeType)
239 i = (oslSocketOption) ( i + 1 );
246 #define OPTION_TO_NATIVE(x) OptionMap[x]
247 #define OPTION_FROM_NATIVE(y) osl_SocketOptionFromNative(y)
250 /*****************************************************************************/
251 /* enum oslSocketOptionLevel */
252 /*****************************************************************************/
254 static sal_uInt32 OptionLevelMap
[]= {
255 SOL_SOCKET
, /* osl_Socket_LevelSocket */
256 IPPROTO_TCP
, /* osl_Socket_LevelTcp */
257 0 /* osl_Socket_LevelInvalid */
262 static oslSocketOptionLevel osl_SocketOptionLevelFromNative(sal_uInt32 nativeType)
264 oslSocketOptionLevel i= (oslSocketOptionLevel)0;
266 while(i != osl_Socket_LevelInvalid)
268 if(OptionLevelMap[i] == nativeType)
270 i = (oslSocketOptionLevel) ( i + 1 );
277 #define OPTION_LEVEL_TO_NATIVE(x) OptionLevelMap[x]
278 #define OPTION_LEVEL_FROM_NATIVE(y) osl_SocketOptionLevelFromNative(y)
280 /*****************************************************************************/
281 /* enum oslSocketMsgFlag */
282 /*****************************************************************************/
284 static sal_uInt32 SocketMsgFlagMap
[]= {
285 0, /* osl_Socket_MsgNormal */
286 MSG_OOB
, /* osl_Socket_MsgOOB */
287 MSG_PEEK
, /* osl_Socket_MsgPeek */
288 MSG_DONTROUTE
, /* osl_Socket_MsgDontRoute */
289 MSG_MAXIOVLEN
, /* osl_Socket_MsgMaxIOVLen */
290 0 /* osl_Socket_MsgInvalid */
295 static oslSocketMsgFlag osl_SocketMsgFlagFromNative(sal_uInt32 nativeType)
297 oslSocketMsgFlag i= (oslSocketMsgFlag)0;
299 while(i != osl_Socket_MsgInvalid)
301 if(SocketMsgFlagMap[i] == nativeType)
303 i = (oslSocketMsgFlag) ( i + 1 );
311 #define MSG_FLAG_TO_NATIVE(x) SocketMsgFlagMap[x]
312 #define MSG_FLAG_FROM_NATIVE(y) osl_SocketMsgFlagFromNative(y)
315 /*****************************************************************************/
316 /* enum oslSocketDirection */
317 /*****************************************************************************/
319 static sal_uInt32 SocketDirection
[]= {
320 SD_RECEIVE
, /* osl_Socket_DirRead */
321 SD_SEND
, /* osl_Socket_DirWrite */
322 SD_BOTH
, /* osl_Socket_DirReadWrite */
323 0 /* osl_Socket_DirInvalid */
328 static oslSocketDirection osl_SocketDirectionFromNative(sal_uInt32 nativeType)
330 oslSocketDirection i= (oslSocketDirection)0;
332 while(i != osl_Socket_DirInvalid)
334 if(SocketDirection[i] == nativeType)
336 i = (oslSocketDirection) ( i + 1 );
344 #define DIRECTION_TO_NATIVE(x) SocketDirection[x]
345 #define DIRECTION_FROM_NATIVE(y) osl_SocketDirectionFromNative(y)
347 /*****************************************************************************/
348 /* enum oslSocketError */
349 /*****************************************************************************/
354 oslSocketError error
;
356 { 0, osl_Socket_E_None
}, /* no error */
357 { ENOTSOCK
, osl_Socket_E_NotSocket
}, /* Socket operation on non-socket */
358 { EDESTADDRREQ
, osl_Socket_E_DestAddrReq
}, /* Destination address required */
359 { EMSGSIZE
, osl_Socket_E_MsgSize
}, /* Message too long */
360 { EPROTOTYPE
, osl_Socket_E_Prototype
}, /* Protocol wrong type for socket */
361 { ENOPROTOOPT
, osl_Socket_E_NoProtocol
}, /* Protocol not available */
362 { EPROTONOSUPPORT
, osl_Socket_E_ProtocolNoSupport
}, /* Protocol not supported */
363 { ESOCKTNOSUPPORT
, osl_Socket_E_TypeNoSupport
}, /* Socket type not supported */
364 { EOPNOTSUPP
, osl_Socket_E_OpNotSupport
}, /* Operation not supported on socket */
365 { EPFNOSUPPORT
, osl_Socket_E_PfNoSupport
}, /* Protocol family not supported */
366 { EAFNOSUPPORT
, osl_Socket_E_AfNoSupport
}, /* Address family not supported by */
367 /* protocol family */
368 { EADDRINUSE
, osl_Socket_E_AddrInUse
}, /* Address already in use */
369 { EADDRNOTAVAIL
, osl_Socket_E_AddrNotAvail
}, /* Can't assign requested address */
370 { ENETDOWN
, osl_Socket_E_NetDown
}, /* Network is down */
371 { ENETUNREACH
, osl_Socket_E_NetUnreachable
}, /* Network is unreachable */
372 { ENETRESET
, osl_Socket_E_NetReset
}, /* Network dropped connection because */
374 { ECONNABORTED
, osl_Socket_E_ConnAborted
}, /* Software caused connection abort */
375 { ECONNRESET
, osl_Socket_E_ConnReset
}, /* Connection reset by peer */
376 { ENOBUFS
, osl_Socket_E_NoBufferSpace
}, /* No buffer space available */
377 { EISCONN
, osl_Socket_E_IsConnected
}, /* Socket is already connected */
378 { ENOTCONN
, osl_Socket_E_NotConnected
}, /* Socket is not connected */
379 { ESHUTDOWN
, osl_Socket_E_Shutdown
}, /* Can't send after socket shutdown */
380 { ETOOMANYREFS
, osl_Socket_E_TooManyRefs
}, /* Too many references: can't splice */
381 { ETIMEDOUT
, osl_Socket_E_TimedOut
}, /* Connection timed out */
382 { ECONNREFUSED
, osl_Socket_E_ConnRefused
}, /* Connection refused */
383 { EHOSTDOWN
, osl_Socket_E_HostDown
}, /* Host is down */
384 { EHOSTUNREACH
, osl_Socket_E_HostUnreachable
}, /* No route to host */
385 { EWOULDBLOCK
, osl_Socket_E_WouldBlock
}, /* call would block on non-blocking socket */
386 { EALREADY
, osl_Socket_E_Already
}, /* operation already in progress */
387 { EINPROGRESS
, osl_Socket_E_InProgress
}, /* operation now in progress */
388 { EAGAIN
, osl_Socket_E_WouldBlock
}, /* same as EWOULDBLOCK */
389 { -1, osl_Socket_E_InvalidError
}
394 static int osl_NativeFromSocketError(oslSocketError errorCode)
398 while ((SocketError[i].error != osl_Socket_E_InvalidError) &&
399 (SocketError[i].error != errorCode)) i++;
401 return SocketError[i].errcode;
406 static oslSocketError
osl_SocketErrorFromNative(int nativeType
)
410 while ((SocketError
[i
].error
!= osl_Socket_E_InvalidError
) &&
411 (SocketError
[i
].errcode
!= nativeType
)) i
++;
413 return SocketError
[i
].error
;
417 #define ERROR_TO_NATIVE(x) osl_NativeFromSocketError(x)
418 #define ERROR_FROM_NATIVE(y) osl_SocketErrorFromNative(y)
420 /*****************************************************************************/
421 /* local function prototypes */
422 /*****************************************************************************/
424 oslSocketAddr SAL_CALL
osl_psz_createInetSocketAddr (
425 const sal_Char
* pszDottedAddr
, sal_Int32 Port
);
427 oslSocketAddr SAL_CALL
osl_psz_createIpxSocketAddr (
428 const sal_Char NetNumber
[4],
429 const sal_Char NodeNumber
[6],
430 sal_uInt32 SocketNumber
);
432 oslHostAddr SAL_CALL
osl_psz_createHostAddr (
433 const sal_Char
*pszHostname
, const oslSocketAddr Addr
);
435 oslHostAddr SAL_CALL
osl_psz_createHostAddrByName (
436 const sal_Char
*pszHostname
);
438 const sal_Char
* SAL_CALL
osl_psz_getHostnameOfHostAddr (
439 const oslHostAddr Addr
);
441 oslSocketResult SAL_CALL
osl_psz_getLocalHostname (
442 sal_Char
*pBuffer
, sal_uInt32 nBufLen
);
444 oslSocketAddr SAL_CALL
osl_psz_resolveHostname (
445 const sal_Char
* pszHostname
);
447 sal_Int32 SAL_CALL
osl_psz_getServicePort (
448 const sal_Char
* pszServicename
, const sal_Char
* pszProtocol
);
450 oslSocketResult SAL_CALL
osl_psz_getHostnameOfSocketAddr (
451 oslSocketAddr Addr
, sal_Char
*pBuffer
, sal_uInt32 BufferSize
);
453 oslSocketResult SAL_CALL
osl_psz_getDottedInetAddrOfSocketAddr (
454 oslSocketAddr Addr
, sal_Char
*pBuffer
, sal_uInt32 BufferSize
);
456 void SAL_CALL
osl_psz_getLastSocketErrorDescription (
457 oslSocket Socket
, sal_Char
* pBuffer
, sal_uInt32 BufferSize
);
459 /*****************************************************************************/
460 /* osl_create/destroy-SocketImpl */
461 /*****************************************************************************/
463 #if OSL_DEBUG_LEVEL > 1
464 static sal_uInt32 g_nSocketImpl
= 0;
465 static sal_uInt32 g_nSocketAddr
= 0;
467 /* sorry, must be implemented otherwise */
474 OSL_TRACE( "sal_socket: %d socket instances leak\n" , g_nSocketImpl
);
476 OSL_TRACE( "sal_socket: %d socket address instances leak\n" , g_nSocketAddr
);
479 LeakWarning socketWarning
;
482 #endif /* OSL_DEBUG_LEVEL */
485 oslSocket
__osl_createSocketImpl(int Socket
)
489 pSocket
= (oslSocket
)calloc(1, sizeof(struct oslSocketImpl
));
491 pSocket
->m_Socket
= Socket
;
492 pSocket
->m_nLastError
= 0;
493 pSocket
->m_CloseCallback
= 0;
494 pSocket
->m_CallbackArg
= 0;
495 pSocket
->m_nRefCount
= 1;
498 pSocket
->m_bIsAccepting
= sal_False
;
501 #if OSL_DEBUG_LEVEL > 1
507 void __osl_destroySocketImpl(oslSocket Socket
)
510 free((struct oslSocketImpl
*) Socket
);
511 #if OSL_DEBUG_LEVEL > 1
516 static oslSocketAddr
__osl_createSocketAddr( )
518 oslSocketAddr pAddr
= (oslSocketAddr
) rtl_allocateZeroMemory( sizeof( struct oslSocketAddrImpl
));
519 #if OSL_DEBUG_LEVEL > 1
525 static oslSocketAddr
__osl_createSocketAddrWithFamily(
526 oslAddrFamily family
, sal_Int32 port
, sal_uInt32 nAddr
)
530 OSL_ASSERT( family
== osl_Socket_FamilyInet
);
532 pAddr
= __osl_createSocketAddr();
535 case osl_Socket_FamilyInet
:
537 struct sockaddr_in
* pInetAddr
= (struct sockaddr_in
*)&(pAddr
->m_sockaddr
);
539 pInetAddr
->sin_family
= FAMILY_TO_NATIVE(osl_Socket_FamilyInet
);
540 pInetAddr
->sin_addr
.s_addr
= nAddr
;
541 pInetAddr
->sin_port
= (sal_uInt16
)(port
&0xffff);
545 pAddr
->m_sockaddr
.sa_family
= FAMILY_TO_NATIVE(family
);
550 static oslSocketAddr
__osl_createSocketAddrFromSystem( struct sockaddr
*pSystemSockAddr
)
552 oslSocketAddr pAddr
= __osl_createSocketAddr();
553 memcpy( &(pAddr
->m_sockaddr
), pSystemSockAddr
, sizeof( struct sockaddr
) );
557 static void __osl_destroySocketAddr( oslSocketAddr addr
)
559 #if OSL_DEBUG_LEVEL > 1
562 rtl_freeMemory( addr
);
565 /*****************************************************************************/
566 /* osl_createEmptySocketAddr */
567 /*****************************************************************************/
568 oslSocketAddr SAL_CALL
osl_createEmptySocketAddr(oslAddrFamily Family
)
570 oslSocketAddr pAddr
= 0;
572 /* is it an internet-Addr? */
573 if (Family
== osl_Socket_FamilyInet
)
575 pAddr
= __osl_createSocketAddrWithFamily(Family
, 0 , htonl(INADDR_ANY
) );
579 pAddr
= __osl_createSocketAddrWithFamily( Family
, 0 , 0 );
585 /*****************************************************************************/
586 /* osl_copySocketAddr */
587 /*****************************************************************************/
588 oslSocketAddr SAL_CALL
osl_copySocketAddr(oslSocketAddr Addr
)
590 oslSocketAddr pCopy
= 0;
593 pCopy
= __osl_createSocketAddr();
596 memcpy(&(pCopy
->m_sockaddr
),&(Addr
->m_sockaddr
), sizeof(struct sockaddr
));
601 /*****************************************************************************/
602 /* osl_isEqualSocketAddr */
603 /*****************************************************************************/
604 sal_Bool SAL_CALL
osl_isEqualSocketAddr (
608 struct sockaddr
* pAddr1
= &(Addr1
->m_sockaddr
);
609 struct sockaddr
* pAddr2
= &(Addr2
->m_sockaddr
);
614 if (pAddr1
->sa_family
== pAddr2
->sa_family
)
616 switch (pAddr1
->sa_family
)
620 struct sockaddr_in
* pInetAddr1
= (struct sockaddr_in
*)pAddr1
;
621 struct sockaddr_in
* pInetAddr2
= (struct sockaddr_in
*)pAddr2
;
623 if ((pInetAddr1
->sin_family
== pInetAddr2
->sin_family
) &&
624 (pInetAddr1
->sin_addr
.s_addr
== pInetAddr2
->sin_addr
.s_addr
) &&
625 (pInetAddr1
->sin_port
== pInetAddr2
->sin_port
))
631 return (memcmp(pAddr1
, Addr2
, sizeof(struct sockaddr
)) == 0);
639 /*****************************************************************************/
640 /* osl_createInetBroadcastAddr */
641 /*****************************************************************************/
642 oslSocketAddr SAL_CALL
osl_createInetBroadcastAddr (
643 rtl_uString
*strDottedAddr
,
646 sal_uInt32 nAddr
= OSL_INADDR_NONE
;
649 if (strDottedAddr
&& strDottedAddr
->length
)
651 /* Dotted host address for limited broadcast */
652 rtl_String
*pDottedAddr
= NULL
;
655 &pDottedAddr
, strDottedAddr
->buffer
, strDottedAddr
->length
,
656 RTL_TEXTENCODING_UTF8
, OUSTRING_TO_OSTRING_CVTFLAGS
);
658 nAddr
= inet_addr (pDottedAddr
->buffer
);
659 rtl_string_release (pDottedAddr
);
662 if (nAddr
!= OSL_INADDR_NONE
)
664 /* Limited broadcast */
665 nAddr
= ntohl(nAddr
);
666 if (IN_CLASSA(nAddr
))
668 nAddr
&= IN_CLASSA_NET
;
669 nAddr
|= IN_CLASSA_HOST
;
671 else if (IN_CLASSB(nAddr
))
673 nAddr
&= IN_CLASSB_NET
;
674 nAddr
|= IN_CLASSB_HOST
;
676 else if (IN_CLASSC(nAddr
))
678 nAddr
&= IN_CLASSC_NET
;
679 nAddr
|= IN_CLASSC_HOST
;
683 /* No broadcast in class D */
684 return ((oslSocketAddr
)NULL
);
686 nAddr
= htonl(nAddr
);
689 pAddr
= __osl_createSocketAddrWithFamily( osl_Socket_FamilyInet
, htons(Port
), nAddr
);
693 /*****************************************************************************/
694 /* osl_createInetSocketAddr */
695 /*****************************************************************************/
696 oslSocketAddr SAL_CALL
osl_createInetSocketAddr (
697 rtl_uString
*ustrDottedAddr
,
700 rtl_String
* strDottedAddr
=0;
702 sal_Char
* pszDottedAddr
=0;
704 if ( ustrDottedAddr
!= 0 )
706 rtl_uString2String( &strDottedAddr
,
707 rtl_uString_getStr(ustrDottedAddr
),
708 rtl_uString_getLength(ustrDottedAddr
),
709 RTL_TEXTENCODING_UTF8
,
710 OUSTRING_TO_OSTRING_CVTFLAGS
);
711 pszDottedAddr
= rtl_string_getStr(strDottedAddr
);
715 Addr
= osl_psz_createInetSocketAddr(pszDottedAddr
, Port
);
717 if ( strDottedAddr
!= 0 )
719 rtl_string_release(strDottedAddr
);
725 oslSocketAddr SAL_CALL
osl_psz_createInetSocketAddr (
726 const sal_Char
* pszDottedAddr
,
729 oslSocketAddr pAddr
= 0;
730 sal_Int32 Addr
= inet_addr(pszDottedAddr
);
733 /* valid dotted addr */
734 pAddr
= __osl_createSocketAddrWithFamily( osl_Socket_FamilyInet
, htons(Port
) , Addr
);
739 /*****************************************************************************/
740 /* osl_setAddrOfSocketAddr */
741 /*****************************************************************************/
742 oslSocketResult SAL_CALL
osl_setAddrOfSocketAddr( oslSocketAddr pAddr
, sal_Sequence
*pByteSeq
)
744 oslSocketResult res
= osl_Socket_Error
;
747 OSL_ASSERT( pByteSeq
);
749 if( pAddr
&& pByteSeq
)
751 struct sockaddr_in
* pSystemInetAddr
;
753 OSL_ASSERT( pAddr
->m_sockaddr
.sa_family
== FAMILY_TO_NATIVE( osl_Socket_FamilyInet
) );
754 OSL_ASSERT( pByteSeq
->nElements
== 4 );
756 pSystemInetAddr
= (struct sockaddr_in
* ) &(pAddr
->m_sockaddr
);
757 memcpy( &(pSystemInetAddr
->sin_addr
) , pByteSeq
->elements
, 4 );
763 /*****************************************************************************/
764 /* osl_getAddrOfSocketAddr */
765 /*****************************************************************************/
766 oslSocketResult SAL_CALL
osl_getAddrOfSocketAddr( oslSocketAddr pAddr
, sal_Sequence
**ppByteSeq
)
768 oslSocketResult res
= osl_Socket_Error
;
771 OSL_ASSERT( ppByteSeq
);
773 if( pAddr
&& ppByteSeq
)
775 struct sockaddr_in
* pSystemInetAddr
= (struct sockaddr_in
* ) &(pAddr
->m_sockaddr
);
776 rtl_byte_sequence_constructFromArray( ppByteSeq
, (sal_Int8
*) &(pSystemInetAddr
->sin_addr
),4);
783 /*****************************************************************************/
784 /* _osl_getFullQualifiedDomainName */
785 /*****************************************************************************/
787 /** try to figure out a full-qualified hostname, by adding the current domain
788 as given by the domainname program to the given hostname.
789 This function MUST NOT call gethostbyname since pHostName allready points
790 to data returned by gethostname and would be garbled: use gethostname_r
794 /* wrap around different interfaces to reentrant gethostbyname */
795 static struct hostent
* _osl_gethostbyname_r (
796 const char *name
, struct hostent
*result
,
797 char *buffer
, int buflen
, int *h_errnop
)
801 struct hostent
*__result
; /* will be the same as result */
803 __error
= gethostbyname_r (name
, result
, buffer
, buflen
,
804 &__result
, h_errnop
);
805 return __error
? NULL
: __result
;
810 return gethostbyname_r( name
, result
, buffer
, buflen
, h_errnop
);
814 static sal_Bool
_osl_getDomainName (sal_Char
*buffer
, sal_Int32 bufsiz
)
821 #if 0 // YD 17/04/06 libc panic for fork() from thread!=1
841 execv ("/bin/domainname", argv
);
842 // arriving here means exec failed
847 sal_Int32 k
= 0, n
= bufsiz
;
850 if ((k
= read (p
[0], buffer
, n
- 1)) > 0)
853 if (buffer
[k
- 1] == '\n')
858 waitpid (pid
, &nStatus
, 0);
871 static sal_Char
* _osl_getFullQualifiedDomainName (const sal_Char
*pHostName
)
873 # define DOMAINNAME_LENGTH 512
874 sal_uInt32 nLengthOfHostName
;
875 static sal_uInt32 nLengthOfDomainName
= 0;
876 static sal_Char
*pDomainName
= NULL
;
878 sal_Char
*pFullQualifiedName
;
880 FILE *pPipeToDomainnameExe
;
881 #endif /* OBSOLETE */
883 /* get a '\0' terminated domainname */
885 /* read default domainname default from environment */
886 if (nLengthOfDomainName
== 0)
888 sal_Char
*pEnvDomain
;
890 pEnvDomain
= getenv ("STAR_OVERRIDE_DOMAINNAME");
893 pDomainName
= strdup (pEnvDomain
);
894 nLengthOfDomainName
= strlen (pDomainName
);
899 if (nLengthOfDomainName
== 0)
901 sal_Char pDomainNameBuffer
[ DOMAINNAME_LENGTH
];
903 pDomainNameBuffer
[0] = '\0';
905 if (_osl_getDomainName (pDomainNameBuffer
, DOMAINNAME_LENGTH
))
907 pDomainName
= strdup (pDomainNameBuffer
);
908 nLengthOfDomainName
= strlen (pDomainName
);
916 /* call 'domainname > /usr/tmp/some-tmp-file', since
917 popen read pclose do block or core-dump,
918 (even the pipe-stuff that comes with pthreads) */
919 if (nLengthOfDomainName
== 0)
921 sal_Char tmp_name
[ L_tmpnam
];
923 sal_Char domain_call
[ L_tmpnam
+ 16 ] = "domainname > ";
928 strcat ( domain_call
, tmp_name
);
929 if ( (system ( domain_call
) == 0)
930 && ((tmp_file
= fopen( tmp_name
, "r" )) != NULL
) )
932 sal_Char pDomainNameBuffer
[ DOMAINNAME_LENGTH
];
934 pDomainNameBuffer
[0] = '\0';
936 if ( fgets ( pDomainNameBuffer
, DOMAINNAME_LENGTH
, tmp_file
) )
938 pDomainName
= strdup( pDomainNameBuffer
);
939 nLengthOfDomainName
= strlen( pDomainName
);
940 if ( ( nLengthOfDomainName
> 0 )
941 && ( pDomainName
[ nLengthOfDomainName
- 1] == '\n' ) )
942 pDomainName
[ --nLengthOfDomainName
] = '\0';
951 /* read the domainname from pipe to the program domainname */
952 if ( (nLengthOfDomainName
== 0)
953 && (pPipeToDomainnameExe
= popen( "domainname", "r")) )
956 sal_Char pDomainNameBuffer
[ DOMAINNAME_LENGTH
];
957 sal_Char
*pDomainNamePointer
;
959 pDomainNameBuffer
[0] = '\0';
961 pDomainNamePointer
= pDomainNameBuffer
;
962 while ( ((c
= getc( pPipeToDomainnameExe
)) != EOF
)
963 && (nLengthOfDomainName
< (DOMAINNAME_LENGTH
- 1)) )
967 nLengthOfDomainName
++ ;
968 *pDomainNamePointer
++ = (sal_Char
)c
;
971 *pDomainNamePointer
= '\0';
972 pDomainName
= strdup( pDomainNameBuffer
);
974 pclose( pPipeToDomainnameExe
);
978 #endif /* OBSOLETE */
980 /* compose hostname and domainname */
981 nLengthOfHostName
= strlen( pHostName
);
982 pFullQualifiedName
= (sal_Char
*) malloc( (nLengthOfHostName
+ 1
983 + nLengthOfDomainName
+ 1) * sizeof(sal_Char
) );
984 memcpy( pFullQualifiedName
, pHostName
,
985 (nLengthOfHostName
+ 1) * sizeof(sal_Char
) );
987 if ( nLengthOfDomainName
> 0 )
989 /* fqdn = hostname + '.' + domainname + '\0' */
990 pFullQualifiedName
[ nLengthOfHostName
] = '.';
991 memcpy( pFullQualifiedName
+ nLengthOfHostName
+ 1, pDomainName
,
992 nLengthOfDomainName
+ 1 );
995 /* check whether full-qualified name and hostname point to the same host
996 * should almost always be true */
997 if ( nLengthOfDomainName
> 0 )
999 struct hostent
*pQualifiedHostByName
;
1000 struct hostent
*pHostByName
;
1001 sal_Bool bHostsAreEqual
;
1003 /* buffer for calls to reentrant version of gethostbyname */
1004 struct hostent aHostByName
, aQualifiedHostByName
;
1005 sal_Char pHostBuffer
[ MAX_HOSTBUFFER_SIZE
];
1006 sal_Char pQualifiedHostBuffer
[ MAX_HOSTBUFFER_SIZE
];
1009 pHostBuffer
[0] = '\0';
1010 pQualifiedHostBuffer
[0] = '\0';
1012 /* get list of addresses */
1013 pQualifiedHostByName
= _osl_gethostbyname_r (
1015 &aQualifiedHostByName
, pQualifiedHostBuffer
,
1016 sizeof(pQualifiedHostBuffer
), &nErrorNo
);
1017 pHostByName
= _osl_gethostbyname_r (
1019 &aHostByName
, pHostBuffer
,
1020 sizeof(pHostBuffer
), &nErrorNo
);
1022 /* compare addresses */
1023 bHostsAreEqual
= sal_False
;
1024 if ( pQualifiedHostByName
&& pHostByName
)
1029 /* lists are expected to be (very) short */
1030 for ( p
= pQualifiedHostByName
->h_addr_list
; *p
!= NULL
; p
++ )
1032 for ( q
= pHostByName
->h_addr_list
; *q
!= NULL
; q
++ )
1034 /* in.s_addr may be in_addr_t or uint32_t or heaven knows */
1035 if ( memcmp( *p
, *q
, sizeof(in
.s_addr
) ) == 0 )
1037 bHostsAreEqual
= sal_True
;
1041 if ( bHostsAreEqual
)
1046 /* very strange case, but have to believe it: reduce the
1047 * full qualified name to the unqualified host name */
1048 if ( !bHostsAreEqual
)
1050 OSL_TRACE("_osl_getFullQualifiedDomainName: "
1051 "suspect FQDN: %s\n", pFullQualifiedName
);
1053 pFullQualifiedName
[ nLengthOfHostName
] = '\0';
1054 pFullQualifiedName
= (sal_Char
*)realloc ( pFullQualifiedName
,
1055 (nLengthOfHostName
+ 1) * sizeof( sal_Char
));
1059 /* always return a hostname looked up as carefully as possible
1060 * this string must be freed by the caller */
1061 return pFullQualifiedName
;
1064 /*****************************************************************************/
1065 /* _osl_isFullQualifiedDomainName */
1066 /*****************************************************************************/
1067 static sal_Bool
_osl_isFullQualifiedDomainName (const sal_Char
*pHostName
)
1069 /* a FQDN (aka 'hostname.domain.top_level_domain' )
1070 * is a name which contains a dot '.' in it ( would
1071 * match as well for 'hostname.' but is good enough
1073 return (sal_Bool
)( strchr( pHostName
, (int)'.' ) != NULL
);
1076 /*****************************************************************************/
1078 /*****************************************************************************/
1079 struct oslHostAddrImpl
1081 sal_Char
*pHostName
;
1082 oslSocketAddr pSockAddr
;
1085 static oslHostAddr
_osl_hostentToHostAddr (const struct hostent
*he
)
1087 oslHostAddr pAddr
= NULL
;
1088 oslSocketAddr pSockAddr
= 0;
1091 if ((he
== NULL
) || (he
->h_name
== NULL
) || (he
->h_addr_list
[0] == NULL
))
1092 return ((oslHostAddr
)NULL
);
1094 //YD 18/06/2006 win32 does this with unicode, see socket.cxx
1096 cn
= (sal_Char
*)malloc(strlen (he
->h_name
) + 1);
1099 return ((oslHostAddr
)NULL
);
1101 strcpy(cn
, he
->h_name
);
1103 #if 0 // YD 17/04/06 win32 doesn't it.
1104 if (_osl_isFullQualifiedDomainName(he
->h_name
))
1106 cn
= (sal_Char
*)malloc(strlen (he
->h_name
) + 1);
1109 return ((oslHostAddr
)NULL
);
1111 strcpy(cn
, he
->h_name
);
1115 cn
=_osl_getFullQualifiedDomainName (he
->h_name
);
1118 return ((oslHostAddr
)NULL
);
1122 pSockAddr
= __osl_createSocketAddr();
1123 OSL_ASSERT(pSockAddr
);
1124 if (pSockAddr
== NULL
)
1127 return ((oslHostAddr
)NULL
);
1130 pSockAddr
->m_sockaddr
.sa_family
= he
->h_addrtype
;
1131 if (pSockAddr
->m_sockaddr
.sa_family
== FAMILY_TO_NATIVE(osl_Socket_FamilyInet
))
1133 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(pSockAddr
->m_sockaddr
);
1135 &(sin
->sin_addr
.s_addr
),
1141 /* unknown address family */
1142 /* future extensions for new families might be implemented here */
1144 OSL_TRACE("_osl_hostentToHostAddr: unknown address family.\n");
1145 OSL_ASSERT(sal_False
);
1147 __osl_destroySocketAddr( pSockAddr
);
1149 return ((oslHostAddr
)NULL
);
1152 pAddr
= (oslHostAddr
) malloc(sizeof(struct oslHostAddrImpl
));
1156 __osl_destroySocketAddr( pSockAddr
);
1158 return ((oslHostAddr
)NULL
);
1161 pAddr
->pHostName
= cn
;
1162 pAddr
->pSockAddr
= pSockAddr
;
1167 /*****************************************************************************/
1168 /* osl_createHostAddr */
1169 /*****************************************************************************/
1170 oslHostAddr SAL_CALL
osl_createHostAddr (
1171 rtl_uString
*ustrHostname
,
1172 const oslSocketAddr Addr
)
1174 oslHostAddr HostAddr
;
1175 rtl_String
* strHostname
=0;
1176 sal_Char
* pszHostName
=0;
1178 if ( ustrHostname
!= 0 )
1180 rtl_uString2String( &strHostname
,
1181 rtl_uString_getStr(ustrHostname
),
1182 rtl_uString_getLength(ustrHostname
),
1183 RTL_TEXTENCODING_UTF8
,
1184 OUSTRING_TO_OSTRING_CVTFLAGS
);
1185 pszHostName
= rtl_string_getStr(strHostname
);
1188 HostAddr
= osl_psz_createHostAddr(pszHostName
,Addr
);
1190 if ( strHostname
!= 0 )
1192 rtl_string_release(strHostname
);
1199 oslHostAddr SAL_CALL
osl_psz_createHostAddr (
1200 const sal_Char
*pszHostname
,
1201 const oslSocketAddr pAddr
)
1203 oslHostAddr pHostAddr
;
1206 OSL_ASSERT(pszHostname
&& pAddr
);
1207 if ((pszHostname
== NULL
) || (pAddr
== NULL
))
1208 return ((oslHostAddr
)NULL
);
1210 cn
= (sal_Char
*)malloc(strlen (pszHostname
) + 1);
1213 return ((oslHostAddr
)NULL
);
1215 strcpy (cn
, pszHostname
);
1217 pHostAddr
= (oslHostAddr
) malloc(sizeof(struct oslHostAddrImpl
));
1218 OSL_ASSERT(pHostAddr
);
1222 return ((oslHostAddr
)NULL
);
1225 pHostAddr
->pHostName
= cn
;
1226 pHostAddr
->pSockAddr
= osl_copySocketAddr( pAddr
);
1231 /*****************************************************************************/
1232 /* osl_createHostAddrByName */
1233 /*****************************************************************************/
1234 oslHostAddr SAL_CALL
osl_createHostAddrByName(rtl_uString
*ustrHostname
)
1236 oslHostAddr HostAddr
;
1237 rtl_String
* strHostname
=0;
1238 sal_Char
* pszHostName
=0;
1240 if ( ustrHostname
!= 0 )
1242 rtl_uString2String( &strHostname
,
1243 rtl_uString_getStr(ustrHostname
),
1244 rtl_uString_getLength(ustrHostname
),
1245 RTL_TEXTENCODING_UTF8
,
1246 OUSTRING_TO_OSTRING_CVTFLAGS
);
1247 pszHostName
=rtl_string_getStr(strHostname
);
1250 HostAddr
= osl_psz_createHostAddrByName(pszHostName
);
1252 if ( strHostname
!= 0 )
1254 rtl_string_release(strHostname
);
1260 oslHostAddr SAL_CALL
osl_psz_createHostAddrByName (const sal_Char
*pszHostname
)
1265 static oslMutex mutex
= NULL
;
1268 mutex
= osl_createMutex();
1270 osl_acquireMutex(mutex
);
1272 he
= gethostbyname((sal_Char
*)pszHostname
);
1273 addr
= _osl_hostentToHostAddr (he
);
1275 osl_releaseMutex(mutex
);
1280 /*****************************************************************************/
1281 /* osl_createHostAddrByAddr */
1282 /*****************************************************************************/
1283 oslHostAddr SAL_CALL
osl_createHostAddrByAddr (const oslSocketAddr pAddr
)
1288 return ((oslHostAddr
)NULL
);
1290 if (pAddr
->m_sockaddr
.sa_family
== FAMILY_TO_NATIVE(osl_Socket_FamilyInet
))
1292 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)&(pAddr
->m_sockaddr
);
1295 if (sin
->sin_addr
.s_addr
== htonl(INADDR_ANY
))
1296 return ((oslHostAddr
)NULL
);
1298 he
= gethostbyaddr((sal_Char
*)&(sin
->sin_addr
),
1299 sizeof (sin
->sin_addr
),
1301 return _osl_hostentToHostAddr (he
);
1304 return ((oslHostAddr
)NULL
);
1307 /*****************************************************************************/
1308 /* osl_copyHostAddr */
1309 /*****************************************************************************/
1310 oslHostAddr SAL_CALL
osl_copyHostAddr (const oslHostAddr pAddr
)
1315 return osl_psz_createHostAddr (pAddr
->pHostName
, pAddr
->pSockAddr
);
1317 return ((oslHostAddr
)NULL
);
1320 /*****************************************************************************/
1321 /* osl_getHostnameOfHostAddr */
1322 /*****************************************************************************/
1323 void SAL_CALL
osl_getHostnameOfHostAddr (
1324 const oslHostAddr Addr
,
1325 rtl_uString
**ustrHostname
)
1327 const sal_Char
* pHostname
=0;
1329 pHostname
= osl_psz_getHostnameOfHostAddr(Addr
);
1331 rtl_uString_newFromAscii (ustrHostname
, pHostname
);
1336 const sal_Char
* SAL_CALL
osl_psz_getHostnameOfHostAddr (const oslHostAddr pAddr
)
1341 return pAddr
->pHostName
;
1346 /*****************************************************************************/
1347 /* osl_getSocketAddrOfHostAddr */
1348 /*****************************************************************************/
1349 oslSocketAddr SAL_CALL
osl_getSocketAddrOfHostAddr (const oslHostAddr pAddr
)
1354 return ((oslSocketAddr
)(pAddr
->pSockAddr
));
1359 /*****************************************************************************/
1360 /* osl_destroyHostAddr */
1361 /*****************************************************************************/
1362 void SAL_CALL
osl_destroyHostAddr (oslHostAddr pAddr
)
1366 if (pAddr
->pHostName
)
1367 free (pAddr
->pHostName
);
1368 if (pAddr
->pSockAddr
)
1369 osl_destroySocketAddr (pAddr
->pSockAddr
);
1374 /*****************************************************************************/
1375 /* osl_getLocalHostname */
1376 /*****************************************************************************/
1377 oslSocketResult SAL_CALL
osl_getLocalHostname(rtl_uString
**ustrLocalHostname
)
1379 oslSocketResult Result
;
1380 sal_Char pszHostname
[1024];
1382 pszHostname
[0] = '\0';
1384 Result
= osl_psz_getLocalHostname(pszHostname
,sizeof(pszHostname
));
1386 rtl_uString_newFromAscii(ustrLocalHostname
,pszHostname
);
1391 oslSocketResult SAL_CALL
osl_psz_getLocalHostname (
1392 sal_Char
*pBuffer
, sal_uInt32 nBufLen
)
1394 static sal_Char LocalHostname
[256] = "";
1396 if (strlen(LocalHostname
) == 0)
1398 const sal_Char
*pStr
;
1403 if (uname(&uts
) < 0)
1404 return osl_Socket_Error
;
1406 if ((strlen(uts
.nodename
) + 1) > nBufLen
)
1407 return osl_Socket_Error
;
1409 strncpy(LocalHostname
, uts
.nodename
, sizeof( LocalHostname
));
1410 #else /* BSD compatible */
1412 if (gethostname(LocalHostname
, sizeof(LocalHostname
)-1) != 0)
1413 return osl_Socket_Error
;
1414 LocalHostname
[sizeof(LocalHostname
)-1] = 0;
1417 /* check if we have an FQDN */
1418 if (strchr(LocalHostname
, '.') == NULL
)
1422 /* no, determine it via dns */
1423 Addr
= osl_psz_createHostAddrByName(LocalHostname
);
1425 if (Addr
&& (pStr
= osl_psz_getHostnameOfHostAddr(Addr
)) != NULL
)
1427 #if 0 /* OBSOLETE */
1429 #endif /* OBSOLETE */
1430 strcpy(LocalHostname
, pStr
);
1432 #if 0 /* OBSOLETE */
1433 /* already done by _osl_getFullQualifiedDomainName() with
1434 much better heuristics, so this may be contraproductive */
1436 /* no FQDN, last try append domain name */
1437 if ((pChr
= strchr(LocalHostname
, '.')) == NULL
)
1441 pChr
= &LocalHostname
[strlen(LocalHostname
)];
1443 if ( (fp
= popen("domainname", "r")) != 0 )
1449 while ((c
= getc(fp
)) != EOF
)
1452 *pChr
++ = (sal_Char
)c
;
1460 LocalHostname
[0] = '\0';
1462 #endif /* OBSOLETE */
1466 osl_destroyHostAddr(Addr
);
1470 if (strlen(LocalHostname
) > 0)
1472 strncpy(pBuffer
, LocalHostname
, nBufLen
);
1473 pBuffer
[nBufLen
- 1] = '\0';
1475 return osl_Socket_Ok
;
1478 return osl_Socket_Error
;
1481 /*****************************************************************************/
1482 /* osl_resolveHostname */
1483 /*****************************************************************************/
1484 oslSocketAddr SAL_CALL
osl_resolveHostname(rtl_uString
*ustrHostname
)
1487 rtl_String
* strHostname
=0;
1488 sal_Char
* pszHostName
=0;
1490 if ( ustrHostname
!= 0 )
1492 rtl_uString2String( &strHostname
,
1493 rtl_uString_getStr(ustrHostname
),
1494 rtl_uString_getLength(ustrHostname
),
1495 RTL_TEXTENCODING_UTF8
,
1496 OUSTRING_TO_OSTRING_CVTFLAGS
);
1497 pszHostName
= rtl_string_getStr(strHostname
);
1501 Addr
= osl_psz_resolveHostname(pszHostName
);
1503 if ( strHostname
!= 0 )
1505 rtl_string_release(strHostname
);
1513 oslSocketAddr SAL_CALL
osl_psz_resolveHostname(const sal_Char
* pszHostname
)
1515 struct oslHostAddrImpl
*pAddr
= (oslHostAddr
)osl_psz_createHostAddrByName(pszHostname
);
1519 oslSocketAddr SockAddr
= osl_copySocketAddr(pAddr
->pSockAddr
);
1521 osl_destroyHostAddr(pAddr
);
1526 return ((oslSocketAddr
)NULL
);
1529 /*****************************************************************************/
1530 /* osl_getServicePort */
1531 /*****************************************************************************/
1532 sal_Int32 SAL_CALL
osl_getServicePort(rtl_uString
*ustrServicename
, rtl_uString
*ustrProtocol
)
1535 rtl_String
* strServicename
=0;
1536 rtl_String
* strProtocol
=0;
1537 sal_Char
* pszServiceName
=0;
1538 sal_Char
* pszProtocol
=0;
1540 if ( ustrServicename
!= 0 )
1542 rtl_uString2String( &strServicename
,
1543 rtl_uString_getStr(ustrServicename
),
1544 rtl_uString_getLength(ustrServicename
),
1545 RTL_TEXTENCODING_UTF8
,
1546 OUSTRING_TO_OSTRING_CVTFLAGS
);
1547 pszServiceName
= rtl_string_getStr(strServicename
);
1550 if ( ustrProtocol
!= 0 )
1552 rtl_uString2String( &strProtocol
,
1553 rtl_uString_getStr(ustrProtocol
),
1554 rtl_uString_getLength(ustrProtocol
),
1555 RTL_TEXTENCODING_UTF8
,
1556 OUSTRING_TO_OSTRING_CVTFLAGS
);
1557 pszProtocol
= rtl_string_getStr(strProtocol
);
1560 nPort
= osl_psz_getServicePort(pszServiceName
,pszProtocol
);
1562 if ( strServicename
!= 0 )
1564 rtl_string_release(strServicename
);
1567 if ( strProtocol
!= 0 )
1569 rtl_string_release(strProtocol
);
1577 sal_Int32 SAL_CALL
osl_psz_getServicePort(const sal_Char
* pszServicename
,
1578 const sal_Char
* pszProtocol
)
1582 ps
= getservbyname(pszServicename
, pszProtocol
);
1585 return ntohs(ps
->s_port
);
1587 return OSL_INVALID_PORT
;
1590 /*****************************************************************************/
1591 /* osl_destroySocketAddr */
1592 /*****************************************************************************/
1593 void SAL_CALL
osl_destroySocketAddr(oslSocketAddr pAddr
)
1595 __osl_destroySocketAddr( pAddr
);
1598 /*****************************************************************************/
1599 /* osl_getFamilyOfSocketAddr */
1600 /*****************************************************************************/
1601 oslAddrFamily SAL_CALL
osl_getFamilyOfSocketAddr(oslSocketAddr pAddr
)
1606 return FAMILY_FROM_NATIVE(pAddr
->m_sockaddr
.sa_family
);
1608 return osl_Socket_FamilyInvalid
;
1611 /*****************************************************************************/
1612 /* osl_getInetPortOfSocketAddr */
1613 /*****************************************************************************/
1614 sal_Int32 SAL_CALL
osl_getInetPortOfSocketAddr(oslSocketAddr pAddr
)
1619 struct sockaddr_in
* pSystemInetAddr
= (struct sockaddr_in
*)&(pAddr
->m_sockaddr
);
1621 if ( pSystemInetAddr
->sin_family
== FAMILY_TO_NATIVE(osl_Socket_FamilyInet
))
1622 return ntohs(pSystemInetAddr
->sin_port
);
1624 return OSL_INVALID_PORT
;
1627 /*****************************************************************************/
1628 /* osl_setInetPortOfSocketAddr */
1629 /*****************************************************************************/
1630 sal_Bool SAL_CALL
osl_setInetPortOfSocketAddr(oslSocketAddr pAddr
, sal_Int32 Port
)
1635 struct sockaddr_in
* pSystemInetAddr
= (struct sockaddr_in
*)&(pAddr
->m_sockaddr
);
1636 if ( pSystemInetAddr
->sin_family
== FAMILY_TO_NATIVE(osl_Socket_FamilyInet
))
1638 pSystemInetAddr
->sin_port
= htons((short)Port
);
1643 /* this is not a inet-addr => can't set port */
1647 /*****************************************************************************/
1648 /* osl_getHostnameOfSocketAddr */
1649 /*****************************************************************************/
1650 oslSocketResult SAL_CALL
osl_getHostnameOfSocketAddr(oslSocketAddr Addr
, rtl_uString
**ustrHostname
)
1652 oslSocketResult Result
;
1653 sal_Char pszHostname
[1024];
1655 pszHostname
[0] = '\0';
1657 Result
= osl_psz_getHostnameOfSocketAddr(Addr
,pszHostname
,sizeof(pszHostname
));
1659 rtl_uString_newFromAscii(ustrHostname
,pszHostname
);
1665 oslSocketResult SAL_CALL
osl_psz_getHostnameOfSocketAddr(oslSocketAddr pAddr
,
1666 sal_Char
*pBuffer
, sal_uInt32 BufferSize
)
1668 oslHostAddr pHostAddr
= (oslHostAddr
)osl_createHostAddrByAddr(pAddr
);
1672 strncpy(pBuffer
, pHostAddr
->pHostName
, BufferSize
);
1674 pBuffer
[BufferSize
- 1] = '\0';
1676 osl_destroyHostAddr(pHostAddr
);
1678 return osl_Socket_Ok
;
1681 return osl_Socket_Error
;
1684 /*****************************************************************************/
1685 /* osl_getDottedInetAddrOfSocketAddr */
1686 /*****************************************************************************/
1687 oslSocketResult SAL_CALL
osl_getDottedInetAddrOfSocketAddr(oslSocketAddr Addr
, rtl_uString
**ustrDottedInetAddr
)
1689 oslSocketResult Result
;
1690 sal_Char pszDottedInetAddr
[1024];
1692 pszDottedInetAddr
[0] = '\0';
1694 Result
= osl_psz_getDottedInetAddrOfSocketAddr(Addr
,pszDottedInetAddr
,sizeof(pszDottedInetAddr
));
1696 rtl_uString_newFromAscii(ustrDottedInetAddr
,pszDottedInetAddr
);
1702 oslSocketResult SAL_CALL
osl_psz_getDottedInetAddrOfSocketAddr(oslSocketAddr pAddr
,
1703 sal_Char
*pBuffer
, sal_uInt32 BufferSize
)
1709 struct sockaddr_in
* pSystemInetAddr
= ( struct sockaddr_in
* ) &(pAddr
->m_sockaddr
);
1711 if (pSystemInetAddr
->sin_family
== FAMILY_TO_NATIVE(osl_Socket_FamilyInet
))
1713 strncpy(pBuffer
, inet_ntoa(pSystemInetAddr
->sin_addr
), BufferSize
);
1714 pBuffer
[BufferSize
- 1] = '\0';
1716 return osl_Socket_Ok
;
1720 return osl_Socket_Error
;
1723 #if 0 /* OBSOLETE */
1724 /*****************************************************************************/
1725 /* osl_getIpxNetNumber */
1726 /*****************************************************************************/
1727 oslSocketResult SAL_CALL
osl_getIpxNetNumber(oslSocketAddr Addr
,
1728 oslSocketIpxNetNumber NetNumber
)
1731 struct sockaddr_ipx
* pAddr
;
1733 pAddr
= (struct sockaddr_ipx
*)Addr
;
1737 if (pAddr
&& (pAddr
->sa_family
== FAMILY_TO_NATIVE(osl_Socket_FamilyIpx
)))
1739 memcpy(NetNumber
, pAddr
->sa_netnum
, sizeof(NetNumber
));
1741 return osl_Socket_Ok
;
1744 return osl_Socket_Error
;
1748 /*****************************************************************************/
1749 /* osl_getIpxNodeNumber */
1750 /*****************************************************************************/
1751 oslSocketResult SAL_CALL
osl_getIpxNodeNumber(oslSocketAddr Addr
,
1752 oslSocketIpxNodeNumber NodeNumber
)
1755 struct sockaddr_ipx
* pAddr
;
1757 pAddr
= (struct sockaddr_ipx
*)Addr
;
1761 if (pAddr
&& (pAddr
->sa_family
== FAMILY_TO_NATIVE(osl_Socket_FamilyIpx
)))
1763 memcpy(NodeNumber
, pAddr
->sa_nodenum
, sizeof(NodeNumber
));
1765 return osl_Socket_Ok
;
1768 return osl_Socket_Error
;
1772 /*****************************************************************************/
1773 /* osl_getIpxSocketNumber */
1774 /*****************************************************************************/
1775 sal_Int32 SAL_CALL
osl_getIpxSocketNumber(oslSocketAddr Addr
)
1777 struct sockaddr_ipx
* pAddr
= (struct sockaddr_ipx
*)Addr
;
1780 if (pAddr
&& (pAddr
->sa_family
== FAMILY_TO_NATIVE(osl_Socket_FamilyIpx
)))
1781 return pAddr
->sa_socket
;
1783 return OSL_INVALID_IPX_SOCKET_NO
;
1786 #endif /* OBSOLETE */
1788 /*****************************************************************************/
1789 /* osl_createSocket */
1790 /*****************************************************************************/
1791 oslSocket SAL_CALL
osl_createSocket(oslAddrFamily Family
,
1793 oslProtocol Protocol
)
1799 pSocket
= __osl_createSocketImpl(OSL_INVALID_SOCKET
);
1802 pSocket
->m_Socket
= socket(FAMILY_TO_NATIVE(Family
),
1803 TYPE_TO_NATIVE(Type
),
1804 PROTOCOL_TO_NATIVE(Protocol
));
1806 /* creation failed => free memory */
1807 if(pSocket
->m_Socket
== OSL_INVALID_SOCKET
)
1809 OSL_TRACE("osl_createSocket failed. Errno: %d; %s\n",
1813 __osl_destroySocketImpl((pSocket
));
1818 /* set close-on-exec flag */
1819 if ((Flags
= fcntl(pSocket
->m_Socket
, F_GETFD
, 0)) != -1)
1821 Flags
|= FD_CLOEXEC
;
1822 if (fcntl(pSocket
->m_Socket
, F_SETFD
, Flags
) == -1)
1824 pSocket
->m_nLastError
=errno
;
1825 OSL_TRACE("osl_createSocket failed changing socket flags. Errno: %d; %s\n",
1832 pSocket
->m_nLastError
=errno
;
1836 pSocket
->m_CloseCallback
= NULL
;
1837 pSocket
->m_CallbackArg
= NULL
;
1843 void SAL_CALL
osl_acquireSocket(oslSocket pSocket
)
1845 osl_incrementInterlockedCount( &(pSocket
->m_nRefCount
) );
1848 void SAL_CALL
osl_releaseSocket( oslSocket pSocket
)
1850 if( pSocket
&& 0 == osl_decrementInterlockedCount( &(pSocket
->m_nRefCount
) ) )
1853 if ( pSocket
->m_bIsAccepting
== sal_True
)
1855 OSL_ENSURE(0, "osl_destroySocket : attempt to destroy socket while accepting\n");
1859 osl_closeSocket( pSocket
);
1860 __osl_destroySocketImpl( pSocket
);
1866 /*****************************************************************************/
1867 /* osl_closeSocket */
1868 /*****************************************************************************/
1869 void SAL_CALL
osl_closeSocket(oslSocket pSocket
)
1874 /* socket already invalid */
1878 pSocket
->m_nLastError
=0;
1879 nFD
= pSocket
->m_Socket
;
1881 pSocket
->m_Socket
= OSL_INVALID_SOCKET
;
1884 pSocket
->m_bIsInShutdown
= sal_True
;
1886 if ( pSocket
->m_bIsAccepting
== sal_True
)
1889 struct sockaddr aSockAddr
;
1890 socklen_t nSockLen
= sizeof(aSockAddr
);
1892 nRet
= getsockname(nFD
, &aSockAddr
, &nSockLen
);
1893 #if OSL_DEBUG_LEVEL > 1
1896 perror("getsockname");
1898 #endif /* OSL_DEBUG_LEVEL */
1900 if ( aSockAddr
.sa_family
== AF_INET
)
1902 struct sockaddr_in
* pSockAddrIn
= (struct sockaddr_in
*) &aSockAddr
;
1904 if ( pSockAddrIn
->sin_addr
.s_addr
== htonl(INADDR_ANY
) )
1906 pSockAddrIn
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1909 nConnFD
= socket(AF_INET
, SOCK_STREAM
, 0);
1910 #if OSL_DEBUG_LEVEL > 1
1915 #endif /* OSL_DEBUG_LEVEL */
1917 nRet
= connect(nConnFD
, &aSockAddr
, sizeof(aSockAddr
));
1918 #if OSL_DEBUG_LEVEL > 1
1923 #endif /* OSL_DEBUG_LEVEL */
1929 /* registrierten Callback ausfuehren */
1930 if (pSocket
->m_CloseCallback
!= NULL
)
1932 pSocket
->m_CloseCallback(pSocket
->m_CallbackArg
);
1938 pSocket
->m_nLastError
=errno
;
1939 OSL_TRACE("closeSocket close error '%s'\n",strerror(errno
));
1942 pSocket
->m_Socket
= OSL_INVALID_SOCKET
;
1945 /*****************************************************************************/
1946 /* osl_getLocalAddrOfSocket */
1947 /* Note that I rely on the fact that oslSocketAddr and struct sockaddr */
1948 /* are the same! I don't like it very much but see no other easy way to conceal */
1949 /* the struct sockaddr from the eyes of the user. */
1950 /*****************************************************************************/
1951 oslSocketAddr SAL_CALL
osl_getLocalAddrOfSocket(oslSocket pSocket
)
1953 #if defined(LINUX) || defined(FREEBSD)
1956 /* mfe: Solaris 'cc +w' means Addrlen should be signed! */
1957 /* it's really defined as 'int*' in /usr/include/sys/socket.h! */
1958 /* the man page says it expects a 'size_t' */
1961 struct sockaddr Addr
;
1962 oslSocketAddr pAddr
;
1964 if (pSocket
== NULL
) /* ENOTSOCK */
1965 return ((oslSocketAddr
)NULL
);
1967 AddrLen
= sizeof(struct sockaddr
);
1969 if (getsockname(pSocket
->m_Socket
, &Addr
, PTR_SIZE_T(AddrLen
)) == OSL_SOCKET_ERROR
)
1970 return ((oslSocketAddr
)NULL
);
1972 pAddr
= __osl_createSocketAddrFromSystem( &Addr
);
1976 /*****************************************************************************/
1977 /* osl_getPeerAddrOfSocket */
1978 /*****************************************************************************/
1979 oslSocketAddr SAL_CALL
osl_getPeerAddrOfSocket(oslSocket pSocket
)
1982 struct sockaddr Addr
;
1984 OSL_ASSERT(pSocket
);
1990 pSocket
->m_nLastError
=0;
1991 AddrLen
= sizeof(struct sockaddr
);
1993 if(getpeername(pSocket
->m_Socket
, &Addr
, (int*)PTR_SIZE_T(AddrLen
)) == OSL_SOCKET_ERROR
)
1995 pSocket
->m_nLastError
=errno
;
1998 return __osl_createSocketAddrFromSystem( &Addr
);
2001 /*****************************************************************************/
2002 /* osl_bindAddrToSocket */
2003 /*****************************************************************************/
2004 sal_Bool SAL_CALL
osl_bindAddrToSocket(oslSocket pSocket
,
2005 oslSocketAddr pAddr
)
2009 OSL_ASSERT(pSocket
&& pAddr
);
2010 if ( pSocket
== 0 || pAddr
== 0 )
2015 pSocket
->m_nLastError
=0;
2017 nRet
= bind(pSocket
->m_Socket
, &(pAddr
->m_sockaddr
), sizeof(struct sockaddr
));
2019 if ( nRet
== OSL_SOCKET_ERROR
)
2021 pSocket
->m_nLastError
=errno
;
2029 /*****************************************************************************/
2030 /* osl_listenOnSocket */
2031 /*****************************************************************************/
2032 sal_Bool SAL_CALL
osl_listenOnSocket(oslSocket pSocket
,
2033 sal_Int32 MaxPendingConnections
)
2037 OSL_ASSERT(pSocket
);
2043 pSocket
->m_nLastError
=0;
2045 nRet
= listen(pSocket
->m_Socket
,
2046 MaxPendingConnections
== -1 ?
2048 MaxPendingConnections
);
2049 if ( nRet
== OSL_SOCKET_ERROR
)
2051 pSocket
->m_nLastError
=errno
;
2059 /*****************************************************************************/
2060 /* osl_connectSocketTo */
2061 /*****************************************************************************/
2062 oslSocketResult SAL_CALL
osl_connectSocketTo(oslSocket pSocket
,
2063 oslSocketAddr pAddr
,
2064 const TimeValue
* pTimeout
)
2070 oslSocketResult Result
= osl_Socket_Ok
;
2072 OSL_PRECOND(pSocket
, "osl_connectSocketTo(): need a valid socket!\n");
2076 return osl_Socket_Error
;
2079 pSocket
->m_nLastError
=0;
2081 if (osl_isNonBlockingMode(pSocket
))
2083 if (connect(pSocket
->m_Socket
,
2084 &(pAddr
->m_sockaddr
),
2085 sizeof(struct sockaddr
)) != OSL_SOCKET_ERROR
)
2086 return osl_Socket_Ok
;
2088 if (errno
== EWOULDBLOCK
|| errno
== EINPROGRESS
)
2090 pSocket
->m_nLastError
=EINPROGRESS
;
2091 return osl_Socket_InProgress
;
2095 pSocket
->m_nLastError
=errno
;
2096 OSL_TRACE("can't connect : '%s'",strerror(errno
));
2097 return osl_Socket_Error
;
2100 /* set socket temporarily to non-blocking */
2101 OSL_VERIFY(osl_enableNonBlockingMode(pSocket
, sal_True
));
2103 /* initiate connect */
2104 if(connect(pSocket
->m_Socket
,
2105 &(pAddr
->m_sockaddr
),
2106 sizeof(struct sockaddr
)) != OSL_SOCKET_ERROR
)
2108 /* immediate connection */
2109 osl_enableNonBlockingMode(pSocket
, sal_False
);
2111 return osl_Socket_Ok
;
2115 /* really an error or just delayed? */
2116 if (errno
!= EINPROGRESS
)
2118 pSocket
->m_nLastError
=errno
;
2120 "osl_connectSocketTo(): connect failed: errno: %d (%s)\n",
2121 errno
, strerror(errno
));
2123 osl_enableNonBlockingMode(pSocket
, sal_False
);
2124 return osl_Socket_Error
;
2129 /* prepare select set for socket */
2132 FD_SET(pSocket
->m_Socket
, &WriteSet
);
2133 FD_SET(pSocket
->m_Socket
, &ExcptSet
);
2135 /* prepare timeout */
2138 /* divide milliseconds into seconds and microseconds */
2139 tv
.tv_sec
= pTimeout
->Seconds
;
2140 tv
.tv_usec
= pTimeout
->Nanosec
/ 1000L;
2144 ReadyHandles
= select(pSocket
->m_Socket
+1,
2146 PTR_FD_SET(WriteSet
),
2147 PTR_FD_SET(ExcptSet
),
2148 (pTimeout
) ? &tv
: 0);
2150 if (ReadyHandles
> 0) /* connected */
2152 if ( FD_ISSET(pSocket
->m_Socket
, &WriteSet
) )
2156 /* mfe: Solaris 'cc +w' means 5th argument should be a 'int*'!
2157 it's really defined as 'int*' in /usr/include/sys/socket.h!
2158 the man page says it expects a 'size_t*'
2160 int nErrorSize
= sizeof( nErrorCode
);
2162 size_t nErrorSize
= sizeof( nErrorCode
);
2167 nSockOpt
= getsockopt ( pSocket
->m_Socket
, SOL_SOCKET
, SO_ERROR
,
2169 /* mfe: Solaris 'cc +w' means 4th argument should be a 'char*'!
2170 it's really defined as 'char*' in /usr/include/sys/socket.h!
2171 the man page says it expects a 'void*'
2175 &nErrorCode
, (int*)&nErrorSize
);
2176 if ( (nSockOpt
== 0) && (nErrorCode
== 0))
2177 Result
= osl_Socket_Ok
;
2179 Result
= osl_Socket_Error
;
2183 Result
= osl_Socket_Error
;
2186 else if (ReadyHandles
< 0) /* error */
2188 if (errno
== EBADF
) /* most probably interrupted by close() */
2190 /* do not access pSockImpl because it is about to be or */
2191 /* already destroyed */
2192 return osl_Socket_Interrupted
;
2196 pSocket
->m_nLastError
=errno
;
2197 Result
= osl_Socket_Error
;
2202 pSocket
->m_nLastError
=errno
;
2203 Result
= osl_Socket_TimedOut
;
2206 osl_enableNonBlockingMode(pSocket
, sal_False
);
2212 /*****************************************************************************/
2213 /* osl_acceptConnectionOnSocket */
2214 /*****************************************************************************/
2215 oslSocket SAL_CALL
osl_acceptConnectionOnSocket(oslSocket pSocket
,
2216 oslSocketAddr
* ppAddr
)
2218 struct sockaddr Addr
;
2219 int Connection
, Flags
;
2220 sal_uInt32 AddrLen
= sizeof(struct sockaddr
);
2221 oslSocket pConnectionSockImpl
;
2223 OSL_ASSERT(pSocket
);
2229 pSocket
->m_nLastError
=0;
2231 pSocket
->m_bIsAccepting
= sal_True
;
2234 if( ppAddr
&& *ppAddr
)
2236 osl_destroySocketAddr( *ppAddr
);
2240 /* prevent Linux EINTR behaviour */
2243 Connection
= accept(pSocket
->m_Socket
, &Addr
, (int*)PTR_SIZE_T(AddrLen
));
2244 } while (Connection
== -1 && errno
== EINTR
);
2247 /* accept failed? */
2248 if( Connection
== OSL_SOCKET_ERROR
)
2250 pSocket
->m_nLastError
=errno
;
2251 OSL_TRACE("osl_acceptConnectionOnSocket : accept error '%s'\n",strerror(errno
));
2254 pSocket
->m_bIsAccepting
= sal_False
;
2259 OSL_ASSERT(AddrLen
== sizeof(struct sockaddr
));
2263 if ( pSocket
->m_bIsInShutdown
== sal_True
)
2266 OSL_TRACE("osl_acceptConnectionOnSocket : close while accept\n");
2274 *ppAddr
= __osl_createSocketAddrFromSystem(&Addr
);
2278 pConnectionSockImpl
= __osl_createSocketImpl(OSL_INVALID_SOCKET
);
2280 /* set close-on-exec flag */
2281 if ((Flags
= fcntl(Connection
, F_GETFD
, 0)) != -1)
2283 Flags
|= FD_CLOEXEC
;
2284 if (fcntl(Connection
, F_SETFD
, Flags
) == -1)
2286 pSocket
->m_nLastError
=errno
;
2287 OSL_TRACE("osl_acceptConnectionOnSocket failed changing socket flags. Errno: %d (%s)\n",
2294 pConnectionSockImpl
->m_Socket
= Connection
;
2295 pConnectionSockImpl
->m_nLastError
= 0;
2296 pConnectionSockImpl
->m_CloseCallback
= NULL
;
2297 pConnectionSockImpl
->m_CallbackArg
= NULL
;
2299 pConnectionSockImpl
->m_bIsAccepting
= sal_False
;
2301 pSocket
->m_bIsAccepting
= sal_False
;
2303 return pConnectionSockImpl
;
2306 /*****************************************************************************/
2307 /* osl_receiveSocket */
2308 /*****************************************************************************/
2309 sal_Int32 SAL_CALL
osl_receiveSocket(oslSocket pSocket
,
2311 sal_uInt32 BytesToRead
,
2312 oslSocketMsgFlag Flag
)
2316 OSL_ASSERT(pSocket
);
2319 OSL_TRACE("osl_receiveSocket : Invalid socket");
2323 pSocket
->m_nLastError
=0;
2327 nRead
= recv(pSocket
->m_Socket
,
2330 MSG_FLAG_TO_NATIVE(Flag
));
2331 } while ( nRead
< 0 && errno
== EINTR
);
2335 pSocket
->m_nLastError
=errno
;
2336 OSL_TRACE("osl_receiveSocket failed : %i '%s'",nRead
,strerror(errno
));
2338 else if ( nRead
== 0 )
2340 OSL_TRACE("osl_receiveSocket failed : %i '%s'",nRead
,"EOL");
2347 /*****************************************************************************/
2348 /* osl_receiveFromSocket */
2349 /*****************************************************************************/
2350 sal_Int32 SAL_CALL
osl_receiveFromSocket(oslSocket pSocket
,
2351 oslSocketAddr pSenderAddr
,
2353 sal_uInt32 BufferSize
,
2354 oslSocketMsgFlag Flag
)
2357 struct sockaddr
*pSystemSockAddr
= 0;
2361 AddrLen
= sizeof( struct sockaddr
);
2362 pSystemSockAddr
= &(pSenderAddr
->m_sockaddr
);
2365 OSL_ASSERT(pSocket
);
2368 OSL_TRACE("osl_receiveFromSocket : Invalid socket");
2372 pSocket
->m_nLastError
=0;
2374 nRead
= recvfrom(pSocket
->m_Socket
,
2377 MSG_FLAG_TO_NATIVE(Flag
),
2379 PTR_SIZE_T(AddrLen
));
2383 pSocket
->m_nLastError
=errno
;
2384 OSL_TRACE("osl_receiveFromSocket failed : %i '%s'",nRead
,strerror(errno
));
2386 else if ( nRead
== 0 )
2388 OSL_TRACE("osl_receiveSocket failed : %i '%s'",nRead
,"EOL");
2395 /*****************************************************************************/
2396 /* osl_sendSocket */
2397 /*****************************************************************************/
2398 sal_Int32 SAL_CALL
osl_sendSocket(oslSocket pSocket
,
2399 const void* pBuffer
,
2400 sal_uInt32 BytesToSend
,
2401 oslSocketMsgFlag Flag
)
2405 OSL_ASSERT(pSocket
);
2408 OSL_TRACE("osl_sendSocket : Invalid socket");
2412 pSocket
->m_nLastError
=0;
2416 nWritten
= send(pSocket
->m_Socket
,
2419 MSG_FLAG_TO_NATIVE(Flag
));
2420 } while ( nWritten
< 0 && errno
== EINTR
);
2425 pSocket
->m_nLastError
=errno
;
2426 OSL_TRACE("osl_sendSocket failed : %i '%s'",nWritten
,strerror(errno
));
2428 else if ( nWritten
== 0 )
2430 OSL_TRACE("osl_sendSocket failed : %i '%s'",nWritten
,"EOL");
2436 /*****************************************************************************/
2437 /* osl_sendToSocket */
2438 /*****************************************************************************/
2439 sal_Int32 SAL_CALL
osl_sendToSocket(oslSocket pSocket
,
2440 oslSocketAddr ReceiverAddr
,
2441 const void* pBuffer
,
2442 sal_uInt32 BytesToSend
,
2443 oslSocketMsgFlag Flag
)
2447 struct sockaddr
*pSystemSockAddr
= 0;
2451 pSystemSockAddr
= &(ReceiverAddr
->m_sockaddr
);
2452 AddrLen
= sizeof( struct sockaddr
);
2455 OSL_ASSERT(pSocket
);
2458 OSL_TRACE("osl_sendToSocket : Invalid socket");
2462 pSocket
->m_nLastError
=0;
2464 /* ReceiverAddr might be 0 when used on a connected socket. */
2465 /* Then sendto should behave like send. */
2467 nWritten
= sendto(pSocket
->m_Socket
,
2470 MSG_FLAG_TO_NATIVE(Flag
),
2476 pSocket
->m_nLastError
=errno
;
2477 OSL_TRACE("osl_sendToSocket failed : %i '%s'",nWritten
,strerror(errno
));
2479 else if ( nWritten
== 0 )
2481 OSL_TRACE("osl_sendToSocket failed : %i '%s'",nWritten
,"EOL");
2487 /*****************************************************************************/
2488 /* osl_readSocket */
2489 /*****************************************************************************/
2490 sal_Int32 SAL_CALL
osl_readSocket (
2491 oslSocket pSocket
, void *pBuffer
, sal_Int32 n
)
2493 sal_uInt8
* Ptr
= (sal_uInt8
*)pBuffer
;
2494 sal_uInt32 BytesRead
= 0;
2495 sal_uInt32 BytesToRead
= n
;
2497 OSL_ASSERT( pSocket
);
2499 /* loop until all desired bytes were read or an error occured */
2500 while (BytesToRead
> 0)
2503 RetVal
= osl_receiveSocket(pSocket
,
2506 osl_Socket_MsgNormal
);
2508 /* error occured? */
2514 BytesToRead
-= RetVal
;
2515 BytesRead
+= RetVal
;
2522 /*****************************************************************************/
2523 /* osl_writeSocket */
2524 /*****************************************************************************/
2525 sal_Int32 SAL_CALL
osl_writeSocket(
2526 oslSocket pSocket
, const void *pBuffer
, sal_Int32 n
)
2528 /* loop until all desired bytes were send or an error occured */
2529 sal_uInt32 BytesSend
= 0;
2530 sal_uInt32 BytesToSend
= n
;
2531 sal_uInt8
*Ptr
= ( sal_uInt8
* )pBuffer
;
2533 OSL_ASSERT( pSocket
);
2535 while (BytesToSend
> 0)
2539 RetVal
= osl_sendSocket( pSocket
,Ptr
,BytesToSend
,osl_Socket_MsgNormal
);
2541 /* error occured? */
2547 BytesToSend
-= RetVal
;
2548 BytesSend
+= RetVal
;
2555 /*****************************************************************************/
2556 /* __osl_socket_poll */
2557 /*****************************************************************************/
2559 #ifdef HAVE_POLL_H /* poll() */
2561 sal_Bool
__osl_socket_poll (
2563 const TimeValue
* pTimeout
,
2570 OSL_ASSERT(pSocket
);
2571 pSocket
->m_nLastError
= 0;
2573 fds
.fd
= pSocket
->m_Socket
;
2574 fds
.events
= nEvent
;
2580 /* Convert to [ms] */
2581 timeout
= pTimeout
->Seconds
* 1000;
2582 timeout
+= pTimeout
->Nanosec
/ (1000 * 1000);
2585 result
= poll (&fds
, 1, timeout
);
2588 pSocket
->m_nLastError
= errno
;
2589 OSL_TRACE("__osl_socket_poll(): poll error: %d (%s)",
2590 errno
, strerror(errno
));
2599 return ((fds
.revents
& nEvent
) == nEvent
);
2602 #else /* select() */
2604 sal_Bool
__osl_socket_poll (
2606 const TimeValue
* pTimeout
,
2613 OSL_ASSERT(pSocket
);
2614 pSocket
->m_nLastError
= 0;
2617 FD_SET(pSocket
->m_Socket
, &fds
);
2621 /* Convert to 'timeval' */
2622 tv
.tv_sec
= pTimeout
->Seconds
;
2623 tv
.tv_usec
= pTimeout
->Nanosec
/ 1000;
2627 pSocket
->m_Socket
+ 1,
2628 (nEvent
== POLLIN
) ? PTR_FD_SET(fds
) : NULL
,
2629 (nEvent
== POLLOUT
) ? PTR_FD_SET(fds
) : NULL
,
2630 (nEvent
== POLLPRI
) ? PTR_FD_SET(fds
) : NULL
,
2631 (pTimeout
) ? &tv
: NULL
);
2635 pSocket
->m_nLastError
= errno
;
2636 OSL_TRACE("__osl_socket_poll(): select error: %d (%s)",
2637 errno
, strerror(errno
));
2646 return (FD_ISSET(pSocket
->m_Socket
, &fds
) ? sal_True
: sal_False
);
2649 #endif /* HAVE_POLL_H */
2651 /*****************************************************************************/
2652 /* osl_isReceiveReady */
2653 /*****************************************************************************/
2654 sal_Bool SAL_CALL
osl_isReceiveReady (
2655 oslSocket pSocket
, const TimeValue
* pTimeout
)
2657 OSL_ASSERT(pSocket
);
2658 if (pSocket
== NULL
)
2664 return __osl_socket_poll (pSocket
, pTimeout
, POLLIN
);
2667 /*****************************************************************************/
2668 /* osl_isSendReady */
2669 /*****************************************************************************/
2670 sal_Bool SAL_CALL
osl_isSendReady (
2671 oslSocket pSocket
, const TimeValue
* pTimeout
)
2673 OSL_ASSERT(pSocket
);
2674 if (pSocket
== NULL
)
2680 return __osl_socket_poll (pSocket
, pTimeout
, POLLOUT
);
2683 /*****************************************************************************/
2684 /* osl_isExceptionPending */
2685 /*****************************************************************************/
2686 sal_Bool SAL_CALL
osl_isExceptionPending (
2687 oslSocket pSocket
, const TimeValue
* pTimeout
)
2689 OSL_ASSERT(pSocket
);
2690 if (pSocket
== NULL
)
2696 return __osl_socket_poll (pSocket
, pTimeout
, POLLPRI
);
2699 /*****************************************************************************/
2700 /* osl_shutdownSocket */
2701 /*****************************************************************************/
2702 sal_Bool SAL_CALL
osl_shutdownSocket(oslSocket pSocket
,
2703 oslSocketDirection Direction
)
2707 OSL_ASSERT(pSocket
);
2713 pSocket
->m_nLastError
=0;
2715 nRet
=shutdown(pSocket
->m_Socket
, DIRECTION_TO_NATIVE(Direction
));
2718 pSocket
->m_nLastError
=errno
;
2719 OSL_TRACE("shutdown error '%s'\n",strerror(errno
));
2725 /*****************************************************************************/
2726 /* osl_getSocketOption */
2727 /*****************************************************************************/
2728 sal_Int32 SAL_CALL
osl_getSocketOption(oslSocket pSocket
,
2729 oslSocketOptionLevel Level
,
2730 oslSocketOption Option
,
2732 sal_uInt32 BufferLen
)
2734 OSL_ASSERT(pSocket
);
2740 pSocket
->m_nLastError
=0;
2742 if(getsockopt(pSocket
->m_Socket
,
2743 OPTION_LEVEL_TO_NATIVE(Level
),
2744 OPTION_TO_NATIVE(Option
),
2746 (int*)PTR_SIZE_T(BufferLen
)) == -1)
2748 pSocket
->m_nLastError
=errno
;
2755 /*****************************************************************************/
2756 /* osl_setSocketOption */
2757 /*****************************************************************************/
2758 sal_Bool SAL_CALL
osl_setSocketOption(oslSocket pSocket
,
2759 oslSocketOptionLevel Level
,
2760 oslSocketOption Option
,
2762 sal_uInt32 BufferLen
)
2766 OSL_ASSERT(pSocket
);
2772 pSocket
->m_nLastError
=0;
2774 nRet
= setsockopt(pSocket
->m_Socket
,
2775 OPTION_LEVEL_TO_NATIVE(Level
),
2776 OPTION_TO_NATIVE(Option
),
2782 pSocket
->m_nLastError
=errno
;
2789 /*****************************************************************************/
2790 /* osl_enableNonBlockingMode */
2791 /*****************************************************************************/
2792 sal_Bool SAL_CALL
osl_enableNonBlockingMode(oslSocket pSocket
,
2798 OSL_ASSERT(pSocket
);
2804 pSocket
->m_nLastError
=0;
2806 flags
= fcntl(pSocket
->m_Socket
, F_GETFL
, 0);
2809 flags
|= O_NONBLOCK
;
2811 flags
&= ~(O_NONBLOCK
);
2813 nRet
= fcntl(pSocket
->m_Socket
, F_SETFL
, flags
);
2817 pSocket
->m_nLastError
=errno
;
2824 /*****************************************************************************/
2825 /* osl_isNonBlockingMode */
2826 /*****************************************************************************/
2827 sal_Bool SAL_CALL
osl_isNonBlockingMode(oslSocket pSocket
)
2831 OSL_ASSERT(pSocket
);
2837 pSocket
->m_nLastError
=0;
2839 flags
= fcntl(pSocket
->m_Socket
, F_GETFL
, 0);
2841 if (flags
== -1 || !(flags
& O_NONBLOCK
))
2847 /*****************************************************************************/
2848 /* osl_getSocketType */
2849 /*****************************************************************************/
2850 oslSocketType SAL_CALL
osl_getSocketType(oslSocket pSocket
)
2853 sal_uInt32 TypeSize
= sizeof(Type
);
2855 OSL_ASSERT(pSocket
);
2858 return osl_Socket_TypeInvalid
;
2861 pSocket
->m_nLastError
=0;
2863 if(getsockopt(pSocket
->m_Socket
,
2864 OPTION_LEVEL_TO_NATIVE(osl_Socket_LevelSocket
),
2865 OPTION_TO_NATIVE(osl_Socket_OptionType
),
2867 (int*)PTR_SIZE_T(TypeSize
)) == -1)
2870 pSocket
->m_nLastError
=errno
;
2871 return osl_Socket_TypeInvalid
;
2874 return TYPE_FROM_NATIVE(Type
);
2878 /*****************************************************************************/
2879 /* osl_getLastSocketErrorDescription */
2880 /*****************************************************************************/
2881 void SAL_CALL
osl_getLastSocketErrorDescription(oslSocket Socket
, rtl_uString
**ustrError
)
2883 sal_Char pszError
[1024];
2887 osl_psz_getLastSocketErrorDescription(Socket
,pszError
,sizeof(pszError
));
2889 rtl_uString_newFromAscii(ustrError
,pszError
);
2895 void SAL_CALL
osl_psz_getLastSocketErrorDescription(oslSocket pSocket
, sal_Char
* pBuffer
, sal_uInt32 BufferSize
)
2897 /* make shure pBuffer will be a zero-terminated string even when strncpy has to cut */
2898 pBuffer
[BufferSize
-1]= '\0';
2902 strncpy(pBuffer
, strerror(EINVAL
), BufferSize
-1);
2906 strncpy(pBuffer
, strerror(pSocket
->m_nLastError
), BufferSize
-1);
2910 /*****************************************************************************/
2911 /* osl_getLastSocketError */
2912 /*****************************************************************************/
2913 oslSocketError SAL_CALL
osl_getLastSocketError(oslSocket pSocket
)
2917 return ERROR_FROM_NATIVE(EINVAL
);
2920 return ERROR_FROM_NATIVE(pSocket
->m_nLastError
);
2923 /*****************************************************************************/
2925 /*****************************************************************************/
2926 typedef struct _TSocketSetImpl
2928 int m_MaxHandle
; /* for select(), the largest descriptor in the set */
2929 fd_set m_Set
; /* the set of descriptors */
2933 /*****************************************************************************/
2934 /* osl_createSocketSet */
2935 /*****************************************************************************/
2936 oslSocketSet SAL_CALL
osl_createSocketSet()
2938 TSocketSetImpl
* pSet
;
2940 pSet
= (TSocketSetImpl
*)malloc(sizeof(TSocketSetImpl
));
2946 pSet
->m_MaxHandle
= 0;
2947 FD_ZERO(&pSet
->m_Set
);
2950 return (oslSocketSet
)pSet
;
2953 /*****************************************************************************/
2954 /* osl_destroySocketSet */
2955 /*****************************************************************************/
2956 void SAL_CALL
osl_destroySocketSet(oslSocketSet Set
)
2962 /*****************************************************************************/
2963 /* osl_clearSocketSet */
2964 /*****************************************************************************/
2965 void SAL_CALL
osl_clearSocketSet(oslSocketSet Set
)
2967 TSocketSetImpl
* pSet
;
2974 pSet
= (TSocketSetImpl
*)Set
;
2975 pSet
->m_MaxHandle
= 0;
2977 FD_ZERO(&pSet
->m_Set
);
2980 /*****************************************************************************/
2981 /* osl_addToSocketSet */
2982 /*****************************************************************************/
2983 void SAL_CALL
osl_addToSocketSet(oslSocketSet Set
, oslSocket pSocket
)
2985 TSocketSetImpl
* pSet
;
2988 OSL_ASSERT(pSocket
);
2990 if ( Set
== 0 || pSocket
== 0)
2995 pSet
= (TSocketSetImpl
*)Set
;
2997 /* correct max handle */
2998 if(pSocket
->m_Socket
> pSet
->m_MaxHandle
)
2999 pSet
->m_MaxHandle
= pSocket
->m_Socket
;
3000 FD_SET(pSocket
->m_Socket
, &pSet
->m_Set
);
3004 /*****************************************************************************/
3005 /* osl_removeFromSocketSet */
3006 /*****************************************************************************/
3007 void SAL_CALL
osl_removeFromSocketSet(oslSocketSet Set
, oslSocket pSocket
)
3009 TSocketSetImpl
* pSet
;
3012 OSL_ASSERT(pSocket
);
3014 if ( Set
== 0 || pSocket
== 0)
3019 pSet
= (TSocketSetImpl
*)Set
;
3021 /* correct max handle */
3022 if(pSocket
->m_Socket
== pSet
->m_MaxHandle
)
3024 /* not optimal, since the next used descriptor might be */
3025 /* much smaller than m_Socket-1, but it will do */
3026 pSet
->m_MaxHandle
--;
3027 if(pSet
->m_MaxHandle
< 0)
3029 pSet
->m_MaxHandle
= 0; /* avoid underflow */
3033 FD_CLR(pSocket
->m_Socket
, &pSet
->m_Set
);
3036 /*****************************************************************************/
3037 /* osl_isInSocketSet */
3038 /*****************************************************************************/
3039 sal_Bool SAL_CALL
osl_isInSocketSet(oslSocketSet Set
, oslSocket pSocket
)
3041 TSocketSetImpl
* pSet
;
3044 OSL_ASSERT(pSocket
);
3045 if ( Set
== 0 || pSocket
== 0 )
3050 pSet
= (TSocketSetImpl
*)Set
;
3052 return (FD_ISSET(pSocket
->m_Socket
, &pSet
->m_Set
) != 0);
3055 /*****************************************************************************/
3056 /* osl_demultiplexSocketEvents */
3057 /*****************************************************************************/
3058 sal_Int32 SAL_CALL
osl_demultiplexSocketEvents(oslSocketSet IncomingSet
,
3059 oslSocketSet OutgoingSet
,
3060 oslSocketSet OutOfBandSet
,
3061 const TimeValue
* pTimeout
)
3065 TSocketSetImpl
* pInSet
;
3066 TSocketSetImpl
* pOutSet
;
3067 TSocketSetImpl
* pOOBSet
;
3071 /* non-blocking call */
3072 tv
.tv_sec
= pTimeout
->Seconds
;
3073 tv
.tv_usec
= pTimeout
->Nanosec
/ 1000L;
3076 /* map opaque data to impl-types */
3077 pInSet
= (TSocketSetImpl
*)IncomingSet
;
3078 pOutSet
= (TSocketSetImpl
*)OutgoingSet
;
3079 pOOBSet
= (TSocketSetImpl
*)OutOfBandSet
;
3081 /* get max handle from all sets */
3083 MaxHandle
= pInSet
->m_MaxHandle
;
3085 if (pOutSet
&& (pOutSet
->m_MaxHandle
> MaxHandle
))
3086 MaxHandle
= pOutSet
->m_MaxHandle
;
3088 if (pOOBSet
&& (pOOBSet
->m_MaxHandle
> MaxHandle
))
3089 MaxHandle
= pOOBSet
->m_MaxHandle
;
3091 return select(MaxHandle
+1,
3092 pInSet
? PTR_FD_SET(pInSet
->m_Set
) : 0,
3093 pOutSet
? PTR_FD_SET(pOutSet
->m_Set
) : 0,
3094 pOOBSet
? PTR_FD_SET(pOOBSet
->m_Set
) : 0,
3095 pTimeout
? &tv
: 0);