Skip to content

Code Manual EN

Alexander Pototsky edited this page Oct 20, 2022 · 11 revisions

Structure

NetObserver.csproj

PingUtility(folder)

IcmpRequestSender.cs

IcmpRequestSenderAsync.cs

PingClassic.cs

PingClassicAsync.cs

TracerouteUtility(folder)

Traceroute.cs

TracerouteAsync.cs

IpAdressUtility(folder)

LocalIp.cs

OpenPort.cs

DnsUtility(folder)

DnsInfo.cs

Model(folder)

PortReply.cs

PortStatus.cs


Classes and working with methods.

class IcmpRequestSender [Nostatic]

Makes an attempt to send an ICMP request.

Uses the standard Microsoft library - System.Net.NetworkInformation. Creates instances of the Ping and PingReply classes.

Methods:

  • public PingReply RequestIcmp(string hostname) - Accepts the name of the destination host to send a single ICMP-ECHO packet. [return PingReply]

  • public PingReply RequestIcmp(string hostname, int timeout) - Accepts the name of the destination host to send a single ICMP-ECHO packet, with the specified response timeout. [return PingReply]

  • public PingReply RequestIcmp(string hostname, int timeout, byte[] buffer, PingOptions options) - Allows the developer to fully customize the ICMP request. PingOptions includes: TTL and DontFragment. [return PingReply]


class IcmpRequestSenderAsync [Nostatic]

Makes an asynchronous sending of an ICMP request.

Uses the standard Microsoft library - System.Net.NetworkInformation. Creates instances of the Pin.

Методы:

  • public async Task<PingReply> RequestPingAsync(string hostname) - Accepts the name of the destination host to send a single asynchronous ICMP-ECHO packet. [return Task]

  • public PingReply RequestIcmp(string hostname, int timeout) - Accepts the name of the destination host to send a single asynchronous ICMP-ECHO packet, with the specified response timeout. [return Task]


class PingClassic \ PingClassicAsync [Nostatic]

Implements the classic behavior of the Windows ping utility.

Methods:

  • public List<PingReply> RequestPing(string hostname) - Accepts a hostname and makes 4 ICMP requests in its direction. Returns a list of PingReply objects. [return List]

  • public List<PingReply> RequestPing(string hostname, int repeat) - Gets the hostname and sends the number of ICMP requests specified in repeat to it. [return List]

  • public List<PingReply> RequestPing(string hostname, int timeout, int repeat) - Receives a hostname and makes the number of ICMP requests specified in repeat to it. Added the ability to set a timeout for waiting for a response from an ICMP request. [return List]


class Traceroute \ TracerouteAsync [Nostatic]

Implements the traceroute utility in code. DOES NOT generate response delay statistics like it does in a Windows utility.

Field:

All settings are taken from the default settings in documentation of the ping utility.

  • private int _timeout = 4000; - Specifies the maximum time (after sending a ping message) to wait for an ICMP ping response message in milliseconds by default.

  • private int _maxTtl = 30; - Specifies the default number of routing hosts that can forward a Ping packet before it is discarded.

  • private byte[] _buffer = new byte[32]; - A default Byte array containing data sent with an ICMP ping message and returned with an ICMP ping response message.

  • private bool _fragment = false; - Specifies a value that controls the fragmentation of packets sent to the remote host.

Methods:

  • public IEnumerable GetIpTraceRoute(string hostname) - Returns a list of route IP addresses to the host specified in hostname. [return IEnumerable]

  • public IEnumerable<string> GetIpTraceRoute(string hostname, int timeout, byte[] buffer, bool frag, int ttl, int maxTll) - Allows the user to fully customize the trace. A special feature is the indication of the TTL start for the ICMP packet in the int ttl argument.[return IEnumerable]

  • public IEnumerable<PingReply> GetDetailTraceRoute(string hostname) - Returns a list of IP addresses on the route to the specified hostname, with detailed information about each node on the route. The information is contained in the PingReply object. [return IEnumerable]

  • public IEnumerable<PingReply> GetDetailTraceRoute(string hostname, int timeout, byte[] buffer, bool frag, int ttl, int maxTtl) - Allows the user to fully customize the tracing detailed route tracing. A feature is the indication of the start TTL for the ICMP packet in the int ttl argument and the maximum number of hops in maxTll. [return IEnumerable]


