Skip to content

Commit

Permalink
fix corrupted packet handling
Browse files Browse the repository at this point in the history
  • Loading branch information
YYHEggEgg authored Dec 19, 2023
1 parent 004787f commit f6a290e
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 20 deletions.
98 changes: 93 additions & 5 deletions .github/workflows/network-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
--packet-repeat-time 200
--packet-interval 40
--github-actions
--clients-count 2
--clients-count 1
--packet-delay-log
- name: Archive logs
Expand All @@ -52,6 +52,78 @@ jobs:
path: Tests/KcpTests/KcpPerformanceTest/logs
retention-days: 21

net-debug-direct-bytecheck-ubuntu:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'

- name: Restore NuGet packages
run: |
dotnet nuget add source https://apiint.nugettest.org/v3/index.json -n "nuget.org (Integration)"
dotnet restore Tests/KcpTests/KcpPerformanceTest/KcpPerformanceTest.csproj
- name: Run tests
run: >
cd Tests/KcpTests/KcpPerformanceTest ;
dotnet run --configuration Debug
--property:DefineConstants=TRACE%3bDEBUG%3bNET%3bNET6_0%3bNETCOREAPP%3bMIHOMO_KC%3bCONNECT_SERVERONLYP%3bBYTE_CHECK_MODE --
--packet-size 3500
--packet-repeat-time 200
--packet-interval 40
--github-actions
--clients-count 1
--packet-delay-log
- name: Archive logs
uses: actions/upload-artifact@v2
with:
name: Report & logs (direct-ubuntu-debug, BYTE_CHECK_MODE)
path: Tests/KcpTests/KcpPerformanceTest/logs
retention-days: 21

net-debug-direct-corrupt-bytecheck-ubuntu:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'

- name: Restore NuGet packages
run: |
dotnet nuget add source https://apiint.nugettest.org/v3/index.json -n "nuget.org (Integration)"
dotnet restore Tests/KcpTests/KcpPerformanceTest/KcpPerformanceTest.csproj
- name: Run tests
run: >
cd Tests/KcpTests/KcpPerformanceTest ;
dotnet run --configuration Debug
--property:DefineConstants=TRACE%3bDEBUG%3bNET%3bNET6_0%3bNETCOREAPP%3bMIHOMO_KCP%3bBYTE_CHECK_MODE%3bCONNECT_SERVERONLY%3bCORRUPT_PACKET --
--packet-size 3500
--packet-repeat-time 200
--packet-interval 40
--github-actions
--clients-count 1
--packet-delay-log
- name: Archive logs
uses: actions/upload-artifact@v2
with:
name: Report & logs (proxy-ubuntu-debug, BYTE_CHECK_MODE, CORRUPT_PACKET)
path: Tests/KcpTests/KcpPerformanceTest/logs
retention-days: 21

net-debug-proxy-ubuntu:
runs-on: ubuntu-latest

Expand All @@ -77,7 +149,7 @@ jobs:
--packet-repeat-time 200
--packet-interval 40
--github-actions
--clients-count 2
--clients-count 1
--packet-delay-log
- name: Archive logs
Expand Down Expand Up @@ -113,7 +185,7 @@ jobs:
--packet-repeat-time 200
--packet-interval 40
--github-actions
--clients-count 2
--clients-count 1
--packet-delay-log
- name: Archive logs
Expand Down Expand Up @@ -149,13 +221,13 @@ jobs:
--packet-repeat-time 200
--packet-interval 40
--github-actions
--clients-count 2
--clients-count 1
--packet-delay-log
- name: Archive logs
uses: actions/upload-artifact@v2
with:
name: Report & logs (proxy-ubuntu-debug, BYTE_CHECK_MODE)
name: Report & logs (proxy-ubuntu-debug, BYTE_CHECK_MODE, CORRUPT_PACKET)
path: Tests/KcpTests/KcpPerformanceTest/logs
retention-days: 21

Expand All @@ -166,6 +238,7 @@ jobs:

needs:
- net-debug-direct-ubuntu
- net-debug-direct-bytecheck-ubuntu
- net-debug-proxy-ubuntu
- net-debug-proxy-bytecheck-ubuntu

Expand Down Expand Up @@ -207,6 +280,7 @@ jobs:

needs:
- net-debug-direct-ubuntu
- net-debug-direct-bytecheck-ubuntu
- net-debug-proxy-ubuntu
- net-debug-proxy-bytecheck-ubuntu

Expand Down Expand Up @@ -248,6 +322,7 @@ jobs:

needs:
- net-debug-direct-ubuntu
- net-debug-direct-bytecheck-ubuntu
- net-debug-proxy-ubuntu
- net-debug-proxy-bytecheck-ubuntu

Expand Down Expand Up @@ -288,6 +363,7 @@ jobs:

needs:
- net-debug-direct-ubuntu
- net-debug-direct-bytecheck-ubuntu
- net-debug-proxy-ubuntu
- net-debug-proxy-bytecheck-ubuntu

Expand Down Expand Up @@ -330,6 +406,7 @@ jobs:

needs:
- net-debug-direct-ubuntu
- net-debug-direct-bytecheck-ubuntu
- net-debug-proxy-ubuntu
- net-debug-proxy-bytecheck-ubuntu

Expand Down Expand Up @@ -371,6 +448,7 @@ jobs:

