OSDN Git Service

Factor <winsock.h> duplicate content out of <winsock2.h>
[mingw/mingw-org-wsl.git] / w32api / include / winsock.h
index 5a1d34c..08967b3 100644 (file)
@@ -1,7 +1,9 @@
 /*
  * winsock.h
  *
- * Definitions for WinSock Version 1.1 API.
+ * Definitions for WinSock Version 1.1 API; also includes a subset of the
+ * definitions which become applicable for WinSock Version 2, filtered such
+ * that they are exposed only when this file is included by <winsock2.h>
  *
  *
  * $Id$
 #endif
 #endif
 
+#if ! defined _USE_SYS_TYPES_FD_SET && defined USE_SYS_TYPES_FD_SET
+/* Originally defined by the deprecated name, USE_SYS_TYPES_FD_SET, users
+ * may specify this to suppress warnings, in the event that (incompatible)
+ * fd_set manipulation macros may have been inherited from <sys/types.h>;
+ * accommodate use of the deprecated feature test name.
+ */
+#warning "Feature test macro USE_SYS_TYPES_FD_SET is deprecated."
+#warning "Use the _USE_SYS_TYPES_FD_SET feature test macro instead."
+#define _USE_SYS_TYPES_FD_SET  1
+#endif
+
+#ifndef __WINSOCK2_DEPRECATED
+/* Some of the WinSock v1.1 declarations have become deprecated in
+ * WinSock v2.  If <winsock2.h> has been included, this deprecation
+ * attribute macro should already have been defined; if not, we now
+ * define it to do nothing.
+ */
+#define __WINSOCK2_DEPRECATED
+#endif
+
 _BEGIN_C_DECLS
 
 typedef u_int  SOCKET;
@@ -100,50 +122,87 @@ struct fd_set
   SOCKET       fd_array[FD_SETSIZE];
 } fd_set;
 
-int PASCAL __WSAFDIsSet (SOCKET, fd_set *);
+#ifndef FD_ISSET
+int FD_ISSET (SOCKET, fd_set *);
+#define FD_ISSET( __fd, __set )  __FD_ISSET ((__fd), (__set))
 
-#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 /* ! defined FD_CLR */
+/* Microsoft provide this library function equivalent of the FD_ISSET
+ * macro, and erroneously claim that it is neccessary to implement the
+ * macro.  We could just as easily implement it entirely inline...
+ */
+int PASCAL __WSAFDIsSet (SOCKET, fd_set *);
+/* ...but, given the availability of the library function, we may just
+ * as well use it.
+ */
+__CRT_ALIAS int __FD_ISSET( SOCKET __fd, fd_set *__set )
+{ return __WSAFDIsSet (__fd, __set); }
+#endif /* ! defined FD_ISSET */
 
 #ifndef FD_SET
-#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)
+#if !_WINSOCK_ANOMALOUS_TYPEDEFS
+/* WinSock is intended to mimic the Berkeley Sockets API, for which
+ * POSIX.1 provides a reference specification; this states that FD_SET
+ * may be implemented as either a macro, or as a function.  The reference
+ * <winsock.h> implementation at http://www.sockets.com/winsock.htm#WinsockH
+ * includes a typedef for FD_SET, which a) conflicts with the latter POSIX.1
+ * provision, and b) creates potential confusion with the former.  Thus, we
+ * prefer to conform with POSIX.1 functional semantics, and recommend that
+ * users avoid the potentially confusing FD_SET typedefs, so allowing us
+ * to provide this function prototype:
+ */
+void FD_SET (SOCKET, fd_set *);
+
+#else  /* _WINSOCK_ANOMALOUS_TYPEDEFS */
+/* However, for users who insist on eschewing standard C/C++ syntax, and
+ * for whatever reason must use FD_SET as a data type, instead of correctly
+ * referring to fd_set, or for pointer references, use PFD_SET or LPFD_SET
+ * instead of standard fd_set * references, we make these anomalous types
+ * visible, when the _WINSOCK_ANOMALOUS_TYPEDEFS feature test macro is
+ * defined with a non-zero value.
+ */
+#warning "FD_SET, PFD_SET, and LPFD_SET data types are non-portable."
+#warning "Use portable fd_set, and fd_set * type references instead."
+
+typedef struct fd_set FD_SET, *PFD_SET, *LPFD_SET;
+#endif
+#define FD_SET( __fd, __set )  __FD_SET ((__fd), (__set))
+__CRT_ALIAS void __FD_SET (SOCKET __fd, fd_set *__set)
+{ if( (__set->fd_count < FD_SETSIZE) && ! FD_ISSET (__fd, __set) )
+    __set->fd_array[__set->fd_count++] = __fd;
+}
 #endif /* ! defined FD_SET */
 
