AccessPoint.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using Vinno.FIS.Sonopost.Wireless.Win32;
  7. using Vinno.FIS.Sonopost.Wireless.Win32.Interop;
  8. namespace Vinno.FIS.Sonopost.Wireless
  9. {
  10. public class AccessPoint
  11. {
  12. private WlanInterface _interface;
  13. private WlanAvailableNetwork _network;
  14. internal AccessPoint(WlanInterface interfac, WlanAvailableNetwork network)
  15. {
  16. _interface = interfac;
  17. _network = network;
  18. }
  19. public string Name
  20. {
  21. get
  22. {
  23. var str = Encoding.UTF8.GetString(_network.dot11Ssid.SSID, 0, (int)_network.dot11Ssid.SSIDLength);
  24. if (string.IsNullOrWhiteSpace(str))
  25. {
  26. return "Others";
  27. }
  28. return str;
  29. }
  30. }
  31. public uint SignalStrength
  32. {
  33. get
  34. {
  35. return _network.wlanSignalQuality;
  36. }
  37. }
  38. /// <summary>
  39. /// If the computer has a connection profile stored for this access point
  40. /// </summary>
  41. public bool HasProfile
  42. {
  43. get
  44. {
  45. try
  46. {
  47. return _interface.GetProfiles().Where(p => p.profileName == Name).Any();
  48. }
  49. catch
  50. {
  51. return false;
  52. }
  53. }
  54. }
  55. public bool IsSecure
  56. {
  57. get
  58. {
  59. return _network.securityEnabled;
  60. }
  61. }
  62. public bool IsConnected
  63. {
  64. get
  65. {
  66. try
  67. {
  68. var a = _interface.CurrentConnection; // This prop throws exception if not connected, which forces me to this try catch. Refactor plix.
  69. return a.profileName == _network.profileName;
  70. }
  71. catch
  72. {
  73. return false;
  74. }
  75. }
  76. }
  77. /// <summary>
  78. /// Returns the underlying network object.
  79. /// </summary>
  80. internal WlanAvailableNetwork Network
  81. {
  82. get
  83. {
  84. return _network;
  85. }
  86. }
  87. /// <summary>
  88. /// Returns the underlying interface object.
  89. /// </summary>
  90. internal WlanInterface Interface
  91. {
  92. get
  93. {
  94. return _interface;
  95. }
  96. }
  97. /// <summary>
  98. /// Checks that the password format matches this access point's encryption method.
  99. /// </summary>
  100. public bool IsValidPassword(string password)
  101. {
  102. return PasswordHelper.IsValid(password, _network.dot11DefaultCipherAlgorithm);
  103. }
  104. /// <summary>
  105. /// Connect synchronous to the access point.
  106. /// </summary>
  107. public bool Connect(AuthRequest request, bool overwriteProfile = false)
  108. {
  109. // No point to continue with the connect if the password is not valid if overwrite is true or profile is missing.
  110. if (!request.IsPasswordValid && (!HasProfile || overwriteProfile))
  111. return false;
  112. // If we should create or overwrite the profile, do so.
  113. if (!HasProfile || overwriteProfile)
  114. {
  115. if (HasProfile)
  116. _interface.DeleteProfile(Name);
  117. request.Process();
  118. }
  119. // TODO: Auth algorithm: IEEE80211_Open + Cipher algorithm: None throws an error.
  120. // Probably due to connectionmode profile + no profile exist, cant figure out how to solve it though.
  121. return _interface.ConnectSynchronously(WlanConnectionMode.Profile, _network.dot11BssType, Name, 6000);
  122. }
  123. /// <summary>
  124. /// Connect asynchronous to the access point.
  125. /// </summary>
  126. public void ConnectAsync(AuthRequest request, bool overwriteProfile = false, Action<bool> onConnectComplete = null)
  127. {
  128. // TODO: Refactor -> Use async connect in wlaninterface.
  129. ThreadPool.QueueUserWorkItem(new WaitCallback((o) =>
  130. {
  131. bool success = false;
  132. try
  133. {
  134. success = Connect(request, overwriteProfile);
  135. }
  136. catch (Win32Exception)
  137. {
  138. success = false;
  139. }
  140. if (onConnectComplete != null)
  141. onConnectComplete(success);
  142. }));
  143. }
  144. public string GetProfileXML()
  145. {
  146. if (HasProfile)
  147. return _interface.GetProfileXml(Name);
  148. else
  149. return string.Empty;
  150. }
  151. public void DeleteProfile()
  152. {
  153. try
  154. {
  155. if (HasProfile)
  156. _interface.DeleteProfile(Name);
  157. }
  158. catch { }
  159. }
  160. public override sealed string ToString()
  161. {
  162. StringBuilder info = new StringBuilder();
  163. info.AppendLine("Interface: " + _interface.InterfaceName);
  164. info.AppendLine("Auth algorithm: " + _network.dot11DefaultAuthAlgorithm);
  165. info.AppendLine("Cipher algorithm: " + _network.dot11DefaultCipherAlgorithm);
  166. info.AppendLine("BSS type: " + _network.dot11BssType);
  167. info.AppendLine("Connectable: " + _network.networkConnectable);
  168. if (!_network.networkConnectable)
  169. info.AppendLine("Reason to false: " + _network.wlanNotConnectableReason);
  170. return info.ToString();
  171. }
  172. public object Clone()
  173. {
  174. return MemberwiseClone();
  175. }
  176. }
  177. }