Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Interop.cs
Go to the documentation of this file.
1using System;
2using System.Net;
7
8internal static class Interop
9{
10 internal static class IpHlpApi
11 {
12 public struct FIXED_INFO
13 {
14 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 132)]
15 public string hostName;
16
17 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 132)]
18 public string domainName;
19
21
23
24 public uint nodeType;
25
26 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
27 public string scopeId;
28
29 public bool enableRouting;
30
31 public bool enableProxy;
32
33 public bool enableDns;
34 }
35
36 public struct IP_ADDR_STRING
37 {
38 public IntPtr Next;
39
40 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
41 public string IpAddress;
42
43 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
44 public string IpMask;
45
46 public uint Context;
47 }
48
49 [Flags]
50 internal enum AdapterFlags
51 {
52 DnsEnabled = 1,
54 DhcpEnabled = 4,
55 ReceiveOnly = 8,
56 NoMulticast = 0x10,
58 NetBiosOverTcp = 0x40,
59 IPv4Enabled = 0x80,
60 IPv6Enabled = 0x100,
62 }
63
64 [Flags]
65 internal enum AdapterAddressFlags
66 {
67 DnsEligible = 1,
68 Transient = 2
69 }
70
71 [Flags]
73 {
74 SkipUnicast = 1,
75 SkipAnycast = 2,
76 SkipMulticast = 4,
77 SkipDnsServer = 8,
78 IncludePrefix = 0x10,
79 SkipFriendlyName = 0x20,
80 IncludeWins = 0x40,
81 IncludeGateways = 0x80,
85 }
86
87 internal struct IpSocketAddress
88 {
89 internal IntPtr address;
90
91 internal int addressLength;
92
94 {
97 Marshal.Copy(address, socketAddress.Buffer, 0, addressLength);
98 return socketAddress.GetIPAddress();
99 }
100 }
101
102 internal struct IpAdapterAddress
103 {
104 internal uint length;
105
107
108 internal IntPtr next;
109
111
113 {
114 InternalIPAddressCollection internalIPAddressCollection = new InternalIPAddressCollection();
115 while (ptr != IntPtr.Zero)
116 {
118 IPAddress iPAddress = ipAdapterAddress.address.MarshalIPAddress();
119 internalIPAddressCollection.InternalAdd(iPAddress);
120 ptr = ipAdapterAddress.next;
121 }
122 return internalIPAddressCollection;
123 }
124
126 {
127 IPAddressInformationCollection iPAddressInformationCollection = new IPAddressInformationCollection();
128 while (ptr != IntPtr.Zero)
129 {
131 IPAddress iPAddress = ipAdapterAddress.address.MarshalIPAddress();
132 iPAddressInformationCollection.InternalAdd(new SystemIPAddressInformation(iPAddress, ipAdapterAddress.flags));
133 ptr = ipAdapterAddress.next;
134 }
135 return iPAddressInformationCollection;
136 }
137 }
138
140 {
141 internal uint length;
142
144
145 internal IntPtr next;
146
148
150
152
154
155 internal uint validLifetime;
156
157 internal uint preferredLifetime;
158
159 internal uint leaseLifetime;
160
161 internal byte prefixLength;
162 }
163
164 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
165 internal struct IpAdapterAddresses
166 {
167 internal uint length;
168
169 internal uint index;
170
171 internal IntPtr next;
172
173 [MarshalAs(UnmanagedType.LPStr)]
174 internal string AdapterName;
175
177
179
181
183
184 internal string dnsSuffix;
185
186 internal string description;
187
188 internal string friendlyName;
189
190 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
191 internal byte[] address;
192
193 internal uint addressLength;
194
196
197 internal uint mtu;
198
200
202
203 internal uint ipv6Index;
204
205 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
206 internal uint[] zoneIndices;
207
209
210 internal ulong transmitLinkSpeed;
211
212 internal ulong receiveLinkSpeed;
213
215
217
218 internal uint ipv4Metric;
219
220 internal uint ipv6Metric;
221
222 internal ulong luid;
223
225
226 internal uint compartmentId;
227
228 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
229 internal byte[] networkGuid;
230
232
234
236
237 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 130)]
238 internal byte[] dhcpv6ClientDuid;
239
241
242 internal uint dhcpV6Iaid;
243 }
244
246 {
247 Dedicated = 1,
248 Passive,
249 Demand,
250 Maximum
251 }
252
254 {
255 None = 0,
256 Other = 1,
257 Direct = 2,
258 SixToFour = 11,
259 Isatap = 13,
260 Teredo = 14,
261 IpHttps = 15
262 }
263
264 internal struct IpPerAdapterInfo
265 {
266 internal bool autoconfigEnabled;
267
268 internal bool autoconfigActive;
269
271
273 }
274
275 internal struct IpAddrString
276 {
277 internal IntPtr Next;
278
279 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
280 internal string IpAddress;
281
282 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
283 internal string IpMask;
284
285 internal uint Context;
286 }
287
288 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
289 internal struct MibIfRow2
290 {
291 internal ulong interfaceLuid;
292
293 internal uint interfaceIndex;
294
295 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
296 internal byte[] interfaceGuid;
297
298 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 257)]
299 internal char[] alias;
300
301 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 257)]
302 internal char[] description;
303
305
306 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
307 internal byte[] physicalAddress;
308
309 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
311
312 internal uint mtu;
313
315
317
318 internal uint mediaType;
319
320 internal uint physicalMediumType;
321
322 internal uint accessType;
323
324 internal uint directionType;
325
327
329
330 internal uint adminStatus;
331
332 internal uint mediaConnectState;
333
334 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
335 internal byte[] networkGuid;
336
338
339 internal ulong transmitLinkSpeed;
340
341 internal ulong receiveLinkSpeed;
342
343 internal ulong inOctets;
344
345 internal ulong inUcastPkts;
346
347 internal ulong inNUcastPkts;
348
349 internal ulong inDiscards;
350
351 internal ulong inErrors;
352
353 internal ulong inUnknownProtos;
354
355 internal ulong inUcastOctets;
356
357 internal ulong inMulticastOctets;
358
359 internal ulong inBroadcastOctets;
360
361 internal ulong outOctets;
362
363 internal ulong outUcastPkts;
364
365 internal ulong outNUcastPkts;
366
367 internal ulong outDiscards;
368
369 internal ulong outErrors;
370
371 internal ulong outUcastOctets;
372
373 internal ulong outMulticastOctets;
374
375 internal ulong outBroadcastOctets;
376
377 internal ulong outQLen;
378 }
379
380 internal struct MibUdpStats
381 {
382 internal uint datagramsReceived;
383
385
387
388 internal uint datagramsSent;
389
390 internal uint udpListeners;
391 }
392
393 internal struct MibTcpStats
394 {
396
398
400
401 internal uint maximumConnections;
402
403 internal uint activeOpens;
404
405 internal uint passiveOpens;
406
408
409 internal uint resetConnections;
410
411 internal uint currentConnections;
412
413 internal uint segmentsReceived;
414
415 internal uint segmentsSent;
416
417 internal uint segmentsResent;
418
419 internal uint errorsReceived;
420
422
424 }
425
426 internal struct MibIpStats
427 {
428 internal bool forwardingEnabled;
429
430 internal uint defaultTtl;
431
432 internal uint packetsReceived;
433
435
437
438 internal uint packetsForwarded;
439
441
443
445
446 internal uint packetOutputRequests;
447
449
451
453
455
457
458 internal uint packetsReassembled;
459
461
462 internal uint packetsFragmented;
463
465
467
468 internal uint interfaces;
469
470 internal uint ipAddresses;
471
472 internal uint routes;
473 }
474
475 internal struct MibIcmpInfo
476 {
478
480 }
481
482 internal struct MibIcmpStats
483 {
484 internal uint messages;
485
486 internal uint errors;
487
489
490 internal uint timeExceeds;
491
492 internal uint parameterProblems;
493
494 internal uint sourceQuenches;
495
496 internal uint redirects;
497
498 internal uint echoRequests;
499
500 internal uint echoReplies;
501
502 internal uint timestampRequests;
503
504 internal uint timestampReplies;
505
506 internal uint addressMaskRequests;
507
508 internal uint addressMaskReplies;
509 }
510
511 internal struct MibIcmpInfoEx
512 {
514
516 }
517
518 internal struct MibIcmpStatsEx
519 {
520 internal uint dwMsgs;
521
522 internal uint dwErrors;
523
524 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
525 internal uint[] rgdwTypeCount;
526 }
527
528 internal struct MibTcpTable
529 {
530 internal uint numberOfEntries;
531 }
532
533 internal struct MibTcpRow
534 {
535 internal TcpState state;
536
537 internal uint localAddr;
538
539 internal byte localPort1;
540
541 internal byte localPort2;
542
543 internal byte ignoreLocalPort3;
544
545 internal byte ignoreLocalPort4;
546
547 internal uint remoteAddr;
548
549 internal byte remotePort1;
550
551 internal byte remotePort2;
552
553 internal byte ignoreRemotePort3;
554
555 internal byte ignoreRemotePort4;
556 }
557
558 internal struct MibTcp6TableOwnerPid
559 {
560 internal uint numberOfEntries;
561 }
562
563 internal struct MibTcp6RowOwnerPid
564 {
565 internal unsafe fixed byte localAddr[16];
566
567 internal uint localScopeId;
568
569 internal byte localPort1;
570
571 internal byte localPort2;
572
573 internal byte ignoreLocalPort3;
574
575 internal byte ignoreLocalPort4;
576
577 internal unsafe fixed byte remoteAddr[16];
578
579 internal uint remoteScopeId;
580
581 internal byte remotePort1;
582
583 internal byte remotePort2;
584
585 internal byte ignoreRemotePort3;
586
587 internal byte ignoreRemotePort4;
588
589 internal TcpState state;
590
591 internal uint owningPid;
592
593 internal unsafe ReadOnlySpan<byte> localAddrAsSpan => MemoryMarshal.CreateSpan(ref localAddr[0], 16);
594
595 internal unsafe ReadOnlySpan<byte> remoteAddrAsSpan => MemoryMarshal.CreateSpan(ref remoteAddr[0], 16);
596 }
597
610
611 internal struct MibUdpTable
612 {
613 internal uint numberOfEntries;
614 }
615
616 internal struct MibUdpRow
617 {
618 internal uint localAddr;
619
620 internal byte localPort1;
621
622 internal byte localPort2;
623
624 internal byte ignoreLocalPort3;
625
626 internal byte ignoreLocalPort4;
627 }
628
629 internal enum UdpTableClass
630 {
634 }
635
636 internal struct MibUdp6TableOwnerPid
637 {
638 internal uint numberOfEntries;
639 }
640
641 internal struct MibUdp6RowOwnerPid
642 {
643 internal unsafe fixed byte localAddr[16];
644
645 internal uint localScopeId;
646
647 internal byte localPort1;
648
649 internal byte localPort2;
650
651 internal byte ignoreLocalPort3;
652
653 internal byte ignoreLocalPort4;
654
655 internal uint owningPid;
656
657 internal unsafe ReadOnlySpan<byte> localAddrAsSpan => MemoryMarshal.CreateSpan(ref localAddr[0], 16);
658 }
659
660 [DllImport("iphlpapi.dll", ExactSpelling = true)]
661 internal static extern uint GetNetworkParams(IntPtr pFixedInfo, ref uint pOutBufLen);
662
663 [DllImport("iphlpapi.dll")]
664 internal static extern uint GetAdaptersAddresses(AddressFamily family, uint flags, IntPtr pReserved, IntPtr adapterAddresses, ref uint outBufLen);
665
666 [DllImport("iphlpapi.dll")]
667 internal static extern uint GetBestInterfaceEx(byte[] ipAddress, out int index);
668
669 [DllImport("iphlpapi.dll")]
670 internal static extern uint GetIfEntry2(ref MibIfRow2 pIfRow);
671
672 [DllImport("iphlpapi.dll")]
673 internal static extern uint GetIpStatisticsEx(out MibIpStats statistics, AddressFamily family);
674
675 [DllImport("iphlpapi.dll")]
676 internal static extern uint GetTcpStatisticsEx(out MibTcpStats statistics, AddressFamily family);
677
678 [DllImport("iphlpapi.dll")]
679 internal static extern uint GetUdpStatisticsEx(out MibUdpStats statistics, AddressFamily family);
680
681 [DllImport("iphlpapi.dll")]
682 internal static extern uint GetIcmpStatistics(out MibIcmpInfo statistics);
683
684 [DllImport("iphlpapi.dll")]
685 internal static extern uint GetIcmpStatisticsEx(out MibIcmpInfoEx statistics, AddressFamily family);
686
687 [DllImport("iphlpapi.dll")]
688 internal static extern uint GetTcpTable(IntPtr pTcpTable, ref uint dwOutBufLen, bool order);
689
690 [DllImport("iphlpapi.dll")]
691 internal static extern uint GetExtendedTcpTable(IntPtr pTcpTable, ref uint dwOutBufLen, bool order, uint IPVersion, TcpTableClass tableClass, uint reserved);
692
693 [DllImport("iphlpapi.dll")]
694 internal static extern uint GetUdpTable(IntPtr pUdpTable, ref uint dwOutBufLen, bool order);
695
696 [DllImport("iphlpapi.dll")]
697 internal static extern uint GetExtendedUdpTable(IntPtr pUdpTable, ref uint dwOutBufLen, bool order, uint IPVersion, UdpTableClass tableClass, uint reserved);
698
699 [DllImport("iphlpapi.dll")]
700 internal static extern uint GetPerAdapterInfo(uint IfIndex, IntPtr pPerAdapterInfo, ref uint pOutBufLen);
701
702 [DllImport("iphlpapi.dll")]
703 internal static extern void FreeMibTable(IntPtr handle);
704
705 [DllImport("iphlpapi.dll")]
706 internal static extern uint CancelMibChangeNotify2(IntPtr notificationHandle);
707
708 [DllImport("iphlpapi.dll")]
709 internal unsafe static extern uint NotifyStableUnicastIpAddressTable(AddressFamily addressFamily, out SafeFreeMibTable table, delegate* unmanaged<IntPtr, IntPtr, void> callback, IntPtr context, out SafeCancelMibChangeNotify notificationHandle);
710 }
711
712 internal static class Winsock
713 {
714 [Flags]
715 internal enum AsyncEventBits
716 {
717 FdNone = 0,
718 FdRead = 1,
719 FdWrite = 2,
720 FdOob = 4,
721 FdAccept = 8,
722 FdConnect = 0x10,
723 FdClose = 0x20,
724 FdQos = 0x40,
725 FdGroupQos = 0x80,
727 FdAddressListChange = 0x200,
728 FdAllEvents = 0x3FF
729 }
730
731 [DllImport("ws2_32.dll", SetLastError = true)]
732 internal static extern SocketError WSAEventSelect([In] SafeSocketHandle socketHandle, [In] SafeHandle Event, [In] AsyncEventBits NetworkEvents);
733
734 [DllImport("ws2_32.dll", EntryPoint = "WSAIoctl", SetLastError = true)]
735 internal static extern SocketError WSAIoctl_Blocking(SafeSocketHandle socketHandle, [In] int ioControlCode, [In] byte[] inBuffer, [In] int inBufferSize, [Out] byte[] outBuffer, [In] int outBufferSize, out int bytesTransferred, [In] IntPtr overlapped, [In] IntPtr completionRoutine);
736 }
737}
static uint GetUdpTable(IntPtr pUdpTable, ref uint dwOutBufLen, bool order)
static uint GetPerAdapterInfo(uint IfIndex, IntPtr pPerAdapterInfo, ref uint pOutBufLen)
static uint GetExtendedTcpTable(IntPtr pTcpTable, ref uint dwOutBufLen, bool order, uint IPVersion, TcpTableClass tableClass, uint reserved)
static uint GetIcmpStatistics(out MibIcmpInfo statistics)
static uint CancelMibChangeNotify2(IntPtr notificationHandle)
static void FreeMibTable(IntPtr handle)
static uint GetIfEntry2(ref MibIfRow2 pIfRow)
static uint GetIpStatisticsEx(out MibIpStats statistics, AddressFamily family)
static uint GetIcmpStatisticsEx(out MibIcmpInfoEx statistics, AddressFamily family)
static uint GetTcpTable(IntPtr pTcpTable, ref uint dwOutBufLen, bool order)
static uint GetBestInterfaceEx(byte[] ipAddress, out int index)
static uint GetTcpStatisticsEx(out MibTcpStats statistics, AddressFamily family)
static unsafe uint NotifyStableUnicastIpAddressTable(AddressFamily addressFamily, out SafeFreeMibTable table, delegate *unmanaged< IntPtr, IntPtr, void > callback, IntPtr context, out SafeCancelMibChangeNotify notificationHandle)
static uint GetExtendedUdpTable(IntPtr pUdpTable, ref uint dwOutBufLen, bool order, uint IPVersion, UdpTableClass tableClass, uint reserved)
static uint GetAdaptersAddresses(AddressFamily family, uint flags, IntPtr pReserved, IntPtr adapterAddresses, ref uint outBufLen)
static uint GetUdpStatisticsEx(out MibUdpStats statistics, AddressFamily family)
static uint GetNetworkParams(IntPtr pFixedInfo, ref uint pOutBufLen)
static SocketError WSAEventSelect([In] SafeSocketHandle socketHandle, [In] SafeHandle Event, [In] AsyncEventBits NetworkEvents)
static SocketError WSAIoctl_Blocking(SafeSocketHandle socketHandle, [In] int ioControlCode, [In] byte[] inBuffer, [In] int inBufferSize, [Out] byte[] outBuffer, [In] int outBufferSize, out int bytesTransferred, [In] IntPtr overlapped, [In] IntPtr completionRoutine)
static readonly int IPv4AddressSize
static void Copy(int[] source, int startIndex, IntPtr destination, int length)
Definition Marshal.cs:800
static ? object PtrToStructure(IntPtr ptr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type structureType)
Definition Marshal.cs:1164
IP_ADDR_STRING DnsServerList
Definition Interop.cs:22
AdapterAddressFlags flags
Definition Interop.cs:106
static IPAddressInformationCollection MarshalIpAddressInformationCollection(IntPtr ptr)
Definition Interop.cs:125
static InternalIPAddressCollection MarshalIpAddressCollection(IntPtr ptr)
Definition Interop.cs:112
InterfaceConnectionType connectionType
Definition Interop.cs:231
InterfaceTunnelType tunnelType
Definition Interop.cs:233
NetworkInterfaceType type
Definition Interop.cs:199
DuplicateAddressDetectionState dadState
Definition Interop.cs:153
InterfaceTunnelType tunnelType
Definition Interop.cs:316
OperationalStatus operStatus
Definition Interop.cs:328
InterfaceConnectionType connectionType
Definition Interop.cs:337
NetworkInterfaceType type
Definition Interop.cs:314
unsafe ReadOnlySpan< byte > localAddrAsSpan
Definition Interop.cs:593
unsafe fixed byte localAddr[16]
Definition Interop.cs:565
unsafe ReadOnlySpan< byte > remoteAddrAsSpan
Definition Interop.cs:595
unsafe fixed byte remoteAddr[16]
Definition Interop.cs:577
unsafe ReadOnlySpan< byte > localAddrAsSpan
Definition Interop.cs:657
unsafe fixed byte localAddr[16]
Definition Interop.cs:643
static readonly IntPtr Zero
Definition IntPtr.cs:18