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