Skip to content

Commit 98f9347

Browse files
committed
Add clang-format
1 parent b13d6b2 commit 98f9347

17 files changed

+492
-391
lines changed

.clang-format

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BasedOnStyle: llvm
2+
IndentWidth: 4

.github/workflows/builtAndTest.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ env:
99
PCAPPLUSPLUS_LATEST_VERSION: 24.09
1010

1111
jobs:
12-
build-and-test:
12+
build-and-test-client:
1313
strategy:
1414
matrix:
1515
api-level: [30, 35]
@@ -111,3 +111,18 @@ jobs:
111111
cd server/linux
112112
cmake -S . -B build
113113
cmake --build build
114+
115+
cpp-lint-format:
116+
runs-on: ubuntu-latest
117+
118+
steps:
119+
- name: Checkout code
120+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
121+
with:
122+
submodules: true
123+
124+
- name: Run clang-format
125+
uses: jidicula/clang-format-action@v4.14.0
126+
with:
127+
clang-format-version: "19"
128+
exclude-regex: "(^|/)(app/libs|server/linux/libs)/"

app/src/main/cpp/PacketAnalyzer.cpp

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
#include <jni.h>
2-
#include <RawPacket.h>
31
#include <DnsLayer.h>
4-
#include <SSLLayer.h>
5-
#include <PacketUtils.h>
62
#include <Packet.h>
3+
#include <PacketUtils.h>
4+
#include <RawPacket.h>
5+
#include <SSLLayer.h>
6+
#include <android/log.h>
7+
#include <iomanip>
78
#include <iostream>
8-
#include <vector>
9-
#include <string>
9+
#include <jni.h>
1010
#include <sstream>
11-
#include <iomanip>
12-
#include <android/log.h>
11+
#include <string>
12+
#include <vector>
1313

1414
#define LOG_TAG "ToyVpnNativeCode"
1515

1616
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
1717

