Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wake on Pattern Match対応 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 123 additions & 40 deletions BonDriver_Proxy/BonDriver_Proxy.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
#define _CRT_SECURE_NO_WARNINGS
#include "BonDriver_Proxy.h"
#include <assert.h>

#if _DEBUG
#define DETAILLOG 0
#endif

typedef enum enumWakeupMode {
MagicPacket = 0,
PatternMatch,
} WakeupMode;
static BOOL WakeupRemoteComputer(const char* host, WakeupMode mode, UINT repeat = 1);

static int Init(HMODULE hModule)
{
char szIniPath[MAX_PATH + 16] = { '\0' };
Expand All @@ -26,7 +33,8 @@ static int Init(HMODULE hModule)
g_ChannelLock = (BYTE)GetPrivateProfileIntA("OPTION", "CHANNEL_LOCK", 0, szIniPath);

g_ConnectTimeOut = GetPrivateProfileIntA("OPTION", "CONNECT_TIMEOUT", 5, szIniPath);
g_UseMagicPacket = (BOOL)GetPrivateProfileIntA("OPTION", "USE_MAGICPACKET", 0, szIniPath);
g_UseMagicPacket = (UINT)min(GetPrivateProfileIntA("OPTION", "USE_MAGICPACKET", 0, szIniPath), 100);
g_UsePatternMatch = (UINT)min(GetPrivateProfileIntA("OPTION", "USE_PATTERNMATCH", 0, szIniPath), 100);
if (g_UseMagicPacket)
{
char mac[32];
Expand Down Expand Up @@ -781,47 +789,12 @@ static SOCKET Connect(char *host, char *port)
timeval tv;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
if (g_UseMagicPacket)
{
char sendbuf[128];
memset(sendbuf, 0xff, 6);
for (i = 1; i <= 16; i++)
memcpy(&sendbuf[i * 6], g_TargetMac, 6);

hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_NUMERICHOST;
if (getaddrinfo(g_TargetHost, g_TargetPort, &hints, &results) != 0)
{
hints.ai_flags = 0;
if (getaddrinfo(g_TargetHost, g_TargetPort, &hints, &results) != 0)
return INVALID_SOCKET;
}

for (rp = results; rp != NULL; rp = rp->ai_next)
{
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sock == INVALID_SOCKET)
continue;

BOOL opt = TRUE;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const char *)&opt, sizeof(opt)) != 0)
{
closesocket(sock);
continue;
}

int ret = sendto(sock, sendbuf, 102, 0, rp->ai_addr, (int)(rp->ai_addrlen));
closesocket(sock);
if (ret == 102)
break;
}
freeaddrinfo(results);
if (rp == NULL)
return INVALID_SOCKET;
}
WakeupRemoteComputer(g_TargetHost, MagicPacket, g_UseMagicPacket);
if (g_UsePatternMatch)
WakeupRemoteComputer(host, PatternMatch, g_UsePatternMatch);

hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_NUMERICHOST;
Expand Down Expand Up @@ -962,3 +935,113 @@ BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID/*lpvReserved*/
}
return TRUE;
}

static BOOL WakeupRemoteComputer(const char* host, WakeupMode mode, UINT repeat/*=1*/)
{
addrinfo hints, *results, *rp;
SOCKET sock;
int i;
BOOL result = FALSE;
unsigned long bf;

rp = NULL;
memset(&hints, 0, sizeof(hints));
try
{
switch (mode)
{
case MagicPacket:
char sendbuf[128];
memset(sendbuf, 0xff, 6);
for (i = 1; i <= 16; i++)
memcpy(&sendbuf[i * 6], g_TargetMac, 6);

hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_V4MAPPED | AI_NUMERICHOST;
if (getaddrinfo(g_TargetHost, g_TargetPort, &hints, &results) != 0)
{
hints.ai_flags = AI_V4MAPPED;
if (getaddrinfo(g_TargetHost, g_TargetPort, &hints, &results) != 0)
throw __LINE__;
}

for (UINT j = 1; j <= repeat; j++)
{
for (rp = results; rp != NULL; rp = rp->ai_next)
{
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sock == INVALID_SOCKET)
continue;

BOOL opt = TRUE;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const char *)&opt, sizeof(opt)) != 0)
{
closesocket(sock);
continue;
}

int ret = sendto(sock, sendbuf, 102, 0, rp->ai_addr, (int)(rp->ai_addrlen));
closesocket(sock);
if (ret == 102)
break;
}
if (j < repeat)
::Sleep(10);
}
break;

case PatternMatch:
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_NUMERICHOST;

sock = INVALID_SOCKET;
if (getaddrinfo(host, "445", &hints, &results) != 0)
{
hints.ai_flags = 0;
if (getaddrinfo(host, "445", &hints, &results) != 0)
throw __LINE__;
}

for (UINT j = 1; j <= repeat; j++)
{
for (rp = results; rp != NULL; rp = rp->ai_next)
{
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sock == INVALID_SOCKET)
continue;
if (connect(sock, rp->ai_addr, (int)(rp->ai_addrlen)) == SOCKET_ERROR)
continue;
if (shutdown(sock, SD_BOTH) == SOCKET_ERROR)
continue;
if (sock != INVALID_SOCKET)
closesocket(sock);
}
if (j < repeat)
::Sleep(10);
}
break;

default:
assert(FALSE);
throw __LINE__;
}

freeaddrinfo(results);
if (rp == NULL)
throw __LINE__;

result = TRUE;
}
catch (const int &e) {
e;
#if _DEBUG
#endif
result = FALSE;
}
return result;
}

3 changes: 2 additions & 1 deletion BonDriver_Proxy/BonDriver_Proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ static size_t g_PacketFifoSize;
static size_t g_TsFifoSize;
static DWORD g_TsPacketBufSize;
static int g_ConnectTimeOut;
static BOOL g_UseMagicPacket;
static UINT g_UseMagicPacket;
static UINT g_UsePatternMatch;
static char g_TargetMac[6];
static char g_TargetHost[MAX_HOST_LEN];
static char g_TargetPort[MAX_PORT_LEN];
Expand Down
1 change: 1 addition & 0 deletions BonDriver_Splitter/BonDriver_Splitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <process.h>
#include <queue>
#include <vector>
#include <string>
#include "Common.h"
#include "IBonDriver2.h"

Expand Down
4 changes: 3 additions & 1 deletion Sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ DISABLE_UNLOAD_BONDRIVER=0
BONDRIVER=C:\ptTimer\BonDriver_ptmr.dll
CHANNEL_LOCK=0
CONNECT_TIMEOUT=5
; �p�P�b�g���M��(0:�����M, ���100)
USE_MAGICPACKET=0
USE_PATTERNMATCH=0

[MAGICPACKET]
TARGET_ADDRESS=192.168.0.100
TARGET_ADDRESS=255.255.255.255
TARGET_PORT=1192
TARGET_MACADDRESS=AA-BB-CC-DD-EE-FF

Expand Down