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