Defines the base class of a network packet. Packets are send back and forth between the client and server. Different types of packets are used for different purposes. The general packet structure is: PACKET_SIZE (2 bytes), PACKET_ID (2 bytes), PACKET_DATA (x bytes). The id bytes are considered to be part of the data bytes. The size bytes are unencrypted, but the id bytes and all data following are encrypted.
Beispiel #1
0
 public string DumpData(Packet p)
 {
     string tmp2 = "";
     for (int i = 0; i < p.data.Length; i++)
     {
         tmp2 += (String.Format("{0:X2} ", p.data[i]));
         if (((i + 1) % 16 == 0) && (i != 0))
         {
             tmp2 += "\r\n";
         }
     }
     return tmp2;
 }
Beispiel #2
0
 public void SendToOneClient(Packet p, string name)
 {
     foreach (MapClient client in this.clients.Values)
     {
         if (client.state == MapClient.SESSION_STATE.IDENTIFIED && name == client.Char.name) client.netIO.SendPacket(p, client.SessionID);
     }
 }
Beispiel #3
0
        public void SendToAllClients(Packet p)
        {
            p.doNotEncryptBuffer = true;

            foreach (MapClient client in this.clients.Values)
            {
                if (client.state == MapClient.SESSION_STATE.IDENTIFIED) client.netIO.SendPacket(p, client.SessionID);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Sends a packet, which is not yet encrypted, to the client.
        /// </summary>
        /// <param name="p">The packet containing all info.</param>
        private void SendPacket(Packet p, uint SessionID, bool nosession)
        {
            p.SetLength();

                try
                {
                    if (this.isGateway)
                    {
                        byte[] data;
                        if (p.doNotEncryptBuffer) data = Encryption.Encrypt((byte[])p.data.Clone(), 2, this.serverKey);
                        else data = Encryption.Encrypt(p.data, 2, this.serverKey);
                        sock.BeginSend(data, 0, data.Length, SocketFlags.None, null, null);
                    }
                    else
                    {
                        if (!nosession)
                        {
                            byte[] data;
                            data = new byte[p.data.Length + 4];
                            Array.Copy(p.data, 2, data, 6, p.data.Length - 2);
                            p.data = data;
                            p.SessionID = SessionID;
                            p.SetLength();
                        }
                        sock.BeginSend(p.data, 0, p.data.Length, SocketFlags.None, null, null);
                        //sock.Send(p.data);
                    }

                }
                catch (Exception)
                {
                    this.Disconnect();
                }
        }
Beispiel #5
0
 public void SendPacket(Packet p, bool nosession)
 {
     SendPacket(p, 0, true);
 }
Beispiel #6
0
 public void SendPacket(Packet p, uint SessionID)
 {
     SendPacket(p, SessionID, false);
 }
Beispiel #7
0
 public void SendPacket(Packet p, bool noWarper)
 {
     SendPacket(p, false, noWarper);
 }
Beispiel #8
0
 public void SendToLogin(byte[] data, uint SessionID)
 {
     Packet send = new Packet(data);
     send.data = data;
     this.netIO.SendPacket(send, SessionID);
 }
 public void OnClientSendGUID()
 {
     SagaLib.Packet send = new Packet(24);
     send.ID = 0x0202;
     this.netIO.SendPacket(send, 0);
 }
 public void SendToLogin(byte[] data)
 {
     Packet send = new Packet(data);
     send.data = data;
     this.netIO.SendPacket(send, 0);
 }
Beispiel #11
0
 private void ReceiveKeyExchange(IAsyncResult ar)
 {
     try
     {
         if (this.isDisconnected)
         {
             return;
         }
         if (!sock.Connected)
         {
             ClientManager.EnterCriticalArea();
             this.Disconnect();
             ClientManager.LeaveCriticalArea();
             return;
         }
         try { stream.EndRead(ar); }
         catch (Exception)
         {
             ClientManager.EnterCriticalArea();
             this.Disconnect();
             ClientManager.LeaveCriticalArea();
             return;
         }
         byte[] raw = (byte[])ar.AsyncState;
         if (raw.Length == 8)
         {
             Packet p1 = new Packet(529);
             p1.PutUInt(1, 4);
             p1.PutByte(0x32, 8);
             p1.PutUInt(0x100, 9);
             Crypt.MakePrivateKey();
             string bufstring = Conversions.bytes2HexString(Encryption.Module.getBytes());
             p1.PutBytes(System.Text.Encoding.ASCII.GetBytes(bufstring.ToLower()), 13);
             p1.PutUInt(0x100, 269);
             bufstring = Conversions.bytes2HexString(Crypt.GetKeyExchangeBytes());
             p1.PutBytes(System.Text.Encoding.ASCII.GetBytes(bufstring), 273);
             SendPacket(p1, true, true);
             try
             {
                 byte[] data = new byte[260];
                 stream.BeginRead(data, 0, 260, this.callbackKeyExchange, data);
             }
             catch (Exception)
             {
                 ClientManager.EnterCriticalArea();
                 this.Disconnect();
                 ClientManager.LeaveCriticalArea();
                 return;
             }
         }
         else if (raw.Length == 260)
         {
             Packet p1 = new Packet();
             p1.data = raw;
             byte[] keyBuf = p1.GetBytes(256, 4);
             Crypt.MakeAESKey(System.Text.Encoding.ASCII.GetString(keyBuf));
             StartPacketParsing();
         }
         else if (raw.Length == 529)
         {
             Packet p1 = new Packet();
             p1.data = raw;
             byte[] keyBuf = p1.GetBytes(256, 273);
             Crypt.MakePrivateKey();
             Packet p2 = new Packet(260);
             p2.PutUInt(0x100, 0);
             string bufstring = Conversions.bytes2HexString(Crypt.GetKeyExchangeBytes());
             p2.PutBytes(System.Text.Encoding.ASCII.GetBytes(bufstring), 4);
             SendPacket(p2, true, true);
             Crypt.MakeAESKey(System.Text.Encoding.ASCII.GetString(keyBuf));
             StartPacketParsing();
         }
     }
     catch (Exception ex)
     {
         Logger.ShowError(ex);
     }
 }
Beispiel #12
0
        private void ReceiveData(IAsyncResult ar)
        {
            try
            {
                if (this.isDisconnected)
                {
                    return;
                }
                if (!sock.Connected)
                {
                    ClientManager.EnterCriticalArea();
                    this.Disconnect();
                    ClientManager.LeaveCriticalArea();
                    return;
                }
                try { stream.EndRead(ar); }
                catch (Exception)
                {
                    ClientManager.EnterCriticalArea();
                    this.Disconnect();
                    ClientManager.LeaveCriticalArea();
                    return;
                }
                byte[] raw = (byte[])ar.AsyncState;
                if (alreadyReceived < lastSize)
                {
                    int left = lastSize - alreadyReceived;
                    if (left > 1024)
                        left = 1024;
                    if (left > sock.Available) left = sock.Available;
                    try
                    {
                        stream.BeginRead(raw, 4 + alreadyReceived, left, this.callbackData, raw);
                    }
                    catch (Exception ex)
                    {
                        Logger.ShowError(ex);
                        ClientManager.EnterCriticalArea();
                        this.Disconnect();
                        ClientManager.LeaveCriticalArea();
                        return;
                    }
                    alreadyReceived += left;
                    return;
                }
                raw = Crypt.Decrypt(raw, 8);

                Packet p = new Packet();
                p.data = raw;
                uint length = p.GetUInt(4);
                uint offset = 0;
                while (offset < length)
                {
                    uint size;
                    if(firstLevelLenth ==4)
                        size= p.GetUInt((ushort)(8 + offset));
                    else
                        size = p.GetUShort((ushort)(8 + offset));

                    offset += firstLevelLenth;
                    if (size + offset > length)
                        break;
                    Packet p2 = new Packet();
                    p2.data = p.GetBytes((ushort)size, (ushort)(8 + offset));
                    offset += size;
                    ProcessPacket(p2);
                }
                try
                {
                    stream.BeginRead(buffer, 0, 4, this.callbackSize, null);
                }
                catch (Exception)
                {
                    ClientManager.EnterCriticalArea();
                    this.Disconnect();
                    ClientManager.LeaveCriticalArea();
                    return;
                }
            }
            catch (Exception e)
            {
                Logger.ShowError(e, null);
            }
        }
Beispiel #13
0
 private void ProcessPacket(Packet p)
 {
     if (commandTable.ContainsKey(p.ID))
     {
         Packet p1 = commandTable[p.ID].New();
         p1.data = p.data;
         p1.size = (ushort)(p.data.Length);
         ClientManager.EnterCriticalArea();
         try
         {
             p1.Parse(this.client);
         }
         catch (Exception ex)
         {
             Logger.ShowError(ex);
         }
         ClientManager.LeaveCriticalArea();
     }
     else
     {
         if (commandTable.ContainsKey(0xFFFF))
         {
             Packet p1 = commandTable[0xFFFF].New();
             p1.data = p.data;
             p1.size = (ushort)(p.data.Length);
             ClientManager.EnterCriticalArea();
             try
             {
                 p1.Parse(this.client);
             }
             catch (Exception ex)
             {
                 Logger.ShowError(ex);
             }
             ClientManager.LeaveCriticalArea();
         }
         else
         {
             Logger.ShowDebug(string.Format("Unknown Packet:0x{0:X4}\r\n       Data:{1}", p.ID, DumpData(p)), Logger.CurrentLogger);
         }
     }
 }
Beispiel #14
0
 public void SendPacket(Packet p)
 {
     SendPacket(p, false);
 }
Beispiel #15
0
 public void OnWeaponUpgrade(Packets.Client.WeaponUpgrade p)
 {
     Weapon weapon = WeaponFactory.GetActiveWeapon(this.Char);
     int price = 0;
     if (weapon.exp >= ExperienceManager.Instance.GetExpForLevel(weapon.level, ExperienceManager.LevelType.WLEVEL))
     {
         for (int i = 1; i <= weapon.level; i++)
         {
             int value = (i / 5) + 1;
             int multpler = value * 25;
             int rest = i % 5;
             if (rest != 0)
                 price += multpler;
             else
             {
                 if ((i % 10) != 0)
                 {
                     int amount = 150 + ((value - 2) / 2) * 300;
                     price += amount;
                 }
                 else
                 {
                     int amount = 400;
                     value = (i / 10);
                     for (int j = 1; j < value; j++)
                     {
                         amount = amount + 300 + 600 * j;
                     }
                     price += amount;
                 }
             }
         }
         weapon.level += 1;
         this.Char.zeny -= (uint)price;
         Packets.Server.WeaponAdjust p1 = new SagaMap.Packets.Server.WeaponAdjust();
         p1.SetFunction(SagaMap.Packets.Server.WeaponAdjust.Function.Level);
         p1.SetValue(weapon.level);
         this.netIO.SendPacket(p1, this.SessionID);
         this.SendZeny();
         this.SendBattleStatus();
         Packet p2 = new Packet();
         p2.data = new byte[5];
         p2.ID = 0x0517;//don't know its function,maybe to close the window?
         this.netIO.SendPacket(p2, this.SessionID);
     }
 }
Beispiel #16
0
        public void Connect()
        {
            bool Connected = false;
            int times = 5;
            do
            {
                if (times < 0)
                {
                    Logger.ShowError("Cannot connect to the server,please check the configuration!", null);
                    return;
                }
                try
                {
                    sock.Connect(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(host), port));
                    Connected = true;
                }
                catch (Exception e)
                {
                    Logger.ShowError("Failed... Trying again in 5sec", null);
                    Logger.ShowError(e.ToString(), null);
                    System.Threading.Thread.Sleep(5000);
                    Connected = false;
                }
                times--;
            } while (!Connected);

            Logger.ShowInfo("Successfully connected to server", null);
            this.state = SESSION_STATE.CONNECTED;
            try
            {
                this.netIO = new NetIO(sock, this.commandTable, this, ProxyClientManager.Instance);
                this.netIO.SetMode(NetIO.Mode.Client);
                this.netIO.FirstLevelLength = this.Client.firstlevel;
                Packet p = new Packet(8);
                p.data[7] = 0x10;
                this.netIO.SendPacket(p, true, true);

            }
            catch (Exception ex)
            {
                Logger.ShowWarning(ex.StackTrace, null);
            }
        }
Beispiel #17
0
 /// <summary>
 /// Sends data, which is not yet encrypted, to the client.
 /// </summary>
 /// <param name="data">The unencrypted data</param>
 public void SendData(byte[] data, uint SessionID)
 {
     Packet p = new Packet(data);
         SendPacket(p, SessionID);
 }
Beispiel #18
0
 public override void OnDie()
 {
     base.OnDie();
     if( delay != -1 )
         respawnTask.Activate();
     corpsetask.Activate();
     ai.Pause();
     if( this.map.GetActor( this.timeSignature.actorID ) != null )
     {
         ActorPC pc = (ActorPC)this.map.GetActor( this.timeSignature.actorID );
         ActorEventHandlers.PC_EventHandler eh = (SagaMap.ActorEventHandlers.PC_EventHandler)pc.e;
         Packet p = new Packet();//don't know its name,maybe for some animation.
         p.data = new byte[9];
         p.ID = 0x060E;
         p.PutUInt( this.Actor.id, 4 );
         p.PutByte( 4, 8 );
         eh.C.netIO.SendPacket(p, eh.C.SessionID);
         SagaDB.Quest.Quest quest = Quest.QuestsManager.GetActiveQuest( pc );
         if( quest != null )//Add a temporary loot for a specificial quest
         {
             if( Quest.QuestsManager.MobQuestItem.ContainsKey( this.Actor.npcType ) )
             {
                 foreach( Quest.QuestsManager.LootInfo i in Quest.QuestsManager.MobQuestItem[this.Actor.npcType] )
                 {
                     if( i.QID == quest.ID )
                     {
                         if( quest.Steps.ContainsKey( i.SID ) )
                         {
                             if( quest.Steps[i.SID].Status == 1 )
                             {
                                 if( this.Actor.NPCinv == null ) Actor.NPCinv = new List<Item>();
                                 int j = Global.Random.Next( 0, 9999 );
                                 if( j < i.rate )
                                     this.Actor.NPCinv.Add( new Item( i.itemID ) );
                                 return;
                             }
                         }
                     }
                 }
             }
         }
         /*if (eh.C.QuestMobItem != null)
         {
             if (eh.C.QuestMobItem.ContainsKey(this.Actor.npcType))
             {
                 if (this.Actor.NPCinv == null) Actor.NPCinv = new List<Item>();
                 this.Actor.NPCinv.Add(ItemFactory.GetItem((int)eh.C.QuestMobItem[this.Actor.npcType]));
             }
         }*/
     }
 }
Beispiel #19
0
        /// <summary>
        /// Sends a packet, which is not yet encrypted, to the client.
        /// </summary>
        /// <param name="p">The packet containing all info.</param>
        public void SendPacket(Packet p, bool nolength, bool noWarper)
        {
            if (!noWarper)
            {
                byte[] buf = new byte[p.data.Length + firstLevelLenth];
                Array.Copy(p.data, 0, buf, firstLevelLenth, p.data.Length);
                p.data = buf;
                if (firstLevelLenth == 4)
                    p.SetLength();
                else
                    p.PutUShort((ushort)(p.data.Length - 2), 0);
                buf = new byte[p.data.Length + 4];
                Array.Copy(p.data, 0, buf, 4, p.data.Length);
                p.data = buf;
                p.SetLength();
                buf = new byte[p.data.Length + 4];
                Array.Copy(p.data, 0, buf, 4, p.data.Length);
                p.data = buf;
            }
            if (!nolength)
            {
                int mod = 16-((p.data.Length - 8) % 16);
                if (mod != 0)
                {
                    byte[] buf = new byte[p.data.Length + mod];
                    Array.Copy(p.data, 0, buf, 0, p.data.Length);
                    p.data = buf;
                }
                p.PutUInt((uint)(p.data.Length - 8), 0);
            }

            try
            {
                byte[] data;
                data = Crypt.Encrypt(p.data, 8);
                sock.BeginSend(data, 0, data.Length, SocketFlags.None, null, null);
            }
            catch (Exception ex)
            {
                Logger.ShowError(ex);
                this.Disconnect();
            }
        }