Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removal of Using Namespace std; #293

Merged
merged 7 commits into from
Jan 2, 2025
152 changes: 74 additions & 78 deletions src/quicreach.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#define QUICREACH_VERSION_ONLY 1

#include <stdio.h>
#include <iostream>
#include <iomanip> /* This Header is Included for Formating Output */
#include <thread>
#include <vector>
#include <mutex>
Expand All @@ -25,8 +27,6 @@
#define QUIC_CALL
#endif

using namespace std;
nibanks marked this conversation as resolved.
Show resolved Hide resolved

const MsQuicApi* MsQuic;

// TODO - Make these public?
Expand Down Expand Up @@ -65,37 +65,37 @@
} Config;

struct ReachResults {
atomic<uint32_t> TotalCount {0};
atomic<uint32_t> ReachableCount {0};
atomic<uint32_t> TooMuchCount {0};
atomic<uint32_t> WayTooMuchCount {0};
atomic<uint32_t> MultiRttCount {0};
atomic<uint32_t> RetryCount {0};
atomic<uint32_t> IPv6Count {0};
atomic<uint32_t> Quicv2Count {0};
std::atomic<uint32_t> TotalCount {0};
std::atomic<uint32_t> ReachableCount {0};
std::atomic<uint32_t> TooMuchCount {0};
std::atomic<uint32_t> WayTooMuchCount {0};
std::atomic<uint32_t> MultiRttCount {0};
std::atomic<uint32_t> RetryCount {0};
std::atomic<uint32_t> IPv6Count {0};
std::atomic<uint32_t> Quicv2Count {0};
// Number of currently active connections.
uint32_t ActiveCount {0};
// Synchronization for active count.
mutex Mutex;
condition_variable NotifyEvent;
std::mutex Mutex;
std::condition_variable NotifyEvent;
void WaitForActiveCount() {
while (ActiveCount >= Config.Parallel) {
unique_lock<mutex> lock(Mutex);
std::unique_lock<std::mutex> lock(Mutex);
NotifyEvent.wait(lock, [this]() { return ActiveCount < Config.Parallel; });
}
}
void WaitForAll() {
while (ActiveCount) {
unique_lock<mutex> lock(Mutex);
std::unique_lock<std::mutex> lock(Mutex);
NotifyEvent.wait(lock, [this]() { return ActiveCount == 0; });
}
}
void IncActive() {
lock_guard<mutex> lock(Mutex);
std::lock_guard<std::mutex> lock(Mutex);
++ActiveCount;
}
void DecActive() {
unique_lock<mutex> lock(Mutex);
std::unique_lock<std::mutex> lock(Mutex);
ActiveCount--;
NotifyEvent.notify_all();
}
Expand All @@ -121,23 +121,22 @@

bool ParseConfig(int argc, char **argv) {
if (argc < 2 || !strcmp(argv[1], "-?") || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
printf("usage: quicreach <hostname(s)> [options...]\n"
" -a, --alpn <alpn> The ALPN to use for the handshake (def=h3)\n"
" -b, --built-in-val Use built-in TLS validation logic\n"
" -c, --csv <file> Writes CSV results to the given file\n"
" -h, --help Prints this help text\n"
" -i, --ip <address> The IP address to use\n"
" -l, --parallel <num> The numer of parallel hosts to test at once (def=1)\n"
" -m, --mtu <mtu> The initial (IPv6) MTU to use (def=1288)\n"
" -p, --port <port> The UDP port to use (def=443)\n"
" -r, --req-all Require all hostnames to succeed\n"
" -R, --repeat <time> Repeat the requests event N milliseconds\n"
" -s, --stats Print connection statistics\n"
" -S, --source <address> Specify a source IP address\n"
" -t, --timeout <time> Timeout in milliseconds to wait for each handshake\n"
" -u, --unsecure Allows unsecure connections\n"
" -v, --version Prints out the version\n"
);
std::cout << "usage: quicreach <hostname(s)> [options...]\n"
<< " -a, --alpn <alpn> The ALPN to use for the handshake (def=h3)\n"
<< " -b, --built-in-val Use built-in TLS validation logic\n"
<< " -c, --csv <file> Writes CSV results to the given file\n"
<< " -h, --help Prints this help text\n"
<< " -i, --ip <address> The IP address to use\n"
<< " -l, --parallel <num> The numer of parallel hosts to test at once (def=1)\n"
<< " -m, --mtu <mtu> The initial (IPv6) MTU to use (def=1288)\n"
<< " -p, --port <port> The UDP port to use (def=443)\n"
<< " -r, --req-all Require all hostnames to succeed\n"
<< " -R, --repeat <time> Repeat the requests event N milliseconds\n"
<< " -s, --stats Print connection statistics\n"
<< " -S, --source <address> Specify a source IP address\n"
<< " -t, --timeout <time> Timeout in milliseconds to wait for each handshake\n"
<< " -u, --unsecure Allows unsecure connections\n"
<< " -v, --version Prints out the version\n";
return false;
}

Expand All @@ -146,59 +145,59 @@
AddHostName(argv[i]);

} else if (!strcmp(argv[i], "--alpn") || !strcmp(argv[i], "-a")) {
if (++i >= argc) { printf("Missing ALPN string\n"); return false; }
if (++i >= argc) { std::cout << "Missing ALPN string\n"; return false; }
Fixed Show fixed Hide fixed
Config.Alpn = argv[i];

} else if (!strcmp(argv[i], "--built-in-val") || !strcmp(argv[i], "-b")) {
Config.CredFlags |= QUIC_CREDENTIAL_FLAG_USE_TLS_BUILTIN_CERTIFICATE_VALIDATION;

} else if (!strcmp(argv[i], "--csv") || !strcmp(argv[i], "-c")) {
if (++i >= argc) { printf("Missing file name\n"); return false; }
if (++i >= argc) { std::cout << "Missing file name\n"; return false; }
Fixed Show fixed Hide fixed
Config.OutCsvFile = argv[i];

} else if (!strcmp(argv[i], "--mtu") || !strcmp(argv[i], "-m")) {
if (++i >= argc) { printf("Missing MTU value\n"); return false; }
if (++i >= argc) { std::cout << "Missing MTU value\n"; return false; }
Fixed Show fixed Hide fixed
Config.Settings.SetMinimumMtu((uint16_t)atoi(argv[i]));

} else if (!strcmp(argv[i], "--ip") || !strcmp(argv[i], "-i")) {
if (++i >= argc) { printf("Missing IP address\n"); return false; }
if (++i >= argc) { std::cout << "Missing IP address\n"; return false; }
Fixed Show fixed Hide fixed
if (!QuicAddrFromString(argv[i], 0, &Config.Address.SockAddr)) {
printf("Invalid address arg\n"); return false;
std::cout << "Invalid address arg\n"; return false;
}

} else if (!strcmp(argv[i], "--parallel") || !strcmp(argv[i], "-l")) {
if (++i >= argc) { printf("Missing parallel number\n"); return false; }
if (++i >= argc) { std::cout << "Missing parallel number\n"; return false; }
Fixed Show fixed Hide fixed
Config.Parallel = (uint32_t)atoi(argv[i]);

} else if (!strcmp(argv[i], "--port") || !strcmp(argv[i], "-p")) {
if (++i >= argc) { printf("Missing port number\n"); return false; }
if (++i >= argc) { std::cout << "Missing port number\n"; return false; }
Fixed Show fixed Hide fixed
Config.Port = (uint16_t)atoi(argv[i]);

} else if (!strcmp(argv[i], "--stats") || !strcmp(argv[i], "-s")) {
Config.PrintStatistics = true;

} else if (!strcmp(argv[i], "--source") || !strcmp(argv[i], "-S")) {
if (++i >= argc) { printf("Missing source address\n"); return false; }
if (++i >= argc) { std::cout << "Missing source address\n"; return false; }
Fixed Show fixed Hide fixed
if (!QuicAddrFromString(argv[i], 0, &Config.SourceAddress.SockAddr)) {
printf("Invalid source address arg\n"); return false;
std::cout << "Invalid source address arg\n"; return false;
}

} else if (!strcmp(argv[i], "--req-all") || !strcmp(argv[i], "-r")) {
Config.RequireAll = true;

} else if (!strcmp(argv[i], "--repeat") || !strcmp(argv[i], "-R")) {
if (++i >= argc) { printf("Missing repeat arg\n"); return false; }
if (++i >= argc) { std::cout << "Missing repeat arg\n"; return false; }
Fixed Show fixed Hide fixed
Config.Repeat = (uint32_t)atoi(argv[i]);

} else if (!strcmp(argv[i], "--timeout") || !strcmp(argv[i], "-t")) {
if (++i >= argc) { printf("Missing timeout arg\n"); return false; }
if (++i >= argc) { std::cout << "Missing timeout arg\n"; return false; }
Fixed Show fixed Hide fixed
Config.Timeout = (uint32_t)atoi(argv[i]);

} else if (!strcmp(argv[i], "--unsecure") || !strcmp(argv[i], "-u")) {
Config.CredFlags |= QUIC_CREDENTIAL_FLAG_NO_CERTIFICATE_VALIDATION;

} else if (!strcmp(argv[i], "--version") || !strcmp(argv[i], "-v")) {
printf("quicreach " QUICREACH_VERSION "\n");
std::cout << "quicreach " QUICREACH_VERSION "\n";
}
}

