8 typedef unsigned long in_addr_t;
10 #include <arpa/inet.h>
13CWakeOnLan::CWakeOnLan(
const std::string &szBroadcastAddress) {
14 SetBroadcastAddress(szBroadcastAddress);
17void CWakeOnLan::SetBroadcastAddress(
const std::string &szBroadcastAddress) {
18 m_szBroadcastAddress = szBroadcastAddress;
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);
37bool CWakeOnLan::StringToArray(
38 uint8_t* mac,
const char* szMac) {
39 char macFormat[23 + 1];
40 unsigned int tempMACAddress[6];
42 if (strlen(szMac) == 12)
43 sprintf(macFormat,
"%%2x%%2x%%2x%%2x%%2x%%2x");
44 else if (strlen(szMac) == 14)
45 sprintf(macFormat,
"%%2x%%2x%c%%2x%%2x%c%%2x%%2x", szMac[4], szMac[9]);
46 else if (strlen(szMac) == 17)
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]);
52 int j = sscanf(szMac, (
const char*)macFormat, &tempMACAddress[0],
53 &tempMACAddress[1], &tempMACAddress[2], &tempMACAddress[3],
54 &tempMACAddress[4], &tempMACAddress[5]);
57 i <
sizeof(tempMACAddress) /
sizeof(*tempMACAddress); i++)
58 mac[i] = (uint8_t)tempMACAddress[i];
71 const std::string &szMac,
72 const std::string szPassword,
78 uint8_t macAddress[6];
80 bool res = StringToArray(macAddress, pszMacAddress);
88 const char* pszMacAddress,
89 const char* pszPassword,
92 uint8_t macAddress[6];
95 bool res = StringToArray(macAddress, pszMacAddress);
99 bool res2 = StringToArray(secureOn, pszPassword);
104 secureOn,
sizeof(secureOn), portNum);
108 size_t sizeOfMacAddress, uint16_t portNum) {
109 size_t magicPacketSize = 6 + (6 * 16);
110 uint8_t* magicPacket =
new uint8_t[magicPacketSize];
112 GenerateMagicPacket(magicPacket, magicPacketSize,
113 pMacAddress, sizeOfMacAddress);
115 SendPacket((
const char*)magicPacket, magicPacketSize,
116 m_szBroadcastAddress, portNum);
118 delete[] magicPacket;
124 size_t sizeOfMacAddress,
125 uint8_t* pszPassword,
126 size_t nPasswordSize,
128 size_t magicPacketSize = 6 + (6 * 16) + 6;
129 uint8_t* magicPacket =
new uint8_t[magicPacketSize];
131 GenerateSecureMagicPacket(magicPacket, magicPacketSize, pMacAddress,
132 sizeOfMacAddress, pszPassword, nPasswordSize);
134 SendPacket((
const char*)magicPacket, magicPacketSize,
135 m_szBroadcastAddress, portNum);
137 delete[] magicPacket;
142void CWakeOnLan::GenerateMagicPacket(uint8_t*& pMagicPacket,
143 size_t& sizeOfMagicPacket,
144 uint8_t* pMacAddress,
145 size_t sizeOfMacAddress) {
146 uint8_t macAddress[6];
148 memcpy(macAddress, pMacAddress, sizeOfMacAddress);
149 memset(pMagicPacket, 0xFF, 6);
151 for (uint8_t i = 0; i < 16; i++) {
152 uint8_t indx = (i + 1) * sizeOfMacAddress;
153 memcpy(&pMagicPacket[indx], &macAddress, sizeOfMacAddress);
157void CWakeOnLan::GenerateSecureMagicPacket(uint8_t*& pMagicPacket,
158 size_t& sizeOfMagicPacket,
159 uint8_t* pMacAddress,
160 size_t sizeOfMacAddress,
162 size_t sizeOfSecureOn) {
163 uint8_t macAddress[6];
166 memcpy(macAddress, pMacAddress, sizeOfMacAddress);
167 memcpy(secureOn, pSecureOn, sizeOfSecureOn);
169 memset(pMagicPacket, 0xFF, 6);
171 for (uint8_t i = 0; i < 16; i++) {
172 uint8_t indx = (i + 1) * sizeOfMacAddress;
173 memcpy(&pMagicPacket[indx], &macAddress, sizeOfMacAddress);
176 memcpy(&pMagicPacket[17 * sizeOfSecureOn], &secureOn, sizeOfSecureOn);
bool SendSecureMagicPacket(const std::string &szMac, const std::string szPassword, uint16_t portNum=9)
SendSecureMagicPacket.
bool SendMagicPacket(const std::string &szMac, uint16_t portNum=9)
SendMagicPacket.