Linux 什么是套接字编程中的 RAW 套接字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14774668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what is RAW socket in socket programming
提问by kar
When i gone through socket programming i could not clearly understand RAW_SOCKET.
当我完成套接字编程时,我无法清楚地理解 RAW_SOCKET。
My understanding is
我的理解是
If i open a socket with this option AF_INET , RAW_SOCKET mean's I can create my now header before AF_INET headers but finaly the data is send in the format of AF_INET protocol. Is my understanding is correct . If wrong can some explain me.
如果我使用此选项 AF_INET 打开套接字,RAW_SOCKET 意味着我可以在 AF_INET 标头之前创建我现在的标头,但最终数据以 AF_INET 协议的格式发送。我的理解是否正确。如果错了可以解释一下。
ThankYou
谢谢你
回答by KBart
RAW_SOCKET allow user to implement it's own transport layer protocol above internet (IP) level . You are responsible for creating and parsing transport level headers and logic behind it. A packet would look like:
RAW_SOCKET 允许用户在互联网 (IP) 级别之上实现自己的传输层协议。您负责创建和解析传输级标头及其背后的逻辑。一个数据包看起来像:
-------------------------------------------------------------------
| Ethernet (typically) header | IP header | Your header | payload |
-------------------------------------------------------------------
EDIT: there's good description of raw sockets on Linux man page, or hereif you are using Windows.
回答by Abdul
It's also used for protocols like ICMP (ping), you have to know structure of ICPM packet to create it. Also kernel doesn'n modify your packets
它也用于像 ICMP (ping) 这样的协议,你必须知道 ICPM 数据包的结构才能创建它。内核也不会修改您的数据包
回答by Forhad Ahmed
You can also use SOCK_RAW with "Packet Sockets" that will allow you to have fullcontrol over the L2 (Ethernet) and L3 (IP) layers.. meaning you can completely custom-render you packet as it comes out of a NIC..
您还可以将 SOCK_RAW 与“数据包套接字”一起使用,这将使您能够完全控制 L2(以太网)和 L3(IP)层。这意味着您可以完全自定义呈现来自 NIC 的数据包。
Details here:
详情在这里:
http://www.kernel.org/doc/man-pages/online/pages/man7/packet.7.html
http://www.kernel.org/doc/man-pages/online/pages/man7/packet.7.html
回答by SuB
In every layer,packet has two disjoint sections: Header, Payload
在每一层,数据包有两个不相交的部分:Header、Payload
non-Raw socketmeans you can just determine Transport Layer Payload. i.e it is OS task to create Transport,Network and Data Link layer headers.
非原始套接字意味着您可以只确定传输层有效负载。即操作系统的任务是创建传输、网络和数据链路层标头。
Raw socketmeans you can determine every section of packet,either header or payload. Please note that raw socket is a general word. I categorize raw socket into: Network Socket andd Data-Link Socket (or alternativly L3 Socket and L2 Socket)
原始套接字意味着您可以确定数据包的每一部分,无论是标头还是有效载荷。请注意,原始套接字是一个通用词。我将原始套接字分为:网络套接字和数据链路套接字(或 L3 套接字和 L2 套接字)
In L3 Socket you can determine header and payload of packet in network layer. For example if network layer protocol is IPv4, you can determine IPv4 header and payload. Thus you can set transport layer header/payload, ICMP header/payload, Routing Protocols headder/payload.
在 L3 Socket 中,您可以确定网络层中数据包的标头和有效载荷。例如,如果网络层协议是 IPv4,您可以确定 IPv4 标头和负载。因此,您可以设置传输层标头/有效载荷、ICMP 标头/有效载荷、路由协议标头/有效载荷。
In L2 Socket you can set header and payload of packet in data link layer, i.e everything in packet. Thus you do everything done with L3 Socket + determine ARP header/payload, PPP header/payload, PPPOE header/payload , .... .
在 L2 Socket 中,您可以在数据链路层设置数据包的报头和有效载荷,即数据包中的所有内容。因此,您使用 L3 Socket + 确定 ARP 标头/有效载荷、PPP 标头/有效载荷、PPPOE 标头/有效载荷.....
Now in programming:
现在在编程:
- socket(AF_INET,RAW_SOCKET,...) means L3 socket , Network Layer Protocol = IPv4
- socket(AF_IPX,RAW_SOCKET,...) means L3 socket , Network Layer Protocol = IPX
- socket(AF_INET6,RAW_SOCKET,...) means L3 socket , Network Layer Protocol=IPv6
- socket(AF_PACKET,RAW_SOCKET,...) means L2 socket , Data-link Layer Protocol= Ethernet
- socket(AF_INET,RAW_SOCKET,...) 表示 L3 套接字,网络层协议 = IPv4
- socket(AF_IPX,RAW_SOCKET,...) 表示 L3 套接字,网络层协议 = IPX
- socket(AF_INET6,RAW_SOCKET,...) 表示 L3 socket , Network Layer Protocol=IPv6
- socket(AF_PACKET,RAW_SOCKET,...) 表示 L2 套接字,数据链路层协议=以太网
Third parameter specify payload protocol.
第三个参数指定有效载荷协议。
回答by pretty
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