Learning-raw socket-general Linux technology-Linux programming and kernel information. The following is a detailed description. The original socket can access protocol packages such as ICMP and ICMP, and read and write IP data packets not processed by the kernel. You can create custom IP packet headers. In one sentence, you can use the original socket
Write a communication program based on the IP protocol.
1. Create the original socket
The specific format is as follows:
Int sockfd;
Sockfd = socktet (AF_INET, SOCK_RAW, IPPROTO_ICMP );
First parameter: AF_INET stands for TCP/IP protocol
Second parameter: SOCKET Type
Third parameter: protocol type
Note:
@ If the specified protocol is 0, the original socket can receive any IP data packet sent by the kernel to the original socket, and only the Super User can create the original socket.
@ When you need to write your own IP packet header, you can set the socket option IP_HDRINCL on the original socket. If this option is not set, the IP protocol automatically fills in the IP packet header.
Int on = 1;
If (setsockopt (sockfd, IPPROTO_IP, IP_HDRINCL, & on, sizeof (on) <0)
{
Fprintf (stderr, "setsockopt IP_HDRINCL ERROR! \ N ");
Exit (1 );
}
The original socket directly uses the IP protocol socket, so it is not connection-oriented. On this socket, you can call the connect and bind functions to bind the peer address and local address respectively.
Note:
Bind function: After the bind function is called, the source IP address of the sent data packet will be the address specified by the bind function. If bind is not called, the kernel will be filled with the primary IP address of the sending interface. If IP_HDRINCL is set, you must manually fill in the source IP address of each sent packet.
Connetc function: after calling the connect function, you can use write and send to send data packets. The kernel uses this bound address to fill in the destination IP address of the IP packet.
Send data packets
The following rules must be followed to send data packets using the original socket:
1. If the connect function is not used to bind the peer address, use the sendto or sendmsg function to send data packets and specify the peer address in the function parameters. If the connect function is called, you can directly use send, write or writev to send data packets.
2. If the IP_HDRINCL option is not set, the kernel automatically creates an IP header if the data content in the package is data. If the IP_HDRINCL option is set, the content to be filled in the package is the IP packet and header. The kernel is only responsible for filling in the following two domains:
· If the ID field of the IP packet is set to 0, the kernel will set this field
· The kernel always calculates and fills in the checksum of IP packet headers.
Note: The content of each domain in the IP packet header is in the byte sequence of the network.
Receive data packets
The kernel follows the following rules to receive data packets:
1. UDP and TCP packets are never transmitted to an original socket. To view these two types of data packets, you can only directly access the data link layer.
2. A copy of most ICMP data packets is sent to the matched original socket.
3. One copy of all other types of data packets processed by the kernel is sent to the matched original socket.
4. All IP data packets of protocol types that cannot be recognized by the kernel are transmitted to the matched original socket. For these IP packets, the kernel only performs necessary checks.
Before sending an IP packet to the original socket, the kernel needs to select the matched original socket.
1. The Protocol domain of the data packet must match the protocol type of the received original socket.
2. If the original socket calls the bind function and binds a local IP address, the source IP address of the incoming IP packet must match the IP address of the other party.
3. If the original socket calls the connect function to specify the IP address of the other party, the source IP address secret of the incoming IP packet must be the same as this.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.