You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-16 23:26:32 +09:00
NetConnection.Status added - reflects the "message consumed" status for consistency. Encryption work in progress.
This commit is contained in:
@@ -54,7 +54,7 @@
|
||||
Lidgren basics</h1>
|
||||
<p>
|
||||
Lidgren network library is all about messages. There are two types of messages:</p>
|
||||
<li>Library messages telling you things like a peer has connected or error messages when unexpected things happen.</li>
|
||||
<li>Library messages telling you things like a peer has connected or diagnostics messages (warnings, errors) when unexpected things happen.</li>
|
||||
<li>Data messages which is data sent from a remote (connected or unconnected) peer.</li>
|
||||
<p>
|
||||
The base class for establishing connections, receiving and sending message are the NetPeer class. Using it you can make a peer-to-peer network, but if you are creating a server/client topology there are special classes called NetServer and NetClient. They inherit NetPeer but sets some defaults and includes some helper methods/properties.</p>
|
||||
|
||||
@@ -46,7 +46,6 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Documentation\Improvements.txt" />
|
||||
<Compile Include="NetBigInteger.cs" />
|
||||
<Compile Include="NetBitVector.cs" />
|
||||
<Compile Include="NetConnectionStatistics.cs" />
|
||||
@@ -88,11 +87,8 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Documentation\ChangedFromV2.txt" />
|
||||
<Content Include="Documentation\Discovery.html" />
|
||||
<Content Include="Documentation\PacketLayout.txt" />
|
||||
<Content Include="Documentation\SimulatingBadNetwork.html" />
|
||||
<Content Include="Documentation\TODO.txt" />
|
||||
<Content Include="Documentation\Tutorial.html" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
||||
@@ -48,6 +48,11 @@ namespace Lidgren.Network
|
||||
info.Write(reason);
|
||||
m_owner.ReleaseMessage(info);
|
||||
}
|
||||
else
|
||||
{
|
||||
// app dont want those messages, update visible status immediately
|
||||
m_visibleStatus = m_status;
|
||||
}
|
||||
}
|
||||
|
||||
private void SendConnect()
|
||||
|
||||
@@ -33,6 +33,7 @@ namespace Lidgren.Network
|
||||
internal double m_lastHeardFrom;
|
||||
internal NetQueue<NetOutgoingMessage> m_unsentMessages;
|
||||
internal NetConnectionStatus m_status;
|
||||
private NetConnectionStatus m_visibleStatus;
|
||||
private double m_lastSentUnsentMessages;
|
||||
private float m_throttleDebt;
|
||||
private NetPeerConfiguration m_peerConfiguration;
|
||||
@@ -61,6 +62,18 @@ namespace Lidgren.Network
|
||||
/// </summary>
|
||||
public long RemoteUniqueIdentifier { get { return m_remoteUniqueIdentifier; } }
|
||||
|
||||
/// <summary>
|
||||
/// The current status of the connection
|
||||
/// </summary>
|
||||
public NetConnectionStatus Status
|
||||
{
|
||||
get { return m_visibleStatus; }
|
||||
internal set
|
||||
{
|
||||
m_visibleStatus = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the remote endpoint for the connection
|
||||
/// </summary>
|
||||
@@ -74,6 +87,7 @@ namespace Lidgren.Network
|
||||
m_unsentMessages = new NetQueue<NetOutgoingMessage>(16);
|
||||
m_fragmentGroups = new Dictionary<int, NetIncomingMessage>();
|
||||
m_status = NetConnectionStatus.None;
|
||||
m_visibleStatus = NetConnectionStatus.None;
|
||||
|
||||
double now = NetTime.Now;
|
||||
m_nextPing = now + 5.0f;
|
||||
@@ -646,7 +660,7 @@ namespace Lidgren.Network
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "[NetConnection to " + m_remoteEndpoint + ": " + m_status + "]";
|
||||
return "[NetConnection to " + m_remoteEndpoint + " Status: " + m_visibleStatus + "]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,9 +121,21 @@ namespace Lidgren.Network
|
||||
{
|
||||
private static readonly BigInteger N = new BigInteger(NetUtility.ToByteArray("0115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3"));
|
||||
private static readonly BigInteger g = new BigInteger((uint)2);
|
||||
private static readonly BigInteger k = ComputeMultiplier();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a verifier that the server can use to authenticate users later on
|
||||
/// Compute multiplier (k)
|
||||
/// </summary>
|
||||
private static BigInteger ComputeMultiplier()
|
||||
{
|
||||
string one = NetUtility.ToHexString(N.GetBytes());
|
||||
string two = NetUtility.ToHexString(g.GetBytes());
|
||||
byte[] cc = NetUtility.ToByteArray(one + two.PadLeft(one.Length, '0'));
|
||||
return BigInteger.Modulus(new BigInteger(NetSHA.Hash(cc)), N);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a verifier that the server can use to authenticate users later on (v)
|
||||
/// </summary>
|
||||
public static byte[] ComputePasswordVerifier(string username, string password, byte[] salt)
|
||||
{
|
||||
@@ -140,5 +152,79 @@ namespace Lidgren.Network
|
||||
BigInteger xx = new BigInteger(x);
|
||||
return g.ModPow(xx, N).GetBytes();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get 256 random bits
|
||||
/// </summary>
|
||||
public static byte[] CreateRandomChallenge()
|
||||
{
|
||||
byte[] retval = new byte[32];
|
||||
NetRandom.Instance.NextBytes(retval);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compute client challenge (A)
|
||||
/// </summary>
|
||||
public static byte[] ComputeClientChallenge(byte[] clientSalt) // a
|
||||
{
|
||||
BigInteger salt = new BigInteger(clientSalt);
|
||||
return g.ModPow(salt, N).GetBytes();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compute server challenge (B)
|
||||
/// </summary>
|
||||
public static byte[] ComputeServerChallenge(byte[] serverSalt, byte[] verifier) // b
|
||||
{
|
||||
BigInteger salt = new BigInteger(serverSalt);
|
||||
|
||||
var bb = g.ModPow(salt, N);
|
||||
var B = BigInteger.Modulus((bb + (new BigInteger(verifier) * k)), N);
|
||||
|
||||
return B.GetBytes();
|
||||
}
|
||||
|
||||
public static byte[] ComputeU(byte[] clientChallenge, byte[] serverChallenge)
|
||||
{
|
||||
byte[] A = clientChallenge;
|
||||
byte[] B = serverChallenge;
|
||||
|
||||
string one = NetUtility.ToHexString(A);
|
||||
string two = NetUtility.ToHexString(B);
|
||||
string compound = one + two.PadLeft(one.Length, '0');
|
||||
byte[] cc = NetUtility.ToByteArray(compound);
|
||||
return NetSHA.Hash(cc);
|
||||
}
|
||||
|
||||
/*
|
||||
public static byte[] ComputeClientToken(byte[] serverChallenge, byte[] x, byte[] u
|
||||
|
||||
|
||||
// S = (B - kg^x) ^ (a + ux) (mod N)
|
||||
function srp_compute_client_S(BB, xx, uu, aa, kk) {
|
||||
var bx = g.modPow(xx, N);
|
||||
var btmp = BB.add(N.multiply(kk)).subtract(bx.multiply(kk)).mod(N);
|
||||
return btmp.modPow(xx.multiply(uu).add(aa), N);
|
||||
}
|
||||
*/
|
||||
|
||||
public static byte[] ComputeServerToken(byte[] clientChallenge, byte[] verifier, byte[] u, byte[] serverChallengeSalt)
|
||||
{
|
||||
// S = (Av^u) ^ b (mod N)
|
||||
// function srp_compute_server_S(AA, vv, uu, bb) {
|
||||
|
||||
BigInteger vv = new BigInteger(verifier);
|
||||
|
||||
BigInteger c1 = vv.ModPow(new BigInteger(u), N);
|
||||
BigInteger c2 = new BigInteger(clientChallenge);
|
||||
|
||||
BigInteger r1 = c1 * c2;
|
||||
|
||||
BigInteger r2 = BigInteger.Modulus(r1, N);
|
||||
|
||||
return r2.ModPow(new BigInteger(serverChallengeSalt), N).GetBytes();
|
||||
//return vv.modPow(uu, N).multiply(A).mod(N).modPow(bb, N);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,7 +143,16 @@ namespace Lidgren.Network
|
||||
if (m_status == NetPeerStatus.NotRunning)
|
||||
return null;
|
||||
|
||||
return m_releasedIncomingMessages.TryDequeue();
|
||||
NetIncomingMessage retval = m_releasedIncomingMessages.TryDequeue();
|
||||
if (retval != null)
|
||||
{
|
||||
if (retval.MessageType == NetIncomingMessageType.StatusChanged)
|
||||
{
|
||||
NetConnectionStatus status = (NetConnectionStatus)retval.PeekByte();
|
||||
retval.SenderConnection.Status = status;
|
||||
}
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
public NetIncomingMessage WaitMessage(int maxMillis)
|
||||
|
||||
@@ -10,10 +10,22 @@ namespace UnitTests
|
||||
{
|
||||
public static void Run()
|
||||
{
|
||||
byte[] salt = NetUtility.ToByteArray("59d7304da9b97e2a9d38");
|
||||
byte[] salt = NetUtility.ToByteArray("e6fb7e23f001f3e6c081"); // s
|
||||
byte[] verifier = NetSRP.ComputePasswordVerifier("user", "password", salt);
|
||||
|
||||
Console.WriteLine("Result: " + NetUtility.ToHexString(verifier));
|
||||
Console.WriteLine("v = " + NetUtility.ToHexString(verifier));
|
||||
|
||||
byte[] a = NetUtility.ToByteArray("3b6485358d1721cb438cb7d0b3c5f8f46186d43e1c47db7cd8aa80e19760e409");
|
||||
byte[] A = NetSRP.ComputeClientChallenge(a);
|
||||
Console.WriteLine("A = " + NetUtility.ToHexString(A));
|
||||
|
||||
byte[] b = NetUtility.ToByteArray("fc17d424ce73a4c73e8fedfb25839e9917e861bc5253fff65697f81c75a87ea3");
|
||||
Console.WriteLine("b = " + NetUtility.ToHexString(b));
|
||||
byte[] B = NetSRP.ComputeServerChallenge(b, verifier);
|
||||
Console.WriteLine("B = " + NetUtility.ToHexString(B));
|
||||
|
||||
byte[] u = NetSRP.ComputeU(A, B);
|
||||
Console.WriteLine("u = " + NetUtility.ToHexString(u));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user