18-
void analyzeDnsPacket(const pcpp::Packet& packet, std::ostringstream& jsonStream) {
18+
void analyzeDnsPacket(const pcpp::Packet &packet,
19+
std::ostringstream &jsonStream) {
1920
auto dnsLayer = packet.getLayerOfType<pcpp::DnsLayer>();
2021

2122
if (dnsLayer == nullptr) {
2223
return;
2324
}
2425

25-
if (dnsLayer->getDnsHeader()->queryOrResponse == 0 && dnsLayer->getQueryCount() > 0) {
26+
if (dnsLayer->getDnsHeader()->queryOrResponse == 0 &&
27+
dnsLayer->getQueryCount() > 0) {
2628
auto dnsQuery = dnsLayer->getFirstQuery();
2729
jsonStream << R"("dnsQuery":")" << dnsQuery->getName() << "\",";
2830
}
2931
}
3032

31-
void analyzeTlsPacket(const pcpp::Packet& packet, std::ostringstream& jsonStream) {
33+
void analyzeTlsPacket(const pcpp::Packet &packet,
34+
std::ostringstream &jsonStream) {
3235
auto tlsLayer = packet.getLayerOfType<pcpp::SSLLayer>();
3336

3437
if (tlsLayer == nullptr) {
@@ -39,55 +42,62 @@ void analyzeTlsPacket(const pcpp::Packet& packet, std::ostringstream& jsonStream
3942
return;
4043
}
4144

42-
auto handshakeLayer = dynamic_cast<pcpp::SSLHandshakeLayer*>(tlsLayer);
45+
auto handshakeLayer = dynamic_cast<pcpp::SSLHandshakeLayer *>(tlsLayer);
4346
if (handshakeLayer == nullptr) {
4447
return;
4548
}
4649

4750
auto clientHelloMessage =
48-
handshakeLayer->getHandshakeMessageOfType<pcpp::SSLClientHelloMessage>();
51+
handshakeLayer
52+
->getHandshakeMessageOfType<pcpp::SSLClientHelloMessage>();
4953

50-
if (clientHelloMessage != nullptr)
51-
{
54+
if (clientHelloMessage != nullptr) {
5255
auto sniExt =
53-
clientHelloMessage->getExtensionOfType<pcpp::SSLServerNameIndicationExtension>();
56+
clientHelloMessage
57+
->getExtensionOfType<pcpp::SSLServerNameIndicationExtension>();
5458
if (sniExt != nullptr) {
55-
jsonStream << R"("tlsServerName":")" << sniExt->getHostName() << "\",";
59+
jsonStream << R"("tlsServerName":")" << sniExt->getHostName()
60+
<< "\",";
5661
}
5762
}
5863

5964
auto serverHelloMessage =
60-
handshakeLayer->getHandshakeMessageOfType<pcpp::SSLServerHelloMessage>();
65+
handshakeLayer
66+
->getHandshakeMessageOfType<pcpp::SSLServerHelloMessage>();
6167

62-
if (serverHelloMessage != nullptr)
63-
{
64-
jsonStream << "\"tlsVersion\":" << serverHelloMessage->getHandshakeVersion().asUInt() << ",";
68+
if (serverHelloMessage != nullptr) {
69+
jsonStream << "\"tlsVersion\":"
70+
<< serverHelloMessage->getHandshakeVersion().asUInt() << ",";
6571
}
6672
}
6773

6874
extern "C" {
6975
JNIEXPORT jbyteArray JNICALL
70-
Java_com_pcapplusplus_toyvpn_pcapplusplus_PcapPlusPlusInterface_analyzePacketNative(JNIEnv *env, jobject obj, jbyteArray packetData) {
76+
Java_com_pcapplusplus_toyvpn_pcapplusplus_PcapPlusPlusInterface_analyzePacketNative(
77+
JNIEnv *env, jobject obj, jbyteArray packetData) {
7178
jsize length = env->GetArrayLength(packetData);
7279
jbyte *rawPacketData = env->GetByteArrayElements(packetData, nullptr);
7380

7481
std::vector<uint8_t> packetVector(rawPacketData, rawPacketData + length);
7582

76-
// TODO: Check if there's a better way to get the time now and convert to timespec
83+
// TODO: Check if there's a better way to get the time now and convert to
84+
// timespec
7785
auto now = std::chrono::system_clock::now();
7886
auto duration = now.time_since_epoch();
7987
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
80-
auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(duration) - seconds;
88+
auto nanoseconds =
89+
std::chrono::duration_cast<std::chrono::nanoseconds>(duration) -
90+
seconds;
8191
struct timespec ts{};
8292
ts.tv_sec = seconds.count();
8393
ts.tv_nsec = nanoseconds.count();
8494

85-
pcpp::RawPacket rawPacket(packetVector.data(), packetVector.size(), ts, false,
86-
pcpp::LINKTYPE_DLT_RAW1);
95+
pcpp::RawPacket rawPacket(packetVector.data(), packetVector.size(), ts,
96+
false, pcpp::LINKTYPE_DLT_RAW1);
8797

8898
pcpp::Packet packet(&rawPacket);
8999

90-
// LOGW("%s", packet.toString().c_str());
100+
// LOGW("%s", packet.toString().c_str());
91101

92102
std::ostringstream jsonStream;
93103
jsonStream << "{";
@@ -135,7 +145,9 @@ Java_com_pcapplusplus_toyvpn_pcapplusplus_PcapPlusPlusInterface_analyzePacketNat
135145

136146
jbyteArray result = env->NewByteArray(jsonString.length());
137147

138-
env->SetByteArrayRegion(result, 0, jsonString.length(), reinterpret_cast<const jbyte*>(jsonString.c_str()));
148+
env->SetByteArrayRegion(
149+
result, 0, jsonString.length(),
150+
reinterpret_cast<const jbyte *>(jsonString.c_str()));
139151

140152
env->ReleaseByteArrayElements(packetData, rawPacketData, 0);
141153

0 commit comments

Comments
 (0)