+#ifndef FD_CLR
+void FD_CLR (SOCKET, fd_set *);
+#define FD_CLR( __fd, __set )  __FD_CLR ((__fd), (__set))
+__CRT_ALIAS void __FD_CLR (SOCKET __fd, fd_set *__set)
+{ u_int __m, __n; for (__m = __n = 0; __n < __set->fd_count; __n++)
+  { if (__fd != __set->fd_array[__n])
+    { if (__m < __n) __set->fd_array[__m] = __set->fd_array[__n];
+      ++__m;
+    }
+  } __set->fd_count = __m;
+}
+#endif /* ! defined FD_CLR */
+
 #ifndef FD_ZERO
-#define FD_ZERO( set )  (((fd_set *)(set))->fd_count = 0)
+void FD_ZERO (fd_set *);
+#define FD_ZERO( __set )  __FD_ZERO (__set)
+__CRT_ALIAS void __FD_ZERO (fd_set *__set)
+{ __set->fd_count = 0; }
 #endif /* ! defined FD_ZERO */
 
-#ifndef FD_ISSET
-#define FD_ISSET( fd, set )  __WSAFDIsSet((SOCKET)(fd), (fd_set *)(set))
-#endif /* ! defined FD_ISSET */
-
-#elif ! defined USE_SYS_TYPES_FD_SET
+#elif ! defined _USE_SYS_TYPES_FD_SET
 /* Definitions from <sys/types.h> probably aren't what the user wants;
  * if they know what they are doing, and they are sure that this really
- * is what they want, then they may enable the USE_SYS_TYPES_FD_SET
+ * is what they want, then they may enable the _USE_SYS_TYPES_FD_SET
  * feature test macro, to suppress this warning.
  */
 #warning "fd_set and associated macros have been defined in <sys/types.h>"
 #warning "Your <sys/types.h> may cause runtime problems with W32 sockets."
 #endif /* !_SYS_TYPES_FD_SET */
 
-#if ! (defined __INSIDE_CYGWIN__ || defined __INSIDE_MSYS__)
+#ifndef __INSIDE_MSYS__
 
 struct hostent
 { char          *h_name;
@@ -159,7 +218,7 @@ struct linger
   u_short        l_linger;
 };
 
-#endif /* ! (__INSIDE_CYGWIN__ || __INSIDE_MSYS__) */
+#endif /* !__INSIDE_MSYS__ */
 
 #define IOCPARM_MASK                         0x7F
 #define IOC_VOID                       0x20000000
@@ -167,7 +226,7 @@ struct linger
 #define IOC_IN                         0x80000000
 #define IOC_INOUT                  (IOC_IN | IOC_OUT)
 
-#if ! (defined __INSIDE_CYGWIN__ || defined __INSIDE_MSYS__)
+#ifndef __INSIDE_MSYS__
 
 #define _IO(x,y)       (IOC_VOID|((x)<<8)|(y))
 #define _IOR(x,y,t)    (IOC_OUT|(((long)sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y))
