This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement UDP protocol and write full ICMP, ARP and UDP demo
- Loading branch information
Showing
11 changed files
with
209 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import unittest | ||
import os | ||
import socket | ||
import random | ||
|
||
dst_ip = '192.168.1.123' | ||
|
||
class TestArpUdpEcho(unittest.TestCase): | ||
def testArpUdpEcho(self): | ||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: | ||
s.settimeout(5) | ||
|
||
for _ in range(50): | ||
data = os.urandom(random.randint(0,1000)) | ||
port = random.randint(0, 65535) | ||
s.sendto(data, (dst_ip, port)) | ||
self.assertEqual((data, (dst_ip, port)), s.recvfrom(1500)) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
{-# language RecordWildCards #-} | ||
|
||
{-| | ||
Module : Clash.Cores.Ethernet.Udp | ||
Description : Circuits and data types to handle the UDP protocol | ||
-} | ||
module Clash.Cores.Ethernet.Udp | ||
( UdpHeaderLite(..) | ||
, udpDepacketizerC | ||
, udpPacketizerC | ||
) | ||
where | ||
|
||
import Clash.Cores.Ethernet.IP.IPv4Types | ||
import Clash.Prelude | ||
import Protocols | ||
import Protocols.Extra.PacketStream | ||
import Protocols.Extra.PacketStream.Packetizers ( depacketizerC, packetizerC ) | ||
|
||
-- | The full UDP header | ||
data UdpHeader = UdpHeader | ||
{ _udpSrcPort :: Unsigned 16 | ||
-- ^ Source port | ||
, _udpDstPort :: Unsigned 16 | ||
-- ^ Destination port | ||
, _udpLength :: Unsigned 16 | ||
-- ^ length of header + payload | ||
, _udpChecksum :: Unsigned 16 | ||
-- ^ UDP Checksum, We do not validate or generate it | ||
} deriving (Generic, NFDataX, BitPack, Eq, Show, ShowX) | ||
|
||
-- | UDP header | ||
data UdpHeaderLite = UdpHeaderLite | ||
{ _udplSrcPort :: Unsigned 16 | ||
-- ^ Source port | ||
, _udplDstPort :: Unsigned 16 | ||
-- ^ Destination port | ||
, _udplPayloadLength :: Unsigned 16 | ||
-- ^ Length of payload | ||
} deriving (Generic, NFDataX, BitPack, Eq, Show, ShowX) | ||
|
||
fromUdpLite :: UdpHeaderLite -> UdpHeader | ||
fromUdpLite UdpHeaderLite{..} = UdpHeader | ||
{ _udpSrcPort = _udplSrcPort | ||
, _udpDstPort = _udplDstPort | ||
, _udpLength = _udplPayloadLength + 8 | ||
, _udpChecksum = 0 | ||
} | ||
|
||
toUdpLite :: UdpHeader -> UdpHeaderLite | ||
toUdpLite UdpHeader{..} = UdpHeaderLite | ||
{ _udplSrcPort = _udpSrcPort | ||
, _udplDstPort = _udpDstPort | ||
, _udplPayloadLength = _udpLength - 8 | ||
} | ||
|
||
-- | Parses out the UDP header from an IP stream, but ignores the checksum. | ||
-- The first element of the metadata is the source IP of incoming packets. | ||
udpDepacketizerC | ||
:: HiddenClockResetEnable dom | ||
=> KnownNat n | ||
=> 1 <= n | ||
=> Circuit | ||
(PacketStream dom n IPv4HeaderLite) | ||
(PacketStream dom n (IPv4Address, UdpHeaderLite)) | ||
udpDepacketizerC = depacketizerC (\udph ipv4lh -> (_ipv4lSource ipv4lh, toUdpLite udph)) | ||
|
||
-- | Serializes the UDP packet to an IP stream. The first element of the metadata | ||
-- is the destination IP for outgoing packets. No checksum is included in the UDP header. | ||
udpPacketizerC | ||
:: HiddenClockResetEnable dom | ||
=> KnownNat n | ||
=> 1 <= n | ||
=> Signal dom IPv4Address | ||
-- ^ Source IP address | ||
-> Circuit | ||
(PacketStream dom n (IPv4Address, UdpHeaderLite)) | ||
(PacketStream dom n IPv4HeaderLite) | ||
udpPacketizerC myIp = mapMetaS (toIp <$> myIp) |> packetizerC fst snd | ||
where | ||
toIp srcIp (dstIp, udpLite) = (ipLite, udpHeader) | ||
where | ||
udpHeader = fromUdpLite udpLite | ||
ipLite = IPv4HeaderLite | ||
{ _ipv4lSource = srcIp | ||
, _ipv4lDestination = dstIp | ||
, _ipv4lProtocol = 0x0011 | ||
, _ipv4lPayloadLength = _udpLength udpHeader | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters