Files
PrimAITE/src/primaite/simulator/network/protocols/ntp.py

45 lines
1.1 KiB
Python
Raw Normal View History

2023-11-13 17:39:27 +00:00
from __future__ import annotations
2023-11-15 10:57:56 +00:00
from datetime import datetime
2023-11-13 17:39:27 +00:00
from ipaddress import IPv4Address
from typing import Optional
2023-11-15 10:57:56 +00:00
2023-11-13 17:39:27 +00:00
from pydantic import BaseModel
2023-11-15 10:57:56 +00:00
2023-11-13 17:39:27 +00:00
from primaite.simulator.network.protocols.packet import DataPacket
class NTPRequest(BaseModel):
"""Represents a NTP Request packet."""
ntp_client: Optional[IPv4Address] = None
2023-11-13 17:39:27 +00:00
class NTPReply(BaseModel):
"""Represents a NTP Reply packet."""
ntp_datetime: datetime
"NTP datetime object set by NTP Server."
2023-11-13 17:39:27 +00:00
class NTPPacket(DataPacket):
"""
Represents the NTP layer of a network frame.
:param ntp_request: NTPRequest packet from NTP client.
:param ntp_reply: NTPReply packet from NTP Server.
"""
ntp_request: NTPRequest
"NTP Request packet sent by NTP Client."
ntp_reply: Optional[NTPReply] = None
def generate_reply(self, time: datetime) -> NTPPacket:
2023-11-15 10:57:56 +00:00
"""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