Exemplo n.º 1
0
 public static void send_to(IPAddress target_url,Packet packet)
 {
     Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     s.Connect(new IPEndPoint(target_url, 2333));
     s.Send(packet.to_bytes());
     s.Close();
     if (DEBUG)
         Console.WriteLine("[LOG][{0}]: send a {1} packet which uuid is {2} to {3}",DateTime.Now.ToString(),Encoding.UTF8.GetString(packet.get_command_string()),Encoding.UTF8.GetString(packet.Packet_uuid) ,target_url.ToString());
 }
Exemplo n.º 2
0
 public static bool __confirm_packet(Packet confirmation_packet)
 {
     if (utils.Utils.bytes2int(confirmation_packet.Version) != 0x0209)
         throw new packet.VersionExcepion();
     if (command_list.Contains(Encoding.UTF8.GetString(confirmation_packet.get_command_string())) == false)
         throw new packet.CommandException();
     //byte[] check_bytes = Encoding.UTF8.GetBytes(utils.HashHelper.Hash_SHA_256(Encoding.UTF8.GetString(confirmation_packet.Payload)));
     SHA256 sha256 = new SHA256CryptoServiceProvider();
     byte[] check_bytes = sha256.ComputeHash(confirmation_packet.Payload);
     byte[] check_bytes_8 = new byte[8];
     Buffer.BlockCopy(check_bytes, 0, check_bytes_8, 0, 8);
     if (Encoding.UTF8.GetString(check_bytes_8) != Encoding.UTF8.GetString(confirmation_packet.Checksum))
     {
         throw new packet.ChecksumExcepiton();
     }
     return true;
 }
Exemplo n.º 3
0
 public bool __eq__(Packet p)
 {
     if (this.version != p.version)
         return false;
     if (this.command != p.command)
         return false;
     if (this.length != p.length)
         return false;
     if (this.packet_uuid != p.packet_uuid)
         return false;
     if (this.checksum != p.checksum)
         return false;
     if (this.payload != p.payload)
         return false;
     return true;
 }
Exemplo n.º 4
0
        public static Packet gen_packet(byte[] com,byte[] payl)
        {
            byte[] version = utils.Utils.int2bytes(0x0209);
            byte[] command = utils.Utils.gen_command_bytes_array(com);
            byte[] length = utils.Utils.int2bytes(payl.Length);
            byte[] packet_uuid = Guid.NewGuid().ToByteArray();

            SHA256 sha256 = new SHA256CryptoServiceProvider();
            byte[] checksum_temp = sha256.ComputeHash(payl);
            byte[] checksum = new byte[8];
            Buffer.BlockCopy(checksum_temp, 0, checksum, 0, 8);

            Packet p = new Packet(version, command, length, packet_uuid, checksum, payl);
            return p;
        }
Exemplo n.º 5
0
        public static Packet from_bytes(byte[] bytes_array)
        {
            byte[] version = new byte[4];
            byte[] command = new byte[8];
            byte[] length = new byte[4];
            byte[] packet_uuid = new byte[16];
            byte[] checksum = new byte[8];

            Buffer.BlockCopy(bytes_array, 0, version, 0, 4);
            Buffer.BlockCopy(bytes_array, 4, command, 0, 8);
            Buffer.BlockCopy(bytes_array, 12, length, 0, 4);
            Buffer.BlockCopy(bytes_array, 16, packet_uuid, 0, 16);
            Buffer.BlockCopy(bytes_array, 32, checksum, 0, 8);
            byte[] payload = new byte[Utils.bytes2int(length)];
            Buffer.BlockCopy(bytes_array, 40, payload, 0, Utils.bytes2int(length));
            Packet p = new Packet(version,command,length,packet_uuid,checksum,payload);
            return p;
        }
Exemplo n.º 6
0
        public void __handle_packet(IPAddress route_address, Packet now_packet)
        {
            byte[] command = now_packet.get_command_string();
            if (Encoding.UTF8.GetString(command) == "POST")
            {
                JsonObject decrypt_post = JsonConvert.DeserializeObject<JsonObject>(Encoding.UTF8.GetString(Utils.json_decrypt(SECRET_KEY.secret_key, now_packet.Payload)));
                this.__send_get_data_packet(route_address, decrypt_post.uuid);
            }
            else if (Encoding.UTF8.GetString(command) == "ACK")
            {
                JsonObject decrypt_ack = JsonConvert.DeserializeObject<JsonObject>(Encoding.UTF8.GetString(Utils.json_decrypt(SECRET_KEY.secret_key, now_packet.Payload)));
                if (DEBUG)
                {
                    Console.WriteLine("[LOG][{0}]: the data packet {1} has been received by {2}!", DateTime.Now.ToString(), decrypt_ack.uuid, route_address);
                }
                if (__caching_dict.ContainsKey(Encoding.UTF8.GetString(decrypt_ack.uuid)))
                {
                    this.__caching_dict.Remove(Encoding.UTF8.GetString(decrypt_ack.uuid));
                }
            }

            else if (Encoding.UTF8.GetString(command) == "GETDATA")
            {
                JsonObject decrypt_getdata = JsonConvert.DeserializeObject<JsonObject>(Encoding.UTF8.GetString(Utils.json_decrypt(SECRET_KEY.secret_key, now_packet.Payload)));
                this.__sending_queue.Enqueue(Tuple.Create<IPAddress, Packet>(route_address, this.__caching_dict[Encoding.UTF8.GetString(decrypt_getdata.uuid)]));
            }
            else if (Encoding.UTF8.GetString(command) == "DATA")
            {
                JsonDataObject decrypt_data = JsonConvert.DeserializeObject<JsonDataObject>(Encoding.UTF8.GetString(Utils.json_decrypt(SECRET_KEY.secret_key, now_packet.Payload)));
                Console.WriteLine("to do functions");

                Console.WriteLine(decrypt_data.command);
                if(decrypt_data.command == "get_plane_number")
                {
                    JsonDataObject js = new JsonDataObject();
                    js.command = "plane_number";
                    Dictionary<string, string> d = new Dictionary<string, string>();
                    d.Add("plane_state", "all");
                    d.Add("plane_number", "5");
                    js.options = d;
                    IPAddress ip = IPAddress.Parse(ROUTE_ADDRESS.route_address);
                    send_data_packet(ip, js);
                }
                if(decrypt_data.command == "get_plane_location")
                {
                    JsonDataObject js = new JsonDataObject();
                    js.command = "plane_location";
                    Dictionary<string, string> d = new Dictionary<string, string>();
                    d.Add("plane_id", "6077");
                    d.Add("plane_location_x", "126.632837");
                    d.Add("plane_location_y", "45.744368");
                    d.Add("plane_location_z", "3");
                    js.options = d;
                    IPAddress ip = IPAddress.Parse(ROUTE_ADDRESS.route_address);
                    send_data_packet(ip, js);
                }

            }
        }