mirror of
https://github.com/sogou/workflow.git
synced 2026-02-08 01:33:17 +08:00
move DnsRoutine to WFDnsResolver (Windows) (#1283)
* move DnsRoutine to WFDnsResolver * remove include DnsRoutine.h * use global __ai_hints
This commit is contained in:
@@ -48,7 +48,6 @@ endif ()
|
||||
|
||||
set(INCLUDE_HEADERS
|
||||
src/PlatformSocket.h
|
||||
src/algorithm/DnsRoutine.h
|
||||
src/algorithm/MapReduce.h
|
||||
src/algorithm/MapReduce.inl
|
||||
src/protocol/ProtocolMessage.h
|
||||
|
||||
@@ -11,7 +11,6 @@ endif ()
|
||||
|
||||
add_subdirectory(util)
|
||||
add_subdirectory(manager)
|
||||
add_subdirectory(algorithm)
|
||||
add_subdirectory(protocol)
|
||||
add_subdirectory(factory)
|
||||
add_subdirectory(nameservice)
|
||||
@@ -21,7 +20,6 @@ add_subdirectory(client)
|
||||
add_dependencies(kernel LINK_HEADERS)
|
||||
add_dependencies(util LINK_HEADERS)
|
||||
add_dependencies(manager LINK_HEADERS)
|
||||
add_dependencies(algorithm LINK_HEADERS)
|
||||
add_dependencies(protocol LINK_HEADERS)
|
||||
add_dependencies(factory LINK_HEADERS)
|
||||
add_dependencies(nameservice LINK_HEADERS)
|
||||
@@ -33,7 +31,6 @@ add_library(
|
||||
$<TARGET_OBJECTS:kernel>
|
||||
$<TARGET_OBJECTS:util>
|
||||
$<TARGET_OBJECTS:manager>
|
||||
$<TARGET_OBJECTS:algorithm>
|
||||
$<TARGET_OBJECTS:protocol>
|
||||
$<TARGET_OBJECTS:factory>
|
||||
$<TARGET_OBJECTS:nameservice>
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.6)
|
||||
project(algorithm)
|
||||
|
||||
set(SRC
|
||||
DnsRoutine.cc
|
||||
)
|
||||
|
||||
add_library(${PROJECT_NAME} OBJECT ${SRC})
|
||||
|
||||
@@ -1,117 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2019 Sogou, Inc.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
Authors: Wu Jiaxu (wujiaxu@sogou-inc.com)
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include "DnsRoutine.h"
|
||||
#include "PlatformSocket.h"
|
||||
|
||||
#define PORT_STR_MAX 5
|
||||
|
||||
DnsOutput::DnsOutput(DnsOutput&& move)
|
||||
{
|
||||
error_ = move.error_;
|
||||
addrinfo_ = move.addrinfo_;
|
||||
|
||||
move.error_ = 0;
|
||||
move.addrinfo_ = NULL;
|
||||
}
|
||||
|
||||
DnsOutput& DnsOutput::operator= (DnsOutput&& move)
|
||||
{
|
||||
if (this != &move)
|
||||
{
|
||||
if (addrinfo_)
|
||||
freeaddrinfo(addrinfo_);
|
||||
|
||||
error_ = move.error_;
|
||||
addrinfo_ = move.addrinfo_;
|
||||
|
||||
move.error_ = 0;
|
||||
move.addrinfo_ = NULL;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
void DnsRoutine::run_local_path(const std::string& path, DnsOutput *out)
|
||||
{
|
||||
struct sockaddr_un *sun = NULL;
|
||||
|
||||
if (path.size() + 1 <= 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() + 1;
|
||||
out->addrinfo_->ai_addrlen = size;
|
||||
out->error_ = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
errno = EINVAL;
|
||||
|
||||
out->error_ = EAI_SYSTEM;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void DnsRoutine::run(const DnsInput *in, DnsOutput *out)
|
||||
{
|
||||
if (!in->host_.empty() && in->host_[0] == '/')
|
||||
{
|
||||
#ifndef _WIN32
|
||||
run_local_path(in->host_, out);
|
||||
#endif // !_WIN32
|
||||
return;
|
||||
}
|
||||
|
||||
struct addrinfo hints = {
|
||||
#ifdef AI_ADDRCONFIG
|
||||
/*.ai_flags = */AI_ADDRCONFIG,
|
||||
#endif
|
||||
/*.ai_family = */AF_UNSPEC,
|
||||
/*.ai_socktype = */SOCK_STREAM
|
||||
};
|
||||
char port_str[PORT_STR_MAX + 1];
|
||||
|
||||
hints.ai_flags |= AI_NUMERICSERV;
|
||||
if (in->is_numeric_host())
|
||||
hints.ai_flags |= AI_NUMERICHOST;
|
||||
|
||||
snprintf(port_str, PORT_STR_MAX + 1, "%u", in->port_);
|
||||
out->error_ = getaddrinfo(in->host_.c_str(),
|
||||
port_str,
|
||||
&hints,
|
||||
&out->addrinfo_);
|
||||
}
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2019 Sogou, Inc.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
Authors: Wu Jiaxu (wujiaxu@sogou-inc.com)
|
||||
*/
|
||||
|
||||
#ifndef _DNSROUTINE_H_
|
||||
#define _DNSROUTINE_H_
|
||||
|
||||
#include <string>
|
||||
#include "PlatformSocket.h"
|
||||
|
||||
class DnsInput
|
||||
{
|
||||
public:
|
||||
DnsInput() :
|
||||
port_(0),
|
||||
numeric_host_(false)
|
||||
{}
|
||||
|
||||
DnsInput(const std::string& host, unsigned short port,
|
||||
bool numeric_host) :
|
||||
host_(host),
|
||||
port_(port),
|
||||
numeric_host_(numeric_host)
|
||||
{}
|
||||
|
||||
//move constructor
|
||||
DnsInput(DnsInput&& move) = default;
|
||||
//move operator
|
||||
DnsInput& operator= (DnsInput &&move) = default;
|
||||
|
||||
void reset(const std::string& host, unsigned short port)
|
||||
{
|
||||
host_.assign(host);
|
||||
port_ = port;
|
||||
numeric_host_ = false;
|
||||
}
|
||||
|
||||
void reset(const std::string& host, unsigned short port,
|
||||
bool numeric_host)
|
||||
{
|
||||
host_.assign(host);
|
||||
port_ = port;
|
||||
numeric_host_ = numeric_host;
|
||||
}
|
||||
|
||||
const std::string& get_host() const { return host_; }
|
||||
unsigned short get_port() const { return port_; }
|
||||
bool is_numeric_host() const { return numeric_host_; }
|
||||
|
||||
protected:
|
||||
std::string host_;
|
||||
unsigned short port_;
|
||||
bool numeric_host_;
|
||||
|
||||
friend class DnsRoutine;
|
||||
};
|
||||
|
||||
class DnsOutput
|
||||
{
|
||||
public:
|
||||
DnsOutput():
|
||||
error_(0),
|
||||
addrinfo_(NULL)
|
||||
{}
|
||||
|
||||
~DnsOutput()
|
||||
{
|
||||
if (addrinfo_)
|
||||
freeaddrinfo(addrinfo_);
|
||||
}
|
||||
|
||||
//move constructor
|
||||
DnsOutput(DnsOutput&& move);
|
||||
//move operator
|
||||
DnsOutput& operator= (DnsOutput&& move);
|
||||
|
||||
int get_error() const { return error_; }
|
||||
const struct addrinfo *get_addrinfo() const { return addrinfo_; }
|
||||
|
||||
//if DONOT want DnsOutput release addrinfo, use move_addrinfo in callback
|
||||
struct addrinfo *move_addrinfo()
|
||||
{
|
||||
struct addrinfo *p = addrinfo_;
|
||||
addrinfo_ = NULL;
|
||||
return p;
|
||||
}
|
||||
|
||||
protected:
|
||||
int error_;
|
||||
struct addrinfo *addrinfo_;
|
||||
|
||||
friend class DnsRoutine;
|
||||
};
|
||||
|
||||
class DnsRoutine
|
||||
{
|
||||
public:
|
||||
static void run(const DnsInput *in, DnsOutput *out);
|
||||
static void create(DnsOutput *out, int error, struct addrinfo *ai)
|
||||
{
|
||||
if (out->addrinfo_)
|
||||
freeaddrinfo(out->addrinfo_);
|
||||
|
||||
out->error_ = error;
|
||||
out->addrinfo_ = ai;
|
||||
}
|
||||
#ifndef _WIN32
|
||||
private:
|
||||
static void run_local_path(const std::string& path, DnsOutput *out);
|
||||
#endif // !_WIN32
|
||||
};
|
||||
|
||||
//new WFDnsTask(queue, executor, dns_routine, callback)
|
||||
//if donot want freeaddrinfo, please std::move output in callback
|
||||
|
||||
#endif
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include "PlatformSocket.h"
|
||||
#include "DnsRoutine.h"
|
||||
#include "EndpointParams.h"
|
||||
#include "RouteManager.h"
|
||||
#include "WFGlobal.h"
|
||||
@@ -41,10 +40,6 @@
|
||||
#define HOSTS_LINEBUF_INIT_SIZE 128
|
||||
#define PORT_STR_MAX 5
|
||||
|
||||
// Dns Thread task. For internal usage only.
|
||||
using ThreadDnsTask = WFThreadTask<DnsInput, DnsOutput>;
|
||||
using thread_dns_callback_t = std::function<void (ThreadDnsTask *)>;
|
||||
|
||||
static constexpr struct addrinfo __ai_hints =
|
||||
{
|
||||
#ifdef AI_ADDRCONFIG
|
||||
@@ -61,6 +56,117 @@ static constexpr struct addrinfo __ai_hints =
|
||||
/*.ai_next = */ NULL
|
||||
};
|
||||
|
||||
class DnsInput
|
||||
{
|
||||
public:
|
||||
DnsInput() :
|
||||
port_(0),
|
||||
numeric_host_(false)
|
||||
{}
|
||||
|
||||
DnsInput(const std::string& host, unsigned short port,
|
||||
bool numeric_host) :
|
||||
host_(host),
|
||||
port_(port),
|
||||
numeric_host_(numeric_host)
|
||||
{}
|
||||
|
||||
void reset(const std::string& host, unsigned short port)
|
||||
{
|
||||
host_.assign(host);
|
||||
port_ = port;
|
||||
numeric_host_ = false;
|
||||
}
|
||||
|
||||
void reset(const std::string& host, unsigned short port,
|
||||
bool numeric_host)
|
||||
{
|
||||
host_.assign(host);
|
||||
port_ = port;
|
||||
numeric_host_ = numeric_host;
|
||||
}
|
||||
|
||||
const std::string& get_host() const { return host_; }
|
||||
unsigned short get_port() const { return port_; }
|
||||
bool is_numeric_host() const { return numeric_host_; }
|
||||
|
||||
protected:
|
||||
std::string host_;
|
||||
unsigned short port_;
|
||||
bool numeric_host_;
|
||||
|
||||
friend class DnsRoutine;
|
||||
};
|
||||
|
||||
class DnsOutput
|
||||
{
|
||||
public:
|
||||
DnsOutput():
|
||||
error_(0),
|
||||
addrinfo_(NULL)
|
||||
{}
|
||||
|
||||
~DnsOutput()
|
||||
{
|
||||
if (addrinfo_)
|
||||
freeaddrinfo(addrinfo_);
|
||||
}
|
||||
|
||||
int get_error() const { return error_; }
|
||||
const struct addrinfo *get_addrinfo() const { return addrinfo_; }
|
||||
|
||||
//if DONOT want DnsOutput release addrinfo, use move_addrinfo in callback
|
||||
struct addrinfo *move_addrinfo()
|
||||
{
|
||||
struct addrinfo *p = addrinfo_;
|
||||
addrinfo_ = NULL;
|
||||
return p;
|
||||
}
|
||||
|
||||
protected:
|
||||
int error_;
|
||||
struct addrinfo *addrinfo_;
|
||||
|
||||
friend class DnsRoutine;
|
||||
};
|
||||
|
||||
class DnsRoutine
|
||||
{
|
||||
public:
|
||||
static void run(const DnsInput *in, DnsOutput *out);
|
||||
static void create(DnsOutput *out, int error, struct addrinfo *ai)
|
||||
{
|
||||
if (out->addrinfo_)
|
||||
freeaddrinfo(out->addrinfo_);
|
||||
|
||||
out->error_ = error;
|
||||
out->addrinfo_ = ai;
|
||||
}
|
||||
};
|
||||
|
||||
void DnsRoutine::run(const DnsInput *in, DnsOutput *out)
|
||||
{
|
||||
if (!in->host_.empty() && in->host_[0] == '/')
|
||||
return;
|
||||
|
||||
struct addrinfo hints = __ai_hints;
|
||||
char port_str[PORT_STR_MAX + 1];
|
||||
|
||||
hints.ai_flags |= AI_NUMERICSERV;
|
||||
if (in->is_numeric_host())
|
||||
hints.ai_flags |= AI_NUMERICHOST;
|
||||
|
||||
snprintf(port_str, PORT_STR_MAX + 1, "%u", in->port_);
|
||||
out->error_ = getaddrinfo(in->host_.c_str(),
|
||||
port_str,
|
||||
&hints,
|
||||
&out->addrinfo_);
|
||||
}
|
||||
|
||||
// Dns Thread task. For internal usage only.
|
||||
using ThreadDnsTask = WFThreadTask<DnsInput, DnsOutput>;
|
||||
using thread_dns_callback_t = std::function<void (ThreadDnsTask *)>;
|
||||
|
||||
struct DnsContext
|
||||
{
|
||||
int state;
|
||||
|
||||
Reference in New Issue
Block a user