class LocalIp [Nostatic]

Specifies the local IP address of the host. At the beginning, it takes the IP addresses assigned "DHCP", if there are no such, it will give the address assigned "manually".

Field:

  • private static List<string> _listIp; - List to fill with the results of the GetIpv4Localhost() method.
  • private static List<Tuple<PrefixOrigin, string>> _listTuples - List to fill with the results of the GetAllIpv4NetInterface() method.

Methods:

  • public static string GetIpLocalhost() -Returns the IP address of the local host. [return string]
  • public static List<Tuple<PrefixOrigin, string>> GetAllIpv4NetInterface() - Returns a tuple indicating how the network prefix of the IP address was set and the IP address itself. [return List<Tuple<PrefixOrigin, string>>]

class DnsInfo

Breaks down simple domain name resolution functionality into separate tasks.

Methods:

  • public static string DnsHostname(string hostname) - Gets a DNS name of the host. [return string]
  • public static string[] DnsAliases(string hostname) - Gets a list of aliases that are associated with a host. [return List<Tuple<PrefixOrigin, string>>] !! Warning!!
  • public static IPAddress[] DnsAddressList(string hostname) - Gets a list of IP addresses that are associated with a host. [return string[]]

class OpenPort [Nostatic]

Trying to check whether the port is open for the TCP protocol.

It is recommended to first check the availability of a remote host. Otherwise, if the host is not available - the working time of the method will be greatly increased.

Методы:

  • public PortReply GetOpenPort(string hostname, int port) - Checks whether the port is open and returns the object with information. [return PortReply]
  • public List<PortReply> GetOpenPort(string hostname, int startPort, int endPort) - Checks whether the ports are open in the indicated range and returns the object with information. [return **List **]

enum PortStatus

Contains enumeration of statuses for working in the PortredPly class.

Enumeration:

  • Open = 1, Closed

class PortReply [Nostatic]

Model class for working with OpenPort class checking utilities.

Properties:

  • public int Port { get; private set; } - Must contain the port number. Opens for recording only through the constructor.
  • public PortStatus Status { get; private set; } - Must contain the status from Enum Portstatus. Opens for recording only through the constructor.

Constructor:

  • public PortReply(int port, PortStatus status) - Writes out the transmitted properties.

Examples

Class IcmpRequestSender

public PingReply RequestIcmp(string hostname)

using NetObserver.PingUtility;
using System.Net.NetworkInformation;

IcmpRequestSender testReques = new IcmpRequestSender();

PingReply result = testReques.RequestIcmp("google.com");

Console.WriteLine(result.Address);
Console.WriteLine(result.RoundtripTime);
Console.WriteLine(result.Options.Ttl);
Console.WriteLine(result.Options.DontFragment);
Console.WriteLine(result.Buffer.Length);

image


public PingReply RequestIcmp(string hostname, int timeout)

using NetObserver.PingUtility;
using System.Net.NetworkInformation;

IcmpRequestSender testReques = new IcmpRequestSender();

PingReply result = testReques.RequestIcmp("google.com", 3000);

Console.WriteLine(result.Address);
Console.WriteLine(result.RoundtripTime);
Console.WriteLine(result.Options.Ttl);
Console.WriteLine(result.Options.DontFragment);
Console.WriteLine(result.Buffer.Length);

image


public PingReply RequestIcmp(string hostname, int timeout, byte[] buffer, PingOptions options)

using NetObserver.PingUtility;
using System.Net.NetworkInformation;

byte[] buffer = new byte[32];
PingOptions options = new PingOptions() { Ttl = 32, DontFragment = true};

