-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIpListenConfigItem.cs
148 lines (114 loc) · 5.24 KB
/
IpListenConfigItem.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
using System;
using System.Collections;
using System.Net;
using System.Runtime.InteropServices;
namespace HttpConfig
{
public class IpListenConfigItem : ConfigItem
{
private IPAddress _address;
public IpListenConfigItem() { }
public IPAddress Address
{
get { return _address; }
set
{
_address = value;
Text = _address.ToString();
}
}
public override string Key
{
get { return _address.ToString(); }
}
public override void ApplyConfig()
{
IntPtr pStruct = IntPtr.Zero;
HttpApi.HTTP_SERVICE_CONFIG_IP_LISTEN_PARAM setStruct = new HttpApi.HTTP_SERVICE_CONFIG_IP_LISTEN_PARAM();
try
{
setStruct.pAddress = HttpApi.BuildSockaddr(2, 0, _address);
setStruct.AddrLength = (short)Marshal.SizeOf(typeof(HttpApi.sockaddr));
pStruct = Marshal.AllocHGlobal(Marshal.SizeOf(setStruct));
Marshal.StructureToPtr(setStruct, pStruct, false);
if((Status == ModifiedStatus.Modified) || (Status == ModifiedStatus.Removed))
{
HttpApi.Error error = HttpApi.HttpDeleteServiceConfiguration(
IntPtr.Zero,
HttpApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigIPListenList,
pStruct,
Marshal.SizeOf(setStruct),
IntPtr.Zero);
if(error != HttpApi.Error.NO_ERROR)
throw new HttpApiException(error, "HttpDeleteServiceConfiguration (IPLISTEN) failed. Error = " + error);
}
if((Status == ModifiedStatus.Modified) || (Status == ModifiedStatus.Added))
{
HttpApi.Error error = HttpApi.HttpSetServiceConfiguration(
IntPtr.Zero,
HttpApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigIPListenList,
pStruct,
Marshal.SizeOf(setStruct),
IntPtr.Zero);
if(error != HttpApi.Error.NO_ERROR)
throw new HttpApiException(error, "HttpSetServiceConfiguration (IPLISTEN) failed. Error = " + error);
}
}
finally
{
if(setStruct.pAddress != IntPtr.Zero)
Marshal.FreeHGlobal(setStruct.pAddress);
if(pStruct != IntPtr.Zero)
Marshal.FreeHGlobal(pStruct);
}
}
public static Hashtable QueryConfig()
{
Hashtable items = new Hashtable();
int requiredBufferLength = 0;
HttpApi.Error error = QueryServiceConfig(IntPtr.Zero, 0, out requiredBufferLength);
if((error != HttpApi.Error.ERROR_FILE_NOT_FOUND) && (error != HttpApi.Error.ERROR_NOT_FOUND))
{
if(error != HttpApi.Error.ERROR_INSUFFICIENT_BUFFER)
throw new HttpApiException(error, "HttpQueryServiceConfiguration (IPLISTEN) failed. Error = " + error);
IntPtr pOutput = Marshal.AllocHGlobal(requiredBufferLength);
try
{
HttpApi.ZeroMemory(pOutput, requiredBufferLength);
error = QueryServiceConfig(pOutput, requiredBufferLength, out requiredBufferLength);
if(error != HttpApi.Error.NO_ERROR)
throw new HttpApiException(error, "HttpQueryServiceConfiguration (IPLISTEN) failed. Error = " + error);
HttpApi.HTTP_SERVICE_CONFIG_IP_LISTEN_QUERY output =
(HttpApi.HTTP_SERVICE_CONFIG_IP_LISTEN_QUERY)Marshal.PtrToStructure(pOutput, typeof(HttpApi.HTTP_SERVICE_CONFIG_IP_LISTEN_QUERY));
int addrSize = Marshal.SizeOf(typeof(HttpApi.SOCKADDR_STORAGE));
IntPtr pAddress = HttpApi.IncIntPtr(pOutput, 8); // Increment past the addrcount member and padding
for(int i = 0; i < output.AddrCount; i++)
{
IpListenConfigItem item = new IpListenConfigItem();
item.Address = new IPAddress((long)Marshal.ReadInt32(pAddress, 4) & 0x00000000ffffffff);
item.Status = ModifiedStatus.Unmodified;
items.Add(item.Key, item);
pAddress = HttpApi.IncIntPtr(pAddress, addrSize); // Increment to the next IP address
}
}
finally
{
Marshal.FreeHGlobal(pOutput);
}
}
return items;
}
private static HttpApi.Error QueryServiceConfig(IntPtr pOutput, int outputLength, out int requiredBufferLength)
{
return HttpApi.HttpQueryServiceConfiguration(
IntPtr.Zero,
HttpApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigIPListenList,
IntPtr.Zero,
0,
pOutput,
outputLength,
out requiredBufferLength,
IntPtr.Zero);
}
}
}