The Labs \ Source Viewer \ SSCLI \ System.Net \ WSAPROTOCOLCHAIN

  1. //------------------------------------------------------------------------------
  2. // <copyright file="UnsafeNativeMethods.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Net
  16. {
  17.     using System.Runtime.InteropServices;
  18.     using System.Runtime.CompilerServices;
  19.     using System.Security.Permissions;
  20.     using System.Text;
  21.     using System.Net.Sockets;
  22.     using System.Net.Cache;
  23.     using System.Threading;
  24.     using System.ComponentModel;
  25.     using System.Collections;
  26.     using System.Globalization;
  27.     using System.Runtime.ConstrainedExecution;
  28.     using System.Security;
  29.     using Microsoft.Win32.SafeHandles;
  30.    
  31.     [System.Security.SuppressUnmanagedCodeSecurityAttribute()]
  32.     static internal class UnsafeNclNativeMethods
  33.     {
  34.        
  35.         #if !PLATFORM_UNIX
  36.         internal const string DLLPREFIX = "";
  37.         internal const string DLLSUFFIX = ".dll";
  38.         #else // !PLATFORM_UNIX
  39.         #if __APPLE__
  40.         internal const string DLLPREFIX = "lib";
  41.         internal const string DLLSUFFIX = ".dylib";
  42.         #else
  43.         internal const string DLLPREFIX = "lib";
  44.         internal const string DLLSUFFIX = ".so";
  45.         #endif
  46.         #endif // !PLATFORM_UNIX
  47.        
  48.         internal const string ROTOR_PAL = DLLPREFIX + "rotor_pal" + DLLSUFFIX;
  49.         internal const string ROTOR_PALRT = DLLPREFIX + "rotor_palrt" + DLLSUFFIX;
  50.         private const string KERNEL32 = ROTOR_PAL;
  51.        
  52.         private const string WS2_32 = ExternDll.Kernel32;
  53.         // Resolves to rotor_pal
  54.         private const string SECUR32 = "secur32.dll";
  55.         private const string CRYPT32 = "crypt32.dll";
  56.         private const string ADVAPI32 = "advapi32.dll";
  57.         private const string HTTPAPI = "httpapi.dll";
  58.         private const string SCHANNEL = "schannel.dll";
  59.         private const string SECURITY = "security.dll";
  60.         private const string RASAPI32 = "rasapi32.dll";
  61.         private const string WININET = "wininet.dll";
  62.         private const string WINHTTP = "winhttp.dll";
  63.        
  64. /*
  65.         //                                   
  66.         [DllImport(KERNEL32)]
  67.         internal static extern IntPtr CreateSemaphore([In] IntPtr lpSemaphoreAttributes, [In] int lInitialCount, [In] int lMaximumCount, [In] string lpName);
  68.         */       
  69.        
  70. /* Consider removing.
  71.         [DllImport(KERNEL32)]
  72.         internal static extern IntPtr CreateEvent([In] IntPtr lpEventAttributes, [In] bool manualResetEvent, [In] bool initalState, [In] IntPtr lpName);
  73.         */       
  74.        
  75.         [DllImport(KERNEL32)]
  76.         static internal extern IntPtr CreateSemaphore(        [In()]
  77. IntPtr lpSemaphoreAttributes,         [In()]
  78. int lInitialCount,         [In()]
  79. int lMaximumCount,         [In()]
  80. IntPtr lpName);
  81.        
  82.         #if DEBUG
  83.         [DllImport(KERNEL32)]
  84.         static internal extern bool ReleaseSemaphore(        [In()]
  85. IntPtr hSemaphore,         [In()]
  86. int lReleaseCount,         [Out()]
  87. out int lpPreviousCount);
  88.        
  89.         #else
  90.         [DllImport(KERNEL32)]
  91.         static internal extern bool ReleaseSemaphore(        [In()]
  92. IntPtr hSemaphore,         [In()]
  93. int lReleaseCount,         [In()]
  94. IntPtr lpPreviousCount);
  95.         #endif
  96.        
  97.         static internal class ErrorCodes
  98.         {
  99.             internal const uint ERROR_SUCCESS = 0;
  100.             internal const uint ERROR_HANDLE_EOF = 38;
  101.             internal const uint ERROR_NOT_SUPPORTED = 50;
  102.             internal const uint ERROR_INVALID_PARAMETER = 87;
  103.             internal const uint ERROR_ALREADY_EXISTS = 183;
  104.             internal const uint ERROR_MORE_DATA = 234;
  105.             internal const uint ERROR_OPERATION_ABORTED = 995;
  106.             internal const uint ERROR_IO_PENDING = 997;
  107.             internal const uint ERROR_NOT_FOUND = 1168;
  108.         }
  109.        
  110.         [DllImport(KERNEL32, ExactSpelling = true, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
  111.         static internal extern uint GetCurrentThreadId();
  112.        
  113.         #if STRESS || !DEBUG
  114.         [DllImport(KERNEL32, ExactSpelling = true)]
  115.         static internal extern void DebugBreak();
  116.         #endif
  117.        
  118.         [DllImport(ROTOR_PALRT, CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "PAL_FetchConfigurationStringW")]
  119.         static internal extern bool FetchConfigurationString(bool perMachine, string parameterName, StringBuilder parameterValue, int parameterValueLength);
  120.        
  121.        
  122.         // Because the regular SafeNetHandles has a LocalAlloc with a different return type.
  123.         [System.Security.SuppressUnmanagedCodeSecurityAttribute()]
  124.         static internal class SafeNetHandlesSafeOverlappedFree
  125.         {
  126.             [DllImport(ExternDll.Kernel32, ExactSpelling = true, SetLastError = true)]
  127.             static internal extern SafeOverlappedFree LocalAlloc(int uFlags, UIntPtr sizetdwBytes);
  128.         }
  129.        
  130.        
  131.         [System.Security.SuppressUnmanagedCodeSecurityAttribute()]
  132.         static internal class SafeNetHandles
  133.         {
  134.            
  135.            
  136.             [DllImport(ExternDll.Kernel32, ExactSpelling = true, SetLastError = true)]
  137.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  138.             static internal extern bool CloseHandle(IntPtr handle);
  139.            
  140.             [DllImport(ExternDll.Kernel32, ExactSpelling = true, SetLastError = true)]
  141.             static internal extern SafeLocalFree LocalAlloc(int uFlags, UIntPtr sizetdwBytes);
  142.            
  143.             [DllImport(ExternDll.Kernel32, ExactSpelling = true, SetLastError = true)]
  144.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  145.             static internal extern IntPtr LocalFree(IntPtr handle);
  146.            
  147.            
  148.            
  149.             [DllImport(KERNEL32, ExactSpelling = true, SetLastError = true)]
  150.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  151.             unsafe static internal extern bool FreeLibrary(            [In()]
  152. IntPtr hModule);
  153.            
  154.            
  155.             [DllImport(ExternDll.Kernel32, ExactSpelling = true, SetLastError = true)]
  156.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  157.             static internal extern IntPtr GlobalFree(IntPtr handle);
  158.            
  159.             // Blocking call - requires IntPtr instead of SafeCloseSocket.
  160.             [DllImport(WS2_32, ExactSpelling = true, SetLastError = true)]
  161.             static internal extern SafeCloseSocket.InnerSafeCloseSocket accept(            [In()]
  162. IntPtr socketHandle,             [Out()]
  163. byte[] socketAddress,             [In(), Out()]
  164. ref int socketAddressSize);
  165.            
  166.             [DllImport(WS2_32, ExactSpelling = true, SetLastError = true)]
  167.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  168.             static internal extern SocketError closesocket(            [In()]
  169. IntPtr socketHandle);
  170.            
  171.             [DllImport(WS2_32, ExactSpelling = true, SetLastError = true)]
  172.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  173.             static internal extern SocketError ioctlsocket(            [In()]
  174. IntPtr handle,             [In()]
  175. int cmd,             [In(), Out()]
  176. ref int argp);
  177.            
  178.             [DllImport(WS2_32, ExactSpelling = true, SetLastError = true)]
  179.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  180.             static internal extern SocketError WSAEventSelect(            [In()]
  181. IntPtr handle,             [In()]
  182. IntPtr Event,             [In()]
  183. AsyncEventBits NetworkEvents);
  184.            
  185.             [DllImport(WS2_32, ExactSpelling = true, SetLastError = true)]
  186.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  187.             static internal extern SocketError setsockopt(            [In()]
  188. IntPtr handle,             [In()]
  189. SocketOptionLevel optionLevel,             [In()]
  190. SocketOptionName optionName,             [In()]
  191. ref Linger linger,             [In()]
  192. int optionLength);
  193.            
  194.             /* Consider removing
  195.             [DllImport(WS2_32, ExactSpelling=true, SetLastError=true)]
  196.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  197.             internal static extern SocketError setsockopt(
  198.                                               [In] IntPtr handle,
  199.                                               [In] SocketOptionLevel optionLevel,
  200.                                               [In] SocketOptionName optionName,
  201.                                               [In] ref int optionValue,
  202.                                               [In] int optionLength
  203.                                               );
  204.             */           
  205.            
  206.         }
  207.        
  208.         //
  209.         // UnsafeNclNativeMethods.OSSOCK class contains all Unsafe() calls and should all be protected
  210.         // by the appropriate SocketPermission() to connect/accept to/from remote
  211.         // peers over the network and to perform name resolution.
  212.         // te following calls deal mainly with:
  213.         // 1) socket calls
  214.         // 2) DNS calls
  215.         //
  216.        
  217.         //
  218.         // here's a brief explanation of all possible decorations we use for PInvoke.
  219.         // these are used in such a way that we hope to gain maximum performance from the
  220.         // unmanaged/managed/unmanaged transition we need to undergo when calling into winsock:
  221.         //
  222.         // [In] (Note: this is similar to what msdn will show)
  223.         // the managed data will be marshalled so that the unmanaged function can read it but even
  224.         // if it is changed in unmanaged world, the changes won't be propagated to the managed data
  225.         //
  226.         // [Out] (Note: this is similar to what msdn will show)
  227.         // the managed data will not be marshalled so that the unmanaged function will not see the
  228.         // managed data, if the data changes in unmanaged world, these changes will be propagated by
  229.         // the marshaller to the managed data
  230.         //
  231.         // objects are marshalled differently if they're:
  232.         //
  233.         // 1) structs
  234.         // for structs, by default, the whole layout is pushed on the stack as it is.
  235.         // in order to pass a pointer to the managed layout, we need to specify either the ref or out keyword.
  236.         //
  237.         // a) for IN and OUT:
  238.         // [In, Out] ref Struct ([In, Out] is optional here)
  239.         //
  240.         // b) for IN only (the managed data will be marshalled so that the unmanaged
  241.         // function can read it but even if it changes it the change won't be propagated
  242.         // to the managed struct)
  243.         // [In] ref Struct
  244.         //
  245.         // c) for OUT only (the managed data will not be marshalled so that the
  246.         // unmanaged function cannot read, the changes done in unmanaged code will be
  247.         // propagated to the managed struct)
  248.         // [Out] out Struct ([Out] is optional here)
  249.         //
  250.         // 2) array or classes
  251.         // for array or classes, by default, a pointer to the managed layout is passed.
  252.         // we don't need to specify neither the ref nor the out keyword.
  253.         //
  254.         // a) for IN and OUT:
  255.         // [In, Out] byte[]
  256.         //
  257.         // b) for IN only (the managed data will be marshalled so that the unmanaged
  258.         // function can read it but even if it changes it the change won't be propagated
  259.         // to the managed struct)
  260.         // [In] byte[] ([In] is optional here)
  261.         //
  262.         // c) for OUT only (the managed data will not be marshalled so that the
  263.         // unmanaged function cannot read, the changes done in unmanaged code will be
  264.         // propagated to the managed struct)
  265.         // [Out] byte[]
  266.         //
  267.         [System.Security.SuppressUnmanagedCodeSecurityAttribute()]
  268.         static internal class OSSOCK
  269.         {
  270.            
  271.             private const string WS2_32 = ROTOR_PAL;
  272.            
  273.             //
  274.             // IPv6 Changes: These are initialized in InitializeSockets - don't set them here or
  275.             // there will be an ordering problem with the call above that will
  276.             // result in both being set to false !
  277.             //
  278.            
  279.             [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  280.             internal struct WSAPROTOCOLCHAIN
  281.             {
  282.                 internal int ChainLen;
  283. /* the length of the chain,    */                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)]
  284.                 internal uint[] ChainEntries;
  285.                 /* a list of dwCatalogEntryIds */            }
  286.            
  287.             [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  288.             internal struct WSAPROTOCOL_INFO
  289.             {
  290.                 internal uint dwServiceFlags1;
  291.                 internal uint dwServiceFlags2;
  292.                 internal uint dwServiceFlags3;
  293.                 internal uint dwServiceFlags4;
  294.                 internal uint dwProviderFlags;
  295.                 Guid ProviderId;
  296.                 internal uint dwCatalogEntryId;
  297.                 WSAPROTOCOLCHAIN ProtocolChain;
  298.                 internal int iVersion;
  299.                 internal AddressFamily iAddressFamily;
  300.                 internal int iMaxSockAddr;
  301.                 internal int iMinSockAddr;
  302.                 internal int iSocketType;
  303.                 internal int iProtocol;
  304.                 internal int iProtocolMaxOffset;
  305.                 internal int iNetworkByteOrder;
  306.                 internal int iSecurityScheme;
  307.                 internal uint dwMessageSize;
  308.                 internal uint dwProviderReserved;
  309.                 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  310.                 internal string szProtocol;
  311.             }
  312.            
  313.             [StructLayout(LayoutKind.Sequential)]
  314.             internal struct ControlData
  315.             {
  316.                 internal UIntPtr length;
  317.                 internal uint level;
  318.                 internal uint type;
  319.                 internal uint address;
  320.                 internal uint index;
  321.             }
  322.            
  323.             [StructLayout(LayoutKind.Sequential)]
  324.             internal struct ControlDataIPv6
  325.             {
  326.                 internal UIntPtr length;
  327.                 internal uint level;
  328.                 internal uint type;
  329.                 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  330.                 internal byte[] address;
  331.                 internal uint index;
  332.             }
  333.            
  334.            
  335.             [StructLayout(LayoutKind.Sequential)]
  336.             internal struct WSAMsg
  337.             {
  338.                 internal IntPtr socketAddress;
  339.                 internal uint addressLength;
  340.                 internal IntPtr buffers;
  341.                 internal uint count;
  342.                 internal WSABuffer controlBuffer;
  343.                 internal SocketFlags flags;
  344.             }
  345.            
  346.            
  347.             // CharSet=Auto here since WSASocket has A and W versions. We can use Auto cause the method is not used under constrained execution region
  348.             [DllImport(WS2_32, CharSet = CharSet.Auto, SetLastError = true)]
  349.             static internal extern SafeCloseSocket.InnerSafeCloseSocket WSASocket(            [In()]
  350. AddressFamily addressFamily,             [In()]
  351. SocketType socketType,             [In()]
  352. ProtocolType protocolType,             [In()]
  353.             // will be WSAProtcolInfo protocolInfo once we include QOS APIs
  354. IntPtr protocolInfo,             [In()]
  355. uint group,             [In()]
  356. SocketConstructorFlags flags);
  357.            
  358.             [DllImport(WS2_32, CharSet = CharSet.Auto, SetLastError = true)]
  359.             unsafe static internal extern SafeCloseSocket.InnerSafeCloseSocket WSASocket(            [In()]
  360. AddressFamily addressFamily,             [In()]
  361. SocketType socketType,             [In()]
  362. ProtocolType protocolType,             [In()]
  363.             // will be WSAProtcolInfo protocolInfo once we include QOS APIs
  364. byte* pinnedBuffer,             [In()]
  365. uint group,             [In()]
  366. SocketConstructorFlags flags);
  367.            
  368.            
  369.             [DllImport(WS2_32, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)]
  370.             static internal extern SocketError WSAStartup(            [In()]
  371. short wVersionRequested,             [Out()]
  372. out WSAData lpWSAData);
  373.            
  374.             [DllImport(WS2_32, SetLastError = true)]
  375.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  376.             static internal extern SocketError ioctlsocket(            [In()]
  377. SafeCloseSocket socketHandle,             [In()]
  378. int cmd,             [In(), Out()]
  379. ref int argp);
  380.            
  381.             [DllImport(WS2_32, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)]
  382.             static internal extern IntPtr gethostbyname(            [In()]
  383. string host);
  384.            
  385.             [DllImport(WS2_32, SetLastError = true)]
  386.             static internal extern IntPtr gethostbyaddr(            [In()]
  387. ref int addr,             [In()]
  388. int len,             [In()]
  389. ProtocolFamily type);
  390.            
  391.             [DllImport(WS2_32, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)]
  392.             static internal extern SocketError gethostname(            [Out()]
  393. StringBuilder hostName,             [In()]
  394. int bufferLength);
  395.            
  396.             // this should belong to SafeNativeMethods, but it will not for simplicity
  397.             [DllImport(WS2_32, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)]
  398.             static internal extern int inet_addr(            [In()]
  399. string cp);
  400.            
  401.             [DllImport(WS2_32, SetLastError = true)]
  402.             static internal extern SocketError getpeername(            [In()]
  403. SafeCloseSocket socketHandle,             [Out()]
  404. byte[] socketAddress,             [In(), Out()]
  405. ref int socketAddressSize);
  406.            
  407.             [DllImport(WS2_32, SetLastError = true)]
  408.             static internal extern SocketError getsockopt(            [In()]
  409. SafeCloseSocket socketHandle,             [In()]
  410. SocketOptionLevel optionLevel,             [In()]
  411. SocketOptionName optionName,             [Out()]
  412. out int optionValue,             [In(), Out()]
  413. ref int optionLength);
  414.            
  415.             [DllImport(WS2_32, SetLastError = true)]
  416.             static internal extern SocketError getsockopt(            [In()]
  417. SafeCloseSocket socketHandle,             [In()]
  418. SocketOptionLevel optionLevel,             [In()]
  419. SocketOptionName optionName,             [Out()]
  420. byte[] optionValue,             [In(), Out()]
  421. ref int optionLength);
  422.            
  423.             [DllImport(WS2_32, SetLastError = true)]
  424.             static internal extern SocketError getsockopt(            [In()]
  425. SafeCloseSocket socketHandle,             [In()]
  426. SocketOptionLevel optionLevel,             [In()]
  427. SocketOptionName optionName,             [Out()]
  428. out Linger optionValue,             [In(), Out()]
  429. ref int optionLength);
  430.            
  431.             [DllImport(WS2_32, SetLastError = true)]
  432.             static internal extern SocketError getsockopt(            [In()]
  433. SafeCloseSocket socketHandle,             [In()]
  434. SocketOptionLevel optionLevel,             [In()]
  435. SocketOptionName optionName,             [Out()]
  436. out IPMulticastRequest optionValue,             [In(), Out()]
  437. ref int optionLength);
  438.            
  439.             //
  440.             // IPv6 Changes: need to receive and IPv6MulticastRequest from getsockopt
  441.             //
  442.             [DllImport(WS2_32, SetLastError = true)]
  443.             static internal extern SocketError getsockopt(            [In()]
  444. SafeCloseSocket socketHandle,             [In()]
  445. SocketOptionLevel optionLevel,             [In()]
  446. SocketOptionName optionName,             [Out()]
  447. out IPv6MulticastRequest optionValue,             [In(), Out()]
  448. ref int optionLength);
  449.            
  450.             [DllImport(WS2_32, SetLastError = true)]
  451.             static internal extern SocketError setsockopt(            [In()]
  452. SafeCloseSocket socketHandle,             [In()]
  453. SocketOptionLevel optionLevel,             [In()]
  454. SocketOptionName optionName,             [In()]
  455. ref int optionValue,             [In()]
  456. int optionLength);
  457.            
  458.             [DllImport(WS2_32, SetLastError = true)]
  459.             static internal extern SocketError setsockopt(            [In()]
  460. SafeCloseSocket socketHandle,             [In()]
  461. SocketOptionLevel optionLevel,             [In()]
  462. SocketOptionName optionName,             [In()]
  463. byte[] optionValue,             [In()]
  464. int optionLength);
  465.            
  466.             [DllImport(WS2_32, SetLastError = true)]
  467.             static internal extern SocketError setsockopt(            [In()]
  468. SafeCloseSocket socketHandle,             [In()]
  469. SocketOptionLevel optionLevel,             [In()]
  470. SocketOptionName optionName,             [In()]
  471. ref IntPtr pointer,             [In()]
  472. int optionLength);
  473.            
  474.             [DllImport(WS2_32, SetLastError = true)]
  475.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  476.             static internal extern SocketError setsockopt(            [In()]
  477. SafeCloseSocket socketHandle,             [In()]
  478. SocketOptionLevel optionLevel,             [In()]
  479. SocketOptionName optionName,             [In()]
  480. ref Linger linger,             [In()]
  481. int optionLength);
  482.            
  483.             [DllImport(WS2_32, SetLastError = true)]
  484.             static internal extern SocketError setsockopt(            [In()]
  485. SafeCloseSocket socketHandle,             [In()]
  486. SocketOptionLevel optionLevel,             [In()]
  487. SocketOptionName optionName,             [In()]
  488. ref IPMulticastRequest mreq,             [In()]
  489. int optionLength);
  490.            
  491.             //
  492.             // IPv6 Changes: need to pass an IPv6MulticastRequest to setsockopt
  493.             //
  494.             [DllImport(WS2_32, SetLastError = true)]
  495.             static internal extern SocketError setsockopt(            [In()]
  496. SafeCloseSocket socketHandle,             [In()]
  497. SocketOptionLevel optionLevel,             [In()]
  498. SocketOptionName optionName,             [In()]
  499. ref IPv6MulticastRequest mreq,             [In()]
  500. int optionLength);
  501.            
  502.            
  503.             // This method is always blocking, so it uses an IntPtr.
  504.             [DllImport(WS2_32, SetLastError = true)]
  505.             unsafe static internal extern int send(            [In()]
  506. IntPtr socketHandle,             [In()]
  507. byte* pinnedBuffer,             [In()]
  508. int len,             [In()]
  509. SocketFlags socketFlags);
  510.            
  511.             // This method is always blocking, so it uses an IntPtr.
  512.             [DllImport(WS2_32, SetLastError = true)]
  513.             unsafe static internal extern int recv(            [In()]
  514. IntPtr socketHandle,             [In()]
  515. byte* pinnedBuffer,             [In()]
  516. int len,             [In()]
  517. SocketFlags socketFlags);
  518.            
  519.             [DllImport(WS2_32, SetLastError = true)]
  520.             static internal extern SocketError listen(            [In()]
  521. SafeCloseSocket socketHandle,             [In()]
  522. int backlog);
  523.            
  524.             [DllImport(WS2_32, SetLastError = true)]
  525.             static internal extern SocketError bind(            [In()]
  526. SafeCloseSocket socketHandle,             [In()]
  527. byte[] socketAddress,             [In()]
  528. int socketAddressSize);
  529.            
  530.             [DllImport(WS2_32, SetLastError = true)]
  531.             static internal extern SocketError shutdown(            [In()]
  532. SafeCloseSocket socketHandle,             [In()]
  533. int how);
  534.            
  535.             // This method is always blocking, so it uses an IntPtr.
  536.             [DllImport(WS2_32, SetLastError = true)]
  537.             unsafe static internal extern int sendto(            [In()]
  538. IntPtr socketHandle,             [In()]
  539. byte* pinnedBuffer,             [In()]
  540. int len,             [In()]
  541. SocketFlags socketFlags,             [In()]
  542. byte[] socketAddress,             [In()]
  543. int socketAddressSize);
  544.            
  545.             // This method is always blocking, so it uses an IntPtr.
  546.             [DllImport(WS2_32, SetLastError = true)]
  547.             unsafe static internal extern int recvfrom(            [In()]
  548. IntPtr socketHandle,             [In()]
  549. byte* pinnedBuffer,             [In()]
  550. int len,             [In()]
  551. SocketFlags socketFlags,             [Out()]
  552. byte[] socketAddress,             [In(), Out()]
  553. ref int socketAddressSize);
  554.            
  555.             [DllImport(WS2_32, SetLastError = true)]
  556.             static internal extern SocketError getsockname(            [In()]
  557. SafeCloseSocket socketHandle,             [Out()]
  558. byte[] socketAddress,             [In(), Out()]
  559. ref int socketAddressSize);
  560.            
  561.             [DllImport(WS2_32, SetLastError = true)]
  562.             static internal extern int select(            [In()]
  563. int ignoredParameter,             [In(), Out()]
  564. IntPtr[] readfds,             [In(), Out()]
  565. IntPtr[] writefds,             [In(), Out()]
  566. IntPtr[] exceptfds,             [In()]
  567. ref TimeValue timeout);
  568.            
  569.             [DllImport(WS2_32, SetLastError = true)]
  570.             static internal extern int select(            [In()]
  571. int ignoredParameter,             [In(), Out()]
  572. IntPtr[] readfds,             [In(), Out()]
  573. IntPtr[] writefds,             [In(), Out()]
  574. IntPtr[] exceptfds,             [In()]
  575. IntPtr nullTimeout);
  576.            
  577.             // This function is always potentially blocking so it uses an IntPtr.
  578.             [DllImport(WS2_32, SetLastError = true)]
  579.             static internal extern SocketError WSAConnect(            [In()]
  580. IntPtr socketHandle,             [In()]
  581. byte[] socketAddress,             [In()]
  582. int socketAddressSize,             [In()]
  583. IntPtr inBuffer,             [In()]
  584. IntPtr outBuffer,             [In()]
  585. IntPtr sQOS,             [In()]
  586. IntPtr gQOS);
  587.            
  588.            
  589.             [DllImport(WS2_32, SetLastError = true)]
  590.             static internal extern SocketError WSASend(            [In()]
  591. SafeCloseSocket socketHandle,             [In()]
  592. ref WSABuffer buffer,             [In()]
  593. int bufferCount,             [Out()]
  594. out int bytesTransferred,             [In()]
  595. SocketFlags socketFlags,             [In()]
  596. IntPtr overlapped,             [In()]
  597. IntPtr completionRoutine);
  598.            
  599.             [DllImport(WS2_32, SetLastError = true)]
  600.             static internal extern SocketError WSASend(            [In()]
  601. SafeCloseSocket socketHandle,             [In()]
  602. WSABuffer[] buffersArray,             [In()]
  603. int bufferCount,             [Out()]
  604. out int bytesTransferred,             [In()]
  605. SocketFlags socketFlags,             [In()]
  606. IntPtr overlapped,             [In()]
  607. IntPtr completionRoutine);
  608.            
  609. /* Consider removing
  610.             [DllImport(WS2_32, SetLastError = true, EntryPoint = "WSASend")]
  611.             internal static extern SocketError WSASend_Blocking(
  612.                                               [In] IntPtr socketHandle,
  613.                                               [In] ref WSABuffer buffer,
  614.                                               [In] int bufferCount,
  615.                                               [Out] out int bytesTransferred,
  616.                                               [In] SocketFlags socketFlags,
  617.                                               [In] IntPtr overlapped,
  618.                                               [In] IntPtr completionRoutine
  619.                                               );
  620.             */           
  621.            
  622.             [DllImport(WS2_32, SetLastError = true, EntryPoint = "WSASend")]
  623.             static internal extern SocketError WSASend_Blocking(            [In()]
  624. IntPtr socketHandle,             [In()]
  625. WSABuffer[] buffersArray,             [In()]
  626. int bufferCount,             [Out()]
  627. out int bytesTransferred,             [In()]
  628. SocketFlags socketFlags,             [In()]
  629. IntPtr overlapped,             [In()]
  630. IntPtr completionRoutine);
  631.            
  632.             [DllImport(WS2_32, SetLastError = true)]
  633.             static internal extern SocketError WSASendTo(            [In()]
  634. SafeCloseSocket socketHandle,             [In()]
  635. ref WSABuffer buffer,             [In()]
  636. int bufferCount,             [Out()]
  637. out int bytesTransferred,             [In()]
  638. SocketFlags socketFlags,             [In()]
  639. IntPtr socketAddress,             [In()]
  640. int socketAddressSize,             [In()]
  641. IntPtr overlapped,             [In()]
  642. IntPtr completionRoutine);
  643.            
  644.             [DllImport(WS2_32, SetLastError = true)]
  645.             static internal extern SocketError WSARecv(            [In()]
  646. SafeCloseSocket socketHandle,             [In(), Out()]
  647. ref WSABuffer buffer,             [In()]
  648. int bufferCount,             [Out()]
  649. out int bytesTransferred,             [In(), Out()]
  650. ref SocketFlags socketFlags,             [In()]
  651. IntPtr overlapped,             [In()]
  652. IntPtr completionRoutine);
  653.            
  654.             [DllImport(WS2_32, SetLastError = true)]
  655.             static internal extern SocketError WSARecv(            [In()]
  656. SafeCloseSocket socketHandle,             [In(), Out()]
  657. WSABuffer[] buffers,             [In()]
  658. int bufferCount,             [Out()]
  659. out int bytesTransferred,             [In(), Out()]
  660. ref SocketFlags socketFlags,             [In()]
  661. IntPtr overlapped,             [In()]
  662. IntPtr completionRoutine);
  663.            
  664. /* Consider removing
  665.             [DllImport(WS2_32, SetLastError = true, EntryPoint = "WSARecv")]
  666.             internal static extern SocketError WSARecv_Blocking(
  667.                                               [In] IntPtr socketHandle,
  668.                                               [In, Out] ref WSABuffer buffer,
  669.                                               [In] int bufferCount,
  670.                                               [Out] out int bytesTransferred,
  671.                                               [In, Out] ref SocketFlags socketFlags,
  672.                                               [In] IntPtr overlapped,
  673.                                               [In] IntPtr completionRoutine
  674.                                               );
  675.             */           
  676.            
  677.             [DllImport(WS2_32, SetLastError = true, EntryPoint = "WSARecv")]
  678.             static internal extern SocketError WSARecv_Blocking(            [In()]
  679. IntPtr socketHandle,             [In(), Out()]
  680. WSABuffer[] buffers,             [In()]
  681. int bufferCount,             [Out()]
  682. out int bytesTransferred,             [In(), Out()]
  683. ref SocketFlags socketFlags,             [In()]
  684. IntPtr overlapped,             [In()]
  685. IntPtr completionRoutine);
  686.            
  687.             [DllImport(WS2_32, SetLastError = true)]
  688.             static internal extern SocketError WSARecvFrom(            [In()]
  689. SafeCloseSocket socketHandle,             [In(), Out()]
  690. ref WSABuffer buffer,             [In()]
  691. int bufferCount,             [Out()]
  692. out int bytesTransferred,             [In(), Out()]
  693. ref SocketFlags socketFlags,             [In()]
  694. IntPtr socketAddressPointer,             [In()]
  695. IntPtr socketAddressSizePointer,             [In()]
  696. IntPtr overlapped,             [In()]
  697. IntPtr completionRoutine);
  698.            
  699.            
  700.             [DllImport(WS2_32, SetLastError = true)]
  701.             static internal extern SocketError WSAEventSelect(            [In()]
  702. SafeCloseSocket socketHandle,             [In()]
  703. SafeHandle Event,             [In()]
  704. AsyncEventBits NetworkEvents);
  705.            
  706.             [DllImport(WS2_32, SetLastError = true)]
  707.             static internal extern SocketError WSAEventSelect(            [In()]
  708. SafeCloseSocket socketHandle,             [In()]
  709. IntPtr Event,             [In()]
  710. AsyncEventBits NetworkEvents);
  711.            
  712.            
  713.             // Used with SIOGETEXTENSIONFUNCTIONPOINTER - we're assuming that will never block.
  714.             [DllImport(WS2_32, SetLastError = true)]
  715.             static internal extern SocketError WSAIoctl(            [In()]
  716. SafeCloseSocket socketHandle,             [In()]
  717. int ioControlCode,             [In(), Out()]
  718. ref Guid guid,             [In()]
  719. int guidSize,             [Out()]
  720. out IntPtr funcPtr,             [In()]
  721. int funcPtrSize,             [Out()]
  722. out int bytesTransferred,             [In()]
  723. IntPtr shouldBeNull,             [In()]
  724. IntPtr shouldBeNull2);
  725.            
  726.             [DllImport(WS2_32, SetLastError = true, EntryPoint = "WSAIoctl")]
  727.             static internal extern SocketError WSAIoctl_Blocking(            [In()]
  728. IntPtr socketHandle,             [In()]
  729. int ioControlCode,             [In()]
  730. byte[] inBuffer,             [In()]
  731. int inBufferSize,             [Out()]
  732. byte[] outBuffer,             [In()]
  733. int outBufferSize,             [Out()]
  734. out int bytesTransferred,             [In()]
  735. IntPtr overlapped,             [In()]
  736. IntPtr completionRoutine);
  737.            
  738.             [DllImport(WS2_32, SetLastError = true)]
  739.             static internal extern SocketError WSAEnumNetworkEvents(            [In()]
  740. SafeCloseSocket socketHandle,             [In()]
  741. SafeWaitHandle Event,             [In(), Out()]
  742. ref NetworkEvents networkEvents);
  743.            
  744.            
  745.             [DllImport(WS2_32, SetLastError = true)]
  746.             static internal extern bool WSAGetOverlappedResult(            [In()]
  747. SafeCloseSocket socketHandle,             [In()]
  748. IntPtr overlapped,             [Out()]
  749. out uint bytesTransferred,             [In()]
  750. bool wait,             [In()]
  751. IntPtr ignored);
  752.            
  753.         }
  754.         // class UnsafeNclNativeMethods.OSSOCK
  755.        
  756.     }
  757. }

Developer Fusion