当前位置: 技术问答>linux和unix
原始套接字中自定义协议问题
来源: 互联网 发布时间:2016-06-09
本文导语: 我在Linux上用SOCK_RAW发送接收数据,协议号定义为0x1111,我使用htons(ETH_P_ALL)可以就收到自己的数据,但也收到很多垃圾数据(比如ip的包),我用htons(0x1111)尝试接收自己的数据,但什么也收不到,谁知道怎么回事呀,谢...
我在Linux上用SOCK_RAW发送接收数据,协议号定义为0x1111,我使用htons(ETH_P_ALL)可以就收到自己的数据,但也收到很多垃圾数据(比如ip的包),我用htons(0x1111)尝试接收自己的数据,但什么也收不到,谁知道怎么回事呀,谢谢。
|
注意接受的时候
1) socket(PF_PACKET, SOCK_RAW, htons(你的协议));
2) 初始化一个 2层的sockaddr 类似于
struct sockaddr_ll lladdr;
注意
sll_protocol = htons(你的协议)
3) sockfd bind 上 sockaddr
4) recvfrom
好了现在,开始底层的网卡帮你过滤不是你的协议的封包了。
初始化sockaddr_ll 比较繁琐 还要知道 接口 ifindex 接口mac地址。
1) socket(PF_PACKET, SOCK_RAW, htons(你的协议));
2) 初始化一个 2层的sockaddr 类似于
struct sockaddr_ll lladdr;
注意
sll_protocol = htons(你的协议)
3) sockfd bind 上 sockaddr
4) recvfrom
好了现在,开始底层的网卡帮你过滤不是你的协议的封包了。
初始化sockaddr_ll 比较繁琐 还要知道 接口 ifindex 接口mac地址。
|
socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP|ETH_P_ARP|ETH_P_ALL))发送接收以太网数据帧
你贴下代码吧。
SOCK_RAW可以参考这个例子
http://blog.chinaunix.net/u2/62281/showart_1096746.html
你贴下代码吧。
SOCK_RAW可以参考这个例子
http://blog.chinaunix.net/u2/62281/showart_1096746.html
|
根据协议的类型不同可以创建不同类型的原始套接字
|
呵呵,非 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.
貌似做不到吧...
|
socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP|ETH_P_ARP|ETH_P_ALL))
|
可能实情不是这样,俺们公司也使用自定义的协议 0x8x8x 收发都没问题,看看是不是其他情况。
|
说错 不是底层网卡 应该是 linux操作系统 帮你过滤