On Jun 28 17:24, Ozkan Sezer wrote:
> On Thu, Jun 28, 2012 at 5:18 PM, Corinna Vinschen <[email protected]> wrote:
> > On second thought, this #undef/#ifndef juggle is only necessary for
> > FD_SET, given that the other three macros are identical between
> > winsock.h and winsock2.h, isn't it?
> >
>
> Can't remember which, but some other macros have different
> values in ws1 and ws2
>
> BTW, we may conditionally include ws1undef.h, too, like:
>
> --- winsock2.h~
> +++ winsock2.h
> @@ -13,6 +13,7 @@
> #define _WINSOCKAPI_
> #else
> #warning Please include winsock2.h before windows.h
> +#define DO_INCLUDE_WS1UNDEF
> #endif
>
> #ifndef INCL_WINSOCK_API_TYPEDEFS
> @@ -41,7 +42,9 @@
> #define WSAAPI WINAPI
>
> /* undefine macros from winsock.h */
> +#ifdef DO_INCLUDE_WS1UNDEF
> #include <psdk_inc/_ws1_undef.h>
> +#endif
>
> #include <_timeval.h>
> #include <_bsd_types.h>
>
> Just cooked this up in a few seconds, without much thinking.
> If that's OK, you can also retain your ifdefs around those FD_*
> macros too
As far as I can see that wouldn't help much since it would still
potentially undef third party (probably Cygwin) FD_xxx macros.
I think I'd rather go with a _fd_types.h-only solution.
Here's my complete proposal for the fd_set patch again. It includes the
psdk_inc/_fd_types.h file and the psdk_inc/_ws1_undef.h file. The
#undef's are removed from the latter. The FD_xxx macros are still
guarded with #ifndef's. Only the FD_SET handling got more complicated.
Hope that's ok.
Thanks,
Corinna
* psdk_inc/_fd_types.h: Add Cygwin build environment magic to avoid
multiple definition of the select function related datatypes and
macros.
(__WSAFDIsSet): Declare here.
(FD_CLR): Define here.
(FD_ZERO): Ditto.
(FD_ISSET): Ditto.
(FD_SET): Ditto. Make sure to undef FD_SET first, if it has been
defined from winsock.h before.
* psdk_inc/_ws1_undef.h (FD_CLR): Drop #undef.
(FD_ZERO): Ditto.
(FD_ISSET): Ditto.
(FD_SET): Ditto.
Index: include/psdk_inc/_fd_types.h
===================================================================
--- include/psdk_inc/_fd_types.h (revision 5139)
+++ include/psdk_inc/_fd_types.h (working copy)
@@ -12,12 +12,95 @@
#ifndef FD_SETSIZE
#define FD_SETSIZE 64
#endif
+
+#ifndef _SYS_TYPES_FD_SET
+/* fd_set may have been defined by the newlib <sys/types.h>
+ * if __USE_W32_SOCKETS not defined.
+ */
+
typedef struct fd_set
{
u_int fd_count;
SOCKET fd_array[FD_SETSIZE];
} fd_set;
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int WINAPI __WSAFDIsSet(SOCKET,fd_set *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#ifndef FD_CLR
+#define FD_CLR(fd,set) \
+ do { \
+ u_int __i; \
+ for(__i = 0; __i < ((fd_set *)(set))->fd_count; __i++) { \
+ if (((fd_set *)(set))->fd_array[__i] == fd) { \
+ while (__i < ((fd_set *)(set))->fd_count - 1) { \
+ ((fd_set *)(set))->fd_array[__i] = \
+ ((fd_set *)(set))->fd_array[__i + 1]; \
+ __i++; \
+ } \
+ ((fd_set *)(set))->fd_count--; \
+ break; \
+ } \
+ } \
+ } while(0)
+#endif /* !FD_CLR */
+
+#ifndef FD_ZERO
+#define FD_ZERO(set) (((fd_set *)(set))->fd_count = 0)
+#endif /* !FD_ZERO */
+
+#ifndef FD_ISSET
+#define FD_ISSET(fd,set) __WSAFDIsSet((SOCKET)(fd),(fd_set *)(set))
+#endif /* !FD_ISSET */
+
+/* FD_SET is differently implement in winsock.h and winsock2.h. If we
+ encounter that we are going to redefine it, and if the original definition
+ is from winsock.h, make sure to undef FD_SET so it can be redefined to
+ the winsock2.h version. */
+#ifdef _FD_SET_WINSOCK_DEFINED
+#undef _FD_SET_WINSOCK_DEFINED
+#undef FD_SET
+#endif
+#ifndef FD_SET
+#ifdef _WINSOCK2API_
+#define FD_SET(fd,set) \
+ do { \
+ u_int __i; \
+ for(__i = 0; __i < ((fd_set *)(set))->fd_count; __i++) { \
+ if (((fd_set *)(set))->fd_array[__i] == (fd)) { \
+ break; \
+ } \
+ } \
+ if (__i == ((fd_set *)(set))->fd_count) { \
+ if (((fd_set *)(set))->fd_count < FD_SETSIZE) { \
+ ((fd_set *)(set))->fd_array[__i] = (fd); \
+ ((fd_set *)(set))->fd_count++; \
+ } \
+ } \
+ } while(0)
+#else
+#define _FD_SET_WINSOCK_DEFINED
+#define FD_SET(fd,set) \
+ do { \
+ if (((fd_set *)(set))->fd_count < FD_SETSIZE) \
+ ((fd_set *)(set))->fd_array[((fd_set *)(set))->fd_count++] =\
+ (fd);\
+ } while(0)
+#endif /* _WINSOCK2API_ */
+#endif /* !FD_SET */
+
+#elif !defined(USE_SYS_TYPES_FD_SET)
+#warning "fd_set and associated macros have been defined in sys/types. \
+ This can cause runtime problems with W32 sockets"
+#endif /* !_SYS_TYPES_FD_SET */
+
typedef struct fd_set FD_SET;
typedef struct fd_set *PFD_SET;
typedef struct fd_set *LPFD_SET;
Index: include/psdk_inc/_ws1_undef.h
===================================================================
--- include/psdk_inc/_ws1_undef.h (revision 5139)
+++ include/psdk_inc/_ws1_undef.h (working copy)
@@ -4,11 +4,6 @@
* No warranty is given; refer to the file DISCLAIMER.PD within this package.
*/
-#undef FD_CLR
-#undef FD_ZERO
-#undef FD_ISSET
-#undef FD_SET
-
#undef IOCPARM_MASK
#undef IOC_VOID
#undef IOC_OUT
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public