LCOV - code coverage report
Current view: top level - libreoffice/sal/osl/unx - socket.c (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 14 764 1.8 %
Date: 2012-12-27 Functions: 2 81 2.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10