@@ -175,7 +234,7 @@ struct linger
 
 #define FIONBIO                _IOW('f', 126, u_long)
 
-#endif /* ! (__INSIDE_CYGWIN__ || __INSIDE_MSYS__) */
+#endif /* !__INSIDE_MSYS__ */
 
 #define FIONREAD               _IOR('f', 127, u_long)
 #define FIOASYNC               _IOW('f', 125, u_long)
@@ -185,7 +244,7 @@ struct linger
 #define SIOCGLOWAT             _IOR('s',  3, u_long)
 #define SIOCATMARK             _IOR('s',  7, u_long)
 
-#if ! ( defined __INSIDE_CYGWIN__ || defined __INSIDE_MSYS__)
+#ifndef __INSIDE_MSYS__
 
 struct netent
 { char          *n_name;
@@ -207,7 +266,7 @@ struct protoent
   short          p_proto;
 };
 
-#endif /* ! (__INSIDE_CYGWIN__ || __INSIDE_MSYS__) */
+#endif /* !__INSIDE_MSYS__ */
 
 #define IPPROTO_IP                                0
 #define IPPROTO_ICMP                              1
@@ -332,6 +391,7 @@ struct WSAData
 
 #endif /* !__INSIDE_MSYS__ */
 
+#ifndef _WINSOCK2_H
 /* The following IP defines are specific to WinSock v1.1 (wsock32.dll).
  * They may cause errors, or produce unexpected results, if exposed when
  * compiling application code which is intended to use the getsockopts(),
@@ -352,6 +412,7 @@ struct ip_mreq
 { struct in_addr        imr_multiaddr;
   struct in_addr        imr_interface;
 };
+#endif /* !_WINSOCK2_H */
 
 #define INVALID_SOCKET                  (SOCKET)(~0)
 #define SOCKET_ERROR                            (-1)
@@ -390,7 +451,7 @@ struct ip_mreq
 #define AF_ATM                                   22
 #define AF_INET6                                 23
 
-#if ! (defined __INSIDE_CYGWIN__ || defined __INSIDE_MSYS__)
+#ifndef __INSIDE_MSYS__
 
 #define AF_MAX                                   24
 
@@ -399,7 +460,7 @@ struct sockaddr
   char                  sa_data[14];
 };
 
