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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VirtualRouter.Wlan;
using IcsMgr;
using System.ServiceModel;
using VirtualRouter.Wlan.WinAPI;
using System.Threading;
namespace VirtualRouterHost
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class VirtualRouterHost : IVirtualRouterHost
{
private WlanManager wlanManager;
private IcsManager icsManager;
private SharableConnection currentSharedConnection;
public VirtualRouterHost()
{
this.wlanManager = new WlanManager();
this.icsManager = new IcsManager();
}
#region IVirtualRouterHost Members
private string _lastErrorMessage;
public string GetLastError()
{
return this._lastErrorMessage;
}
public bool Start(SharableConnection sharedConnection)
{
try
{
this.Stop();
this.wlanManager.StartHostedNetwork();
Thread.Sleep(1000);
if (sharedConnection != null)
{
if (sharedConnection.Guid != Guid.Empty)
{
if (this.icsManager.SharingInstalled)
{
var privateConnectionGuid = this.wlanManager.HostedNetworkInterfaceGuid;
if (privateConnectionGuid == Guid.Empty)
{
// If the GUID for the Hosted Network Adapter isn't return properly,
// then retrieve it by the DeviceName.
privateConnectionGuid = (from c in this.icsManager.Connections
where c.props.DeviceName.ToLowerInvariant().Contains("microsoft virtual wifi miniport adapter") // Windows 7
|| c.props.DeviceName.ToLowerInvariant().Contains("microsoft hosted network virtual adapter") // Windows 8
select c.Guid).FirstOrDefault();
// Note: For some reason the DeviceName can have different names, currently it checks for the ones that I have identified thus far.
if (privateConnectionGuid == Guid.Empty)
{
// Device still now found, so throw exception so the message gets raised up to the client.
throw new Exception("Virtual Wifi device not found!\n\nNeither \"Microsoft Hosted Network Virtual Adapter\" or \"Microsoft Virtual Wifi Miniport Adapter\" were found.");
}
}
this.icsManager.EnableIcs(sharedConnection.Guid, privateConnectionGuid);
this.currentSharedConnection = sharedConnection;
}
}
}
else
{
this.currentSharedConnection = null;
}
return true;
}
catch(Exception ex)
{
this._lastErrorMessage = ex.Message;
return false;
}
}
public bool Stop()
{
try
{
if (this.icsManager.SharingInstalled)
{
this.icsManager.DisableIcsOnAll();
}
this.wlanManager.StopHostedNetwork();
return true;
}
catch
{
return false;
}
}
public bool SetConnectionSettings(string ssid, int maxNumberOfPeers)
{
try
{
this.wlanManager.SetConnectionSettings(ssid, maxNumberOfPeers);
return true;
}
catch
{
return false;
}
}
public ConnectionSettings GetConnectionSettings()
{
try
{
string ssid;
int maxNumberOfPeers;
var r = this.wlanManager.QueryConnectionSettings(out ssid, out maxNumberOfPeers);
return new ConnectionSettings()
{
SSID = ssid,
MaxPeerCount = maxNumberOfPeers
};
}
catch
{
return null;
}
}
public IEnumerable<SharableConnection> GetSharableConnections()
{
List<IcsConnection> connections;
try
{
connections = this.icsManager.Connections;
}
catch
{
connections = new List<IcsConnection>();
}
// Empty item to signify No Connection Sharing
yield return new SharableConnection() { DeviceName = "None", Guid = Guid.Empty, Name = "None" };
if (connections != null)
{
foreach (var conn in connections)
{
if (conn.IsConnected && conn.IsSupported)
{
yield return new SharableConnection(conn);
}
}
}
}
public bool SetPassword(string password)
{
try
{
this.wlanManager.SetSecondaryKey(password);
return true;
}
catch
{
return false;
}
}
public string GetPassword()
{
try
{
string passKey = string.Empty;
bool isPassPhrase;
bool isPersistent;
var r = this.wlanManager.QuerySecondaryKey(out passKey, out isPassPhrase, out isPersistent);
return passKey;
}
catch
{
return null;
}
}
public bool IsStarted()
{
try
{
return wlanManager.IsHostedNetworkStarted;
}
catch
{
return false;
}
}
public IEnumerable<ConnectedPeer> GetConnectedPeers()
{
foreach (var v in wlanManager.Stations)
{
yield return new ConnectedPeer(v.Value);
}
}
public SharableConnection GetSharedConnection()
{
return this.currentSharedConnection;
}
#endregion
}
}