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