来自wireshark数据包

时间:2016-08-22 21:11:32

标签: c# sockets asynchronous networking tcp

我正在尝试为这个“异步”嵌入式卡写一个网络接口,用于LED标志。现有的软件名为“PlutoManager”,但它是在中国生产的,对我们的老客户来说太难了。

该软件通过以太网电缆与嵌入式卡(称为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发送自定义有效负载,但在我继续沿着rabbithole前进之前,它看起来有必要吗? 第二个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 个答案:

答案 0 :(得分: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

我希望这会有所帮助。