ISCSITarget.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Copyright (C) 2012-2017 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.Net;
  10. using System.Text;
  11. using SCSI;
  12. using Utilities;
  13. namespace ISCSI.Server
  14. {
  15. public class ISCSITarget : SCSITargetInterface
  16. {
  17. private string m_targetName; // ISCSI name
  18. public SCSITargetInterface Target { get; }
  19. // SCSI events:
  20. public event EventHandler<StandardInquiryEventArgs> OnStandardInquiry;
  21. public event EventHandler<UnitSerialNumberInquiryEventArgs> OnUnitSerialNumberInquiry;
  22. public event EventHandler<DeviceIdentificationInquiryEventArgs> OnDeviceIdentificationInquiry;
  23. // iSCSI events:
  24. public event EventHandler<AuthorizationRequestArgs> OnAuthorizationRequest;
  25. public event EventHandler<TextRequestArgs> OnTextRequest;
  26. public event EventHandler<SessionTerminationArgs> OnSessionTermination;
  27. public ISCSITarget(string targetName, List<Disk> disks) : this(targetName, new VirtualSCSITarget(targetName, disks))
  28. {
  29. }
  30. public ISCSITarget(string targetName, SCSITargetInterface scsiTarget)
  31. {
  32. m_targetName = targetName;
  33. Target = scsiTarget;
  34. Target.OnStandardInquiry += new EventHandler<StandardInquiryEventArgs>(Target_OnStandardInquiry);
  35. Target.OnDeviceIdentificationInquiry += new EventHandler<DeviceIdentificationInquiryEventArgs>(Target_OnDeviceIdentificationInquiry);
  36. Target.OnUnitSerialNumberInquiry += new EventHandler<UnitSerialNumberInquiryEventArgs>(Target_OnUnitSerialNumberInquiry);
  37. }
  38. public void QueueCommand(byte[] commandBytes, LUNStructure lun, byte[] data, object task, OnCommandCompleted OnCommandCompleted)
  39. {
  40. Target.QueueCommand(commandBytes, lun, data, task, OnCommandCompleted);
  41. }
  42. public SCSIStatusCodeName ExecuteCommand(byte[] commandBytes, LUNStructure lun, byte[] data, out byte[] response)
  43. {
  44. return Target.ExecuteCommand(commandBytes, lun, data, out response);
  45. }
  46. public void Target_OnStandardInquiry(object sender, StandardInquiryEventArgs args)
  47. {
  48. args.Data.VersionDescriptors.Add(VersionDescriptorName.iSCSI);
  49. // To be thread-safe we must capture the delegate reference first
  50. EventHandler<StandardInquiryEventArgs> handler = OnStandardInquiry;
  51. if (handler != null)
  52. {
  53. handler(this, args);
  54. }
  55. }
  56. void Target_OnUnitSerialNumberInquiry(object sender, UnitSerialNumberInquiryEventArgs args)
  57. {
  58. // To be thread-safe we must capture the delegate reference first
  59. EventHandler<UnitSerialNumberInquiryEventArgs> handler = OnUnitSerialNumberInquiry;
  60. if (handler != null)
  61. {
  62. handler(this, args);
  63. }
  64. }
  65. public void Target_OnDeviceIdentificationInquiry(object sender, DeviceIdentificationInquiryEventArgs args)
  66. {
  67. // ISCSI identifier is needed for WinPE to pick up the disk during boot (after iPXE's sanhook)
  68. args.Page.IdentificationDescriptorList.Add(IdentificationDescriptor.GetSCSINameStringIdentifier(m_targetName));
  69. // To be thread-safe we must capture the delegate reference first
  70. EventHandler<DeviceIdentificationInquiryEventArgs> handler = OnDeviceIdentificationInquiry;
  71. if (handler != null)
  72. {
  73. handler(this, args);
  74. }
  75. }
  76. internal bool AuthorizeInitiator(string initiatorName, ulong isid, IPEndPoint initiatorEndPoint)
  77. {
  78. // To be thread-safe we must capture the delegate reference first
  79. EventHandler<AuthorizationRequestArgs> handler = OnAuthorizationRequest;
  80. if (handler != null)
  81. {
  82. AuthorizationRequestArgs args = new AuthorizationRequestArgs(initiatorName, isid, initiatorEndPoint);
  83. handler(this, args);
  84. return args.Accept;
  85. }
  86. return true;
  87. }
  88. internal KeyValuePairList<string, string> GetTextResponse(KeyValuePairList<string, string> requestParameters)
  89. {
  90. EventHandler<TextRequestArgs> handler = OnTextRequest;
  91. if (handler != null)
  92. {
  93. TextRequestArgs args = new TextRequestArgs(requestParameters);
  94. handler(this, args);
  95. return args.ResponseParaemeters;
  96. }
  97. return new KeyValuePairList<string, string>();
  98. }
  99. internal void NotifySessionTermination(string initiatorName, ulong isid, SessionTerminationReason reason)
  100. {
  101. EventHandler<SessionTerminationArgs> handler = OnSessionTermination;
  102. if (handler != null)
  103. {
  104. SessionTerminationArgs args = new SessionTerminationArgs(initiatorName, isid, reason);
  105. handler(this, args);
  106. }
  107. }
  108. public string TargetName
  109. {
  110. get
  111. {
  112. return m_targetName;
  113. }
  114. }
  115. public SCSITargetInterface SCSITarget
  116. {
  117. get
  118. {
  119. return Target;
  120. }
  121. }
  122. }
  123. }