Expand Down Expand Up @@ -290,28 +289,25 @@
'\0'};
QUIC_ADDR_STR AddrStr;
QuicAddrToString(&RemoteAddr.SockAddr, &AddrStr);
unique_lock<mutex> lock(Results.Mutex);
printf("%30s %3u.%03u ms %3u.%03u ms %3u.%03u ms %u:%u %u:%u (%2.1fx) %4u %4u %s %20s %s\n",
HostName,
Stats.Rtt / 1000, Stats.Rtt % 1000,
InitialTime / 1000, InitialTime % 1000,
HandshakeTime / 1000, HandshakeTime % 1000,
(uint32_t)Stats.SendTotalPackets,
(uint32_t)Stats.RecvTotalPackets,
(uint32_t)Stats.SendTotalBytes,
(uint32_t)Stats.RecvTotalBytes,
Amplification,
Stats.HandshakeClientFlight1Bytes,
Stats.HandshakeServerFlight1Bytes,
Version == QUIC_VERSION_1 ? "v1" : "v2",
AddrStr.Address,
HandshakeTags);
std::unique_lock<std::mutex> lock(Results.Mutex);
std::cout << std::setw(30) << HostName << " "
Astabol marked this conversation as resolved.
Show resolved Hide resolved
<< std::setw(3) << Stats.Rtt / 1000 << "." << std::setfill('0') << std::setw(3) << Stats.Rtt % 1000 << std::setfill(' ') << " ms" << " "
<< std::setw(3) << InitialTime / 1000 << "." << std::setfill('0') << std::setw(3) << InitialTime % 1000 << std::setfill(' ') << " ms" << " "
<< std::setw(3) << HandshakeTime / 1000 << "." << std::setfill('0') << std::setw(3) << HandshakeTime % 1000 << std::setfill(' ') << " ms" << " "
<< (uint32_t)Stats.SendTotalPackets << ":" << (uint32_t)Stats.RecvTotalPackets << " "
<< (uint32_t)Stats.SendTotalBytes << ":" << (uint32_t)Stats.RecvTotalBytes << " "
<< "(" << std::fixed << std::setprecision(1) << Amplification << "x)" << " "
<< std::setw(4) << Stats.HandshakeClientFlight1Bytes << " "
<< std::setw(4) << Stats.HandshakeServerFlight1Bytes << " "
<< (Version == QUIC_VERSION_1 ? "v1" : "v2") << " "
<< std::setw(20) << AddrStr.Address << " "
<< HandshakeTags << std::endl;
}
}
void OnUnreachable() {
if (Config.PrintStatistics) {
unique_lock<mutex> lock(Results.Mutex);
printf("%30s\n", HostName);
std::unique_lock<std::mutex> lock(Results.Mutex);
std::cout << std::setw(30) << HostName << std::endl;
}
}
};
Expand All @@ -321,7 +317,7 @@
if (!File) {
File = fopen(Config.OutCsvFile, "a"); // Open an existing file
if (!File) {
printf("Failed to open output file: %s\n", Config.OutCsvFile);
std::cout << "Failed to open output file: " << Config.OutCsvFile << std::endl;;
return;
}
} else {
Expand All @@ -335,7 +331,7 @@
Results.TotalCount.load(), Results.ReachableCount.load(), Results.TooMuchCount.load(), Results.MultiRttCount.load(),
Results.RetryCount.load(), Results.IPv6Count.load(), Results.Quicv2Count.load(), Results.WayTooMuchCount.load());
fclose(File);
printf("\nOutput written to %s\n", Config.OutCsvFile);
std::cout << "\nOutput written to " << Config.OutCsvFile << std::endl;
}

