forked from Loriowar/IpHlpApidotnet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
IPHlpAPI32.cs
247 lines (208 loc) · 6.86 KB
/
IPHlpAPI32.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Net;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
namespace IpHlpApidotnet
{
#region UDP
[StructLayout(LayoutKind.Sequential)]
public struct MIB_UDPROW_OWNER_PID
{
public IPEndPoint Local;
public int dwOwningPid;
public string ProcessName;
}
public struct MIB_UDPROW_OWNER_MODULE
{
public IPEndPoint Local;
public uint dwOwningPid;
public long liCreateTimestamp; //LARGE_INTEGER
/*union {
struct {
DWORD SpecificPortBind : 1;
};
DWORD dwFlags;
};*/
public ulong[] OwningModuleInfo; //size TCPIP_OWNING_MODULE_SIZE
}
public struct MIB_UDPTABLE_OWNER_PID
{
public int dwNumEntries;
public MIB_UDPROW_OWNER_PID[] table;
}
public struct _MIB_UDPTABLE_OWNER_MODULE
{
public uint dwNumEntries;
public MIB_UDPROW_OWNER_MODULE[] table;
}
public enum UDP_TABLE_CLASS
{
UDP_TABLE_BASIC, //A MIB_UDPTABLE table that contains all UDP endpoints on the machine is returned to the caller.
UDP_TABLE_OWNER_PID, //A MIB_UDPTABLE_OWNER_PID or MIB_UDP6TABLE_OWNER_PID that contains all UDP endpoints on the machine is returned to the caller.
UDP_TABLE_OWNER_MODULE //A MIB_UDPTABLE_OWNER_MODULE or MIB_UDP6TABLE_OWNER_MODULE that contains all UDP endpoints on the machine is returned to the caller.
}
public struct MIB_UDPSTATS
{
public int dwInDatagrams ;
public int dwNoPorts ;
public int dwInErrors ;
public int dwOutDatagrams ;
public int dwNumAddrs;
}
public struct MIB_UDPTABLE
{
public int dwNumEntries;
public MIB_UDPROW[] table;
}
public struct MIB_UDPROW
{
public IPEndPoint Local;
}
public struct MIB_EXUDPTABLE
{
public int dwNumEntries;
public MIB_EXUDPROW[] table;
}
public struct MIB_EXUDPROW
{
public IPEndPoint Local;
public int dwProcessId;
public string ProcessName;
}
#endregion
#region TCP
[StructLayout(LayoutKind.Sequential)]
public struct MIB_TCPTABLE_OWNER_MODULE
{
public uint dwNumEntries;
public MIB_TCPROW_OWNER_MODULE[] table;
}
public struct MIB_TCPROW_OWNER_MODULE
{
public const int TCPIP_OWNING_MODULE_SIZE = 16;
public uint dwState;
public IPEndPoint Local; //LocalAddress
public IPEndPoint Remote; //RemoteAddress
public uint dwOwningPid;
public uint liCreateTimestamp; //LARGE_INTEGER
public ulong[] OwningModuleInfo; //Look how to define array size in structure ULONGLONG = new ulong[TCPIP_OWNING_MODULE_SIZE]
}
public struct MIB_TCPTABLE_OWNER_PID
{
public int dwNumEntries;
public MIB_TCPROW_OWNER_PID[] table;
}
public struct MIB_TCPROW_OWNER_PID
{
public int dwState;
public IPEndPoint Local; //LocalAddress
public IPEndPoint Remote; //RemoteAddress
public int dwOwningPid;
public string State;
public string ProcessName;
}
public enum TCP_TABLE_CLASS
{
TCP_TABLE_BASIC_LISTENER,
TCP_TABLE_BASIC_CONNECTIONS,
TCP_TABLE_BASIC_ALL,
TCP_TABLE_OWNER_PID_LISTENER,
TCP_TABLE_OWNER_PID_CONNECTIONS,
TCP_TABLE_OWNER_PID_ALL,
TCP_TABLE_OWNER_MODULE_LISTENER,
TCP_TABLE_OWNER_MODULE_CONNECTIONS,
TCP_TABLE_OWNER_MODULE_ALL,
}
public struct MIB_TCPSTATS
{
public int dwRtoAlgorithm ;
public int dwRtoMin ;
public int dwRtoMax ;
public int dwMaxConn ;
public int dwActiveOpens ;
public int dwPassiveOpens ;
public int dwAttemptFails ;
public int dwEstabResets ;
public int dwCurrEstab ;
public int dwInSegs ;
public int dwOutSegs ;
public int dwRetransSegs ;
public int dwInErrs ;
public int dwOutRsts ;
public int dwNumConns ;
}
public struct MIB_TCPTABLE
{
public int dwNumEntries;
public MIB_TCPROW[] table;
}
public struct MIB_TCPROW
{
public string StrgState;
public int iState;
public IPEndPoint Local;
public IPEndPoint Remote;
}
public struct MIB_EXTCPTABLE
{
public int dwNumEntries;
public MIB_EXTCPROW[] table;
}
public struct MIB_EXTCPROW
{
public string StrgState;
public int iState;
public IPEndPoint Local;
public IPEndPoint Remote;
public int dwProcessId;
public string ProcessName;
}
#endregion
public class IPHlpAPI32Wrapper
{
public const byte NO_ERROR = 0;
public const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
public const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
public const int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
public int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS;
[DllImport("iphlpapi.dll",SetLastError=true)]
public extern static int GetUdpStatistics (ref MIB_UDPSTATS pStats );
[DllImport("iphlpapi.dll",SetLastError=true)]
public static extern int GetUdpTable(byte[] UcpTable, out int pdwSize, bool bOrder);
[DllImport("iphlpapi.dll",SetLastError=true)]
public extern static int GetTcpStatistics (ref MIB_TCPSTATS pStats );
[DllImport("iphlpapi.dll",SetLastError=true)]
public static extern int GetTcpTable(byte[] pTcpTable, out int pdwSize, bool bOrder);
[DllImport("iphlpapi.dll",SetLastError=true)]
public extern static int AllocateAndGetTcpExTableFromStack(ref IntPtr pTable, bool bOrder, IntPtr heap ,int zero,int flags );
[DllImport("iphlpapi.dll",SetLastError=true)]
public extern static int AllocateAndGetUdpExTableFromStack(ref IntPtr pTable, bool bOrder, IntPtr heap,int zero,int flags );
[ DllImport( "kernel32" ,SetLastError=true)]
public static extern IntPtr GetProcessHeap();
[ DllImport( "kernel32" ,SetLastError=true)]
private static extern int FormatMessage( int flags, IntPtr source, int messageId,
int languageId, StringBuilder buffer, int size, IntPtr arguments );
[DllImport("iphlpapi.dll", SetLastError=true)]
public static extern int GetExtendedTcpTable(byte[] pTcpTable, out int dwOutBufLen, bool sort,
int ipVersion, TCP_TABLE_CLASS tblClass, int reserved);
[DllImport("iphlpapi.dll", SetLastError=true)]
public static extern int GetExtendedUdpTable(byte[] pUdpTable, out int dwOutBufLen, bool sort,
int ipVersion, UDP_TABLE_CLASS tblClass, int reserved);
public static string GetAPIErrorMessageDescription(int ApiErrNumber )
{
System.Text.StringBuilder sError = new System.Text.StringBuilder(512);
int lErrorMessageLength;
lErrorMessageLength = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,(IntPtr)0, ApiErrNumber, 0, sError, sError.Capacity, (IntPtr)0) ;
if(lErrorMessageLength > 0)
{
string strgError = sError.ToString();
strgError=strgError.Substring(0,strgError.Length-2);
return strgError+" ("+ApiErrNumber.ToString()+")";
}
return "none";
}
}
}