IcmpRequestSender testReques = new IcmpRequestSender();

PingReply result = testReques.RequestIcmp("google.com", 3000, buffer, options);

Console.WriteLine(result.Address);
Console.WriteLine(result.RoundtripTime);
Console.WriteLine(result.Options.Ttl);
Console.WriteLine(result.Options.DontFragment);
Console.WriteLine(result.Buffer.Length);

image


Class IcmpRequestSenderAsync

public async Task<PingReply> RequestPingAsync(string hostname)

using NetObserver.PingUtility;
using System.Net.NetworkInformation;

IcmpRequestSenderAsync test = new IcmpRequestSenderAsync();

List<string> list1 = new List<string> { "ya.ru", "8.8.8.8", "vk.com" };

foreach(string s in list1)
{
    var rr = Test(s, 1000);
    Console.WriteLine(rr.Result.Buffer);
    Console.WriteLine("------");
}

async Task<PingReply> Test(string name, int timeout)
{
    var res =  await test.RequestPingAsync(name);
    Console.Write(res.Address + " : " + res.RoundtripTime);
    Console.WriteLine();
    Console.WriteLine(DateTime.UtcNow);
    Console.WriteLine();
    return res;
}

image


public async Task<PingReply> RequestPingAsync(string hostname)

using NetObserver.PingUtility;
using System.Net.NetworkInformation;

IcmpRequestSenderAsync test = new IcmpRequestSenderAsync();

List<string> list1 = new List<string> { "ya.ru", "8.8.8.8", "vk.com" };

foreach(string s in list1)
{
    var rr = Test(s, 1000);
    Console.WriteLine(rr.Result.Buffer);
    Console.WriteLine("------");
}

async Task<PingReply> Test(string name, int timeout)
{
    var res =  await test.RequestPingAsync(name , 4000);
    Console.Write(res.Address + " : " + res.RoundtripTime);
    Console.WriteLine();
    Console.WriteLine(DateTime.UtcNow);
    Console.WriteLine();
    return res;
}

image


Class PingClassic

public List<PingReply> RequestPing(string hostname)

using NetObserver.PingUtility;
using System.Net.NetworkInformation;

PingClassic testReques = new PingClassic();

List<PingReply> result = testReques.RequestPing("google.com");

foreach (PingReply reply in result)
{
    Console.WriteLine(reply.Address);
    Console.WriteLine(reply.RoundtripTime);
    Console.WriteLine(reply.Status);
    Console.WriteLine("------------");
}

image


public List<PingReply> RequestPing(string hostname, int repeat)

using NetObserver.PingUtility;
using System.Net.NetworkInformation;

PingClassic testReques = new PingClassic();

List<PingReply> result = testReques.RequestPing("google.com", 7);

foreach (PingReply reply in result)
{
    Console.WriteLine(reply.Address);
    Console.WriteLine(reply.RoundtripTime);
    Console.WriteLine(reply.Status);
    Console.WriteLine("------------");
}

image


public List<PingReply> RequestPing(string hostname, int timeout, int repeat)

using NetObserver.PingUtility;
using System.Net.NetworkInformation;

PingClassic testReques = new PingClassic();

List<PingReply> result = testReques.RequestPing("google.com", 2000 , 5);

foreach (PingReply reply in result)
{
    Console.WriteLine(reply.Address);
    Console.WriteLine(reply.RoundtripTime);
    Console.WriteLine(reply.Status);
    Console.WriteLine("------------");
}

image


Class Traceroute

public IEnumerable<string> GetIpTraceRoute(string hostname)

using NetObserver.TracerouteUtility;

Traceroute testReques = new Traceroute();

List<string> result = (List<string>)testReques.GetIpTraceRoute("google.com");

foreach (string route in result)
{
    Console.WriteLine(route);
}

image


public IEnumerable<string> GetIpTraceRoute(string hostname, int timeout, byte[] buffer, bool frag = _fragment, int ttl = 1, int maxTll = _maxTtl)

