UNP Summary Chapter 26~29 threads, IP options, raw sockets, Data link access

Source: Internet
Author: User

This is the last section of UNP summary Chapter30 mainly for code practice please refer to UNP

One, Thread

This UNP thread is basically consistent with the threads in Apue, but Apue is more nuanced, so here's a list of the main things (thread data will be a little more detailed) in detail in Apue thread-related chapters or in this blog's apue topic

1. Basic threading function: Creating and terminating

2. Thread-specific data

using thread-specific data is a common technique for making out-of-the-box functions thread safe

The point here is to mention

1. Each system supports a limited number of thread-specific data items. The system (most likely the line threading) maintains a data structure for each process, which we call the key structure, as shown below

2). The flags in the key structure indicate whether this array element is being used, and all flags are initialized to "not in use". When a thread calls Pthread_key_create to create a new thread-specific data item, the system searches its key structure array and finds the first unused item. The subscript (0~127) is called the Key (key), and the subscript is returned to the calling thread. We soon talked about another member of the key structure, "destructor pointers."

In addition to the process-wide array of key structures, the system maintains more than one piece of information for each thread of the process. These thread-specific information, which we call the pthread structure, is part of an array of pointers to the 128 elements we call the Pkey array, as shown in the following figure:

3) The diagram below shows a diagram of the malloc memory area associated with thread-specific data pointers

4. Thread n data structure after initialization of its thread-specific data

3. Mutual exclusion Lock

4. Condition variable

second, IP options

1. Overview

IPV4 allows a maximum of 40 byte options after the 20-byte header fixed part. Although the IPV4 option already defined is public 10, the most common is the source path option, which is accessed by accessing ip_options socket options.

IPV6 allows an extended header (extension header) to occur between a fixed length 40-byte IPv6 header and a transport-layer head (such as ICMPV6,TCP or UDP). Unlike IPv4, the way to access the IPV6 extension head is through a function interface, rather than forcing the user to understand the real details of how the head appears in the IPV6 group.

2.ipv4 options, IPv4 source path options

1. Read and Set IP option fields using GetSockOpt and setsockopt (the level parameter is the Ipproto_ip,optname parameter is ip_options). The fourth parameter of getsockopt and setsockopt is a pointer to a buffer (greater than or equal to 44 bytes), and the fifth parameter is the size of the buffer.

2. After the IP option is set with setsockopt, these options will be included in all IP datagrams sent in the appropriate socket. Sockets can be tcp,udp or original IP set interfaces. To clear these options, you can call setsockopt, place the fourth argument as a null pointer, or set the fifth argument (length) to 0

3. When call getsockopt gets an IP option for a connected TCP socket created by accept, a reversal of the source path option is returned for the client received on the corresponding listening socket. The source path is automatically reversed by TCP because the client specifies the source path from the client to the server, and the server needs to use the reverse of the path in the data sent to the customer

4. The source path is a list of IP addresses specified by the sender of the IP datagram. If the source path is strict (strict), the datagram must and only pass through the listed nodes. If the source path is loose (loose), datagrams must pass through the listed nodes, but you can also pass through nodes that are not listed in the source path.

The following illustration shows the source path to the kernel

The following illustration shows the source path option format returned by getsockopt

Detailed code examples see UNP

The 3.IPv extended First step jump (hop_by_hop) option must be immediately followed by a 40-byte IPv6 header. This option is not currently defined for use by an application. Purpose (Destination) option: This option is not currently defined for use by an application. Routing Head (routing header): This is a source routing option, conceptually similar to the IPV4 source path option that we described in section 24.3. Fragment head (Fragmentation Hearder): The head is automatically generated by the host that will IPv6 the datagram and is processed when the final destination host reorganizes the fragment. Certified Head (Authentication header, AH): Encapsulating Secure Payload (Encapsulating Security Payload, ESP) head:

4.ipv6 Routing Header

The following illustration shows the IPV6 routing header:

5.ipv6 adhesion Options IPV6 packet Information Outbound jump limit dry or jump limit next address out of circulation category or accept the flow category step-jump Option destination Option route header

third, the original socket

1. Overview

The original set of interfaces provides the following three types of TCP and UDP socket interfaces generally do not provide functionality.

