Kea 3.3.1
udp_socket.h
Go to the documentation of this file.
1// Copyright (C) 2011-2026 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 return (socket_.native_handle());
67 }
68
70 virtual int getProtocol() const {
71 return (IPPROTO_UDP);
72 }
73
77 virtual bool isOpenSynchronous() const {
78 return (true);
79 }
80
89 virtual void open(const IOEndpoint* endpoint, C& callback);
90
104 virtual void asyncSend(const void* data, size_t length,
105 const IOEndpoint* endpoint, C& callback);
106
118 virtual void asyncReceive(void* data, size_t length, size_t offset,
119 IOEndpoint* endpoint, C& callback);
120
136 virtual bool processReceivedData(const void* staging, size_t length,
137 size_t& cumulative, size_t& offset,
138 size_t& expected,
140
142 virtual void cancel();
143
145 virtual void close();
146
147
148private:
150 IOServicePtr io_service_;
151
152 // Two variables to hold the socket - a socket and a pointer to it. This
153 // handles the case where a socket is passed to the UDPSocket on
154 // construction, or where it is asked to manage its own socket.
155
157 std::unique_ptr<boost::asio::ip::udp::socket> socket_ptr_;
158
159 // Socket
160 boost::asio::ip::udp::socket& socket_;
161
162 // True when socket is open
163 bool isopen_;
164};
165
166// Constructor - caller manages socket
167
168template <typename C>
169UDPSocket<C>::UDPSocket(boost::asio::ip::udp::socket& socket) :
170 socket_ptr_(), socket_(socket), isopen_(true) {
171}
172
173// Constructor - create socket on the fly
174
175template <typename C>
176UDPSocket<C>::UDPSocket(const IOServicePtr& io_service) : io_service_(io_service),
177 socket_ptr_(new boost::asio::ip::udp::socket(io_service_->getInternalIOService())),
178 socket_(*socket_ptr_), isopen_(false) {
179}
180
181// Destructor.
182
183template <typename C>
187
188// Open the socket.
189
190template <typename C> void
191UDPSocket<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
226template <typename C> void
227UDPSocket<C>::asyncSend(const void* data, size_t length,
228 const IOEndpoint* endpoint, C& callback) {
229 if (isopen_) {
230
231 // Upconvert to a UDPEndpoint. We need to do this because although
232 // IOEndpoint is the base class of UDPEndpoint and TCPEndpoint, it
233 // does not contain a method for getting at the underlying endpoint
234 // type - that is in the derived class and the two classes differ on
235 // return type.
236 isc_throw_assert(endpoint->getProtocol() == IPPROTO_UDP);
237 const UDPEndpoint* udp_endpoint =
238 static_cast<const UDPEndpoint*>(endpoint);
239
240 // ... and send the message.
241 socket_.async_send_to(boost::asio::buffer(data, length),
242 udp_endpoint->getASIOEndpoint(), callback);
243 } else {
245 "attempt to send on a UDP socket that is not open");
246 }
247}
248
249// Receive a message. Should never do this if the socket is not open, so throw
250// an exception if this is the case.
251
252template <typename C> void
253UDPSocket<C>::asyncReceive(void* data, size_t length, size_t offset,
254 IOEndpoint* endpoint, C& callback) {
255 if (isopen_) {
256
257 // Upconvert the endpoint again.
258 isc_throw_assert(endpoint->getProtocol() == IPPROTO_UDP);
259 UDPEndpoint* udp_endpoint = static_cast<UDPEndpoint*>(endpoint);
260
261 // Ensure we can write into the buffer
262 if (offset >= length) {
263 isc_throw(BufferOverflow, "attempt to read into area beyond end of "
264 "UDP receive buffer");
265 }
266 void* buffer_start = static_cast<void*>(static_cast<uint8_t*>(data) + offset);
267
268 // Issue the read
269 socket_.async_receive_from(boost::asio::buffer(buffer_start, length - offset),
270 udp_endpoint->getASIOEndpoint(), callback);
271 } else {
273 "attempt to receive from a UDP socket that is not open");
274 }
275}
276
277// Receive complete. Just copy the data across to the output buffer and
278// update arguments as appropriate.
279
280template <typename C> bool
281UDPSocket<C>::processReceivedData(const void* staging, size_t length,
282 size_t& cumulative, size_t& offset,
283 size_t& expected,
285 // Set return values to what we should expect.
286 cumulative = length;
287 expected = length;
288 offset = 0;
289
290 // Copy data across
291 outbuff->writeData(staging, length);
292
293 // ... and mark that we have everything.
294 return (true);
295}
296
297// Cancel I/O on the socket. No-op if the socket is not open.
298
299template <typename C> void
301 if (isopen_) {
302 socket_.cancel();
303 }
304}
305
306// Close the socket down. Can only do this if the socket is open and we are
307// managing it ourself.
308
309template <typename C> void
311 if (isopen_ && socket_ptr_) {
312 socket_.close();
313 isopen_ = false;
314 }
315}
316
317} // namespace asiolink
318} // namespace isc
319
320#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:583
Defines the logger used by the top-level component of kea-lfc.