Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion STUN/Attributes/STUNXorMappedAddressAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,66 @@
namespace STUN.Attributes
using System;
using System.Net;

namespace STUN.Attributes
{
public class STUNXorMappedAddressAttribute : STUNEndPointAttribute
{
public override string ToString()
{
return string.Format("XOR-MAPPED-ADDRESS {0}", EndPoint);
}

public override void Parse(STUNMessage msg, STUNBinaryReader binary, int length)
{
binary.BaseStream.Position++;

var ipFamily = binary.ReadByte();
// Port is computed by taking the mapped port in host byte order,
// XOR'ing it with the most significant 16 bits of the magic cookie.
byte[] p = binary.ReadBytes(2);
for (int i = 0; i < 2; ++i)
{
p[i] = (byte)(p[i] ^ msg.TransactionID[i]);
}

if (BitConverter.IsLittleEndian)
{
Array.Reverse(p);
}

var port = BitConverter.ToUInt16(p, 0);

IPAddress address;

if (ipFamily == 1)
{
// IPv4 address is computed by taking the mapped IP
// address in host byte order and XOR'ing it with the magic cookie.
byte [] a = binary.ReadBytes(4);
for (int i = 0; i < 4; ++i)
{
a[i] = (byte)(a[i] ^ msg.TransactionID[i]);
}
address = new IPAddress(a);
}
else if (ipFamily == 2)
{
// IPv6 address is computed by taking the mapped IP address
// in host byte order, XOR'ing it with the concatenation of the magic
// cookie and the 96 - bit transaction ID.
byte[] a = binary.ReadBytes(16);
for (int i = 0; i < 16; ++i)
{
a[i] = (byte)(a[i] ^ msg.TransactionID[i]);
}
address = new IPAddress(a);
}
else
{
throw new Exception("Unsupported IP Family " + ipFamily.ToString());
}

EndPoint = new IPEndPoint(address, port);
}
}
}
2 changes: 1 addition & 1 deletion STUN/Attributes/StunAsciiTextAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class STUNAsciiTextAttribute : STUNAttribute
{
public string Text { get; set; }

public override void Parse(STUNBinaryReader binary, int length)
public override void Parse(STUNMessage msg, STUNBinaryReader binary, int length)
{
Text = Encoding.ASCII.GetString(binary.ReadBytes(length));
}
Expand Down
2 changes: 1 addition & 1 deletion STUN/Attributes/StunChangeRequestAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public STUNChangeRequestAttribute(bool ip, bool port)
ChangePort = port;
}

public override void Parse(STUNBinaryReader binary, int length)
public override void Parse(STUNMessage msg, STUNBinaryReader binary, int length)
{
binary.BaseStream.Position += 3;
var b = binary.ReadByte();
Expand Down
2 changes: 1 addition & 1 deletion STUN/Attributes/StunEndPointAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class STUNEndPointAttribute : STUNAttribute
{
public IPEndPoint EndPoint { get; set; }

public override void Parse(STUNBinaryReader binary, int length)
public override void Parse(STUNMessage msg, STUNBinaryReader binary, int length)
{
binary.BaseStream.Position++;
var ipFamily = binary.ReadByte();
Expand Down
2 changes: 1 addition & 1 deletion STUN/Attributes/StunErrorCodeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class STUNErrorCodeAttribute : STUNAttribute
public STUNErrorCodes Error { get; set; }
public string Phrase { get; set; }

public override void Parse(STUNBinaryReader binary, int length)
public override void Parse(STUNMessage msg, STUNBinaryReader binary, int length)
{
throw new NotImplementedException();
}
Expand Down
2 changes: 1 addition & 1 deletion STUN/Attributes/StunMessageIntegrityAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace STUN.Attributes
{
public class STUNMessageIntegrityAttribute : STUNAttribute
{
public override void Parse(STUNBinaryReader binary, int length)
public override void Parse(STUNMessage msg, STUNBinaryReader binary, int length)
{
throw new NotImplementedException();
}
Expand Down
2 changes: 1 addition & 1 deletion STUN/Attributes/StunReflectedFromAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace STUN.Attributes
{
public class STUNReflectedFromAttribute : STUNAttribute
{
public override void Parse(STUNBinaryReader binary, int length)
public override void Parse(STUNMessage msg, STUNBinaryReader binary, int length)
{
throw new NotImplementedException();
}
Expand Down
2 changes: 1 addition & 1 deletion STUN/STUN.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net45</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net452</TargetFrameworks>
<Version>0.5.0</Version>
<Authors>Moien007</Authors>
<Product />
Expand Down
2 changes: 1 addition & 1 deletion STUN/STUNAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static STUNAttribute()
AddAttribute<STUNOtherAddressAttribute>(0x802C);
}

public abstract void Parse(STUNBinaryReader binary, int length);
public abstract void Parse(STUNMessage msg, STUNBinaryReader binary, int length);

public virtual void Write(STUNBinaryWriter binary)
{
Expand Down
2 changes: 1 addition & 1 deletion STUN/STUNClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class STUNClient
/// <summary>
/// Period of time in miliseconds to wait for server response.
/// </summary>
public static int ReceiveTimeout = 5000;
public static int ReceiveTimeout = 500;

/// <param name="server">Server address</param>
/// <param name="queryType">Query type</param>
Expand Down
2 changes: 1 addition & 1 deletion STUN/STUNMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void Parse(STUNBinaryReader binary)
if (type != null)
{
var attr = Activator.CreateInstance(type) as STUNAttribute;
attr.Parse(binary, attrLength);
attr.Parse(this, binary, attrLength);
Attributes.Add(attr);
}
else
Expand Down
1 change: 1 addition & 0 deletions STUN/STUNNatMappingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public enum STUNNatMappingBehavior
{
NoMapping,
EndpointIndependentMapping,
AddressDependMapping,
AddressAndPortDependMapping
Expand Down
2 changes: 1 addition & 1 deletion STUN/STUNQueryError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public enum STUNQueryError
/// <summary>
/// Indicates the server didn't response a request within a time interval
/// </summary>
Timedout,
Timeout,
/// <summary>
/// Indicates the server did not support nat detection
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions STUN/STUNQueryResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class STUNQueryResult
/// </summary>
public STUNNATType NATType { get; set; }

public STUNNatFilteringBehavior FilteringBehavior { get; set; }

/// <summary>
/// Contains the public endpoint that queried from server.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions STUN/STUNRfc3489.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static STUNQueryResult Query(Socket socket, IPEndPoint server, STUNQueryT
// didn't receive anything
if (responseBuffer == null)
{
result.QueryError = STUNQueryError.Timedout;
result.QueryError = STUNQueryError.Timeout;
return result;
}

Expand Down Expand Up @@ -240,7 +240,7 @@ public static STUNQueryResult Query(Socket socket, IPEndPoint server, STUNQueryT

if (responseBuffer == null)
{
result.QueryError = STUNQueryError.Timedout;
result.QueryError = STUNQueryError.Timeout;
return result;
}

Expand Down Expand Up @@ -312,7 +312,7 @@ public static STUNQueryResult Query(Socket socket, IPEndPoint server, STUNQueryT

if (!message.TryParse(responseBuffer))
{
result.QueryError = STUNQueryError.Timedout;
result.QueryError = STUNQueryError.Timeout;
return result;
}

Expand Down
Loading