Skip to content

Commit

Permalink
Detect winsock2 and setup _socket module on MINGW
Browse files Browse the repository at this point in the history
Co-authored-by: Алексей <[email protected]>
  • Loading branch information
Alexpux authored and lazka committed Aug 25, 2023
1 parent a96e9c7 commit f3aa6d1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Misc/config_mingw
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ shutdown(how) -- shut down traffic in one or both directions\n\
# endif

/* Macros based on the IPPROTO enum, see: https://bugs.python.org/issue29515 */
#ifdef MS_WINDOWS
#ifdef _MSC_VER
#define IPPROTO_ICMP IPPROTO_ICMP
#define IPPROTO_IGMP IPPROTO_IGMP
#define IPPROTO_GGP IPPROTO_GGP
Expand Down Expand Up @@ -305,7 +305,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 /* MS_WINDOWS */
#endif /* _MSC_VER */

/* Provides the IsWindows7SP1OrGreater() function */
#include <versionhelpers.h>
Expand Down Expand Up @@ -404,6 +404,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
Expand Down
28 changes: 23 additions & 5 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -5329,18 +5329,33 @@ if test $ac_cv_header_time_altzone = yes; then
AC_DEFINE(HAVE_ALTZONE, 1, [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
Expand Down Expand Up @@ -6421,7 +6436,10 @@ fi

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
Expand Down
4 changes: 2 additions & 2 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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. */
Expand Down Expand Up @@ -1842,7 +1842,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. */
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,9 @@ def detect_simple_extensions(self):
# grp(3)
self.addext(Extension('grp', ['grpmodule.c']))

self.addext(Extension('_socket', ['socketmodule.c']))
self.addext(Extension(
'_socket', ['socketmodule.c'],
libraries=(['ws2_32', 'iphlpapi'] if MS_WINDOWS else None)))
self.addext(Extension('spwd', ['spwdmodule.c']))

# select(2); not on ancient System V
Expand Down

0 comments on commit f3aa6d1

Please sign in to comment.