当我进行套接字编程时,我无法清楚地理解RAW_SOCKET。
我的理解是
如果我用这个选项AF_INET打开一个套接字,RAW_SOCKET意味着我可以创建我现在的标题 在AF_INET标题之前,最后数据以AF_INET协议的格式发送。 我的理解是否正确。如果有错,可以解释一下。
THANKYOU
答案 0 :(得分:44)
在每个图层中,数据包都有两个不相交的部分:标题,有效负载
非原始套接字表示您只能确定传输层有效负载。即创建传输,网络和数据链路层标头是OS任务。
原始套接字表示您可以确定数据包的每个部分,包括标头或有效负载。请注意,原始套接字是一般词。我将原始套接字分类为:网络套接字和数据链接套接字(或者替代L3套接字和L2套接字)
在L3 Socket中,您可以确定网络层中数据包的标头和有效负载。例如,如果网络层协议是IPv4,则可以确定IPv4标头和有效负载。因此,您可以设置传输层标头/有效负载,ICMP标头/有效负载,路由协议标头/有效负载。
在L2 Socket中,您可以在数据链路层中设置数据包的标头和有效负载,即数据包中的所有内容。因此,您可以使用L3 Socket完成所有操作,并确定ARP报头/有效负载,PPP报头/有效负载,PPPOE报头/有效负载,....
现在编程:
第三个参数指定有效载荷协议。
答案 1 :(得分:15)
RAW_SOCKET允许用户在互联网(IP)级别上实现自己的传输层协议。您负责创建和解析传输级别标头及其背后的逻辑。数据包看起来像:
-------------------------------------------------------------------
| Ethernet (typically) header | IP header | Your header | payload |
-------------------------------------------------------------------
编辑:如果你使用的是Windows,Linux man page或here上的原始套接字有很好的描述。
答案 2 :(得分:2)
您还可以将SOCK_RAW与“数据包套接字”一起使用,这样您就可以对L2(以太网)和L3(IP)图层进行完全控制..这意味着您可以完全自定义渲染因为它来自NIC ..
详细信息:
http://www.kernel.org/doc/man-pages/online/pages/man7/packet.7.html
答案 3 :(得分:1)
它也用于ICMP(ping)等协议,您必须知道ICPM数据包的结构才能创建它。内核也不会修改你的数据包
答案 4 :(得分:-2)
Once the application creates RAW socket is used to send and
receive packets from source to destination those all packets are
treated as datagram on an unconnected socket
when sending IPv4 data, an application has a choice on
whether to specify the IPv4 header at the front of the outgoing
datagram for the packet.
If the IP_HDRINCL socket option is set to true for an IPv4
socket (address family of AF_INET), the application must supply the
IPv4 header in the outgoing data for send operations.
If this socket option is false (the default setting), then
the IPv4 header should not be in included the outgoing data for
send operations.
It is important to understand that some sockets of type
SOCK_RAW may receive many unexpected datagrams. For example, a PING
program may create a socket of type SOCK_RAW to send ICMP echo
requests and receive responses. While the application is expecting
ICMP echo responses, if several SOCK_RAW sockets are open on a
computer at the same time, the same datagrams may be delivered to
all the open sockets. An application must have a mechanism to
recognize and to ignore all others.
For a PING program, such a mechanism might include
inspecting the received IP header for unique identifiers in the
ICMP header (the application's process ID, for example)
TCP data cannot be sent by using raw socket
Referred from below link :
https://msdn.microsoft.com/en-us/library/windows/desktop/ms740548%28v=vs.85%29.aspx