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 #ifndef _OSL_SOCKET_HXX_ 28 #define _OSL_SOCKET_HXX_ 29 30 #include <osl/socket_decl.hxx> 31 32 namespace osl 33 { 34 //______________________________________________________________________________ 35 inline SocketAddr::SocketAddr() 36 : m_handle( osl_createEmptySocketAddr( osl_Socket_FamilyInet ) ) 37 {} 38 39 //______________________________________________________________________________ 40 inline SocketAddr::SocketAddr(const SocketAddr& Addr) 41 : m_handle( osl_copySocketAddr( Addr.m_handle ) ) 42 { 43 } 44 45 //______________________________________________________________________________ 46 inline SocketAddr::SocketAddr(oslSocketAddr Addr) 47 : m_handle( osl_copySocketAddr( Addr ) ) 48 { 49 } 50 51 //______________________________________________________________________________ 52 inline SocketAddr::SocketAddr(oslSocketAddr Addr, __osl_socket_NoCopy ) 53 : m_handle( Addr ) 54 { 55 } 56 57 //______________________________________________________________________________ 58 inline SocketAddr::SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort) 59 : m_handle( osl_createInetSocketAddr( strAddrOrHostName.pData, nPort ) ) 60 { 61 if(! m_handle ) 62 { 63 m_handle = osl_resolveHostname(strAddrOrHostName.pData); 64 65 // host found? 66 if(m_handle) 67 { 68 osl_setInetPortOfSocketAddr(m_handle, nPort); 69 } 70 else 71 { 72 osl_destroySocketAddr( m_handle ); 73 m_handle = 0; 74 } 75 } 76 } 77 78 //______________________________________________________________________________ 79 inline SocketAddr::~SocketAddr() 80 { 81 if( m_handle ) 82 osl_destroySocketAddr( m_handle ); 83 } 84 85 //______________________________________________________________________________ 86 inline ::rtl::OUString SocketAddr::getHostname( oslSocketResult *pResult ) const 87 { 88 ::rtl::OUString hostname; 89 oslSocketResult result = osl_getHostnameOfSocketAddr( m_handle, &(hostname.pData) ); 90 if( pResult ) 91 *pResult = result; 92 return hostname; 93 } 94 95 //______________________________________________________________________________ 96 inline sal_Int32 SAL_CALL SocketAddr::getPort() const 97 { 98 return osl_getInetPortOfSocketAddr(m_handle); 99 } 100 101 //______________________________________________________________________________ 102 inline sal_Bool SAL_CALL SocketAddr::setPort( sal_Int32 nPort ) 103 { 104 return osl_setInetPortOfSocketAddr(m_handle, nPort ); 105 } 106 107 inline sal_Bool SAL_CALL SocketAddr::setHostname( const ::rtl::OUString &sDottedIpOrHostname ) 108 { 109 *this = SocketAddr( sDottedIpOrHostname , getPort() ); 110 return is(); 111 } 112 113 //______________________________________________________________________________ 114 inline sal_Bool SAL_CALL SocketAddr::setAddr( const ::rtl::ByteSequence & address ) 115 { 116 return osl_setAddrOfSocketAddr( m_handle, address.getHandle() ) 117 == osl_Socket_Ok; 118 } 119 120 inline ::rtl::ByteSequence SAL_CALL SocketAddr::getAddr( oslSocketResult *pResult ) const 121 { 122 ::rtl::ByteSequence sequence; 123 oslSocketResult result = osl_getAddrOfSocketAddr( m_handle,(sal_Sequence **) &sequence ); 124 if( pResult ) 125 *pResult = result; 126 return sequence; 127 } 128 129 //______________________________________________________________________________ 130 inline SocketAddr & SAL_CALL SocketAddr::operator= (oslSocketAddr Addr) 131 { 132 oslSocketAddr pNewAddr = osl_copySocketAddr( Addr ); 133 if( m_handle ) 134 osl_destroySocketAddr( m_handle ); 135 m_handle = pNewAddr; 136 return *this; 137 } 138 139 //______________________________________________________________________________ 140 inline SocketAddr & SAL_CALL SocketAddr::operator= (const SocketAddr& Addr) 141 { 142 *this = (Addr.getHandle()); 143 return *this; 144 } 145 146 inline SocketAddr & SAL_CALL SocketAddr::assign( oslSocketAddr Addr, __osl_socket_NoCopy ) 147 { 148 if( m_handle ) 149 osl_destroySocketAddr( m_handle ); 150 m_handle = Addr; 151 return *this; 152 } 153 154 //______________________________________________________________________________ 155 inline sal_Bool SAL_CALL SocketAddr::operator== (oslSocketAddr Addr) const 156 { 157 return osl_isEqualSocketAddr( m_handle, Addr ); 158 } 159 160 inline oslSocketAddr SocketAddr::getHandle() const 161 { 162 return m_handle; 163 } 164 165 //______________________________________________________________________________ 166 inline sal_Bool SocketAddr::is() const 167 { 168 return m_handle != 0; 169 } 170 171 // (static method)______________________________________________________________ 172 inline ::rtl::OUString SAL_CALL SocketAddr::getLocalHostname( oslSocketResult *pResult ) 173 { 174 ::rtl::OUString hostname; 175 oslSocketResult result = osl_getLocalHostname( &(hostname.pData) ); 176 if(pResult ) 177 *pResult = result; 178 return hostname; 179 } 180 181 // (static method)______________________________________________________________ 182 inline void SAL_CALL SocketAddr::resolveHostname( 183 const ::rtl::OUString & strHostName, SocketAddr &Addr) 184 { 185 Addr = SocketAddr( osl_resolveHostname( strHostName.pData ) , SAL_NO_COPY ); 186 } 187 188 // (static method)______________________________________________________________ 189 inline sal_Int32 SAL_CALL SocketAddr::getServicePort( 190 const ::rtl::OUString& strServiceName, 191 const ::rtl::OUString & strProtocolName ) 192 { 193 return osl_getServicePort( strServiceName.pData, strProtocolName.pData ); 194 } 195 196 //______________________________________________________________________________ 197 inline Socket::Socket(oslSocketType Type, 198 oslAddrFamily Family, 199 oslProtocol Protocol) 200 : m_handle( osl_createSocket(Family, Type, Protocol) ) 201 {} 202 203 //______________________________________________________________________________ 204 inline Socket::Socket( oslSocket socketHandle, __sal_NoAcquire ) 205 : m_handle( socketHandle ) 206 {} 207 208 //______________________________________________________________________________ 209 inline Socket::Socket( oslSocket socketHandle ) 210 : m_handle( socketHandle ) 211 { 212 osl_acquireSocket( m_handle ); 213 } 214 215 //______________________________________________________________________________ 216 inline Socket::Socket( const Socket & socket ) 217 : m_handle( socket.getHandle() ) 218 { 219 osl_acquireSocket( m_handle ); 220 } 221 222 //______________________________________________________________________________ 223 inline Socket::~Socket() 224 { 225 osl_releaseSocket( m_handle ); 226 } 227 228 //______________________________________________________________________________ 229 inline Socket& Socket::operator= ( oslSocket socketHandle) 230 { 231 osl_acquireSocket( socketHandle ); 232 osl_releaseSocket( m_handle ); 233 m_handle = socketHandle; 234 return *this; 235 } 236 237 //______________________________________________________________________________ 238 inline Socket& Socket::operator= (const Socket& sock) 239 { 240 return (*this) = sock.getHandle(); 241 } 242 243 //______________________________________________________________________________ 244 inline sal_Bool Socket::operator==( const Socket& rSocket ) const 245 { 246 return m_handle == rSocket.getHandle(); 247 } 248 249 //______________________________________________________________________________ 250 inline sal_Bool Socket::operator==( const oslSocket socketHandle ) const 251 { 252 return m_handle == socketHandle; 253 } 254 255 //______________________________________________________________________________ 256 inline void Socket::shutdown( oslSocketDirection Direction ) 257 { 258 osl_shutdownSocket( m_handle , Direction ); 259 } 260 261 //______________________________________________________________________________ 262 inline void Socket::close() 263 { 264 osl_closeSocket( m_handle ); 265 } 266 267 //______________________________________________________________________________ 268 inline void Socket::getLocalAddr( SocketAddr & addr) const 269 { 270 addr.assign( osl_getLocalAddrOfSocket( m_handle ) , SAL_NO_COPY ); 271 } 272 273 //______________________________________________________________________________ 274 inline sal_Int32 Socket::getLocalPort() const 275 { 276 SocketAddr addr( 0 ); 277 getLocalAddr( addr ); 278 return addr.getPort(); 279 } 280 281 //______________________________________________________________________________ 282 inline ::rtl::OUString Socket::getLocalHost() const 283 { 284 SocketAddr addr( 0 ); 285 getLocalAddr( addr ); 286 return addr.getHostname(); 287 } 288 289 //______________________________________________________________________________ 290 inline void Socket::getPeerAddr( SocketAddr &addr ) const 291 { 292 addr.assign( osl_getPeerAddrOfSocket( m_handle ), SAL_NO_COPY ); 293 } 294 295 //______________________________________________________________________________ 296 inline sal_Int32 Socket::getPeerPort() const 297 { 298 SocketAddr addr( 0 ); 299 getPeerAddr( addr ); 300 return addr.getPort(); 301 } 302 303 //______________________________________________________________________________ 304 inline ::rtl::OUString Socket::getPeerHost() const 305 { 306 SocketAddr addr( 0 ); 307 getPeerAddr( addr ); 308 return addr.getHostname(); 309 } 310 311 //______________________________________________________________________________ 312 inline sal_Bool Socket::bind(const SocketAddr& LocalInterface) 313 { 314 return osl_bindAddrToSocket( m_handle , LocalInterface.getHandle() ); 315 } 316 317 //______________________________________________________________________________ 318 inline sal_Bool Socket::isRecvReady(const TimeValue *pTimeout ) const 319 { 320 return osl_isReceiveReady( m_handle , pTimeout ); 321 } 322 323 //______________________________________________________________________________ 324 inline sal_Bool Socket::isSendReady(const TimeValue *pTimeout ) const 325 { 326 return osl_isSendReady( m_handle, pTimeout ); 327 } 328 329 //______________________________________________________________________________ 330 inline sal_Bool Socket::isExceptionPending(const TimeValue *pTimeout ) const 331 { 332 return osl_isExceptionPending( m_handle, pTimeout ); 333 } 334 335 //______________________________________________________________________________ 336 inline oslSocketType Socket::getType() const 337 { 338 return osl_getSocketType( m_handle ); 339 } 340 341 //______________________________________________________________________________ 342 inline sal_Int32 Socket::getOption( 343 oslSocketOption Option, 344 void* pBuffer, 345 sal_uInt32 BufferLen, 346 oslSocketOptionLevel Level) const 347 { 348 return osl_getSocketOption( m_handle, Level, Option, pBuffer , BufferLen ); 349 } 350 351 //______________________________________________________________________________ 352 inline sal_Bool Socket::setOption( oslSocketOption Option, 353 void* pBuffer, 354 sal_uInt32 BufferLen, 355 oslSocketOptionLevel Level ) const 356 { 357 return osl_setSocketOption( m_handle, Level, Option , pBuffer, BufferLen ); 358 } 359 360 //______________________________________________________________________________ 361 inline sal_Bool Socket::setOption( oslSocketOption option, sal_Int32 nValue ) 362 { 363 return setOption( option, &nValue, sizeof( nValue ) ); 364 } 365 366 //______________________________________________________________________________ 367 inline sal_Int32 Socket::getOption( oslSocketOption option ) const 368 { 369 sal_Int32 n; 370 getOption( option, &n, sizeof( n ) ); 371 return n; 372 } 373 374 //______________________________________________________________________________ 375 inline sal_Bool Socket::enableNonBlockingMode( sal_Bool bNonBlockingMode) 376 { 377 return osl_enableNonBlockingMode( m_handle , bNonBlockingMode ); 378 } 379 380 //______________________________________________________________________________ 381 inline sal_Bool Socket::isNonBlockingMode() const 382 { 383 return osl_isNonBlockingMode( m_handle ); 384 } 385 386 //______________________________________________________________________________ 387 inline void SAL_CALL Socket::clearError() const 388 { 389 sal_Int32 err = 0; 390 getOption(osl_Socket_OptionError, &err, sizeof(err)); 391 } 392 393 //______________________________________________________________________________ 394 inline oslSocketError Socket::getError() const 395 { 396 return osl_getLastSocketError( m_handle ); 397 } 398 399 //______________________________________________________________________________ 400 inline ::rtl::OUString Socket::getErrorAsString( ) const 401 { 402 ::rtl::OUString error; 403 osl_getLastSocketErrorDescription( m_handle, &(error.pData) ); 404 return error; 405 } 406 407 //______________________________________________________________________________ 408 inline oslSocket Socket::getHandle() const 409 { 410 return m_handle; 411 } 412 413 //______________________________________________________________________________ 414 inline StreamSocket::StreamSocket(oslAddrFamily Family, 415 oslProtocol Protocol, 416 oslSocketType Type ) 417 : Socket( Type, Family, Protocol ) 418 {} 419 420 //______________________________________________________________________________ 421 inline StreamSocket::StreamSocket( oslSocket socketHandle, __sal_NoAcquire noacquire ) 422 : Socket( socketHandle, noacquire ) 423 {} 424 425 //______________________________________________________________________________ 426 inline StreamSocket::StreamSocket( oslSocket socketHandle ) 427 : Socket( socketHandle ) 428 {} 429 430 //______________________________________________________________________________ 431 inline StreamSocket::StreamSocket( const StreamSocket & socket ) 432 : Socket( socket ) 433 {} 434 435 //______________________________________________________________________________ 436 inline sal_Int32 StreamSocket::read(void* pBuffer, sal_uInt32 n) 437 { 438 return osl_readSocket( m_handle, pBuffer, n ); 439 } 440 441 //______________________________________________________________________________ 442 inline sal_Int32 StreamSocket::write(const void* pBuffer, sal_uInt32 n) 443 { 444 return osl_writeSocket( m_handle, pBuffer, n ); 445 } 446 447 448 //______________________________________________________________________________ 449 inline sal_Int32 StreamSocket::recv(void* pBuffer, 450 sal_uInt32 BytesToRead, 451 oslSocketMsgFlag Flag) 452 { 453 return osl_receiveSocket( m_handle, pBuffer,BytesToRead, Flag ); 454 } 455 456 //______________________________________________________________________________ 457 inline sal_Int32 StreamSocket::send(const void* pBuffer, 458 sal_uInt32 BytesToSend, 459 oslSocketMsgFlag Flag) 460 { 461 return osl_sendSocket( m_handle, pBuffer, BytesToSend, Flag ); 462 } 463 464 //______________________________________________________________________________ 465 inline ConnectorSocket::ConnectorSocket(oslAddrFamily Family, 466 oslProtocol Protocol, 467 oslSocketType Type) 468 : StreamSocket( Family, Protocol ,Type ) 469 {} 470 471 //______________________________________________________________________________ 472 inline oslSocketResult ConnectorSocket::connect( const SocketAddr& TargetHost, 473 const TimeValue* pTimeout ) 474 { 475 return osl_connectSocketTo( m_handle , TargetHost.getHandle(), pTimeout ); 476 } 477 478 //______________________________________________________________________________ 479 inline AcceptorSocket::AcceptorSocket(oslAddrFamily Family , 480 oslProtocol Protocol , 481 oslSocketType Type ) 482 : Socket( Type, Family, Protocol ) 483 {} 484 485 //______________________________________________________________________________ 486 inline sal_Bool AcceptorSocket::listen(sal_Int32 MaxPendingConnections) 487 { 488 return osl_listenOnSocket( m_handle, MaxPendingConnections ); 489 } 490 491 //______________________________________________________________________________ 492 inline oslSocketResult AcceptorSocket::acceptConnection( StreamSocket& Connection) 493 { 494 oslSocket o = osl_acceptConnectionOnSocket( m_handle, 0 ); 495 oslSocketResult status = osl_Socket_Ok; 496 if( o ) 497 { 498 Connection = StreamSocket( o , SAL_NO_ACQUIRE ); 499 } 500 else 501 { 502 Connection = StreamSocket(); 503 status = osl_Socket_Error; 504 } 505 return status; 506 } 507 508 //______________________________________________________________________________ 509 inline oslSocketResult AcceptorSocket::acceptConnection( 510 StreamSocket& Connection, SocketAddr & PeerAddr) 511 { 512 // TODO change in/OUT parameter 513 oslSocket o = osl_acceptConnectionOnSocket( m_handle, (oslSocketAddr *)&PeerAddr ); 514 oslSocketResult status = osl_Socket_Ok; 515 if( o ) 516 { 517 Connection = StreamSocket( o , SAL_NO_ACQUIRE ); 518 } 519 else 520 { 521 Connection = StreamSocket(); 522 status = osl_Socket_Error; 523 } 524 return status; 525 } 526 527 //______________________________________________________________________________ 528 inline DatagramSocket::DatagramSocket(oslAddrFamily Family, 529 oslProtocol Protocol, 530 oslSocketType Type) 531 : Socket( Type, Family, Protocol ) 532 {} 533 534 //______________________________________________________________________________ 535 inline sal_Int32 DatagramSocket::recvFrom(void* pBuffer, 536 sal_uInt32 BufferSize, 537 SocketAddr* pSenderAddr, 538 oslSocketMsgFlag Flag ) 539 { 540 sal_Int32 nByteRead; 541 if( pSenderAddr ) 542 { 543 // TODO : correct the out-parameter pSenderAddr outparameter 544 nByteRead = osl_receiveFromSocket( m_handle, pSenderAddr->getHandle() , pBuffer, 545 BufferSize, Flag); 546 // nByteRead = osl_receiveFromSocket( m_handle, *(oslSocketAddr**) &pSenderAddr , pBuffer, 547 // BufferSize, Flag); 548 } 549 else 550 { 551 nByteRead = osl_receiveFromSocket( m_handle, 0 , pBuffer , BufferSize , Flag ); 552 } 553 return nByteRead; 554 } 555 556 //______________________________________________________________________________ 557 inline sal_Int32 DatagramSocket::sendTo( const SocketAddr& ReceiverAddr, 558 const void* pBuffer, 559 sal_uInt32 BufferSize, 560 oslSocketMsgFlag Flag ) 561 { 562 return osl_sendToSocket( m_handle, ReceiverAddr.getHandle(), pBuffer, BufferSize, Flag ); 563 } 564 } 565 #endif 566