New GitHub and NuGet package: Division42.NetworkTools

For a couple of little projects I’ve worked on over the years, I’ve needed some network functionality. So, I cobbled together chunks of code I had and put them together into one library – I put that in GitHub, and published it as a NuGet package.

This library is compiled against .NET 4.5 and offers functionality for:

  • Ping – using the ICMP protocol, determines if a remote host is connected to the network and responding to pings.
  • Traceroute – essentially pings a remote host, and looks up each stop along the way to show you the route from your computer, to the destination computer.
  • Port scan – scans a computer and attempts to make a connection on a port. This shows you which ports are open on a computer.
  • Whois – uses the “whois” protocol to look up domain registration details.

Below, I’ll show a complete example how to use each of these, and a pointer to a gist where you can get the sample code.

Ping:
Ping is a simply mechanism that simply asks if the other computer is there. If ping is enabled on the remote machine, it will respond. This is part of the ICMP protocol. In this case, I am just wrapping the built-in functionality of the .NET Framework, because I wanted it work slightly differently. Here’s an example of how to use the ping functionality:

private static void DemoPing()
{
    String hostName = "www.yahoo.com";
    IPingManager pingManager = 
        new PingManager(hostName, TimeSpan.FromSeconds(2));
            
    // Wire up the event that will be fired when either: 
    //      we get a response, or get an error.
    pingManager.PingResult += (sender, e) =>
    {
        String responseString;

        if (e.Success)
        {
            responseString = String.Format(
                "Reply from {0}: bytes={1} time={2}ms TTL={3}", e.Reply.Address,
                e.Reply.Buffer.Length, e.Reply.RoundtripTime, e.Reply.Options.Ttl);
        }
        else
        {
            responseString = e.LastException.InnerException.Message;
        }

        Console.WriteLine(responseString);
    };

    Console.WriteLine("Pinging {0}...",hostName);
    Console.WriteLine();
    pingManager.Start();

    // Ping for 5 seconds.
    Thread.Sleep(5000);

    pingManager.Stop();
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

which results in an output like this:

image

Traceroute:
Traceroute is a mechanism that looks at the “hops” along the while, while you do a ping – to see all of the routers through which the request went. To use traceroute functionality, you can use code like this:

private async static Task DemoTraceRoute()
{
    String hostName = "www.yahoo.com";

    TraceRouteManager traceRouteManager = new TraceRouteManager();
    traceRouteManager.TraceRouteNodeFound += (sender, e) =>
    {
        Console.WriteLine(e.Detail.ToString());
    };
    traceRouteManager.TraceRouteComplete += (sender, e) =>
    {
        Console.WriteLine("Trace complete.");
    };

    Console.WriteLine("Tracing route to {0}:", hostName);
    Console.WriteLine();
    Console.Write(TraceRouteHopDetail.FormattedTextHeader);
            
    Task<IEnumerable<TraceRouteHopDetail>> traceEntries = 
        traceRouteManager.ExecuteTraceRoute(hostName);

    traceEntries.Wait();

    // We dumped the results to the screen as we got them, but 
    // they are also available in "traceEntries.Result" now, too.
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

which results in an output like this:

image

Port scan:
A port scan simply attempts to try to connect to various TCP or UDP ports to see if there is a response. As you might know, when a port is open on a computer, that is a mechanism for another program to connect to that computer. For example, web servers host HTTP on port 80, and HTTPS on port 443. However, you might want to see what other ports are available. So, you can use some code like this:

private static void DemoPortScan()
{
    String hostName = "localhost";

    PortScanManager portScanManager = new PortScanManager(hostName);

    portScanManager.PortScanResult += (sender, e) =>
    {
        Console.WriteLine("Found port {0} ({1})", e.Port, e.PortType);
    };

    Console.WriteLine("Scanning ports on {0}", hostName);
    Console.WriteLine();

    portScanManager.Start(1, 200, PortTypes.Tcp);

    Task.WaitAll(portScanManager.Tasks.ToArray());

    Console.WriteLine();
    Console.WriteLine("Port scan complete.");
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

which results in an output like this:

image

Whois:
Whois is an internet facility where you can retrieve domain registration information. Meaning, you can find out to whom a domain is registered. The tricky part is you typically have to make at least 2 calls. One, to the top-level-domain root servers to find out who manages “whois” for the domain, then make a second request to that whois server to find out the details of the actual domain you were looking up. To use this, you can use code like this:

private static void DemoWhois()
{
    String domainName = "division42.com";

    WhoisManager whoisManager = new WhoisManager();

    Console.WriteLine("Looking up whois information for " + domainName);
    Console.WriteLine();

    String result = whoisManager.ExecuteWhoisForDomain("=" + domainName);

    IEnumerable<String> whoisServers = whoisManager.FindWhoisServerInOutput(result);

    if (whoisServers != null && whoisServers.Count() > 0)
    {
        String actualResult = whoisManager.ExecuteWhoisForDomain(domainName, whoisServers.First());

        Console.WriteLine(actualResult);
    }
    else
    {
        Console.WriteLine(result);
    }
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

which results in an output like this:

image

Sample code:
To get the sample code for the above, see this gist:

https://gist.github.com/RobSeder/3325a5631b7910e36c38

GitHub:
This project is open-source and is available here (with GPLv3 license):

https://github.com/Division42LLC/Division42.NetworkTools

As of this writing, this has around 80% unit test code coverage, and is fully documented with XMl code comments.

NuGet:
This project is also published in NuGet and is available here:

https://www.nuget.org/packages/Division42.NetworkTools/

or you can of course install it from the Package Manager Console in Visual Studio:

image

or by right-clicking on your project and choosing “Manage NuGet Packages”:

image

Next steps…
As I said, I cobbled together VERY old code, and tried to clean it up, add unit tests etc. So, although this works – the API is inconsistent. One feature uses async/await and returns the results, while another fires events, and also returns the results, etc, etc. I’d like to make a consistent approach to each piece of functionality so that they all work similarly.

Please let me know if you have any ideas, questions or problems with this component!

Posted in Computers and Internet, General, Infrastructure, Open Source, Organization will set you free, Professional Development, Uncategorized
One comment on “New GitHub and NuGet package: Division42.NetworkTools
  1. […] wanted to get familiar with this, so I immediately thought I’d whip up a simple UI for my new Division42 Network Tools NuGet package. This app has 4 pieces of functionality: ping, traceroute, port scan, and […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 2 other followers

%d bloggers like this: