From 477ada14d309018f034a77cc313d4012701cc87d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9?=
 <alexey.pawlow@gmail.com>
Date: Thu, 17 Jun 2021 18:51:30 +0530
Subject: [PATCH 014/N] Detect winsock2 and setup _socket module on MINGW
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Алексей <alexey.pawlow@gmail.com>
Co-authored-by: Naveen M K <naveen521kk@gmail.com>
---
 Misc/config_mingw      |  3 +++
 Modules/socketmodule.c |  6 ++++++
 configure.ac           | 38 ++++++++++++++++++++++++++++++++------
 pyconfig.h.in          |  4 ++--
 4 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/Misc/config_mingw b/Misc/config_mingw
index 513065d..9be43fd 100644
--- a/Misc/config_mingw
+++ b/Misc/config_mingw
@@ -10,3 +10,6 @@ ac_cv_func_alarm=ignore
 # files to ignore
 ac_cv_file__dev_ptmx=ignore #NOTE: under MSYS environment device exist
 ac_cv_file__dev_ptc=no
+
+# force detection of winsock2 functionality - require wxp or newer
+ac_cv_func_getpeername=yes
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 27afd73..7038e10 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -282,6 +282,7 @@ shutdown(how) -- shut down traffic in one or both directions\n\
 # include <Rpc.h>
 
 /* Macros based on the IPPROTO enum, see: https://bugs.python.org/issue29515 */
+#ifdef _MSC_VER
 #define IPPROTO_ICMP IPPROTO_ICMP
 #define IPPROTO_IGMP IPPROTO_IGMP
 #define IPPROTO_GGP IPPROTO_GGP
@@ -312,6 +313,7 @@ shutdown(how) -- shut down traffic in one or both directions\n\
 #define IPPROTO_PGM IPPROTO_PGM  // WinSock2 only
 #define IPPROTO_L2TP IPPROTO_L2TP  // WinSock2 only
 #define IPPROTO_SCTP IPPROTO_SCTP  // WinSock2 only
+#endif /* _MSC_VER */
 
 /* Provides the IsWindows7SP1OrGreater() function */
 #include <versionhelpers.h>
@@ -426,6 +428,10 @@ remove_unusable_flags(PyObject *m)
   /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and
    * EAI_* constants are defined in (the already included) ws2tcpip.h.
    */
+#elif defined(__MINGW32__)
+  /* Do not include addrinfo.h as minimum supported version is
+   * _WIN32_WINNT >= WindowsXP(0x0501)
+   */
 #else
 #  include "addrinfo.h"
 #endif
diff --git a/configure.ac b/configure.ac
index e03f864..37554f7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -4188,6 +4188,12 @@ AS_CASE([$ac_sys_system],
   [OSSAUDIODEV_LIBS=""]
 )
 
+dnl On MINGW, you need to link against ws2_32 and iphlpapi for sockets to work
+AS_CASE([$MACHDEP],
+  [win32], [SOCKET_LIBS="-lws2_32 -liphlpapi -lrpcrt4"],
+  [SOCKET_LIBS=""]
+)
+
 dnl detect sqlite3 from Emscripten emport
 PY_CHECK_EMSCRIPTEN_PORT([LIBSQLITE3], [-sUSE_SQLITE3])
 
@@ -5651,18 +5657,33 @@ if test $ac_cv_header_time_altzone = yes; then
     [Define this if your time.h defines altzone.])
 fi
 
+AC_CHECK_HEADERS([ws2tcpip.h])
 AC_CACHE_CHECK([for addrinfo], [ac_cv_struct_addrinfo],
-AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[@%:@include <netdb.h>]], [[struct addrinfo a]])],
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
+#ifdef HAVE_WS2TCPIP_H
+#  include <ws2tcpip.h>
+#else
+#  include <netdb.h>
+#endif]],
+    [[struct addrinfo a]])],
   [ac_cv_struct_addrinfo=yes],
   [ac_cv_struct_addrinfo=no]))
 if test $ac_cv_struct_addrinfo = yes; then
-	AC_DEFINE([HAVE_ADDRINFO], [1], [struct addrinfo (netdb.h)])
+	AC_DEFINE([HAVE_ADDRINFO], [1], [struct addrinfo])
 fi
 
 AC_CACHE_CHECK([for sockaddr_storage], [ac_cv_struct_sockaddr_storage],
 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
-#		include <sys/types.h>
-@%:@		include <sys/socket.h>]], [[struct sockaddr_storage s]])],
+#ifdef HAVE_WS2TCPIP_H
+#include <ws2tcpip.h>
+#endif
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif]],
+    [[struct sockaddr_storage s]])],
   [ac_cv_struct_sockaddr_storage=yes],
   [ac_cv_struct_sockaddr_storage=no]))
 if test $ac_cv_struct_sockaddr_storage = yes; then
@@ -6855,8 +6876,11 @@ AC_CHECK_TYPE(
   [socklen_t], [],
   [AC_DEFINE(
     [socklen_t], [int],
-    [Define to `int' if <sys/socket.h> does not define.]
+    [Define to `int' if <sys/socket.h> or <ws2tcpip.h> does not define.]
   )], [
+#ifdef HAVE_WS2TCPIP_H
+#include <ws2tcpip.h>
+#endif
 #ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif
@@ -7665,7 +7689,9 @@ PY_STDLIB_MOD([mmap],
 PY_STDLIB_MOD([_socket],
   [], m4_flatten([test "$ac_cv_header_sys_socket_h" = "yes"
                     -a "$ac_cv_header_sys_types_h" = "yes"
-                    -a "$ac_cv_header_netinet_in_h" = "yes"]))
+                    -a "$ac_cv_header_netinet_in_h" = "yes"
+                    -o "$MACHDEP" = "win32"]),
+  [], [$SOCKET_LIBS])
 
 dnl platform specific extensions
 PY_STDLIB_MOD([grp], [], [test "$ac_cv_func_getgrgid" = yes -o "$ac_cv_func_getgrgid_r" = yes])
diff --git a/pyconfig.h.in b/pyconfig.h.in
index b359e13..398b64f 100644
--- a/pyconfig.h.in
+++ b/pyconfig.h.in
@@ -59,7 +59,7 @@
 /* Define to 1 if you have the `acosh' function. */
 #undef HAVE_ACOSH
 
-/* struct addrinfo (netdb.h) */
+/* struct addrinfo */
 #undef HAVE_ADDRINFO
 
 /* Define to 1 if you have the `alarm' function. */
@@ -1926,7 +1926,7 @@
 /* Define to `unsigned int' if <sys/types.h> does not define. */
 #undef size_t
 
-/* Define to `int' if <sys/socket.h> does not define. */
+/* Define to `int' if <sys/socket.h> or <ws2tcpip.h> does not define. */
 #undef socklen_t
 
 /* Define to `int' if <sys/types.h> doesn't define. */
