/// <summary>Creates a new channel in the network stack with a given encryption IV.</summary> void CreateChannel(int Reference, byte[] IV) { EncryptWrapper ew = new EncryptWrapper(Endpoint, (byte[])Key.Clone(), IV); lock (DataChannels) DataChannels.Add(Reference, ew); }
/// <summary>Handler for messages on the control channel.</summary> byte[] ControlReply(byte[] IMsg, int IOf, int IOc, out int OOf, out int OOc) { int MsgT = GetInt(IMsg, IOf); MessageType Type = (MessageType)MsgT; int Reference; switch (Type) { case MessageType.Handshake: OOf = IOf; OOc = IOc; return(IMsg); case MessageType.OpenReference: byte[] IV = EncryptWrapper.RequestIV(); Reference = GetInt(IMsg, IOf + 4); CreateChannel(Reference, IV); OOf = 0; OOc = IV.Length; return(IV); case MessageType.CloseReference: Reference = GetInt(IMsg, IOf + 4); ControlChannel.UnregisterCallback(Reference); lock (DataChannels) DataChannels.Remove(Reference); OOf = IOf; OOc = IOc; return(IMsg); } OOf = 0; OOc = 0; return(new byte[0]); }
/// <summary> /// Listens on the give <paramref name="Port"/> with the given keys. /// </summary> /// <param name="Port">Local listening port.</param> /// <param name="NEKey">Non-encryption key. Used for fast filtering incoming connections.</param> /// <param name="EKey">Encryption key.</param> public void CreateListener(int Port, string NEKey, byte[] EKey) { Key = EKey; Endpoint = new NetLibEndpoint(Log); Endpoint.RegisterCallback(0, NSReply0); ControlChannel = new EncryptWrapper(Endpoint, EKey, IV0); ControlChannel.RegisterCallback(1, ControlReply); Endpoint.Listen(Port, NEKey); }
/// <summary> /// Connects to the remote <paramref name="Host"/> on the given <paramref name="Port"/>, using the given keys. /// </summary> /// <param name="Host">Remote host.</param> /// <param name="Port">Remote port.</param> /// <param name="NEKey">Non-encryption key. Used for fast filtering on the remote host.</param> /// <param name="EKey">Encryption key.</param> public void EstablishConnection(string Host, int Port, string NEKey, byte[] EKey) { Key = (byte[])EKey.Clone(); Endpoint = new NetLibEndpoint(Log); Endpoint.Connect(Host, Port, NEKey); string Message = SendReceiveText(Endpoint, 0, "WNS-C"); if (Message != "WNS-S") { throw new System.Net.ProtocolViolationException("Remote host does not follow Webrella Network Stack"); } ControlChannel = new EncryptWrapper(Endpoint, EKey, IV0); ControlChannel.RegisterCallback(1, ControlReply); SendHandshake(); }