2013-02-08 50 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