ConnectionParameters.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (C) 2012-2018 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.Text;
  10. using System.Net;
  11. using Utilities;
  12. namespace ISCSI.Server
  13. {
  14. internal class TransferEntry
  15. {
  16. public SCSICommandPDU Command;
  17. public uint NextR2TSN;
  18. public uint NextOffset;
  19. public uint TotalR2Ts; // Numbers of R2Ts that will be sent during this transfer
  20. public TransferEntry(SCSICommandPDU command, uint nextR2TSN, uint nextOffset, uint totalR2Ts)
  21. {
  22. Command = command;
  23. NextR2TSN = nextR2TSN;
  24. NextOffset = nextOffset;
  25. TotalR2Ts = totalR2Ts;
  26. }
  27. }
  28. internal class ConnectionParameters
  29. {
  30. public ISCSISession Session;
  31. public ushort CID; // connection ID, generated by the initiator
  32. public IPEndPoint InitiatorEndPoint;
  33. public int InitiatorMaxRecvDataSegmentLength = DefaultParameters.Connection.MaxRecvDataSegmentLength;
  34. public int TargetMaxRecvDataSegmentLength = ISCSIServer.DeclaredParameters.MaxRecvDataSegmentLength;
  35. public uint StatSN = 0; // Initial StatSN, any number will do
  36. private Dictionary<uint, string> m_textSequences = new Dictionary<uint, string>();
  37. private Dictionary<uint, uint> m_taskTagToTransferTag = new Dictionary<uint, uint>();
  38. // Dictionary of current transfers: <transfer-tag, TransferEntry>
  39. private Dictionary<uint, TransferEntry> m_transfers = new Dictionary<uint, TransferEntry>();
  40. public string AddTextToSequence(uint initiatorTaskTag, string text)
  41. {
  42. string precedingText;
  43. if (m_textSequences.TryGetValue(initiatorTaskTag, out precedingText))
  44. {
  45. string sequence = precedingText + text;
  46. m_textSequences[initiatorTaskTag] = sequence;
  47. return sequence;
  48. }
  49. else
  50. {
  51. m_textSequences.Add(initiatorTaskTag, text);
  52. return text;
  53. }
  54. }
  55. public void RemoveTextSequence(uint initiatorTaskTag)
  56. {
  57. m_textSequences.Remove(initiatorTaskTag);
  58. }
  59. public TransferEntry AddTransfer(uint initiatorTaskTag, uint transferTag, SCSICommandPDU command, uint nextR2TSN, uint nextOffset, uint totalR2Ts)
  60. {
  61. m_taskTagToTransferTag.Add(initiatorTaskTag, transferTag);
  62. TransferEntry entry = new TransferEntry(command, nextR2TSN, nextOffset, totalR2Ts);
  63. m_transfers.Add(transferTag, entry);
  64. return entry;
  65. }
  66. public TransferEntry GetTransferEntryUsingTaskTag(uint initiatorTaskTag)
  67. {
  68. uint transferTag;
  69. if (m_taskTagToTransferTag.TryGetValue(initiatorTaskTag, out transferTag))
  70. {
  71. return GetTransferEntry(transferTag);
  72. }
  73. return null;
  74. }
  75. public TransferEntry GetTransferEntry(uint transferTag)
  76. {
  77. TransferEntry result;
  78. m_transfers.TryGetValue(transferTag, out result);
  79. return result;
  80. }
  81. public void RemoveTransfer(uint initiatorTaskTag, uint transferTag)
  82. {
  83. m_taskTagToTransferTag.Remove(initiatorTaskTag);
  84. m_transfers.Remove(transferTag);
  85. }
  86. public string ConnectionIdentifier
  87. {
  88. get
  89. {
  90. if (Session != null)
  91. {
  92. return Session.SessionIdentifier + String.Format(",CID={0}", this.CID.ToString("x"));
  93. }
  94. else
  95. {
  96. return String.Format("ISID=0,TSIH=0,CID={0}", this.CID.ToString("x"));
  97. }
  98. }
  99. }
  100. }
  101. }