public int WriteToFile(G2Packet packet, Stream stream) { byte[] data = packet.Encode(this); stream.Write(data, 0, data.Length); return(data.Length); }
public int WritePacket(G2Packet packet) { byte[] data = packet.Encode(Protocol); ParentStream.Write(data, 0, data.Length); return(data.Length); }
public int SendPacket(G2Packet packet) { if (Core.InvokeRequired) Debug.Assert(false); if (State != TcpState.Connected) return 0; // usually when an inbound connection (dont know remote userId) is determined to be a loopback, we close the connection // even before the userId is set, if the userId is not set then the encryptor cant be init'd to send the 'close' packet if (UserID == 0) return 0; if (Core.Sim == null || Core.Sim.Internet.TestEncryption) if(Encryptor == null) CreateEncryptor(); try { if (packet is NetworkPacket) { ((NetworkPacket)packet).SourceID = Network.Local.UserID; ((NetworkPacket)packet).ClientID = Network.Local.ClientID; } byte[] encoded = packet.Encode(Network.Protocol); PacketLogEntry logEntry = new PacketLogEntry(Core.TimeNow, TransportProtocol.Tcp, DirectionType.Out, new DhtAddress(RemoteIP, this), encoded); Network.LogPacket(logEntry); lock(FinalSendBuffer) { // fill up final buffer, keep encrypt buffer clear if (BUFF_SIZE - FinalSendBuffSize < encoded.Length + 128) throw new Exception("SendBuff Full"); //crit check packet log // encrypt, turn off encryption during simulation if (Core.Sim == null || Core.Sim.Internet.TestEncryption) { encoded.CopyTo(SendBuffer, SendBuffSize); SendBuffSize += encoded.Length; int remainder = SendBuffSize % Encryptor.InputBlockSize; if (remainder > 0) { CryptPadding padding = new CryptPadding(); int fillerNeeded = Encryptor.InputBlockSize - remainder; if (fillerNeeded > 2) padding.Filler = new byte[fillerNeeded - 2]; encoded = padding.Encode(Network.Protocol); encoded.CopyTo(SendBuffer, SendBuffSize); SendBuffSize += encoded.Length; } int tryTransform = SendBuffSize - (SendBuffSize % Encryptor.InputBlockSize); if (tryTransform == 0) return 0; int tranformed = Encryptor.TransformBlock(SendBuffer, 0, tryTransform, FinalSendBuffer, FinalSendBuffSize); if (tranformed == 0) return 0; FinalSendBuffSize += tranformed; SendBuffSize -= tranformed; Buffer.BlockCopy(SendBuffer, tranformed, SendBuffer, 0, SendBuffSize); } else { encoded.CopyTo(FinalSendBuffer, FinalSendBuffSize); FinalSendBuffSize += encoded.Length; } } TrySend(); // record bandwidth return encoded.Length; } catch(Exception ex) { LogException("SendPacket", ex.Message); } return 0; }
public int SendPacket(DhtAddress contact, G2Packet packet) { if (contact.TunnelServer != null) return SendTunnelPacket(contact, packet); else return UdpControl.SendTo(contact, packet); }
// nodes in lookup proxy mode are psuedo-open, instead of udp they send tunneled packets // tunnel packets include routing information to the lookup target as well as // the encrytped operation packet embedded in the payload public int SendTunnelPacket(DhtAddress contact, G2Packet embed) { Debug.Assert(contact.TunnelClient != null && contact.TunnelServer != null); Debug.Assert(Core.Context.Lookup != null); Debug.Assert(!IsLookup); Debug.Assert(Core.User.Settings.OpAccess != AccessType.Secret); if (IsLookup || Core.Context.Lookup == null || Core.User.Settings.OpAccess == AccessType.Secret) return 0; OpCore lookup = Core.Context.Lookup; // tunnel packet through lookup network byte[] encoded = embed.Encode(Protocol); PacketLogEntry logEntry = new PacketLogEntry(Core.TimeNow, TransportProtocol.Tunnel, DirectionType.Out, contact, encoded); LogPacket(logEntry); TunnelPacket packet = new TunnelPacket(); // encrypt, turn off encryption during simulation if (Core.Sim == null || Core.Sim.Internet.TestEncryption) packet.Payload = Utilities.EncryptBytes(encoded, GetAugmentedKey(contact.UserID)); else packet.Payload = encoded; packet.Source = new TunnelAddress(lookup.Network.Local, Core.TunnelID); packet.Target = contact.TunnelClient; int bytesSent = 0; // if we are the tunnel server (our lookup net is open, but op is blocked) if (lookup.Network.Local.Equals(contact.TunnelServer)) // use dhtclient compare { lookup.RunInCoreAsync(delegate() { TcpConnect direct = lookup.Network.TcpControl.GetProxy(packet.Target); if (direct != null) { packet.SourceServer = new DhtAddress(Core.LocalIP, lookup.Network.GetLocalSource()); bytesSent = direct.SendPacket(packet); } }); return bytesSent; } // if not open send proxied through local lookup proxy // NAT as well because receiver would need to send all responses through same local lookup proxy // for NATd host to get replies if (Core.Firewall != FirewallType.Open) { packet.TargetServer = contact.TunnelServer; lookup.RunInCoreAsync(delegate() { TcpConnect server = lookup.Network.TcpControl.GetProxy(packet.TargetServer) ?? // direct path lookup.Network.TcpControl.GetProxyServer(contact.IP) ?? // reRoute through same server lookup.Network.TcpControl.GetRandomProxy(); // random proxy if (server != null) { packet.SourceServer = new DhtAddress(server.RemoteIP, server); bytesSent = server.SendPacket(packet); } }); } // else we are open, send op ip address in the souce server else { packet.SourceServer = new DhtAddress(Core.LocalIP, lookup.Network.GetLocalSource()); lookup.RunInCoreAsync(delegate() { bytesSent = lookup.Network.UdpControl.SendTo(contact.TunnelServer, packet); }); } return bytesSent; }
public int SendRandomProxy(G2Packet packet) { TcpConnect socket = GetRandomProxy(); return (socket != null) ? socket.SendPacket(packet) : 0; }
public void SendTo(G2Packet packet) { if (Core.InvokeRequired) Debug.Assert(false); if (packet is NetworkPacket) { ((NetworkPacket)packet).SourceID = Network.Local.UserID; ((NetworkPacket)packet).ClientID = Network.Local.ClientID; } byte[] encoded = packet.Encode(Network.Protocol); PacketLogEntry logEntry = new PacketLogEntry(Core.TimeNow, TransportProtocol.LAN, DirectionType.Out, null, encoded); Network.LogPacket(logEntry); byte[] final = null; // encrypt, turn off encryption during simulation if (Core.Sim == null || Core.Sim.Internet.TestEncryption) { final = Utilities.EncryptBytes(encoded, Network.OpCrypt.Key); } else final = encoded; // send try { if (Core.Sim != null) { //Core.Sim.Internet.SendPacket(SimPacketType.Udp, Network, final, address.ToEndPoint(), null); return; } if (LanSocket == null) return; if (encoded.Length> MAX_UDP_SIZE) throw new Exception("Packet larger than " + MAX_UDP_SIZE.ToString() + " bytes"); EndPoint tempSender = (EndPoint)new IPEndPoint(IPAddress.Broadcast, ListenPort); LanSocket.BeginSendTo(final, 0, final.Length, SocketFlags.None, tempSender, new AsyncCallback(UdpSocket_SendTo), LanSocket); } catch(Exception ex) { Network.UpdateLog("Exception", "LanHandler::SendTo: " + ex.Message); } }
public int SendTo(DhtAddress address, G2Packet packet) { if (Core.InvokeRequired) Debug.Assert(false); Debug.Assert(address.UdpPort != 0); if (packet is NetworkPacket) { ((NetworkPacket)packet).SourceID = Network.Local.UserID; ((NetworkPacket)packet).ClientID = Network.Local.ClientID; } byte[] encoded = packet.Encode(Network.Protocol); PacketLogEntry logEntry = new PacketLogEntry(Core.TimeNow, TransportProtocol.Udp, DirectionType.Out, address, encoded); Network.LogPacket(logEntry); byte[] final = null; // encrypt, turn off encryption during simulation if (Core.Sim == null || Core.Sim.Internet.TestEncryption) final = Utilities.EncryptBytes(encoded, Network.GetAugmentedKey(address.UserID)); else final = encoded; // send try { if (Core.Sim != null) { Core.Sim.Internet.SendPacket(SimPacketType.Udp, Network, final, address.ToEndPoint(), null); } else { if (UdpSocket == null) return 0; if (encoded.Length > MAX_UDP_SIZE) throw new Exception("Packet larger than " + MAX_UDP_SIZE.ToString() + " bytes"); UdpSocket.BeginSendTo(final, 0, final.Length, SocketFlags.None, address.ToEndPoint(), new AsyncCallback(UdpSocket_SendTo), UdpSocket); } // record bandwidth Bandwidth.OutPerSec += final.Length; return final.Length; } catch(Exception ex) { Network.UpdateLog("Exception", "UdpHandler::SendTo: " + ex.Message); } return 0; }
public int WritePacket(G2Packet packet) { byte[] data = packet.Encode(Protocol); ParentStream.Write(data, 0, data.Length); return data.Length; }
public int WriteToFile(G2Packet packet, Stream stream) { byte[] data = packet.Encode(this); stream.Write(data, 0, data.Length); return data.Length; }