Kea  2.1.7-git
udp_socket.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef UDP_SOCKET_H
8 #define UDP_SOCKET_H 1
9 
10 #ifndef BOOST_ASIO_HPP
11 #error "asio.hpp must be included before including this, see asiolink.h as to why"
12 #endif
13 
14 #include <netinet/in.h>
15 #include <sys/socket.h>
16 #include <unistd.h> // for some IPC/network system calls
17 
18 #include <cstddef>
19 
21 #include <asiolink/io_endpoint.h>
22 #include <asiolink/io_service.h>
23 #include <asiolink/udp_endpoint.h>
24 
25 #include <exceptions/isc_assert.h>
26 
27 namespace isc {
28 namespace asiolink {
29 
34 template <typename C>
35 class UDPSocket : public IOAsioSocket<C> {
36 private:
38  UDPSocket(const UDPSocket&);
39  UDPSocket& operator=(const UDPSocket&);
40 
41 public:
42  enum {
43  MIN_SIZE = 4096 // Minimum send and receive size
44  };
45 
51  UDPSocket(boost::asio::ip::udp::socket& socket);
52 
59  UDPSocket(IOService& service);
60 
62  virtual ~UDPSocket();
63 
65  virtual int getNative() const {
66 #if BOOST_VERSION < 106600
67  return (socket_.native());
68 #else
69  return (socket_.native_handle());
70 #endif
71  }
72 
74  virtual int getProtocol() const {
75  return (IPPROTO_UDP);
76  }
77 
81  virtual bool isOpenSynchronous() const {
82  return true;
83  }
84 
93  virtual void open(const IOEndpoint* endpoint, C& callback);
94 
105  virtual void asyncSend(const void* data, size_t length,
106  const IOEndpoint* endpoint, C& callback);
107 
119  virtual void asyncReceive(void* data, size_t length, size_t offset,
120  IOEndpoint* endpoint, C& callback);
121 
137  virtual bool processReceivedData(const void* staging, size_t length,
138  size_t& cumulative, size_t& offset,
139  size_t& expected,
140  isc::util::OutputBufferPtr& outbuff);
141 
143  virtual void cancel();
144 
146  virtual void close();
147 
148 
149 private:
150  // Two variables to hold the socket - a socket and a pointer to it. This
151  // handles the case where a socket is passed to the UDPSocket on
152  // construction, or where it is asked to manage its own socket.
153 
155  std::unique_ptr<boost::asio::ip::udp::socket> socket_ptr_;
156 
157  // Socket
158  boost::asio::ip::udp::socket& socket_;
159 
160  // True when socket is open
161  bool isopen_;
162 };
163 
164 // Constructor - caller manages socket
165 
166 template <typename C>
167 UDPSocket<C>::UDPSocket(boost::asio::ip::udp::socket& socket) :
168  socket_ptr_(), socket_(socket), isopen_(true)
169 {
170 }
171 
172 // Constructor - create socket on the fly
173 
174 template <typename C>
176  socket_ptr_(new boost::asio::ip::udp::socket(service.get_io_service())),
177  socket_(*socket_ptr_), isopen_(false)
178 {
179 }
180 
181 // Destructor.
182 
183 template <typename C>
185 {
186 }
187 
188 // Open the socket.
189 
190 template <typename C> void
191 UDPSocket<C>::open(const IOEndpoint* endpoint, C&) {
192 
193  // Ignore opens on already-open socket. (Don't throw a failure because
194  // of uncertainties as to what precedes when using asynchronous I/O.)
195  // It also allows us a treat a passed-in socket in exactly the same way as
196  // a self-managed socket (in that we can call the open() and close() methods
197  // of this class).
198  if (!isopen_) {
199  if (endpoint->getFamily() == AF_INET) {
200  socket_.open(boost::asio::ip::udp::v4());
201  } else {
202  socket_.open(boost::asio::ip::udp::v6());
203  }
204  isopen_ = true;
205 
206  // Ensure it can send and receive at least 4K buffers.
207  boost::asio::ip::udp::socket::send_buffer_size snd_size;
208  socket_.get_option(snd_size);
209  if (snd_size.value() < MIN_SIZE) {
210  snd_size = MIN_SIZE;
211  socket_.set_option(snd_size);
212  }
213 
214  boost::asio::ip::udp::socket::receive_buffer_size rcv_size;
215  socket_.get_option(rcv_size);
216  if (rcv_size.value() < MIN_SIZE) {
217  rcv_size = MIN_SIZE;
218  socket_.set_option(rcv_size);
219  }
220  }
221 }
222 
223 // Send a message. Should never do this if the socket is not open, so throw
224 // an exception if this is the case.
225 
226 template <typename C> void
227 UDPSocket<C>::asyncSend(const void* data, size_t length,
228  const IOEndpoint* endpoint, C& callback)
229 {
230  if (isopen_) {
231 
232  // Upconvert to a UDPEndpoint. We need to do this because although
233  // IOEndpoint is the base class of UDPEndpoint and TCPEndpoint, it
234  // does not contain a method for getting at the underlying endpoint
235  // type - that is in the derived class and the two classes differ on
236  // return type.
237  isc_throw_assert(endpoint->getProtocol() == IPPROTO_UDP);
238  const UDPEndpoint* udp_endpoint =
239  static_cast<const UDPEndpoint*>(endpoint);
240 
241  // ... and send the message.
242  socket_.async_send_to(boost::asio::buffer(data, length),
243  udp_endpoint->getASIOEndpoint(), callback);
244  } else {
246  "attempt to send on a UDP socket that is not open");
247  }
248 }
249 
250 // Receive a message. Should never do this if the socket is not open, so throw
251 // an exception if this is the case.
252 
253 template <typename C> void
254 UDPSocket<C>::asyncReceive(void* data, size_t length, size_t offset,
255  IOEndpoint* endpoint, C& callback)
256 {
257  if (isopen_) {
258 
259  // Upconvert the endpoint again.
260  isc_throw_assert(endpoint->getProtocol() == IPPROTO_UDP);
261  UDPEndpoint* udp_endpoint = static_cast<UDPEndpoint*>(endpoint);
262 
263  // Ensure we can write into the buffer
264  if (offset >= length) {
265  isc_throw(BufferOverflow, "attempt to read into area beyond end of "
266  "UDP receive buffer");
267  }
268  void* buffer_start = static_cast<void*>(static_cast<uint8_t*>(data) + offset);
269 
270  // Issue the read
271  socket_.async_receive_from(boost::asio::buffer(buffer_start, length - offset),
272  udp_endpoint->getASIOEndpoint(), callback);
273  } else {
275  "attempt to receive from a UDP socket that is not open");
276  }
277 }
278 
279 // Receive complete. Just copy the data across to the output buffer and
280 // update arguments as appropriate.
281 
282 template <typename C> bool
283 UDPSocket<C>::processReceivedData(const void* staging, size_t length,
284  size_t& cumulative, size_t& offset,
285  size_t& expected,
287 {
288  // Set return values to what we should expect.
289  cumulative = length;
290  expected = length;
291  offset = 0;
292 
293  // Copy data across
294  outbuff->writeData(staging, length);
295 
296  // ... and mark that we have everything.
297  return (true);
298 }
299 
300 // Cancel I/O on the socket. No-op if the socket is not open.
301 
302 template <typename C> void
304  if (isopen_) {
305  socket_.cancel();
306  }
307 }
308 
309 // Close the socket down. Can only do this if the socket is open and we are
310 // managing it ourself.
311 
312 template <typename C> void
314  if (isopen_ && socket_ptr_) {
315  socket_.close();
316  isopen_ = false;
317  }
318 }
319 
320 } // namespace asiolink
321 } // namespace isc
322 
323 #endif // UDP_SOCKET_H
#define isc_throw_assert(expr)
Replacement for assert() that throws if the expression is false.
Definition: isc_assert.h:18
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the logger used by the top-level component of kea-lfc.
boost::shared_ptr< OutputBuffer > OutputBufferPtr
Definition: buffer.h:603