needs:
- net-debug-direct-ubuntu
- net-debug-direct-bytecheck-ubuntu
- net-debug-proxy-ubuntu
- net-debug-proxy-bytecheck-ubuntu

Expand Down Expand Up @@ -412,6 +490,7 @@ jobs:

needs:
- net-debug-direct-ubuntu
- net-debug-direct-bytecheck-ubuntu
- net-debug-proxy-ubuntu
- net-debug-proxy-bytecheck-ubuntu

Expand Down Expand Up @@ -453,6 +532,7 @@ jobs:

needs:
- net-debug-direct-ubuntu
- net-debug-direct-bytecheck-ubuntu
- net-debug-proxy-ubuntu
- net-debug-proxy-bytecheck-ubuntu

Expand Down Expand Up @@ -494,6 +574,8 @@ jobs:

needs:
- net-debug-direct-ubuntu
- net-debug-direct-bytecheck-ubuntu
- net-debug-direct-corrupt-bytecheck-ubuntu
- net-debug-proxy-ubuntu
- net-debug-proxy-bytecheck-ubuntu
- net-debug-proxy-corrupt-bytecheck-ubuntu
Expand Down Expand Up @@ -536,6 +618,8 @@ jobs:

needs:
- net-debug-direct-ubuntu
- net-debug-direct-bytecheck-ubuntu
- net-debug-direct-corrupt-bytecheck-ubuntu
- net-debug-proxy-ubuntu
- net-debug-proxy-bytecheck-ubuntu
- net-debug-proxy-corrupt-bytecheck-ubuntu
Expand Down Expand Up @@ -578,6 +662,8 @@ jobs:

needs:
- net-debug-direct-ubuntu
- net-debug-direct-bytecheck-ubuntu
- net-debug-direct-corrupt-bytecheck-ubuntu
- net-debug-proxy-ubuntu
- net-debug-proxy-bytecheck-ubuntu
- net-debug-proxy-corrupt-bytecheck-ubuntu
Expand Down Expand Up @@ -620,6 +706,8 @@ jobs:

needs:
- net-debug-direct-ubuntu
- net-debug-direct-bytecheck-ubuntu
- net-debug-direct-corrupt-bytecheck-ubuntu
- net-debug-proxy-ubuntu
- net-debug-proxy-bytecheck-ubuntu
- net-debug-proxy-corrupt-bytecheck-ubuntu
Expand Down
25 changes: 12 additions & 13 deletions KCP/Kcp/KcpCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1969,18 +1969,6 @@ ref length
switch (cmd)
{
case IKCP_CMD_PUSH:
#if BYTE_CHECK_MODE
if (!byteCheck(span.Slice(offset, (int)length), byteCheckMode, byteCheckCode))
{
if (CanLog(KcpLogMask.IKCP_LOG_IN_DATA))
{
LogWriteLine($"input psh dropped (checksum failed): sn={sn} ts={ts}", KcpLogMask.IKCP_LOG_IN_DATA.ToString());
}
offset += (int)length;
continue;
}
break;
#endif
case IKCP_CMD_ACK:
case IKCP_CMD_WASK:
case IKCP_CMD_WINS:
Expand Down Expand Up @@ -2049,6 +2037,17 @@ ref length

if (Itimediff(sn, rcv_nxt + rcv_wnd) < 0)
{
#if BYTE_CHECK_MODE
if (!byteCheck(span.Slice(offset, (int)length), byteCheckMode, byteCheckCode))
{
if (CanLog(KcpLogMask.IKCP_LOG_IN_DATA))
{
LogWriteLine($"input psh dropped (checksum failed): sn={sn} ts={ts}", KcpLogMask.IKCP_LOG_IN_DATA.ToString());
}
offset += (int)length;
continue;
}
#endif
///instead of ikcp_ack_push
acklist.Enqueue((sn, ts));

Expand Down Expand Up @@ -2320,10 +2319,10 @@ ref length
{
LogWriteLine($"input psh dropped (checksum failed): sn={sn} ts={ts}", KcpLogMask.IKCP_LOG_IN_DATA.ToString());
}
offset += (int)length;
continue;
}
#endif

///instead of ikcp_ack_push
acklist.Enqueue((sn, ts));

Expand Down
10 changes: 9 additions & 1 deletion KCP/Kcp/KcpSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,15 @@ public Span<byte> data
private void CorruptData(Span<byte> buffer)
{
if (buffer.Length == 0 || !corrupt) return;
if (xmit >= 2 || sn <= 500) return;
if (sn <= 500) return;
if (xmit < 2)
{
Log.Verb($"Packet sn={sn}: corrupted packet", nameof(KcpSegment));
}
else
{
Log.Verb($"Packet sn={sn}: normal packet", nameof(KcpSegment));
}
lock (Random.Shared)
{
buffer[Random.Shared.Next(0, buffer.Length)] = 0;
Expand Down
2 changes: 1 addition & 1 deletion Tests/KcpTests/SharedLib/KCP/SpecialUdp/SocketUdpClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// #define SOCKET_UDP_VERBOSE
// #define SOCKET_UDP_PACKET_CONTENT_VERBOSE
// #define SOCKET_UDP_PACKET_CONTENT_VERBOSE_SYNCRONOUS
#define SOCKET_UDP_PACKET_CONTENT_VERBOSE_SYNCRONOUS

// Generated by ChatGPT
using System.Net;
Expand Down

0 comments on commit f6a290e

Please sign in to comment.