Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
WakeOnLan.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <string.h>
4#include "WakeOnLan.h"
5
6#ifdef WIN32
7 #include <winsock2.h>
8 typedef unsigned long in_addr_t;
9#else
10 #include <arpa/inet.h>
11#endif
12
13CWakeOnLan::CWakeOnLan(const std::string &szBroadcastAddress) {
14 SetBroadcastAddress(szBroadcastAddress);
15}
16
17void CWakeOnLan::SetBroadcastAddress(const std::string &szBroadcastAddress) {
18 m_szBroadcastAddress = szBroadcastAddress;
19}
20
21std::string CWakeOnLan::SetBroadcastAddress(
22 const std::string &szIP, const std::string &szMask) {
23 in_addr_t ip = inet_addr(szIP.c_str());
24 in_addr_t mask = inet_addr(szMask.c_str());
25 in_addr_t broadcast = 0;
26 char* pIP = (char*)&ip;
27 char* pMask = (char*)&mask;
28 char* pBroadcast = (char*)&broadcast;
29 for (int i = 0; i < 4; i++)
30 pBroadcast[i] = ~ pMask[i] | pIP[i];
31 std::string szBroadcast = inet_ntoa((struct in_addr&)broadcast);
32 if(!szBroadcast.empty())
33 SetBroadcastAddress(szBroadcast);
34 return szBroadcast;
35}
36
37bool CWakeOnLan::StringToArray(
38 /*[out]*/uint8_t* mac, /*[in]*/const char* szMac) {
39 char macFormat[23 + 1];
40 unsigned int tempMACAddress[6];
41
42 if (strlen(szMac) == 12) // FFFFFFFFFFFF
43 sprintf(macFormat, "%%2x%%2x%%2x%%2x%%2x%%2x");
44 else if (strlen(szMac) == 14) // FFFF-FFFF-FFFF
45 sprintf(macFormat, "%%2x%%2x%c%%2x%%2x%c%%2x%%2x", szMac[4], szMac[9]);
46 else if (strlen(szMac) == 17) // FF-FF-FF-FF-FF-FF
47 sprintf(macFormat, "%%2x%c%%2x%c%%2x%c%%2x%c%%2x%c%%2x",
48 szMac[2], szMac[5], szMac[8], szMac[11], szMac[14]);
49 else
50 return false;
51
52 int j = sscanf(szMac, (const char*)macFormat, &tempMACAddress[0],
53 &tempMACAddress[1], &tempMACAddress[2], &tempMACAddress[3],
54 &tempMACAddress[4], &tempMACAddress[5]);
55 if (j == 6) {
56 for (uint8_t i = 0;
57 i < sizeof(tempMACAddress) / sizeof(*tempMACAddress); i++)
58 mac[i] = (uint8_t)tempMACAddress[i];
59
60 return true;
61 }
62
63 return false;
64}
65
66bool CWakeOnLan::SendMagicPacket(const std::string &szMac, uint16_t portNum) {
67 return SendMagicPacket(szMac.c_str(), portNum);
68}
69
71 const std::string &szMac,
72 const std::string szPassword,
73 uint16_t portNum) {
74 return SendSecureMagicPacket(szMac.c_str(), szPassword.c_str(), portNum);
75}
76
77bool CWakeOnLan::SendMagicPacket(const char* pszMacAddress, uint16_t portNum) {
78 uint8_t macAddress[6];
79
80 bool res = StringToArray(macAddress, pszMacAddress);
81 if (!res)
82 return false;
83
84 return SendMagicPacket(macAddress, sizeof(macAddress), portNum);
85}
86
88 const char* pszMacAddress,
89 const char* pszPassword,
90 uint16_t portNum) {
91
92 uint8_t macAddress[6];
93 uint8_t secureOn[6];
94
95 bool res = StringToArray(macAddress, pszMacAddress);
96 if (!res)
97 return false;
98
99 bool res2 = StringToArray(secureOn, pszPassword);
100 if (!res2)
101 return false;
102
103 return SendSecureMagicPacket(macAddress, sizeof(macAddress),
104 secureOn, sizeof(secureOn), portNum);
105}
106
107bool CWakeOnLan::SendMagicPacket(uint8_t* pMacAddress,
108 size_t sizeOfMacAddress, uint16_t portNum) {
109 size_t magicPacketSize = 6 + (6 * 16); // FF*6 + MAC*16
110 uint8_t* magicPacket = new uint8_t[magicPacketSize];
111
112 GenerateMagicPacket(magicPacket, magicPacketSize,
113 pMacAddress, sizeOfMacAddress);
114
115 SendPacket((const char*)magicPacket, magicPacketSize,
116 m_szBroadcastAddress, portNum);
117
118 delete[] magicPacket;
119
120 return true;
121}
122
123bool CWakeOnLan::SendSecureMagicPacket(uint8_t* pMacAddress,
124 size_t sizeOfMacAddress,
125 uint8_t* pszPassword,
126 size_t nPasswordSize,
127 uint16_t portNum) {
128 size_t magicPacketSize = 6 + (6 * 16) + 6; // FF*6 + MAC*16 + SecureOn
129 uint8_t* magicPacket = new uint8_t[magicPacketSize];
130
131 GenerateSecureMagicPacket(magicPacket, magicPacketSize, pMacAddress,
132 sizeOfMacAddress, pszPassword, nPasswordSize);
133
134 SendPacket((const char*)magicPacket, magicPacketSize,
135 m_szBroadcastAddress, portNum);
136
137 delete[] magicPacket;
138
139 return true;
140}
141
142void CWakeOnLan::GenerateMagicPacket(uint8_t*& pMagicPacket,
143 size_t& sizeOfMagicPacket,
144 uint8_t* pMacAddress,
145 size_t sizeOfMacAddress) {
146 uint8_t macAddress[6];
147
148 memcpy(macAddress, pMacAddress, sizeOfMacAddress);
149 memset(pMagicPacket, 0xFF, 6);
150
151 for (uint8_t i = 0; i < 16; i++) {
152 uint8_t indx = (i + 1) * sizeOfMacAddress;
153 memcpy(&pMagicPacket[indx], &macAddress, sizeOfMacAddress);
154 }
155}
156
157void CWakeOnLan::GenerateSecureMagicPacket(uint8_t*& pMagicPacket,
158 size_t& sizeOfMagicPacket,
159 uint8_t* pMacAddress,
160 size_t sizeOfMacAddress,
161 uint8_t* pSecureOn,
162 size_t sizeOfSecureOn) {
163 uint8_t macAddress[6];
164 uint8_t secureOn[6];
165
166 memcpy(macAddress, pMacAddress, sizeOfMacAddress);
167 memcpy(secureOn, pSecureOn, sizeOfSecureOn);
168
169 memset(pMagicPacket, 0xFF, 6);
170
171 for (uint8_t i = 0; i < 16; i++) {
172 uint8_t indx = (i + 1) * sizeOfMacAddress;
173 memcpy(&pMagicPacket[indx], &macAddress, sizeOfMacAddress);
174 }
175
176 memcpy(&pMagicPacket[17 * sizeOfSecureOn], &secureOn, sizeOfSecureOn);
177}
bool SendSecureMagicPacket(const std::string &szMac, const std::string szPassword, uint16_t portNum=9)
SendSecureMagicPacket.
Definition WakeOnLan.cpp:70
bool SendMagicPacket(const std::string &szMac, uint16_t portNum=9)
SendMagicPacket.
Definition WakeOnLan.cpp:66