1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #ifndef _VOS_SOCKET_HXX_ 25 #define _VOS_SOCKET_HXX_ 26 27 # include <vos/types.hxx> 28 # include <vos/object.hxx> 29 # include <vos/istream.hxx> 30 #ifndef _VOS_REFERMCE_HXX_ 31 # include <vos/refernce.hxx> 32 #endif 33 # include <vos/refobj.hxx> 34 # include <rtl/ustring.hxx> 35 # include <osl/socket.h> 36 # include <vos/vosdllapi.h> 37 38 #include <osl/time.h> 39 40 namespace vos 41 { 42 43 /** Base data types 44 */ 45 class ISocketTypes 46 { 47 public: 48 ISocketTypes()49 ISocketTypes() { } ~ISocketTypes()50 virtual ~ISocketTypes() { } 51 52 /* 53 Represents the address-family of a socket 54 */ 55 enum TAddrFamily { 56 TFamily_Inet = osl_Socket_FamilyInet, /* IP */ 57 TFamily_Ipx = osl_Socket_FamilyIpx, /* Novell IPX/SPX */ 58 TFamily_Invalid = osl_Socket_FamilyInvalid 59 }; 60 61 /* 62 represent a specific protocol within a address-family 63 */ 64 enum TProtocol { 65 TProtocol_Ip = osl_Socket_ProtocolIp, /* for all af_inet */ 66 TProtocol_Ipx = osl_Socket_ProtocolIpx, /* af_ipx datagram sockets (IPX) */ 67 TProtocol_Spx = osl_Socket_ProtocolSpx, /* af_ipx seqpacket or stream for SPX */ 68 TProtocol_SpxII = osl_Socket_ProtocolSpxII, /* af_ipx seqpacket or stream for SPX II */ 69 TProtocol_Invalid = osl_Socket_ProtocolInvalid 70 }; 71 72 /* 73 Represents the type of a socket 74 */ 75 enum TSocketType { 76 TType_Stream = osl_Socket_TypeStream, 77 TType_Dgram = osl_Socket_TypeDgram, 78 TType_Raw = osl_Socket_TypeRaw, 79 TType_Rdm = osl_Socket_TypeRdm, 80 TType_SeqPacket = osl_Socket_TypeSeqPacket, 81 TType_Invalid = osl_Socket_TypeInvalid 82 }; 83 84 /* 85 Represents socket-options 86 */ 87 enum TSocketOption { 88 TOption_Debug = osl_Socket_OptionDebug, 89 TOption_AcceptConn = osl_Socket_OptionAcceptConn, 90 TOption_ReuseAddr = osl_Socket_OptionReuseAddr, 91 TOption_KeepAlive = osl_Socket_OptionKeepAlive, 92 TOption_DontRoute = osl_Socket_OptionDontRoute, 93 TOption_Broadcast = osl_Socket_OptionBroadcast, 94 TOption_UseLoopback = osl_Socket_OptionUseLoopback, 95 TOption_Linger = osl_Socket_OptionLinger, 96 TOption_OOBinLine = osl_Socket_OptionOOBinLine, 97 TOption_SndBuf = osl_Socket_OptionSndBuf, 98 TOption_RcvBuf = osl_Socket_OptionRcvBuf, 99 TOption_SndLowat = osl_Socket_OptionSndLowat, 100 TOption_RcvLowat = osl_Socket_OptionRcvLowat, 101 TOption_SndTimeo = osl_Socket_OptionSndTimeo, 102 TOption_RcvTimeo = osl_Socket_OptionRcvTimeo, 103 TOption_Error = osl_Socket_OptionError, 104 TOption_Type = osl_Socket_OptionType, 105 TOption_TcpNoDelay = osl_Socket_OptionTcpNoDelay, 106 TOption_Invalid = osl_Socket_OptionInvalid 107 }; 108 109 /* 110 Represents the different socket-option levels 111 */ 112 enum TSocketOptionLevel { 113 TLevel_Socket = osl_Socket_LevelSocket, 114 TLevel_Tcp = osl_Socket_LevelTcp, 115 TLevel_Invalid = osl_Socket_LevelInvalid 116 }; 117 118 /* 119 Represents flags to be used with send/recv-calls. 120 */ 121 enum TSocketMsgFlag { 122 TMsg_Normal = osl_Socket_MsgNormal, 123 TMsg_OOB = osl_Socket_MsgOOB, 124 TMsg_Peek = osl_Socket_MsgPeek, 125 TMsg_DontRoute = osl_Socket_MsgDontRoute, 126 TMsg_MaxIOVLen = osl_Socket_MsgMaxIOVLen, 127 TMsg_Invalid = osl_Socket_MsgInvalid 128 }; 129 130 /* 131 Used by shutdown to denote which end of the socket to "close". 132 */ 133 enum TSocketDirection { 134 TDirection_Read = osl_Socket_DirRead, 135 TDirection_Write = osl_Socket_DirWrite, 136 TDirection_ReadWrite = osl_Socket_DirReadWrite, 137 TDirection_Invalid = osl_Socket_DirInvalid 138 }; 139 140 enum TSocketError { 141 E_None = osl_Socket_E_None, /* no error */ 142 E_NotSocket = osl_Socket_E_NotSocket, /* Socket operation on non-socket */ 143 E_DestAddrReq = osl_Socket_E_DestAddrReq, /* Destination address required */ 144 E_MsgSize = osl_Socket_E_MsgSize, /* Message too sal_Int32 */ 145 E_Prototype = osl_Socket_E_Prototype, /* Protocol wrong type for socket */ 146 E_NoProtocol = osl_Socket_E_NoProtocol, /* Protocol not available */ 147 E_ProtocolNoSupport = osl_Socket_E_ProtocolNoSupport, /* Protocol not supported */ 148 E_TypeNoSupport = osl_Socket_E_TypeNoSupport, /* Socket type not supported */ 149 E_OpNotSupport = osl_Socket_E_OpNotSupport, /* Operation not supported on socket */ 150 E_PfNoSupport = osl_Socket_E_PfNoSupport, /* Protocol family not supported */ 151 E_AfNoSupport = osl_Socket_E_AfNoSupport, /* Address family not supported by */ 152 /* protocol family */ 153 E_AddrInUse = osl_Socket_E_AddrInUse, /* Address already in use */ 154 E_AddrNotAvail = osl_Socket_E_AddrNotAvail, /* Can't assign requested address */ 155 E_NetDown = osl_Socket_E_NetDown, /* Network is down */ 156 E_NetUnreachable = osl_Socket_E_NetUnreachable, /* Network is unreachable */ 157 E_NetReset = osl_Socket_E_NetReset, /* Network dropped connection because */ 158 /* of reset */ 159 E_ConnAborted = osl_Socket_E_ConnAborted, /* Software caused connection abort */ 160 E_ConnReset = osl_Socket_E_ConnReset, /* Connection reset by peer */ 161 E_NoBufferSpace = osl_Socket_E_NoBufferSpace, /* No buffer space available */ 162 E_IsConnected = osl_Socket_E_IsConnected, /* Socket is already connected */ 163 E_NotConnected = osl_Socket_E_NotConnected, /* Socket is not connected */ 164 E_Shutdown = osl_Socket_E_Shutdown, /* Can't send after socket shutdown */ 165 E_TooManyRefs = osl_Socket_E_TooManyRefs, /* Too many references: can't splice */ 166 E_TimedOut = osl_Socket_E_TimedOut, /* Connection timed out */ 167 E_ConnRefused = osl_Socket_E_ConnRefused, /* Connection refused */ 168 E_HostDown = osl_Socket_E_HostDown, /* Host is down */ 169 E_HostUnreachable = osl_Socket_E_HostUnreachable, /* No route to host */ 170 E_WouldBlock = osl_Socket_E_WouldBlock, /* call would block on non-blocking socket */ 171 E_Already = osl_Socket_E_Already, /* operation already in progress */ 172 E_InProgress = osl_Socket_E_InProgress, /* operation now in progress */ 173 174 E_Invalid = osl_Socket_E_InvalidError /* unmapped error */ 175 }; 176 177 enum TResult { 178 TResult_Ok = osl_Socket_Ok, /* successful completion */ 179 TResult_Error = osl_Socket_Error, /* error occurred, check osl_getLastSocketError() for details */ 180 TResult_TimedOut = osl_Socket_TimedOut, /* blocking operation timed out */ 181 TResult_Interrupted = osl_Socket_Interrupted, /* blocking operation was interrupted */ 182 TResult_InProgress = osl_Socket_InProgress /* nonblocking operation is in progress */ 183 }; 184 }; 185 186 187 /** Base class for socket addresses. 188 */ 189 class ISocketAddr : public vos::ISocketTypes 190 { 191 public: ~ISocketAddr()192 virtual ~ISocketAddr() { } 193 194 195 virtual TAddrFamily SAL_CALL getFamily() const= 0; 196 virtual TResult SAL_CALL getHostname(::rtl::OUString& strHostName) const= 0; 197 virtual SAL_CALL operator oslSocketAddr() const= 0; 198 virtual void SAL_CALL operator= (oslSocketAddr Addr)= 0; 199 virtual sal_Bool SAL_CALL operator== (oslSocketAddr Addr)= 0; 200 }; 201 202 class VOS_DLLPUBLIC OSocketAddr : public vos::ISocketAddr, 203 public vos::OObject 204 205 { 206 VOS_DECLARE_CLASSINFO(vos::OSocketAddr); 207 public: 208 209 /** Creates socket address of unknown type. 210 */ 211 OSocketAddr(); 212 213 /** Copy constructor. 214 */ 215 OSocketAddr(const OSocketAddr& Addr); 216 217 /** 218 */ 219 OSocketAddr(oslSocketAddr Addr); 220 221 /** destroys underlying oslSocketAddress 222 */ 223 virtual ~OSocketAddr(); 224 225 /** Queries the socket for its address family. 226 @return the address family of the socket. 227 */ 228 virtual TAddrFamily SAL_CALL getFamily() const; 229 230 /** Cast Object to the underlying oslSocketAddr. 231 */ 232 virtual SAL_CALL operator oslSocketAddr() const; 233 234 /** Converts the address to a (human readable) domain-name. 235 @return the hostname represented by the address. 236 On failure returns the empty string. 237 */ 238 virtual TResult SAL_CALL getHostname(::rtl::OUString& strHostName) const; 239 240 /** Get the hostname for the local interface. 241 @return the hostname or an error. 242 */ 243 static TResult SAL_CALL getLocalHostname(::rtl::OUString& strLocalHostName); 244 245 /** Tries to find an address for a host. 246 @return A new created socket-address or 0 if the name could not be found. 247 */ 248 static oslSocketAddr SAL_CALL resolveHostname(const ::rtl::OUString& strHostName); 249 250 /** Wraps itself around the osl Socket-Address. 251 The object assumes ownership of the Addr, it 252 will be destroyed by destructor(). If the socket is already attached to 253 an oslSocketAddr, the existing one will be destroyed. 254 */ 255 virtual void SAL_CALL operator= (oslSocketAddr Addr); 256 257 /** Compares to Addr 258 */ 259 virtual sal_Bool SAL_CALL operator== (oslSocketAddr Addr); 260 261 /** Makes a copy of Addr. 262 */ 263 OSocketAddr& SAL_CALL operator= (const OSocketAddr& Addr); 264 265 266 protected: 267 268 oslSocketAddr m_SockAddr; 269 }; 270 271 272 /** Represents an internet-address. 273 */ 274 class VOS_DLLPUBLIC OInetSocketAddr : public vos::OSocketAddr 275 { 276 VOS_DECLARE_CLASSINFO(vos::OInetSocketAddr); 277 public: 278 279 /** Creates an empty internet-address (INADDR_ANY). 280 */ 281 OInetSocketAddr(); 282 283 /** Wraps itself around the osl Socket-Address. 284 The object assumes ownership of the Addr, it 285 will be destroyed by ~OInetSocketAddr(). 286 */ 287 OInetSocketAddr(oslSocketAddr Addr); 288 289 /** 290 Create a socket address either from a dotted decimal address 291 (e.g. 141.99.128.50) or a hostname (e.g. www.openoffice.org). 292 */ 293 OInetSocketAddr(const ::rtl::OUString& strAddrOrHostName, sal_Int32 Port); 294 295 /** 296 Copy constructor. 297 */ 298 OInetSocketAddr(const OInetSocketAddr& Addr); 299 300 /** 301 */ 302 OInetSocketAddr(const OSocketAddr& Addr); 303 304 305 virtual ~OInetSocketAddr(); 306 307 /** 308 */ 309 virtual void SAL_CALL operator= (oslSocketAddr Addr); 310 311 /** 312 */ 313 virtual sal_Bool SAL_CALL operator== (oslSocketAddr Addr); 314 315 /** 316 */ 317 OInetSocketAddr& SAL_CALL operator= (const OInetSocketAddr& Addr); 318 319 /** 320 */ 321 OInetSocketAddr& SAL_CALL operator= (const OSocketAddr& Addr); 322 323 /** 324 Tries to find the port associated with the given service/protocol- 325 pair (e.g. "ftp"/"tcp"). 326 @return the port number in host-byte order or CVOS_PORT_NONE 327 if no service/protocol pair could be found. 328 */ 329 static sal_Int32 SAL_CALL getServicePort(const ::rtl::OUString& strServiceName, 330 const ::rtl::OUString& strProtocolName= ::rtl::OUString::createFromAscii( "tcp" ) ); 331 332 333 /** Delivers the port number of the address. 334 @return the port in host-byte order or or OSL_INVALID_PORT on errors. 335 */ 336 sal_Int32 SAL_CALL getPort() const; 337 338 /** Sets the port number of the address. 339 @return False if the port couldn't be set 340 (e.g because the address is not of family TFamily_Inet). 341 */ 342 sal_Bool SAL_CALL setPort(sal_Int32 Port); 343 344 /** @return the dotted-address-form (141.99.128.90) of this address. 345 On failure returns the empty string. 346 */ 347 TResult SAL_CALL getDottedAddr(::rtl::OUString& strDottedAddr) const; 348 349 /** Sets the host-part of the address from the dotted-address-form (141.99.128.90) 350 or from a hostname. 351 @param strDottedAddrOrHostname the address in dotted form or a hostname. 352 */ 353 sal_Bool SAL_CALL setAddr(const ::rtl::OUString& strDottedAddrOrHostname); 354 355 }; 356 357 /** Represents an IPX/SPX address. 358 */ 359 class VOS_DLLPUBLIC OIpxSocketAddr : public vos::OSocketAddr 360 { 361 VOS_DECLARE_CLASSINFO(vos::OIpxSocketAddr); 362 public: 363 364 typedef oslSocketIpxNetNumber TIpxNetNumber; 365 typedef oslSocketIpxNodeNumber TIpxNodeNumber; 366 367 /** Creates an empty ipx-address. 368 */ 369 OIpxSocketAddr(); 370 371 /** Wraps itself around the osl Socket-Address. 372 The object assumes ownership of the Addr, it 373 will be destroyed by the destructor. 374 */ 375 OIpxSocketAddr(oslSocketAddr Addr); 376 377 /** Create an IPX/SPX socketaddress from native parameters. 378 */ 379 OIpxSocketAddr(const ::rtl::OUString& strNetNumber, 380 const ::rtl::OUString& strNodeNumber, 381 sal_uInt32 SocketNumber); 382 383 /** Copy constructor. 384 */ 385 OIpxSocketAddr(const OIpxSocketAddr& Addr); 386 387 /** 388 */ 389 OIpxSocketAddr(const OSocketAddr& Addr); 390 391 virtual ~OIpxSocketAddr(); 392 393 /** 394 */ 395 virtual void SAL_CALL operator= (oslSocketAddr Addr); 396 397 /** 398 */ 399 virtual sal_Bool SAL_CALL operator== (oslSocketAddr Addr); 400 401 /** 402 */ 403 OIpxSocketAddr& SAL_CALL operator= (const OIpxSocketAddr& Addr); 404 405 /** 406 */ 407 OIpxSocketAddr& SAL_CALL operator= (const OSocketAddr& Addr); 408 409 /** 410 */ 411 TResult SAL_CALL getNetNumber(TIpxNetNumber& NetNumber) const; 412 413 /** 414 */ 415 TResult SAL_CALL getNodeNumber(TIpxNodeNumber& NodeNumber) const; 416 417 /** 418 */ 419 sal_uInt32 SAL_CALL getSocketNumber() const; 420 421 /** Builds a human readable string in the format network.node:socket. 422 The numbers are given in hexadecimal form. 423 */ 424 void SAL_CALL getAddressString(::rtl::OUString& strAddressString) const; 425 }; 426 427 428 /** Represents a socket. 429 */ 430 class VOS_DLLPUBLIC OSocket : public vos::ISocketTypes, 431 public vos::OReference, 432 public vos::OObject 433 { 434 VOS_DECLARE_CLASSINFO(vos::OSocket); 435 436 protected: 437 typedef ORefObj<oslSocket> SockRef; 438 439 SockRef* m_pSockRef; 440 441 TimeValue* m_pSendTimeout; 442 TimeValue* m_pRecvTimeout; 443 444 public: 445 446 /** Does not create a socket. Use assignment operator to 447 make this a usable socket. 448 */ 449 OSocket(); 450 451 /** Creates a socket. 452 @param Family 453 @param Type 454 @param Protocol 455 */ 456 OSocket(TSocketType Type, 457 TAddrFamily Family = TFamily_Inet, 458 TProtocol Protocol = TProtocol_Ip); 459 460 /** Copy constructor. 461 */ 462 OSocket(const OSocket& sock); 463 464 /** Creates socket as wrapper around the underlying oslSocket. 465 @param Socket 466 */ 467 OSocket(oslSocket Socket); 468 469 /** Destructor. Destroys the underlying oslSocket. 470 */ 471 virtual ~OSocket(); 472 473 /** Create a socket with the given attributes. 474 If socket was already created, the old one will be discarded. 475 @param Type 476 @param Family 477 @param Protocol 478 @return True if socket was successfully created. 479 */ 480 sal_Bool SAL_CALL create(TSocketType Type, 481 TAddrFamily Family = TFamily_Inet, 482 TProtocol Protocol = TProtocol_Ip); 483 484 /** Assignment operator. If socket was already created, the old one will 485 be discarded. 486 */ 487 OSocket& SAL_CALL operator= (const OSocket& sock); 488 489 /** Allow cast to underlying oslSocket. 490 */ 491 SAL_CALL operator oslSocket() const; 492 493 /** Checks if the socket is valid. 494 @return True if the object represents a valid socket. 495 */ 496 sal_Bool SAL_CALL isValid() const; 497 operator ==(const OSocket & rSocket)498 sal_Bool SAL_CALL operator==( const OSocket& rSocket ) 499 { 500 return m_pSockRef == rSocket.m_pSockRef; 501 } 502 503 /** Closes the socket. 504 */ 505 virtual void SAL_CALL close(); 506 507 /** Retrieves the address of the local interface of this socket. 508 @param Addr [out] receives the address. 509 */ 510 void SAL_CALL getLocalAddr(OSocketAddr& Addr) const; 511 512 /** Get the local port of the socket. 513 @return the port number or OSL_INVALID_PORT on errors. 514 */ 515 sal_Int32 SAL_CALL getLocalPort() const; 516 517 /** Get the hostname for the local interface. 518 @return the hostname or an empty string (""). 519 */ 520 TResult SAL_CALL getLocalHost(::rtl::OUString& strLocalHost) const; 521 522 /** Retrieves the address of the remote host of this socket. 523 @param Addr [out] receives the address. 524 */ 525 void SAL_CALL getPeerAddr(OSocketAddr& Addr) const; 526 527 /** Get the remote port of the socket. 528 @return the port number or OSL_INVALID_PORT on errors. 529 */ 530 sal_Int32 SAL_CALL getPeerPort() const; 531 532 /** Get the hostname for the remote interface. 533 @return the hostname or an empty string (""). 534 */ 535 TResult SAL_CALL getPeerHost(::rtl::OUString& strPeerHost) const; 536 537 /** Binds the socket to the specified (local) interface. 538 @param LocalInterface Address of the Interface 539 @return True if bind was successful. 540 */ 541 sal_Bool SAL_CALL bind(const OSocketAddr& LocalInterface); 542 543 544 /** Blocking send operations will unblock after the send-timeout. 545 @return 0 for disables timeout else timeout value. 546 */ getSendTimeout() const547 const TimeValue* SAL_CALL getSendTimeout() const 548 { return m_pSendTimeout; } 549 550 /** Blocking receive operations will unblock after the send-timeout. 551 @return 0 for disables timeout else timeout value. 552 */ getRecvTimeout() const553 const TimeValue* SAL_CALL getRecvTimeout() const 554 { return m_pRecvTimeout; } 555 556 /** Blocking send operations will unblock after the send-timeout. 557 @param time pTimeout == 0 disables timeout. pTimeout != 0 sets timeout value. 558 */ 559 void SAL_CALL setSendTimeout(const TimeValue* pTimeout = 0); 560 561 /** Blocking receive operations will unblock after the send-timeout. 562 @param time pTimeout == 0 disables timeout. pTimeout != 0 sets timeout value. 563 */ 564 void SAL_CALL setRecvTimeout(const TimeValue* pTimeout = 0); 565 566 /** Checks if read operations will block. 567 You can specify a timeout-value in seconds/nanoseconds that denotes 568 how sal_Int32 the operation will block if the Socket is not ready. 569 @return True if read operations (recv, recvFrom, accept) on the Socket 570 will NOT block; False if it would block or if an error occurred. 571 572 @param pTimeout if 0, the operation will block without a timeout. Otherwise 573 the specified amount of time. 574 */ 575 sal_Bool SAL_CALL isRecvReady(const TimeValue* pTimeout = 0) const; 576 577 /** Checks if send operations will block. 578 You can specify a timeout-value in seconds/nanoseconds that denotes 579 how sal_Int32 the operation will block if the Socket is not ready. 580 @return True if send operations (send, sendTo) on the Socket 581 will NOT block; False if it would block or if an error occurred. 582 583 @param pTimeout if 0, the operation will block without a timeout. Otherwise 584 the specified amount of time. 585 */ 586 sal_Bool SAL_CALL isSendReady(const TimeValue* pTimeout = 0) const; 587 588 /** Checks if a request for out-of-band data will block. 589 You can specify a timeout-value in seconds/nanoseconds that denotes 590 how sal_Int32 the operation will block if the Socket has no pending OOB data. 591 592 @return True if OOB-request operations (recv with appropriate flags) 593 on the Socket will NOT block; False if it would block or if an error occurred. 594 595 @param pTimeout if 0, the operation will block without a timeout. Otherwise 596 the specified amount of time. 597 */ 598 sal_Bool SAL_CALL isExceptionPending(const TimeValue* pTimeout = 0) const; 599 600 /** Retrieves option-attributes associated with the socket. 601 @param Option The attribute to query. 602 Valid values (depending on the Level) are: 603 <ul> 604 <li> TOption_Debug 605 <li> TOption_AcceptConn 606 <li> TOption_ReuseAddr 607 <li> TOption_KeepAlive 608 <li> TOption_DontRoute 609 <li> TOption_Broadcast 610 <li> TOption_UseLoopback 611 <li> TOption_Linger 612 <li> TOption_OOBinLine 613 <li> TOption_SndBuf 614 <li> TOption_RcvBuf 615 <li> TOption_SndLowat 616 <li> TOption_RcvLowat 617 <li> TOption_SndTimeo 618 <li> TOption_RcvTimeo 619 <li> TOption_Error 620 <li> TOption_Type 621 <li> TOption_TcpNoDelay 622 </ul> 623 If not above mentioned otherwise, the options are only valid for 624 level TLevel_Socket. 625 626 @param pBuffer The Buffer will be filled with the attribute. 627 628 @param BufferSize The size of pBuffer. 629 630 @param Level The option level. Valid values are: 631 <ul> 632 <li> TLevel_Socket : Socket Level 633 <li> TLevel_Tcp : Level of Transmission Control Protocol 634 </ul> 635 636 @return The size of the attribute copied into pBuffer ot -1 if an error 637 occurred. 638 */ 639 sal_Int32 SAL_CALL getOption(TSocketOption Option, 640 void* pBuffer, 641 sal_uInt32 BufferLen, 642 TSocketOptionLevel Level= TLevel_Socket) const; 643 644 /** Sets the sockets attributes. 645 646 @param Option denotes the option to modify. 647 Valid values (depending on the Level) are: 648 <ul> 649 <li> TOption_Debug 650 <li> TOption_AcceptConn 651 <li> TOption_ReuseAddr 652 <li> TOption_KeepAlive 653 <li> TOption_DontRoute 654 <li> TOption_Broadcast 655 <li> TOption_UseLoopback 656 <li> TOption_Linger 657 <li> TOption_OOBinLine 658 <li> TOption_SndBuf 659 <li> TOption_RcvBuf 660 <li> TOption_SndLowat 661 <li> TOption_RcvLowat 662 <li> TOption_SndTimeo 663 <li> TOption_RcvTimeo 664 <li> TOption_Error 665 <li> TOption_Type 666 <li> TOption_TcpNoDelay 667 </ul> 668 If not above mentioned otherwise, the options are only valid for 669 level TLevel_Socket. 670 671 @param pBuffer Pointer to a Buffer which contains the attribute-value. 672 673 @param BufferSize contains the length of the Buffer. 674 675 @param Level selects the level for which an option should be changed. 676 Valid values are: 677 <ul> 678 <li> TLevel_Socket : Socket Level 679 <li> TLevel_Tcp : Level of Transmission Control Protocol 680 </ul> 681 682 @return True if the option could be changed. 683 */ 684 sal_Bool SAL_CALL setOption(TSocketOption Option, 685 void* pBuffer, 686 sal_uInt32 BufferLen, 687 TSocketOptionLevel Level= TLevel_Socket) const; 688 689 690 /** Enables/disables non-blocking mode of the socket. 691 @param On If True, non-blocking mode will be switched on, if False 692 socket will become a blocking socket, which is the default behavior of a 693 socket. 694 @return True if mode could be set. 695 */ 696 sal_Bool SAL_CALL enableNonBlockingMode(sal_Bool On= sal_True); 697 698 /** Query non-blocking mode of the socket. 699 @return True if non-blocking mode is set. 700 */ 701 sal_Bool SAL_CALL isNonBlockingMode() const; 702 703 /** Queries the socket for its type. 704 @return one of: 705 <ul> 706 <li> TType_Stream 707 <li> TType_Dgram 708 <li> TType_Raw 709 <li> TType_Rdm 710 <li> TType_SeqPacket 711 <li> TType_Invalid 712 </ul> 713 */ 714 TSocketType SAL_CALL getType() const; 715 716 717 /** Gets and clears the error status of the socket. 718 @returns the current error state. 719 */ 720 sal_Int32 SAL_CALL clearError() const; 721 722 /** Enables/Disables debugging. 723 @param opt 1 sets, 0 resets, -1 won't change anything 724 @return the previous setting 725 */ 726 sal_Int32 SAL_CALL setDebug(sal_Int32 opt = -1) const; 727 728 /** Allow the socket to be bound to an address that is already in use. 729 @param opt 1 sets, 0 resets, -1 won't change anything 730 @return the previous setting 731 */ 732 sal_Int32 SAL_CALL setReuseAddr(sal_Int32 opt = -1) const; 733 734 /** Send keepalive-packets. 735 @param opt 1 sets, 0 resets, -1 won't change anything 736 @return the previous setting 737 */ 738 sal_Int32 SAL_CALL setKeepAlive(sal_Int32 opt = -1) const; 739 740 /** Do not route: send directly to interface. 741 @param opt 1 sets, 0 resets, -1 won't change anything 742 @return the previous setting 743 */ 744 sal_Int32 SAL_CALL setDontRoute(sal_Int32 opt = -1) const; 745 746 747 /** Allow transmission of broadcast messages on the socket. 748 @param opt 1 sets, 0 resets, -1 won't change anything 749 @return the previous setting 750 */ 751 sal_Int32 SAL_CALL setBroadcast(sal_Int32 opt = -1) const; 752 753 /** Receive out-of-band data in the normal data stream. 754 @param opt 1 sets, 0 resets, -1 won't change anything 755 @return the previous setting 756 */ 757 sal_Int32 SAL_CALL setOobinline(sal_Int32 opt = -1) const; 758 759 /** Linger on close if unsent data is present. 760 @param time values > 0 enable lingering with a timeout of time in seconds. 761 If time is 0, lingering will be disabled. If time is -1 no changes will occur. 762 @return the previous setting (0 == off, > 0 timeout-value in seconds). 763 */ 764 sal_Int32 SAL_CALL setLinger(sal_Int32 time = -1) const; 765 766 /** Specify buffer size for sends. 767 You might want to use getOption() to check if the size changes were 768 really successful. 769 @param size Size >= 0 sets the size, -1 won't change anything. 770 @return the previous setting 771 */ 772 sal_Int32 SAL_CALL setSendBufSize(sal_Int32 size =-1) const; 773 774 /** Specify buffer size for receives. 775 You might want to use getOption() to check if the size changes were 776 really successful. 777 @param size Size >= 0 sets the size, -1 won't change anything. 778 @return the previous setting 779 */ 780 sal_Int32 SAL_CALL setRecvBufSize(sal_Int32 size =-1) const; 781 782 /** Disables the Nagle algorithm for send coalescing. (Do not 783 collect data until a packet is full, instead send immediately. 784 This increases network traffic but might improve response-times.) 785 @param opt 1 sets, 0 resets, -1 won't change anything 786 @return the previous setting 787 */ 788 sal_Int32 SAL_CALL setTcpNoDelay(sal_Int32 sz =-1) const; 789 790 /** Builds a string with the last error-message for the socket. 791 @param pBuffer is filled with the error message. 792 @param nSize the size of pBuffer. The message will be cut 793 sal_Int16 if the buffer isn't large enough, but still remains 794 a valid zero-terminated string. 795 */ 796 void SAL_CALL getError(::rtl::OUString& strError) const; 797 798 /** Delivers a constant describing the last error for the socket system. 799 @return NONE if no error occurred, invalid_SocketError if 800 an unknown (unmapped) error occurred, otherwise an enum describing the 801 error. 802 */ 803 TSocketError SAL_CALL getError() const; 804 805 }; 806 807 /** A socket to send or receive a stream of data. 808 */ 809 class VOS_DLLPUBLIC OStreamSocket : public vos::OSocket, 810 public vos::IStream 811 { 812 VOS_DECLARE_CLASSINFO(vos::OStreamSocket); 813 public: 814 815 /** Creates an unattached socket. You must attach the socket to an oslSocket 816 e.g. by using the operator=(oslSocket), before you can use the stream- 817 functionality of the object. 818 */ 819 OStreamSocket(); 820 821 822 823 /** Creates socket as wrapper around the underlying oslSocket. 824 @param Socket 825 */ 826 OStreamSocket(oslSocket Socket); 827 828 829 /** Copy constructor. 830 @param Socket 831 */ 832 OStreamSocket(const OStreamSocket& Socket); 833 834 835 /** 836 */ 837 OStreamSocket(const OSocket& Socket); 838 839 /** Destructor. Calls shutdown(readwrite) and close(). 840 */ 841 virtual ~OStreamSocket(); 842 843 /** Closes the socket after calling shutdown. 844 */ 845 virtual void SAL_CALL close(); 846 847 /** Attaches the oslSocket to this object. If the object 848 already was attached to an oslSocket, the old one will 849 be closed and destroyed. 850 @param Socket. 851 */ 852 OStreamSocket& SAL_CALL operator=(oslSocket Socket); 853 854 /** 855 */ 856 OStreamSocket& SAL_CALL operator=(const OSocket& Socket); 857 858 /** 859 */ 860 OStreamSocket& SAL_CALL operator=(const OStreamSocket& Socket); 861 862 /** Retrieves n bytes from the stream and copies them into pBuffer. 863 The method avoids incomplete reads due to packet boundaries. 864 @param pBuffer receives the read data. 865 @param n the number of bytes to read. pBuffer must be large enough 866 to hold the n bytes! 867 @return the number of read bytes. The number will only be smaller than 868 n if an exceptional condition (e.g. connection closed) occurs. 869 */ 870 virtual sal_Int32 SAL_CALL read(void* pBuffer, sal_uInt32 n) const; 871 872 /** Writes n bytes from pBuffer to the stream. The method avoids 873 incomplete writes due to packet boundaries. 874 @param pBuffer contains the data to be written. 875 @param n the number of bytes to write. 876 @return the number of written bytes. The number will only be smaller than 877 n if an exceptional condition (e.g. connection closed) occurs. 878 */ 879 virtual sal_Int32 SAL_CALL write(const void* pBuffer, sal_uInt32 n); 880 881 /** Checks if socket is closed. 882 @return True if socket is closed. 883 */ 884 virtual sal_Bool SAL_CALL isEof() const; 885 886 /** Tries to receives BytesToRead data from the connected socket, 887 888 @param pBuffer [out] Points to a buffer that will be filled with the received 889 data. 890 @param BytesToRead [in] The number of bytes to read. pBuffer must have at least 891 this size. 892 @param Flag [in] Modifier for the call. Valid values are: 893 <ul> 894 <li> TMsg_Normal 895 <li> TMsg_OOB 896 <li> TMsg_Peek 897 <li> TMsg_DontRoute 898 <li> TMsg_MaxIOVLen 899 </ul> 900 901 @return the number of received bytes. 902 */ 903 sal_Int32 SAL_CALL recv(void* pBuffer, 904 sal_uInt32 BytesToRead, 905 TSocketMsgFlag Flag= TMsg_Normal); 906 907 908 /** Tries to sends BytesToSend data from the connected socket. 909 910 @param pBuffer [in] Points to a buffer that contains the send-data. 911 @param BytesToSend [in] The number of bytes to send. pBuffer must have at least 912 this size. 913 @param Flag [in] Modifier for the call. Valid values are: 914 <ul> 915 <li> TMsg_Normal 916 <li> TMsg_OOB 917 <li> TMsg_Peek 918 <li> TMsg_DontRoute 919 <li> TMsg_MaxIOVLen 920 </ul> 921 922 @return the number of transferred bytes. 923 */ 924 sal_Int32 SAL_CALL send(const void* pBuffer, 925 sal_uInt32 BytesToSend, 926 TSocketMsgFlag Flag= TMsg_Normal); 927 928 /** Closes a connection in a controlled manner. 929 @param Direction Says which "end" of the socket is to be closed. 930 */ 931 sal_Bool SAL_CALL shutdown(TSocketDirection Direction= TDirection_ReadWrite); 932 protected: 933 934 /** Creates a socket. This constructor is used only by derived classes 935 (e.g. OConnectorSocket). 936 @param Family 937 @param Protocol 938 @param Type For some protocols it might be desirable to 939 use a different type than sock_stream (like sock_seqpacket). 940 Therefore we do not hide this parameter here. 941 */ 942 OStreamSocket(TAddrFamily Family, 943 TProtocol Protocol, 944 TSocketType Type= TType_Stream); 945 946 947 }; 948 949 950 /** A socket to accept incoming connections. 951 */ 952 class VOS_DLLPUBLIC OAcceptorSocket : public vos::OSocket 953 { 954 VOS_DECLARE_CLASSINFO(vos::OAcceptorSocket); 955 public: 956 957 /** Creates a socket that can accept connections. 958 @param Type For some protocols it might be desirable to 959 use a different type than sock_stream (like sock_seqpacket). 960 Therefore we do not hide this parameter here. 961 */ 962 OAcceptorSocket(TAddrFamily Family= TFamily_Inet, 963 TProtocol Protocol= TProtocol_Ip, 964 TSocketType Type= TType_Stream); 965 966 /** Copy constructor. 967 */ 968 OAcceptorSocket(const OAcceptorSocket& Socket); 969 970 /** Destructor. Closes the socket and destroys the underlying oslSocket. 971 */ 972 virtual ~OAcceptorSocket(); 973 974 /** Closes the socket. Also calls shutdown, needed to unblock 975 accept on some systems. 976 */ 977 virtual void SAL_CALL close(); 978 979 /** Prepare a socket for the accept-call. 980 @param MaxPendingConnections The maximum number of pending 981 connections (waiting to be accepted) on this socket. If you use 982 -1, a system default value is used. 983 @return True if call was successful. 984 */ 985 sal_Bool SAL_CALL listen(sal_Int32 MaxPendingConnections= -1); 986 987 /** Accepts incoming connections on the socket. You must 988 precede this call with bind() and listen(). 989 @param Connection receives the incoming connection. 990 @return result_ok: if a connection has been accepted, 991 result_timeout: if m_RecvTimeout milliseconds passed without connect, 992 result_error: on errors. 993 */ 994 TResult SAL_CALL acceptConnection(OStreamSocket& Connection); 995 996 /** Accepts incoming connections on the socket. You must 997 precede this call with bind() and listen(). 998 @param PeerAddr receives the address of the connecting entity 999 (your communication partner). 1000 @param Connection receives the incoming connection. 1001 @return True if a connection has been accepted, False on errors. 1002 @return result_ok: if a connection has been accepted, 1003 result_timeout: if m_RecvTimeout milliseconds passed without connect, 1004 result_error: on errors. 1005 */ 1006 TResult SAL_CALL acceptConnection(OStreamSocket& Connection, 1007 OSocketAddr& PeerAddr); 1008 1009 }; 1010 1011 1012 /** A socket to initiate a connection. 1013 */ 1014 class VOS_DLLPUBLIC OConnectorSocket : public vos::OStreamSocket 1015 { 1016 VOS_DECLARE_CLASSINFO(vos::OConnectorSocket); 1017 public: 1018 1019 /** Creates a socket that can accept connections. 1020 @param Type For some protocols it might be desirable to 1021 use a different type than sock_stream (like sock_seqpacket). 1022 Therefore we do not hide this parameter here. 1023 */ 1024 OConnectorSocket(TAddrFamily Family= TFamily_Inet, 1025 TProtocol Protocol= TProtocol_Ip, 1026 TSocketType Type= TType_Stream); 1027 1028 /** Copy constructor. Doesn't duplicate oslSocket. 1029 */ 1030 OConnectorSocket(const OConnectorSocket& Socket); 1031 1032 /** Destructor. Relies on ~OStreamSocket to close down connection gracefully. 1033 */ 1034 virtual ~OConnectorSocket(); 1035 1036 /** Connects the socket to a (remote) host. 1037 @param TargetHost The address of the target. 1038 @param msTimeout The timeout in milliseconds. Use -1 to block. 1039 @return result_ok if connected successfully, 1040 result_timeout on timeout, 1041 result_interrupted if unblocked forcefully (by close()), 1042 result_error if connect failed. 1043 */ 1044 TResult SAL_CALL connect(const OSocketAddr& TargetHost, const TimeValue* pTimeout = 0); 1045 }; 1046 1047 1048 /** A connectionless socket to send and receive datagrams. 1049 */ 1050 class VOS_DLLPUBLIC ODatagramSocket : public vos::OSocket 1051 { 1052 VOS_DECLARE_CLASSINFO(vos::ODatagramSocket); 1053 public: 1054 1055 /** Creates a datagram socket. 1056 @param Type is sock_dgram by default. 1057 */ 1058 ODatagramSocket(TAddrFamily Family= TFamily_Inet, 1059 TProtocol Protocol= TProtocol_Ip, 1060 TSocketType Type= TType_Dgram); 1061 1062 /** Copy constructor. 1063 */ 1064 ODatagramSocket(const ODatagramSocket& Socket); 1065 1066 /** Destructor. Closes the socket. 1067 */ 1068 virtual ~ODatagramSocket(); 1069 1070 1071 /** Tries to receives BufferSize data from the socket, if no error occurs. 1072 1073 @param pSenderAddr [out] You must provide pointer to a SocketAddr. 1074 It will be filled with the address of the datagrams sender. 1075 If pSenderAddr is 0, it is ignored. 1076 @param pBuffer [out] Points to a buffer that will be filled with the received 1077 datagram. 1078 @param BufferSize [in] The size of pBuffer. 1079 @param Flag [in] Modifier for the call. Valid values are: 1080 <ul> 1081 <li> TMsg_Normal 1082 <li> TMsg_OOB 1083 <li> TMsg_Peek 1084 <li> TMsg_DontRoute 1085 <li> TMsg_MaxIOVLen 1086 </ul> 1087 1088 @return the number of received bytes. 1089 */ 1090 sal_Int32 SAL_CALL recvFrom(void* pBuffer, 1091 sal_uInt32 BufferSize, 1092 OSocketAddr* pSenderAddr= 0, 1093 TSocketMsgFlag Flag= TMsg_Normal); 1094 1095 /** Tries to send one datagram with BytesToSend data to the given ReceiverAddr. 1096 Since we only send one packet, we don't need to concern ourselves here with 1097 incomplete sends due to packet boundaries. 1098 1099 @param ReceiverAddr [in] A SocketAddr that contains 1100 the destination address for this send. 1101 1102 @param pBuffer [in] Points to a buffer that contains the send-data. 1103 @param BufferSize [in] The number of bytes to send. pBuffer must have at least 1104 this size. 1105 @param Flag [in] Modifier for the call. Valid values are: 1106 <ul> 1107 <li> TMsg_Normal 1108 <li> TMsg_OOB 1109 <li> TMsg_Peek 1110 <li> TMsg_DontRoute 1111 <li> TMsg_MaxIOVLen 1112 </ul> 1113 1114 @return the number of transferred bytes. 1115 */ 1116 sal_Int32 SAL_CALL sendTo(const OSocketAddr& ReceiverAddr, 1117 const void* pBuffer, 1118 sal_uInt32 BufferSize, 1119 TSocketMsgFlag Flag= TMsg_Normal); 1120 }; 1121 1122 1123 1124 } 1125 1126 #endif // _VOS_SOCKET_HXX_ 1127 1128 /* vim: set noet sw=4 ts=4: */ 1129