-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpApi.cs
283 lines (228 loc) · 8.43 KB
/
HttpApi.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading;
namespace HttpConfig
{
public class HttpApi
{
private HttpApi() {}
#region Methods
[DllImport("Httpapi.dll")]
public static extern HttpApi.Error HttpInitialize(
HTTPAPI_VERSION version,
InitFlag flags,
IntPtr reserved);
[DllImport("Httpapi.dll")]
public static extern HttpApi.Error HttpQueryServiceConfiguration(
IntPtr ServiceHandle,
HTTP_SERVICE_CONFIG_ID ConfigId,
IntPtr pInputConfigInfo,
int InputConfigInfoLength,
IntPtr pOutputConfigInfo,
int OutputConfigInfoLength,
out int pReturnLength,
IntPtr pOverlapped);
[DllImport("Httpapi.dll")]
public static extern HttpApi.Error HttpSetServiceConfiguration(
IntPtr ServiceHandle,
HTTP_SERVICE_CONFIG_ID ConfigId,
IntPtr pConfigInformation,
int ConfigInformationLength,
IntPtr pOverlapped);
[DllImport("Httpapi.dll")]
public static extern HttpApi.Error HttpDeleteServiceConfiguration(
IntPtr ServiceHandle,
HTTP_SERVICE_CONFIG_ID ConfigId,
IntPtr pConfigInformation,
int ConfigInformationLength,
IntPtr pOverlapped);
[DllImport("Httpapi.dll")]
public static extern HttpApi.Error HttpTerminate(
InitFlag flags,
IntPtr reserved);
[DllImport("Kernel32.dll", EntryPoint="RtlZeroMemory")]
public static extern void ZeroMemory(
IntPtr dest,
int size);
#endregion Methods
#region Structures
[StructLayout(LayoutKind.Sequential)]
public struct HTTPAPI_VERSION
{
public HTTPAPI_VERSION(ushort majorVersion, ushort minorVersion)
{
major = majorVersion;
minor = minorVersion;
}
public ushort major;
public ushort minor;
}
[StructLayout(LayoutKind.Sequential)]
public struct SOCKADDR_STORAGE
{
public short sa_family;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType=UnmanagedType.U1, SizeConst=6)]
public byte[] __ss_pad1;
public long __ss_align;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType=UnmanagedType.U1, SizeConst=112)]
public byte[] __ss_pad2;
}
[StructLayout(LayoutKind.Sequential)]
public struct HTTP_SERVICE_CONFIG_IP_LISTEN_QUERY
{
public int AddrCount;
public SOCKADDR_STORAGE AddrList; // [ANYSIZE_ARRAY];
}
[StructLayout(LayoutKind.Sequential)]
public struct HTTP_SERVICE_CONFIG_IP_LISTEN_PARAM
{
public short AddrLength;
public IntPtr pAddress; // PSOCKADDR
}
[StructLayout(LayoutKind.Sequential)]
public struct HTTP_SERVICE_CONFIG_SSL_KEY
{
public IntPtr pIpPort; // PSOCKADDR
}
[StructLayout(LayoutKind.Sequential)]
public struct HTTP_SERVICE_CONFIG_SSL_QUERY
{
public HTTP_SERVICE_CONFIG_QUERY_TYPE QueryDesc;
public HTTP_SERVICE_CONFIG_SSL_KEY KeyDesc;
public int dwToken;
}
[StructLayout(LayoutKind.Sequential)]
public struct HTTP_SERVICE_CONFIG_URLACL_KEY
{
[MarshalAs(UnmanagedType.LPWStr)]
public string pUrlPrefix;
}
[StructLayout(LayoutKind.Sequential)]
public struct sockaddr
{
public short sa_family;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=14, ArraySubType=UnmanagedType.U1)]
char[] sa_data;
};
[StructLayout(LayoutKind.Sequential)]
public struct HTTP_SERVICE_CONFIG_URLACL_QUERY
{
public HTTP_SERVICE_CONFIG_QUERY_TYPE QueryDesc;
public HTTP_SERVICE_CONFIG_URLACL_KEY KeyDesc;
public int dwToken;
}
[StructLayout(LayoutKind.Sequential)]
public struct HTTP_SERVICE_CONFIG_SSL_PARAM
{
public int SslHashLength;
public IntPtr pSslHash;
public Guid AppId;
[MarshalAs(UnmanagedType.LPWStr)]
public string pSslCertStoreName;
public ClientCertCheckMode CertCheckMode;
public int RevocationFreshnessTime;
public int RevocationUrlRetrievalTimeout;
[MarshalAs(UnmanagedType.LPWStr)]
public string pSslCtlIdentifier;
[MarshalAs(UnmanagedType.LPWStr)]
public string pSslCtlStoreName;
public SslConfigFlag Flags;
}
[StructLayout(LayoutKind.Sequential)]
public struct HTTP_SERVICE_CONFIG_SSL_SET
{
public HTTP_SERVICE_CONFIG_SSL_KEY KeyDesc;
public HTTP_SERVICE_CONFIG_SSL_PARAM ParamDesc;
}
[StructLayout(LayoutKind.Sequential)]
public struct HTTP_SERVICE_CONFIG_URLACL_PARAM
{
[MarshalAs(UnmanagedType.LPWStr)]
public string pStringSecurityDescriptor;
}
[StructLayout(LayoutKind.Sequential)]
public struct HTTP_SERVICE_CONFIG_URLACL_SET
{
public HTTP_SERVICE_CONFIG_URLACL_KEY KeyDesc;
public HTTP_SERVICE_CONFIG_URLACL_PARAM ParamDesc;
}
#endregion Structures
#region Enumerations
public enum InitFlag : int
{
HTTP_INITIALIZE_SERVER = 1,
HTTP_INITIALIZE_CONFIG = 2
}
public enum HTTP_SERVICE_CONFIG_ID : int
{
HttpServiceConfigIPListenList = 0,
HttpServiceConfigSSLCertInfo = 1,
HttpServiceConfigUrlAclInfo = 2
}
public enum HTTP_SERVICE_CONFIG_QUERY_TYPE : int
{
HttpServiceConfigQueryExact = 0,
HttpServiceConfigQueryNext = 1
}
public enum Error : int
{
NO_ERROR = 0,
ERROR_FILE_NOT_FOUND = 2,
ERROR_INVALID_DATA = 13,
ERROR_HANDLE_EOF = 38,
ERROR_INVALID_PARAMETER = 87,
ERROR_INSUFFICIENT_BUFFER = 122,
ERROR_NO_MORE_ITEMS = 259,
ERROR_INVALID_DLL = 1154,
ERROR_NOT_FOUND = 1168
}
public enum ClientCertCheckMode
{
NoVerifyRevocation = 1,
CachedRevocationOnly = 2,
UseRevocationFreshnessTime = 4,
NoUsageCheck = 0x10000,
}
public enum SslConfigFlag : int
{
UseDSMapper = 1,
NegotiateClientCertificates = 2,
DoNotRouteToRawIsapiFilters = 4,
}
#endregion Enumerations
public static IntPtr IncIntPtr(IntPtr ptr, int count)
{
return (IntPtr)((int)ptr + count);
}
public static IntPtr BuildSockaddr(short family, ushort port, IPAddress address)
{
int sockaddrSize = Marshal.SizeOf(typeof(HttpApi.sockaddr));
IntPtr pSockaddr = Marshal.AllocHGlobal(sockaddrSize);
HttpApi.ZeroMemory(pSockaddr, sockaddrSize);
Marshal.WriteInt16(pSockaddr, family);
ushort p = (ushort)IPAddress.HostToNetworkOrder((short)port);
Marshal.WriteInt16(pSockaddr, 2, (short)p);
byte[] addr = address.GetAddressBytes();
IntPtr pAddr = HttpApi.IncIntPtr(pSockaddr, 4);
Marshal.Copy(addr, 0, pAddr, addr.Length);
return pSockaddr;
}
}
public class HttpApiException : Exception
{
public readonly HttpApi.Error HttpApiErrorCode;
public HttpApiException(HttpApi.Error error) : base()
{
HttpApiErrorCode = error;
}
public HttpApiException(HttpApi.Error error, string message) : base(message)
{
HttpApiErrorCode = error;
}
public HttpApiException(HttpApi.Error error, string message, Exception innerException) : base(message, innerException)
{
HttpApiErrorCode = error;
}
}
}