Skip to content

Commit 7d8cd73

Browse files
committed
CDRIVER-525 Fix IPv6 support for replica sets
Fix _mongoc_host_list_from_string to correctly handle ipv6 addresses
1 parent 2826632 commit 7d8cd73

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

src/mongoc/mongoc-uri.c

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -253,42 +253,33 @@ bool
253253
_mongoc_host_list_from_string (mongoc_host_list_t *host_list,
254254
const char *host_and_port)
255255
{
256-
uint16_t port;
257-
const char *end_host;
258-
char *hostname = NULL;
256+
bool rval = false;
257+
char *uri_str = NULL;
258+
mongoc_uri_t *uri = NULL;
259+
const mongoc_host_list_t *uri_hl;
259260

260261
bson_return_val_if_fail(host_list, false);
261262
bson_return_val_if_fail(host_and_port, false);
262263

263-
memset(host_list, 0, sizeof *host_list);
264+
uri_str = bson_strdup_printf("mongodb://%s/", host_and_port);
265+
if (! uri_str) goto CLEANUP;
264266

265-
if ((hostname = scan_to_unichar(host_and_port, ':', "", &end_host))) {
266-
end_host++;
267-
if (!isdigit(*end_host)) {
268-
bson_free(hostname);
269-
return false;
270-
}
271-
#ifdef _MSC_VER
272-
sscanf_s (end_host, "%hu", &port);
273-
#else
274-
sscanf (end_host, "%hu", &port);
275-
#endif
276-
} else {
277-
hostname = bson_strdup(host_and_port);
278-
port = MONGOC_DEFAULT_PORT;
279-
}
267+
uri = mongoc_uri_new(uri_str);
268+
if (! uri) goto CLEANUP;
280269

281-
bson_strncpy (host_list->host_and_port, host_and_port,
282-
sizeof host_list->host_and_port - 1);
270+
uri_hl = mongoc_uri_get_hosts(uri);
271+
if (uri_hl->next) goto CLEANUP;
283272

284-
bson_strncpy (host_list->host, hostname, sizeof host_list->host - 1);
273+
memcpy(host_list, uri_hl, sizeof(*uri_hl));
285274

286-
host_list->port = port;
287-
host_list->family = AF_INET;
275+
rval = true;
288276

289-
bson_free(hostname);
277+
CLEANUP:
290278

291-
return true;
279+
bson_free(uri_str);
280+
if (uri) mongoc_uri_destroy(uri);
281+
282+
return rval;
292283
}
293284

294285

0 commit comments

Comments
 (0)