gpt4 book ai didi

c# - 自定义 TCP header /从 wireshark 数据包复制 TCP header

转载 作者:可可西里 更新时间:2023-11-01 02:53:58 25 4
gpt4 key购买 nike

我正在尝试为这个用于 LED 标志的“异步”嵌入式卡编写网络接口(interface)。现有软件叫“PlutoManager”,但它是中国制造的,我们的老客户很难使用。

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

我查看了一些文档,文档指出该卡通过标准 TCP/IP 协议(protocol)进行通信。 (或者类似TCP/IP的东西,不太确定)

我从我拿到的中文文档中翻译了一些东西,这就是我找到的关于卡协议(protocol)的内容:

(我对 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 有效负载向标志发送消息,或者它是否使用标准协议(protocol)。而且我不知道如何发现差异。我曾尝试使用 Pcap.net 发送自定义有效负载,但在我继续深陷困境之前,这似乎是必要的吗? 第二个 Wireshark 输出是 TCP/IP 协议(protocol)中常见的东西吗?是否可以按顺序发送字符串“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 中内置的协议(protocol)和这个嵌入式设备使用的协议(protocol)是不同的。

我可以访问包含该设备某些规范的中文文档(我会发布它,但它处于保密协议(protocol)之下)。如果我遗漏了什么,或者如果您需要文档中的更多信息,我会尽我所能发布。我尽量提供最相关的信息,但这对我来说很陌生。

我想我可以将问题简化为“如何修改 Sockets.Connect() 方法以使用自定义 TCP 协议(protocol)?” 但我认为最好提供更多信息对我想要完成的事情的总体概述,因为这可能不是我需要做的。

感谢您花时间查看此问题。如果您有任何建议,甚至是指点我去图书馆或书籍或某种阅读 Material ,我很乐意听取。谢谢。

最佳答案

这个答案可能来得有点晚,但是您得到的 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

希望对您有所帮助。

关于c# - 自定义 TCP header /从 wireshark 数据包复制 TCP header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39088798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com