2016-08-22 60 views
0

我正在尝试为这个“异步”嵌入式卡的LED标志写一个网络接口。现有的软件名为“PlutoManager”,但它是在中国生产的,对于我们的老客户来说太难了。从wireshark数据包自定义TCP头/复制TCP头

该软件通过与以太网电缆与嵌入式卡(称为PSD100)进行交互来完成许多操作。

我看了一些文档,文档指出卡通过标准的TCP/IP协议进行通信。 (或类似的TCP/IP,搞不清东西)

我翻译了一些东西从中国的文件我得到阿霍德的,这就是我来了解该卡的协议:

(我不太了解TCP/IP,所以这种翻译可能很粗糙,请记住这些词可能是错误的词,这可能是我的问题的重要组成部分。)

因此,对于每个与卡(发送文件,握手,改变LED标志的亮度等)有两件事情必须发生:

  • 的消息被发送给卡(请求分组)
  • 从卡接收到的应答(应答包)

请求分组结构如下:(来自中国,和我的翻译很烂)

> 1. Header: 1 byte (16 bits) with a hex value of "0x02" 

>2. Card Address(??): 2 bytes (32 bits) 
>3. Packet Type: 2 bytes (32 bits) 
>4. data: indeterminate length 
>5. CRC Check: 2 bytes (32 bits) 
>6. End of Text Character: 1 byte (16 bits) (value: "0x03" [I guess that's equal to ^c ?] 

这是否看起来像正常的TCP/IP结构,不然我和定制包运走之前?

我想我可以使用Wireshark来嗅探PlutoManager握手时发送的数据包。我还在C#中编写了一些代码,试图与设备的端口建立连接。这是两个并排的。请注意,这只是转储的TCP数据包部分,wireshark输出的TCP部分是唯一不同的部分。

TCP SEGMENT CAPTURED FROM WIRESHARK HEX + ASCII DUMP (FROM MY C# CODE) 
HEX 
0000 d0 6b 7a 43 5e a3 79 62 67 78 dc bf 50 10 80 51  ASCII: .kzC^.ybgx..P..Q 
0010 46 60 00 00            F`.. 

TCP SEGMENT CAPTURED FROM WIRESHARK HEX + ASCII DUMP (PLUTOMANAGER CODE) 

HEX 
0000 7a 42 d0 6a 34 17 04 36 5e a3 0b 1d 50 10 01 00  ASCII: zB.j4..6^...P... 
0010 82 50 00 00  

我算了一下,“哎,我可以只发送自定义负载到卡用的send()命令和复制什么PlutoManager代码做什么!”

我不知道这个中文软件是否使用一些特殊的TCP有效载荷来发送消息到标志,或者它是否使用标准协议。我不知道如何发现差异。我曾尝试使用Pcap.net发送自定义有效内容,但在继续沿着拉比方向走下去之前,似乎有必要这样做? 第二个Wireshark输出是TCP/IP协议中常见的东西吗?是否可以发送字符串“zB/^ T3mPP”(这是握手的十六进制转储输出)以便进行握手?

这是如何我现在有我的节目结构(基本上STR:

using System; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 
using System.Text; 

// State object for receiving data from remote device. 
public class StateObject 
{ 
    // Client socket. 
    public Socket workSocket = null; 
    // Size of receive buffer. 
    public const int BufferSize = 256; 
    // Receive buffer. 
    public byte[] buffer = new byte[BufferSize]; 
    // Received data string. 
    public StringBuilder sb = new StringBuilder(); 
} 

public class AsynchronousClient 
{ 
    // The port number for the remote device. 
    private const int port = 31298; 

    // ManualResetEvent instances signal completion. 
    private static ManualResetEvent connectDone = 
     new ManualResetEvent(false); 
    private static ManualResetEvent sendDone = 
     new ManualResetEvent(false); 
    private static ManualResetEvent receiveDone = 
     new ManualResetEvent(false); 

    // The response from the remote device. 
    private static String response = String.Empty; 

    private static void StartClient() 
    { 
     // Connect to a remote device. 
     try 
     { 
      // Establish the remote endpoint for the socket. 
      // The name of the 
      // remote device is "host.contoso.com". 
      //IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com"); 
      IPAddress ipAddress = IPAddress.Parse("192.168.0.59"); //ipHostInfo.AddressList[0]; 
      IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); 

      // Create a TCP/IP socket. 
      Socket client = new Socket(AddressFamily.InterNetwork, 
       SocketType.Stream, ProtocolType.Tcp); 

      // Connect to the remote endpoint. 
      client.BeginConnect(remoteEP, 
       new AsyncCallback(ConnectCallback), client); 
      connectDone.WaitOne(); 

      // Send test data to the remote device. 
      Send(client, "This is a test<EOF>"); 
      sendDone.WaitOne(); 

      // Receive the response from the remote device. 
      Receive(client); 
      receiveDone.WaitOne(); 

      // Write the response to the console. 
      Console.WriteLine("Response received : {0}", response); 

      // Release the socket. 
      client.Shutdown(SocketShutdown.Both); 
      client.Close(); 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    private static void ConnectCallback(IAsyncResult ar) 
    { 
     try 
     { 
      // Retrieve the socket from the state object. 
      Socket client = (Socket)ar.AsyncState; 

      // Complete the connection. 
      client.EndConnect(ar); 

      Console.WriteLine("Socket connected to {0}", 
       client.RemoteEndPoint.ToString()); 

      // Signal that the connection has been made. 
      connectDone.Set(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    private static void Receive(Socket client) 
    { 
     try 
     { 
      // Create the state object. 
      StateObject state = new StateObject(); 
      state.workSocket = client; 

      // Begin receiving the data from the remote device. 
      client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
       new AsyncCallback(ReceiveCallback), state); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    private static void ReceiveCallback(IAsyncResult ar) 
    { 
     try 
     { 
      // Retrieve the state object and the client socket 
      // from the asynchronous state object. 
      StateObject state = (StateObject)ar.AsyncState; 
      Socket client = state.workSocket; 

      // Read data from the remote device. 
      int bytesRead = client.EndReceive(ar); 

      if (bytesRead > 0) 
      { 
       // There might be more data, so store the data received so far. 
       state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); 

       // Get the rest of the data. 
       client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
        new AsyncCallback(ReceiveCallback), state); 
      } 
      else 
      { 
       // All the data has arrived; put it in response. 
       if (state.sb.Length > 1) 
       { 
        response = state.sb.ToString(); 
       } 
       // Signal that all bytes have been received. 
       receiveDone.Set(); 
      } 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    private static void Send(Socket client, String data) 
    { 
     // Convert the string data to byte data using ASCII encoding. 
     byte[] byteData = Encoding.ASCII.GetBytes(data); 

     // Begin sending the data to the remote device. 
     client.BeginSend(byteData, 0, byteData.Length, 0, 
      new AsyncCallback(SendCallback), client); 
    } 

    private static void SendCallback(IAsyncResult ar) 
    { 
     try 
     { 
      // Retrieve the socket from the state object. 
      Socket client = (Socket)ar.AsyncState; 

      // Complete sending the data to the remote device. 
      int bytesSent = client.EndSend(ar); 
      Console.WriteLine("Sent {0} bytes to server.", bytesSent); 

      // Signal that all bytes have been sent. 
      sendDone.Set(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    public static int Main(String[] args) 
    { 
     StartClient(); 
     return 0; 
    } 
} 

main()的运行命令StartClient(),其尝试连接,但最终输出错误消息:

System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it 192.168.0.59:31298 
    at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) 
    at AsynchronousClient.ConnectCallback(IAsyncResult ar) in C:\Users\xxxxx\Desktop\SocketListenerTest\SocketListenerTest\SocketListenerTest\Program.cs:line 87 

87号线: client.EndConnect(ar);

这让我觉得,我连接到正确的IP和正确的端口,但内置于.NET中的协议和此嵌入式设备使用的协议不同。

我有权访问一个中文文档(我会发布它,但它是在NDA之下)的一些规格的设备。如果我错过了一些东西,或者如果您需要更多信息,我会尽我所能。我试图提供我能够提供的最相关的信息,但这对我来说很陌生。

我想我可以简化问题到“如何修改Sockets.Connect()方法以使用自定义TCP协议?”但我认为最好是对我想要完成的事情给出一个更全面的概述,因为那可能不是我所需要做的。

感谢您抽出时间看一下这个问题。如果您有任何建议,甚至可以将我指向图书馆或书籍或某种阅读材料,我很乐意听到它。谢谢。

+1

你确定它是正确的IP和端口吗?没有“自定义TCP协议”。您的规格显示的是数据包的**有效负载**。由于TCP是基于流的,因此使用wireshark进行嗅探比较困难(我更像是一个UDP人)。 TCP数据包被分段并在到达时按顺序放回。然而网络实施已经这样做了。 – jython234

回答

0

这个答案可能会晚一点,但是您所得到的SocketException指的是端点拒绝整个连接而不仅仅是自定义部分(位于有效内容中)的事实。

可能是由于所提到的PlutoManager使用特定的源端口进行通信,而嵌入式设备的防火墙拒绝来自所有其他源端口的连接。您可以检查使用Wireshark的源端口和定义源端口为您Socket客户是这样的:

Socket client = new Socket(AddressFamily.InterNetwork, 
          SocketType.Stream, 
          ProtocolType.Tcp); 
client.Bind(new IPEndPoint(IPAddress.Any, 32000)); //The port number that PlutoManager uses 

我希望这有助于。