1. With the original socket, the process can read and write Icmpv4,igmpv4,icmpv6 groupings. For example, the PING program sends an ICMP echo request using the original socket and accepts an ICMP echo response.

2. With the original socket, the process can read and write the IPV4 datagram that the kernel does not process its Protocol field.

3. With the original socket, the process can also use the IP_HDRINCL socket option to construct its own IPV4 header.

2. Original Socket Creation

Creating an original socket involves the following steps:

1. When the second argument is sock_raw, the socket function is called to create an original socket. The third parameter (protocol) should generally not be 0, for example, in order to create a IPV4 primitive socket interface, we can write this:

int sockfd;
SOCKFD = socket (af_inet, SOCK_RAW, protocol);

Where the value of the protocol parameter is a constant value, such as IPPROTO_XXX, defined by the <netinet/in.h> header file, such as Ipproto_igmp

2). You can set the IP_HDRINCL socket option:

const int on = 1;
if (setsockopt (SOCKFD, Ipproto_ip, Ip_hdrincl, &on, sizeof (ON)) < 0)
       error handling

3. You can call the BIND function on this original socket, but it is relatively rare. The BIND function is used only to set the local address: because the original socket does not have the concept of a port number . In the case of output, bind is set to the source IP address that will be used for all data sent from this raw socket (only if the IP_HDRINCL socket option is not open), and if Bind is not invoked, the kernel sets the IP address to the primary IP address of the outgoing interface.

4. You can call the Connect function on this original socket, but it is also rare. The Connect function simply sets the field address, also because the original socket does not have the concept of a port number . For the output, after the call to connect, we can change the sendto call to a write or send call, because the destination IP address is already specified.

3. Original socket output

The output of the original socket follows these rules:

1. Normal output is accomplished by calling SendTo or sendmsg and specifying the destination IP address. If the socket is already connected, you can also call Write,writev or send

2. If the IP_HDRINCL option is not turned on, the first byte after the IP header is the starting address of the data that the process lets the kernel send. Because the kernel constructs the IP header and places it before the data from the process, the kernel sets the Protocol field of the IPV4 header to the third parameter from the socket call.

3. If the Ip_hdrincl copper ring option is turned on, the data start address that the process lets the kernel write refers to the first byte of the IP header.

4. The kernel performs fragmentation on the original groupings that exceed the outgoing interface MTU.

4. Raw socket Input

The kernel passes incoming IP datagrams to the original socket to follow the rules: Incoming TCP groupings and UDP packets are never passed to any of the original sockets. Most ICMP packets are passed to the original socket after the kernel has finished processing the ICMP message All IGMP groupings are passed to the original socket after the kernel completes processing the IGMP message to the original set interface word kernel that does not recognize its protocol field. If a datagram arrives in fragment form, the grouping is passed to the original socket after all fragments arrive and are reorganized.

5.ping Program

The ping program is very simple, send an ICMP echo request to an IP address, and the node responds with an ICMP echo, as described in the following procedure, see UNP

6.traceroute Program

The purpose of the traceroute is to determine the path of the IP datagram from our host to the destination, traceroute use the IPv4 TTL field or the IPv6 jump field, and two ICMP messages. It starts by sending a UDP datagram with a TTL (or skip limit) of 1 to the destination host. This datagram causes the first-hop router to return a "time exceeded in transmit (timeout in transit)" error. It then adds 1 to the TTL each time and issues the UDP datagram separately, which allows the next router to be determined progressively. When the UDP datagram finally arrives at the destination host, it is hoped that the destination host can return a "Port unreachable" error, which is achieved by sending a UDP datagram to a random port that is not expected to be used.

7. An ICMP message daemon

Here gives the description diagram code see UNP

Once the local copy of the socket is closed after obtaining the port number that is bound on that UDP socket, ICMPD ICMPD the ICMP error that is caused by the UDP datagram sent by the application process through the port that is bound on its UDP socket. Sends a message to the application through a UNIX domain connection.

Iv. Data Link Access

Only the main contents are listed here

1.BPF:BSD packet Filter

2.DLPI: Data Link provides interface

3.linux:sock_packet and Pf_packet

4.libpcap: Grouped capture function library

5.libnet: Grouped structure and output function library

6. Check the checksum field of UDP

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.