Jump the Great Firewall "STEP16 optimized packet loss rate"

Source: Internet
Author: User

Why do I lose my bag?

After the long-term use of the Bo Master experience, the use of UDP protocol on the Internet to transmit data, there is a certain loss rate. Especially in the evening rush hour, the packet loss rate is more obvious, so how do we optimize to reduce the packet loss rate?

Second, how to solve

    1. The server side and the client at the same time check, in a certain period if a packet is found, send a new message to notify the other party to resend the message
    2. Server-side and client sends a message multiple times each time it sends messages

Let's take a look at the first scenario: this is the most traditional approach, but there is one drawback to this approach. When the network condition is very poor, the outgoing request can also be lost, so the peer may again request the lost request to re-send the message. Similar to TCP, this causes the entire network to become worse.

Let's take a look at the second scenario: for each message, the two ends are sent multiple times, and the overall bandwidth is theoretically reduced. In the case of bad network conditions, assuming a message we send 5 times, it is not so coincidental that these 5 times have been dropped. So I prefer this scenario, and in the actual testing process we find that the bandwidth is not reduced, but the download file is more stable than before.

Third, how to achieve

    1. First of all, modify the lowest-level contract function Write_c
      ssize_t Write_c (client_t* client, const void* BUF, size_t count) {    unsigned char i;    unsigned char successed = 0;    for (i = 0; i < qtun->multi_send; ++i) {        if (qtun->use_udp) {            if (sendto (QTUN->REMOTEFD, buf, (int) cou NT, 0, (struct sockaddr*) &client->addr, sizeof (CLIENT->ADDR)) >= 0)                ++successed;        } else {            Const char* ptr = BUF;            size_t left = count;            while (left) {                ssize_t written = write (CLIENT->FD, PTR, (unsigned int.) left);                if (written = = 0)                    return 0;                else if (written = =-1) {                    if (errno = = Eagain | | errno = = ewouldblock) continue;                    return-1;                }                PTR  + = written;                Left-= written;            }            ++successed;        }    }    return count;}
      Here we decide how many times to send according to the Multi_send parameter in the configuration file, in TCP mode the Multi_send parameter is always 1
    2. We define a msg_state_t structure to hold every message received.
      typedef struct {    unsigned int   ident;    unsigned short idx;    unsigned char  used;} msg_state_t;
    3. In the client_t structure we always record the message ID received by the Msg_max_ttl Bar.
          msg_state_t        Recv_msgs[msg_max_ttl];
    4. When we receive a message, we need to first check whether the sequence number of the message has been recorded in the Recv_msg table in the corresponding client_t structure, and if the sequence number of the message is already contained in the table, you need to discard the message

Iv. Enhancing Security

In order to enhance security, in the new version of the Qtun by default prohibits communication with other hosts in the LAN, by specifying the USE_LOCAL_FORWARD flag in the configuration file to power on the LAN communication. The specific implementation method is: In the Server_process function to check whether the source and destination address of the packet is the current LAN, if it is the current local area network packets, then directly drop packets

            if (!qtun->use_local_forward &&                check_ip_by_mask (iphdr->saddr, Qtun->localip, qtun-> netmask) &&                check_ip_by_mask (iphdr->daddr, Qtun->localip, Qtun->netmask)) {                return;            }

Five, complete code

Full code can be viewed in step16

Version number: 1.1.0

Date: 2015-06-13

Jump the Great Firewall "STEP16 optimized packet loss rate"

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.