Micro-credit Payment api.mch.weixin.qq.com domain name resolution slow down what's the problem

Source: Internet
Author: User
Tags curl socket

Have friends in the Aliyun host implementation of micro-credit payment logic, found that api.mch.weixin.qq.com parsing is too slow.
So there has been manual modification of the/etc/hosts, of course, which day micro-letter payment if a change in the room must be hung.

Our room also has similar problems, specifically recorded.

The code uses the curl to request the micro-letter, often times out, when using the wget test:
[Root@01 tmp]# wget api.mch.weixin.qq.com
--2016-06-18 14:51:03--http://api.mch.weixin.qq.com/
Resolving api.mch.weixin.qq.com ... Domain name resolution A long time not come out
Test confirmation is IPv6 problem

To Wget plus-4, mandatory use of IPv4, if very soon, that is basically determined to be IPv6 caused the trouble.
[Root@01 tmp]# wget-4 api.mch.weixin.qq.com
--2016-06-18 17:03:52--http://api.mch.weixin.qq.com/
Resolving api.mch.weixin.qq.com ... 123.151.71.149, 123.151.79.109
Connecting to api.mch.weixin.qq.com|123.151.71.149|:80 ... Connected.
Code Analysis

Specifically write a code to test IPv6 parsing, using the system function Getaddrinfo:
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <iostream>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>


using namespace Std;

int main () {

struct Addrinfo hints,*answer,*curr,*p;

int error;

memset (&hints, 0, sizeof hints);
hints.ai_family = Af_inet6;//af_unspec; Use AF_INET6 to force IPv6
Hints.ai_socktype = Sock_stream;//sock_dgram; Sock_stream

if (Error = getaddrinfo ("api.mch.weixin.qq.com", NULL, &hints, &answer)!= 0) {
fprintf (stderr, "getaddrinfo:%s\n", Gai_strerror (Error));
return 1;
else cout << "Success with a url\n";

Char ipstr[16];
for (Curr = answer; Curr!= NULL; curr = curr->ai_next) {
Inet_ntop (af_inet,& (struct sockaddr_in *) (CURR->AI_ADDR)->sin_addr), IPSTR, 16);
printf ("%s\n", ipstr);
}

Freeaddrinfo (answer);


return 0;
}
Include header file
Netdb.h
Function prototypes
int getaddrinfo (const char hostname, const char service, const struct addrinfo *hints, struct addrinfo);
Parameter description
Hints: Either a null pointer or a pointer to a ADDRINFO structure in which the caller fills in a hint about the type of information expected to return. For example, if the specified service supports both TCP and UDP, the caller can set the Ai_socktype member in the hints structure to sock_dgram so that only information that is appropriate for the datagram socket interface is returned. And whether IPv6 is decided by ai_family.
Result: This function returns a pointer to a linked list of addrinfo structures through the return pointer argument.
Return value: 0--successful, non 0--error

Test results

ai_family for IPv6, will only look for the IPv6 resolution, the general domain name is not set. Ai_family for Af_unspec, will first IPv6 IPv4, and api.mch.weixin.qq.com this domain name IPv6 parse surprisingly slow (qq.com but not slow, after the reason see).

Solutions

If the curl,c can be forced to specify IPv4, use Curl_easy_setopt (Curl, Curlopt_ipresolve, curl_ipresolve_v4);
Other languages also refer to this method.
Test code Download

Deep reason analysis

Nslookup-query=aaaa Api.mch.weixin.qq.com-debug is unable to find the resolution (the specified AAAA is IPv6), Then you will find an SOA statement and his superiors weixin.qq.com has a IPv6 cname, to minorshort.weixin.qq.com, and this domain name is not IPv6 parsing.
Visual IPv6 find resolution is in this SOA and CNAME place dozen circles, micro-letter students are not consider let everyone better, the domain name IPv6 settings removed.
Dig @ns-tel1.qq.com weixin.qq.com AAAA

weixin.qq.com. 43200 in SOA ns-tel1.qq.com. Webmaster.qq.com 1293502040 300 600 86400 300

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.