public void GetKey(ref Workstation station) { NetworkMessage Sendmsg = new NetworkMessage(); Sendmsg.label = PackageLabel.getkey; Sendmsg.TimeStamp = DateTime.Now; Sendmsg.NextIP = basic.CurrentIP; Sendmsg.Data = null; byte[] Data = Serialization(Sendmsg); byte[] key = new byte[100000]; try { Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sock.Connect(station.IPAddr, basic.port); sock.Send(Data, Data.Length, SocketFlags.None); sock.Receive(key, SocketFlags.None); sock.Close(); } catch (SocketException ex) { MessageBox.Show(ex.Message, "Ошибка при отправке пакета с запросом на ключ", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void Handler(NetworkMessage s, IPAddress ip) { //MessageBox.Show("Мы знаем, что пришло сообщение : " + s); switch (s.label) { case (PackageLabel.final): { foreach (Chat C in AllChats) { if (ip.ToString() == C.CurrentWorkstation.IPAddr.ToString()) { C.ShowString(Encoding.UTF8.GetString(s.Data)); return; } } // Workstation SetCurrentWorkstation; for (int i = 0; i < client.workstations.Count; i++) { if (client.workstations[i].IPAddr.ToString() == ip.ToString()) { client.workstations[i].buffer += Encoding.UTF8.GetString(s.Data) + "\r\n"; client.workstations[i].couner++; } } break; } case (PackageLabel.openkey): { for (int i = 0; i < client.workstations.Count; i++) { if (client.workstations[i].IPAddr.ToString() == ip.ToString()) { client.workstations[i].publickey = Encoding.UTF8.GetString(s.Data); } } break; } }//switch }
public void SendString(String str, Workstation IP) { Random rnd = new Random(); log.printlog("[send] IP: " + IP.IPAddr.ToString() + " msg: " + str); /*Разметка приемочного пакета*/ NetworkMessage Sendmsg = new NetworkMessage(); Sendmsg.label = PackageLabel.final; //Финальный пакет Sendmsg.TimeStamp = DateTime.Now; //Будет иметь временной штамп, Sendmsg.NextIP = basic.CurrentIP; //IP отправителя (то есть текущий) Encoding Utf8 = Encoding.UTF8; Sendmsg.Data = Utf8.GetBytes(str); //и данные. /*Этот пакет должен иметь "транзитника последней мили" чтобы адес отправителя всегда был равен нужному приемнику*/ String TunnelPath = ""; NetworkMessage Buffer = new NetworkMessage(); Buffer.Data = RSAEncrypt(Serialization(Sendmsg), IP.publickey); Buffer.TimeStamp = DateTime.Now; Buffer.label = PackageLabel.transit; Buffer.NextIP = IP.IPAddr; TunnelPath += IP.IPAddr.ToString() + " "; Sendmsg = Buffer; for (int i = 0; i < layernum-1; i++) { int rand = rnd.Next(workstations.Count); Buffer.Data = RSAEncrypt(Serialization(Sendmsg), workstations[rand].publickey); Buffer.TimeStamp = DateTime.Now; Buffer.label = PackageLabel.transit; Buffer.NextIP = workstations[rand].IPAddr; TunnelPath += workstations[rand].IPAddr.ToString() + " "; Sendmsg = Buffer; } int rnd_next = rnd.Next(workstations.Count); //MessageBox.Show("Сообщение готовится к отправке по туннелю"); basic.SendNext(workstations[rnd_next].IPAddr, RSAEncrypt(Serialization(Sendmsg), workstations[rnd_next].publickey), log); log.printlog("[tunnel] size: " + layernum.ToString() + " path: " + TunnelPath + " " + workstations[rnd_next].IPAddr.ToString()); }
public void SendNonEncryptMessage(NetworkMessage msg, IPAddress IP) { basic.SendNext(IP, Serialization(msg), log); }