From d4f0fa0f5bb6ecffefd990c7b4c32be3469bc0d6 Mon Sep 17 00:00:00 2001 From: xiehan <52160700+Barenboim@users.noreply.github.com> Date: Thu, 15 Apr 2021 01:24:41 +0800 Subject: [PATCH] fix addrinfo with unix domain socket (#337) * fix addrinfo with unix domain socket * add a return * update workflow package version --- CMakeLists.txt | 2 +- src/algorithm/DNSRoutine.cc | 54 ++++++++++++++++++++++++++----------- src/algorithm/DNSRoutine.h | 3 +++ 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 69a245f8..51829e42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ set(CMAKE_SKIP_RPATH TRUE) project( workflow - VERSION 0.9.5 + VERSION 0.9.6 LANGUAGES C CXX ) diff --git a/src/algorithm/DNSRoutine.cc b/src/algorithm/DNSRoutine.cc index ba3ad2e1..f5eb698c 100644 --- a/src/algorithm/DNSRoutine.cc +++ b/src/algorithm/DNSRoutine.cc @@ -16,8 +16,14 @@ Authors: Wu Jiaxu (wujiaxu@sogou-inc.com) */ -#include +#include +#include #include +#include +#include +#include +#include +#include #include "DNSRoutine.h" #define PORT_STR_MAX 5 @@ -48,25 +54,41 @@ DNSOutput& DNSOutput::operator= (DNSOutput&& move) return *this; } +void DNSRoutine::run_local_path(const std::string& path, DNSOutput *out) +{ + struct sockaddr_un *sun = NULL; + + if (path.size() <= sizeof sun->sun_path) + { + size_t size = sizeof (struct addrinfo) + sizeof (struct sockaddr_un); + + out->addrinfo_ = (struct addrinfo *)calloc(size, 1); + if (out->addrinfo_) + { + sun = (struct sockaddr_un *)(out->addrinfo_ + 1); + sun->sun_family = AF_UNIX; + memcpy(sun->sun_path, path.c_str(), path.size()); + + out->addrinfo_->ai_family = AF_UNIX; + out->addrinfo_->ai_socktype = SOCK_STREAM; + out->addrinfo_->ai_addr = (struct sockaddr *)sun; + size = offsetof(struct sockaddr_un, sun_path) + path.size(); + out->addrinfo_->ai_addrlen = size; + out->error_ = 0; + return; + } + } + else + errno = EINVAL; + + out->error_ = EAI_SYSTEM; +} + void DNSRoutine::run(const DNSInput *in, DNSOutput *out) { if (!in->host_.empty() && in->host_[0] == '/') { - out->error_ = 0; - out->addrinfo_ = (addrinfo*)malloc(sizeof (struct addrinfo) + sizeof (struct sockaddr_un)); - out->addrinfo_->ai_flags = AI_ADDRCONFIG; - out->addrinfo_->ai_family = AF_UNIX; - out->addrinfo_->ai_socktype = SOCK_STREAM; - out->addrinfo_->ai_protocol = 0; - out->addrinfo_->ai_addrlen = sizeof (struct sockaddr_un); - out->addrinfo_->ai_addr = (struct sockaddr *)((char *)(out->addrinfo_) + sizeof (struct addrinfo)); - out->addrinfo_->ai_canonname = NULL; - out->addrinfo_->ai_next = NULL; - struct sockaddr_un *sun = (struct sockaddr_un *)(out->addrinfo_->ai_addr); - - sun->sun_family = AF_UNIX; - memset(sun->sun_path, 0, sizeof (sun->sun_path)); - strcpy(sun->sun_path, in->host_.c_str()); + run_local_path(in->host_, out); return; } diff --git a/src/algorithm/DNSRoutine.h b/src/algorithm/DNSRoutine.h index 785cae14..0ec69aad 100644 --- a/src/algorithm/DNSRoutine.h +++ b/src/algorithm/DNSRoutine.h @@ -92,6 +92,9 @@ class DNSRoutine { public: static void run(const DNSInput *in, DNSOutput *out); + +private: + static void run_local_path(const std::string& path, DNSOutput *out); }; //new WFDNSTask(queue, executor, dns_routine, callback)