public void GetIpV4BytesTest() { AddressAttribute target = new MappedAddress() { IpAddress = IPAddress.Loopback, Port = 0x1234, }; byte[] expected = new byte[] { 0xee, 0xee, 0xee, 0xee, 0x00, 0x01, 0x00, 0x08, 0x00, 0x01, 0x12, 0x34, 0x7f, 0x00, 0x00, 0x01, }; byte[] actual = new byte[] { 0xee, 0xee, 0xee, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; int startIndex = 4; target.GetBytes(actual, ref startIndex); Assert.AreEqual(16, startIndex); Helpers.AreArrayEqual(expected, actual); Assert.AreEqual(AddressFamily.InterNetwork, target.IpEndPoint.AddressFamily); }
public void ParseIp4Test() { byte[] bytes = new byte[] { 0x00, 0x01, 0x00, 0x08, 0xff, 0x01, 0x12, 0x34, 0x01, 0x02, 0x03, 0x04, }; int startIndex = 0; AddressAttribute target = new MappedAddress(); target.Parse(bytes, ref startIndex); Assert.AreEqual(12, startIndex); Assert.AreEqual(AttributeType.MappedAddress, target.AttributeType); Assert.AreEqual(0x1234, target.Port); Assert.AreEqual(@"1.2.3.4", target.IpAddress.ToString()); }
/// <summary> /// Helper method using UDP or TCP that returns needed informations to begin peer-to-peer Punch Hole operations /// using an existing IPEndPoint /// </summary> /// <param name="hostEP">The IPEndPoint to which the socket will be bound to</param> /// <param name="serverEP">The IP Address of the STUN server</param> /// <param name="type">The connection type used to do the STUN Binding Request</param> /// <returns> /// A key-value pair where : /// * the key is the local IPEndPoint from where the STUN request occurs /// * the value is the MappedAddress IPEndPoint returned by the STUN server /// </returns> public static KeyValuePair <IPEndPoint, IPEndPoint> GetMappedAddressFrom(IPEndPoint hostEP, IPEndPoint serverEP, ProtocolType type) { StunMessage msg = new StunMessage(StunMethodType.Binding, StunMethodClass.Request, StunUtilities.NewTransactionId); StunClient cli = new StunClient(hostEP, serverEP, type, null, null); cli.Connect(); StunMessage resp = cli.SendMessage(msg); if (hostEP == null) { hostEP = cli.HostEP; } MappedAddress mappedAddress = resp.MappedAddress; cli.Disconnect(); return(new KeyValuePair <IPEndPoint, IPEndPoint>(hostEP, mappedAddress.EndPoint)); }
/// <summary> /// Helper method using TLS over TCP that returns needed informations to begin peer-to-peer Punch Hole operations /// using an existing IPEndPoint /// </summary> /// <param name="hostEP">The IPEndPoint to which the socket will be bound to</param> /// <param name="serverEP">The IP Address of the STUN server</param> /// <param name="remoteCertificateValidationHandler">The callback handler which validate STUN Server TLS certificate</param> /// <param name="clientCertificate"> /// Client certificate used for mutual authentication. This certificate must be in PKCS #12 format and must contains its private key /// The simpler way to create a certificate of this type is to follow this makecert tutorial http://www.inventec.ch/chdh/notes/14.htm. /// Once your certificate is created : launch "mmc", CTRL+M, select "Certificates", add, choose "Local machine". /// Find your certificate under "Personal", it must have a little key in its icon, right click on it, choose "All tasks > Export...". /// Check the "Export key" checkbox, finish the process and then you have a valid X509Certificate2 with its private key in it /// </param> /// <returns> /// A key-value pair where : /// * the key is the local IPEndPoint from where the STUN request occurs /// * the value is the MappedAddress IPEndPoint returned by the STUN server /// </returns> public static KeyValuePair <IPEndPoint, IPEndPoint> GetMappedAddressFrom(IPEndPoint hostEP, IPEndPoint serverEP, RemoteCertificateValidationCallback remoteCertificateValidationHandler, X509Certificate2 clientCertificate) { StunMessage msg = new StunMessage(StunMethodType.Binding, StunMethodClass.Request, StunUtilities.NewTransactionId); StunClient cli = new StunClient(hostEP, serverEP, ProtocolType.Tcp, clientCertificate, remoteCertificateValidationHandler); cli.Connect(); StunMessage resp = cli.SendMessage(msg); if (hostEP == null) { hostEP = cli.HostEP; } MappedAddress mappedAddress = resp.MappedAddress; cli.Disconnect(); return(new KeyValuePair <IPEndPoint, IPEndPoint>(hostEP, mappedAddress.EndPoint)); }
public void ParseIp6Test() { byte[] bytes = new byte[] { 0x00, 0x01, 0x00, 20, 0x00, 0x02, 0x12, 0x34, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xf2, }; int startIndex = 0; AddressAttribute target = new MappedAddress(); target.Parse(bytes, ref startIndex); Assert.AreEqual(24, startIndex); Assert.AreEqual(AttributeType.MappedAddress, target.AttributeType); Assert.AreEqual(0x1234, target.Port); Assert.AreEqual(@"1122:3344:5566:7788:99aa:bbcc:ddee:fff2", target.IpAddress.ToString()); }
public void GetIpV6BytesTest() { AddressAttribute target = new MappedAddress() { IpAddress = IPAddress.Parse("0102:0304:0506:0708:090a:0b0c:0d0e:0f10"), Port = 0x1234, }; byte[] expected = new byte[] { 0xee, 0xee, 0xee, 0xee, 0x00, 0x01, 0x00, 20, 0x00, 0x02, 0x12, 0x34, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, }; byte[] actual = new byte[] { 0xee, 0xee, 0xee, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; int startIndex = 4; target.GetBytes(actual, ref startIndex); Assert.AreEqual(28, startIndex); Helpers.AreArrayEqual(expected, actual); Assert.AreEqual(AddressFamily.InterNetworkV6, target.IpEndPoint.AddressFamily); }