swift
ping.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/network/ping.h"
5 
6 #include <QProcess>
7 
8 #include "config/buildconfig.h"
9 
10 using namespace swift::config;
11 
12 namespace swift::misc::network
13 {
14  bool canPing(const QString &hostAddress)
15  {
16  if (hostAddress.isEmpty()) { return false; }
17  QProcess process;
18  process.setProgram("ping");
19  if (CBuildConfig::isRunningOnWindowsNtPlatform()) { process.setArguments({ "-n", "1", hostAddress }); }
20  else
21  {
22  // all UNIX alike
23  process.setArguments({ "-c", "1", hostAddress });
24  }
25  process.start();
26  process.waitForFinished();
27  const int rc = process.exitCode();
28  if (rc != 0) { return false; }
29 
30  const QString std = process.readAllStandardOutput();
31  const QString err = process.readAllStandardError();
32  if (std.contains("unreachable", Qt::CaseInsensitive)) { return false; }
33  if (err.contains("unreachable", Qt::CaseInsensitive)) { return false; }
34  return true;
35  }
36 
37  bool canPing(const QUrl &url)
38  {
39  if (url.isEmpty()) { return false; }
40  return canPing(url.host());
41  }
42 } // namespace swift::misc::network
static constexpr bool isRunningOnWindowsNtPlatform()
Running on Windows NT platform?