Skip to content

Commit

Permalink
Solved problems with dns
Browse files Browse the repository at this point in the history
  • Loading branch information
lanstat committed May 22, 2024
1 parent ea92f99 commit 9554e9e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 47 deletions.
22 changes: 6 additions & 16 deletions src/Dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,7 @@
Dns::Dns() {
ipv4_regex_ =
std::regex("^(((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4})(:\\d+)*$");
ipv6_regex_ = std::regex(
"^%5B(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:"
"|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:["
"0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|"
"([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,"
"2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|"
":((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]"
"{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-"
"9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:"
")"
"{1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-"
"4]"
"|1{0,1}[0-9]){0,1}[0-9]))%5D(:\\d+)*$");
ipv6_regex_ = std::regex("\\[([a-f0-9:]+:+)+[a-f0-9]+\\](:\\d+)*");

LoadHostFile();
}
Expand Down Expand Up @@ -66,7 +54,9 @@ void Dns::AddFetchAAAARequest(struct Request *request, bool isHttps) {
std::string host((char *)request->iov[2].iov_base);
host = host.substr(1);
std::size_t pos = host.find("/");
host = host.substr(0, pos);
if (pos != std::string::npos) {
host = host.substr(0, pos);
}
int port = 80;
if (isHttps) {
port = 443;
Expand Down Expand Up @@ -122,9 +112,9 @@ void Dns::AddFetchAAAARequest(struct Request *request, bool isHttps) {
}

if (std::regex_match(host, ipv6_regex_)) {
pos = host.find("%5D");
pos = host.find("]");
std::string tmp = host.substr(pos);
host = host.substr(3, pos - 3);
host = host.substr(1, pos - 1);
pos = tmp.find(":");
if (pos != std::string::npos) {
port = std::stoi(tmp.substr(pos + 1));
Expand Down
38 changes: 19 additions & 19 deletions src/DnsSeeker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,19 +597,18 @@ bool DnsSeeker::read8Bits(uint8_t &var, const char *const data, const int &size,
return true;
}

//bool DnsSeeker::read16Bits(uint16_t &var, const char *const data,
//const int &size, int &pos) {
//uint16_t t = 0;
//read16BitsRaw(t, data, size, pos);
//var = be16toh(t);
//return var;
// bool DnsSeeker::read16Bits(uint16_t &var, const char *const data,
// const int &size, int &pos) {
// uint16_t t = 0;
// read16BitsRaw(t, data, size, pos);
// var = be16toh(t);
// return var;
//}

bool DnsSeeker::read16Bits(uint16_t &var, const char *const data,
const int &size, int &pos) {
uint16_t t = 0;
if(!read16BitsRaw(t, data, size, pos))
return false;
if (!read16BitsRaw(t, data, size, pos)) return false;
var = be16toh(t);
return true;
}
Expand Down Expand Up @@ -824,24 +823,25 @@ void DnsSeeker::removeQuery(const uint16_t &id, const bool &withNextDueTime) {
const Query &query = queryList.at(id);
if (withNextDueTime) {
if (queryByNextDueTime.find(query.nextRetry) ==
queryByNextDueTime.cend()) {
queryByNextDueTime.cend())
std::cerr << __FILE__ << ":" << __LINE__ << " query " << id
<< " not found into queryByNextDueTime: "
<< query.nextRetry << std::endl;
}
queryByNextDueTime.erase(query.nextRetry);
}
if (queryByNextDueTime.find(query.nextRetry) == queryByNextDueTime.cend()) {
Log(__FILE__, __LINE__, Log::kError)
<< "query " << id
<< " not found into queryListByHost: " << query.host;
}

if (queryListByHost.find(query.host) == queryListByHost.cend())
std::cerr << __FILE__ << ":" << __LINE__ << " query " << id
<< " not found into queryListByHost: " << query.host
<< " into Dns::removeQuery()" << std::endl;
queryListByHost.erase(query.host);
if (queryByNextDueTime.find(query.nextRetry) == queryByNextDueTime.cend()) {
Log(__FILE__, __LINE__, Log::kError)
<< "query " << id << " not found into queryList";
}

if (queryList.find(id) == queryList.cend())
std::cerr << __FILE__ << ":" << __LINE__ << " query " << id
<< " not found into queryList: " << id
<< " into Dns::removeQuery()" << std::endl;
queryList.erase(id);

if (httpInProgress > 0) httpInProgress--;
}

Expand Down
28 changes: 17 additions & 11 deletions src/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ bool HttpClient::HandleFetchRequest(struct Request *inner, bool ipv4) {
std::string url((char *)inner->iov[2].iov_base);
url = url.substr(1);
std::size_t pos = url.find("/");
std::string host = url.substr(0, pos);
std::string query = url.substr(pos);
std::string host;
std::string query;

Log(__FILE__, __LINE__, Log::kDebug) << "Requesting: " << query;
if (pos != std::string::npos) {
host = url.substr(0, pos);
query = url.substr(pos);
} else {
host = url;
query = "/";
}

Log(__FILE__, __LINE__, Log::kDebug)
<< "Requesting: " << host << " " << query;
int sock = -1;
int is_connected = -1;

Expand Down Expand Up @@ -56,7 +65,8 @@ bool HttpClient::HandleFetchRequest(struct Request *inner, bool ipv4) {
Retry(inner, ipv4);
return false;
} else {
Log(__FILE__, __LINE__, Log::kError) << "Could not connect " << strerror(errno);
Log(__FILE__, __LINE__, Log::kError)
<< "Could not connect " << strerror(errno);
stream_->ReleaseErrorAllWaitingRequest(inner->resource_id, 502);
}
return true;
Expand Down Expand Up @@ -87,13 +97,11 @@ bool HttpClient::HandleFetchRequest(struct Request *inner, bool ipv4) {

http->iov[1].iov_len = inner->iov[2].iov_len;
http->iov[1].iov_base = malloc(http->iov[1].iov_len);
memcpy(http->iov[1].iov_base, inner->iov[2].iov_base,
http->iov[1].iov_len);
memcpy(http->iov[1].iov_base, inner->iov[2].iov_base, http->iov[1].iov_len);

http->iov[2].iov_len = inner->iov[4].iov_len;
http->iov[2].iov_base = malloc(http->iov[2].iov_len);
memcpy(http->iov[2].iov_base, inner->iov[4].iov_base,
http->iov[2].iov_len);
memcpy(http->iov[2].iov_base, inner->iov[4].iov_base, http->iov[2].iov_len);

// io_uring_prep_readv(sqe, sock, &http->iov[0], 1, 0);
io_uring_prep_read(sqe, http->client_socket, http->iov[0].iov_base,
Expand All @@ -108,9 +116,7 @@ void HttpClient::ReleaseSocket(struct Request *http) {
Utils::ReleaseRequest(http);
}

int HttpClient::PreRequest(struct Request *http, int readed) {
return readed;
}
int HttpClient::PreRequest(struct Request *http, int readed) { return readed; }

int HttpClient::PostRequest(struct Request *http) {
struct io_uring_sqe *sqe = io_uring_get_sqe(ring_);
Expand Down
1 change: 0 additions & 1 deletion src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ void Utils::ReleaseRequest(struct Request *request) {
free(request->iov[i].iov_base);
}
}
std::cout<< "LAN_[" << __FILE__ << ":" << __LINE__ << "] "<< request->event_type << std::endl;
free(request);
}

Expand Down

0 comments on commit 9554e9e

Please sign in to comment.