1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /* 3 * This file is part of the LibreOffice project. 4 * 5 * This Source Code Form is subject to the terms of the Mozilla Public 6 * License, v. 2.0. If a copy of the MPL was not distributed with this 7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 8 * 9 * This file incorporates work covered by the following license notice: 10 * 11 * Licensed to the Apache Software Foundation (ASF) under one or more 12 * contributor license agreements. See the NOTICE file distributed 13 * with this work for additional information regarding copyright 14 * ownership. The ASF licenses this file to you under the Apache 15 * License, Version 2.0 (the "License"); you may not use this file 16 * except in compliance with the License. You may obtain a copy of 17 * the License at http://www.apache.org/licenses/LICENSE-2.0 . 18 */ 19 #ifndef INCLUDED_OSL_SOCKET_HXX 20 #define INCLUDED_OSL_SOCKET_HXX 21 22 #include "osl/socket_decl.hxx" 23 24 namespace osl 25 { 26 SocketAddr()27 inline SocketAddr::SocketAddr() 28 : m_handle( osl_createEmptySocketAddr( osl_Socket_FamilyInet ) ) 29 {} 30 31 SocketAddr(const SocketAddr & Addr)32 inline SocketAddr::SocketAddr(const SocketAddr& Addr) 33 : m_handle( osl_copySocketAddr( Addr.m_handle ) ) 34 { 35 } 36 37 #if defined LIBO_INTERNAL_ONLY SocketAddr(SocketAddr && other)38 SocketAddr::SocketAddr(SocketAddr && other) noexcept : m_handle(other.m_handle) { 39 other.m_handle = nullptr; 40 } 41 #endif 42 SocketAddr(oslSocketAddr Addr)43 inline SocketAddr::SocketAddr(oslSocketAddr Addr) 44 : m_handle( osl_copySocketAddr( Addr ) ) 45 { 46 } 47 48 SocketAddr(oslSocketAddr Addr,__osl_socket_NoCopy)49 inline SocketAddr::SocketAddr(oslSocketAddr Addr, __osl_socket_NoCopy ) 50 : m_handle( Addr ) 51 { 52 } 53 54 SocketAddr(const::rtl::OUString & strAddrOrHostName,sal_Int32 nPort)55 inline SocketAddr::SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort) 56 : m_handle( osl_createInetSocketAddr( strAddrOrHostName.pData, nPort ) ) 57 { 58 if(! m_handle ) 59 { 60 m_handle = osl_resolveHostname(strAddrOrHostName.pData); 61 62 // host found? 63 if(m_handle) 64 { 65 osl_setInetPortOfSocketAddr(m_handle, nPort); 66 } 67 else 68 { 69 osl_destroySocketAddr( m_handle ); 70 m_handle = NULL; 71 } 72 } 73 } 74 75 ~SocketAddr()76 inline SocketAddr::~SocketAddr() 77 { 78 if( m_handle ) 79 osl_destroySocketAddr( m_handle ); 80 } 81 82 getHostname(oslSocketResult * pResult) const83 inline ::rtl::OUString SocketAddr::getHostname( oslSocketResult *pResult ) const 84 { 85 ::rtl::OUString hostname; 86 oslSocketResult result = osl_getHostnameOfSocketAddr( m_handle, &(hostname.pData) ); 87 if( pResult ) 88 *pResult = result; 89 return hostname; 90 } 91 92 getPort() const93 inline sal_Int32 SAL_CALL SocketAddr::getPort() const 94 { 95 return osl_getInetPortOfSocketAddr(m_handle); 96 } 97 98 setPort(sal_Int32 nPort)99 inline bool SAL_CALL SocketAddr::setPort( sal_Int32 nPort ) 100 { 101 return osl_setInetPortOfSocketAddr(m_handle, nPort ); 102 } 103 setHostname(const::rtl::OUString & sDottedIpOrHostname)104 inline bool SAL_CALL SocketAddr::setHostname( const ::rtl::OUString &sDottedIpOrHostname ) 105 { 106 *this = SocketAddr( sDottedIpOrHostname , getPort() ); 107 return is(); 108 } 109 110 setAddr(const::rtl::ByteSequence & address)111 inline bool SAL_CALL SocketAddr::setAddr( const ::rtl::ByteSequence & address ) 112 { 113 return osl_setAddrOfSocketAddr( m_handle, address.getHandle() ) 114 == osl_Socket_Ok; 115 } 116 getAddr(oslSocketResult * pResult) const117 inline ::rtl::ByteSequence SAL_CALL SocketAddr::getAddr( oslSocketResult *pResult ) const 118 { 119 ::rtl::ByteSequence sequence; 120 oslSocketResult result = osl_getAddrOfSocketAddr( m_handle, reinterpret_cast<sal_Sequence **>(&sequence) ); 121 if( pResult ) 122 *pResult = result; 123 return sequence; 124 } 125 126 operator =(oslSocketAddr Addr)127 inline SocketAddr & SAL_CALL SocketAddr::operator= (oslSocketAddr Addr) 128 { 129 oslSocketAddr pNewAddr = osl_copySocketAddr( Addr ); 130 if( m_handle ) 131 osl_destroySocketAddr( m_handle ); 132 m_handle = pNewAddr; 133 return *this; 134 } 135 136 operator =(const SocketAddr & Addr)137 inline SocketAddr & SAL_CALL SocketAddr::operator= (const SocketAddr& Addr) 138 { 139 *this = Addr.getHandle(); 140 return *this; 141 } 142 143 #if defined LIBO_INTERNAL_ONLY operator =(SocketAddr && other)144 SocketAddr & SocketAddr::operator =(SocketAddr && other) noexcept { 145 if (m_handle != nullptr) { 146 osl_destroySocketAddr(m_handle); 147 } 148 m_handle = other.m_handle; 149 other.m_handle = nullptr; 150 return *this; 151 } 152 #endif 153 assign(oslSocketAddr Addr,__osl_socket_NoCopy)154 inline SocketAddr & SAL_CALL SocketAddr::assign( oslSocketAddr Addr, __osl_socket_NoCopy ) 155 { 156 if( m_handle ) 157 osl_destroySocketAddr( m_handle ); 158 m_handle = Addr; 159 return *this; 160 } 161 162 operator ==(oslSocketAddr Addr) const163 inline bool SAL_CALL SocketAddr::operator== (oslSocketAddr Addr) const 164 { 165 return osl_isEqualSocketAddr( m_handle, Addr ); 166 } 167 getHandle() const168 inline oslSocketAddr SocketAddr::getHandle() const 169 { 170 return m_handle; 171 } 172 173 is() const174 inline bool SocketAddr::is() const 175 { 176 return m_handle != NULL; 177 } 178 getLocalHostname(oslSocketResult * pResult)179 inline ::rtl::OUString SAL_CALL SocketAddr::getLocalHostname( oslSocketResult *pResult ) 180 { 181 ::rtl::OUString hostname; 182 oslSocketResult result = osl_getLocalHostname( &(hostname.pData) ); 183 if(pResult ) 184 *pResult = result; 185 return hostname; 186 } 187 resolveHostname(const::rtl::OUString & strHostName,SocketAddr & Addr)188 inline void SAL_CALL SocketAddr::resolveHostname( 189 const ::rtl::OUString & strHostName, SocketAddr &Addr) 190 { 191 Addr = SocketAddr( osl_resolveHostname( strHostName.pData ) , SAL_NO_COPY ); 192 } 193 getServicePort(const::rtl::OUString & strServiceName,const::rtl::OUString & strProtocolName)194 inline sal_Int32 SAL_CALL SocketAddr::getServicePort( 195 const ::rtl::OUString& strServiceName, 196 const ::rtl::OUString & strProtocolName ) 197 { 198 return osl_getServicePort( strServiceName.pData, strProtocolName.pData ); 199 } 200 201 Socket(oslSocketType Type,oslAddrFamily Family,oslProtocol Protocol)202 inline Socket::Socket(oslSocketType Type, 203 oslAddrFamily Family, 204 oslProtocol Protocol) 205 : m_handle( osl_createSocket(Family, Type, Protocol) ) 206 {} 207 208 Socket(oslSocket socketHandle,__sal_NoAcquire)209 inline Socket::Socket( oslSocket socketHandle, __sal_NoAcquire ) 210 : m_handle( socketHandle ) 211 {} 212 213 Socket(oslSocket socketHandle)214 inline Socket::Socket( oslSocket socketHandle ) 215 : m_handle( socketHandle ) 216 { 217 osl_acquireSocket( m_handle ); 218 } 219 220 Socket(const Socket & socket)221 inline Socket::Socket( const Socket & socket ) 222 : m_handle( socket.getHandle() ) 223 { 224 osl_acquireSocket( m_handle ); 225 } 226 227 ~Socket()228 inline Socket::~Socket() 229 { 230 osl_releaseSocket( m_handle ); 231 } 232 233 operator =(oslSocket socketHandle)234 inline Socket& Socket::operator= ( oslSocket socketHandle) 235 { 236 osl_acquireSocket( socketHandle ); 237 osl_releaseSocket( m_handle ); 238 m_handle = socketHandle; 239 return *this; 240 } 241 242 operator =(const Socket & sock)243 inline Socket& Socket::operator= (const Socket& sock) 244 { 245 return (*this) = sock.getHandle(); 246 } 247 248 operator ==(const Socket & rSocket) const249 inline bool Socket::operator==( const Socket& rSocket ) const 250 { 251 return m_handle == rSocket.getHandle(); 252 } 253 254 operator ==(const oslSocket socketHandle) const255 inline bool Socket::operator==( const oslSocket socketHandle ) const 256 { 257 return m_handle == socketHandle; 258 } 259 260 shutdown(oslSocketDirection Direction)261 inline void Socket::shutdown( oslSocketDirection Direction ) 262 { 263 osl_shutdownSocket( m_handle , Direction ); 264 } 265 266 close()267 inline void Socket::close() 268 { 269 osl_closeSocket( m_handle ); 270 } 271 272 getLocalAddr(SocketAddr & addr) const273 inline void Socket::getLocalAddr( SocketAddr & addr) const 274 { 275 addr.assign( osl_getLocalAddrOfSocket( m_handle ) , SAL_NO_COPY ); 276 } 277 278 getLocalPort() const279 inline sal_Int32 Socket::getLocalPort() const 280 { 281 SocketAddr addr( NULL ); 282 getLocalAddr( addr ); 283 return addr.getPort(); 284 } 285 286 getLocalHost() const287 inline ::rtl::OUString Socket::getLocalHost() const 288 { 289 SocketAddr addr( NULL ); 290 getLocalAddr( addr ); 291 return addr.getHostname(); 292 } 293 294 getPeerAddr(SocketAddr & addr) const295 inline void Socket::getPeerAddr( SocketAddr &addr ) const 296 { 297 addr.assign( osl_getPeerAddrOfSocket( m_handle ), SAL_NO_COPY ); 298 } 299 300 getPeerPort() const301 inline sal_Int32 Socket::getPeerPort() const 302 { 303 SocketAddr addr( NULL ); 304 getPeerAddr( addr ); 305 return addr.getPort(); 306 } 307 308 getPeerHost() const309 inline ::rtl::OUString Socket::getPeerHost() const 310 { 311 SocketAddr addr( NULL ); 312 getPeerAddr( addr ); 313 return addr.getHostname(); 314 } 315 316 bind(const SocketAddr & LocalInterface)317 inline bool Socket::bind(const SocketAddr& LocalInterface) 318 { 319 return osl_bindAddrToSocket( m_handle , LocalInterface.getHandle() ); 320 } 321 322 isRecvReady(const TimeValue * pTimeout) const323 inline bool Socket::isRecvReady(const TimeValue *pTimeout ) const 324 { 325 return osl_isReceiveReady( m_handle , pTimeout ); 326 } 327 328 isSendReady(const TimeValue * pTimeout) const329 inline bool Socket::isSendReady(const TimeValue *pTimeout ) const 330 { 331 return osl_isSendReady( m_handle, pTimeout ); 332 } 333 334 isExceptionPending(const TimeValue * pTimeout) const335 inline bool Socket::isExceptionPending(const TimeValue *pTimeout ) const 336 { 337 return osl_isExceptionPending( m_handle, pTimeout ); 338 } 339 340 getType() const341 inline oslSocketType Socket::getType() const 342 { 343 return osl_getSocketType( m_handle ); 344 } 345 346 getOption(oslSocketOption Option,void * pBuffer,sal_uInt32 BufferLen,oslSocketOptionLevel Level) const347 inline sal_Int32 Socket::getOption( 348 oslSocketOption Option, 349 void* pBuffer, 350 sal_uInt32 BufferLen, 351 oslSocketOptionLevel Level) const 352 { 353 return osl_getSocketOption( m_handle, Level, Option, pBuffer , BufferLen ); 354 } 355 356 setOption(oslSocketOption Option,void * pBuffer,sal_uInt32 BufferLen,oslSocketOptionLevel Level) const357 inline bool Socket::setOption( oslSocketOption Option, 358 void* pBuffer, 359 sal_uInt32 BufferLen, 360 oslSocketOptionLevel Level ) const 361 { 362 return osl_setSocketOption( m_handle, Level, Option , pBuffer, BufferLen ); 363 } 364 365 setOption(oslSocketOption option,sal_Int32 nValue)366 inline bool Socket::setOption( oslSocketOption option, sal_Int32 nValue ) 367 { 368 return setOption( option, &nValue, sizeof( nValue ) ); 369 } 370 371 getOption(oslSocketOption option) const372 inline sal_Int32 Socket::getOption( oslSocketOption option ) const 373 { 374 sal_Int32 n; 375 getOption( option, &n, sizeof( n ) ); 376 return n; 377 } 378 379 enableNonBlockingMode(bool bNonBlockingMode)380 inline bool Socket::enableNonBlockingMode( bool bNonBlockingMode) 381 { 382 return osl_enableNonBlockingMode( m_handle , bNonBlockingMode ); 383 } 384 385 isNonBlockingMode() const386 inline bool Socket::isNonBlockingMode() const 387 { 388 return osl_isNonBlockingMode( m_handle ); 389 } 390 391 clearError() const392 inline void SAL_CALL Socket::clearError() const 393 { 394 sal_Int32 err = 0; 395 getOption(osl_Socket_OptionError, &err, sizeof(err)); 396 } 397 398 getError() const399 inline oslSocketError Socket::getError() const 400 { 401 return osl_getLastSocketError( m_handle ); 402 } 403 404 getErrorAsString() const405 inline ::rtl::OUString Socket::getErrorAsString( ) const 406 { 407 ::rtl::OUString error; 408 osl_getLastSocketErrorDescription( m_handle, &(error.pData) ); 409 return error; 410 } 411 412 getHandle() const413 inline oslSocket Socket::getHandle() const 414 { 415 return m_handle; 416 } 417 418 StreamSocket(oslAddrFamily Family,oslProtocol Protocol,oslSocketType Type)419 inline StreamSocket::StreamSocket(oslAddrFamily Family, 420 oslProtocol Protocol, 421 oslSocketType Type ) 422 : Socket( Type, Family, Protocol ) 423 {} 424 425 StreamSocket(oslSocket socketHandle,__sal_NoAcquire noacquire)426 inline StreamSocket::StreamSocket( oslSocket socketHandle, __sal_NoAcquire noacquire ) 427 : Socket( socketHandle, noacquire ) 428 {} 429 430 StreamSocket(oslSocket socketHandle)431 inline StreamSocket::StreamSocket( oslSocket socketHandle ) 432 : Socket( socketHandle ) 433 {} 434 435 read(void * pBuffer,sal_uInt32 n)436 inline sal_Int32 StreamSocket::read(void* pBuffer, sal_uInt32 n) 437 { 438 return osl_readSocket( m_handle, pBuffer, n ); 439 } 440 441 write(const void * pBuffer,sal_uInt32 n)442 inline sal_Int32 StreamSocket::write(const void* pBuffer, sal_uInt32 n) 443 { 444 return osl_writeSocket( m_handle, pBuffer, n ); 445 } 446 447 recv(void * pBuffer,sal_uInt32 BytesToRead,oslSocketMsgFlag Flag)448 inline sal_Int32 StreamSocket::recv(void* pBuffer, 449 sal_uInt32 BytesToRead, 450 oslSocketMsgFlag Flag) 451 { 452 return osl_receiveSocket( m_handle, pBuffer,BytesToRead, Flag ); 453 } 454 455 send(const void * pBuffer,sal_uInt32 BytesToSend,oslSocketMsgFlag Flag)456 inline sal_Int32 StreamSocket::send(const void* pBuffer, 457 sal_uInt32 BytesToSend, 458 oslSocketMsgFlag Flag) 459 { 460 return osl_sendSocket( m_handle, pBuffer, BytesToSend, Flag ); 461 } 462 463 ConnectorSocket(oslAddrFamily Family,oslProtocol Protocol,oslSocketType Type)464 inline ConnectorSocket::ConnectorSocket(oslAddrFamily Family, 465 oslProtocol Protocol, 466 oslSocketType Type) 467 : StreamSocket( Family, Protocol ,Type ) 468 {} 469 470 connect(const SocketAddr & TargetHost,const TimeValue * pTimeout)471 inline oslSocketResult ConnectorSocket::connect( const SocketAddr& TargetHost, 472 const TimeValue* pTimeout ) 473 { 474 return osl_connectSocketTo( m_handle , TargetHost.getHandle(), pTimeout ); 475 } 476 477 AcceptorSocket(oslAddrFamily Family,oslProtocol Protocol,oslSocketType Type)478 inline AcceptorSocket::AcceptorSocket(oslAddrFamily Family , 479 oslProtocol Protocol , 480 oslSocketType Type ) 481 : Socket( Type, Family, Protocol ) 482 {} 483 484 listen(sal_Int32 MaxPendingConnections)485 inline bool AcceptorSocket::listen(sal_Int32 MaxPendingConnections) 486 { 487 return osl_listenOnSocket( m_handle, MaxPendingConnections ); 488 } 489 490 acceptConnection(StreamSocket & Connection)491 inline oslSocketResult AcceptorSocket::acceptConnection( StreamSocket& Connection) 492 { 493 oslSocket o = osl_acceptConnectionOnSocket( m_handle, NULL ); 494 oslSocketResult status = osl_Socket_Ok; 495 if( o ) 496 { 497 Connection = StreamSocket( o , SAL_NO_ACQUIRE ); 498 } 499 else 500 { 501 Connection = StreamSocket(); 502 status = osl_Socket_Error; 503 } 504 return status; 505 } 506 507 acceptConnection(StreamSocket & Connection,SocketAddr & PeerAddr)508 inline oslSocketResult AcceptorSocket::acceptConnection( 509 StreamSocket& Connection, SocketAddr & PeerAddr) 510 { 511 // TODO change in/OUT parameter 512 oslSocket o = osl_acceptConnectionOnSocket( 513 m_handle, reinterpret_cast<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 DatagramSocket(oslAddrFamily Family,oslProtocol Protocol,oslSocketType Type)528 inline DatagramSocket::DatagramSocket(oslAddrFamily Family, 529 oslProtocol Protocol, 530 oslSocketType Type) 531 : Socket( Type, Family, Protocol ) 532 {} 533 534 recvFrom(void * pBuffer,sal_uInt32 BufferSize,SocketAddr * pSenderAddr,oslSocketMsgFlag Flag)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 } 547 else 548 { 549 nByteRead = osl_receiveFromSocket( m_handle, NULL , pBuffer , BufferSize , Flag ); 550 } 551 return nByteRead; 552 } 553 554 sendTo(const SocketAddr & ReceiverAddr,const void * pBuffer,sal_uInt32 BufferSize,oslSocketMsgFlag Flag)555 inline sal_Int32 DatagramSocket::sendTo( const SocketAddr& ReceiverAddr, 556 const void* pBuffer, 557 sal_uInt32 BufferSize, 558 oslSocketMsgFlag Flag ) 559 { 560 return osl_sendToSocket( m_handle, ReceiverAddr.getHandle(), pBuffer, BufferSize, Flag ); 561 } 562 } 563 #endif 564 565 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 566