public void ParsesBasicFields() { byte[] rawBytes = { 0x45, 0x00, 0x00, 0x50, 0x00, 0x00, 0x40, 0x00, 0x40, 0x11 , 0xb6, 0xce, 0xc0, 0xa8, 0x01, 0x03, 0xc0, 0xa8, 0x01, 0x7b, 0x80, 0x0d, 0xf1, 0x00, 0x00, 0x3c, 0x89, 0x11, 0xec, 0x14, 0x61, 0x11, 0x00, 0x00, 0x00, 0xfe, 0x09, 0x65, 0x1b, 0xbb, 0x9f, 0xa2, 0x70, 0x43, 0x74, 0x90, 0x9c, 0x29, 0x13, 0xbb, 0x34, 0x92, 0x14, 0x27, 0x25, 0xdf, 0x1b, 0x8f, 0x9f, 0xd8, 0xb7, 0xc4, 0xc5, 0xa6, 0x07, 0x36, 0xca, 0xce, 0x11, 0x3b, 0x32, 0xcd, 0x87, 0x1c, 0x24, 0x2d, 0xad, 0x15, 0xc4, 0x0f }; IPv4 packet; var parseResult = IPv4Factory.Instance.TryParse(rawBytes, out packet); parseResult.Should().BeTrue(); ((int)packet.Version).Should().Be(4); ((int)packet.HeaderLength).Should().Be(5); packet.DifferentiatedServices.Should().Be(0); packet.TotalLength.Should().Be(80); packet.ID.Should().Be(0); packet.ReservedFlag.Should().Be(false); packet.DontFragmentFlag.Should().Be(true); packet.MoreFragmentsFlag.Should().Be(false); packet.FragOff.Should().Be(0); packet.TTL.Should().Be(64); packet.Protocol.Should().Be(17); packet.HeaderChecksum.Should().Be(46798); packet.SourceAddress.Should().Be(IPv4Address.Parse("192.168.1.3")); packet.DestAddress.Should().Be(IPv4Address.Parse("192.168.1.123")); }
public void ParseIPv4String_001() { var _IPv4String = "141.24.12.2"; var _IPv4Address = IPv4Address.Parse(_IPv4String); Assert.AreEqual(_IPv4String, _IPv4Address.ToString()); }
public void CompareIPv4Addresses() { var a = IPv4Address.Parse("141.24.12.2"); var b = IPv4Address.Parse("127.0.0.1"); Assert.IsTrue(a.CompareTo(b) > 0); }
public void TestMinimizeTunet() { var netsMust = new IPv4Network[] { new IPv4Network(IPv4Address.Parse("128.130.0.0"), 15), new IPv4Network(IPv4Address.Parse("192.35.240.0"), 22), new IPv4Network(IPv4Address.Parse("192.35.244.0"), 24), new IPv4Network(IPv4Address.Parse("193.170.72.0"), 21), new IPv4Network(IPv4Address.Parse("193.170.72.0"), 22), new IPv4Network(IPv4Address.Parse("193.170.76.0"), 23), new IPv4Network(IPv4Address.Parse("193.170.78.0"), 24), new IPv4Network(IPv4Address.Parse("193.170.79.0"), 24), }; List <IPNetwork <IPv4Address> > netsMinimized = Minimize.MinimizeSubnets(netsMust, (addr, mask) => new IPv4Network(addr, mask)); foreach (IPNetwork <IPv4Address> minNet in netsMinimized) { for (IPv4Address addr = minNet.BaseAddress; addr.CompareTo(minNet.LastAddressOfSubnet) <= 0; addr = addr + 1) { bool contained = false; foreach (IPNetwork <IPv4Address> origNet in netsMust) { if (origNet.Contains(addr)) { contained = true; break; } } if (!contained) { Assert.True(false, $"IP address {addr} in minimized net {minNet} not contained in any original net"); } } } foreach (IPNetwork <IPv4Address> origNet in netsMust) { for (IPv4Address addr = origNet.BaseAddress; addr.CompareTo(origNet.LastAddressOfSubnet) <= 0; addr = addr + 1) { bool contained = false; foreach (IPNetwork <IPv4Address> minNet in netsMinimized) { if (minNet.Contains(addr)) { contained = true; break; } } if (!contained) { Assert.True(false, $"IP address {addr} in original net {origNet} not contained in any minimized net"); } } } }
public void IPv4AddressesAreNotEqual() { var a = IPv4Address.Parse("141.24.12.2"); var b = IPv4Address.Parse("127.0.0.1"); Assert.IsFalse(a.Equals(b)); Assert.IsFalse(a == b); Assert.IsTrue(a != b); Assert.AreNotEqual(a, b); }
private void btApply_Click(object sender, EventArgs e) { try { currentAddress = IPv4Address.Parse(tbIpAddress.Text); } catch (InvalidIPv4AddressFormatException exception) { MessageBox.Show(exception.Message); return; } // MessageBox.Show(ip.ToCompleteString()); presenter.UpdateView(currentAddress); btPing.Enabled = currentAddress.IsHostAddress; btSave.Enabled = true; // MessageBox.Show($"{ip} \nMaska: {ip.Mask.Value.ToBinaryString()}"); }
/// <summary> /// Create a new DNS cache. /// </summary> /// <param name="CleanUpEvery">How often to remove outdated entries from DNS cache.</param> public DNSCache(TimeSpan?CleanUpEvery = null) { _DNSCache = new Dictionary <String, DNSCacheEntry>(); _CleanUpTimer = new Timer(RemoveExpiredCacheEntries, null, CleanUpEvery.HasValue ? CleanUpEvery.Value : TimeSpan.FromMinutes(1), // delay one round! CleanUpEvery.HasValue ? CleanUpEvery.Value : TimeSpan.FromMinutes(1)); _DNSCache.Add("localhost", new DNSCacheEntry(RefreshTime: DateTime.Now, EndOfLife: DateTime.Now + TimeSpan.FromDays(3650), DNSInfo: new DNSInfo(Origin: new IPSocket(IPv4Address.Parse("127.0.0.1"), IPPort.Parse(53)), QueryId: 0, IsAuthorativeAnswer: true, IsTruncated: false, RecursionDesired: false, RecursionAvailable: false, ResponseCode: DNSResponseCodes.NoError, Answers: new ADNSResourceRecord[] { new A("localhost", DNSQueryClasses.IN, TimeSpan.FromDays(3650), IPv4Address.Parse("127.0.0.1")), new AAAA("localhost", DNSQueryClasses.IN, TimeSpan.FromDays(3650), IPv6Address.Parse("::1")) }, Authorities: new ADNSResourceRecord[0], AdditionalRecords: new ADNSResourceRecord[0]) )); _DNSCache.Add("loopback", new DNSCacheEntry(RefreshTime: DateTime.Now, EndOfLife: DateTime.Now + TimeSpan.FromDays(3650), DNSInfo: new DNSInfo(Origin: new IPSocket(IPv4Address.Parse("127.0.0.1"), IPPort.Parse(53)), QueryId: 0, IsAuthorativeAnswer: true, IsTruncated: false, RecursionDesired: false, RecursionAvailable: false, ResponseCode: DNSResponseCodes.NoError, Answers: new ADNSResourceRecord[] { new A("loopback", DNSQueryClasses.IN, TimeSpan.FromDays(3650), IPv4Address.Parse("127.0.0.1")), new AAAA("loopback", DNSQueryClasses.IN, TimeSpan.FromDays(3650), IPv6Address.Parse("::1")) }, Authorities: new ADNSResourceRecord[0], AdditionalRecords: new ADNSResourceRecord[0]) )); }
public void CorrectsFields() { // This test is poor. It seems the original test for CorrectFields didn't set an IPv4 parent, and thus was basically worthless. EthernetII packet; EthernetIIFactory.Instance.TryParse(new byte[] { 0x64, 0x70, 0x02, 0xd1, 0x9e, 0x6f, 0x00, 0x23, 0x54, 0x80, 0xb3, 0xca, 0x08, 0x00, 0x45, 0x00, 0x00, 0x21, 0x2d, 0x57, 0x00, 0x00, 0x80, 0x11, 0x80, 0xbd, 0xc0, 0xa8, 0x05, 0x68, 0xc0, 0xa8, 0x05, 0xff, 0x22, 0xb9, 0x22, 0xb9, 0x00, 0x0d, 0x80, 0x30, 0x02, 0xe1, 0x69, 0x98, 0x41, }, out packet).Should().BeTrue(); var ip = (IPv4 <UDP>)packet.Payload; ip.Payload.Checksum.Should().Be(0x8030); packet.CorrectFields(); ip.Payload.Checksum.Should().Be(0x8030); ip.DestAddress = IPv4Address.Parse("192.168.3.101"); packet.CorrectFields(); ip.Payload.Checksum.Should().Be(0x82ca); }
/// <summary> /// Execute the given HTTP request and return its result. /// </summary> /// <param name="Request">A HTTP request.</param> /// <param name="RequestLogDelegate">A delegate for logging the HTTP request.</param> /// <param name="ResponseLogDelegate">A delegate for logging the HTTP request/response.</param> /// <param name="Timeout">An optional timeout.</param> /// <param name="CancellationToken">A cancellation token.</param> public async Task <HTTPResponse> Execute(HTTPRequest Request, ClientRequestLogHandler RequestLogDelegate = null, ClientResponseLogHandler ResponseLogDelegate = null, TimeSpan?Timeout = null, CancellationToken?CancellationToken = null) { #region Call the optional HTTP request log delegate try { RequestLogDelegate?.Invoke(DateTime.Now, this, Request); } catch (Exception e) { e.Log(nameof(HTTPClient) + "." + nameof(RequestLogDelegate)); } #endregion var task = Task <HTTPResponse> .Factory.StartNew(() => { HTTPResponse Response = null; try { if (Environment.MachineName.Contains("QUAD2QUANTOR") && Request.URI.Contains("eRoaming")) { throw new Exception("Catch me if you can!"); } Thread.CurrentThread.Priority = ThreadPriority.BelowNormal; #region Data var HTTPHeaderBytes = new Byte[0]; var sw = new Stopwatch(); if (!Timeout.HasValue) { Timeout = TimeSpan.FromSeconds(60); } #endregion #region Create TCP connection (possibly also do DNS lookups) if (TCPClient == null) { System.Net.IPEndPoint _FinalIPEndPoint = null; IIPAddress _ResolvedRemoteIPAddress = null; if (RemoteIPAddress == null) { if (Hostname.Trim() == "127.0.0.1") { _ResolvedRemoteIPAddress = IPv4Address.Localhost; } else { var RegExpr = new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"); if (RegExpr.IsMatch(Hostname)) { _ResolvedRemoteIPAddress = IPv4Address.Parse(Hostname); } } #region DNS lookup... if (_ResolvedRemoteIPAddress == null) { try { var IPv4AddressTask = DNSClient. Query <A>(Hostname). ContinueWith(QueryTask => QueryTask.Result. Select(ARecord => ARecord.IPv4Address). FirstOrDefault()); IPv4AddressTask.Wait(); _ResolvedRemoteIPAddress = IPv4AddressTask.Result; } catch (Exception e) { Debug.WriteLine("[" + DateTime.Now + "] " + e.Message); } } #endregion } else { _ResolvedRemoteIPAddress = RemoteIPAddress; } _FinalIPEndPoint = new System.Net.IPEndPoint(new System.Net.IPAddress(_ResolvedRemoteIPAddress.GetBytes()), RemotePort.ToInt32()); sw.Start(); TCPClient = new TcpClient(); TCPClient.Connect(_FinalIPEndPoint); TCPClient.ReceiveTimeout = (Int32)Timeout.Value.TotalMilliseconds; } #endregion #region Create (Crypto-)Stream TCPStream = TCPClient.GetStream(); TCPStream.ReadTimeout = (Int32)Timeout.Value.TotalMilliseconds; TLSStream = RemoteCertificateValidator != null ? new SslStream(TCPStream, false, RemoteCertificateValidator) // ClientCertificateSelector, //EncryptionPolicy.RequireEncryption) : null; if (TLSStream != null) { TLSStream.ReadTimeout = (Int32)Timeout.Value.TotalMilliseconds; } HTTPStream = null; if (RemoteCertificateValidator != null) { HTTPStream = TLSStream; TLSStream.AuthenticateAsClient(Hostname);//, new X509CertificateCollection(new X509Certificate[] { ClientCert }), SslProtocols.Default, false); } else { HTTPStream = TCPStream; } HTTPStream.ReadTimeout = (Int32)Timeout.Value.TotalMilliseconds; #endregion #region Send Request HTTPStream.Write(String.Concat(Request.EntireRequestHeader, Environment.NewLine, Environment.NewLine). ToUTF8Bytes()); var RequestBodyLength = Request.HTTPBody == null ? Request.ContentLength.HasValue ? (Int32)Request.ContentLength.Value : 0 : Request.ContentLength.HasValue ? Math.Min((Int32)Request.ContentLength.Value, Request.HTTPBody.Length) : Request.HTTPBody.Length; if (RequestBodyLength > 0) { HTTPStream.Write(Request.HTTPBody, 0, RequestBodyLength); } var _MemoryStream = new MemoryStream(); var _Buffer = new Byte[10485760]; // 10 MBytes, a smaller value leads to read errors! #endregion #region Wait timeout for the server to react! //Debug.WriteLine("[" + DateTime.Now + "] HTTPClient timeout: " + Timeout.Value.ToString()); while (!TCPStream.DataAvailable) { if (sw.ElapsedMilliseconds >= Timeout.Value.TotalMilliseconds) { TCPClient.Close(); throw new Exception("[" + DateTime.Now + "] Could not read from the TCP stream for " + sw.ElapsedMilliseconds + "ms!"); } Thread.Sleep(1); } //Debug.WriteLine("[" + DateTime.Now + "] HTTPClient (" + TCPClient.Client.LocalEndPoint.ToString() + " -> " + RemoteSocket.ToString() + ") got first response after " + sw.ElapsedMilliseconds + "ms!"); #endregion #region Read the entire HTTP header, and maybe some of the HTTP body var CurrentDataLength = 0; do { #region When data available, write it to the buffer... while (TCPStream.DataAvailable) { CurrentDataLength = HTTPStream.Read(_Buffer, 0, _Buffer.Length); if (CurrentDataLength > -1) { _MemoryStream.Write(_Buffer, 0, CurrentDataLength); // Debug.WriteLine("[" + DateTime.Now + "] Read " + CurrentDataLength + " bytes from HTTP connection (" + TCPClient.Client.LocalEndPoint.ToString() + " -> " + RemoteSocket.ToString() + ") (" + sw.ElapsedMilliseconds + "ms)!"); } } #endregion #region Check if the entire HTTP header was already read into the buffer if (_MemoryStream.Length > 4) { var MemoryCopy = _MemoryStream.ToArray(); for (var pos = 3; pos < MemoryCopy.Length; pos++) { if (MemoryCopy[pos] == 0x0a && MemoryCopy[pos - 1] == 0x0d && MemoryCopy[pos - 2] == 0x0a && MemoryCopy[pos - 3] == 0x0d) { Array.Resize(ref HTTPHeaderBytes, pos - 3); Array.Copy(MemoryCopy, 0, HTTPHeaderBytes, 0, pos - 3); break; } } //if (HTTPHeaderBytes.Length > 0) // Debug.WriteLine("[" + DateTime.Now + "] End of (" + TCPClient.Client.LocalEndPoint.ToString() + " -> " + RemoteSocket.ToString() + ") HTTP header at " + HTTPHeaderBytes.Length + " bytes (" + sw.ElapsedMilliseconds + "ms)!"); } #endregion Thread.Sleep(1); } // Note: Delayed parts of the HTTP body may not be read into the buffer // => Must be read later! while (TCPStream.DataAvailable || ((sw.ElapsedMilliseconds < HTTPStream.ReadTimeout) && HTTPHeaderBytes.Length == 0)); //Debug.WriteLine("[" + DateTime.Now + "] Finally read " + _MemoryStream.Length + " bytes of HTTP client (" + TCPClient.Client.LocalEndPoint.ToString() + " -> " + RemoteSocket.ToString() + ") data (" + sw.ElapsedMilliseconds + "ms)!"); #endregion #region Copy HTTP header data and create HTTP response if (HTTPHeaderBytes.Length == 0) { throw new ApplicationException(DateTime.Now + " Could not find the end of the HTTP protocol header!"); } Response = HTTPResponse.Parse(HTTPHeaderBytes.ToUTF8String(), Request); #endregion #region Read 'Content-Length' bytes... // Copy only the number of bytes given within // the HTTP header element 'Content-Length'! if (Response.ContentLength.HasValue && Response.ContentLength.Value > 0) { _MemoryStream.Seek(HTTPHeaderBytes.Length + 4, SeekOrigin.Begin); var _Read = _MemoryStream.Read(_Buffer, 0, _Buffer.Length); var _StillToRead = (Int32)Response.ContentLength.Value - _Read; Response.HTTPBodyStream.Write(_Buffer, 0, _Read); var _CurrentBufferSize = 0; do { while (TCPStream.DataAvailable && _StillToRead > 0) { _CurrentBufferSize = Math.Min(_Buffer.Length, (Int32)_StillToRead); _Read = HTTPStream.Read(_Buffer, 0, _CurrentBufferSize); Response.HTTPBodyStream.Write(_Buffer, 0, _Read); _StillToRead -= _Read; } if (_StillToRead <= 0) { break; } Thread.Sleep(1); }while (sw.ElapsedMilliseconds < HTTPStream.ReadTimeout); Response.ContentStreamToArray(); } #endregion #region ...or read till timeout (e.g. for chunked transport)! else { try { _MemoryStream.Seek(HTTPHeaderBytes.Length + 4, SeekOrigin.Begin); Response.NewContentStream(); Response.HTTPBodyStream.Write(_Buffer, 0, _MemoryStream.Read(_Buffer, 0, _Buffer.Length)); var Retries = 0; while (Retries < 10) { while (TCPStream.DataAvailable) { Response.HTTPBodyStream.Write(_Buffer, 0, HTTPStream.Read(_Buffer, 0, _Buffer.Length)); Retries = 0; } Thread.Sleep(10); Retries++; } if (Response.TransferEncoding == "chunked") { //Debug.WriteLine(DateTime.Now + " Chunked encoding detected"); var TEContent = ((MemoryStream)Response.HTTPBodyStream).ToArray(); var TEString = TEContent.ToUTF8String(); var ReadBlockLength = true; var TEMemStram = new MemoryStream(); var LastPos = 0; var i = 0; do { if (i > 2 && ReadBlockLength && TEContent[i - 1] == '\n' && TEContent[i - 2] == '\r') { var len = TEContent.ReadTEBlockLength(LastPos, i - LastPos - 2); //Debug.WriteLine(DateTime.Now + " Chunked encoded block of length " + len + " bytes detected"); if (len == 0) { break; } if (i + len <= TEContent.Length) { TEMemStram.Write(TEContent, i, len); i = i + len; if (TEContent[i] == 0x0d) { i++; } if (i < TEContent.Length - 1) { if (TEContent[i] == 0x0a) { i++; } } else { } LastPos = i; ReadBlockLength = false; } else { // Reaching this point seems to be an endless loop! break; } } else { ReadBlockLength = true; i++; } } while (i < TEContent.Length); Response.ContentStreamToArray(TEMemStram); } else { Response.ContentStreamToArray(); } } catch (Exception e) { Debug.WriteLine(DateTime.Now + " " + e.Message); } } #endregion #region Close connection if requested! if (Response.Connection == null || Response.Connection == "close") { TCPClient.Close(); HTTPStream = null; TCPClient = null; } #endregion } catch (Exception e) { #region Create a HTTP response for the exception... while (e.InnerException != null) { e = e.InnerException; } Response = new HTTPResponseBuilder(Request, HTTPStatusCode.BadRequest) { ContentType = HTTPContentType.JSON_UTF8, Content = JSONObject.Create(new JProperty("Message", e.Message), new JProperty("StackTrace", e.StackTrace)). ToUTF8Bytes() }; #endregion } #region Call the optional HTTP response log delegate try { ResponseLogDelegate?.Invoke(DateTime.Now, this, Request, Response); } catch (Exception e2) { e2.Log(nameof(HTTPClient) + "." + nameof(ResponseLogDelegate)); } #endregion return(Response); }, TaskCreationOptions.AttachedToParent); return(await task); }
public static DNSClient GoogleDNS() { return(new DNSClient(IPv4Address.Parse("8.8.8.8"))); }
public void ParseIPv4String_004() { IPv4Address.Parse("141.300.12.2"); }
public void ParseIPv4String_003() { IPv4Address.Parse("300.24.12.2"); }
public void ParseIPv4String_006() { IPv4Address.Parse("141.24.12.300"); }
public void ParseIPv4String_005() { IPv4Address.Parse("141.24.300.2"); }
/// <summary> /// Main. /// </summary> /// <param name="myArgs">The arguments.</param> public static void Main(String[] myArgs) { #region Start TCPServers //var _TCPServer1 = new TCPServer(new IPv4Address(new Byte[] { 192, 168, 178, 31 }), new IPPort(2001), NewConnection => // { // NewConnection.WriteToResponseStream("Hello world!" + Environment.NewLine + Environment.NewLine); // NewConnection.Close(); // }, true); //Console.WriteLine(_TCPServer1); // The first line of the repose will be served from the CustomTCPConnection handler. var _TCPServer2 = new TCPServer <CustomTCPConnection>(IPv4Address.Any, new IPPort(2002), NewConnection => { NewConnection.WriteToResponseStream("...world!" + Environment.NewLine + Environment.NewLine); NewConnection.Close(); }, true); Console.WriteLine(_TCPServer2); #endregion #region Start HTTPServers // Although the socket listens on IPv4Address.Any the service is // configured to serve clients only on http://localhost:8181 // More details within DefaultHTTPService.cs var _HTTPServer1 = new HTTPServer(IPv4Address.Any, new IPPort(8181), Autostart: true) { ServerName = "Default Hermod Demo" }; Console.WriteLine(_HTTPServer1); // This service uses a custom HTTPService defined within IRESTService.cs var _HTTPServer2 = new HTTPServer <IRESTService>(IPv4Address.Any, IPPort.HTTP, Autostart: true) { ServerName = "Customized Hermod Demo" }; Console.WriteLine(_HTTPServer2); #endregion #region UDP Servers var _UDPServer1 = new UDPServer(new IPPort(5555), NewPacket => { //NewPacket.Data = new Byte[10]; // NewPacket.Close(); }, true); #endregion var _client1 = new HTTPClient(IPv4Address.Localhost, IPPort.HTTP); var _request0 = _client1.GET("/HelloWorld"). SetHost("localhorst"). AddAccept(HTTPContentType.TEXT_UTF8, 1); var _request1 = _client1.GET("/HelloWorld"). SetHost("localhorst"). AddAccept(HTTPContentType.HTML_UTF8, 1); //WriteRequest(_request0.EntireRequestHeader); //_client1.Execute(_request0, response => WriteResponse(response.Content.ToUTF8String())). // ContinueWith(HTTPClient => { WriteRequest(_request1.EntireRequestHeader); return HTTPClient.Result; }). // ContinueWith(HTTPClient => HTTPClient.Result.Execute(_request1, response => WriteResponse(response.Content.ToUTF8String()))). // Wait(); var _client2 = new HTTPClient(IPv4Address.Parse("188.40.47.229"), IPPort.HTTP); var _requestA = _client2.GET("/"). SetProtocolVersion(HTTPVersion.HTTP_1_1). SetHost("www.ahzf.de"). SetUserAgent("Hermod HTTP Client v0.1"). SetConnection("keep-alive"). AddAccept(HTTPContentType.HTML_UTF8, 1); var _requestB = _client2.GET("/nfgj"). SetProtocolVersion(HTTPVersion.HTTP_1_1). SetHost("www.ahzf.de"). SetUserAgent("Hermod HTTP Client v0.1"). SetConnection("keep-alive"). AddAccept(HTTPContentType.HTML_UTF8, 1); WriteRequest(_requestA); _client2.Execute(_requestA, response => WriteResponse(response)). ContinueWith(Client => Client.Result.Execute(_requestB, response => WriteResponse(response))); var _req23a = new HTTPRequestBuilder(). SetHTTPMethod(HTTPMethod.GET). SetProtocolName("µHTTP"). SetProtocolVersion(new HTTPVersion(2, 0)). SetHost("localhorst"). SetUserAgent("Hermod µHTTP Client"). SetContent("This the HTTP content..."); _req23a.QueryString.Add("name", "alice"). Add("friend", "bob"). Add("friend", "carol"); var _req23b = new HTTPRequestBuilder() { HTTPMethod = HTTPMethod.GET, ProtocolName = "µHTTP", ProtocolVersion = new HTTPVersion(2, 0), Host = "localhorst", UserAgent = "Hermod µHTTP Client", Content = "This the HTTP content...".ToUTF8Bytes() }; // var Response = new TCPClientRequest("localhost", 80).Send("GETTT / HTTP/1.1").FinishCurrentRequest().Response; Console.ReadLine(); Console.WriteLine("done!"); }
public void ParseIPv4String_002() { IPv4Address.Parse("141.24.12"); }
/** * Produces a new zone from an input XML file. */ public static DNSZone Unserialize(XmlDocument config) { DNSRecord start_of_authority = null; var records = new List <DNSRecord>(); var relays = new List <EndPoint>(); if (config.DocumentElement.Name != "zone") { throw new InvalidDataException("Root element must be called zone"); } foreach (var entry in config.DocumentElement.ChildNodes.OfType <XmlNode>()) { if (entry.NodeType != XmlNodeType.Element) { logger.Trace("Ignoring node of type {0}", entry.NodeType); continue; } bool is_record = entry.Name == "A" || entry.Name == "NS" || entry.Name == "CNAME" || entry.Name == "MX" || entry.Name == "SOA" || entry.Name == "PTR" || entry.Name == "AAAA"; if (is_record) { if (entry.Attributes["name"] == null || entry.Attributes["class"] == null || entry.Attributes["ttl"] == null) { throw new InvalidDataException("Resource records must have 'name', 'class' and 'ttl' attributes"); } var record_name = new Domain(entry.Attributes["name"].Value); AddressClass record_class; switch (entry.Attributes["class"].Value) { case "IN": record_class = AddressClass.INTERNET; break; default: throw new InvalidDataException("Only address class 'IN' is supported"); } UInt32 record_ttl = 0; try { record_ttl = UInt32.Parse(entry.Attributes["ttl"].Value); } catch (Exception err) { if (err is OverflowException || err is FormatException) { throw new InvalidDataException(entry.Attributes["ttl"].Value + " is not a valid TTL"); } else { throw; } } IDNSResource resource = null; switch (entry.Name) { case "A": if (entry.Attributes["address"] == null) { throw new InvalidDataException("A record must have address"); } IPv4Address address; try { address = IPv4Address.Parse(entry.Attributes["address"].Value); } catch (FormatException) { throw new InvalidDataException(entry.Attributes["address"].Value + " is not a valid IPv4 address"); } resource = new AResource(address); logger.Trace("A record: address={0}", ((AResource)resource).Address); break; case "NS": if (entry.Attributes["nameserver"] == null) { throw new InvalidDataException("NS record must have a nameserver"); } resource = new NSResource(new Domain(entry.Attributes["nameserver"].Value)); logger.Trace("NS record: nameserver={0}", ((NSResource)resource).Nameserver); break; case "CNAME": if (entry.Attributes["alias"] == null) { throw new InvalidDataException("CNAME record must have an alias"); } resource = new CNAMEResource(new Domain(entry.Attributes["alias"].Value)); logger.Trace("CNAME record: alias={0}", ((CNAMEResource)resource).Alias); break; case "MX": if (entry.Attributes["priority"] == null || entry.Attributes["mailserver"] == null) { throw new InvalidDataException("MX record must have priority and mailserver"); } var mailserver = new Domain(entry.Attributes["mailserver"].Value); UInt16 preference = 0; try { preference = UInt16.Parse(entry.Attributes["priority"].Value); } catch (Exception err) { if (err is OverflowException || err is FormatException) { throw new InvalidDataException(entry.Attributes["priority"].Value + " is not a valid priority value"); } else { throw; } } resource = new MXResource(preference, mailserver); logger.Trace("MX record: priority={0} mailserver={1}", ((MXResource)resource).Preference, ((MXResource)resource).Mailserver); break; case "PTR": if (entry.Attributes["pointer"] == null) { throw new InvalidDataException("PTR record must have pointer"); } if (!reverse_zone_v4.IsSubdomain(record_name) && !reverse_zone_v6.IsSubdomain(record_name)) { throw new InvalidDataException("PTR record be in the in-addr.arpa or ip6.arpa zone"); } resource = new PTRResource(new Domain(entry.Attributes["pointer"].Value)); logger.Trace("PTR record: pointer={0}", ((PTRResource)resource).Pointer); break; case "AAAA": if (entry.Attributes["address"] == null) { throw new InvalidDataException("AAAA record must have address"); } IPv6Address v6address; try { v6address = IPv6Address.Parse(entry.Attributes["address"].Value); } catch (FormatException) { throw new InvalidDataException(entry.Attributes["address"].Value + " is not a valid IPv4 address"); } resource = new AAAAResource(v6address); logger.Trace("AAAA record: address={0}", ((AAAAResource)resource).Address); break; case "SOA": if (entry.Attributes["primary-ns"] == null || entry.Attributes["hostmaster"] == null || entry.Attributes["serial"] == null || entry.Attributes["refresh"] == null || entry.Attributes["retry"] == null || entry.Attributes["expire"] == null || entry.Attributes["min-ttl"] == null) { throw new InvalidDataException("SOA record missing one of: primary-ns, hostmaster, serial, refresh, retry, expire and min-ttl"); } var primary_ns = new Domain(entry.Attributes["primary-ns"].Value); var hostmaster = new Domain(entry.Attributes["hostmaster"].Value); UInt32 serial = 0; try { serial = UInt32.Parse(entry.Attributes["serial"].Value); } catch (Exception err) { if (err is OverflowException || err is FormatException) { throw new InvalidDataException(entry.Attributes["serial"].Value + " is not a valid serial number"); } else { throw; } } UInt32 refresh = 0; try { refresh = UInt32.Parse(entry.Attributes["refresh"].Value); } catch (Exception err) { if (err is OverflowException || err is FormatException) { throw new InvalidDataException(entry.Attributes["refresh"].Value + " is not a valid refresh value"); } else { throw; } } UInt32 retry = 0; try { retry = UInt32.Parse(entry.Attributes["retry"].Value); } catch (Exception err) { if (err is OverflowException || err is FormatException) { throw new InvalidDataException(entry.Attributes["retry"].Value + " is not a valid retry value"); } else { throw; } } UInt32 expire = 0; try { expire = UInt32.Parse(entry.Attributes["expire"].Value); } catch (Exception err) { if (err is OverflowException || err is FormatException) { throw new InvalidDataException(entry.Attributes["expire"].Value + " is not a valid expire value"); } else { throw; } } UInt32 minttl = 0; try { minttl = UInt32.Parse(entry.Attributes["min-ttl"].Value); } catch (Exception err) { if (err is OverflowException || err is FormatException) { throw new InvalidDataException(entry.Attributes["min-ttl"].Value + " is not a valid expire value"); } else { throw; } } resource = new SOAResource(primary_ns, hostmaster, serial, refresh, retry, expire, minttl); logger.Trace("SOA record: primary-ns={0} hostmaster={1} serial={2} refresh={3} retry={4} expire={5} min-ttl={6}", ((SOAResource)resource).PrimaryNameServer, ((SOAResource)resource).Hostmaster, ((SOAResource)resource).Serial, ((SOAResource)resource).RefreshSeconds, ((SOAResource)resource).RetrySeconds, ((SOAResource)resource).ExpireSeconds, ((SOAResource)resource).MinimumTTL); break; } var record = new DNSRecord(record_name, record_class, record_ttl, resource); if (record.Resource.Type == ResourceRecordType.START_OF_AUTHORITY) { if (start_of_authority == null) { logger.Trace("Found SOA: {0}", record); start_of_authority = record; } else { throw new InvalidDataException("Cannot have more than one SOA record in zone"); } } else { logger.Trace("Found other record: {0}", record); records.Add(record); } } else if (entry.Name == "relay") { if (entry.Attributes["address"] == null || entry.Attributes["port"] == null) { throw new InvalidDataException("relay record must have address and port"); } IPAddress address; int port; try { address = IPAddress.Parse(entry.Attributes["address"].Value); } catch (FormatException) { throw new InvalidDataException(entry.Attributes["address"].Value + " is not a valid IPv4 address"); } try { port = int.Parse(entry.Attributes["port"].Value); } catch (FormatException) { throw new InvalidDataException(entry.Attributes["port"].Value + " is not a valid port"); } try { relays.Add(new IPEndPoint(address, port)); } catch (ArgumentOutOfRangeException) { throw new InvalidDataException(entry.Attributes["port"].Value + " is not a valid port"); } logger.Trace("Found relay: {0}:{1}", address, port); } else { throw new InvalidDataException(entry.Name + " is not a valid zone entry"); } } if (start_of_authority == null) { throw new InvalidDataException("Zone does not have SOA record"); } var zone = new DNSZone(start_of_authority, relays.ToArray()); foreach (var record in records) { if (record.TimeToLive < ((SOAResource)start_of_authority.Resource).MinimumTTL) { logger.Trace("Correcting TTL: Record {0} has smaller TTL than SOA MinTTL {1}", record, start_of_authority); record.TimeToLive = ((SOAResource)start_of_authority.Resource).MinimumTTL; } zone.Add(record); } return(zone); }