2013-02-08 51 views
17

当我通过套接字编程时,我无法清楚地理解RAW_SOCKET。什么是套接字编程中的RAW套接字

我的理解是

如果我打开一个插座,此选项AF_INET,RAW_SOCKET意思的,我可以创造我现在才AF_INET头头 但finaly的数据是在发送AF_INET协议的格式。 我的理解是否正确。如果错的话可以解释一下。

THANKYOU

回答

12

RAW_SOCKET允许用户来实现它的上面互联网(IP)级自己的传输层协议。您负责创建和解析传输级别标题和背后的逻辑。一个数据包看起来像:

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

编辑:有上Linux man page原始套接字的很好的说明,或here如果您使用的是Windows。

+0

你能给我多一点解释 – kar 2013-02-08 14:30:49

+0

@kar编辑的例子。 – KBart 2013-02-08 14:32:34

1

它也用于ICMP(ping)等协议,你必须知道ICPM包的结构来创建它。此外内核doesn'n修改你的数据包

33

在每一层,包有拆分部分:头部,有效负载

非原始插座表示您可以确定传输层有效载荷。即创建传输,网络和数据链路层报头是OS任务。

原始套接字意味着您可以确定数据包的每个部分,包括头部或有效负载。请注意,原始套接字是一个普遍的词。我将原始套接字分为:网络套接字和数据链接套接字(或者替代地,L3套接字和L2套接字)

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

在L2 Socket中,您可以在数据链路层设置数据包的标题和有效载荷,即数据包中的所有内容。因此,您可以完成L3 Socket的所有工作+确定ARP头/有效负载,PPP头/有效负载,PPPOE头/有效负载....

现在,在编程:

  • 插座(AF_INET,RAW_SOCKET,...)表示L3插座,网络层协议的IPv4 =
  • 插座(AF_IPX,RAW_SOCKET,...)指L3插座,网络层协议= IPX
  • 插座(AF_INET6,RAW_SOCKET,...)表示L3插座,网络层协议的IPv6 =
  • 插座(AF_PACKET,RAW_SOCKET,...)表示L2插座,数据链路层协议=以太网

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

-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