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