Skip to content

Commit 395985a

Browse files
committed
添加连接探测日志
1 parent 2353509 commit 395985a

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

src/core/client.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ void Client::tick() {
351351
peer.updateState(PeerState::FAILED);
352352
continue;
353353
}
354+
spdlog::info("connecting: {} {}:{} => {}:{}", Address::ipToStr(peer.tun), Address::ipToStr(this->selfInfo.ip),
355+
this->selfInfo.port, Address::ipToStr(peer.ip), peer.port);
354356
sendHeartbeat(peer);
355357
}
356358
// CONNECTED 状态下,进行超时检测,超时后清空对端信息,否则发送心跳
@@ -413,7 +415,7 @@ void Client::sendPeerConnMessage(uint32_t src, uint32_t dst, uint32_t ip, uint16
413415
message.buffer.assign((char *)(&header), sizeof(PeerConnMessage));
414416
this->ws.write(message);
415417

416-
spdlog::debug("send peer conn message: src {:x} dst {:x} ip {:x} port {}", src, dst, ip, port);
418+
spdlog::debug("send peer conn message: src {:08x} dst {:08x} ip {:08x} port {}", src, dst, ip, port);
417419
return;
418420
}
419421

@@ -716,6 +718,9 @@ int Client::handleStunResponse(const std::string &buffer) {
716718
return -1;
717719
}
718720

721+
this->selfInfo.ip = ip;
722+
this->selfInfo.port = port;
723+
719724
// 收到 STUN 响应后,向所有 PREPARING 状态的对端发送自己的公网信息,如果当前持有对端公网信息,就将状态调整为 CONNECTING,
720725
// 否则调整为 SYNCHRONIZING
721726
std::unique_lock lock(this->ipPeerMutex);
@@ -748,11 +753,11 @@ int Client::handleHeartbeatMessage(const UdpMessage &message) {
748753
std::unique_lock lock(this->ipPeerMutex);
749754
PeerInfo &peer = this->ipPeerMap[Address::netToHost(heartbeat->tun)];
750755
if (peer.ip != message.ip) {
751-
spdlog::debug("peer address does not match: {:x} {:x}", peer.ip, message.ip);
756+
spdlog::debug("peer address does not match: {:08x} {:08x}", peer.ip, message.ip);
752757
return -1;
753758
}
754759
if (peer.port != message.port) {
755-
spdlog::debug("peer port does not match, update: old {:x} new {:x}", peer.ip, message.ip);
760+
spdlog::debug("peer port does not match, update: old {:08x} new {:08x}", peer.ip, message.ip);
756761
peer.port = message.port;
757762
}
758763
peer.count = 0;
@@ -764,8 +769,8 @@ int Client::sendHeartbeat(const PeerInfo &peer) {
764769
PeerHeartbeatMessage heartbeat;
765770
heartbeat.type = PeerMessageType::HEARTBEAT;
766771
heartbeat.tun = Address::hostToNet(this->tun.getIP());
767-
heartbeat.ip = Address::hostToNet(this->stun.ip);
768-
heartbeat.port = Address::hostToNet(this->stun.port);
772+
heartbeat.ip = Address::hostToNet(this->selfInfo.ip);
773+
heartbeat.port = Address::hostToNet(this->selfInfo.port);
769774

770775
UdpMessage message;
771776
message.ip = peer.ip;

src/peer/common.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ std::string PeerInfo::getKey() const {
3434

3535
void PeerInfo::updateState(PeerState state) {
3636
if (this->state != state) {
37-
Address address;
38-
address.ipUpdate(this->tun);
39-
spdlog::info("conn state: {} {} -> {}", address.getIpStr(), getStateStr(this->state), getStateStr(state));
37+
spdlog::info("conn state: {} {} => {}", Address::ipToStr(this->tun), getStateStr(this->state), getStateStr(state));
4038
this->state = state;
4139
}
4240
}

src/utility/address.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ uint16_t Address::hostToNet(uint16_t port) {
167167
return netToHost(port);
168168
}
169169

170+
std::string Address::ipToStr(uint32_t ip) {
171+
Address address;
172+
address.ipUpdate(ip);
173+
return address.getIpStr();
174+
}
175+
170176
int Address::prefixStrToMaskStr(const std::string &prefixStr, std::string &maskStr) {
171177
uint32_t prefix = std::stoi(prefixStr);
172178
uint32_t mask = 0;

src/utility/address.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class Address {
5353
static uint16_t netToHost(uint16_t port);
5454
static uint16_t hostToNet(uint16_t port);
5555

56+
static std::string ipToStr(uint32_t ip);
57+
5658
private:
5759
int prefixStrToMaskStr(const std::string &netPrefixStr, std::string &maskStr);
5860
int prefixToMask(uint32_t prefix, uint32_t &mask);

0 commit comments

Comments
 (0)