[CDRIVER-54] IPV6 compile-time flag Created: 06/Apr/11  Updated: 03/May/17  Resolved: 22/Jan/14

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Kyle Banker Assignee: Mira Carey
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File mongo-c-ipv6.dif    

 Comments   
Comment by Ruslan Udovichenko [ 15/Oct/14 ]

asdfsfd

Comment by Christian Hergert [ 22/Jan/14 ]

Master now also has support for IPV6 literals such as:

mongodb://[::1]:27017,[::2]:27017/?ssl=true

Comment by Christian Hergert [ 22/Jan/14 ]

This is now fixed in the released mongo-c-driver as long as you use hostnames. For example, mongodb://localhost6/ works.

What is still missing is URI parsing of IPV6 addresses using the [::1] style format.

Comment by Christian Hergert [ 08/Nov/13 ]

The new C driver uses getaddrinfo as well, so this will be obsolete with the release of it. For those that want to be early testers, you need libbson and https://github.com/chergert/libmongoc.

Comment by Jonathan Hudson [ 23/Dec/11 ]

The patch

Comment by Jonathan Hudson [ 23/Dec/11 ]

I locally patched my C api source to (a) use getaddrinfo() (b) fix up some bugs / omissions in the extant use of getaddrinfo() and (c) create either an AF_INET of AF_INET6 socket depending of the result back from getaddrinfo(). It's probably a naive solution, put it fixed some problems I'd had where I'd defined a replica set with hostnames, and then use the C api (which only worked with IPv4 addresses). It may also address thus unresolved ticket.

The patches follow. Please note I know even less Scons that 'C', so I've forced #define _GNU_SOURCE 1 in the platform/linux/net.h, which is probably undesirable.

Apologies in advance if this is not how JIRA / mongo works (and there's a better way to submit patches) ... all new to me.

-jonathan

— platform/linux/net.orig.h 2011-12-23 14:16:22.907469604 +0000
+++ platform/linux/net.h 2011-12-23 14:08:12.856557766 +0000
@@ -22,6 +22,8 @@
#ifndef MONGO_NET_H
#define MONGO_NET_H

+#define _GNU_SOURCE 1
+
#include "mongo.h"

#include <arpa/inet.h>
@@ -33,6 +35,8 @@
#include <netinet/tcp.h>
#include <fcntl.h>
#include <errno.h>
+#include <unistd.h>
+
#define mongo_close_socket(sock) ( close(sock) )

#if defined(_XOPEN_SOURCE) || defined(_POSIX_SOURCE) || _POSIX_C_SOURCE >= 1
— platform/linux/net.orig.c 2011-12-23 14:16:14.817344493 +0000
+++ platform/linux/net.c 2011-12-23 14:18:22.129313328 +0000
@@ -49,10 +49,10 @@
return MONGO_OK;
}

-static int mongo_create_socket( mongo *conn ) {
+static int mongo_create_socket( mongo *conn, int sock_type ) {
int fd;

  • if( ( fd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) {
    + if( ( fd = socket( sock_type, SOCK_STREAM, 0 ) ) == -1 ) { conn->err = MONGO_CONN_NO_SOCKET; return MONGO_ERROR; }

    @@ -113,28 +113,29 @@
    int flag = 1;
    char port_str[12];
    int ret;
    -
    +
    conn->sock = 0;
    conn->connected = 0;

memset( &hints, 0, sizeof( hints ) );

  • hints.ai_family = AF_INET;
    + hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    -
  • if( mongo_create_socket( conn ) != MONGO_OK )
  • return MONGO_ERROR;
    -
  • if( getaddrinfo( host, port_str, &hints, &addrs ) != 0 ) {
    + sprintf(port_str,"%d",port);
    +
    + if( (ret = getaddrinfo( host, port_str, &hints, &addrs )) != 0 ) { bson_errprintf( "getaddrinfo failed: %s", gai_strerror( ret ) ); conn->err = MONGO_CONN_ADDR_FAIL; return MONGO_ERROR; }

+ if( mongo_create_socket(conn, addrs->ai_family) != MONGO_OK )
+ return MONGO_ERROR;
+
if ( connect( conn->sock, addrs->ai_addr, addrs->ai_addrlen ) == -1 )

{ mongo_close_socket( conn->sock ); freeaddrinfo( addrs ); conn->err = MONGO_CONN_FAIL; - return MONGO_ERROR: + return MONGO_ERROR; }

setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY, ( char * )&flag, sizeof( flag ) );
@@ -152,7 +153,7 @@
socklen_t addressSize;
int flag = 1;

  • if( mongo_create_socket( conn ) != MONGO_OK )
    + if( mongo_create_socket(conn, AF_INET) != MONGO_OK )
    return MONGO_ERROR;

memset( sa.sin_zero , 0 , sizeof( sa.sin_zero ) );

Generated at Wed Feb 07 21:08:19 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.