PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Native/System.Native/pal_networking.h

https://gitlab.com/0072016/0072016-corefx-
C Header | 448 lines | 289 code | 80 blank | 79 comment | 0 complexity | 7329b9f8bbe165f56b8aad008a7d2aad MD5 | raw file
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. #pragma once
  5. #include "pal_types.h"
  6. #include "pal_errno.h"
  7. /**
  8. * These error values are different on every platform so make a
  9. * platform-agnostic version that we convert to and send to managed
  10. */
  11. enum GetAddrInfoErrorFlags
  12. {
  13. PAL_EAI_SUCCESS = 0, // Success
  14. PAL_EAI_AGAIN = 1, // Temporary failure in name resolution.
  15. PAL_EAI_BADFLAGS = 2, // Invalid value for `ai_flags' field.
  16. PAL_EAI_FAIL = 3, // Non-recoverable failure in name resolution.
  17. PAL_EAI_FAMILY = 4, // 'ai_family' not supported.
  18. PAL_EAI_NONAME = 5, // NAME or SERVICE is unknown.
  19. PAL_EAI_BADARG = 6, // One or more input arguments were invalid.
  20. PAL_EAI_NOMORE = 7, // No more entries are present in the list.
  21. };
  22. /**
  23. * Flags to pass to GetNameInfo. These do not match
  24. * from platform to platform and must be converted
  25. */
  26. enum GetNameInfoFlags
  27. {
  28. PAL_NI_NAMEREQD = 0x1,
  29. PAL_NI_NUMERICHOST = 0x2,
  30. };
  31. /**
  32. * Error codes from GetHostByName and GetHostByAddr
  33. */
  34. enum GetHostErrorCodes
  35. {
  36. PAL_HOST_NOT_FOUND = 1,
  37. PAL_TRY_AGAIN = 2,
  38. PAL_NO_RECOVERY = 3,
  39. PAL_NO_DATA = 4,
  40. PAL_NO_ADDRESS = PAL_NO_DATA,
  41. PAL_BAD_ARG = 5,
  42. PAL_NO_MEM = 6,
  43. };
  44. /**
  45. * Address families recognized by {Get,Set}AddressFamily.
  46. *
  47. * NOTE: these values are taken from System.Net.AddressFamily. If you add
  48. * new entries, be sure that the values are chosen accordingly.
  49. */
  50. enum AddressFamily : int32_t
  51. {
  52. PAL_AF_UNSPEC = 0, // System.Net.AddressFamily.Unspecified
  53. PAL_AF_UNIX = 1, // System.Net.AddressFamily.Unix
  54. PAL_AF_INET = 2, // System.Net.AddressFamily.InterNetwork
  55. PAL_AF_INET6 = 23, // System.Net.AddressFamily.InterNetworkV6
  56. };
  57. /*
  58. * Socket types.
  59. *
  60. * NOTE: these values are taken from System.Net.SocketType.
  61. */
  62. enum SocketType : int32_t
  63. {
  64. PAL_SOCK_STREAM = 1, // System.Net.SocketType.Stream
  65. PAL_SOCK_DGRAM = 2, // System.Net.SocketType.Dgram
  66. PAL_SOCK_RAW = 3, // System.Net.SocketType.Raw
  67. PAL_SOCK_RDM = 4, // System.Net.SocketType.Rdm
  68. PAL_SOCK_SEQPACKET = 5, // System.Net.SocketType.SeqPacket
  69. };
  70. /*
  71. * Protocol types.
  72. *
  73. * NOTE: these values are taken from System.Net.ProtocolType.
  74. */
  75. enum ProtocolType : int32_t
  76. {
  77. PAL_PT_UNSPECIFIED = 0, // System.Net.ProtocolType.Unspecified
  78. PAL_PT_ICMP = 1, // System.Net.ProtocolType.Icmp
  79. PAL_PT_TCP = 6, // System.Net.ProtocolType.Tcp
  80. PAL_PT_UDP = 17, // System.Net.ProtocolType.Udp
  81. PAL_PT_ICMPV6 = 58, // System.Net.ProtocolType.IcmpV6
  82. };
  83. enum MulticastOption : int32_t
  84. {
  85. PAL_MULTICAST_ADD = 0, // IP{,V6}_ADD_MEMBERSHIP
  86. PAL_MULTICAST_DROP = 1, // IP{,V6}_DROP_MEMBERSHIP
  87. PAL_MULTICAST_IF = 2 // IP_MULTICAST_IF
  88. };
  89. /*
  90. * Socket shutdown modes.
  91. *
  92. * NOTE: these values are taken from System.Net.SocketShutdown.
  93. */
  94. enum SocketShutdown : int32_t
  95. {
  96. PAL_SHUT_READ = 0, // SHUT_RD
  97. PAL_SHUT_WRITE = 1, // SHUT_WR
  98. PAL_SHUT_BOTH = 2, // SHUT_RDWR
  99. };
  100. /*
  101. * Socket option levels.
  102. *
  103. * NOTE: these values are taken from System.Net.SocketOptionLevel.
  104. */
  105. enum SocketOptionLevel : int32_t
  106. {
  107. PAL_SOL_SOCKET = 0xffff,
  108. PAL_SOL_IP = 0,
  109. PAL_SOL_IPV6 = 41,
  110. PAL_SOL_TCP = 6,
  111. PAL_SOL_UDP = 17,
  112. };
  113. /*
  114. * Socket option names.
  115. *
  116. * NOTE: these values are taken from System.Net.SocketOptionName. Only values that are known to be usable on all target
  117. * platforms are represented here. Unsupported values are present as commented-out entries.
  118. */
  119. enum SocketOptionName : int32_t
  120. {
  121. // Names for level PAL_SOL_SOCKET
  122. PAL_SO_DEBUG = 0x0001,
  123. PAL_SO_ACCEPTCONN = 0x0002,
  124. PAL_SO_REUSEADDR = 0x0004,
  125. PAL_SO_KEEPALIVE = 0x0008,
  126. PAL_SO_DONTROUTE = 0x0010,
  127. PAL_SO_BROADCAST = 0x0020,
  128. // PAL_SO_USELOOPBACK = 0x0040,
  129. PAL_SO_LINGER = 0x0080,
  130. PAL_SO_OOBINLINE = 0x0100,
  131. // PAL_SO_DONTLINGER = ~PAL_SO_LINGER,
  132. // PAL_SO_EXCLUSIVEADDRUSE = ~PAL_SO_REUSEADDR,
  133. PAL_SO_SNDBUF = 0x1001,
  134. PAL_SO_RCVBUF = 0x1002,
  135. PAL_SO_SNDLOWAT = 0x1003,
  136. PAL_SO_RCVLOWAT = 0x1004,
  137. PAL_SO_SNDTIMEO = 0x1005,
  138. PAL_SO_RCVTIMEO = 0x1006,
  139. PAL_SO_ERROR = 0x1007,
  140. PAL_SO_TYPE = 0x1008,
  141. // PAL_SO_MAXCONN = 0x7fffffff,
  142. // Names for level PAL_SOL_IP
  143. PAL_SO_IP_OPTIONS = 1,
  144. PAL_SO_IP_HDRINCL = 2,
  145. PAL_SO_IP_TOS = 3,
  146. PAL_SO_IP_TTL = 4,
  147. PAL_SO_IP_MULTICAST_IF = 9,
  148. PAL_SO_IP_MULTICAST_TTL = 10,
  149. PAL_SO_IP_MULTICAST_LOOP = 11,
  150. PAL_SO_IP_ADD_MEMBERSHIP = 12,
  151. PAL_SO_IP_DROP_MEMBERSHIP = 13,
  152. // PAL_SO_IP_DONTFRAGMENT = 14,
  153. PAL_SO_IP_ADD_SOURCE_MEMBERSHIP = 15,
  154. PAL_SO_IP_DROP_SOURCE_MEMBERSHIP = 16,
  155. PAL_SO_IP_BLOCK_SOURCE = 17,
  156. PAL_SO_IP_UNBLOCK_SOURCE = 18,
  157. PAL_SO_IP_PKTINFO = 19,
  158. // Names for PAL_SOL_IPV6
  159. PAL_SO_IPV6_HOPLIMIT = 21,
  160. // PAL_SO_IPV6_PROTECTION_LEVEL = 23,
  161. PAL_SO_IPV6_V6ONLY = 27,
  162. // Names for PAL_SOL_TCP
  163. PAL_SO_TCP_NODELAY = 1,
  164. // PAL_SO_TCP_BSDURGENT = 2,
  165. // Names for PAL_SOL_UDP
  166. // PAL_SO_UDP_NOCHECKSUM = 1,
  167. // PAL_SO_UDP_CHECKSUM_COVERAGE = 20,
  168. // PAL_SO_UDP_UPDATEACCEPTCONTEXT = 0x700b,
  169. // PAL_SO_UDP_UPDATECONNECTCONTEXT = 0x7010,
  170. };
  171. /*
  172. * Socket flags.
  173. *
  174. * NOTE: these values are taken from System.Net.SocketFlags. Only values that are known to be usable on all target
  175. * platforms are represented here. Unsupported values are present as commented-out entries.
  176. */
  177. enum SocketFlags : int32_t
  178. {
  179. PAL_MSG_OOB = 0x0001, // SocketFlags.OutOfBand
  180. PAL_MSG_PEEK = 0x0002, // SocketFlags.Peek
  181. PAL_MSG_DONTROUTE = 0x0004, // SocketFlags.DontRoute
  182. PAL_MSG_TRUNC = 0x0100, // SocketFlags.Truncated
  183. PAL_MSG_CTRUNC = 0x0200, // SocketFlags.ControlDataTruncated
  184. };
  185. /*
  186. * Socket async events.
  187. */
  188. enum SocketEvents : int32_t
  189. {
  190. PAL_SA_NONE = 0x00,
  191. PAL_SA_READ = 0x01,
  192. PAL_SA_WRITE = 0x02,
  193. PAL_SA_READCLOSE = 0x04,
  194. PAL_SA_CLOSE = 0x08,
  195. PAL_SA_ERROR = 0x10,
  196. };
  197. /**
  198. * IP address sizes.
  199. */
  200. enum
  201. {
  202. NUM_BYTES_IN_IPV4_ADDRESS = 4,
  203. NUM_BYTES_IN_IPV6_ADDRESS = 16,
  204. MAX_IP_ADDRESS_BYTES = 16,
  205. };
  206. struct IPAddress
  207. {
  208. uint8_t Address[MAX_IP_ADDRESS_BYTES]; // Buffer to fit IPv4 or IPv6 address
  209. uint32_t IsIPv6; // Non-zero if this is an IPv6 endpoint; zero for IPv4.
  210. uint32_t ScopeId; // Scope ID (IPv6 only)
  211. };
  212. struct HostEntry
  213. {
  214. uint8_t* CanonicalName; // Canonical name of the host
  215. uint8_t** Aliases; // List of aliases for the host
  216. void* AddressListHandle; // Handle for host socket addresses
  217. int32_t IPAddressCount; // Number of IP end points in the list
  218. int32_t HandleType; // Indicates the type of the handle. Opaque to managed code.
  219. };
  220. struct IPPacketInformation
  221. {
  222. IPAddress Address; // Destination IP address
  223. int32_t InterfaceIndex; // Interface index
  224. int32_t Padding; // Pad out to 8-byte alignment
  225. };
  226. struct IPv4MulticastOption
  227. {
  228. uint32_t MulticastAddress; // Multicast address
  229. uint32_t LocalAddress; // Local address
  230. int32_t InterfaceIndex; // Interface index
  231. int32_t Padding; // Pad out to 8-byte alignment
  232. };
  233. struct IPv6MulticastOption
  234. {
  235. IPAddress Address; // Multicast address
  236. int32_t InterfaceIndex; // Interface index
  237. int32_t Padding; // Pad out to 8-byte alignment
  238. };
  239. struct LingerOption
  240. {
  241. int32_t OnOff; // Non-zero to enable linger
  242. int32_t Seconds; // Number of seconds to linger for
  243. };
  244. // NOTE: the layout of this type is intended to exactly match the layout of a `struct iovec`. There are
  245. // assertions in pal_networking.cpp that validate this.
  246. struct IOVector
  247. {
  248. uint8_t* Base;
  249. uintptr_t Count;
  250. };
  251. struct MessageHeader
  252. {
  253. uint8_t* SocketAddress;
  254. IOVector* IOVectors;
  255. uint8_t* ControlBuffer;
  256. int32_t SocketAddressLen;
  257. int32_t IOVectorCount;
  258. int32_t ControlBufferLen;
  259. int32_t Flags;
  260. };
  261. struct SocketEvent
  262. {
  263. uintptr_t Data; // User data for this event
  264. SocketEvents Events; // Event flags
  265. uint32_t Padding; // Pad out to 8-byte alignment
  266. };
  267. /**
  268. * Converts string-representations of IP Addresses to
  269. */
  270. extern "C" int32_t SystemNative_IPv6StringToAddress(
  271. const uint8_t* address, const uint8_t* port, uint8_t* buffer, int32_t bufferLength, uint32_t* scope);
  272. extern "C" int32_t SystemNative_IPv4StringToAddress(const uint8_t* address, uint8_t* buffer, int32_t bufferLength, uint16_t* port);
  273. extern "C" int32_t SystemNative_IPAddressToString(const uint8_t* address,
  274. int32_t addressLength,
  275. bool isIPv6,
  276. uint8_t* string,
  277. int32_t stringLength,
  278. uint32_t scope = 0);
  279. extern "C" int32_t SystemNative_GetHostEntryForName(const uint8_t* address, HostEntry* entry);
  280. extern "C" int32_t SystemNative_GetHostByName(const uint8_t* hostname, HostEntry* entry);
  281. extern "C" int32_t SystemNative_GetHostByAddress(const IPAddress* address, HostEntry* entry);
  282. extern "C" int32_t SystemNative_GetNextIPAddress(const HostEntry* entry, void** addressListHandle, IPAddress* endPoint);
  283. extern "C" void SystemNative_FreeHostEntry(HostEntry* entry);
  284. extern "C" int32_t SystemNative_GetNameInfo(const uint8_t* address,
  285. int32_t addressLength,
  286. bool isIPv6,
  287. uint8_t* host,
  288. int32_t hostLength,
  289. uint8_t* service,
  290. int32_t serviceLength,
  291. int32_t flags);
  292. extern "C" int32_t SystemNative_GetDomainName(uint8_t* name, int32_t nameLength);
  293. extern "C" int32_t SystemNative_GetHostName(uint8_t* name, int32_t nameLength);
  294. extern "C" Error SystemNative_GetIPSocketAddressSizes(int32_t* ipv4SocketAddressSize, int32_t* ipv6SocketAddressSize);
  295. extern "C" Error SystemNative_GetAddressFamily(const uint8_t* socketAddress, int32_t socketAddressLen, int32_t* addressFamily);
  296. extern "C" Error SystemNative_SetAddressFamily(uint8_t* socketAddress, int32_t socketAddressLen, int32_t addressFamily);
  297. extern "C" Error SystemNative_GetPort(const uint8_t* socketAddress, int32_t socketAddressLen, uint16_t* port);
  298. extern "C" Error SystemNative_SetPort(uint8_t* socketAddress, int32_t socketAddressLen, uint16_t port);
  299. extern "C" Error SystemNative_GetIPv4Address(const uint8_t* socketAddress, int32_t socketAddressLen, uint32_t* address);
  300. extern "C" Error SystemNative_SetIPv4Address(uint8_t* socketAddress, int32_t socketAddressLen, uint32_t address);
  301. extern "C" Error SystemNative_GetIPv6Address(
  302. const uint8_t* socketAddress, int32_t socketAddressLen, uint8_t* address, int32_t addressLen, uint32_t* scopeId);
  303. extern "C" int32_t SystemNative_GetControlMessageBufferSize(int32_t isIPv4, int32_t isIPv6);
  304. extern "C" int32_t
  305. SystemNative_TryGetIPPacketInformation(MessageHeader* messageHeader, int32_t isIPv4, IPPacketInformation* packetInfo);
  306. extern "C" Error SystemNative_GetIPv4MulticastOption(intptr_t socket, int32_t multicastOption, IPv4MulticastOption* option);
  307. extern "C" Error SystemNative_GetIPv4MulticastOption_IntPtr(intptr_t socket, int32_t multicastOption, IPv4MulticastOption* option);
  308. extern "C" Error SystemNative_SetIPv4MulticastOption(intptr_t socket, int32_t multicastOption, IPv4MulticastOption* option);
  309. extern "C" Error SystemNative_SetIPv4MulticastOption_IntPtr(intptr_t socket, int32_t multicastOption, IPv4MulticastOption* option);
  310. extern "C" Error SystemNative_GetIPv6MulticastOption(intptr_t socket, int32_t multicastOption, IPv6MulticastOption* option);
  311. extern "C" Error SystemNative_GetIPv6MulticastOption_IntPtr(intptr_t socket, int32_t multicastOption, IPv6MulticastOption* option);
  312. extern "C" Error SystemNative_SetIPv6MulticastOption(intptr_t socket, int32_t multicastOption, IPv6MulticastOption* option);
  313. extern "C" Error SystemNative_SetIPv6MulticastOption_IntPtr(intptr_t socket, int32_t multicastOption, IPv6MulticastOption* option);
  314. extern "C" Error SystemNative_GetLingerOption(intptr_t socket, LingerOption* option);
  315. extern "C" Error SystemNative_GetLingerOption_IntPtr(intptr_t socket, LingerOption* option);
  316. extern "C" Error SystemNative_SetLingerOption(intptr_t socket, LingerOption* option);
  317. extern "C" Error SystemNative_SetLingerOption_IntPtr(intptr_t socket, LingerOption* option);
  318. extern "C" Error SystemNative_SetReceiveTimeout(intptr_t socket, int32_t millisecondsTimeout);
  319. extern "C" Error SystemNative_SetReceiveTimeout_IntPtr(intptr_t socket, int32_t millisecondsTimeout);
  320. extern "C" Error SystemNative_SetSendTimeout(intptr_t socket, int32_t millisecondsTimeout);
  321. extern "C" Error SystemNative_SetSendTimeout_IntPtr(intptr_t socket, int32_t millisecondsTimeout);
  322. extern "C" Error SystemNative_ReceiveMessage(intptr_t socket, MessageHeader* messageHeader, int32_t flags, int64_t* received);
  323. extern "C" Error SystemNative_ReceiveMessage_IntPtr(intptr_t socket, MessageHeader* messageHeader, int32_t flags, int64_t* received);
  324. extern "C" Error SystemNative_SendMessage(intptr_t socket, MessageHeader* messageHeader, int32_t flags, int64_t* sent);
  325. extern "C" Error SystemNative_SendMessage_IntPtr(intptr_t socket, MessageHeader* messageHeader, int32_t flags, int64_t* sent);
  326. extern "C" Error SystemNative_Accept(intptr_t socket, uint8_t* socketAddress, int32_t* socketAddressLen, intptr_t* acceptedSocket);
  327. extern "C" Error SystemNative_Accept_IntPtr(intptr_t socket, uint8_t* socketAddress, int32_t* socketAddressLen, intptr_t* acceptedSocket);
  328. extern "C" Error SystemNative_Bind(intptr_t socket, uint8_t* socketAddress, int32_t socketAddressLen);
  329. extern "C" Error SystemNative_Bind_IntPtr(intptr_t socket, uint8_t* socketAddress, int32_t socketAddressLen);
  330. extern "C" Error SystemNative_Connect(intptr_t socket, uint8_t* socketAddress, int32_t socketAddressLen);
  331. extern "C" Error SystemNative_Connect_IntPtr(intptr_t socket, uint8_t* socketAddress, int32_t socketAddressLen);
  332. extern "C" Error SystemNative_GetPeerName(intptr_t socket, uint8_t* socketAddress, int32_t* socketAddressLen);
  333. extern "C" Error SystemNative_GetPeerName_IntPtr(intptr_t socket, uint8_t* socketAddress, int32_t* socketAddressLen);
  334. extern "C" Error SystemNative_GetSockName(intptr_t socket, uint8_t* socketAddress, int32_t* socketAddressLen);
  335. extern "C" Error SystemNative_GetSockName_IntPtr(intptr_t socket, uint8_t* socketAddress, int32_t* socketAddressLen);
  336. extern "C" Error SystemNative_Listen(intptr_t socket, int32_t backlog);
  337. extern "C" Error SystemNative_Listen_IntPtr(intptr_t socket, int32_t backlog);
  338. extern "C" Error SystemNative_Shutdown(intptr_t socket, int32_t socketShutdown);
  339. extern "C" Error SystemNative_Shutdown_IntPtr(intptr_t socket, int32_t socketShutdown);
  340. extern "C" Error SystemNative_GetSocketErrorOption(intptr_t socket, Error* error);
  341. extern "C" Error SystemNative_GetSocketErrorOption_IntPtr(intptr_t socket, Error* error);
  342. extern "C" Error SystemNative_GetSockOpt(
  343. intptr_t socket, int32_t socketOptionLevel, int32_t socketOptionName, uint8_t* optionValue, int32_t* optionLen);
  344. extern "C" Error SystemNative_GetSockOpt_IntPtr(
  345. intptr_t socket, int32_t socketOptionLevel, int32_t socketOptionName, uint8_t* optionValue, int32_t* optionLen);
  346. extern "C" Error SystemNative_SetSockOpt(
  347. intptr_t socket, int32_t socketOptionLevel, int32_t socketOptionName, uint8_t* optionValue, int32_t optionLen);
  348. extern "C" Error SystemNative_SetSockOpt_IntPtr(
  349. intptr_t socket, int32_t socketOptionLevel, int32_t socketOptionName, uint8_t* optionValue, int32_t optionLen);
  350. extern "C" Error SystemNative_Socket(int32_t addressFamily, int32_t socketType, int32_t protocolType, intptr_t* createdSocket);
  351. extern "C" Error SystemNative_Socket_IntPtr(int32_t addressFamily, int32_t socketType, int32_t protocolType, intptr_t* createdSocket);
  352. extern "C" Error SystemNative_GetBytesAvailable(intptr_t socket, int32_t* available);
  353. extern "C" Error SystemNative_GetBytesAvailable_IntPtr(intptr_t socket, int32_t* available);
  354. extern "C" Error SystemNative_CreateSocketEventPort(intptr_t* port);
  355. extern "C" Error SystemNative_CreateSocketEventPort_IntPtr(intptr_t* port);
  356. extern "C" Error SystemNative_CloseSocketEventPort(intptr_t port);
  357. extern "C" Error SystemNative_CloseSocketEventPort_IntPtr(intptr_t port);
  358. extern "C" Error SystemNative_CreateSocketEventBuffer(int32_t count, SocketEvent** buffer);
  359. extern "C" Error SystemNative_FreeSocketEventBuffer(SocketEvent* buffer);
  360. extern "C" Error SystemNative_TryChangeSocketEventRegistration(
  361. intptr_t port, intptr_t socket, int32_t currentEvents, int32_t newEvents, uintptr_t data);
  362. extern "C" Error SystemNative_TryChangeSocketEventRegistration_IntPtr(
  363. intptr_t port, intptr_t socket, int32_t currentEvents, int32_t newEvents, uintptr_t data);
  364. extern "C" Error SystemNative_WaitForSocketEvents(intptr_t port, SocketEvent* buffer, int32_t* count);
  365. extern "C" Error SystemNative_WaitForSocketEvents_IntPtr(intptr_t port, SocketEvent* buffer, int32_t* count);
  366. extern "C" int32_t SystemNative_PlatformSupportsDualModeIPv4PacketInfo();
  367. extern "C" char* SystemNative_GetPeerUserName(intptr_t socket);
  368. extern "C" void SystemNative_GetDomainSocketSizes(int32_t* pathOffset, int32_t* pathSize, int32_t* addressSize);