1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-06 02:11:06 +09:00

BREAKING CHANGE: NatIntroductionSuccess is now DISABLED by default; you must enable it using EnableMessageType

Added GarbageThrowerSample - a small library sample that sends random and semi-random data to detect problems
Made lots of changes that caused exceptions when malformed data was received
This commit is contained in:
lidgren
2014-07-31 14:55:50 +00:00
parent 04593ef00f
commit d2ae3cf41d
13 changed files with 336 additions and 18 deletions

View File

@@ -406,7 +406,7 @@ namespace Lidgren.Network
{
int num1 = 0;
int num2 = 0;
while (true)
while (m_bitLength - m_readPosition >= 8)
{
byte num3 = this.ReadByte();
num1 |= (num3 & 0x7f) << num2;
@@ -414,6 +414,9 @@ namespace Lidgren.Network
if ((num3 & 0x80) == 0)
return (uint)num1;
}
// ouch; failed to find enough bytes; malformed variable length number?
return (uint)num1;
}
/// <summary>
@@ -424,7 +427,7 @@ namespace Lidgren.Network
{
int num1 = 0;
int num2 = 0;
while (true)
while (m_bitLength - m_readPosition >= 8)
{
byte num3;
if (ReadByte(out num3) == false)
@@ -440,6 +443,8 @@ namespace Lidgren.Network
return true;
}
}
result = (uint)num1;
return false;
}
/// <summary>
@@ -468,7 +473,7 @@ namespace Lidgren.Network
{
UInt64 num1 = 0;
int num2 = 0;
while (true)
while (m_bitLength - m_readPosition >= 8)
{
//if (num2 == 0x23)
// throw new FormatException("Bad 7-bit encoded integer");
@@ -479,6 +484,9 @@ namespace Lidgren.Network
if ((num3 & 0x80) == 0)
return num1;
}
// ouch; failed to find enough bytes; malformed variable length number?
return num1;
}
/// <summary>
@@ -543,10 +551,19 @@ namespace Lidgren.Network
{
int byteLen = (int)ReadVariableUInt32();
if (byteLen == 0)
if (byteLen <= 0)
return String.Empty;
NetException.Assert(m_bitLength - m_readPosition >= (byteLen * 8), c_readOverflowError);
if ((ulong)(m_bitLength - m_readPosition) < ((ulong)byteLen * 8))
{
// not enough data
#if DEBUG
throw new NetException(c_readOverflowError);
#else
m_readPosition = m_bitLength;
return null; // unfortunate; but we need to protect against DDOS
#endif
}
if ((m_readPosition & 7) == 0)
{
@@ -572,7 +589,7 @@ namespace Lidgren.Network
return false;
}
if (byteLen == 0)
if (byteLen <= 0)
{
result = String.Empty;
return true;

View File

@@ -76,14 +76,12 @@ namespace Lidgren.Network
case NetConnectionStatus.RespondedConnect:
SendConnectResponse(now, true);
break;
case NetConnectionStatus.None:
case NetConnectionStatus.ReceivedInitiation:
m_peer.LogWarning("Time to resend handshake, but status is " + m_status);
break;
case NetConnectionStatus.RespondedAwaitingApproval:
// awaiting approval
m_lastHandshakeSendTime = now; // postpone handshake resend
break;
case NetConnectionStatus.None:
case NetConnectionStatus.ReceivedInitiation:
default:
m_peer.LogWarning("Time to resend handshake, but status is " + m_status);
break;
@@ -95,8 +93,6 @@ namespace Lidgren.Network
{
m_peer.VerifyNetworkThread();
//m_peer.LogDebug("Executing disconnect");
// clear send queues
for (int i = 0; i < m_sendChannels.Length; i++)
{
@@ -108,7 +104,15 @@ namespace Lidgren.Network
if (sendByeMessage)
SendDisconnect(reason, true);
SetStatus(NetConnectionStatus.Disconnected, reason);
if (m_status == NetConnectionStatus.ReceivedInitiation)
{
// nothing much has happened yet; no need to send disconnected status message
m_status = NetConnectionStatus.Disconnected;
}
else
{
SetStatus(NetConnectionStatus.Disconnected, reason);
}
// in case we're still in handshake
lock (m_peer.m_handshakes)
@@ -448,7 +452,6 @@ namespace Lidgren.Network
if (remoteAppIdentifier != m_peer.m_configuration.AppIdentifier)
{
// wrong app identifier
ExecuteDisconnect("Wrong application identifier!", true);
return false;
}

View File

@@ -499,6 +499,7 @@ namespace Lidgren.Network
msg.m_senderConnection = sender;
msg.m_senderEndPoint = ipsender;
msg.m_bitLength = payloadBitLength;
Buffer.BlockCopy(m_receiveBuffer, ptr, msg.m_data, 0, payloadByteLength);
if (sender != null)
{
@@ -594,10 +595,12 @@ namespace Lidgren.Network
HandleIncomingDiscoveryResponse(now, senderEndPoint, ptr, payloadByteLength);
return;
case NetMessageType.NatIntroduction:
HandleNatIntroduction(ptr);
if (m_configuration.IsMessageTypeEnabled(NetIncomingMessageType.NatIntroductionSuccess))
HandleNatIntroduction(ptr);
return;
case NetMessageType.NatPunchMessage:
HandleNatPunch(ptr, senderEndPoint);
if (m_configuration.IsMessageTypeEnabled(NetIncomingMessageType.NatIntroductionSuccess))
HandleNatPunch(ptr, senderEndPoint);
return;
case NetMessageType.ConnectResponse:

View File

@@ -318,7 +318,7 @@ namespace Lidgren.Network
#if DEBUG
public void RawSend(byte[] arr, int offset, int length, IPEndPoint destination)
#else
internal void RawSend(byte[] arr, int offset, int length, IPEndPoint destination)
public void RawSend(byte[] arr, int offset, int length, IPEndPoint destination)
#endif
{
// wrong thread - this miiiight crash with network thread... but what's a boy to do.

View File

@@ -84,7 +84,7 @@ namespace Lidgren.Network
//
// default values
//
m_disabledTypes = NetIncomingMessageType.ConnectionApproval | NetIncomingMessageType.UnconnectedData | NetIncomingMessageType.VerboseDebugMessage | NetIncomingMessageType.ConnectionLatencyUpdated;
m_disabledTypes = NetIncomingMessageType.ConnectionApproval | NetIncomingMessageType.UnconnectedData | NetIncomingMessageType.VerboseDebugMessage | NetIncomingMessageType.ConnectionLatencyUpdated | NetIncomingMessageType.NatIntroductionSuccess;
m_networkThreadName = "Lidgren network thread";
m_localAddress = IPAddress.Any;
m_broadcastAddress = IPAddress.Broadcast;

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
</configuration>

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using Lidgren.Network;
using System.Threading;
using System.Net;
namespace Client
{
class Program
{
static void Main(string[] args)
{
NetPeerConfiguration config = new NetPeerConfiguration("garbagethrower");
var client = new NetClient(config);
client.Start();
var target = new IPEndPoint(NetUtility.Resolve("localhost"), 14242);
var buffer = new byte[1024];
var rnd = new Random();
// use RawSend to throw poop at server
while(true)
{
rnd.NextBytes(buffer);
int length = rnd.Next(1, 1023);
switch (rnd.Next(2))
{
case 0:
// complete randomness
break;
case 1:
// semi-sensical
buffer[1] = 0; // not a fragment, sequence number 0
buffer[2] = 0; // not a fragment, sequence number 0
buffer[3] = (byte)length; // correct payload length
buffer[4] = (byte)(length >> 8); // correct payload length
break;
}
// fling teh poop
client.RawSend(buffer, 0, length, target);
Thread.Sleep(1);
}
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Client")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Client")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("0c9a409f-00cd-4b05-87f6-6f4a86d82987")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,38 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lidgren.Network", "..\..\..\Lidgren.Network\Lidgren.Network.csproj", "{49BA1C69-6104-41AC-A5D8-B54FA9F696E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SamplesCommon", "..\..\SamplesCommon\SamplesCommon.csproj", "{773069DA-B66E-4667-ADCB-0D215AD8CF3E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{154863C7-4198-48B7-B7BC-CB27821EE7E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csproj", "{CCE7E8B5-4167-464C-9B11-EEB9A4439EC1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{49BA1C69-6104-41AC-A5D8-B54FA9F696E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{49BA1C69-6104-41AC-A5D8-B54FA9F696E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{49BA1C69-6104-41AC-A5D8-B54FA9F696E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{49BA1C69-6104-41AC-A5D8-B54FA9F696E8}.Release|Any CPU.Build.0 = Release|Any CPU
{773069DA-B66E-4667-ADCB-0D215AD8CF3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{773069DA-B66E-4667-ADCB-0D215AD8CF3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{773069DA-B66E-4667-ADCB-0D215AD8CF3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{773069DA-B66E-4667-ADCB-0D215AD8CF3E}.Release|Any CPU.Build.0 = Release|Any CPU
{154863C7-4198-48B7-B7BC-CB27821EE7E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{154863C7-4198-48B7-B7BC-CB27821EE7E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{154863C7-4198-48B7-B7BC-CB27821EE7E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{154863C7-4198-48B7-B7BC-CB27821EE7E8}.Release|Any CPU.Build.0 = Release|Any CPU
{CCE7E8B5-4167-464C-9B11-EEB9A4439EC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CCE7E8B5-4167-464C-9B11-EEB9A4439EC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CCE7E8B5-4167-464C-9B11-EEB9A4439EC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CCE7E8B5-4167-464C-9B11-EEB9A4439EC1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
</configuration>

View File

@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using Lidgren.Network;
namespace Server
{
class Program
{
static void Main(string[] args)
{
NetPeerConfiguration config = new NetPeerConfiguration("garbagethrower");
config.MaximumConnections = 1;
config.Port = 14242;
var server = new NetServer(config);
server.Start();
while (true)
{
NetIncomingMessage msg;
while ((msg = server.ReadMessage()) != null)
{
switch (msg.MessageType)
{
case NetIncomingMessageType.StatusChanged:
var status = (NetConnectionStatus)msg.ReadByte();
var reason = msg.ReadString();
Console.WriteLine("New status: " + status + " (" + reason + ")");
break;
case NetIncomingMessageType.WarningMessage:
case NetIncomingMessageType.VerboseDebugMessage:
case NetIncomingMessageType.ErrorMessage:
case NetIncomingMessageType.DebugMessage:
var str = msg.ReadString();
if (str.StartsWith("Malformed packet; stated") ||
str.StartsWith("Received unhandled library message") ||
str.StartsWith("Unexpected NetMessageType"))
break; // we'll get a bunch of these and we're fine with that
Console.WriteLine(msg.MessageType + ": " + str);
break;
case NetIncomingMessageType.Data:
Console.WriteLine("Received " + msg.LengthBits + " bits of data");
break;
case NetIncomingMessageType.UnconnectedData:
Console.WriteLine("Received " + msg.LengthBits + " bits of unconnected data");
break;
default:
Console.WriteLine("Received " + msg.MessageType);
break;
}
}
}
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Server")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Server")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("afeebf34-cb2c-4377-ace7-5c55f2882c0d")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CCE7E8B5-4167-464C-9B11-EEB9A4439EC1}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Server</RootNamespace>
<AssemblyName>Server</AssemblyName>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\Lidgren.Network\Lidgren.Network.csproj">
<Project>{49ba1c69-6104-41ac-a5d8-b54fa9f696e8}</Project>
<Name>Lidgren.Network</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\SamplesCommon\SamplesCommon.csproj">
<Project>{773069da-b66e-4667-adcb-0d215ad8cf3e}</Project>
<Name>SamplesCommon</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>