Skip to content

Commit

Permalink
fix: ipv6 support in extract ip
Browse files Browse the repository at this point in the history
  • Loading branch information
algonathan committed Dec 6, 2022
1 parent 598ebc6 commit e48447a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/services/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace services {
::distribicom::Ack *response) {
try {

auto requesting_worker = utils::extract_ipv4(context);
auto requesting_worker = utils::extract_ip(context);
std::string subscribing_worker_address = requesting_worker + ":" + std::to_string(request->workerport());

// creating client to the worker:
Expand Down Expand Up @@ -47,7 +47,7 @@ namespace services {
::grpc::Status Manager::ReturnLocalWork(::grpc::ServerContext *context,
::grpc::ServerReader<::distribicom::MatrixPart> *reader,
::distribicom::Ack *response) {
auto sending_worker = utils::extract_ipv4(context);
auto sending_worker = utils::extract_ip(context);

mtx.lock();
auto exists = worker_stubs.find(sending_worker) != worker_stubs.end();
Expand Down
2 changes: 1 addition & 1 deletion src/services/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ services::FullServer::RegisterAsClient(grpc::ServerContext *context, const distr

try {

auto requesting_client = utils::extract_ipv4(context);
auto requesting_client = utils::extract_ip(context);
std::string subscribing_client_address = requesting_client + ":" + std::to_string(request->client_port());

// creating stub to the client:
Expand Down
65 changes: 59 additions & 6 deletions src/services/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "utils.hpp"
#include <charconv>

namespace services::utils{
int extract_size_from_metadata(const std::multimap<grpc::string_ref, grpc::string_ref> &mp,
namespace services::utils {
int extract_size_from_metadata(const std::multimap <grpc::string_ref, grpc::string_ref> &mp,
const services::constants::metadata &md) {
auto ntmp = mp.find(std::string(md));
return std::stoi(std::string(ntmp->second.data(), ntmp->second.size()));
Expand All @@ -11,11 +12,63 @@ namespace services::utils{
context.AddMetadata(std::string(md), std::to_string(size));
}

std::string extract_ipv4(grpc::ServerContext *pContext) {
const std::string ipv4("ipv4:");
const std::string ipv6("ipv6:");

char from_hex(char ch) {
return isdigit(ch) ? char(ch - '0') : char(tolower(ch) - 'a' + 10);
}

// help understanding in: https://stackoverflow.com/questions/154536/encode-decode-urls-in-c
std::string url_decode(std::string text) {

std::ostringstream decoded;
for (auto i = text.begin(), end = text.end(); i != end; ++i) {
std::string::value_type c = (*i);

switch (c) {
case '%':
if (i + 1 == end || i + 2 == end) {
return decoded.str();
}

// decoding from 2 hex values into a single char:
decoded << char(from_hex(i[1]) << 4 | from_hex(i[2]));
i += 2;

break;
case '+':
decoded << ' ';
break;
default:
decoded << c;
}

}

return decoded.str();
}

std::string extract_ip(grpc::ServerContext *pContext) {
auto fullip = std::string(pContext->peer());
const std::string delimiter("ipv4:");
fullip.erase(0, fullip.find(delimiter) + delimiter.length());
return fullip.substr(0, fullip.find(':'));

if (fullip.find(ipv4) != std::string::npos) {
fullip.erase(0, fullip.find(ipv4) + ipv4.length());
return fullip.substr(0, fullip.find(':'));
}

// assuming ipv6:
fullip = url_decode(fullip);

fullip.erase(0, fullip.find(ipv6) + ipv6.length());
fullip.substr(0, fullip.find(':'));

if (fullip.find(']') != std::string::npos) {
fullip.erase(fullip.find(']'), fullip.length());
fullip.erase(0, fullip.find('[') + 1);
}

return fullip;
}

seal::EncryptionParameters setup_enc_params(const distribicom::AppConfigs &cnfgs) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace services::utils {

void add_metadata_size(grpc::ClientContext &context, const services::constants::metadata &md, int size);

std::string extract_ipv4(grpc::ServerContext *pContext);
std::string extract_ip(grpc::ServerContext *pContext);

seal::EncryptionParameters setup_enc_params(const distribicom::AppConfigs &cnfgs);
}
Expand Down

0 comments on commit e48447a

Please sign in to comment.