Linux 套接字编程中的原始套接字是什么

Linux 套接字编程中的原始套接字是什么,linux,sockets,Linux,Sockets,当我进行socket编程时,我不能清楚地理解RAW\u socket 我的理解是 如果使用此选项打开套接字,AF_INET,RAW_socketmean,我可以创建自己的头 在AF_INET头之前,但最后数据以AF_INET协议的格式发送。 我的理解是正确的。如果错了,可以解释一下 谢谢原始套接字允许用户在internet(IP)级别上实现自己的传输层协议。您负责创建和解析传输级头及其背后的逻辑。一个包看起来像: -----------------------------------------

当我进行socket编程时,我不能清楚地理解
RAW\u socket

我的理解是

如果使用此选项打开套接字,
AF_INET
RAW_socket
mean,我可以创建自己的头 在
AF_INET
头之前,但最后数据以
AF_INET
协议的格式发送。 我的理解是正确的。如果错了,可以解释一下


谢谢

原始套接字允许用户在internet(IP)级别上实现自己的传输层协议。您负责创建和解析传输级头及其背后的逻辑。一个包看起来像:

-------------------------------------------------------------------
| Ethernet (typically) header | IP header | Your header | payload |
-------------------------------------------------------------------

编辑:如果您正在使用Windows,或在Windows上有关于原始套接字的详细描述。

它也用于ICMP(ping)等协议,您必须知道ICPM数据包的结构才能创建它。此外,内核不修改数据包

您还可以将SOCK_RAW与“数据包套接字”一起使用,这将允许您完全控制L2(以太网)和L3(IP)层。。这意味着您可以完全自定义数据包,因为它来自NIC

详情如下:


在每一层中,数据包都有两个不相交的部分:报头、有效负载

非原始套接字意味着您可以确定传输层负载。i、 创建传输层、网络层和数据链路层头是操作系统的任务

原始套接字意味着您可以确定数据包的每个部分,无论是报头还是有效负载。请注意,原始套接字是一个通用词。我将原始套接字分为:网络套接字和数据链路套接字(或者L3套接字和L2套接字)

在L3套接字中,您可以确定网络层中数据包的报头和有效负载。例如,如果网络层协议是IPv4,则可以确定IPv4标头和负载。因此,您可以设置传输层头/负载、ICMP头/负载、路由协议头/负载

在L2套接字中,您可以在数据链路层设置数据包的报头和有效负载,即数据包中的所有内容。因此,您可以使用L3套接字+确定ARP头/有效负载、PPP头/有效负载、PPPOE头/有效负载

正在编程中:

  • 套接字(AF_INET,RAW_socket,…)指L3套接字,网络层协议=IPv4
  • 套接字(AF_IPX,原始套接字…)指L3套接字,网络层协议=IPX
  • 套接字(AF_INET6,原始套接字,…)表示L3套接字,网络层协议=IPv6
  • 套接字(AF_数据包,原始套接字,…)指L2套接字,数据链路层协议=以太网

第三个参数指定有效负载协议

你能再给我解释一下吗
            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