-
Notifications
You must be signed in to change notification settings - Fork 3
/
wow.class.php
91 lines (73 loc) · 2.14 KB
/
wow.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* This class deals with wake on wan
*/
class WoW
{
private $hostname; // hostname (domain) of the target machine or the router that talks to listening computer
private $mac; // mac address of the target machine
private $port; // open port on router mapped to target machine wol port
// note that same outgoing port should be open on sending server!
private $ip; // ip address of target machine
private $msg = array(
0 => "Target machine seems to be Online.",
1 => "socket_create function failed for some reason",
2 => "socket_set_option function failed for some reason",
3 => "magic packet sent successfully!",
4 => "magic packet failed!"
);
function __construct($hostname,$mac,$port,$ip = false)
{
$this->hostname = $hostname;
$this->mac = $mac;
$this->port = $port;
$this->ip = $ip ?: $this->get_ip_from_hostname();
}
public function wake_on_wan()
{
if ($this->is_awake())
{
return $this->msg[0]; // is awake, nothing to do
}
else
{
$addr_byte = explode(':', $this->mac);
$hw_addr = '';
for ($a=0; $a<6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
for ($a=1; $a<=16; $a++) $msg .= $hw_addr;
// send it to the broadcast address using UDP
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($s == false)
{
return $this->msg[1]; // socket_create failed
}
$set_opt = @socket_set_option($s, 1, 6, TRUE);
if ($set_opt < 0)
{
return $this->msg[2]; // socket_set_option failed
}
$sendto = @socket_sendto($s, $msg, strlen($msg), 0, $this->ip, $this->port);
if ($sendto)
{
socket_close($s);
return $this->msg[3]; // magic packet sent successfully!
}
return $this->msg[4]; // magic packet failed!
}
}
private function is_awake()
{
$awake = @fsockopen($this->ip, 80, $errno, $errstr, 2);
if ($awake)
{
fclose($awake);
}
return $awake;
}
private function get_ip_from_hostname()
{
return gethostbyname($this->hostname);
}
}
?>