singrdk/base/Applications/Network/DNS/DNS.cs

351 lines
11 KiB
C#
Raw Normal View History

2008-03-05 09:52:00 -05:00
////////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Note: Simple Singularity test program.
//
using System;
using System.Diagnostics;
using System.Net.IP;
using Microsoft.SingSharp;
using Microsoft.Singularity;
using Microsoft.Singularity.Channels;
using Microsoft.Singularity.Directory;
using NetStack.Contracts;
using NetStack.Channels.Public;
using Microsoft.Contracts;
using Microsoft.SingSharp.Reflection;
using Microsoft.Singularity.Applications;
using Microsoft.Singularity.Io;
using Microsoft.Singularity.Configuration;
[assembly: Transform(typeof(ApplicationResourceTransform))]
2008-11-17 18:29:00 -05:00
namespace Microsoft.Singularity.Applications.Network
2008-03-05 09:52:00 -05:00
{
[ConsoleCategory(HelpMessage="Add network routing information", DefaultAction=true)]
2008-11-17 18:29:00 -05:00
internal class AddConfig
{
2008-03-05 09:52:00 -05:00
[InputEndpoint("data")]
public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
[OutputEndpoint("data")]
public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
[Endpoint]
public readonly TRef<DNSContract.Imp:Start> dnsRef;
2008-11-17 18:29:00 -05:00
[StringArrayParameter( "Servers", HelpMessage="Servers to add")]
2008-03-05 09:52:00 -05:00
internal string[] servers;
reflective internal AddConfig();
2008-11-17 18:29:00 -05:00
internal int AppMain()
{
DebugStub.Break();
2008-03-05 09:52:00 -05:00
DNS.Add(this);
2008-11-17 18:29:00 -05:00
return 0;
2008-03-05 09:52:00 -05:00
}
}
[ConsoleCategory(Action="show", HelpMessage="Show DNS information")]
2008-11-17 18:29:00 -05:00
internal class ShowConfig
{
2008-03-05 09:52:00 -05:00
[Endpoint]
public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
[Endpoint]
public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
[Endpoint]
public readonly TRef<DNSContract.Imp:Start> dnsRef;
reflective internal ShowConfig();
2008-11-17 18:29:00 -05:00
internal int AppMain()
{
2008-03-05 09:52:00 -05:00
return DNS.Show(this);
}
}
[ConsoleCategory(Action="delete", HelpMessage="Delete DNS information")]
2008-11-17 18:29:00 -05:00
internal class DeleteConfig
{
2008-03-05 09:52:00 -05:00
[Endpoint]
public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
[Endpoint]
public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
[Endpoint]
public readonly TRef<DNSContract.Imp:Start> dnsRef;
2008-11-17 18:29:00 -05:00
[StringArrayParameter( "Servers", Mandatory=true, Position=0, HelpMessage="Servers to delete")]
2008-03-05 09:52:00 -05:00
internal string[] servers;
reflective internal DeleteConfig();
2008-11-17 18:29:00 -05:00
internal int AppMain()
{
2008-03-05 09:52:00 -05:00
DNS.Delete(this);
2008-11-17 18:29:00 -05:00
return 0;
2008-03-05 09:52:00 -05:00
}
}
[ConsoleCategory(Action="rotate", HelpMessage="Rotate name servers")]
2008-11-17 18:29:00 -05:00
internal class RotateConfig
{
2008-03-05 09:52:00 -05:00
[Endpoint]
public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
[Endpoint]
public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
[Endpoint]
public readonly TRef<DNSContract.Imp:Start> dnsRef;
reflective internal RotateConfig();
2008-11-17 18:29:00 -05:00
internal int AppMain()
{
2008-03-05 09:52:00 -05:00
DNS.Rotate(this);
2008-11-17 18:29:00 -05:00
return 0;
2008-03-05 09:52:00 -05:00
}
}
[ConsoleCategory(Action="query", HelpMessage="Query DNS information")]
2008-11-17 18:29:00 -05:00
internal class QueryConfig
{
2008-03-05 09:52:00 -05:00
[Endpoint]
public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
[Endpoint]
public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
[Endpoint]
public readonly TRef<DNSContract.Imp:Start> dnsRef;
[Endpoint]
public readonly TRef<IPContract.Imp:Start> ipRef;
2008-11-17 18:29:00 -05:00
2008-03-05 09:52:00 -05:00
[StringParameter( "host", Mandatory=true, Position=0, HelpMessage="host to be queried")]
internal string name;
reflective internal QueryConfig();
2008-11-17 18:29:00 -05:00
internal int AppMain()
{
2008-03-05 09:52:00 -05:00
DNS.Query(this);
2008-11-17 18:29:00 -05:00
return 0;
2008-03-05 09:52:00 -05:00
}
}
public class DNS
{
internal static int Add(AddConfig! config)
{
2008-11-17 18:29:00 -05:00
DebugStub.Break();
if (config.servers == null) {
Console.WriteLine("no servers given");
2008-03-05 09:52:00 -05:00
return -1;
2008-11-17 18:29:00 -05:00
}
DNSContract.Imp dnsConn = ((!)config.dnsRef).Acquire();
2008-03-05 09:52:00 -05:00
if (dnsConn == null) {
Console.WriteLine("Could not initialize DNS endpoint.");
2008-11-17 18:29:00 -05:00
delete dnsConn;
2008-03-05 09:52:00 -05:00
return 1;
}
dnsConn.RecvReady();
2008-11-17 18:29:00 -05:00
for (int i = 0; i < config.servers.Length; i++) {
2008-03-05 09:52:00 -05:00
IPv4 resolver;
2008-11-17 18:29:00 -05:00
Console.WriteLine("Resolving {0}", config.servers[i]);
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
if (IPv4.Parse(config.servers[i], out resolver) == false) {
2008-03-05 09:52:00 -05:00
Console.WriteLine("Invalid IP address: {0}", config.servers[i]);
}
dnsConn.SendAddNameServer((uint)resolver);
dnsConn.RecvAck();
}
2008-11-17 18:29:00 -05:00
delete dnsConn;
2008-03-05 09:52:00 -05:00
return 0;
}
internal static int Delete(DeleteConfig! config)
2008-11-17 18:29:00 -05:00
{
if (config.servers == null) {
Console.WriteLine("no servers given");
2008-03-05 09:52:00 -05:00
return -1;
2008-11-17 18:29:00 -05:00
}
DNSContract.Imp dnsConn = ((!)config.dnsRef).Acquire();
2008-03-05 09:52:00 -05:00
if (dnsConn == null) {
Console.WriteLine("Could not initialize DNS endpoint.");
return 1;
}
dnsConn.RecvReady();
2008-11-17 18:29:00 -05:00
for (int i = 0; i < config.servers.Length; i++) {
2008-03-05 09:52:00 -05:00
IPv4 resolver;
2008-11-17 18:29:00 -05:00
if (IPv4.Parse(config.servers[i], out resolver) == false) {
2008-03-05 09:52:00 -05:00
Console.WriteLine("Invalid IP address: {0}", config.servers[i]);
}
dnsConn.SendRemoveNameServer((uint)resolver);
dnsConn.RecvAck();
}
2008-11-17 18:29:00 -05:00
delete dnsConn;
2008-03-05 09:52:00 -05:00
return 0;
}
private static bool QueryInternal(string! name)
{
2008-11-17 18:29:00 -05:00
// NOTE: use the System.NET APIs here, just to exercise our
2008-03-05 09:52:00 -05:00
// port of them. We could also accomplish this by using a
// DNS channel.
2008-11-17 18:29:00 -05:00
try {
2008-03-05 09:52:00 -05:00
System.Net.IPHostEntry hostInfo = System.Net.Dns.GetHostByName(name);
if (hostInfo == null) return false;
IPAddress[] addresses = hostInfo.AddressList;
if (addresses == null) return false;
String[] aliases = hostInfo.Aliases;
if (aliases == null) return false;
Console.Write("Host names: ");
int end = aliases.Length - 1;
2008-11-17 18:29:00 -05:00
for (int i = 0; i <= end; i++) {
2008-03-05 09:52:00 -05:00
Console.Write("{0}{1}", aliases[i], (i == end) ? "" : ", ");
}
Console.Write("\n");
end = addresses.Length - 1;
Console.Write("IP addresses: ");
2008-11-17 18:29:00 -05:00
for (int i = 0; i <= end; i++) {
2008-03-05 09:52:00 -05:00
Console.Write("{0}{1}", addresses[i], (i == end) ? "" : ", ");
}
Console.Write("\n");
return true;
}
2008-11-17 18:29:00 -05:00
catch (System.Net.Sockets.SocketException) {
2008-03-05 09:52:00 -05:00
return false;
}
}
internal static int Query(QueryConfig! config)
2008-11-17 18:29:00 -05:00
requires config.name != null;
2008-03-05 09:52:00 -05:00
{
bool isValid;
2008-11-17 18:29:00 -05:00
string name = config.name;
DNSContract.Imp dnsConn = ((!)config.dnsRef).Acquire();
if (dnsConn == null) {
2008-03-05 09:52:00 -05:00
Console.WriteLine("Could not initialize DNS endpoint.");
return 1;
}
dnsConn.RecvReady();
2008-11-17 18:29:00 -05:00
2008-03-05 09:52:00 -05:00
dnsConn.SendIsValidName(Bitter.FromString(name));
dnsConn.RecvIsValid(out isValid);
2008-11-17 18:29:00 -05:00
if (!isValid) {
2008-03-05 09:52:00 -05:00
Console.Write("Invalid name: {0}", name);
delete dnsConn;
2008-11-17 18:29:00 -05:00
return -1;
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
if (QueryInternal(name)) {
2008-03-05 09:52:00 -05:00
delete dnsConn;
2008-11-17 18:29:00 -05:00
return -1;
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
IPContract.Imp ipConn = ((!)config.ipRef).Acquire();
if (ipConn == null) {
2008-03-05 09:52:00 -05:00
Console.WriteLine("Could not initialize IP endpoint.");
delete dnsConn;
2008-11-17 18:29:00 -05:00
return -1;
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
try {
2008-03-05 09:52:00 -05:00
// Ad-hoc search list
char[]! in ExHeap repDomain;
ipConn.SendGetDomainName();
ipConn.RecvDomainName(out repDomain);
string domain = Bitter.ToString(repDomain);
delete repDomain;
2008-11-17 18:29:00 -05:00
if (domain == null) {
2008-03-05 09:52:00 -05:00
Console.WriteLine("Couldn't resolve \"" + name + "\"");
delete dnsConn;
delete ipConn;
2008-11-17 18:29:00 -05:00
return -1;
2008-03-05 09:52:00 -05:00
}
name = String.Concat(name, ".");
int index = 0;
do {
string guess = String.Concat(name, domain.Substring(index));
dnsConn.SendIsValidName(Bitter.FromString(guess));
dnsConn.RecvIsValid(out isValid);
2008-11-17 18:29:00 -05:00
if (isValid && QueryInternal(guess)) {
2008-03-05 09:52:00 -05:00
delete dnsConn;
delete ipConn;
2008-11-17 18:29:00 -05:00
return -1;
2008-03-05 09:52:00 -05:00
}
index = domain.IndexOf('.', index) + 1;
} while (index > 0 && index < domain.Length);
Console.WriteLine("No IP address found");
}
2008-11-17 18:29:00 -05:00
finally {
2008-03-05 09:52:00 -05:00
}
delete ipConn;
2008-11-17 18:29:00 -05:00
delete dnsConn;
return 0;
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
internal static int Rotate(RotateConfig! config)
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
DNSContract.Imp dnsConn = ((!)config.dnsRef).Acquire();
if (dnsConn == null) {
2008-03-05 09:52:00 -05:00
Console.WriteLine("Could not initialize DNS endpoint.");
return 1;
}
dnsConn.RecvReady();
dnsConn.SendRotateNameServers();
dnsConn.RecvAck();
delete dnsConn;
return 0;
}
internal static int Show(ShowConfig! config)
{
2008-11-17 18:29:00 -05:00
DNSContract.Imp dnsConn = ((!)config.dnsRef).Acquire();
if (dnsConn == null) {
2008-03-05 09:52:00 -05:00
Console.WriteLine("Could not initialize DNS endpoint.");
return 1;
}
dnsConn.RecvReady();
Utils.DNSShow(dnsConn);
delete dnsConn;
return 0;
}
} // end class DNS
}