Example #1
0
    def _run(self, address, port):
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.bind((address, port))
        while not self._stopflag:
            rlist, wlist, elist = select.select([sock], [], [], 1)
            for tempSocket in rlist:
                with self._time_lock:
                    if self._time is None:
                        recvTimestamp = ntplib.system_to_ntp_time(time.time())
                    else:
                        recvTimestamp = ntplib.system_to_ntp_time(self._time)

                    data, addr = tempSocket.recvfrom(1024)
                    recvPacket = ntplib.NTPPacket()
                    recvPacket.from_data(data)
                    timeStamp_high = ntplib._to_int(recvPacket.tx_timestamp)
                    timeStamp_low = ntplib._to_frac(recvPacket.tx_timestamp)
                    sendPacket = ntplib.NTPPacket(version=3, mode=4)
                    sendPacket.stratum = 2
                    sendPacket.poll = 10
                    sendPacket.ref_timestamp = recvTimestamp - 5
                    sendPacket.orig_timestamp = ntplib._to_time(
                        timeStamp_high, timeStamp_low)
                    sendPacket.recv_timestamp = recvTimestamp
                    if self._time is None:
                        sendPacket.tx_timestamp = (ntplib.system_to_ntp_time(
                            time.time()))
                    else:
                        sendPacket.tx_timestamp = (ntplib.system_to_ntp_time(
                            self._time))
                    sock.sendto(sendPacket.to_data(), addr)
                    print("Served request from {}".format(addr))
Example #2
0
 def run(self):
     while True:
         try:
             data, addr, recvTimestamp = self.taskQueue.get(timeout=1)
             recvPacket = ntplib.NTPPacket()
             recvPacket.from_data(data)
             timeStamp_high = ntplib._to_int(recvPacket.tx_timestamp)
             timeStamp_low = ntplib._to_frac(recvPacket.tx_timestamp)
             sendPacket = ntplib.NTPPacket(version=3, mode=4)
             sendPacket.stratum = 2
             sendPacket.poll = 10
             '''
             sendPacket.precision = 0xfa
             sendPacket.root_delay = 0x0bfa
             sendPacket.root_dispersion = 0x0aa7
             sendPacket.ref_id = 0x808a8c2c
             '''
             sendPacket.ref_timestamp = recvTimestamp - 5
             sendPacket.orig_timestamp = ntplib._to_time(
                 timeStamp_high, timeStamp_low)
             sendPacket.recv_timestamp = recvTimestamp
             sendPacket.tx_timestamp = ntplib.system_to_ntp_time(
                 time.time())
             self.sock.sendto(sendPacket.to_data(), addr)
             print("Sent to %s:%d" % (addr[0], addr[1]))
         except queue.Empty:
             continue
Example #3
0
    def create_ntp_request_data():
        ntp_query_packet = ntp.NTPPacket(
            mode=3,  # 3 = client, 4 = server
            version=app.config['NTP_VERSION'],
            tx_timestamp=ntp.system_to_ntp_time(time()))

        return ntp_query_packet.to_data()
Example #4
0
def run(ip, port=123):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.setblocking(0)
    sock.bind((ip, port))
    logger.info(f"NTP Server started on {ip}:{port}. Press Ctrl+C for stop")

    while True:
        ready, _, _ = select.select([sock], [], [], 0.1)
        if ready:
            data, addr = sock.recvfrom(0x400)
            packet = ntplib.NTPPacket()
            packet.from_data(data)
            logger.info(f"IN ({addr}) {binascii.b2a_hex(data).decode()}")
            packet.mode = 4
            packet.stratum = 2
            packet.tx_timestamp = ntplib.system_to_ntp_time(time.time())
            sock.sendto(packet.to_data(), addr)
            logger.info(
                f"OUT({addr}) {binascii.b2a_hex(packet.to_data()).decode()}")
Example #5
0
import sys
import socket
from time import ctime

import ntplib

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(b'\x1b' + 47 * b'\0', ('ntp2.stratum2.ru', 123))
p = ntplib.NTPPacket(s.recv(1024))
print(p)

print(ctime(p.recv_timestamp))

sys.exit()
correction = int(open('config.txt').read())
print(correction)

client = ntplib.NTPClient()
response = client.request('ntp2.stratum2.ru')
print(ctime(response.tx_time))
new_time = response.tx_time + correction
print(new_time)
print()
print(response.recv_timestamp)
print(ctime(response.recv_timestamp))
print(response.tx_timestamp)
print(ctime(response.tx_timestamp))
response.recv_timestamp += correction
response.tx_timestamp += correction
print(ctime(response.tx_time))