# enet_lib.ps1 — e:net 协议验证公共函数库 $script:pass = 0; $script:fail = 0 function Check($name, $cond, $detail) { if ($cond) { $script:pass++; Write-Host (" [PASS] {0} {1}" -f $name, $detail) } else { $script:fail++; Write-Host (" [FAIL] {0} {1}" -f $name, $detail) } } function Read-Pcap([string]$path) { $b = [IO.File]::ReadAllBytes($path) function U32([byte[]]$x, [int]$o, [bool]$sw) { if ($sw) { return ([uint32]$x[$o] -shl 24) -bor ([uint32]$x[$o+1] -shl 16) -bor ([uint32]$x[$o+2] -shl 8) -bor [uint32]$x[$o+3] } return [BitConverter]::ToUInt32($x, $o) } $magic = U32 $b 0 $false $swap = ($magic -eq 0xa1b2c3d4) $pkts = New-Object System.Collections.Generic.List[object] $o = 24 while ($o + 16 -le $b.Length) { $incl = U32 $b ($o+8) $swap if ($o + 16 + $incl -gt $b.Length) { break } $data = New-Object byte[] $incl [Array]::Copy($b, $o+16, $data, 0, $incl) $pkts.Add([pscustomobject]@{ Len = $incl; Data = $data }) $o += 16 + $incl } return $pkts } function UdpPayload($pkt) { $d = $pkt.Data if ($d.Length -lt 42) { return $null } $ethType = ([uint32]$d[12] -shl 8) -bor $d[13] $off = 14 if ($ethType -eq 0x8100) { $ethType = ([uint32]$d[16] -shl 8) -bor $d[17]; $off = 18 } if ($ethType -ne 0x0800) { return $null } $ihl = ($d[$off] -band 0x0F) * 4 if ($d[$off + 9] -ne 17) { return $null } $ipStart = $off; $udp = $off + $ihl $src = "{0}.{1}.{2}.{3}" -f $d[$ipStart+12], $d[$ipStart+13], $d[$ipStart+14], $d[$ipStart+15] $dst = "{0}.{1}.{2}.{3}" -f $d[$ipStart+16], $d[$ipStart+17], $d[$ipStart+18], $d[$ipStart+19] $sport = ([uint32]$d[$udp] -shl 8) -bor $d[$udp+1] $dport = ([uint32]$d[$udp+2] -shl 8) -bor $d[$udp+3] $pl = New-Object byte[] ($d.Length - $udp - 8) [Array]::Copy($d, $udp + 8, $pl, 0, $pl.Length) return [pscustomobject]@{ Src=$src; Dst=$dst; Sport=$sport; Dport=$dport; Payload=$pl } } function LE32([byte[]]$x, [int]$o) { return [uint32]$x[$o] -bor ([uint32]$x[$o+1] -shl 8) -bor ([uint32]$x[$o+2] -shl 16) -bor ([uint32]$x[$o+3] -shl 24) } function LE16([byte[]]$x, [int]$o) { return [uint32]([uint32]$x[$o] -bor ([uint32]$x[$o+1] -shl 8)) } function Hex([byte[]]$x, [int]$o, [int]$n) { ($x[$o..($o+$n-1)] | ForEach-Object { $_.ToString('x2') }) -join '' }