Kea 3.3.4
pkt_filter.cc
Go to the documentation of this file.
1// Copyright (C) 2013-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#include <config.h>
8#include <dhcp/iface_mgr.h>
9#include <dhcp/pkt_filter.h>
10
11#include <sys/socket.h>
12#include <fcntl.h>
13
14namespace isc {
15namespace dhcp {
16
17bool
19
20int
22 const uint16_t port) {
23 // Create socket.
24 int sock = socket(AF_INET, SOCK_DGRAM, 0);
25 if (sock < 0) {
26 isc_throw(SocketConfigError, "failed to create fallback socket for"
27 " address " << addr << ", port " << port
28 << ", reason: " << strerror(errno));
29 }
30 // Set the close-on-exec flag.
31 if (fcntl(sock, F_SETFD, FD_CLOEXEC) < 0) {
32 close(sock);
33 isc_throw(SocketConfigError, "Failed to set close-on-exec flag"
34 << " on fallback socket for address " << addr
35 << ", port " << port
36 << ", reason: " << strerror(errno));
37 }
38 // Bind the socket to a specified address and port.
39 struct sockaddr_in addr4;
40 memset(&addr4, 0, sizeof(addr4));
41 addr4.sin_family = AF_INET;
42 addr4.sin_addr.s_addr = htonl(addr.toUint32());
43 addr4.sin_port = htons(port);
44
45 if (bind(sock, reinterpret_cast<struct sockaddr*>(&addr4),
46 sizeof(addr4)) < 0) {
47 // Get the error message immediately after the bind because the
48 // invocation to close() below would override the errno.
49 char* errmsg = strerror(errno);
50 // Remember to close the socket if we failed to bind it.
51 close(sock);
52 isc_throw(SocketConfigError, "failed to bind fallback socket to"
53 " address " << addr << ", port " << port
54 << ", reason: " << errmsg
55 << " - is another DHCP server running?");
56 }
57
58 // Set socket to non-blocking mode. This is to prevent the read from the
59 // fallback socket to block message processing on the primary socket.
60 if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0) {
61 // Get the error message immediately after the bind because the
62 // invocation to close() below would override the errno.
63 char* errmsg = strerror(errno);
64 close(sock);
65 isc_throw(SocketConfigError, "failed to set SO_NONBLOCK option on the"
66 " fallback socket, bound to " << addr << ", port "
67 << port << ", reason: " << errmsg);
68 }
69 // Successfully created and bound a fallback socket. Return a descriptor.
70 return (sock);
71}
72
73} // end of isc::dhcp namespace
74} // end of isc namespace
virtual int openFallbackSocket(const isc::asiolink::IOAddress &addr, const uint16_t port)
Default implementation to open a fallback socket.
Definition pkt_filter.cc:21
static bool use_fallbacks_
Use fallbacks.
Definition pkt_filter.h:117
IfaceMgr exception thrown thrown when socket opening or configuration failed.
Definition iface_mgr.h:66
#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.