import java.net.*;

public class IPHeaderAnalyzer {
    public static void main(String[] args) throws Exception {
        // 创建原始套接字
        DatagramSocket socket = new DatagramSocket();
        socket.setSoTimeout(5000); // 设置超时时间为5秒

        // 构造IP数据报
        byte[] ipHeader = new byte[20];
        ipHeader[0] = (byte) 0x45; // 版本号和头部长度
        ipHeader[2] = (byte) 0x00; // 总长度(高字节)
        ipHeader[3] = (byte) 0x14; // 总长度(低字节)
        ipHeader[4] = (byte) 0x00; // 标识(高字节)
        ipHeader[5] = (byte) 0x01; // 标识(低字节)
        ipHeader[6] = (byte) 0x00; // 片偏移(高字节)
        ipHeader[7] = (byte) 0x00; // 片偏移(低字节)
        ipHeader[8] = (byte) 0x40; // 生存时间
        ipHeader[9] = (byte) 0x01; // 协议
        ipHeader[10] = (byte) 0x00; // 头部校验和(高字节)
        ipHeader[11] = (byte) 0x00; // 头部校验和(低字节)
        ipHeader[12] = (byte) 0xc0; // 源IP地址(高字节)
        ipHeader[13] = (byte) 0xa8; // 源IP地址
        ipHeader[14] = (byte) 0x01; // 源IP地址
        ipHeader[15] = (byte) 0x01; // 源IP地址(低字节)
        ipHeader[16] = (byte) 0xc0; // 目标IP地址(高字节)
        ipHeader[17] = (byte) 0xa8; // 目标IP地址
        ipHeader[18] = (byte) 0x01; // 目标IP地址
        ipHeader[19] = (byte) 0x02; // 目标IP地址(低字节)

        // 发送IP数据报
        InetAddress destAddress = InetAddress.getByName('127.0.0.1');
        DatagramPacket packet = new DatagramPacket(ipHeader, ipHeader.length, destAddress, 0);
        socket.send(packet);

        // 接收IP数据报
        byte[] buffer = new byte[65536];
        packet = new DatagramPacket(buffer, buffer.length);
        socket.receive(packet);

        // 提取IP头部信息
        byte[] receivedPacket = packet.getData();
        byte[] receivedIPHeader = new byte[20];
        System.arraycopy(receivedPacket, 0, receivedIPHeader, 0, 20);

        // 解析IP头部信息
        int ipVersion = (receivedIPHeader[0] & 0xf0) >> 4;
        int ipHeaderLength = (receivedIPHeader[0] & 0x0f) * 4;
        int totalLength = (receivedIPHeader[2] << 8) | (receivedIPHeader[3] & 0xff);
        int identification = (receivedIPHeader[4] << 8) | (receivedIPHeader[5] & 0xff);
        int fragmentOffset = ((receivedIPHeader[6] & 0x1f) << 8) | (receivedIPHeader[7] & 0xff);
        int timeToLive = receivedIPHeader[8] & 0xff;
        int protocol = receivedIPHeader[9] & 0xff;
        int headerChecksum = (receivedIPHeader[10] << 8) | (receivedIPHeader[11] & 0xff);
        long sourceAddress = ((long) (receivedIPHeader[12] & 0xff) << 24) | ((long) (receivedIPHeader[13] & 0xff) << 16) | ((long) (receivedIPHeader[14] & 0xff) << 8) | (receivedIPHeader[15] & 0xff);
        long destinationAddress = ((long) (receivedIPHeader[16] & 0xff) << 24) | ((long) (receivedIPHeader[17] & 0xff) << 16) | ((long) (receivedIPHeader[18] & 0xff) << 8) | (receivedIPHeader[19] & 0xff);

        // 输出解析结果
        System.out.println('Version: ' + ipVersion);
        System.out.println('Header Length: ' + ipHeaderLength);
        System.out.println('Total Length: ' + totalLength);
        System.out.println('Identification: ' + identification);
        System.out.println('Fragment Offset: ' + fragmentOffset);
        System.out.println('Time to Live: ' + timeToLive);
        System.out.println('Protocol: ' + protocol);
        System.out.println('Header Checksum: ' + headerChecksum);
        System.out.println('Source Address: ' + sourceAddress);
        System.out.println('Destination Address: ' + destinationAddress);

        // 关闭套接字
        socket.close();
    }
}

This program utilizes Java's Socket and DatagramPacket classes to send and receive IP datagrams. It first crafts an IP header and sends it to a specific destination address. Then, it receives an IP datagram from the destination address, extracts and analyzes the IP header information, and finally outputs the parsed results while closing the socket. The program demonstrates the process of dissecting an IP packet and extracting key header information, providing a practical example for understanding network protocols in Java.

Java IP Header Analyzer: Decode and Analyze IP Packets

原文地址: https://www.cveoy.top/t/topic/fiWv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录