Skip to content

Commit 41f203c

Browse files
committed
gethostbyname -> getaddrinfo
Signed-off-by: Salil Chandra <[email protected]>
1 parent b5f1ea6 commit 41f203c

File tree

1 file changed

+65
-59
lines changed

1 file changed

+65
-59
lines changed

cdb2api/cdb2api.c

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ static int get_dbinfo = 0;
232232
static int cdb2_get_dbinfo_set_from_env = 0;
233233

234234
/* Request host name of a connection obtained from sockpool */
235-
static int get_hostname_from_sockpool_fd = 1;
235+
static int get_hostname_from_sockpool_fd = 0;
236236
static int cdb2_get_hostname_from_sockpool_fd_set_from_env = 0;
237237

238238
#define CDB2_ALLOW_PMUX_ROUTE_DEFAULT 0
@@ -1113,9 +1113,7 @@ static int cdb2_tcpresolve(const char *host, struct in_addr *in, int *port)
11131113
{
11141114
/*RESOLVE AN ADDRESS*/
11151115
in_addr_t inaddr;
1116-
11171116
int len;
1118-
struct hostent *hp = NULL;
11191117
char tok[128], *cc;
11201118
cc = strchr(host, (int)':');
11211119
if (cc == 0) {
@@ -1136,29 +1134,29 @@ static int cdb2_tcpresolve(const char *host, struct in_addr *in, int *port)
11361134
/* it's dotted-decimal */
11371135
memcpy(&in->s_addr, &inaddr, sizeof(inaddr));
11381136
} else {
1139-
#ifdef __APPLE__
1140-
hp = gethostbyname(tok);
1141-
#elif _LINUX_SOURCE
1142-
int herr;
1143-
char tmp[8192];
1144-
int tmplen = 8192;
1145-
struct hostent hostbuf;
1146-
gethostbyname_r(tok, &hostbuf, tmp, tmplen, &hp, &herr);
1147-
#elif _SUN_SOURCE
1148-
int herr;
1149-
char tmp[8192];
1150-
int tmplen = 8192;
1151-
struct hostent hostbuf;
1152-
hp = gethostbyname_r(tok, &hostbuf, tmp, tmplen, &herr);
1153-
#else
1154-
hp = gethostbyname(tok);
1155-
#endif
1156-
if (hp == NULL) {
1157-
fprintf(stderr, "%s:gethostbyname(%s): errno=%d err=%s\n", __func__,
1158-
tok, errno, strerror(errno));
1137+
struct addrinfo hints = {.ai_family = AF_INET, .ai_socktype = SOCK_STREAM, .ai_protocol = 0, .ai_flags = 0};
1138+
struct addrinfo *result = NULL;
1139+
int gai_rc = getaddrinfo(tok, /*service=*/NULL, &hints, &result);
1140+
if (gai_rc != 0) {
1141+
fprintf(stderr, "%s:getaddrinfo(%s): %d %s\n", __func__, tok, gai_rc, gai_strerror(gai_rc));
1142+
return -1;
1143+
}
1144+
1145+
/* if multiple results are returned, just use the first one. */
1146+
if (!result) {
1147+
fprintf(stderr, "%s:getaddrinfo(%s): no results returned\n", __func__, tok);
11591148
return -1;
11601149
}
1161-
memcpy(&in->s_addr, hp->h_addr, hp->h_length);
1150+
1151+
if (result->ai_family != AF_INET) {
1152+
fprintf(stderr, "%s:getaddrinfo(%s): expected AF_INET\n", __func__, tok);
1153+
freeaddrinfo(result);
1154+
return -1;
1155+
}
1156+
1157+
memcpy(&in->s_addr, &((const struct sockaddr_in *)result->ai_addr)->sin_addr, sizeof(in->s_addr));
1158+
1159+
freeaddrinfo(result);
11621160
}
11631161
return 0;
11641162
}
@@ -2218,51 +2216,59 @@ int cdb2_get_comdb2db(char **comdb2dbname, char **comdb2dbclass)
22182216
}
22192217

