From 00d475bc7e44abe90944b399efc65d66a8691596 Mon Sep 17 00:00:00 2001 From: cpq Date: Thu, 21 Sep 2023 12:35:47 +0100 Subject: [PATCH] Address https://github.com/cesanta/mongoose/discussions/2305 - bind to v6 only / v6 and v4 --- mongoose.c | 3 ++- mongoose.h | 4 ++++ src/config.h | 4 ++++ src/sock.c | 3 ++- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/mongoose.c b/mongoose.c index ec134faea8..22bb4448b6 100644 --- a/mongoose.c +++ b/mongoose.c @@ -6062,7 +6062,8 @@ bool mg_open_listener(struct mg_connection *c, const char *url) { // won't work! (setsockopt will return EINVAL) MG_ERROR(("setsockopt(SO_REUSEADDR): %d", MG_SOCK_ERR(rc))); #endif -#if defined(IPV6_V6ONLY) +#if MG_IPV6_V6ONLY + // Bind only to the V6 address, not V4 address on this port } else if (c->loc.is_ip6 && (rc = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &on, sizeof(on))) != 0) { diff --git a/mongoose.h b/mongoose.h index df3d236dd2..90268f4db9 100644 --- a/mongoose.h +++ b/mongoose.h @@ -709,6 +709,10 @@ struct timeval { #define MG_ENABLE_IPV6 0 #endif +#ifndef MG_IPV6_V6ONLY +#define MG_IPV6_V6ONLY 0 // IPv6 socket binds only to V6, not V4 address +#endif + #ifndef MG_ENABLE_MD5 #define MG_ENABLE_MD5 1 #endif diff --git a/src/config.h b/src/config.h index a1eff241ec..f69da6571b 100644 --- a/src/config.h +++ b/src/config.h @@ -44,6 +44,10 @@ #define MG_ENABLE_IPV6 0 #endif +#ifndef MG_IPV6_V6ONLY +#define MG_IPV6_V6ONLY 0 // IPv6 socket binds only to V6, not V4 address +#endif + #ifndef MG_ENABLE_MD5 #define MG_ENABLE_MD5 1 #endif diff --git a/src/sock.c b/src/sock.c index 089714c754..ff4d2dff87 100644 --- a/src/sock.c +++ b/src/sock.c @@ -212,7 +212,8 @@ bool mg_open_listener(struct mg_connection *c, const char *url) { // won't work! (setsockopt will return EINVAL) MG_ERROR(("setsockopt(SO_REUSEADDR): %d", MG_SOCK_ERR(rc))); #endif -#if defined(IPV6_V6ONLY) +#if MG_IPV6_V6ONLY + // Bind only to the V6 address, not V4 address on this port } else if (c->loc.is_ip6 && (rc = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &on, sizeof(on))) != 0) {