#2041: Implement NTP protocol for server
This commit is contained in:
@@ -2,22 +2,22 @@ from __future__ import annotations
|
||||
|
||||
from ipaddress import IPv4Address
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from primaite.simulator.network.protocols.packet import DataPacket
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class NTPRequest(BaseModel):
|
||||
"""Represents a NTP Request packet."""
|
||||
|
||||
pass
|
||||
ntp_client: IPv4Address = None
|
||||
|
||||
|
||||
class NTPReply(BaseModel):
|
||||
"""Represents a NTP Reply packet."""
|
||||
|
||||
pass
|
||||
ntp_datetime: datetime
|
||||
"NTP datetime object set by NTP Server."
|
||||
|
||||
|
||||
class NTPPacket(DataPacket):
|
||||
@@ -31,4 +31,12 @@ class NTPPacket(DataPacket):
|
||||
ntp_request: NTPRequest
|
||||
"NTP Request packet sent by NTP Client."
|
||||
ntp_reply: Optional[NTPReply] = None
|
||||
"NTP Reply packet generated by NTP Server."
|
||||
|
||||
def generate_reply(self, time: datetime) -> NTPPacket:
|
||||
""" Generate a NTPPacket containing the time in a NTPReply object
|
||||
|
||||
:param time: datetime object representing the time from the NTP server.
|
||||
:return: A new NTPPacket object.
|
||||
"""
|
||||
self.ntp_reply = NTPReply(time)
|
||||
return self
|
||||
|
||||
Reference in New Issue
Block a user