當我通過套接字編程時,我無法清楚地理解RAW_SOCKET。什麼是套接字編程中的RAW套接字
我的理解是
如果我打開一個插座,此選項AF_INET,RAW_SOCKET意思的,我可以創造我現在才AF_INET頭頭 但finaly的數據是在發送AF_INET協議的格式。 我的理解是否正確。如果錯的話可以解釋一下。
THANKYOU
當我通過套接字編程時,我無法清楚地理解RAW_SOCKET。什麼是套接字編程中的RAW套接字
我的理解是
如果我打開一個插座,此選項AF_INET,RAW_SOCKET意思的,我可以創造我現在才AF_INET頭頭 但finaly的數據是在發送AF_INET協議的格式。 我的理解是否正確。如果錯的話可以解釋一下。
THANKYOU
RAW_SOCKET允許用戶來實現它的上面互聯網(IP)級自己的傳輸層協議。您負責創建和解析傳輸級別標題和背後的邏輯。一個數據包看起來像:
-------------------------------------------------------------------
| Ethernet (typically) header | IP header | Your header | payload |
-------------------------------------------------------------------
編輯:有上Linux man page原始套接字的很好的說明,或here如果您使用的是Windows。
它也用於ICMP(ping)等協議,你必須知道ICPM包的結構來創建它。此外內核doesn'n修改你的數據包
您還可以使用SOCK_RAW與「包套接字」,將讓你有充分控制在L2(以太網)和L3(IP)層..這意味着你可以完全定製渲染你包作爲它出來一個網卡..
詳情點擊這裏:
http://www.kernel.org/doc/man-pages/online/pages/man7/packet.7.html
在每一層,包有拆分部分:頭部,有效負載
非原始插座表示您可以確定傳輸層有效載荷。即創建傳輸,網絡和數據鏈路層報頭是OS任務。
原始套接字意味着您可以確定數據包的每個部分,包括頭部或有效負載。請注意,原始套接字是一個普遍的詞。我將原始套接字分爲:網絡套接字和數據鏈接套接字(或者替代地,L3套接字和L2套接字)
在L3套接字中,您可以確定網絡層中數據包的標頭和有效負載。例如,如果網絡層協議是IPv4,則可以確定IPv4標頭和有效負載。因此,您可以設置傳輸層頭/淨荷,ICMP頭/淨荷,路由協議負責人/淨荷。
在L2 Socket中,您可以在數據鏈路層設置數據包的標題和有效載荷,即數據包中的所有內容。因此,您可以完成L3 Socket的所有工作+確定ARP頭/有效負載,PPP頭/有效負載,PPPOE頭/有效負載....
現在,在編程:
第三個參數指定有效載荷協議。
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
你能給我多一點解釋 – kar 2013-02-08 14:30:49
@kar編輯的例子。 – KBart 2013-02-08 14:32:34