PageRenderTime 75ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 350 lines | 271 code | 67 blank | 12 comment | 41 complexity | ca35299395b9d01c8be930f6f7a2dfec MD5 | raw file
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Research Singularity
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // Note: Simple Singularity test program.
  8. //
  9. using System;
  10. using System.Diagnostics;
  11. using System.Net.IP;
  12. using Microsoft.SingSharp;
  13. using Microsoft.Singularity;
  14. using Microsoft.Singularity.Channels;
  15. using Microsoft.Singularity.Directory;
  16. using NetStack.Contracts;
  17. using NetStack.Channels.Public;
  18. using Microsoft.Contracts;
  19. using Microsoft.SingSharp.Reflection;
  20. using Microsoft.Singularity.Applications;
  21. using Microsoft.Singularity.Io;
  22. using Microsoft.Singularity.Configuration;
  23. [assembly: Transform(typeof(ApplicationResourceTransform))]
  24. namespace Microsoft.Singularity.Applications.Network
  25. {
  26. [ConsoleCategory(HelpMessage="Add network routing information", DefaultAction=true)]
  27. internal class AddConfig
  28. {
  29. [InputEndpoint("data")]
  30. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  31. [OutputEndpoint("data")]
  32. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  33. [Endpoint]
  34. public readonly TRef<DNSContract.Imp:Start> dnsRef;
  35. [StringArrayParameter( "Servers", HelpMessage="Servers to add")]
  36. internal string[] servers;
  37. reflective internal AddConfig();
  38. internal int AppMain()
  39. {
  40. DebugStub.Break();
  41. DNS.Add(this);
  42. return 0;
  43. }
  44. }
  45. [ConsoleCategory(Action="show", HelpMessage="Show DNS information")]
  46. internal class ShowConfig
  47. {
  48. [Endpoint]
  49. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  50. [Endpoint]
  51. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  52. [Endpoint]
  53. public readonly TRef<DNSContract.Imp:Start> dnsRef;
  54. reflective internal ShowConfig();
  55. internal int AppMain()
  56. {
  57. return DNS.Show(this);
  58. }
  59. }
  60. [ConsoleCategory(Action="delete", HelpMessage="Delete DNS information")]
  61. internal class DeleteConfig
  62. {
  63. [Endpoint]
  64. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  65. [Endpoint]
  66. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  67. [Endpoint]
  68. public readonly TRef<DNSContract.Imp:Start> dnsRef;
  69. [StringArrayParameter( "Servers", Mandatory=true, Position=0, HelpMessage="Servers to delete")]
  70. internal string[] servers;
  71. reflective internal DeleteConfig();
  72. internal int AppMain()
  73. {
  74. DNS.Delete(this);
  75. return 0;
  76. }
  77. }
  78. [ConsoleCategory(Action="rotate", HelpMessage="Rotate name servers")]
  79. internal class RotateConfig
  80. {
  81. [Endpoint]
  82. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  83. [Endpoint]
  84. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  85. [Endpoint]
  86. public readonly TRef<DNSContract.Imp:Start> dnsRef;
  87. reflective internal RotateConfig();
  88. internal int AppMain()
  89. {
  90. DNS.Rotate(this);
  91. return 0;
  92. }
  93. }
  94. [ConsoleCategory(Action="query", HelpMessage="Query DNS information")]
  95. internal class QueryConfig
  96. {
  97. [Endpoint]
  98. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  99. [Endpoint]
  100. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  101. [Endpoint]
  102. public readonly TRef<DNSContract.Imp:Start> dnsRef;
  103. [Endpoint]
  104. public readonly TRef<IPContract.Imp:Start> ipRef;
  105. [StringParameter( "host", Mandatory=true, Position=0, HelpMessage="host to be queried")]
  106. internal string name;
  107. reflective internal QueryConfig();
  108. internal int AppMain()
  109. {
  110. DNS.Query(this);
  111. return 0;
  112. }
  113. }
  114. public class DNS
  115. {
  116. internal static int Add(AddConfig! config)
  117. {
  118. DebugStub.Break();
  119. if (config.servers == null) {
  120. Console.WriteLine("no servers given");
  121. return -1;
  122. }
  123. DNSContract.Imp dnsConn = ((!)config.dnsRef).Acquire();
  124. if (dnsConn == null) {
  125. Console.WriteLine("Could not initialize DNS endpoint.");
  126. delete dnsConn;
  127. return 1;
  128. }
  129. dnsConn.RecvReady();
  130. for (int i = 0; i < config.servers.Length; i++) {
  131. IPv4 resolver;
  132. Console.WriteLine("Resolving {0}", config.servers[i]);
  133. if (IPv4.Parse(config.servers[i], out resolver) == false) {
  134. Console.WriteLine("Invalid IP address: {0}", config.servers[i]);
  135. }
  136. dnsConn.SendAddNameServer((uint)resolver);
  137. dnsConn.RecvAck();
  138. }
  139. delete dnsConn;
  140. return 0;
  141. }
  142. internal static int Delete(DeleteConfig! config)
  143. {
  144. if (config.servers == null) {
  145. Console.WriteLine("no servers given");
  146. return -1;
  147. }
  148. DNSContract.Imp dnsConn = ((!)config.dnsRef).Acquire();
  149. if (dnsConn == null) {
  150. Console.WriteLine("Could not initialize DNS endpoint.");
  151. return 1;
  152. }
  153. dnsConn.RecvReady();
  154. for (int i = 0; i < config.servers.Length; i++) {
  155. IPv4 resolver;
  156. if (IPv4.Parse(config.servers[i], out resolver) == false) {
  157. Console.WriteLine("Invalid IP address: {0}", config.servers[i]);
  158. }
  159. dnsConn.SendRemoveNameServer((uint)resolver);
  160. dnsConn.RecvAck();
  161. }
  162. delete dnsConn;
  163. return 0;
  164. }
  165. private static bool QueryInternal(string! name)
  166. {
  167. // NOTE: use the System.NET APIs here, just to exercise our
  168. // port of them. We could also accomplish this by using a
  169. // DNS channel.
  170. try {
  171. System.Net.IPHostEntry hostInfo = System.Net.Dns.GetHostByName(name);
  172. if (hostInfo == null) return false;
  173. IPAddress[] addresses = hostInfo.AddressList;
  174. if (addresses == null) return false;
  175. String[] aliases = hostInfo.Aliases;
  176. if (aliases == null) return false;
  177. Console.Write("Host names: ");
  178. int end = aliases.Length - 1;
  179. for (int i = 0; i <= end; i++) {
  180. Console.Write("{0}{1}", aliases[i], (i == end) ? "" : ", ");
  181. }
  182. Console.Write("\n");
  183. end = addresses.Length - 1;
  184. Console.Write("IP addresses: ");
  185. for (int i = 0; i <= end; i++) {
  186. Console.Write("{0}{1}", addresses[i], (i == end) ? "" : ", ");
  187. }
  188. Console.Write("\n");
  189. return true;
  190. }
  191. catch (System.Net.Sockets.SocketException) {
  192. return false;
  193. }
  194. }
  195. internal static int Query(QueryConfig! config)
  196. requires config.name != null;
  197. {
  198. bool isValid;
  199. string name = config.name;
  200. DNSContract.Imp dnsConn = ((!)config.dnsRef).Acquire();
  201. if (dnsConn == null) {
  202. Console.WriteLine("Could not initialize DNS endpoint.");
  203. return 1;
  204. }
  205. dnsConn.RecvReady();
  206. dnsConn.SendIsValidName(Bitter.FromString(name));
  207. dnsConn.RecvIsValid(out isValid);
  208. if (!isValid) {
  209. Console.Write("Invalid name: {0}", name);
  210. delete dnsConn;
  211. return -1;
  212. }
  213. if (QueryInternal(name)) {
  214. delete dnsConn;
  215. return -1;
  216. }
  217. IPContract.Imp ipConn = ((!)config.ipRef).Acquire();
  218. if (ipConn == null) {
  219. Console.WriteLine("Could not initialize IP endpoint.");
  220. delete dnsConn;
  221. return -1;
  222. }
  223. try {
  224. // Ad-hoc search list
  225. char[]! in ExHeap repDomain;
  226. ipConn.SendGetDomainName();
  227. ipConn.RecvDomainName(out repDomain);
  228. string domain = Bitter.ToString(repDomain);
  229. delete repDomain;
  230. if (domain == null) {
  231. Console.WriteLine("Couldn't resolve \"" + name + "\"");
  232. delete dnsConn;
  233. delete ipConn;
  234. return -1;
  235. }
  236. name = String.Concat(name, ".");
  237. int index = 0;
  238. do {
  239. string guess = String.Concat(name, domain.Substring(index));
  240. dnsConn.SendIsValidName(Bitter.FromString(guess));
  241. dnsConn.RecvIsValid(out isValid);
  242. if (isValid && QueryInternal(guess)) {
  243. delete dnsConn;
  244. delete ipConn;
  245. return -1;
  246. }
  247. index = domain.IndexOf('.', index) + 1;
  248. } while (index > 0 && index < domain.Length);
  249. Console.WriteLine("No IP address found");
  250. }
  251. finally {
  252. }
  253. delete ipConn;
  254. delete dnsConn;
  255. return 0;
  256. }
  257. internal static int Rotate(RotateConfig! config)
  258. {
  259. DNSContract.Imp dnsConn = ((!)config.dnsRef).Acquire();
  260. if (dnsConn == null) {
  261. Console.WriteLine("Could not initialize DNS endpoint.");
  262. return 1;
  263. }
  264. dnsConn.RecvReady();
  265. dnsConn.SendRotateNameServers();
  266. dnsConn.RecvAck();
  267. delete dnsConn;
  268. return 0;
  269. }
  270. internal static int Show(ShowConfig! config)
  271. {
  272. DNSContract.Imp dnsConn = ((!)config.dnsRef).Acquire();
  273. if (dnsConn == null) {
  274. Console.WriteLine("Could not initialize DNS endpoint.");
  275. return 1;
  276. }
  277. dnsConn.RecvReady();
  278. Utils.DNSShow(dnsConn);
  279. delete dnsConn;
  280. return 0;
  281. }
  282. } // end class DNS
  283. }