Skip to content

Commit

Permalink
1.shortlink 线程名加上cgi名;2.去掉 v4tonatv6的无效调用
Browse files Browse the repository at this point in the history
  • Loading branch information
outman-zhou committed Mar 10, 2023
1 parent 6edbb88 commit dd2df4d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 84 deletions.
7 changes: 3 additions & 4 deletions mars/comm/network/getaddrinfo_with_timeout.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,16 @@ static void __WorkerFunc() {
}
}




int getaddrinfo_with_timeout(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res, bool& _is_timeout, unsigned long long _timeout_msec) {
xverbose_function();
//Check param

ScopedLock lock(sg_mutex);

std::string threadname(node);
threadname.append("@dns_with_timeout");

Thread thread(&__WorkerFunc, node);
Thread thread(&__WorkerFunc, threadname.c_str());
int start_ret = thread.start();

if (start_ret != 0) {
Expand Down
159 changes: 81 additions & 78 deletions mars/comm/socket/nat64_prefix_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,84 +199,87 @@ static void ReplaceNat64WithV4IP(struct in6_addr* _replaced_nat64_addr, const st
}

bool ConvertV4toNat64V6(const struct in_addr& _v4_addr, struct in6_addr& _v6_addr) {
xdebug_function();
if (ELocalIPStack_IPv6 != local_ipstack_detect()) {
xwarn2(TSF"Current Network is not ELocalIPStack_IPv6, no need GetNetworkNat64Prefix.");
return false;
}
struct addrinfo hints, *res=NULL, *res0=NULL;
int error = 0;

memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_INET6;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_ADDRCONFIG;

char v4_ip[16] = {0};
socket_inet_ntop(AF_INET, &_v4_addr, v4_ip, sizeof(v4_ip));
bool is_timeout = false;
#ifdef __APPLE__
if (publiccomponent_GetSystemVersion() >= 9.2f) {//higher than iOS9.2
error = getaddrinfo_with_timeout(v4_ip, NULL, &hints, &res0, is_timeout, 2000);
} else {//lower than iOS9.2 or other platform
#endif
error = mars::comm::getaddrinfo_with_timeout("ipv4only.arpa", NULL, &hints, &res0, is_timeout, 2000);
#ifdef __APPLE__
}
#endif

bool ret = false;
if (error==0) {
for (res = res0; res; res = res->ai_next) {
char ip_buf[64] = {0};

if (AF_INET6 == res->ai_family) {
#ifdef __APPLE__
if (publiccomponent_GetSystemVersion() >= 9.2f) { //higher than iOS9.2
//copy all 16 bytes
memcpy ( (char*)&_v6_addr, (char*)&((((sockaddr_in6*)res->ai_addr)->sin6_addr).s6_addr32), 16);
ret = true;
break;
} else { //lower than iOS9.2 or other platform
#endif

if (IsNat64AddrValid((struct in6_addr*)&(((sockaddr_in6*)res->ai_addr)->sin6_addr))) {
ReplaceNat64WithV4IP((struct in6_addr*)&(((sockaddr_in6*)res->ai_addr)->sin6_addr) , &_v4_addr);
#ifdef WIN32
memcpy ( (char*)&_v6_addr, (char*)&((((sockaddr_in6*)res->ai_addr)->sin6_addr).u), 16);
#else
memcpy ( (char*)&_v6_addr, (char*)&((((sockaddr_in6*)res->ai_addr)->sin6_addr).s6_addr16), 16);
#endif
const char* ip_str = socket_inet_ntop(AF_INET6, &_v6_addr, ip_buf, sizeof(ip_buf));
xdebug2(TSF"AF_INET6 v4_ip=%_, nat64 ip_str = %_", v4_ip, ip_str);
ret = true;
break;
} else {
xerror2(TSF"Nat64 addr invalid, =%_",
strutil::Hex2Str((char*)&(((sockaddr_in6*)res->ai_addr)->sin6_addr), 16));
ret = false;
}
#ifdef __APPLE__
}
#endif

} else if (AF_INET == res->ai_family){
const char* ip_str = socket_inet_ntop(AF_INET, &(((sockaddr_in*)res->ai_addr)->sin_addr), ip_buf, sizeof(ip_buf));
xinfo2(TSF"AF_INET ip_str = %_", ip_str);
ret = false;
} else {
xerror2(TSF"invalid ai_family = %_", res->ai_family);
ret = false;
}

}
} else {
xerror2(TSF" getaddrinfo error = %_, res0:@%_", error, res0);
ret = false;
}
if (NULL != res0)
freeaddrinfo(res0);
return ret;
// never connect success when use V4toV6Nat convert.
return false;
//
// xdebug_function();
// if (ELocalIPStack_IPv6 != local_ipstack_detect()) {
// xwarn2(TSF"Current Network is not ELocalIPStack_IPv6, no need GetNetworkNat64Prefix.");
// return false;
// }
// struct addrinfo hints, *res=NULL, *res0=NULL;
// int error = 0;
//
// memset(&hints, 0, sizeof(hints));
// hints.ai_family = PF_INET6;
// hints.ai_socktype = SOCK_STREAM;
// hints.ai_flags = AI_ADDRCONFIG;
//
// char v4_ip[16] = {0};
// socket_inet_ntop(AF_INET, &_v4_addr, v4_ip, sizeof(v4_ip));
// bool is_timeout = false;
//#ifdef __APPLE__
// if (publiccomponent_GetSystemVersion() >= 9.2f) {//higher than iOS9.2
// error = getaddrinfo_with_timeout(v4_ip, NULL, &hints, &res0, is_timeout, 2000);
// } else {//lower than iOS9.2 or other platform
//#endif
// error = mars::comm::getaddrinfo_with_timeout("ipv4only.arpa", NULL, &hints, &res0, is_timeout, 2000);
//#ifdef __APPLE__
// }
//#endif
//
// bool ret = false;
// if (error==0) {
// for (res = res0; res; res = res->ai_next) {
// char ip_buf[64] = {0};
//
// if (AF_INET6 == res->ai_family) {
//#ifdef __APPLE__
// if (publiccomponent_GetSystemVersion() >= 9.2f) { //higher than iOS9.2
// //copy all 16 bytes
// memcpy ( (char*)&_v6_addr, (char*)&((((sockaddr_in6*)res->ai_addr)->sin6_addr).s6_addr32), 16);
// ret = true;
// break;
// } else { //lower than iOS9.2 or other platform
//#endif
//
// if (IsNat64AddrValid((struct in6_addr*)&(((sockaddr_in6*)res->ai_addr)->sin6_addr))) {
// ReplaceNat64WithV4IP((struct in6_addr*)&(((sockaddr_in6*)res->ai_addr)->sin6_addr) , &_v4_addr);
//#ifdef WIN32
// memcpy ( (char*)&_v6_addr, (char*)&((((sockaddr_in6*)res->ai_addr)->sin6_addr).u), 16);
//#else
// memcpy ( (char*)&_v6_addr, (char*)&((((sockaddr_in6*)res->ai_addr)->sin6_addr).s6_addr16), 16);
//#endif
// const char* ip_str = socket_inet_ntop(AF_INET6, &_v6_addr, ip_buf, sizeof(ip_buf));
// xdebug2(TSF"AF_INET6 v4_ip=%_, nat64 ip_str = %_", v4_ip, ip_str);
// ret = true;
// break;
// } else {
// xerror2(TSF"Nat64 addr invalid, =%_",
// strutil::Hex2Str((char*)&(((sockaddr_in6*)res->ai_addr)->sin6_addr), 16));
// ret = false;
// }
//#ifdef __APPLE__
// }
//#endif
//
// } else if (AF_INET == res->ai_family){
// const char* ip_str = socket_inet_ntop(AF_INET, &(((sockaddr_in*)res->ai_addr)->sin_addr), ip_buf, sizeof(ip_buf));
// xinfo2(TSF"AF_INET ip_str = %_", ip_str);
// ret = false;
// } else {
// xerror2(TSF"invalid ai_family = %_", res->ai_family);
// ret = false;
// }
//
// }
// } else {
// xerror2(TSF" getaddrinfo error = %_, res0:@%_", error, res0);
// ret = false;
// }
// if (NULL != res0)
// freeaddrinfo(res0);
// return ret;

}

Expand Down
2 changes: 1 addition & 1 deletion mars/stn/src/longlink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ LongLink::LongLink(const mq::MessageQueue_t& _messagequeueid, NetSource& _netsou
: asyncreg_(MessageQueue::InstallAsyncHandler(_messagequeueid))
, netsource_(_netsource)
, config_(_config)
, thread_(boost::bind(&LongLink::__Run, this), XLOGGER_TAG "::lonklink")
, thread_(boost::bind(&LongLink::__Run, this), XLOGGER_TAG "::longlink")
, connectstatus_(kConnectIdle)
, disconnectinternalcode_(kNone)
, identifychecker_(_encoder, _config.name, Task::kChannelMinorLong == _config.link_type)
Expand Down
13 changes: 12 additions & 1 deletion mars/stn/src/shortlink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,24 @@ class ShortLinkConnectObserver : public MComplexConnect {
};

}}

namespace internal{
std::string threadName(const std::string& fullcgi){
auto pos = fullcgi.find_last_of("/");
if (pos != std::string::npos){
return fullcgi.substr(pos + 1, 32) + "@shortlink";
}
return fullcgi + "@shortlink";
}
};

///////////////////////////////////////////////////////////////////////////////////////
ShortLink::ShortLink(MessageQueue::MessageQueue_t _messagequeueid, NetSource& _netsource, const Task& _task, bool _use_proxy, std::unique_ptr<SocketOperator> _operator)
: asyncreg_(MessageQueue::InstallAsyncHandler(_messagequeueid))
, net_source_(_netsource)
, socketOperator_(_operator == nullptr ? std::make_unique<TcpSocketOperator>(std::make_shared<ShortLinkConnectObserver>(*this)) : std::move(_operator))
, task_(_task)
, thread_(boost::bind(&ShortLink::__Run, this), XLOGGER_TAG "::shortlink")
, thread_(boost::bind(&ShortLink::__Run, this), internal::threadName(_task.cgi).c_str())
, use_proxy_(_use_proxy)
, tracker_(shortlink_tracker::Create())
, is_keep_alive_(CheckKeepAlive(_task))
Expand Down

0 comments on commit dd2df4d

Please sign in to comment.