wol.cc
Go to the documentation of this file.
1 /*
2  * rcdiscover - the network discovery tool for Roboception devices
3  *
4  * Copyright (c) 2017 Roboception GmbH
5  * All rights reserved
6  *
7  * Author: Raphael Schaller
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include "wol.h"
37 
38 #ifdef WIN32
39 #include "socket_windows.h"
40 #else
41 #include "socket_linux.h"
42 #endif
43 
44 #include "socket_exception.h"
45 
46 namespace rcdiscover
47 {
48 
49 WOL::WOL(uint64_t hardware_addr, uint16_t port) noexcept :
50  hardware_addr_(toByteArray<6>(std::move(hardware_addr))),
51  port_{port}
52 { }
53 
54 WOL::WOL(std::array<uint8_t, 6> hardware_addr, uint16_t port) noexcept :
55  hardware_addr_(std::move(hardware_addr)),
56  port_{port}
57 { }
58 
59 void WOL::send() const
60 {
61  sendImpl(nullptr);
62 }
63 
64 void WOL::send(const std::array<uint8_t, 4>& password) const
65 {
66  sendImpl(&password);
67 }
68 
69 std::vector<uint8_t>& WOL::appendMagicPacket(
70  std::vector<uint8_t>& sendbuf,
71  const std::array<uint8_t, 4> *password) const
72 {
73  for (int i = 0; i < 6; ++i)
74  {
75  sendbuf.push_back(0xFF);
76  }
77  for (int i = 0; i < 16; ++i)
78  {
79  for (size_t j = 0; j < hardware_addr_.size(); ++j)
80  {
81  sendbuf.push_back(hardware_addr_[j]);
82  }
83  }
84  if (password != nullptr)
85  {
86  for (int i = 0; i < 4; ++i)
87  {
88  sendbuf.push_back((*password)[i]);
89  }
90  }
91 
92  return sendbuf;
93 }
94 
95 template<uint8_t num>
96 std::array<uint8_t, num> WOL::toByteArray(uint64_t data) noexcept
97 {
98  std::array<uint8_t, num> result;
99  for (uint8_t i = 0; i < num; ++i)
100  {
101  result[i] = static_cast<uint8_t>((data >> (((num - 1 - i)*8))) & 0xFF);
102  }
103  return result;
104 }
105 
106 void WOL::sendImpl(const std::array<uint8_t, 4> *password) const
107 {
109 
110  for (auto &socket : sockets)
111  {
112  std::vector<uint8_t> sendbuf;
113  appendMagicPacket(sendbuf, password);
114 
115  socket.enableBroadcast();
116  socket.enableNonBlocking();
117 
118  try
119  {
120  socket.send(sendbuf);
121  }
122  catch(const NetworkUnreachableException &)
123  {
124  continue;
125  }
126  }
127 }
128 
129 }
static std::vector< SocketLinux > createAndBindForAllInterfaces(uint16_t port)
Creates sockets for all interfaces and binds them to the respective interface.
Definition: socket_linux.cc:76
Exception representing a Network Unreachable error (code 101 on Unix).
std::vector< uint8_t > & appendMagicPacket(std::vector< uint8_t > &sendbuf, const std::array< uint8_t, 4 > *password) const
Appends a magic packet to a data buffer.
Definition: wol.cc:69
WOL(uint64_t hardware_addr, uint16_t port) noexcept
Constructor.
Definition: wol.cc:49
std::array< uint8_t, num > toByteArray(uint64_t data) noexcept
Converts a larger-than-byte data type to an array of bytes.
Definition: wol.cc:96
uint16_t port_
Definition: wol.h:125
void sendImpl(const std::array< uint8_t, 4 > *password) const
sendImpl Actually send Magic packet with specified data.
Definition: wol.cc:106
const std::array< uint8_t, 6 > hardware_addr_
Definition: wol.h:124
void send() const
Send Magic Packet without any data ("password").
Definition: wol.cc:59


rcdiscover
Author(s): Heiko Hirschmueller , Raphael Schaller
autogenerated on Sun Apr 18 2021 02:16:32