ISCSITester.cs 3.9 KB

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