Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

mongoose examples -> IPv6 not running

moatzemoatze Germany

Hi all,

I was trying to establish a tcp connection to an IPv6 webserver.
I tried the examples but things don't seem to work.

Ubuntu 16.04 32bit

mongoose.h

#define MG_ENABLE_IPV6 1
#define MG_ENABLE_DEBUG 1

Using code from simplest_web_server/ and http_client/

Both, server and client work fine with ipv4, with ipv6 I cannot establish connection.

Debug output from server doesnt give me much information back,
output from client:

mg_connect_opt             0x9cfbbc8 tcp://[::1]:8000 -,-,-
mg_do_connect              0x9cfbbc8 tcp://0.0.0.0:8000
mg_socket_if_connect  0x9cfbbc8 sock 5 rc -1 errno 97 err 97 

I was trying to use netcat or curl to establish connection to the mongoose server, this failed too:

curl -gv 6 "http://[::1]:8000
...
Trying ::1...
connect to ::1 port 8000 failed: Connection refused

Opening a listener via netcat and using netcat or curl to connect works fine.
Trying to use mongoose http_client results in same error as above.

Does anyone have hints what could be the problem?

Best regards!

Thanked by 1codewarrior71

Comments

  • moatzemoatze Germany

    I could get it running with following patch:

    in mg_socket_if_connect_tcp
    -- nc->sock = socket(AF_INET; SOCK_STREAM, proto);
    ...
    -- rc = connect(nc->sock, &sa->sa,(sizeof) sa->sa.sin);
    
    ++ socklen_t sa_len = (sa->sa.sa_family ==AF_INET) ? sizeof(sa->sin) : sizeof(sa->sin6);
    ++ nc->sock = socket(sa->sa.sa_family;SOCK_STREAM, proto);
    ...
    ++  rc = connect(nc->sock, &sa->sa, sa_len);
    
    Thanked by 1numo68
  • codewarrior71codewarrior71 Deutschland

    Great, now it works - thanks :)

  • numo68numo68 Austria

    Still not fixed in 6.13 :( At least on windows additionally the socket creation in mg_socket_if_connect_tcp is using an incorrect family so the complete function is

    void mg_socket_if_connect_tcp(struct mg_connection *nc,
        const union socket_address *sa) {
        int rc, proto = 0;
        socklen_t sa_len = (sa->sa.sa_family == AF_INET) ? sizeof(sa->sin) : sizeof(sa->sin6);
        nc->sock = socket(sa->sa.sa_family, SOCK_STREAM, proto);
        if (nc->sock == INVALID_SOCKET) {
            nc->err = mg_get_errno() ? mg_get_errno() : 1;
            return;
        }
    #if !defined(MG_ESP8266)
        mg_set_non_blocking_mode(nc->sock);
    #endif
        rc = connect(nc->sock, &sa->sa, sa_len);
      nc->err = rc < 0 && mg_is_error() ? mg_get_errno() : 0;
      DBG(("%p sock %d rc %d errno %d err %d", nc, nc->sock, rc, mg_get_errno(),
           nc->err));
    }
    

    mg_socket_if_connect_udp seems to have the same problem but I don't have a test code handy to verify

Sign In or Register to comment.