Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Cannot start service on [::]:[port] ipv6 on Linux

Unable to start service on ipv6 [::]:[port] which is equivalent to 0.0.0.0 in ipv4 address. I am able to start the service using the below code on windows but fails to create ipv6 listener on Linux

#include "qcoreapplication.h"
#include "mongoose\mongoose.h"

static const char *s_https_ipv4 = "0.0.0.0:8443";
static const char *s_https_ipv6 = "[::]:8443";
static const char *s_ssl_cert = "server.pem";
static const char *s_ssl_key = "server.key";
static struct mg_serve_http_opts s_http_server_opts;


static void ev_handler(struct mg_connection *nc, int ev, void *p) {
    if (ev == MG_EV_HTTP_REQUEST) {
        char buff[100];
        mg_conn_addr_to_str(nc, buff, 100, MG_SOCK_STRINGIFY_REMOTE);
        printf("Ip address contacted %s\n", buff);
        mg_serve_http(nc, (struct http_message *) p, s_http_server_opts);
    }
}

int main(int argc , char** argv) {

    QCoreApplication(argc, argv);
    struct mg_mgr mgr;
    struct mg_connection *nc;
    struct mg_bind_opts bind_opts;
    const char *err;

    mg_mgr_init(&mgr, NULL);
    memset(&bind_opts, 0, sizeof(bind_opts));
    bind_opts.ssl_cert = s_ssl_cert;
    bind_opts.ssl_key = s_ssl_key;
    bind_opts.error_string = &err;

    printf("Starting SSL server on port %s, cert from %s, key from %s\n",
        s_https_ipv4, bind_opts.ssl_cert, bind_opts.ssl_key);
    nc = mg_bind_opt(&mgr, s_https_ipv4, ev_handler, bind_opts);
    if (nc == NULL) {
        printf("Failed to create listener: %s\n", err);
        return 1;
    }

    // Set up HTTP server parameters
    mg_set_protocol_http_websocket(nc);
    s_http_server_opts.document_root = ".";  // Serve current directory
    s_http_server_opts.enable_directory_listing = "yes";

    nc = mg_bind_opt(&mgr, s_https_ipv6, ev_handler, bind_opts);
    if (nc == NULL) {
        printf("Failed to create listener: %s\n", err);
        return 1;
    }

    // Set up HTTP server parameters
    mg_set_protocol_http_websocket(nc);
    s_http_server_opts.document_root = ".";  // Serve current directory
    s_http_server_opts.enable_directory_listing = "yes";



    for (;;) {
        mg_mgr_poll(&mgr, 1000);
    }
    mg_mgr_free(&mgr);

    return 0;
}

Comments

Sign In or Register to comment.