-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServiceImpl.cs
298 lines (275 loc) · 12.2 KB
/
ServiceImpl.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Management;
using System.Collections.Generic;
namespace NetworkSharing
{
public class ServiceImpl : IDisposable
{
private ServiceConfig Configuration = new ServiceConfig();
private string UsedInterfaceId = string.Empty;
private string UsedInterfaceName = string.Empty;
private IPAddress UsedIpAddress = null;
private Process DhcpServiceProcess = null;
private IPAddress GetServedSubnet(UInt16 NetworkId)
{
Debug.Assert(!string.IsNullOrEmpty(UsedInterfaceId));
Debug.Assert(!string.IsNullOrEmpty(UsedInterfaceName));
Debug.Assert(UsedIpAddress != null);
Debug.Assert(UsedIpAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6);
// Full length: 128 bit | 16 bytes
// Prefix length: 64 bit, 8 bytes
// Address is represented in the network order (as is)
byte[] UsedIpAddressBytes = UsedIpAddress.GetAddressBytes();
Debug.Assert(UsedIpAddressBytes.Length == 16);
// We will slice this /64 subnet as by /112 subnets as follows
// XXXX:XXXX:XXXX:XXXX:FFFF:FFFF:NNNN:****:
// X - assigned by provider
// N - network id
// * - host id (will be zero in the subnet)
int UsedIpAddressBytesIndex = 8;
while (UsedIpAddressBytesIndex < 12) UsedIpAddressBytes[UsedIpAddressBytesIndex++] = 0xff;
byte[] NetworkIdBytes = BitConverter.GetBytes(NetworkId);
if (BitConverter.IsLittleEndian) Array.Reverse(NetworkIdBytes);
Debug.Assert(NetworkIdBytes.Length == 2);
UsedIpAddressBytes[UsedIpAddressBytesIndex++] = NetworkIdBytes[0];
UsedIpAddressBytes[UsedIpAddressBytesIndex++] = NetworkIdBytes[1];
while (UsedIpAddressBytesIndex < 16) UsedIpAddressBytes[UsedIpAddressBytesIndex++] = 0x0;
return new IPAddress(UsedIpAddressBytes);
}
private string RenderDibblerServerConfig()
{
StringBuilder ConfigBuilder = new StringBuilder();
ConfigBuilder.Append(@"
log-level 7
log-mode short
");
foreach (var ServedInterface in Configuration.ServedInterfaceList)
{
ConfigBuilder.AppendFormat(@"
iface ""{1}"" # {0}
{{
T1 60 # renew timout
T2 120 # emergency rebind timeout
preferred-lifetime 120
valid-lifetime 300
class
{{
pool {2}/112
}}
# Google DNS64
option dns-server 2001:4860:4860::6464, 2001:4860:4860::64
option domain local
}}
", ServedInterface.Id, ServedInterface.Name.Replace("\"", "\\\""), GetServedSubnet(ServedInterface.NetworkId).ToString());
}
return ConfigBuilder.ToString();
}
private void NewDibblerServerProcess()
{
Debug.Assert(DhcpServiceProcess == null);
// Generate the DHCPv6 server config
string DaemonDirectory = Path.Combine(Program.MyProgramDir, "dibbler_server");
Directory.CreateDirectory(DaemonDirectory); // Won't fail if the directory already exists, or will create a new one
string DibblerServerConfigPath = Path.Combine(DaemonDirectory, "server.conf");
using (FileStream ConfigFile = File.OpenWrite(DibblerServerConfigPath))
{
Byte[] content = new UTF8Encoding(true).GetBytes(RenderDibblerServerConfig());
ConfigFile.Write(content, 0, content.Length);
ConfigFile.SetLength(content.Length);
}
// Run the DHCPv6 process
DhcpServiceProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = Path.Combine(Program.MyProgramDir, "dibbler-server.exe"),
Arguments = "run",
WorkingDirectory = DaemonDirectory
}
};
DhcpServiceProcess.Start();
// Add routes
var mgmtRouteCls = new ManagementClass(new ManagementScope("\\\\.\\ROOT\\StandardCimv2"), new ManagementPath("MSFT_NetRoute"), null);
foreach (var ServedInterface in Configuration.ServedInterfaceList)
{
var mgmtRoute_CreateParams = mgmtRouteCls.GetMethodParameters("Create");
mgmtRoute_CreateParams["DestinationPrefix"] = GetServedSubnet(ServedInterface.NetworkId).ToString() + "/112";
mgmtRoute_CreateParams["InterfaceIndex"] = Convert.ToUInt32(ServedInterface.Index);
mgmtRoute_CreateParams["AddressFamily"] = Convert.ToUInt16(23); // IPv6
mgmtRoute_CreateParams["NextHop"] = "::";
mgmtRoute_CreateParams["Publish"] = Convert.ToByte(2); // Yes
mgmtRoute_CreateParams["PolicyStore"] = "ActiveStore";
try
{
var mgmtRoute_CreateResult = mgmtRouteCls.InvokeMethod("Create", mgmtRoute_CreateParams, null);
}
catch (ManagementException ex)
{
Program.LogEventLogError(string.Format("Error(s) occured while adding a route. More information: {0}", ex.ToString()));
}
}
}
private void KillDibblerServerProcess()
{
Debug.Assert(DhcpServiceProcess != null);
// Remove routes
try
{
var mgmtRouteCls = new ManagementClass(new ManagementScope("\\\\.\\ROOT\\StandardCimv2"), new ManagementPath("MSFT_NetRoute"), null);
foreach (var ServedInterface in Configuration.ServedInterfaceList)
{
foreach (ManagementObject mgmtRoute in mgmtRouteCls.GetInstances())
{
if (
(mgmtRoute["DestinationPrefix"] as string) == (GetServedSubnet(ServedInterface.NetworkId).ToString() + "/112")
&&
(mgmtRoute["InterfaceIndex"] as UInt32?) == ServedInterface.Index
&&
(mgmtRoute["AddressFamily"] as UInt16?) == 23 // IPv6
&&
(mgmtRoute["NextHop"] as string) == "::"
&&
(mgmtRoute["Publish"] as Byte?) == 2 // Yes
&&
(mgmtRoute["Store"] as Byte?) == 1 // Active
)
{
try
{
mgmtRoute.Delete();
}
catch (ManagementException ex)
{
Program.LogEventLogError(string.Format("Error(s) occured while removing the route. More information: {0}", ex.ToString()));
}
}
}
}
}
catch (System.Runtime.InteropServices.COMException com_ex)
{
unchecked
{
if (com_ex.HResult == (int)0x8007045B)
{
// System shutdown is in progress. Ignore.
}
else
{
throw;
}
}
}
// Kill the DHCPv6 process
try
{
DhcpServiceProcess.Kill();
}
catch (InvalidOperationException)
{
Program.LogEventLogWarning("Could not kill dibbler-server (DHCPv6 daemon). This might cause problems. Please stop the service, kill the rest of lingering daemons and restart the service again.");
}
finally
{
DhcpServiceProcess = null;
}
}
public void RefreshAddresses(ServiceConfig NewConfiguration)
{
string NewUsedInterfaceId = string.Empty;
string NewUsedInterfaceName = string.Empty;
IPAddress NewUsedIpAddress = null;
// create the mapping of current interfaces (interface id -> interface ref)
var netInterfaceMap = NetworkInterface.GetAllNetworkInterfaces().ToDictionary((adapter) => adapter.Id);
// iterate the WAN interface list config by priority
foreach (var wanInterface in NewConfiguration.WanInterfaceList)
{
if (!netInterfaceMap.TryGetValue(wanInterface, out var adapter))
{
continue;
}
// Skip non-operational adapters, loopback adapters
if (adapter.OperationalStatus != OperationalStatus.Up || adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
{
continue;
}
var IpProperties = adapter.GetIPProperties();
foreach (var IpAddressInfo in IpProperties.UnicastAddresses)
{
var IpAddress = IpAddressInfo.Address;
if (IpAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 &&
IpAddressInfo.PrefixLength == 64 &&
!IpAddress.IsIPv6LinkLocal && !IpAddress.IsIPv6Multicast && !IpAddress.IsIPv6SiteLocal && !IpAddress.IsIPv6Teredo
)
{
NewUsedInterfaceId = adapter.Id;
NewUsedInterfaceName = adapter.Name;
NewUsedIpAddress = IpAddressInfo.Address;
Program.Log(String.Format("Discovered interface {0}({1}), address {2}. Will be using this address. I won't start DHCPv6 daemon on this interface, of course.",
NewUsedInterfaceId, NewUsedInterfaceName, NewUsedIpAddress.ToString()
));
break; // address has been found
}
}
}
if (string.Equals(UsedInterfaceId, NewUsedInterfaceId) && IPAddress.Equals(UsedIpAddress, NewUsedIpAddress) && NewConfiguration.Equals(Configuration))
{
Program.Log("No changes");
}
else
{
Program.Log("Changes. Killing and restarting DHCPv6 servers");
if (UsedIpAddress != null)
{
// Kill all DHCPv6 servers
KillDibblerServerProcess();
}
Configuration = NewConfiguration;
UsedInterfaceId = NewUsedInterfaceId;
UsedInterfaceName = NewUsedInterfaceName;
UsedIpAddress = NewUsedIpAddress;
if (UsedIpAddress != null)
{
// Start new DHCPv6 servers
NewDibblerServerProcess();
}
}
}
#region IDisposable Support
private object disposeLock = new object();
private bool disposedFlag = false; // To detect redundant calls
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool bothManagedAndNative)
{
if (bothManagedAndNative)
{
lock (disposeLock)
{
if (!disposedFlag)
{
if (UsedIpAddress != null)
{
KillDibblerServerProcess();
}
UsedIpAddress = null;
disposedFlag = true;
}
}
}
}
#endregion
~ServiceImpl()
{
Dispose(false);
}
}
}