-#endif /* ! (__INSIDE_CYGWIN__ || __INSIDE_MSYS__) */
+#endif /* !__INSIDE_MSYS__ */
 
 struct sockproto
 { u_short               sp_family;
@@ -434,30 +495,78 @@ struct sockproto
 #define PF_MAX                               AF_MAX
 
 #define SOL_SOCKET                           0xFFFF
-#define SOMAXCONN                                 5
 
-#if ! (defined __INSIDE_CYGWIN__ || defined __INSIDE_MSYS__)
+#ifndef __INSIDE_MSYS__
+
+/* This definition of SOMAXCONN is correct for WinSock v1.1, but not
+ * for WinSock v2; we define it thus here, but note that, if included
+ * by <winsock2.h>, it will subsequently be overridden by the correct
+ * WinSock v2 definition.
+ */
+#define SOMAXCONN                                 5
 
 #define MSG_OOB                                   1
 #define MSG_PEEK                                  2
 #define MSG_DONTROUTE                             4
 
-#endif  /* ! (__INSIDE_CYGWIN__ || __INSIDE_MSYS__) */
+#endif  /* !__INSIDE_MSYS__ */
 
 #define MSG_MAXIOVLEN                            16
 #define MSG_PARTIAL                          0x8000
 #define MAXGETHOSTSTRUCT                       1024
 
-#define FD_READ                                   1
-#define FD_WRITE                                  2
-#define FD_OOB                                    4
-#define FD_ACCEPT                                 8
-#define FD_CONNECT                               16
-#define FD_CLOSE                                 32
+enum
+{ /* Enumerate the flags used to represent the events which may be
+   * detected on any socket, when monitored via an fd_set array.
+   */
+  FD_READ_BIT = 0,
+# define FD_READ                     (1 << FD_READ_BIT)
+
+  FD_WRITE_BIT,
+# define FD_WRITE                    (1 << FD_WRITE_BIT)
+
+  FD_OOB_BIT,
+# define FD_OOB                      (1 << FD_OOB_BIT)
+
+  FD_ACCEPT_BIT,
+# define FD_ACCEPT                   (1 << FD_ACCEPT_BIT)
+
+  FD_CONNECT_BIT,
+# define FD_CONNECT                  (1 << FD_CONNECT_BIT)
+
+  FD_CLOSE_BIT,
+# define FD_CLOSE                    (1 << FD_CLOSE_BIT)
+
+# ifdef _WINSOCK2_H
+/* WinSock v1.1 defines no further events, beyond FD_CLOSE (1 << 5 = 32).
+ * The following are specific to WinSock v2; for convenience, they may be
+ * enumerated here, but they are exposed only when <winsock.h> is included
+ * indirectly, by way of including <winsock2.h>
+ */
+  FD_QOS_BIT,
+# define FD_QOS                      (1 << FD_QOS_BIT)
+
+  FD_GROUP_QOS_BIT,
+# define FD_GROUP_QOS                (1 << FD_GROUP_QOS_BIT)
+
+  FD_ROUTING_INTERFACE_CHANGE_BIT,
+# define FD_ROUTING_INTERFACE_CHANGE  (1 << FD_ROUTING_INTERFACE_CHANGE_BIT)
+
+  FD_ADDRESS_LIST_CHANGE_BIT,
+# define FD_ADDRESS_LIST_CHANGE       (1 << FD_ADDRESS_LIST_CHANGE_BIT)
+
+# endif /* _WINSOCK2_H */
+  /* Regardless of WinSock version, FD_MAX_EVENTS represents the first
+   * unused flag bit, whence we may deduce FD_ALL_EVENTS, as a mask for
+   * all supported event flags, specific to the WinSock version in use.
+   */
+  FD_MAX_EVENTS,
+# define FD_ALL_EVENTS                 ((1 << FD_MAX_EVENTS) - 1)
+};
 
 #define WSANO_ADDRESS                  WSANO_DATA
 
-#if ! (defined __INSIDE_CYGWIN__ || defined __INSIDE_MSYS__)
+#ifndef __INSIDE_MSYS__
 
 #define h_errno                        WSAGetLastError()
 #define HOST_NOT_FOUND                 WSAHOST_NOT_FOUND
@@ -466,7 +575,7 @@ struct sockproto
 #define NO_DATA                        WSANO_DATA
 #define NO_ADDRESS                     WSANO_ADDRESS
 
-#endif /* ! (__INSIDE_CYGWIN__ || __INSIDE_MSYS__) */
+#endif /* !__INSIDE_MSYS__ */
 
 WINSOCK_API_LINKAGE SOCKET PASCAL accept (SOCKET, struct sockaddr *, int *);
 
@@ -504,10 +613,15 @@ WINSOCK_API_LINKAGE int PASCAL WSACleanup (void);
 WINSOCK_API_LINKAGE void PASCAL WSASetLastError (int);
 WINSOCK_API_LINKAGE int PASCAL WSAGetLastError (void);
 
-WINSOCK_API_LINKAGE BOOL PASCAL WSAIsBlocking (void);
-WINSOCK_API_LINKAGE int PASCAL WSAUnhookBlockingHook (void);
-WINSOCK_API_LINKAGE FARPROC PASCAL WSASetBlockingHook (FARPROC);
-WINSOCK_API_LINKAGE int PASCAL WSACancelBlockingCall (void);
+/* The following four pseudo-blocking functions are provided
+ * to support WinSock v1.1, but have been marked as deprecated
+ * in WinSock v2; they remain available, but it is recommended
+ * that threads should be used instead!
+ */
+WINSOCK_API_LINKAGE BOOL PASCAL WSAIsBlocking (void) __WINSOCK2_DEPRECATED;
+WINSOCK_API_LINKAGE int PASCAL WSAUnhookBlockingHook (void) __WINSOCK2_DEPRECATED;
+WINSOCK_API_LINKAGE FARPROC PASCAL WSASetBlockingHook (FARPROC) __WINSOCK2_DEPRECATED;
+WINSOCK_API_LINKAGE int PASCAL WSACancelBlockingCall (void) __WINSOCK2_DEPRECATED;
 
 WINSOCK_API_LINKAGE HANDLE PASCAL WSAAsyncGetServByName (HWND, u_int, const char *, const char *, char *, int);
 WINSOCK_API_LINKAGE HANDLE PASCAL WSAAsyncGetServByPort (HWND, u_int, int, const char *, char *, int);
@@ -519,16 +633,17 @@ WINSOCK_API_LINKAGE HANDLE PASCAL WSAAsyncGetHostByAddr (HWND, u_int, const char
 WINSOCK_API_LINKAGE int PASCAL WSACancelAsyncRequest (HANDLE);
 WINSOCK_API_LINKAGE int PASCAL WSAAsyncSelect (SOCKET, HWND, u_int, long);
 
-#if ! (defined __INSIDE_CYGWIN__ || defined __INSIDE_MSYS__)
+#ifndef __INSIDE_MSYS__
 
 WINSOCK_API_LINKAGE u_long PASCAL htonl (u_long);
 WINSOCK_API_LINKAGE u_long PASCAL ntohl (u_long);
 WINSOCK_API_LINKAGE u_short PASCAL htons (u_short);
 WINSOCK_API_LINKAGE u_short PASCAL ntohs (u_short);
 WINSOCK_API_LINKAGE int PASCAL select (int nfds, fd_set *, fd_set *, fd_set *, const struct timeval *);
-int PASCAL gethostname (char *, int );
 
-#endif /* ! (__INSIDE_CYGWIN__ || __INSIDE_MSYS__) */
+#endif /* !__INSIDE_MSYS__ */
+
+WINSOCK_API_LINKAGE int PASCAL gethostname (char *, int);
 
 #define WSAMAKEASYNCREPLY(b,e)                 MAKELONG(b,e)
 #define WSAMAKESELECTREPLY(e,error)            MAKELONG(e,error)
@@ -537,7 +652,6 @@ int PASCAL gethostname (char *, int );
 #define WSAGETSELECTEVENT(l)                   LOWORD(l)
 #define WSAGETSELECTERROR(l)                   HIWORD(l)
 
-typedef struct fd_set FD_SET, *PFD_SET, *LPFD_SET;
 typedef struct sockaddr SOCKADDR, *PSOCKADDR, *LPSOCKADDR;
 typedef struct sockaddr_in SOCKADDR_IN, *PSOCKADDR_IN, *LPSOCKADDR_IN;
 typedef struct linger LINGER, *PLINGER, *LPLINGER;
@@ -549,6 +663,7 @@ typedef struct timeval TIMEVAL, *PTIMEVAL, *LPTIMEVAL;
 
 _END_C_DECLS
 
+#ifndef _WINSOCK2_H
 /* MSDN documentation indicates that the MS-specific extensions exported
  * from mswsock.dll, (i.e. the AcceptEx(), TransmitFile(), WSARecEx(), and
  * GetAcceptExSockaddrs() functions), are declared in <mswsock.h>.  These
@@ -562,6 +677,7 @@ _END_C_DECLS
  * references to the mswsock extensions.
  */
 #include <mswsock.h>
+#endif
 
 #undef __WINSOCK_H_SOURCED__
 #endif /* _WINSOCK_H: $RCSfile$: end of file */