using NetObserver.TracerouteUtility;

string hostname = "google.com";
int timeout = 2000;
byte[] buffer = new byte[32];
bool fraq = true;
int ttl = 1; // Start value ttl!
int maxTtl = 32; 

Traceroute testReques = new Traceroute();

List<string> result = (List<string>)testReques.GetIpTraceRoute(hostname, timeout, buffer, fraq, ttl, maxTtl);

foreach (string route in result)
{
    Console.WriteLine(route);
}

image


public IEnumerable<PingReply> GetDetailTraceRoute(string hostname)

using NetObserver.TracerouteUtility;
using System.Net.NetworkInformation;

string hostname = "google.com";

Traceroute testReques = new Traceroute();

List<PingReply> result = (List<PingReply>)testReques.GetDetailTraceRoute(hostname);

var tpm = result;

image


public IEnumerable<PingReply> GetDetailTraceRoute(string hostname, int timeout, byte[] buffer, bool frag = _fragment, int ttl = 1, int maxTtl = _maxTtl)

using NetObserver.TracerouteUtility;
using System.Net.NetworkInformation;

string hostname = "google.com";
int timeout = 2000;
byte[] buffer = new byte[32];
bool fraq = true;
int ttl = 1; // Start value ttl!
int maxTtl = 32; 

Traceroute testReques = new Traceroute();

List<PingReply> result = (List<PingReply>)testReques.GetDetailTraceRoute(hostname, timeout, buffer, fraq, ttl, maxTtl);

foreach(PingReply route in result)
{
    Console.WriteLine(route.Address);
    Console.WriteLine(route.RoundtripTime);
    Console.WriteLine(route.Status);
    Console.WriteLine("---------------");
}

image

image


Class LocalIp

public static string GetIpv4Localhost()

using NetObserver.IpAdressUtility;


string result = LocalIp.GetIpv4Localhost();

Console.WriteLine(result);

image


public static List<Tuple<PrefixOrigin, string>> GetAllIpv4NetInterface()

using NetObserver.IpAdressUtility;
using System.Net.NetworkInformation;

List<Tuple<PrefixOrigin, string>> getList = LocalIp.GetAllIpv4NetInterface();

foreach (var ip in getList)
{
    Console.WriteLine(ip.Item1);
    Console.WriteLine(ip.Item2);
    Console.WriteLine("-------");
}

image

image


Class DnsInfo

public static string DnsHostname(string hostname)

using NetObserver.DnsUtility;

var test = DnsInfo.DnsHostname("8.8.8.8");

Console.WriteLine(test);

image


public static string DnsHostname(string hostname)

using NetObserver.DnsUtility;

var test = DnsInfo.DnsHostname("8.8.8.8");

Console.WriteLine(test);

public static string[] DnsAliases(string hostname)

using NetObserver.DnsUtility;

var test = DnsInfo.DnsAliases("ghs.googlehosted.com");

Console.WriteLine(test);

public static IPAddress[] DnsAddressList(string hostname)

using NetObserver.DnsUtility;

var test = DnsInfo.DnsAddressList("google.com");

foreach (var ip in test)
{
    Console.WriteLine(ip);
}

image


Class OpenPort

public PortReply GetOpenPort(string hostname, int port)

using NetObserver.IpAdressUtility;

var hostname = "8.8.8.8";

OpenPort reqeuest = new OpenPort();
var response = reqeuest.GetOpenPort(hostname, 27015);
Console.WriteLine("Ready");
Console.WriteLine(response.Port);
Console.WriteLine(response.Status.ToString());

image


public List<PortReply> GetOpenPort(string hostname, int startPort, int endPort)

using NetObserver.IpAdressUtility;

var hostname = "8.8.8.8";

OpenPort reqeuest = new OpenPort();
var response = reqeuest.GetOpenPort(hostname, 27013, 27017);
Console.WriteLine("Ready");
Console.WriteLine(response.Count);

image

Clone this wiki locally