MainForm.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Copyright (C) 2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Net;
  11. using System.Net.NetworkInformation;
  12. using System.Net.Sockets;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Windows.Forms;
  16. using ISCSI.Server;
  17. using DiskAccessLibrary;
  18. using Utilities;
  19. namespace ISCSIConsole
  20. {
  21. public partial class MainForm : Form
  22. {
  23. private ISCSIServer m_server = new ISCSIServer();
  24. private List<ISCSITarget> m_targets = new List<ISCSITarget>();
  25. private UsageCounter m_usageCounter = new UsageCounter();
  26. private bool m_started = false;
  27. public MainForm()
  28. {
  29. InitializeComponent();
  30. }
  31. private void MainForm_Load(object sender, EventArgs e)
  32. {
  33. Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
  34. this.Text += " v" + version.ToString(3);
  35. m_server.OnLogEntry += Program.OnLogEntry;
  36. List<IPAddress> localIPs = GetHostIPAddresses();
  37. KeyValuePairList<string, IPAddress> list = new KeyValuePairList<string, IPAddress>();
  38. list.Add("Any", IPAddress.Any);
  39. foreach (IPAddress address in localIPs)
  40. {
  41. list.Add(address.ToString(), address);
  42. }
  43. comboIPAddress.DataSource = list;
  44. comboIPAddress.DisplayMember = "Key";
  45. comboIPAddress.ValueMember = "Value";
  46. lblStatus.Text = "Author: Tal Aloni (tal.aloni.il@gmail.com)";
  47. #if Win32
  48. if (!SecurityHelper.IsAdministrator())
  49. {
  50. lblStatus.Text = "Some features require administrator privileges and have been disabled";
  51. }
  52. #endif
  53. }
  54. private void btnStart_Click(object sender, EventArgs e)
  55. {
  56. if (!m_started)
  57. {
  58. IPAddress serverAddress = (IPAddress)comboIPAddress.SelectedValue;
  59. int port = Conversion.ToInt32(txtPort.Text, 0);
  60. if (port <= 0 || port > UInt16.MaxValue)
  61. {
  62. MessageBox.Show("Invalid TCP port", "Error");
  63. return;
  64. }
  65. IPEndPoint endpoint = new IPEndPoint(serverAddress, port);
  66. try
  67. {
  68. m_server.Start(endpoint);
  69. }
  70. catch (SocketException ex)
  71. {
  72. MessageBox.Show("Cannot start server, " + ex.Message, "Error");
  73. return;
  74. }
  75. btnStart.Text = "Stop";
  76. txtPort.Enabled = false;
  77. comboIPAddress.Enabled = false;
  78. m_started = true;
  79. UpdateUI();
  80. }
  81. else
  82. {
  83. m_server.Stop();
  84. lblStatus.Text = String.Empty;
  85. m_started = false;
  86. btnStart.Text = "Start";
  87. txtPort.Enabled = true;
  88. comboIPAddress.Enabled = true;
  89. }
  90. }
  91. private void btnAddTarget_Click(object sender, EventArgs e)
  92. {
  93. AddTargetForm addTarget = new AddTargetForm();
  94. DialogResult addTargetResult = addTarget.ShowDialog();
  95. if (addTargetResult == DialogResult.OK)
  96. {
  97. ISCSITarget target = addTarget.Target;
  98. ((SCSI.VirtualSCSITarget)target.SCSITarget).OnLogEntry += Program.OnLogEntry;
  99. target.OnAuthorizationRequest += new EventHandler<AuthorizationRequestArgs>(ISCSITarget_OnAuthorizationRequest);
  100. target.OnSessionTermination += new EventHandler<SessionTerminationArgs>(ISCSITarget_OnSessionTermination);
  101. m_targets.Add(target);
  102. try
  103. {
  104. m_server.AddTarget(target);
  105. }
  106. catch (ArgumentException ex)
  107. {
  108. MessageBox.Show(ex.Message, "Error");
  109. return;
  110. }
  111. listTargets.Items.Add(target.TargetName);
  112. }
  113. }
  114. private void btnRemoveTarget_Click(object sender, EventArgs e)
  115. {
  116. if (listTargets.SelectedIndices.Count > 0)
  117. {
  118. int targetIndex = listTargets.SelectedIndices[0];
  119. ISCSITarget target = m_targets[targetIndex];
  120. bool isTargetRemoved = m_server.RemoveTarget(target.TargetName);
  121. if (!isTargetRemoved)
  122. {
  123. MessageBox.Show("Could not remove iSCSI target", "Error");
  124. return;
  125. }
  126. List<Disk> disks = ((SCSI.VirtualSCSITarget)target.SCSITarget).Disks;
  127. LockUtils.ReleaseDisks(disks);
  128. m_targets.RemoveAt(targetIndex);
  129. listTargets.Items.RemoveAt(targetIndex);
  130. }
  131. }
  132. private void ISCSITarget_OnAuthorizationRequest(object sender, AuthorizationRequestArgs e)
  133. {
  134. string targetName = ((ISCSITarget)sender).TargetName;
  135. m_usageCounter.NotifySessionStart(targetName);
  136. UpdateUI();
  137. }
  138. private void ISCSITarget_OnSessionTermination(object sender, SessionTerminationArgs e)
  139. {
  140. string targetName = ((ISCSITarget)sender).TargetName;
  141. m_usageCounter.NotifySessionTermination(targetName);
  142. UpdateUI();
  143. }
  144. private void UpdateUI()
  145. {
  146. if (this.InvokeRequired)
  147. {
  148. this.Invoke((MethodInvoker)UpdateUI);
  149. }
  150. else
  151. {
  152. if (m_started)
  153. {
  154. lblStatus.Text = String.Format("{0} Active Sessions", m_usageCounter.SessionCount);
  155. }
  156. if (listTargets.SelectedIndices.Count > 0)
  157. {
  158. int targetIndex = listTargets.SelectedIndices[0];
  159. ISCSITarget target = m_targets[targetIndex];
  160. bool isInUse = m_usageCounter.IsTargetInUse(target.TargetName);
  161. btnRemoveTarget.Enabled = !isInUse;
  162. }
  163. else
  164. {
  165. btnRemoveTarget.Enabled = false;
  166. }
  167. }
  168. }
  169. private static List<IPAddress> GetHostIPAddresses()
  170. {
  171. List<IPAddress> result = new List<IPAddress>();
  172. foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
  173. {
  174. IPInterfaceProperties ipProperties = netInterface.GetIPProperties();
  175. foreach (UnicastIPAddressInformation addressInfo in ipProperties.UnicastAddresses)
  176. {
  177. if (addressInfo.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  178. {
  179. result.Add(addressInfo.Address);
  180. }
  181. }
  182. }
  183. return result;
  184. }
  185. private void listTargets_SelectedIndexChanged(object sender, EventArgs e)
  186. {
  187. UpdateUI();
  188. }
  189. }
  190. }