RawSocket.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // RawSocket.h
  3. //
  4. // Library: Net
  5. // Package: Sockets
  6. // Module: RawSocket
  7. //
  8. // Definition of the RawSocket class.
  9. //
  10. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Net_RawSocket_INCLUDED
  16. #define Net_RawSocket_INCLUDED
  17. #include "Poco/Net/Net.h"
  18. #include "Poco/Net/Socket.h"
  19. namespace Poco {
  20. namespace Net {
  21. class Net_API RawSocket: public Socket
  22. /// This class provides an interface to a
  23. /// raw IP socket.
  24. {
  25. public:
  26. RawSocket();
  27. /// Creates an unconnected IPv4 raw socket.
  28. RawSocket(SocketAddress::Family family, int proto = IPPROTO_RAW);
  29. /// Creates an unconnected raw socket.
  30. ///
  31. /// The socket will be created for the
  32. /// given address family.
  33. RawSocket(const SocketAddress& address, bool reuseAddress = false);
  34. /// Creates a raw socket and binds it
  35. /// to the given address.
  36. ///
  37. /// Depending on the address family, the socket
  38. /// will be either an IPv4 or an IPv6 socket.
  39. RawSocket(const Socket& socket);
  40. /// Creates the RawSocket with the SocketImpl
  41. /// from another socket. The SocketImpl must be
  42. /// a RawSocketImpl, otherwise an InvalidArgumentException
  43. /// will be thrown.
  44. ~RawSocket();
  45. /// Destroys the RawSocket.
  46. RawSocket& operator = (const Socket& socket);
  47. /// Assignment operator.
  48. ///
  49. /// Releases the socket's SocketImpl and
  50. /// attaches the SocketImpl from the other socket and
  51. /// increments the reference count of the SocketImpl.
  52. void connect(const SocketAddress& address);
  53. /// Restricts incoming and outgoing
  54. /// packets to the specified address.
  55. ///
  56. /// Calls to connect() cannot come before calls to bind().
  57. void bind(const SocketAddress& address, bool reuseAddress = false);
  58. /// Bind a local address to the socket.
  59. ///
  60. /// This is usually only done when establishing a server
  61. /// socket.
  62. ///
  63. /// If reuseAddress is true, sets the SO_REUSEADDR
  64. /// socket option.
  65. ///
  66. /// Calls to connect() cannot come before calls to bind().
  67. void bind(const SocketAddress& address, bool reuseAddress, bool reusePort);
  68. /// Bind a local address to the socket.
  69. ///
  70. /// This is usually only done when establishing a server
  71. /// socket.
  72. ///
  73. /// If reuseAddress is true, sets the SO_REUSEADDR
  74. /// socket option.
  75. ///
  76. /// If reusePort is true, sets the SO_REUSEPORT
  77. /// socket option.
  78. ///
  79. /// Calls to connect() cannot come before calls to bind().
  80. int sendBytes(const void* buffer, int length, int flags = 0);
  81. /// Sends the contents of the given buffer through
  82. /// the socket.
  83. ///
  84. /// Returns the number of bytes sent, which may be
  85. /// less than the number of bytes specified.
  86. int receiveBytes(void* buffer, int length, int flags = 0);
  87. /// Receives data from the socket and stores it
  88. /// in buffer. Up to length bytes are received.
  89. ///
  90. /// Returns the number of bytes received.
  91. int sendTo(const void* buffer, int length, const SocketAddress& address, int flags = 0);
  92. /// Sends the contents of the given buffer through
  93. /// the socket to the given address.
  94. ///
  95. /// Returns the number of bytes sent, which may be
  96. /// less than the number of bytes specified.
  97. int receiveFrom(void* buffer, int length, SocketAddress& address, int flags = 0);
  98. /// Receives data from the socket and stores it
  99. /// in buffer. Up to length bytes are received.
  100. /// Stores the address of the sender in address.
  101. ///
  102. /// Returns the number of bytes received.
  103. void setBroadcast(bool flag);
  104. /// Sets the value of the SO_BROADCAST socket option.
  105. ///
  106. /// Setting this flag allows sending datagrams to
  107. /// the broadcast address.
  108. bool getBroadcast() const;
  109. /// Returns the value of the SO_BROADCAST socket option.
  110. protected:
  111. RawSocket(SocketImpl* pImpl);
  112. /// Creates the Socket and attaches the given SocketImpl.
  113. /// The socket takes ownership of the SocketImpl.
  114. ///
  115. /// The SocketImpl must be a StreamSocketImpl, otherwise
  116. /// an InvalidArgumentException will be thrown.
  117. };
  118. //
  119. // inlines
  120. //
  121. inline void RawSocket::setBroadcast(bool flag)
  122. {
  123. impl()->setBroadcast(flag);
  124. }
  125. inline bool RawSocket::getBroadcast() const
  126. {
  127. return impl()->getBroadcast();
  128. }
  129. } } // namespace Poco::Net
  130. #endif // Net_RawSocket_INCLUDED