ISCSIDisk.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright (C) 2012-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.Net;
  10. using System.Text;
  11. namespace ISCSI.Client
  12. {
  13. /// <summary>
  14. /// Disk Adapter utilizing ISCSIClient
  15. /// </summary>
  16. public class ISCSIDisk : Disk
  17. {
  18. private ISCSIClient m_client;
  19. private int m_bytesPerSector;
  20. private long m_size;
  21. private bool m_isLoggedIn;
  22. private ushort m_lun;
  23. public ISCSIDisk()
  24. {
  25. string initiatorName = "iqn.1991-05.com.microsoft:" + Environment.MachineName;
  26. m_client = new ISCSIClient(initiatorName);
  27. }
  28. public ISCSIDisk(string initiatorName)
  29. {
  30. m_client = new ISCSIClient(initiatorName);
  31. }
  32. public bool Connect(IPAddress targetAddress, int targetPort, string targetName, ushort lun)
  33. {
  34. bool isConnected = m_client.Connect(targetAddress, targetPort);
  35. if (isConnected)
  36. {
  37. m_isLoggedIn = m_client.Login(targetName);
  38. if (m_isLoggedIn)
  39. {
  40. List<ushort> luns = m_client.GetLUNList();
  41. if (luns.Contains(lun))
  42. {
  43. m_lun = lun;
  44. m_size = (long)m_client.ReadCapacity(lun, out m_bytesPerSector);
  45. return true;
  46. }
  47. m_client.Logout();
  48. m_isLoggedIn = false;
  49. }
  50. }
  51. return false;
  52. }
  53. public void Disconnect()
  54. {
  55. if (m_isLoggedIn)
  56. {
  57. m_client.Logout();
  58. }
  59. m_client.Disconnect();
  60. }
  61. public override byte[] ReadSectors(long sectorIndex, int sectorCount)
  62. {
  63. if (!m_isLoggedIn)
  64. {
  65. throw new InvalidOperationException("Not connected");
  66. }
  67. return m_client.Read(m_lun, (ulong)sectorIndex, (uint)sectorCount, m_bytesPerSector);
  68. }
  69. public override void WriteSectors(long sectorIndex, byte[] data)
  70. {
  71. if (!m_isLoggedIn)
  72. {
  73. throw new InvalidOperationException("Not connected");
  74. }
  75. if (IsReadOnly)
  76. {
  77. throw new UnauthorizedAccessException("Attempted to perform write on a readonly disk");
  78. }
  79. bool success = m_client.Write(m_lun, (ulong)sectorIndex, data, m_bytesPerSector);
  80. if (!success)
  81. {
  82. string message = String.Format("Failed to write to sector {0}", sectorIndex);
  83. }
  84. }
  85. public override int BytesPerSector
  86. {
  87. get
  88. {
  89. return m_bytesPerSector;
  90. }
  91. }
  92. public override long Size
  93. {
  94. get
  95. {
  96. return m_size;
  97. }
  98. }
  99. }
  100. }