void sethostent(int stayopen) { struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); if (_res_init(0) == -1) return; if (stayopen) _resp->options |= RES_STAYOPEN | RES_USEVC; }
struct hostent *gethostbyaddr(const char *addr, int len, int type) { struct res_data *data = _res_init(); if (!data) return NULL; if (!data->buf) { data->buf = malloc(sizeof(struct hostent) + HOST_BUFSIZE); if (!data->buf) { errno = 0; data->errval = NO_RECOVERY; return NULL; } } return gethostbyaddr_r(addr, len, type, (struct hostent *) data->buf, data->buf + sizeof(struct hostent), HOST_BUFSIZE, &data->errval); }
#if PACKETSZ > 1024 #define MAXPACKET PACKETSZ #else #define MAXPACKET 1024 #endif int res_query(char *name, int class, int type, unsigned char *answer, int anslen) { struct res_data *data; char buf[MAXPACKET]; int result; HEADER *hp; data = _res_init(); if (!data) return -1; /* Make the query. */ result = res_mkquery(QUERY, name, class, type, NULL, 0, NULL, buf, sizeof(buf)); if (result <= 0) { data->errval = NO_RECOVERY; return result; } result = res_send(buf, result, (char *) answer, anslen); if (result < 0) { data->errval = TRY_AGAIN; return result;
struct hostent *gethostbyaddr_r(const char *addr, int len, int type, struct hostent *result, char *buf, int bufsize, int *errval) { struct res_data *data; querybuf qbuf; char lookups[MAXDNSLUS], addrbuf[MAXDNAME], *abuf; struct hostent *hp; int n, i; /* Default failure condition is not a range error and not recoverable. */ errno = 0; *errval = NO_RECOVERY; data = _res_init(); if (!data) return NULL; if (type != AF_INET) return NULL; sprintf(addrbuf, "%u.%u.%u.%u.in-addr.arpa", (unsigned)addr[3] & 0xff, (unsigned)addr[2] & 0xff, (unsigned)addr[1] & 0xff, (unsigned)addr[0] & 0xff); memcpy(lookups, data->state.lookups, sizeof(lookups)); if (*lookups == 0) strncpy(lookups, "bf", sizeof(lookups)); hp = NULL; for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { switch (lookups[i]) { case 'b': /* Allocate space for a one-item list of addresses. */ abuf = SP(SP(buf, char *, 2), struct in_addr, 1); if (abuf > buf + bufsize) { errno = ERANGE; return NULL; } /* Perform and parse the query. */ n = res_query(addrbuf, C_IN, T_PTR, (char *)&qbuf, sizeof(qbuf)); if (n < 0) break; hp = _res_parse_answer(&qbuf, n, 1, result, abuf, bufsize - (abuf - buf), errval); if (hp == NULL) break; /* Fill in our own address list. */ result->h_addrtype = type; result->h_length = len; result->h_addr_list = (char **) ALIGN(buf, char *); result->h_addr_list[0] = ALIGN(&result->h_addr_list[2], struct in_addr); result->h_addr_list[1] = NULL; break; case 'f': hp = file_find_addr(addr, len, type, result, buf, bufsize, errval); break; } } return hp; }