6.0
关注
AI SCORE
技术实践2026-05-11 07:02
Claude 模拟网络栈:延迟和吞吐量测试
Hacker News · dunkels.com#Claude#创意项目
Editor brief · 编辑速览
创意项目用 Claude 实现完整 IP 协议栈。展示 LLM 能力边界的趣味案例,但实用性有限。
创意项目用 Claude 实现完整 IP 协议栈。展示 LLM 能力边界的趣味案例,但实用性有限。
有个很有意思的想法:如果让 Claude Code 充当一个用户态 IP 协议栈,它能多快响应 ping?
也就是说,我们让 Claude 逐字节读取 IP 数据包,并像普通 IP 协议栈一样处理它们。最终,我们可以 ping Claude,并让它返回一个格式正确的 ping 响应包。
荒唐吗?是的。浪费 token 吗?当然。有趣吗?那可太有趣了!
而且,这也能让我们看看,“Markdown 是代码,LLM 是执行代码的处理器”这一理念究竟能被推进到什么程度。在这个案例中,执行的还是相当底层的代码。
我让 Claude 开发了一个名为 ping-respond.md 的命令,让它充当这样的原生 LLM IP 协议栈。下面是它给出的结果:
# Ping Respond — Process one ICMP echo request from tun0
You are acting as a userspace IP stack. The TUN helper is already running in FIFO mode (`sudo python3 scripts/tun_helper.py --fifo`). Your job: read one packet, parse it, construct a valid ICMP echo reply, and write it back. All IP logic happens in your reasoning — no libraries, no scripts.
## Step 1: Read a packet
Run this bash command to read one packet from the TUN device:
echo "READ" > /tmp/tun_cmd && timeout 35 cat /tmp/tun_resp
You will get back a hex string (or "TIMEOUT"). If TIMEOUT, report it and stop.
Save the hex string — this is the raw IPv4 packet.
## Step 2: Parse the IPv4 header
The packet is a sequence of hex bytes (2 hex chars = 1 byte). Parse these fields:
| Offset (bytes) | Length | Field |
| -------------- | ------ | --------------------------------------------------------- |
| 0 | 1 | Version (high nibble) + IHL (low nibble, in 32-bit words) |
| 1 | 1 | DSCP/ECN |
| 2–3 | 2 | Total length |
| 4–5 | 2 | Identification |
| 6–7 | 2 | Flags (3 bits) + Fragment offset (13 bits) |
| 8 | 1 | TTL |
| 9 | 1 | Protocol (must be 0x01 = ICMP) |
| 10–11 | 2 | Header checksum |
| 12–15 | 4 | Source IP |
| 16–19 | 4 | Destination IP |
IHL tells you the header length: IHL × 4 bytes. Standard is IHL=5 → 20 bytes.
If protocol is not 0x01, report "not ICMP, ignoring" and stop.
## Step 3: Parse the ICMP header
The ICMP header starts right after the IP header (at byte offset IHL×4):
| Offset (from ICMP start) | Length | Field |
| ------------------------ | -------- | ---------------------------------- |
| 0 | 1 | Type (must be 0x08 = echo request) |
| 1 | 1 | Code (must be 0x00) |
| 2–3 | 2 | ICMP checksum |
| 4–5 | 2 | Identifier |
| 6–7 | 2 | Sequence number |
| 8+ | variable | Payload (copy unchanged) |
If type is not 0x08, report "not echo request, ignoring" and stop.
## Step 4: Construct the echo reply
Build the reply packet by modifying specific fields. Work in hex throughout.
### 4a: IP header for the reply
Start with the original IP header bytes and change:
- **Byte 8 (TTL):** set to `40` (decimal 64)
- **Bytes 10–11 (header checksum):** set to `0000` temporarily
- **Bytes 12–15 (source IP):** set to the original **destination** IP
- **Bytes 16–19 (destination IP):** set to the original **source** IP
Now compute the IP header checksum:
1. Take the IP header (IHL×4 bytes) with checksum field = 0000
2. Split into 16-bit words (2 bytes each)
3. Sum all words as unsigned integers
4. While the sum exceeds 0xFFFF: add the carry (sum >> 16) to the lower 16 bits (sum & 0xFFFF)
5. Take the one's complement: result = 0xFFFF - folded_sum
6. Write this value into bytes 10–11 (big-endian)
### 4b: ICMP payload for the reply
Start with the original ICMP data (from ICMP start to end of packet) and change:
- **Byte 0 (type):** set to `00` (echo reply)
- **Byte 1 (code):** keep `00`
- **Bytes 2–3 (ICMP checksum):** set to `0000` temporarily
- **Bytes 4+ (identifier, sequence, payload):** keep unchanged
Now compute the ICMP checksum using the same algorithm as the IP checksum:
1. Take the entire ICMP messa
这个命令从 /dev/tun0 设备读取字节,并通过一个轻量的 Python helper 正确处理所有 stty 选项。随后,它会计算 IP checksum;如果识别到 ICMP echo message,就返回一个 ICMP echo reply message,其中所有 checksum 都会被正确计算。
运行后,Claude 会生成类似下面的输出:
Received packet: 45000054812840004001615dac100001ac100002080054f5000d0001a1fb006a0000000041c4000000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637
Parsing the IP header (first 40 hex chars = 20 bytes):
- 45 → version=4, IHL=5 (20 bytes)
- 0054 → total length = 84
- TTL=40 (64), Protocol=01 (ICMP)
- Source: ac100001 = 172.16.0.1
- Dest: ac100002 = 172.16.0.2
ICMP: type=08 (echo request), code=00, id=000d, seq=0001
Now constructing the reply. IP header: swap src/dst, TTL=64, recompute checksum. ICMP: type=00 (echo reply), recompute checksum.
IP header checksum — header with zeroed checksum and swapped IPs:
4500 0054 8128 4000 4001 0000 ac10 0002 ac10 0001
Sum: 4500+0054+8128+4000+4001+0000+ac10+0002+ac10+0001 = 0x29F20
Fold: 0x9F20+0x2 = 0x9F22
Complement: 0xFFFF-0x9F22 = 0x60DD
ICMP checksum — type changed 08→00, checksum zeroed:
Original checksum 54F5 corresponded to folded sum 0xAB0A. Changing type 08→00 reduces sum by 0x0800: 0xAB0A-0x0800 = 0xA30A.
Complement: 0xFFFF-0xA30A = 0x5CF5
Reply packet: 4500005481284000400160ddac100002ac10000100005cf5000d0001a1fb006a0000000041c4000000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637
$ ping -W 300 -c 1 172.16.0.2
PING 172.16.0.2 (172.16.0.2) 56(84) bytes of data.
64 bytes from 172.16.0.2: icmp_seq=1 ttl=64 time=42593 ms
--- 172.16.0.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 42592.723/42592.723/42592.723/0.000 ms
这次运行使用的是 Haiku 4.5,它已经算是速度相当快的模型了。但往返时间高达 45 秒,这个 ping 仍然非常慢。不过,倒也还没有这些 ping 那么慢。