static int query_domain(st_netfd_t nfd, const char *name, struct in_addr *addr, st_utime_t timeout) { querybuf_t qbuf; u_char *buf = qbuf.buf; HEADER *hp = &qbuf.hdr; int blen = sizeof(qbuf); int i, len, id; for (i = 0; i < _res.nscount; i++) { len = res_mkquery(QUERY, name, C_IN, T_A, NULL, 0, NULL, buf, blen); if (len <= 0) { h_errno = NO_RECOVERY; return -1; } id = hp->id; if (st_sendto(nfd, buf, len, (struct sockaddr *)&(_res.nsaddr_list[i]), sizeof(struct sockaddr), timeout) != len) { h_errno = NETDB_INTERNAL; /* EINTR means interrupt by other thread, NOT by a caught signal */ if (errno == EINTR) return -1; continue; } /* Wait for reply */ do { len = st_recvfrom(nfd, buf, blen, NULL, NULL, timeout); if (len <= 0) break; } while (id != hp->id); if (len < HFIXEDSZ) { h_errno = NETDB_INTERNAL; if (len >= 0) errno = EMSGSIZE; else if (errno == EINTR) /* see the comment above */ return -1; continue; } hp->ancount = ntohs(hp->ancount); hp->qdcount = ntohs(hp->qdcount); if ((hp->rcode != NOERROR) || (hp->ancount == 0)) { switch (hp->rcode) { case NXDOMAIN: h_errno = HOST_NOT_FOUND; break; case SERVFAIL: h_errno = TRY_AGAIN; break; case NOERROR: h_errno = NO_DATA; break; case FORMERR: case NOTIMP: case REFUSED: default: h_errno = NO_RECOVERY; } continue; } if (parse_answer(&qbuf, len, addr) == 0) return 0; } return -1; }
static int query_domain(st_netfd_t nfd, const char *name, struct in_addr *addr, st_utime_t timeout) { // name="www.baidu.com"; #if 0 char *name=malloc(128); memset(name,0,128); strcpy(name,hostname); LOGD("hostname [%s] name[%s]",hostname,name); #endif querybuf_t qbuf; u_char *buf = qbuf.buf; HEADER *hp = &qbuf.hdr; int blen = sizeof(qbuf); int i, len, id; for (i = 0; i < _res.nscount; i++) { len = res_mkquery(QUERY, name, C_IN, T_A, NULL, 0, NULL, buf, blen); if (len <= 0) { h_errno = NO_RECOVERY; return -1; } id = hp->id; if (st_sendto(nfd, buf, len, (struct sockaddr *)&(_res.nsaddr_list[i]), sizeof(struct sockaddr), timeout) != len) { h_errno = NETDB_INTERNAL; /* EINTR means interrupt by other thread, NOT by a caught signal */ if (errno == EINTR) return -1; continue; } len = st_recvfrom(nfd, buf, blen, NULL, NULL, timeout); LOGD(" st_recvfrom len[%d]",len); return 0; /* Wait for reply */ do { len = st_recvfrom(nfd, buf, blen, NULL, NULL, timeout); if (len <= 0) /*一直接收不到应答消息啊*/ { LOGD(" st_recvfrom len[%d]",len); break; } } while (id != hp->id); if (len < HFIXEDSZ) { h_errno = NETDB_INTERNAL; if (len >= 0) errno = EMSGSIZE; else if (errno == EINTR) /* see the comment above */ return -1; continue; } hp->ancount = ntohs(hp->ancount); hp->qdcount = ntohs(hp->qdcount); if ((hp->rcode != NOERROR) || (hp->ancount == 0)) { switch (hp->rcode) { case NXDOMAIN: h_errno = HOST_NOT_FOUND; break; case SERVFAIL: h_errno = TRY_AGAIN; break; case NOERROR: /*走这里*/ h_errno = NO_DATA; break; case FORMERR: case NOTIMP: case REFUSED: default: /*走这里*/ h_errno = NO_RECOVERY; } continue; } if (parse_answer(&qbuf, len, addr) == 0) { LOGD("after parse_answer"); //free(name);//add by me for malloc return 0;//走这里 } } ///free(name);//add by me 20150923 return -1; }