private TurnMessage ProcessAllocateRequest(ref Allocation allocation, TurnMessage request, ServerEndPoint local, IPEndPoint remote)
{
uint sequenceNumber = (request.MsSequenceNumber != null) ? request.MsSequenceNumber.SequenceNumber : 0;
{
uint lifetime = (request.Lifetime != null) ? ((request.Lifetime.Value > MaxLifetime.Seconds) ? MaxLifetime.Seconds : request.Lifetime.Value) : DefaultLifetime.Seconds;
if (allocation != null)
{
allocation.Lifetime = lifetime;
if (lifetime == 0)
logger.WriteInformation(string.Format("Update Allocation: {2} seconds {0} <--> {1}", allocation.Alocated.ToString(), allocation.Reflexive.ToString(), lifetime));
}
else
{
if (lifetime <= 0)
throw new TurnServerException(ErrorCode.NoBinding);
ProtocolPort pp = new ProtocolPort() { Protocol = local.Protocol, };
if (peerServer.Bind(ref pp) != SocketError.Success)
throw new TurnServerException(ErrorCode.ServerError);
allocation = new Allocation()
{
TransactionId = request.TransactionId,
ConnectionId = ConnectionIdGenerator.Generate(local, remote),
Local = local,
Alocated = new ServerEndPoint(pp, PublicIp),
Real = new ServerEndPoint(pp, RealIp),
Reflexive = new IPEndPoint(remote.Address, remote.Port),
Lifetime = lifetime,
};
allocations.Replace(allocation);
logger.WriteInformation(string.Format("Allocated: {0} <--> {1} for {2} seconds", allocation.Alocated.ToString(), allocation.Reflexive.ToString(), allocation.Lifetime));
}
}
return new TurnMessage()
{
IsAttributePaddingDisabled = true,
MessageType = MessageType.AllocateResponse,
TransactionId = request.TransactionId,
MagicCookie = new MagicCookie(),
MappedAddress = new MappedAddress()
{
IpAddress = allocation.Alocated.Address,
Port = (UInt16)allocation.Alocated.Port,
},
Lifetime = new Lifetime() { Value = allocation.Lifetime, },
Bandwidth = new Bandwidth() { Value = 750, },
XorMappedAddress = new XorMappedAddress(TurnMessageRfc.MsTurn)
{
IpAddress = remote.Address,
Port = (UInt16)remote.Port,
},
Realm = new Realm(TurnMessageRfc.MsTurn)
{
Ignore = true,
Value = Authentificater.Realm,
},
MsUsername = new MsUsername()
{
Ignore = true,
Value = request.MsUsername.Value,
},
//MsUsername = allocation.Username,
MessageIntegrity = new MessageIntegrity(),
MsSequenceNumber = new MsSequenceNumber()
{
ConnectionId = allocation.ConnectionId,
SequenceNumber = sequenceNumber,//allocation.SequenceNumber,
},
};
}