forked from majetzx/ntplite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.php
More file actions
66 lines (56 loc) · 2.27 KB
/
Copy pathserver.php
File metadata and controls
66 lines (56 loc) · 2.27 KB
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
<?php
// NTP server, you may need root permissions to run it
// Waits for connections and sends time
require_once 'NTPLite.php';
// Listens on the local IP address
$address = '0.0.0.0';
$port = 123;
// Opens the socket using UDP datagrams
$socket = @stream_socket_server( "udp://$address:$port", $errno, $errstr, STREAM_SERVER_BIND );
if (!$socket) {
echo "Socket error $errno: $errstr\n";
return -1;
}
do {
$data = stream_socket_recvfrom($socket, 1500, 0, $from);
// If the connection comes from an unknown client, we don't treat it
if ($from == '') {
echo ">> Connection from unknown client, rejected\n";
} else {
echo ">> Connection: $from\n";
// The client-sent query
$NTP = new NTPLite();
// NTPLite only supports client & server modes, other messages are rejected
if (!$NTP->readMessage($data)) {
$hex = '';
for ($i = 0; $i < strlen($data); $i++) {
$hex .= sprintf('%02x', ord($data[$i]));
}
echo "Bad request, aborted\n$hex\n";
} else {
$NTP->dump();
echo "\n", $NTP;
// Build the response, using arbitrary values for precision, delay & dispersion
$NTP->leapIndicator = 0;
//$NTP->versionNumber re-uses the sent value
$NTP->mode = 4;
$NTP->stratum = 6;
//$NTP->pollInterval re-uses the sent value
$NTP->precision = -20;
$NTP->rootDelay = 0;
$NTP->rootDispersion = 0.0120;
$NTP->referenceIdentifier = ip2long('127.127.1.0'); // 'LOCL' is valid too
// The current timestamp
$now = new DateTime(NULL);
$NTP->referenceTimestamp = NTPLite::convertDateTimeToSntp($now);
$NTP->originateTimestamp = $NTP->transmitTimestamp; // re-uses the transmit TS
$NTP->receiveTimestamp = NTPLite::convertDateTimeToSntp($now);
$NTP->transmitTimestamp = NTPLite::convertDateTimeToSntp($now);
// Sends the response
stream_socket_sendto($socket, $NTP->writeMessage(), 0, $from);
}
unset($NTP);
echo "<< Connection: $from\n\n";
}
} while ($data !== false);
?>