22202218
/* populate comdb2db_hosts based on hostname info of comdb2db_name
2221-
* returns -1 if error or no osts wa found
2219+
* returns -1 if error or no hosts were found
22222220
* returns 0 if hosts were found
22232221
* this function has functionality similar to cdb2_tcpresolve()
22242222
*/
2225-
static int get_host_by_name(const char *comdb2db_name, char comdb2db_hosts[][CDB2HOSTNAME_LEN], int *num_hosts)
2223+
static int get_host_by_addr(const char *comdb2db_name, char comdb2db_hosts[][CDB2HOSTNAME_LEN], int *num_hosts)
22262224
{
2227-
struct hostent *hp = NULL;
2228-
char dns_name[512];
2225+
char dns_name[256];
2226+
int size = 0;
22292227

22302228
if (cdb2_default_cluster[0] == '\0') {
2231-
snprintf(dns_name, sizeof(dns_name), "%s.%s", comdb2db_name, cdb2_dnssuffix);
2229+
size = snprintf(dns_name, sizeof(dns_name), "%s.%s", comdb2db_name, cdb2_dnssuffix);
22322230
} else {
2233-
snprintf(dns_name, sizeof(dns_name), "%s-%s.%s", cdb2_default_cluster, comdb2db_name, cdb2_dnssuffix);
2234-
}
2235-
#ifdef __APPLE__
2236-
hp = gethostbyname(dns_name);
2237-
#elif _LINUX_SOURCE
2238-
int herr;
2239-
char tmp[8192];
2240-
int tmplen = sizeof(tmp);
2241-
struct hostent hostbuf;
2242-
gethostbyname_r(dns_name, &hostbuf, tmp, tmplen, &hp, &herr);
2243-
#elif _SUN_SOURCE
2244-
int herr;
2245-
char tmp[8192];
2246-
int tmplen = sizeof(tmp);
2247-
struct hostent hostbuf;
2248-
hp = gethostbyname_r(dns_name, &hostbuf, tmp, tmplen, &herr);
2249-
#else
2250-
hp = gethostbyname(dns_name);
2251-
#endif
2252-
if (!hp) {
2253-
fprintf(stderr, "%s:gethostbyname(%s): errno=%d err=%s\n", __func__,
2254-
dns_name, errno, strerror(errno));
2231+
size = snprintf(dns_name, sizeof(dns_name), "%s-%s.%s", cdb2_default_cluster, comdb2db_name, cdb2_dnssuffix);
2232+
}
2233+
2234+
if (size < 0 || size >= sizeof(dns_name)) {
2235+
fprintf(stderr, "%s: DNS name too long\n", __func__);
22552236
return -1;
22562237
}
22572238

2258-
int rc = -1;
2259-
struct in_addr **addr_list = (struct in_addr **)hp->h_addr_list;
2260-
for (int i = 0; addr_list[i] != NULL; i++) {
2261-
strcpy(comdb2db_hosts[i], inet_ntoa(*addr_list[i]));
2262-
(*num_hosts)++;
2263-
rc = 0;
2239+
struct addrinfo hints = {.ai_family = AF_INET, .ai_socktype = SOCK_STREAM, .ai_protocol = 0, .ai_flags = 0};
2240+
struct addrinfo *result = NULL;
2241+
int gai_rc = getaddrinfo(dns_name, /*service=*/NULL, &hints, &result);
2242+
if (gai_rc != 0) {
2243+
fprintf(stderr, "%s:getaddrinfo(%s): %d %s\n", __func__, dns_name, gai_rc, gai_strerror(gai_rc));
2244+
return -1;
22642245
}
2265-
return rc;
2246+
2247+
int count = 0;
2248+
const struct addrinfo *rp;
2249+
for (rp = result; rp != NULL; rp = rp->ai_next) {
2250+
if (count >= MAX_NODES) {
2251+
fprintf(stderr, "WARNING: %s:getaddrinfo(%s) returned more than %d results\n", __func__, dns_name,
2252+
MAX_NODES);
2253+
break;
2254+
} else if (rp->ai_family != AF_INET) {
2255+
fprintf(stderr, "WARNING: %s:getaddrinfo(%s) returned non-AF_INET results\n", __func__, dns_name);
2256+
} else if (!inet_ntop(AF_INET, &((const struct sockaddr_in *)rp->ai_addr)->sin_addr, comdb2db_hosts[count],
2257+
sizeof(comdb2db_hosts[count]))) {
2258+
fprintf(stderr, "%s:inet_ntop(): %d %s\n", __func__, errno, strerror(errno));
2259+
/* Don't make this a fatal error; just don't increment num_hosts */
2260+
} else {
2261+
count++;
2262+
(*num_hosts)++;
2263+
}
2264+
}
2265+
2266+
freeaddrinfo(result);
2267+
2268+
if (count > 0)
2269+
return 0;
2270+
2271+
return -1;
22662272
}
22672273

22682274
static int get_comdb2db_hosts(cdb2_hndl_tp *hndl, char comdb2db_hosts[][CDB2HOSTNAME_LEN], int *comdb2db_ports,
@@ -2295,7 +2301,7 @@ static int get_comdb2db_hosts(cdb2_hndl_tp *hndl, char comdb2db_hosts[][CDB2HOST
22952301
comdb2db_ports, master, num_hosts, NULL);
22962302
/* DNS lookup comdb2db hosts. */
22972303
if (rc != 0)
2298-
rc = get_host_by_name(comdb2db_name, comdb2db_hosts, num_hosts);
2304+
rc = get_host_by_addr(comdb2db_name, comdb2db_hosts, num_hosts);
22992305
}
23002306

23012307
return rc;

0 commit comments

Comments
 (0)