Ws2_32.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Runtime.InteropServices;
  4. namespace Microsoft.Win32
  5. {
  6. /// <summary>
  7. /// Ws2_32.dll
  8. /// </summary>
  9. public static partial class UnsafeNativeMethods
  10. {
  11. /// <summary>
  12. /// The WSAStartup function initiates use of the Winsock DLL by a process.
  13. /// </summary>
  14. /// <param name="wVersionRequested">The highest version of Windows Sockets specification that the caller can use. The high-order byte specifies the minor version number; the low-order byte specifies the major version number.</param>
  15. /// <param name="lpWSAData">A pointer to the WSADATA data structure that is to receive details of the Windows Sockets implementation.</param>
  16. /// <returns>If successful, the WSAStartup function returns zero. Otherwise, it returns one of the error codes listed below.The WSAStartup function directly returns the extended error code in the return value for this function. A call to the WSAGetLastError function is not needed and should not be used.</returns>
  17. [DllImport("ws2_32.dll", SetLastError = true)]
  18. public static extern SocketError WSAStartup(int wVersionRequested, out NativeMethods.WSADATA lpWSAData);
  19. /// <summary>
  20. /// The WSACleanup function terminates use of the Winsock 2 DLL (Ws2_32.dll).
  21. /// </summary>
  22. /// <returns>The return value is zero if the operation was successful. Otherwise, the value SOCKET_ERROR is returned, and a specific error number can be retrieved by calling WSAGetLastError.In a multithreaded environment, WSACleanup terminates Windows Sockets operations for all threads.</returns>
  23. [DllImport("ws2_32.dll", SetLastError = true)]
  24. public static extern SocketError WSACleanup();
  25. /// <summary>
  26. /// The WSASocket function creates a socket that is bound to a specific transport-service provider.
  27. /// </summary>
  28. /// <param name="af">The address family specification. Possible values for the address family are defined in the Winsock2.h header file.On the Windows SDK released for Windows Vista and later, the organization of header files has changed and the possible values for the address family are defined in the Ws2def.h header file. Note that the Ws2def.h header file is automatically included in Winsock2.h, and should never be used directly.The values currently supported are AF_INET or AF_INET6, which are the Internet address family formats for IPv4 and IPv6. Other options for address family (AF_NETBIOS for use with NetBIOS, for example) are supported if a Windows Sockets service provider for the address family is installed. Note that the values for the AF_ address family and PF_ protocol family constants are identical (for example, AF_INET and PF_INET), so either constant can be used.The table below lists common values for address family although many other values are possible.</param>
  29. /// <param name="type">The type specification for the new socket. Possible values for the socket type are defined in the Winsock2.h header file.The following table lists the possible values for the type parameter supported for Windows Sockets 2:In Windows Sockets 2, new socket types were introduced. An application can dynamically discover the attributes of each available transport protocol through the WSAEnumProtocols function. So an application can determine the possible socket type and protocol options for an address family and use this information when specifying this parameter. Socket type definitions in the Winsock2.h and Ws2def.h header files will be periodically updated as new socket types, address families, and protocols are defined.In Windows Sockets 1.1, the only possible socket types are SOCK_DGRAM and SOCK_STREAM.</param>
  30. /// <param name="protocol">The protocol to be used. The possible options for the protocol parameter are specific to the address family and socket type specified. Possible values for the protocol are defined are defined in the Winsock2.h and Wsrm.h header files.On the Windows SDK released for Windows Vista and later,, the organization of header files has changed and this parameter can be one of the values from the IPPROTO enumeration type defined in the Ws2def.h header file. Note that the Ws2def.h header file is automatically included in Winsock2.h, and should never be used directly.If a value of 0 is specified, the caller does not wish to specify a protocol and the service provider will choose the protocol to use.When the af parameter is AF_INET or AF_INET6 and the type is SOCK_RAW, the value specified for the protocol is set in the protocol field of the IPv6 or IPv4 packet header.The table below lists common values for the protocol although many other values are possible.</param>
  31. /// <param name="lpProtocolInfo">A pointer to a WSAPROTOCOL_INFO structure that defines the characteristics of the socket to be created. If this parameter is not NULL, the socket will be bound to the provider associated with the indicated WSAPROTOCOL_INFO structure.</param>
  32. /// <param name="g">An existing socket group ID or an appropriate action to take when creating a new socket and a new socket group.If g is an existing socket group ID, join the new socket to this socket group, provided all the requirements set by this group are met.If g is not an existing socket group ID, then the following values are possible.</param>
  33. /// <param name="dwFlags">A set of flags used to specify additional socket attributes.A combination of these flags may be set, although some combinations are not allowed.Important For multipoint sockets, only one of WSA_FLAG_MULTIPOINT_C_ROOT or WSA_FLAG_MULTIPOINT_C_LEAF flags can be specified, and only one of WSA_FLAG_MULTIPOINT_D_ROOT or WSA_FLAG_MULTIPOINT_D_LEAF flags can be specified. Refer to Multipoint and Multicast Semantics for additional information.</param>
  34. /// <returns>If no error occurs, WSASocket returns a descriptor referencing the new socket. Otherwise, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.Note This error code description is Microsoft-specific.</returns>
  35. [DllImport("ws2_32.dll", SetLastError = true)]
  36. public static extern IntPtr WSASocket(AddressFamily af, SocketType type, ProtocolType protocol, IntPtr lpProtocolInfo, uint g, int dwFlags);
  37. /// <summary>
  38. /// The WSASend function sends data on a connected socket.
  39. /// </summary>
  40. /// <param name="s">A descriptor that identifies a connected socket.</param>
  41. /// <param name="lpBuffers">A pointer to an array of WSABUF structures. Each WSABUF structure contains a pointer to a buffer and the length, in bytes, of the buffer. For a Winsock application, once the WSASend function is called, the system owns these buffers and the application may not access them. This array must remain valid for the duration of the send operation.</param>
  42. /// <param name="dwBufferCount">The number of WSABUF structures in the lpBuffers array.</param>
  43. /// <param name="lpNumberOfBytesSent">A pointer to the number, in bytes, sent by this call if the I/O operation completes immediately.Use NULL for this parameter if the lpOverlapped parameter is not NULL to avoid potentially erroneous results. This parameter can be NULL only if the lpOverlapped parameter is not NULL.</param>
  44. /// <param name="dwFlags">The flags used to modify the behavior of the WSASend function call. For more information, see Using dwFlags in the Remarks section.</param>
  45. /// <param name="lpOverlapped">A pointer to a WSAOVERLAPPED structure. This parameter is ignored for nonoverlapped sockets.</param>
  46. /// <param name="lpCompletionRoutine">A pointer to the completion routine called when the send operation has been completed. This parameter is ignored for nonoverlapped sockets.</param>
  47. /// <returns>If no error occurs and the send operation has completed immediately, WSASend returns zero. In this case, the completion routine will have already been scheduled to be called once the calling thread is in the alertable state. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError. The error code WSA_IO_PENDING indicates that the overlapped operation has been successfully initiated and that completion will be indicated at a later time. Any other error code indicates that the overlapped operation was not successfully initiated and no completion indication will occur.</returns>
  48. [DllImport("ws2_32.dll", SetLastError = true)]
  49. public static extern SocketError WSASend(IntPtr s, ref NativeMethods.WSABUF lpBuffers, int dwBufferCount, int lpNumberOfBytesSent, ref SocketFlags dwFlags, IntPtr lpOverlapped, IntPtr lpCompletionRoutine);
  50. /// <summary>
  51. /// The WSARecvFrom function receives a datagram and stores the source address.
  52. /// </summary>
  53. /// <param name="s">A descriptor identifying a socket.</param>
  54. /// <param name="lpBuffers">A pointer to an array of WSABUF structures. Each WSABUF structure contains a pointer to a buffer and the length of the buffer.</param>
  55. /// <param name="dwBufferCount">The number of WSABUF structures in the lpBuffers array.</param>
  56. /// <param name="lpNumberOfBytesRecvd">A pointer to the number of bytes received by this call if the WSARecvFrom operation completes immediately.Use NULL for this parameter if the lpOverlapped parameter is not NULL to avoid potentially erroneous results. This parameter can be NULL only if the lpOverlapped parameter is not NULL.</param>
  57. /// <param name="lpFlags">A pointer to flags used to modify the behavior of the WSARecvFrom function call. See remarks below.</param>
  58. /// <param name="lpOverlapped">An optional pointer to a buffer that will hold the source address upon the completion of the overlapped operation.</param>
  59. /// <param name="lpCompletionRoutine">A pointer to the size, in bytes, of the "from" buffer required only if lpFrom is specified.</param>
  60. /// <returns>A pointer to a WSAOVERLAPPED structure (ignored for nonoverlapped sockets).</returns>
  61. [DllImport("ws2_32.dll", SetLastError = true)]
  62. public static extern SocketError WSARecv(IntPtr s, ref NativeMethods.WSABUF lpBuffers, int dwBufferCount, out int lpNumberOfBytesRecvd, ref SocketFlags lpFlags, IntPtr lpOverlapped, IntPtr lpCompletionRoutine);
  63. /// <summary>
  64. /// The WSAGetOverlappedResult function retrieves the results of an overlapped operation on the specified socket.
  65. /// </summary>
  66. /// <param name="s">A descriptor identifying the socket.This is the same socket that was specified when the overlapped operation was started by a call to any of the Winsock functions that supports overlappped operations. These functions include AcceptEx, ConnectEx, DisconnectEx, TransmitFile, TransmitPackets, WSARecv, WSARecvFrom, WSARecvMsg, WSASend, WSASendMsg, WSASendTo, and WSAIoctl.</param>
  67. /// <param name="lpOverlapped">A pointer to a WSAOVERLAPPED structure that was specified when the overlapped operation was started. This parameter must not be a NULL pointer.</param>
  68. /// <param name="lpcbTransfer">A pointer to a 32-bit variable that receives the number of bytes that were actually transferred by a send or receive operation, or by the WSAIoctl function. This parameter must not be a NULL pointer.</param>
  69. /// <param name="fWait">A flag that specifies whether the function should wait for the pending overlapped operation to complete. If TRUE, the function does not return until the operation has been completed. If FALSE and the operation is still pending, the function returns FALSE and the WSAGetLastError function returns WSA_IO_INCOMPLETE. The fWait parameter may be set to TRUE only if the overlapped operation selected the event-based completion notification.</param>
  70. /// <param name="lpdwFlags">A pointer to a 32-bit variable that will receive one or more flags that supplement the completion status. If the overlapped operation was initiated through WSARecv or WSARecvFrom, this parameter will contain the results value for lpFlags parameter. This parameter must not be a NULL pointer.</param>
  71. /// <returns>If WSAGetOverlappedResult succeeds, the return value is TRUE. This means that the overlapped operation has completed successfully and that the value pointed to by lpcbTransfer has been updated.If WSAGetOverlappedResult returns FALSE, this means that either the overlapped operation has not completed, the overlapped operation completed but with errors, or the overlapped operation's completion status could not be determined due to errors in one or more parameters to WSAGetOverlappedResult. On failure, the value pointed to by lpcbTransfer will not be updated. Use WSAGetLastError to determine the cause of the failure (either by the WSAGetOverlappedResult function or by the associated overlapped operation).</returns>
  72. [DllImport("ws2_32.dll", SetLastError = true)]
  73. [return: MarshalAs(UnmanagedType.Bool)]
  74. public static extern bool WSAGetOverlappedResult(IntPtr s, IntPtr lpOverlapped, out int lpcbTransfer, bool fWait, out SocketFlags lpdwFlags);
  75. /// <summary>
  76. /// The WSAIoctl function controls the mode of a socket.
  77. /// </summary>
  78. /// <param name="s">A descriptor identifying a socket.</param>
  79. /// <param name="dwIoControlCode">The control code of operation to perform.</param>
  80. /// <param name="lpvInBuffer">A pointer to the input buffer.</param>
  81. /// <param name="cbInBuffer">The size, in bytes, of the input buffer.</param>
  82. /// <param name="lpvOutBuffer">A pointer to the output buffer.</param>
  83. /// <param name="cbOutBuffer">The size, in bytes, of the output buffer.</param>
  84. /// <param name="lpcbBytesReturned">A pointer to actual number of bytes of output.</param>
  85. /// <param name="lpOverlapped">A pointer to a WSAOVERLAPPED structure (ignored for non-overlapped sockets).</param>
  86. /// <param name="lpCompletionRoutine">Note A pointer to the completion routine called when the operation has been completed (ignored for non-overlapped sockets). See Remarks.</param>
  87. /// <returns>Upon successful completion, the WSAIoctl returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.</returns>
  88. [DllImport("ws2_32.dll", SetLastError = true)]
  89. public static extern SocketError WSAIoctl(IntPtr s, int dwIoControlCode, byte[] lpvInBuffer, int cbInBuffer, byte[] lpvOutBuffer, int cbOutBuffer, out int lpcbBytesReturned, IntPtr lpOverlapped, IntPtr lpCompletionRoutine);
  90. /// <summary>
  91. /// The WSAIoctl function controls the mode of a socket.
  92. /// </summary>
  93. /// <param name="s">A descriptor identifying a socket.</param>
  94. /// <param name="dwIoControlCode">The control code of operation to perform.</param>
  95. /// <param name="lpvInBuffer">A pointer to the input buffer.</param>
  96. /// <param name="cbInBuffer">The size, in bytes, of the input buffer.</param>
  97. /// <param name="lpvOutBuffer">A pointer to the output buffer.</param>
  98. /// <param name="cbOutBuffer">The size, in bytes, of the output buffer.</param>
  99. /// <param name="lpcbBytesReturned">A pointer to actual number of bytes of output.</param>
  100. /// <param name="lpOverlapped">A pointer to a WSAOVERLAPPED structure (ignored for non-overlapped sockets).</param>
  101. /// <param name="lpCompletionRoutine">Note A pointer to the completion routine called when the operation has been completed (ignored for non-overlapped sockets). See Remarks.</param>
  102. /// <returns>Upon successful completion, the WSAIoctl returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.</returns>
  103. [DllImport("Ws2_32.dll", SetLastError = true)]
  104. public static extern SocketError WSAIoctl(IntPtr s, int dwIoControlCode, ref Guid lpvInBuffer, int cbInBuffer, ref IntPtr lpvOutBuffer, int cbOutBuffer, out int lpcbBytesReturned, IntPtr lpOverlapped, IntPtr lpCompletionRoutine);
  105. /// <summary>
  106. /// The bind function associates a local address with a socket.
  107. /// </summary>
  108. /// <param name="s">A descriptor identifying an unbound socket.</param>
  109. /// <param name="name">A pointer to a sockaddr structure of the local address to assign to the bound socket .</param>
  110. /// <param name="namelen">The length, in bytes, of the value pointed to by the name parameter.</param>
  111. /// <returns>If no error occurs, bind returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.</returns>
  112. [DllImport("ws2_32.dll", SetLastError = true)]
  113. public static extern SocketError bind(IntPtr s, byte[] name, int namelen);
  114. /// <summary>
  115. /// The listen function places a socket in a state in which it is listening for an incoming connection.
  116. /// </summary>
  117. /// <param name="s">A descriptor identifying a bound, unconnected socket.</param>
  118. /// <param name="backlog">The maximum length of the queue of pending connections. If set to SOMAXCONN, the underlying service provider responsible for socket s will set the backlog to a maximum reasonable value. If set to SOMAXCONN_HINT(N) (where N is a number), the backlog value will be N, adjusted to be within the range (200, 65535). Note that SOMAXCONN_HINT can be used to set the backlog to a larger value than possible with SOMAXCONN.SOMAXCONN_HINT is only supported by the Microsoft TCP/IP service provider. There is no standard provision to obtain the actual backlog value.</param>
  119. /// <returns>If no error occurs, listen returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.</returns>
  120. [DllImport("ws2_32.dll", SetLastError = true)]
  121. public static extern SocketError listen(IntPtr s, int backlog);
  122. /// <summary>
  123. /// The closesocket function closes an existing socket.
  124. /// </summary>
  125. /// <param name="s">A descriptor identifying the socket to close.</param>
  126. /// <returns>If no error occurs, closesocket returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.</returns>
  127. [DllImport("ws2_32.dll", SetLastError = true)]
  128. public static extern SocketError closesocket(IntPtr s);
  129. /// <summary>
  130. /// The getsockname function retrieves the local name for a socket.
  131. /// </summary>
  132. /// <param name="s">Descriptor identifying a socket.</param>
  133. /// <param name="name">Pointer to a SOCKADDR structure that receives the address (name) of the socket.</param>
  134. /// <param name="namelen">Size of the name buffer, in bytes.</param>
  135. /// <returns>If no error occurs, getsockname returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.</returns>
  136. [DllImport("ws2_32.dll", SetLastError = true)]
  137. public static extern SocketError getsockname(IntPtr s, byte[] name, ref int namelen);
  138. /// <summary>
  139. /// The getpeername function retrieves the address of the peer to which a socket is connected.
  140. /// </summary>
  141. /// <param name="s">A descriptor identifying a connected socket.</param>
  142. /// <param name="name">The SOCKADDR structure that receives the address of the peer.</param>
  143. /// <param name="namelen">A pointer to the size, in bytes, of the name parameter.</param>
  144. /// <returns>If no error occurs, getpeername returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.</returns>
  145. [DllImport("ws2_32.dll", SetLastError = true)]
  146. public static extern SocketError getpeername(IntPtr s, byte[] name, ref int namelen);
  147. /// <summary>
  148. /// The getsockopt function retrieves a socket option.
  149. /// </summary>
  150. /// <param name="s">A descriptor identifying a socket.</param>
  151. /// <param name="level">The level at which the option is defined. Example: SOL_SOCKET.</param>
  152. /// <param name="optname">The socket option for which the value is to be retrieved. Example: SO_ACCEPTCONN. The optname value must be a socket option defined within the specified level, or behavior is undefined.</param>
  153. /// <param name="optval">A pointer to the buffer in which the value for the requested option is to be returned.</param>
  154. /// <param name="optlen">A pointer to the size, in bytes, of the optval buffer.</param>
  155. /// <returns>If no error occurs, getsockopt returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.</returns>
  156. [DllImport("ws2_32.dll", SetLastError = true)]
  157. public static extern SocketError getsockopt(IntPtr s, int level, int optname, byte[] optval, int optlen);
  158. /// <summary>
  159. /// The setsockopt function sets a socket option.
  160. /// </summary>
  161. /// <param name="s">A descriptor that identifies a socket.</param>
  162. /// <param name="level">The level at which the option is defined (for example, SOL_SOCKET).</param>
  163. /// <param name="optname">The socket option for which the value is to be set (for example, SO_BROADCAST). The optname parameter must be a socket option defined within the specified level, or behavior is undefined.</param>
  164. /// <param name="optval">A pointer to the buffer in which the value for the requested option is specified.</param>
  165. /// <param name="optlen">The size, in bytes, of the buffer pointed to by the optval parameter.</param>
  166. /// <returns>If no error occurs, setsockopt returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.</returns>
  167. [DllImport("ws2_32.dll", SetLastError = true)]
  168. public static extern SocketError setsockopt(IntPtr s, int level, int optname, byte[] optval, int optlen);
  169. }
  170. }