ISCSITester.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Copyright (C) 2012-2015 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.Sockets;
  10. using System.Text;
  11. using Utilities;
  12. namespace ISCSI
  13. {
  14. public class ISCSITester
  15. {
  16. private Socket m_targetSocket;
  17. private Socket m_initiatorSocket;
  18. private bool m_waitingForResponse = false;
  19. public List<ISCSIPDU> m_pduSent = new List<ISCSIPDU>();
  20. public List<ISCSIPDU> m_pduReceived = new List<ISCSIPDU>();
  21. private bool m_isTargetConnected = false;
  22. public ISCSITester(Socket initiatorSocket)
  23. {
  24. m_initiatorSocket = initiatorSocket;
  25. }
  26. public void Transmit(byte[] data)
  27. {
  28. ISCSIPDU pdu = ISCSIPDU.GetPDU(data);
  29. if (pdu is LoginRequestPDU && !m_isTargetConnected)
  30. {
  31. m_isTargetConnected = true;
  32. m_targetSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  33. m_targetSocket.Connect("tal2", 3260);
  34. }
  35. //m_pduToSend.Add(data);
  36. //if (!m_waitingForResponse)
  37. //{
  38. //m_waitingForResponse = true;
  39. //byte[] toSend = m_pduToSend[0];
  40. //m_pduToSend.RemoveAt(0);
  41. StateObject state = new StateObject();
  42. state.ReceiveBuffer = new byte[StateObject.ReceiveBufferSize];
  43. m_targetSocket.BeginReceive(state.ReceiveBuffer, 0, StateObject.ReceiveBufferSize, 0, ReceiveCallback, state);
  44. Console.WriteLine("waiting for respone: " + m_waitingForResponse.ToString());
  45. m_targetSocket.Send(data);
  46. m_pduSent.Add(ISCSIPDU.GetPDU(data));
  47. Console.WriteLine("Transmitted PDU to real target");
  48. //}
  49. }
  50. private void ReceiveCallback(IAsyncResult result)
  51. {
  52. StateObject state = (StateObject)result.AsyncState;
  53. //Socket clientSocket = state.WorkerSocket;
  54. byte[] buffer = state.ReceiveBuffer;
  55. int bytesReceived;
  56. try
  57. {
  58. bytesReceived = m_targetSocket.EndReceive(result);
  59. m_waitingForResponse = false;
  60. }
  61. catch (Exception)
  62. {
  63. //An error has occured when reading
  64. Console.WriteLine("An error has occured when reading from real target");
  65. return;
  66. }
  67. if (bytesReceived == 0)
  68. {
  69. //The connection has been closed.
  70. Console.WriteLine("The connection with the real target has been closed");
  71. return;
  72. }
  73. //iSCSIPDU pdu1 = GetResponcePDU((LoginRequestPDU)pdu);
  74. Console.WriteLine("Received PDU from real target");
  75. byte[] pduBytes = new byte[bytesReceived];
  76. Array.Copy(buffer, pduBytes, bytesReceived);
  77. ISCSIPDU pdu = ISCSIPDU.GetPDU(pduBytes);
  78. m_pduReceived.Add(pdu);
  79. m_initiatorSocket.Send(pduBytes);
  80. Console.WriteLine("Sent PDU to real initiator, OpCode: " + pdu.OpCode);
  81. if (pdu is LogoutResponsePDU)
  82. {
  83. m_initiatorSocket.Close();
  84. m_targetSocket.Close();
  85. m_isTargetConnected = false;
  86. return;
  87. }
  88. //Do something with the data object here.
  89. //Then start reading from the network again.
  90. m_targetSocket.BeginReceive(state.ReceiveBuffer, 0, StateObject.ReceiveBufferSize, 0, ReceiveCallback, state);
  91. }
  92. }
  93. }