/*
* Copyright (c) 2006, Second Life Reverse Engineering Team
* All rights reserved.
*
* - Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Neither the name of the Second Life Reverse Engineering Team nor the names
* of its contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Text;
using System.Timers;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using Nwc.XmlRpc;
using Nii.JSON;
using libsecondlife.Packets;
namespace libsecondlife
{
///
///
///
///
///
public delegate void PacketCallback(Packet packet, Simulator simulator);
///
///
///
///
///
public delegate void SimDisconnectCallback(Simulator simulator, DisconnectType reason);
///
///
///
///
///
public delegate void DisconnectCallback(DisconnectType reason, string message);
///
///
///
public enum DisconnectType
{
///
ClientInitiated,
///
ServerInitiated,
///
NetworkTimeout
}
///
/// This exception is thrown whenever a network operation is attempted
/// without a network connection.
///
public class NotConnectedException : ApplicationException { }
///
/// Simulator is a wrapper for a network connection to a simulator and the
/// Region class representing the block of land in the metaverse.
///
public class Simulator
{
///
/// The Region class that this Simulator wraps
///
public Region Region;
///
/// The ID number associated with this particular connection to the
/// simulator, used to emulate TCP connections. This is used
/// internally for packets that have a CircuitCode field.
///
public uint CircuitCode
{
get { return circuitCode; }
set { circuitCode = value; }
}
///
/// The IP address and port of the server.
///
public IPEndPoint IPEndPoint
{
get { return ipEndPoint; }
}
///
/// A boolean representing whether there is a working connection to the
/// simulator or not.
///
public bool Connected
{
get { return connected; }
}
///
/// Used internally to track sim disconnections, do not modify this
/// variable.
///
public bool DisconnectCandidate;
private SecondLife Client;
private NetworkManager Network;
private Dictionary> Callbacks;
private ushort Sequence;
private byte[] RecvBuffer;
private Socket Connection;
private AsyncCallback ReceivedData;
private Dictionary NeedAck;
private SortedList Inbox;
private List PendingAcks;
private bool connected;
private uint circuitCode;
private IPEndPoint ipEndPoint;
private EndPoint endPoint;
private System.Timers.Timer AckTimer;
///
///
///
///
///
///
///
///
public Simulator(SecondLife client, Dictionary> callbacks, uint circuit,
IPAddress ip, int port)
{
Client = client;
Network = client.Network;
Callbacks = callbacks;
Region = new Region(client);
circuitCode = circuit;
Sequence = 0;
RecvBuffer = new byte[2048];
Connection = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
connected = false;
DisconnectCandidate = false;
AckTimer = new System.Timers.Timer(500);
AckTimer.Elapsed += new ElapsedEventHandler(AckTimer_Elapsed);
// Initialize the dictionary for reliable packets waiting on ACKs from the server
NeedAck = new Dictionary();
// Initialize the lists of sequence numbers we've received so far
Inbox = new SortedList();
PendingAcks = new List();
Client.Log("Connecting to " + ip.ToString() + ":" + port, Helpers.LogLevel.Info);
try
{
// Setup the callback
ReceivedData = new AsyncCallback(OnReceivedData);
// Create an endpoint that we will be communicating with (need it in two
// types due to .NET weirdness)
ipEndPoint = new IPEndPoint(ip, port);
endPoint = (EndPoint)ipEndPoint;
// Associate this simulator's socket with the given ip/port and start listening
Connection.Connect(endPoint);
Connection.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref endPoint, ReceivedData, null);
// Send the UseCircuitCode packet to initiate the connection
UseCircuitCodePacket use = new UseCircuitCodePacket();
use.CircuitCode.Code = circuitCode;
use.CircuitCode.ID = Network.AgentID;
use.CircuitCode.SessionID = Network.SessionID;
// Start the ACK timer
AckTimer.Start();
// Send the initial packet out
SendPacket(use, true);
// Track the current time for timeout purposes
int start = Environment.TickCount;
while (true)
{
if (connected || Environment.TickCount - start > 8000)
{
return;
}
System.Threading.Thread.Sleep(10);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Client.Log(e.ToString(), Helpers.LogLevel.Error);
}
}
///
///
///
public void Disconnect()
{
// Send the CloseCircuit notice
CloseCircuitPacket close = new CloseCircuitPacket();
try
{
Connection.Send(close.ToBytes());
}
catch (SocketException)
{
// There's a high probability of this failing if the network is
// disconnected, so don't even bother logging the error
}
try
{
// Shut the socket communication down
Connection.Shutdown(SocketShutdown.Both);
}
catch (SocketException e)
{
Client.Log(e.ToString(), Helpers.LogLevel.Error);
}
connected = false;
}
///
///
///
///
///
public void SendPacket(Packet packet, bool incrementSequence)
{
byte[] buffer;
int bytes;
if (!connected && packet.Type != PacketType.UseCircuitCode)
{
Client.Log("Trying to send a " + packet.Type.ToString() + " packet when the socket is closed",
Helpers.LogLevel.Warning);
throw new NotConnectedException();
}
if (incrementSequence)
{
// Set the sequence number here since we are manually serializing the packet
packet.Header.Sequence = ++Sequence;
if (packet.Header.Reliable)
{
// Keep track of when this packet was sent out
packet.TickCount = Environment.TickCount;
lock (NeedAck)
{
if (!NeedAck.ContainsKey(packet.Header.Sequence))
{
NeedAck.Add(packet.Header.Sequence, packet);
}
else
{
Client.Log("Attempted to add a duplicate sequence number (" +
packet.Header.Sequence + ") to the NeedAck dictionary for packet type " +
packet.Type.ToString(), Helpers.LogLevel.Warning);
}
}
// Append any ACKs that need to be sent out to this packet
lock (PendingAcks)
{
if (PendingAcks.Count > 0 && packet.Type != PacketType.PacketAck &&
packet.Type != PacketType.LogoutRequest)
{
packet.Header.AckList = new uint[PendingAcks.Count];
int i = 0;
foreach (uint ack in PendingAcks)
{
packet.Header.AckList[i] = ack;
i++;
}
PendingAcks.Clear();
packet.Header.AppendedAcks = true;
}
}
}
}
// Serialize the packet
buffer = packet.ToBytes();
bytes = buffer.Length;
// Zerocode if needed
if (packet.Header.Zerocoded)
{
byte[] zeroBuffer = new byte[4096];
bytes = Helpers.ZeroEncode(buffer, bytes, zeroBuffer);
buffer = zeroBuffer;
}
try
{
Connection.Send(buffer, bytes, SocketFlags.None);
}
catch (SocketException e)
{
Client.Log(e.ToString(), Helpers.LogLevel.Error);
}
}
///
///
///
///
public void SendPacket(byte[] payload)
{
if (!connected)
{
throw new NotConnectedException();
}
try
{
Connection.Send(payload, payload.Length, SocketFlags.None);
}
catch (SocketException e)
{
Client.Log(e.ToString(), Helpers.LogLevel.Error);
}
}
private void OnReceivedData(IAsyncResult result)
{
Packet packet = null;
int numBytes;
// If we're receiving data the sim connection is open
connected = true;
// Update the disconnect flag so this sim doesn't time out
DisconnectCandidate = false;
lock (RecvBuffer)
{
// Retrieve the incoming packet
try
{
numBytes = Connection.EndReceiveFrom(result, ref endPoint);
int packetEnd = numBytes - 1;
packet = Packet.BuildPacket(RecvBuffer, ref packetEnd);
Connection.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref endPoint, ReceivedData, null);
}
catch (SocketException)
{
Client.Log(endPoint.ToString() + " socket is closed, shutting down " + this.Region.Name,
Helpers.LogLevel.Info);
connected = false;
Network.DisconnectSim(this);
return;
}
}
// Fail-safe check
if (packet == null)
{
Client.Log("Couldn't build a message from the incoming data", Helpers.LogLevel.Warning);
return;
}
// Track the sequence number for this packet if it's marked as reliable
if (packet.Header.Reliable)
{
// Check if we already received this packet
lock (Inbox)
{
if (Inbox.ContainsKey(packet.Header.Sequence))
{
Client.Log("Received a duplicate " + packet.Type.ToString() + ", sequence=" +
packet.Header.Sequence + ", resent=" + ((packet.Header.Resent) ? "Yes" : "No"),
Helpers.LogLevel.Info);
// Avoid firing a callback twice for the same packet
return;
}
else
{
Inbox.Add(packet.Header.Sequence, packet.Header.Sequence);
PendingAcks.Add((uint)packet.Header.Sequence);
}
}
}
// Handle appended ACKs
if (packet.Header.AppendedAcks)
{
lock (NeedAck)
{
foreach (ushort ack in packet.Header.AckList)
{
if (NeedAck.ContainsKey(ack))
{
NeedAck.Remove(ack);
}
else
{
Client.Log("Appended ACK for a packet we didn't send: " + ack, Helpers.LogLevel.Warning);
}
}
}
}
// Handle PacketAck packets
if (packet.Type == PacketType.PacketAck)
{
lock (NeedAck)
{
PacketAckPacket ackPacket = (PacketAckPacket)packet;
foreach (PacketAckPacket.PacketsBlock block in ackPacket.Packets)
{
NeedAck.Remove((ushort)block.ID);
}
}
}
// Fire the registered packet events
#region FireCallbacks
try
{
if (Callbacks.ContainsKey(packet.Type))
{
List callbackArray = Callbacks[packet.Type];
// Fire any registered callbacks
foreach (PacketCallback callback in callbackArray)
{
if (callback != null)
{
callback(packet, this);
}
}
}
if (Callbacks.ContainsKey(PacketType.Default))
{
List callbackArray = Callbacks[PacketType.Default];
// Fire any registered callbacks
foreach (PacketCallback callback in callbackArray)
{
if (callback != null)
{
callback(packet, this);
}
}
}
}
catch (Exception e)
{
Client.Log("Caught an exception in a packet callback: " + e.ToString(), Helpers.LogLevel.Warning);
}
#endregion FireCallbacks
}
private void AckTimer_Elapsed(object sender, ElapsedEventArgs ea)
{
if (!connected)
{
AckTimer.Stop();
return;
}
lock (PendingAcks)
{
if (PendingAcks.Count > 0)
{
int i = 0;
PacketAckPacket acks = new PacketAckPacket();
acks.Packets = new PacketAckPacket.PacketsBlock[PendingAcks.Count];
acks.Header.Reliable = false;
foreach (uint ack in PendingAcks)
{
acks.Packets[i] = new PacketAckPacket.PacketsBlock();
acks.Packets[i].ID = ack;
i++;
}
SendPacket(acks, true);
PendingAcks.Clear();
}
}
}
}
///
/// NetworkManager is responsible for managing the network layer of
/// libsecondlife. It tracks all the server connections, serializes
/// outgoing traffic and deserializes incoming traffic, and provides
/// instances of delegates for network-related events.
///
public class NetworkManager
{
///
/// The permanent UUID for the logged in avatar
///
public LLUUID AgentID;
///
/// A temporary UUID assigned to this session, used for secure
/// transactions
///
public LLUUID SessionID;
///
/// A string holding a descriptive error on login failure, empty
/// otherwise
///
public string LoginError;
///
/// The simulator that the logged in avatar is currently occupying
///
public Simulator CurrentSim;
///
/// The complete dictionary of all the login values returned by the
/// RPC login server, converted to native data types wherever possible
///
public Dictionary LoginValues;
///
/// Shows whether the network layer is logged in to the grid or not
///
public bool Connected
{
get { return connected; }
}
///
/// An event for the connection to a simulator other than the currently
/// occupied one disconnecting
///
public SimDisconnectCallback OnSimDisconnected;
///
/// An event for being logged out either through client request, server
/// forced, or network error
///
public DisconnectCallback OnDisconnected;
private Dictionary> Callbacks;
private SecondLife Client;
private List Simulators;
private System.Timers.Timer DisconnectTimer;
private bool connected;
///
///
///
///
public NetworkManager(SecondLife client)
{
Client = client;
Simulators = new List();
Callbacks = new Dictionary>();
CurrentSim = null;
LoginValues = null;
// Register the internal callbacks
RegisterCallback(PacketType.RegionHandshake, new PacketCallback(RegionHandshakeHandler));
RegisterCallback(PacketType.StartPingCheck, new PacketCallback(StartPingCheckHandler));
RegisterCallback(PacketType.ParcelOverlay, new PacketCallback(ParcelOverlayHandler));
RegisterCallback(PacketType.EnableSimulator, new PacketCallback(EnableSimulatorHandler));
RegisterCallback(PacketType.KickUser, new PacketCallback(KickUserHandler));
// Disconnect a sim if no network traffic has been received for 15 seconds
DisconnectTimer = new System.Timers.Timer(15000);
DisconnectTimer.Elapsed += new ElapsedEventHandler(DisconnectTimer_Elapsed);
}
///
///
///
///
///
public void RegisterCallback(PacketType type, PacketCallback callback)
{
if (!Callbacks.ContainsKey(type))
{
Callbacks[type] = new List();
}
List callbackArray = Callbacks[type];
callbackArray.Add(callback);
}
///
///
///
///
///
public void UnregisterCallback(PacketType type, PacketCallback callback)
{
if (!Callbacks.ContainsKey(type))
{
Client.Log("Trying to unregister a callback for packet " + type.ToString() +
" when no callbacks are setup for that packet", Helpers.LogLevel.Info);
return;
}
List callbackArray = Callbacks[type];
if (callbackArray.Contains(callback))
{
callbackArray.Remove(callback);
}
else
{
Client.Log("Trying to unregister a non-existant callback for packet " + type.ToString(),
Helpers.LogLevel.Info);
}
}
///
///
///
///
public void SendPacket(Packet packet)
{
if (CurrentSim != null && CurrentSim.Connected)
{
CurrentSim.SendPacket(packet, true);
}
}
///
///
///
///
///
public void SendPacket(Packet packet, Simulator simulator)
{
if (simulator.Connected)
{
simulator.SendPacket(packet, true);
}
}
///
///
///
///
public void SendPacket(byte[] payload)
{
if (CurrentSim != null)
{
CurrentSim.SendPacket(payload);
}
else
{
throw new NotConnectedException();
}
}
///
///
///
///
///
///
///
///
///
public static Dictionary DefaultLoginValues(
string firstName, string lastName, string password, string userAgent, string author)
{
return DefaultLoginValues(firstName, lastName, password, "00:00:00:00:00:00", "last",
1, 50, 50, 50, "Win", "0", userAgent, author);
}
public static Dictionary DefaultLoginValues(string firstName,
string lastName, string password, string mac, string startLocation, string platform,
string viewerDigest, string userAgent, string author)
{
return DefaultLoginValues(firstName, lastName, password, mac, startLocation,
1, 50, 50, 50, platform, viewerDigest, userAgent, author);
}
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
public static Dictionary DefaultLoginValues(string firstName,
string lastName, string password, string mac, string startLocation, int major, int minor,
int patch, int build, string platform, string viewerDigest, string userAgent, string author)
{
Dictionary values = new Dictionary();
// Generate an MD5 hash of the password
MD5 md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(Encoding.ASCII.GetBytes(password));
StringBuilder passwordDigest = new StringBuilder();
// Convert the hash to a hex string
foreach(byte b in hash)
{
passwordDigest.AppendFormat("{0:x2}", b);
}
values["first"] = firstName;
values["last"] = lastName;
values["passwd"] = "$1$" + passwordDigest;
values["start"] = startLocation;
values["major"] = major;
values["minor"] = minor;
values["patch"] = patch;
values["build"] = build;
values["platform"] = platform;
values["mac"] = mac;
values["agree_to_tos"] = "true";
values["viewer_digest"] = viewerDigest;
values["user-agent"] = userAgent + " (" + Helpers.VERSION + ")";
values["author"] = author;
// Build the options array
List