PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/base/Applications/Network/IPConfig/IPConfig.cs

#
C# | 382 lines | 294 code | 75 blank | 13 comment | 35 complexity | fc28d11b474de09b5aaa4990e84e81fe 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.Singularity;
  13. using Microsoft.Singularity.Channels;
  14. using Microsoft.Singularity.Directory;
  15. using Microsoft.SingSharp;
  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="Configure nic with IP addresses", DefaultAction=true)]
  27. internal class DefaultConfig {
  28. [InputEndpoint("data")]
  29. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  30. [OutputEndpoint("data")]
  31. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  32. [Endpoint]
  33. public readonly TRef<IPContract.Imp:Start> ipRef;
  34. [StringParameter( "device", Mandatory=true, Position=0, HelpMessage="Nic to configure")]
  35. internal string device;
  36. [StringParameter( "address", Mandatory=true, Position=1, HelpMessage="address to give it")]
  37. internal string address;
  38. [StringParameter( "mask", Mandatory=true, Position=2, HelpMessage="mask to use")]
  39. internal string mask;
  40. [StringParameter( "gateway", Mandatory=true, Position=3, HelpMessage="gateway")]
  41. internal string gateway;
  42. reflective internal DefaultConfig();
  43. internal int AppMain() {
  44. return IPConfig.DefaultMain(this);
  45. }
  46. }
  47. [ConsoleCategory(Action="show", HelpMessage="SHOW nic' IP configuration" )]
  48. internal class ShowConfig {
  49. [Endpoint]
  50. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  51. [Endpoint]
  52. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  53. [Endpoint]
  54. public readonly TRef<IPContract.Imp:Start> ipRef;
  55. [Endpoint]
  56. public readonly TRef<DNSContract.Imp:Start> dnsRef;
  57. reflective internal ShowConfig();
  58. internal int AppMain() {
  59. return IPConfig.Show(this);
  60. }
  61. }
  62. [ConsoleCategory(Action="dhcp", HelpMessage="Configure nic via DHCP" )]
  63. internal class DhcpConfig {
  64. [Endpoint]
  65. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  66. [Endpoint]
  67. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  68. [Endpoint]
  69. public readonly TRef<IPContract.Imp:Start> ipRef;
  70. [StringParameter( "device", Mandatory=true, Position=0, HelpMessage="Nic to configure")]
  71. internal string device;
  72. [StringParameter( "verb", Mandatory=true, Position=1, HelpMessage="verb (start or stop).")]
  73. internal string action;
  74. reflective internal DhcpConfig();
  75. internal int AppMain() {
  76. return IPConfig.Dhcp(this);
  77. }
  78. }
  79. [ConsoleCategory(Action="verify", HelpMessage="check device interface" )]
  80. internal class VerifyConfig {
  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<IPContract.Imp:Start> ipRef;
  87. [StringParameter( "device", Mandatory=true, Position=0, HelpMessage="Nic to configure")]
  88. internal string device;
  89. reflective internal VerifyConfig();
  90. internal int AppMain() {
  91. return IPConfig.Verify(this);
  92. }
  93. }
  94. /// <summary>
  95. /// Class for interface configuration related commands.
  96. /// </summary>
  97. public class IPConfig
  98. {
  99. // Marked unsafe for now since checker
  100. // has problems with custom vector access.
  101. public static void ShowConfig(IPContract.Imp:ReadyState! ipConn, DNSContract.Imp:ReadyState! dnsConn)
  102. {
  103. int done = 0;
  104. char[]! in ExHeap hostName, domainName;
  105. ipConn.SendGetHostName();
  106. ipConn.RecvHostName(out hostName);
  107. ipConn.SendGetDomainName();
  108. ipConn.RecvDomainName(out domainName);
  109. Console.WriteLine("Hostname: {0}.{1}", Bitter.ToString2(hostName), Bitter.ToString2(domainName));
  110. delete hostName;
  111. delete domainName;
  112. Console.WriteLine("DNS Servers:");
  113. try {
  114. Utils.DNSShow(dnsConn);
  115. }
  116. finally {
  117. }
  118. char[][]! in ExHeap ifNames;
  119. ipConn.SendGetInterfaces();
  120. ipConn.RecvInterfaceList(out ifNames);
  121. for (int i = 0; i < ifNames.Length; ++i) {
  122. CustomVector.Expose(ifNames, i);
  123. char[] in ExHeap ifName = ifNames[i];
  124. if (ifName == null)
  125. throw new Exception("ifName is null");
  126. string! deviceName = Bitter.ToString2(ifName);
  127. CustomVector.UnExpose(ifNames, i);
  128. ipConn.SendGetInterfaceState(Bitter.FromString2(deviceName));
  129. switch receive {
  130. case ipConn.InterfaceNotFound() :
  131. Console.WriteLine("Unexpected error");
  132. break;
  133. case ipConn.InterfaceState(InterfaceInfo ifInfo) :
  134. ShowInterface(ifInfo, deviceName);
  135. break;
  136. case ipConn.ChannelClosed() :
  137. throw new Exception("ipConn channel closed");
  138. }
  139. done++;
  140. }
  141. delete ifNames;
  142. if (done == 0)
  143. Console.WriteLine("No adapters present.");
  144. }
  145. public static void ConfigureInterface(IPContract.Imp! ipConn,
  146. string! adapterName,
  147. string! addressString,
  148. string! netmaskString,
  149. string! gatewayString)
  150. {
  151. IPv4 ifAddress, netmask, gateway;
  152. if (IPv4.Parse(addressString, out ifAddress) == false ||
  153. IPv4.Parse(netmaskString, out netmask) == false ||
  154. IPv4.Parse(gatewayString, out gateway) == false)
  155. {
  156. return;
  157. }
  158. ipConn.SendSetInterfaceState(Bitter.FromString2(adapterName),
  159. (uint)ifAddress, (uint)netmask, (uint)gateway);
  160. switch receive {
  161. case ipConn.InterfaceNotFound() :
  162. Console.WriteLine("No such interface found");
  163. break;
  164. case ipConn.Err() :
  165. Console.WriteLine("Error occurred setting interface state");
  166. break;
  167. case ipConn.OK() :
  168. Console.WriteLine("Successfully set interface state");
  169. break;
  170. }
  171. }
  172. public static void ShowInterface([Claims] InterfaceInfo ifInfo, string! ifName)
  173. {
  174. Console.WriteLine("Interface: {0}", ifName);
  175. Console.WriteLine("Adapter: {0}",
  176. Bitter.ToString(ifInfo.driverName));
  177. Console.WriteLine("Version: {0}",
  178. Bitter.ToString(ifInfo.driverVersion));
  179. Console.WriteLine("MAC address: {0}",
  180. ChannelUtils.HardwareAddressToString(ifInfo.hardwareAddress));
  181. bool found = false;
  182. delete ifInfo.driverName;
  183. delete ifInfo.driverVersion;
  184. InterfaceIPInfo[] in ExHeap ipConfigs = ifInfo.ipConfigs;
  185. if (ipConfigs != null) {
  186. for (int i = 0; i < ipConfigs.Length; i++) {
  187. InterfaceIPInfo ipc = ipConfigs[i];
  188. Console.WriteLine(" Address: {0,-14} NetMask: {1, -14} Gateway: {2, -14}", new IPv4(ipc.address), new IPv4(ipc.netmask), new IPv4(ipc.gateway));
  189. found |= true;
  190. }
  191. delete ipConfigs;
  192. }
  193. if (found == false)
  194. Console.WriteLine(" Not configured.");
  195. }
  196. public static void StartDhcp(IPContract.Imp! ipConn,
  197. string! adapterName)
  198. {
  199. ipConn.SendStartDhcp(Bitter.FromString2(adapterName));
  200. switch receive {
  201. case ipConn.InterfaceNotFound() :
  202. Console.WriteLine("No such interface found");
  203. break;
  204. case ipConn.OK() :
  205. Console.WriteLine("Successfully started DHCP");
  206. break;
  207. }
  208. }
  209. public static void StopDhcp(IPContract.Imp! ipConn,
  210. string! adapterName)
  211. {
  212. ipConn.SendStopDhcp(Bitter.FromString2(adapterName));
  213. switch receive {
  214. case ipConn.InterfaceNotFound() :
  215. Console.WriteLine("No such interface found");
  216. break;
  217. case ipConn.OK() :
  218. Console.WriteLine("Successfully stopped DHCP");
  219. break;
  220. }
  221. }
  222. internal static int Show(ShowConfig! config)
  223. {
  224. IPContract.Imp ipConn = ((!)config.ipRef).Acquire();
  225. if (ipConn == null) {
  226. throw new Exception("Unable to acquire handle to the IP network");
  227. }
  228. ipConn.RecvReady();
  229. DNSContract.Imp dnsConn = ((!)config.dnsRef).Acquire();
  230. if (dnsConn == null) {
  231. Console.WriteLine("Could not initialize DNS endpoint.");
  232. delete ipConn;
  233. return -1;
  234. }
  235. dnsConn.RecvReady();
  236. ShowConfig(ipConn, dnsConn);
  237. delete ipConn;
  238. delete dnsConn;
  239. return 0;
  240. }
  241. internal static int Dhcp(DhcpConfig! config)
  242. {
  243. IPContract.Imp ipConn = ((!)config.ipRef).Acquire();
  244. if (ipConn == null) {
  245. throw new Exception("Unable to acquire handle to the IP network");
  246. }
  247. ipConn.RecvReady();
  248. if (config.action == "start") {
  249. StartDhcp(ipConn, (!)config.device);
  250. }
  251. else if (config.action == "stop") {
  252. StopDhcp(ipConn, (!)config.device);
  253. }
  254. else {
  255. Console.WriteLine("Unknown dhcp action:{0}", config.action);
  256. }
  257. delete ipConn;
  258. return 0;
  259. }
  260. internal static int Verify(VerifyConfig! config)
  261. {
  262. string! arg1 = (!)config.device;
  263. IPContract.Imp ipConn = ((!)config.ipRef).Acquire();
  264. if (ipConn == null) {
  265. throw new Exception("Unable to acquire handle to the IP network");
  266. }
  267. ipConn.RecvReady();
  268. ipConn.SendGetInterfaceState(Bitter.FromString2(arg1));
  269. switch receive {
  270. case ipConn.InterfaceNotFound() :
  271. Console.WriteLine("Interface not found: \"" + arg1 + "\"");
  272. break;
  273. case ipConn.InterfaceState(InterfaceInfo ifInfo) :
  274. ShowInterface(ifInfo, arg1);
  275. break;
  276. case ipConn.ChannelClosed() :
  277. throw new Exception("ipConn channel closed");
  278. }
  279. delete ipConn;
  280. return 0;
  281. }
  282. internal static int DefaultMain(DefaultConfig! config)
  283. {
  284. IPContract.Imp ipConn = ((!)config.ipRef).Acquire();
  285. if (ipConn == null) {
  286. throw new Exception("Unable to acquire handle to the IP network");
  287. }
  288. ipConn.RecvReady();
  289. try {
  290. ConfigureInterface(ipConn,
  291. (!)config.device,
  292. (!)config.address,
  293. (!)config.mask,
  294. (!)config.gateway);
  295. delete ipConn;
  296. return 0;
  297. }
  298. catch (Exception e) {
  299. Console.WriteLine("Caught exception: " + e);
  300. }
  301. finally {
  302. }
  303. delete ipConn;
  304. return 1;
  305. }
  306. } // end class IPConfig
  307. }