Kea 2.5.8
udp_socket.h
Go to the documentation of this file.
1// Copyright (C) 2011-2024 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
22#include <asiolink/io_service.h>
24
26
27namespace isc {
28namespace asiolink {
29
34template <typename C>
35class UDPSocket : public IOAsioSocket<C> {
36private:
38 UDPSocket(const UDPSocket&);
39 UDPSocket& operator=(const UDPSocket&);
40
41public:
42 enum {
43 MIN_SIZE = 4096 // Minimum send and receive size
44 };
45
51 UDPSocket(boost::asio::ip::udp::socket& socket);
52
59 UDPSocket(const IOServicePtr& 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,
141
143 virtual void cancel();
144
146 virtual void close();
147
148
149private:
151 IOServicePtr io_service_;
152
153 // Two variables to hold the socket - a socket and a pointer to it. This
154 // handles the case where a socket is passed to the UDPSocket on
155 // construction, or where it is asked to manage its own socket.
156
158 std::unique_ptr<boost::asio::ip::udp::socket> socket_ptr_;
159
160 // Socket
161 boost::asio::ip::udp::socket& socket_;
162
163 // True when socket is open
164 bool isopen_;
165};
166
167// Constructor - caller manages socket
168
169template <typename C>
170UDPSocket<C>::UDPSocket(boost::asio::ip::udp::socket& socket) :
171 socket_ptr_(), socket_(socket), isopen_(true) {
172}
173
174// Constructor - create socket on the fly
175
176template <typename C>
177UDPSocket<C>::UDPSocket(const IOServicePtr& io_service) : io_service_(io_service),
178 socket_ptr_(new boost::asio::ip::udp::socket(io_service_->getInternalIOService())),
179 socket_(*socket_ptr_), isopen_(false) {
180}
181
182// Destructor.
183
184template <typename C>
186 close();
187}
188
189// Open the socket.
190
191template <typename C> void
192UDPSocket<C>::open(const IOEndpoint* endpoint, C&) {
193
194 // Ignore opens on already-open socket. (Don't throw a failure because
195 // of uncertainties as to what precedes when using asynchronous I/O.)
196 // It also allows us a treat a passed-in socket in exactly the same way as
197 // a self-managed socket (in that we can call the open() and close() methods
198 // of this class).
199 if (!isopen_) {
200 if (endpoint->getFamily() == AF_INET) {
201 socket_.open(boost::asio::ip::udp::v4());
202 } else {
203 socket_.open(boost::asio::ip::udp::v6());
204 }
205 isopen_ = true;
206
207 // Ensure it can send and receive at least 4K buffers.
208 boost::asio::ip::udp::socket::send_buffer_size snd_size;
209 socket_.get_option(snd_size);
210 if (snd_size.value() < MIN_SIZE) {
211 snd_size = MIN_SIZE;
212 socket_.set_option(snd_size);
213 }
214
215 boost::asio::ip::udp::socket::receive_buffer_size rcv_size;
216 socket_.get_option(rcv_size);
217 if (rcv_size.value() < MIN_SIZE) {
218 rcv_size = MIN_SIZE;
219 socket_.set_option(rcv_size);
220 }
221 }
222}
223
224// Send a message. Should never do this if the socket is not open, so throw
225// an exception if this is the case.
226
227template <typename C> void
228UDPSocket<C>::asyncSend(const void* data, size_t length,
229 const IOEndpoint* endpoint, C& callback) {
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
253template <typename C> void
254UDPSocket<C>::asyncReceive(void* data, size_t length, size_t offset,
255 IOEndpoint* endpoint, C& callback) {
256 if (isopen_) {
257
258 // Upconvert the endpoint again.
259 isc_throw_assert(endpoint->getProtocol() == IPPROTO_UDP);
260 UDPEndpoint* udp_endpoint = static_cast<UDPEndpoint*>(endpoint);
261
262 // Ensure we can write into the buffer
263 if (offset >= length) {
264 isc_throw(BufferOverflow, "attempt to read into area beyond end of "
265 "UDP receive buffer");
266 }
267 void* buffer_start = static_cast<void*>(static_cast<uint8_t*>(data) + offset);
268
269 // Issue the read
270 socket_.async_receive_from(boost::asio::buffer(buffer_start, length - offset),
271 udp_endpoint->getASIOEndpoint(), callback);
272 } else {
274 "attempt to receive from a UDP socket that is not open");
275 }
276}
277
278// Receive complete. Just copy the data across to the output buffer and
279// update arguments as appropriate.
280
281template <typename C> bool
282UDPSocket<C>::processReceivedData(const void* staging, size_t length,
283 size_t& cumulative, size_t& offset,
284 size_t& expected,
286 // Set return values to what we should expect.
287 cumulative = length;
288 expected = length;
289 offset = 0;
290
291 // Copy data across
292 outbuff->writeData(staging, length);
293
294 // ... and mark that we have everything.
295 return (true);
296}
297
298// Cancel I/O on the socket. No-op if the socket is not open.
299
300template <typename C> void
302 if (isopen_) {
303 socket_.cancel();
304 }
305}
306
307// Close the socket down. Can only do this if the socket is open and we are
308// managing it ourself.
309
310template <typename C> void
312 if (isopen_ && socket_ptr_) {
313 socket_.close();
314 isopen_ = false;
315 }
316}
317
318} // namespace asiolink
319} // namespace isc
320
321#endif // UDP_SOCKET_H
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define isc_throw_assert(expr)
Replacement for assert() that throws if the expression is false.
Definition: isc_assert.h:18
boost::shared_ptr< OutputBuffer > OutputBufferPtr
Type of pointers to output buffers.
Definition: buffer.h:571
Defines the logger used by the top-level component of kea-lfc.