Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | // This source is mostly a bunch of Windows API calls. It is only compiled for |
michael@0 | 6 | // Windows builds. |
michael@0 | 7 | // |
michael@0 | 8 | // Registry entries for Autodial mappings are located here: |
michael@0 | 9 | // HKEY_CURRENT_USER\Software\Microsoft\RAS Autodial\Addresses |
michael@0 | 10 | |
michael@0 | 11 | #include <windows.h> |
michael@0 | 12 | #include <winsvc.h> |
michael@0 | 13 | #include "nsString.h" |
michael@0 | 14 | #include "nsAutodialWin.h" |
michael@0 | 15 | #include "prlog.h" |
michael@0 | 16 | #include "nsWindowsHelpers.h" |
michael@0 | 17 | |
michael@0 | 18 | #define AUTODIAL_DEFAULT AUTODIAL_NEVER |
michael@0 | 19 | |
michael@0 | 20 | // |
michael@0 | 21 | // Log module for autodial logging... |
michael@0 | 22 | // |
michael@0 | 23 | // To enable logging (see prlog.h for full details): |
michael@0 | 24 | // |
michael@0 | 25 | // set NSPR_LOG_MODULES=Autodial:5 |
michael@0 | 26 | // set NSPR_LOG_FILE=nspr.log |
michael@0 | 27 | // |
michael@0 | 28 | // this enables PR_LOG_DEBUG level information and places all output in |
michael@0 | 29 | // the file nspr.log |
michael@0 | 30 | // |
michael@0 | 31 | |
michael@0 | 32 | #ifdef PR_LOGGING |
michael@0 | 33 | static PRLogModuleInfo* gLog = nullptr; |
michael@0 | 34 | #endif |
michael@0 | 35 | |
michael@0 | 36 | #undef LOGD |
michael@0 | 37 | #undef LOGE |
michael@0 | 38 | #define LOGD(args) PR_LOG(gLog, PR_LOG_DEBUG, args) |
michael@0 | 39 | #define LOGE(args) PR_LOG(gLog, PR_LOG_ERROR, args) |
michael@0 | 40 | |
michael@0 | 41 | // Don't try to dial again within a few seconds of when user pressed cancel. |
michael@0 | 42 | #define NO_RETRY_PERIOD_SEC 5 |
michael@0 | 43 | PRIntervalTime nsAutodial::mDontRetryUntil = 0; |
michael@0 | 44 | |
michael@0 | 45 | // ctor. |
michael@0 | 46 | nsAutodial::nsAutodial() |
michael@0 | 47 | : mAutodialBehavior(AUTODIAL_DEFAULT), |
michael@0 | 48 | mAutodialServiceDialingLocation(-1), |
michael@0 | 49 | mNumRASConnectionEntries(0) |
michael@0 | 50 | { |
michael@0 | 51 | // Initializations that can be made again since RAS OS settings can |
michael@0 | 52 | // change. |
michael@0 | 53 | Init(); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | // dtor |
michael@0 | 57 | nsAutodial::~nsAutodial() |
michael@0 | 58 | { |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | |
michael@0 | 62 | // Get settings from the OS. These are settings that might change during |
michael@0 | 63 | // the OS session. Call Init() again to pick up those changes if required. |
michael@0 | 64 | // Returns NS_ERROR_FAILURE if error or NS_OK if success. |
michael@0 | 65 | nsresult nsAutodial::Init() |
michael@0 | 66 | { |
michael@0 | 67 | #ifdef PR_LOGGING |
michael@0 | 68 | if (!gLog) |
michael@0 | 69 | gLog = PR_NewLogModule("Autodial"); |
michael@0 | 70 | #endif |
michael@0 | 71 | |
michael@0 | 72 | mDefaultEntryName[0] = '\0'; |
michael@0 | 73 | mNumRASConnectionEntries = 0; |
michael@0 | 74 | mAutodialBehavior = QueryAutodialBehavior(); |
michael@0 | 75 | |
michael@0 | 76 | // No need to continue in this case. |
michael@0 | 77 | if (mAutodialBehavior == AUTODIAL_NEVER) |
michael@0 | 78 | { |
michael@0 | 79 | return NS_OK; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | |
michael@0 | 83 | // Get the number of dialup entries in the phonebook. |
michael@0 | 84 | mNumRASConnectionEntries = NumRASEntries(); |
michael@0 | 85 | |
michael@0 | 86 | // Get the name of the default entry. |
michael@0 | 87 | nsresult result = GetDefaultEntryName(mDefaultEntryName, |
michael@0 | 88 | sizeof(mDefaultEntryName)); |
michael@0 | 89 | |
michael@0 | 90 | return result; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | |
michael@0 | 94 | // Should we attempt to dial on a network error? Yes if the Internet Options |
michael@0 | 95 | // configured as such. Yes if the RAS autodial service is running (we'll try to |
michael@0 | 96 | // force it to dial in that case by adding the network address to its db.) |
michael@0 | 97 | bool nsAutodial::ShouldDialOnNetworkError() |
michael@0 | 98 | { |
michael@0 | 99 | // Don't try to dial again within a few seconds of when user pressed cancel. |
michael@0 | 100 | if (mDontRetryUntil) |
michael@0 | 101 | { |
michael@0 | 102 | PRIntervalTime intervalNow = PR_IntervalNow(); |
michael@0 | 103 | if (intervalNow < mDontRetryUntil) |
michael@0 | 104 | { |
michael@0 | 105 | LOGD(("Autodial: Not dialing: too soon.")); |
michael@0 | 106 | return false; |
michael@0 | 107 | } |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | |
michael@0 | 111 | return ((mAutodialBehavior == AUTODIAL_ALWAYS) |
michael@0 | 112 | || (mAutodialBehavior == AUTODIAL_ON_NETWORKERROR) |
michael@0 | 113 | || (mAutodialBehavior == AUTODIAL_USE_SERVICE)); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | |
michael@0 | 117 | // The autodial info is set in Control Panel | Internet Options | Connections. |
michael@0 | 118 | // The values are stored in the registry. This function gets those values from |
michael@0 | 119 | // the registry and determines if we should never dial, always dial, or dial |
michael@0 | 120 | // when there is no network found. |
michael@0 | 121 | int nsAutodial::QueryAutodialBehavior() |
michael@0 | 122 | { |
michael@0 | 123 | if (IsAutodialServiceRunning()) |
michael@0 | 124 | { |
michael@0 | 125 | // Is Autodial service enabled for the current login session? |
michael@0 | 126 | DWORD disabled = 0; |
michael@0 | 127 | DWORD size = sizeof(DWORD); |
michael@0 | 128 | if (RasGetAutodialParamW(RASADP_LoginSessionDisable, &disabled, &size) == ERROR_SUCCESS) |
michael@0 | 129 | { |
michael@0 | 130 | if (!disabled) |
michael@0 | 131 | { |
michael@0 | 132 | // If current dialing location has autodial on, we'll let the service dial. |
michael@0 | 133 | mAutodialServiceDialingLocation = GetCurrentLocation(); |
michael@0 | 134 | if (IsAutodialServiceEnabled(mAutodialServiceDialingLocation)) |
michael@0 | 135 | { |
michael@0 | 136 | return AUTODIAL_USE_SERVICE; |
michael@0 | 137 | } |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | // If we get to here, then the service is not going to dial on error, so we |
michael@0 | 143 | // can dial ourselves if the control panel settings are set up that way. |
michael@0 | 144 | HKEY hKey = 0; |
michael@0 | 145 | LONG result = ::RegOpenKeyExW( |
michael@0 | 146 | HKEY_CURRENT_USER, |
michael@0 | 147 | L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", |
michael@0 | 148 | 0, |
michael@0 | 149 | KEY_READ, |
michael@0 | 150 | &hKey); |
michael@0 | 151 | |
michael@0 | 152 | if (result != ERROR_SUCCESS) |
michael@0 | 153 | { |
michael@0 | 154 | LOGE(("Autodial: Error opening reg key Internet Settings")); |
michael@0 | 155 | return AUTODIAL_NEVER; |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | DWORD entryType = 0; |
michael@0 | 159 | DWORD autodial = 0; |
michael@0 | 160 | DWORD onDemand = 0; |
michael@0 | 161 | DWORD paramSize = sizeof(DWORD); |
michael@0 | 162 | |
michael@0 | 163 | result = ::RegQueryValueExW(hKey, L"EnableAutodial", nullptr, &entryType, (LPBYTE)&autodial, ¶mSize); |
michael@0 | 164 | if (result != ERROR_SUCCESS) |
michael@0 | 165 | { |
michael@0 | 166 | ::RegCloseKey(hKey); |
michael@0 | 167 | LOGE(("Autodial: Error reading reg value EnableAutodial.")); |
michael@0 | 168 | return AUTODIAL_NEVER; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | result = ::RegQueryValueExW(hKey, L"NoNetAutodial", nullptr, &entryType, (LPBYTE)&onDemand, ¶mSize); |
michael@0 | 172 | if (result != ERROR_SUCCESS) |
michael@0 | 173 | { |
michael@0 | 174 | ::RegCloseKey(hKey); |
michael@0 | 175 | LOGE(("Autodial: Error reading reg value NoNetAutodial.")); |
michael@0 | 176 | return AUTODIAL_NEVER; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | ::RegCloseKey(hKey); |
michael@0 | 180 | |
michael@0 | 181 | if (!autodial) |
michael@0 | 182 | { |
michael@0 | 183 | return AUTODIAL_NEVER; |
michael@0 | 184 | } |
michael@0 | 185 | else |
michael@0 | 186 | { |
michael@0 | 187 | if (onDemand) |
michael@0 | 188 | { |
michael@0 | 189 | return AUTODIAL_ON_NETWORKERROR; |
michael@0 | 190 | } |
michael@0 | 191 | else |
michael@0 | 192 | { |
michael@0 | 193 | return AUTODIAL_ALWAYS; |
michael@0 | 194 | } |
michael@0 | 195 | } |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | // If the RAS autodial service is running, use it. Otherwise, dial |
michael@0 | 199 | // the default RAS connection. There are two possible RAS dialogs: |
michael@0 | 200 | // one that dials a single entry, and one that lets the user choose which |
michael@0 | 201 | // to dial. If there is only one connection entry in the phone book, or |
michael@0 | 202 | // there are multiple entries but one is defined as the default, we'll use |
michael@0 | 203 | // the single entry dial dialog. If there are multiple connection entries, |
michael@0 | 204 | // and none is specified as default, we'll bring up the diallog which lets |
michael@0 | 205 | // the user select the connection entry to use. |
michael@0 | 206 | // |
michael@0 | 207 | // Return values: |
michael@0 | 208 | // NS_OK: dialing was successful and caller should retry |
michael@0 | 209 | // all other values indicate that the caller should not retry |
michael@0 | 210 | nsresult nsAutodial::DialDefault(const char16_t* hostName) |
michael@0 | 211 | { |
michael@0 | 212 | mDontRetryUntil = 0; |
michael@0 | 213 | |
michael@0 | 214 | if (mAutodialBehavior == AUTODIAL_NEVER) |
michael@0 | 215 | { |
michael@0 | 216 | return NS_ERROR_FAILURE; // don't retry the network error |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | // If already a RAS connection, bail. |
michael@0 | 220 | if (IsRASConnected()) |
michael@0 | 221 | { |
michael@0 | 222 | LOGD(("Autodial: Not dialing: active connection.")); |
michael@0 | 223 | return NS_ERROR_FAILURE; // don't retry |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | // If no dialup connections configured, bail. |
michael@0 | 227 | if (mNumRASConnectionEntries <= 0) |
michael@0 | 228 | { |
michael@0 | 229 | LOGD(("Autodial: Not dialing: no entries.")); |
michael@0 | 230 | return NS_ERROR_FAILURE; // don't retry |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | |
michael@0 | 234 | // If autodial service is running, let it dial. In order for it to dial more |
michael@0 | 235 | // reliably, we have to add the target address to the autodial database. |
michael@0 | 236 | // This is the only way the autodial service dial if there is a network |
michael@0 | 237 | // adapter installed. But even then it might not dial. We have to assume that |
michael@0 | 238 | // it will though, or we could end up with two attempts to dial on the same |
michael@0 | 239 | // network error if the user cancels the first one: one from the service and |
michael@0 | 240 | // one from us. |
michael@0 | 241 | // See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rras/ras4over_3dwl.asp |
michael@0 | 242 | if (mAutodialBehavior == AUTODIAL_USE_SERVICE) |
michael@0 | 243 | { |
michael@0 | 244 | AddAddressToAutodialDirectory(hostName); |
michael@0 | 245 | return NS_ERROR_FAILURE; // don't retry |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | // Do the dialing ourselves. |
michael@0 | 249 | else |
michael@0 | 250 | { |
michael@0 | 251 | // If a default dial entry is configured, use it. |
michael@0 | 252 | if (mDefaultEntryName[0] != '\0') |
michael@0 | 253 | { |
michael@0 | 254 | LOGD(("Autodial: Dialing default: %s.",mDefaultEntryName)); |
michael@0 | 255 | |
michael@0 | 256 | RASDIALDLG rasDialDlg; |
michael@0 | 257 | memset(&rasDialDlg, 0, sizeof(rasDialDlg)); |
michael@0 | 258 | rasDialDlg.dwSize = sizeof(rasDialDlg); |
michael@0 | 259 | |
michael@0 | 260 | BOOL dialed = |
michael@0 | 261 | RasDialDlgW(nullptr, mDefaultEntryName, nullptr, &rasDialDlg); |
michael@0 | 262 | |
michael@0 | 263 | if (!dialed) |
michael@0 | 264 | { |
michael@0 | 265 | if (rasDialDlg.dwError != 0) |
michael@0 | 266 | { |
michael@0 | 267 | LOGE(("Autodial ::RasDialDlg failed: Error: %d.", |
michael@0 | 268 | rasDialDlg.dwError)); |
michael@0 | 269 | } |
michael@0 | 270 | else |
michael@0 | 271 | { |
michael@0 | 272 | mDontRetryUntil = PR_IntervalNow() + PR_SecondsToInterval(NO_RETRY_PERIOD_SEC); |
michael@0 | 273 | LOGD(("Autodial: User cancelled dial.")); |
michael@0 | 274 | } |
michael@0 | 275 | return NS_ERROR_FAILURE; // don't retry |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | LOGD(("Autodial: RAS dialup connection successful.")); |
michael@0 | 279 | } |
michael@0 | 280 | |
michael@0 | 281 | // If no default connection specified, open the dialup dialog that lets |
michael@0 | 282 | // the user specifiy which connection to dial. |
michael@0 | 283 | else |
michael@0 | 284 | { |
michael@0 | 285 | LOGD(("Autodial: Prompting for phonebook entry.")); |
michael@0 | 286 | |
michael@0 | 287 | RASPBDLG rasPBDlg; |
michael@0 | 288 | memset(&rasPBDlg, 0, sizeof(rasPBDlg)); |
michael@0 | 289 | rasPBDlg.dwSize = sizeof(rasPBDlg); |
michael@0 | 290 | |
michael@0 | 291 | BOOL dialed = RasPhonebookDlgW(nullptr, nullptr, &rasPBDlg); |
michael@0 | 292 | |
michael@0 | 293 | if (!dialed) |
michael@0 | 294 | { |
michael@0 | 295 | if (rasPBDlg.dwError != 0) |
michael@0 | 296 | { |
michael@0 | 297 | LOGE(("Autodial: ::RasPhonebookDlg failed: Error = %d.", |
michael@0 | 298 | rasPBDlg.dwError)); |
michael@0 | 299 | } |
michael@0 | 300 | else |
michael@0 | 301 | { |
michael@0 | 302 | mDontRetryUntil = PR_IntervalNow() + PR_SecondsToInterval(NO_RETRY_PERIOD_SEC); |
michael@0 | 303 | LOGD(("Autodial: User cancelled dial.")); |
michael@0 | 304 | } |
michael@0 | 305 | |
michael@0 | 306 | return NS_ERROR_FAILURE; // don't retry |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | LOGD(("Autodial: RAS dialup connection successful.")); |
michael@0 | 310 | } |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | // Retry because we just established a dialup connection. |
michael@0 | 314 | return NS_OK; |
michael@0 | 315 | } |
michael@0 | 316 | |
michael@0 | 317 | |
michael@0 | 318 | // Check to see if RAS is already connected. |
michael@0 | 319 | bool nsAutodial::IsRASConnected() |
michael@0 | 320 | { |
michael@0 | 321 | DWORD connections; |
michael@0 | 322 | RASCONN rasConn; |
michael@0 | 323 | rasConn.dwSize = sizeof(rasConn); |
michael@0 | 324 | DWORD structSize = sizeof(rasConn); |
michael@0 | 325 | |
michael@0 | 326 | DWORD result = RasEnumConnectionsW(&rasConn, &structSize, &connections); |
michael@0 | 327 | |
michael@0 | 328 | // ERROR_BUFFER_TOO_SMALL is OK because we only need one struct. |
michael@0 | 329 | if (result == ERROR_SUCCESS || result == ERROR_BUFFER_TOO_SMALL) |
michael@0 | 330 | { |
michael@0 | 331 | return (connections > 0); |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | LOGE(("Autodial: ::RasEnumConnections failed: Error = %d", result)); |
michael@0 | 335 | return false; |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | // Get the first RAS dial entry name from the phonebook. |
michael@0 | 339 | nsresult nsAutodial::GetFirstEntryName(wchar_t* entryName, int bufferSize) |
michael@0 | 340 | { |
michael@0 | 341 | RASENTRYNAMEW rasEntryName; |
michael@0 | 342 | rasEntryName.dwSize = sizeof(rasEntryName); |
michael@0 | 343 | DWORD cb = sizeof(rasEntryName); |
michael@0 | 344 | DWORD cEntries = 0; |
michael@0 | 345 | |
michael@0 | 346 | DWORD result = |
michael@0 | 347 | RasEnumEntriesW(nullptr, nullptr, &rasEntryName, &cb, &cEntries); |
michael@0 | 348 | |
michael@0 | 349 | // ERROR_BUFFER_TOO_SMALL is OK because we only need one struct. |
michael@0 | 350 | if (result == ERROR_SUCCESS || result == ERROR_BUFFER_TOO_SMALL) |
michael@0 | 351 | { |
michael@0 | 352 | wcsncpy(entryName, rasEntryName.szEntryName, |
michael@0 | 353 | bufferSize / sizeof(*entryName)); |
michael@0 | 354 | return NS_OK; |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 | return NS_ERROR_FAILURE; |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | // Get the number of RAS dial entries in the phonebook. |
michael@0 | 361 | int nsAutodial::NumRASEntries() |
michael@0 | 362 | { |
michael@0 | 363 | RASENTRYNAMEW rasEntryName; |
michael@0 | 364 | rasEntryName.dwSize = sizeof(rasEntryName); |
michael@0 | 365 | DWORD cb = sizeof(rasEntryName); |
michael@0 | 366 | DWORD cEntries = 0; |
michael@0 | 367 | |
michael@0 | 368 | |
michael@0 | 369 | DWORD result = |
michael@0 | 370 | RasEnumEntriesW(nullptr, nullptr, &rasEntryName, &cb, &cEntries); |
michael@0 | 371 | |
michael@0 | 372 | // ERROR_BUFFER_TOO_SMALL is OK because we only need one struct. |
michael@0 | 373 | if (result == ERROR_SUCCESS || result == ERROR_BUFFER_TOO_SMALL) |
michael@0 | 374 | { |
michael@0 | 375 | return (int)cEntries; |
michael@0 | 376 | } |
michael@0 | 377 | |
michael@0 | 378 | return 0; |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | // Get the name of the default dial entry. |
michael@0 | 382 | nsresult nsAutodial::GetDefaultEntryName(wchar_t* entryName, int bufferSize) |
michael@0 | 383 | { |
michael@0 | 384 | // No RAS dialup entries. |
michael@0 | 385 | if (mNumRASConnectionEntries <= 0) |
michael@0 | 386 | { |
michael@0 | 387 | return NS_ERROR_FAILURE; |
michael@0 | 388 | } |
michael@0 | 389 | |
michael@0 | 390 | // Single RAS dialup entry. Use it as the default to autodial. |
michael@0 | 391 | if (mNumRASConnectionEntries == 1) |
michael@0 | 392 | { |
michael@0 | 393 | return GetFirstEntryName(entryName, bufferSize); |
michael@0 | 394 | } |
michael@0 | 395 | |
michael@0 | 396 | // Multiple RAS dialup entries. If a default configured in the registry, |
michael@0 | 397 | // use it. |
michael@0 | 398 | // |
michael@0 | 399 | // For Windows XP: HKCU/Software/Microsoft/RAS Autodial/Default/DefaultInternet. |
michael@0 | 400 | // or HKLM/Software/Microsoft/RAS Autodial/Default/DefaultInternet. |
michael@0 | 401 | |
michael@0 | 402 | const wchar_t* key = L"Software\\Microsoft\\RAS Autodial\\Default"; |
michael@0 | 403 | const wchar_t* val = L"DefaultInternet"; |
michael@0 | 404 | |
michael@0 | 405 | HKEY hKey = 0; |
michael@0 | 406 | LONG result = 0; |
michael@0 | 407 | |
michael@0 | 408 | |
michael@0 | 409 | // Try HKCU first. |
michael@0 | 410 | result = ::RegOpenKeyExW( |
michael@0 | 411 | HKEY_CURRENT_USER, |
michael@0 | 412 | key, |
michael@0 | 413 | 0, |
michael@0 | 414 | KEY_READ, |
michael@0 | 415 | &hKey); |
michael@0 | 416 | |
michael@0 | 417 | if (result != ERROR_SUCCESS) |
michael@0 | 418 | { |
michael@0 | 419 | // If not present, try HKLM. |
michael@0 | 420 | result = ::RegOpenKeyExW( |
michael@0 | 421 | HKEY_LOCAL_MACHINE, |
michael@0 | 422 | key, |
michael@0 | 423 | 0, |
michael@0 | 424 | KEY_READ, |
michael@0 | 425 | &hKey); |
michael@0 | 426 | |
michael@0 | 427 | if (result != ERROR_SUCCESS) |
michael@0 | 428 | { |
michael@0 | 429 | return NS_ERROR_FAILURE; |
michael@0 | 430 | } |
michael@0 | 431 | } |
michael@0 | 432 | |
michael@0 | 433 | |
michael@0 | 434 | DWORD entryType = 0; |
michael@0 | 435 | DWORD buffSize = bufferSize; |
michael@0 | 436 | |
michael@0 | 437 | result = ::RegQueryValueExW(hKey, |
michael@0 | 438 | val, |
michael@0 | 439 | nullptr, |
michael@0 | 440 | &entryType, |
michael@0 | 441 | (LPBYTE)entryName, |
michael@0 | 442 | &buffSize); |
michael@0 | 443 | |
michael@0 | 444 | ::RegCloseKey(hKey); |
michael@0 | 445 | |
michael@0 | 446 | |
michael@0 | 447 | if (result != ERROR_SUCCESS) |
michael@0 | 448 | { |
michael@0 | 449 | // Results in a prompt for which to use at dial time. |
michael@0 | 450 | return NS_ERROR_FAILURE; |
michael@0 | 451 | } |
michael@0 | 452 | |
michael@0 | 453 | return NS_OK; |
michael@0 | 454 | } |
michael@0 | 455 | |
michael@0 | 456 | |
michael@0 | 457 | // Determine if the autodial service is running on this PC. |
michael@0 | 458 | bool nsAutodial::IsAutodialServiceRunning() |
michael@0 | 459 | { |
michael@0 | 460 | nsAutoServiceHandle hSCManager(OpenSCManager(nullptr, |
michael@0 | 461 | SERVICES_ACTIVE_DATABASE, |
michael@0 | 462 | SERVICE_QUERY_STATUS)); |
michael@0 | 463 | |
michael@0 | 464 | if (hSCManager == nullptr) |
michael@0 | 465 | { |
michael@0 | 466 | LOGE(("Autodial: failed to open service control manager. Error %d.", |
michael@0 | 467 | ::GetLastError())); |
michael@0 | 468 | |
michael@0 | 469 | return false; |
michael@0 | 470 | } |
michael@0 | 471 | |
michael@0 | 472 | nsAutoServiceHandle hService(OpenServiceW(hSCManager, |
michael@0 | 473 | L"RasAuto", |
michael@0 | 474 | SERVICE_QUERY_STATUS)); |
michael@0 | 475 | |
michael@0 | 476 | if (hSCManager == nullptr) |
michael@0 | 477 | { |
michael@0 | 478 | LOGE(("Autodial: failed to open RasAuto service.")); |
michael@0 | 479 | return false; |
michael@0 | 480 | } |
michael@0 | 481 | |
michael@0 | 482 | SERVICE_STATUS status; |
michael@0 | 483 | if (!QueryServiceStatus(hService, &status)) |
michael@0 | 484 | { |
michael@0 | 485 | LOGE(("Autodial: ::QueryServiceStatus() failed. Error: %d", |
michael@0 | 486 | ::GetLastError())); |
michael@0 | 487 | |
michael@0 | 488 | return false; |
michael@0 | 489 | } |
michael@0 | 490 | |
michael@0 | 491 | return (status.dwCurrentState == SERVICE_RUNNING); |
michael@0 | 492 | } |
michael@0 | 493 | |
michael@0 | 494 | // Add the specified address to the autodial directory. |
michael@0 | 495 | bool nsAutodial::AddAddressToAutodialDirectory(char16ptr_t hostName) |
michael@0 | 496 | { |
michael@0 | 497 | // First see if there is already a db entry for this address. |
michael@0 | 498 | RASAUTODIALENTRYW autodialEntry; |
michael@0 | 499 | autodialEntry.dwSize = sizeof(autodialEntry); |
michael@0 | 500 | DWORD size = sizeof(autodialEntry); |
michael@0 | 501 | DWORD entries = 0; |
michael@0 | 502 | |
michael@0 | 503 | DWORD result = RasGetAutodialAddressW(hostName, |
michael@0 | 504 | nullptr, |
michael@0 | 505 | &autodialEntry, |
michael@0 | 506 | &size, |
michael@0 | 507 | &entries); |
michael@0 | 508 | |
michael@0 | 509 | // If there is already at least 1 entry in db for this address, return. |
michael@0 | 510 | if (result != ERROR_FILE_NOT_FOUND) |
michael@0 | 511 | { |
michael@0 | 512 | LOGD(("Autodial: Address %s already in autodial db.", hostName)); |
michael@0 | 513 | return false; |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | autodialEntry.dwSize = sizeof(autodialEntry); |
michael@0 | 517 | autodialEntry.dwFlags = 0; |
michael@0 | 518 | autodialEntry.dwDialingLocation = mAutodialServiceDialingLocation; |
michael@0 | 519 | GetDefaultEntryName(autodialEntry.szEntry, sizeof(autodialEntry.szEntry)); |
michael@0 | 520 | |
michael@0 | 521 | result = RasSetAutodialAddressW(hostName, |
michael@0 | 522 | 0, |
michael@0 | 523 | &autodialEntry, |
michael@0 | 524 | sizeof(autodialEntry), |
michael@0 | 525 | 1); |
michael@0 | 526 | |
michael@0 | 527 | if (result != ERROR_SUCCESS) |
michael@0 | 528 | { |
michael@0 | 529 | LOGE(("Autodial ::RasSetAutodialAddress failed result %d.", result)); |
michael@0 | 530 | return false; |
michael@0 | 531 | } |
michael@0 | 532 | |
michael@0 | 533 | LOGD(("Autodial: Added address %s to RAS autodial db for entry %s.", |
michael@0 | 534 | hostName, NS_ConvertUTF16toUTF8(autodialEntry.szEntry).get())); |
michael@0 | 535 | |
michael@0 | 536 | return true; |
michael@0 | 537 | } |
michael@0 | 538 | |
michael@0 | 539 | // Get the current TAPI dialing location. |
michael@0 | 540 | int nsAutodial::GetCurrentLocation() |
michael@0 | 541 | { |
michael@0 | 542 | HKEY hKey = 0; |
michael@0 | 543 | LONG result = ::RegOpenKeyExW( |
michael@0 | 544 | HKEY_LOCAL_MACHINE, |
michael@0 | 545 | L"Software\\Microsoft\\Windows\\CurrentVersion\\Telephony\\Locations", |
michael@0 | 546 | 0, |
michael@0 | 547 | KEY_READ, |
michael@0 | 548 | &hKey); |
michael@0 | 549 | |
michael@0 | 550 | if (result != ERROR_SUCCESS) |
michael@0 | 551 | { |
michael@0 | 552 | LOGE(("Autodial: Error opening reg key ...CurrentVersion\\Telephony\\Locations")); |
michael@0 | 553 | return -1; |
michael@0 | 554 | } |
michael@0 | 555 | |
michael@0 | 556 | DWORD entryType = 0; |
michael@0 | 557 | DWORD location = 0; |
michael@0 | 558 | DWORD paramSize = sizeof(DWORD); |
michael@0 | 559 | |
michael@0 | 560 | result = ::RegQueryValueExW(hKey, L"CurrentID", nullptr, &entryType, (LPBYTE)&location, ¶mSize); |
michael@0 | 561 | if (result != ERROR_SUCCESS) |
michael@0 | 562 | { |
michael@0 | 563 | ::RegCloseKey(hKey); |
michael@0 | 564 | LOGE(("Autodial: Error reading reg value CurrentID.")); |
michael@0 | 565 | return -1; |
michael@0 | 566 | } |
michael@0 | 567 | |
michael@0 | 568 | ::RegCloseKey(hKey); |
michael@0 | 569 | return location; |
michael@0 | 570 | |
michael@0 | 571 | } |
michael@0 | 572 | |
michael@0 | 573 | // Check to see if autodial for the specified location is enabled. |
michael@0 | 574 | bool nsAutodial::IsAutodialServiceEnabled(int location) |
michael@0 | 575 | { |
michael@0 | 576 | if (location < 0) |
michael@0 | 577 | return false; |
michael@0 | 578 | |
michael@0 | 579 | BOOL enabled; |
michael@0 | 580 | if (RasGetAutodialEnableW(location, &enabled) != ERROR_SUCCESS) |
michael@0 | 581 | { |
michael@0 | 582 | LOGE(("Autodial: Error calling RasGetAutodialEnable()")); |
michael@0 | 583 | return false; |
michael@0 | 584 | } |
michael@0 | 585 | |
michael@0 | 586 | return enabled; |
michael@0 | 587 | } |