// TODO:
Expand All @@ -345,12 +341,12 @@
bool TestReachability() {
MsQuicRegistration Registration("quicreach");
MsQuicConfiguration Configuration(Registration, Config.Alpn, Config.Settings, MsQuicCredentialConfig(Config.CredFlags));
if (!Configuration.IsValid()) { printf("Configuration initializtion failed!\n"); return false; }
if (!Configuration.IsValid()) { std::cout << "Configuration initializtion failed!\n"; return false; }
Configuration.SetVersionSettings(VersionSettings);
Configuration.SetVersionNegotiationExtEnabled();

if (Config.PrintStatistics)
printf("%30s RTT TIME_I TIME_H SEND:RECV C1 S1 VER IP\n", "SERVER");
std::cout << std::setw(30) << "SERVER" << " RTT TIME_I TIME_H SEND:RECV C1 S1 VER IP\n";

do {
for (auto HostName : Config.HostNames) {
Expand All @@ -372,21 +368,21 @@

if (Config.PrintStatistics) {
if (Results.ReachableCount > 1) {
printf("\n");
printf("%4u domain(s) attempted\n", (uint32_t)Config.HostNames.size());
printf("%4u domain(s) reachable\n", Results.ReachableCount.load());
std::cout << std::endl;
std::cout << std::setw(4) << (uint32_t)Config.HostNames.size() << " domain(s) attempted\n";
std::cout << std::setw(4) << Results.ReachableCount.load() << " domain(s) reachable\n";
if (Results.MultiRttCount)
printf("%4u domain(s) required multiple round trips (*)\n", Results.MultiRttCount.load());
std::cout << std::setw(4) << Results.MultiRttCount.load() << " domain(s) required multiple round trips (*)\n";
if (Results.TooMuchCount)
printf("%4u domain(s) exceeded amplification limits (!)\n", Results.TooMuchCount.load());
std::cout << std::setw(4) << Results.TooMuchCount.load() << " domain(s) exceeded amplification limits (!)\n";
if (Results.WayTooMuchCount)
printf("%4u domain(s) well exceeded amplification limits (5x)\n", Results.WayTooMuchCount.load());
std::cout << std::setw(4) << Results.WayTooMuchCount.load() << " domain(s) well exceeded amplification limits (5x)\n";
if (Results.RetryCount)
printf("%4u domain(s) sent RETRY packets (R)\n", Results.RetryCount.load());
std::cout << std::setw(4) << Results.RetryCount.load() << " domain(s) sent RETRY packets (R)\n";
if (Results.IPv6Count)
printf("%4u domain(s) used IPv6\n", Results.IPv6Count.load());
std::cout << std::setw(4) << Results.IPv6Count.load() << " domain(s) used IPv6\n";
if (Results.Quicv2Count)
printf("%4u domain(s) used QUIC v2\n", Results.Quicv2Count.load());
std::cout << std::setw(4) << Results.Quicv2Count.load() << " domain(s) used QUIC v2\n";
}
}

Expand All @@ -401,13 +397,13 @@

MsQuic = new (std::nothrow) MsQuicApi();
if (QUIC_FAILED(MsQuic->GetInitStatus())) {
printf("MsQuicApi failed, 0x%x\n", MsQuic->GetInitStatus());
std::cout << "MsQuicApi failed, 0x" << MsQuic->GetInitStatus() << std::endl;
Astabol marked this conversation as resolved.
Show resolved Hide resolved
return 1;
}

bool Result = TestReachability();
if (!Config.PrintStatistics) {
printf("%s\n", Result ? "Success" : "Failure");
std::cout << (Result ? "Success" : "Failure") << std::endl;
}
delete MsQuic;
return Result ? 0 : 1;
Expand Down
Loading