linux 自定义协议,原始套接字中自定义协议问题

本文介绍了 Raw Socket 的三个主要功能:发送与接收 ICMP 和 IGMP 数据包,处理未被内核解析的 IPv4 协议字段,以及自行构建 IPv4 头部。这些功能使得应用程序能够独立于内核处理特定的网络协议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

呵呵,非 ip 协议处理么,在 unp 中描述 raw socket 为:

Raw sockets provide three features not provided by normal TCP and UDP sockets:

Raw sockets let us read and write ICMPv4, IGMPv4, and ICMPv6 packets. The ping program, for example, sends ICMP echo requests and receives ICMP echo replies. (We will develop our own version of the ping program in Section 28.5.) The multicast routing daemon, mrouted, sends and receives IGMPv4 packets.

This capability also allows applications that are built using ICMP or IGMP to be handled entirely as user processes, instead of putting more code into the kernel. The router discovery daemon (in. rdisc under Solaris 2.x; Appendix F of TCPv1 describes how to obtain the source code for a publicly available version), for example, is built this way. It processes two ICMP messages (router advertisement and router solicitation) that the kernel knows nothing about.

With a raw socket, a process can read and write IPv4 datagrams with an IPV4 protocol field that is not processed by the kernel. Recall the 8-bit IPv4 protocol field in Figure A.1. Most kernels only process datagrams containing values of 1 (ICMP), 2 (IGMP), 6 (TCP), and 17 (UDP). But many other values are defined for the protocol field: The IANA's "Protocol Numbers" registry lists all the values. For example, the OSPF routing protocol does not use TCP or UDP, but it uses IP directly, setting the protocol field of the IP datagram to 89. The gated program that implements OSPF must use a raw socket to read and write these IP datagrams since they contain a protocol field the kernel knows nothing about. This capability carries over to IPv6 also.

With a raw socket, a process can build its own IPv4 header using the IP_HDRINCL socket option. This can be used, for example, to build UDP and TCP packets, and we will show an example of this in Section 29.7.

貌似做不到吧...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值