|
@@ -0,0 +1,84 @@
|
|
|
+// Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
|
|
|
+//
|
|
|
+// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
+// purpose with or without fee is hereby granted, provided that the above
|
|
|
+// copyright notice and this permission notice appear in all copies.
|
|
|
+//
|
|
|
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
+// PERFORMANCE OF THIS SOFTWARE.
|
|
|
+
|
|
|
+#include <config.h>
|
|
|
+#include <dhcp/iface_mgr.h>
|
|
|
+#include <dhcp/pkt4.h>
|
|
|
+#include <dhcp/pkt_filter_inet.h>
|
|
|
+
|
|
|
+namespace isc {
|
|
|
+namespace dhcp {
|
|
|
+
|
|
|
+int
|
|
|
+PktFilterInet::openSocket(const std::string& ifname, const isc::asiolink::IOAddress& addr,
|
|
|
+ const uint16_t port, const bool receive_bcast,
|
|
|
+ const bool send_bcast) {
|
|
|
+
|
|
|
+ struct sockaddr_in addr4;
|
|
|
+ memset(&addr4, 0, sizeof(sockaddr));
|
|
|
+ addr4.sin_family = AF_INET;
|
|
|
+ addr4.sin_port = htons(port);
|
|
|
+
|
|
|
+ // If we are to receive broadcast messages we have to bind
|
|
|
+ // to "ANY" address.
|
|
|
+ addr4.sin_addr.s_addr = receive_bcast ? INADDR_ANY : htonl(addr);
|
|
|
+
|
|
|
+ int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
+ if (sock < 0) {
|
|
|
+ isc_throw(SocketConfigError, "Failed to create UDP6 socket.");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (receive_bcast) {
|
|
|
+ // Bind to device so as we receive traffic on a specific interface.
|
|
|
+ if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname.c_str(),
|
|
|
+ ifname.length() + 1) < 0) {
|
|
|
+ close(sock);
|
|
|
+ isc_throw(SocketConfigError, "Failed to set SO_BINDTODEVICE option"
|
|
|
+ << "on socket " << sock);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (send_bcast) {
|
|
|
+ // Enable sending to broadcast address.
|
|
|
+ int flag = 1;
|
|
|
+ if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &flag, sizeof(flag)) < 0) {
|
|
|
+ close(sock);
|
|
|
+ isc_throw(SocketConfigError, "Failed to set SO_BINDTODEVICE option"
|
|
|
+ << "on socket " << sock);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (bind(sock, (struct sockaddr *)&addr4, sizeof(addr4)) < 0) {
|
|
|
+ close(sock);
|
|
|
+ isc_throw(SocketConfigError, "Failed to bind socket " << sock << " to " << addr.toText()
|
|
|
+ << "/port=" << port);
|
|
|
+ }
|
|
|
+
|
|
|
+ // if there is no support for IP_PKTINFO, we are really out of luck
|
|
|
+ // it will be difficult to undersand, where this packet came from
|
|
|
+#if defined(IP_PKTINFO)
|
|
|
+ int flag = 1;
|
|
|
+ if (setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &flag, sizeof(flag)) != 0) {
|
|
|
+ close(sock);
|
|
|
+ isc_throw(SocketConfigError, "setsockopt: IP_PKTINFO: failed.");
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+ return sock;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+} // end of isc::dhcp namespace
|
|
|
+} // end of isc namespace
|