mirror of
https://github.com/sogou/workflow.git
synced 2026-02-08 01:33:17 +08:00
Hide DNS parser's macros from global space. (#1743)
This commit is contained in:
@@ -49,6 +49,7 @@ set(INCLUDE_HEADERS
|
||||
src/protocol/mysql_byteorder.h
|
||||
src/protocol/PackageWrapper.h
|
||||
src/protocol/SSLWrapper.h
|
||||
src/protocol/dns_types.h
|
||||
src/protocol/dns_parser.h
|
||||
src/protocol/DnsMessage.h
|
||||
src/protocol/DnsUtil.h
|
||||
|
||||
@@ -21,6 +21,8 @@
|
||||
#include <atomic>
|
||||
#include "URIParser.h"
|
||||
#include "StringUtil.h"
|
||||
#include "dns_types.h"
|
||||
#include "DnsMessage.h"
|
||||
#include "WFDnsClient.h"
|
||||
|
||||
using namespace protocol;
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include <string>
|
||||
#include <atomic>
|
||||
#include "WFTaskFactory.h"
|
||||
#include "dns_types.h"
|
||||
#include "DnsMessage.h"
|
||||
|
||||
class WFDnsClient
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
#include <string>
|
||||
#include <atomic>
|
||||
#include "dns_types.h"
|
||||
#include "DnsMessage.h"
|
||||
#include "WFTaskError.h"
|
||||
#include "WFTaskFactory.h"
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
|
||||
#include <errno.h>
|
||||
#include <arpa/inet.h>
|
||||
#include "dns_types.h"
|
||||
#include "dns_parser.h"
|
||||
#include "DnsMessage.h"
|
||||
|
||||
#define DNS_LABELS_MAX 63
|
||||
@@ -215,6 +217,135 @@ DnsMessage& DnsMessage::operator = (DnsMessage&& msg)
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline struct list_head *DnsMessage::get_section(int section)
|
||||
{
|
||||
switch (section)
|
||||
{
|
||||
case DNS_ANSWER_SECTION:
|
||||
return &parser->answer_list;
|
||||
case DNS_AUTHORITY_SECTION:
|
||||
return &parser->authority_list;
|
||||
case DNS_ADDITIONAL_SECTION:
|
||||
return &parser->additional_list;
|
||||
default:
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int DnsMessage::add_a_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
const void *data)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_raw_record(name, DNS_TYPE_A, rclass, ttl, 4, data, list);
|
||||
}
|
||||
|
||||
int DnsMessage::add_aaaa_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
const void *data)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_raw_record(name, DNS_TYPE_AAAA, rclass, ttl, 16, data, list);
|
||||
}
|
||||
|
||||
int DnsMessage::add_ns_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
const char *data)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_str_record(name, DNS_TYPE_NS, rclass, ttl, data, list);
|
||||
}
|
||||
|
||||
int DnsMessage::add_cname_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
const char *data)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_str_record(name, DNS_TYPE_CNAME, rclass, ttl, data, list);
|
||||
}
|
||||
|
||||
int DnsMessage::add_ptr_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
const char *data)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_str_record(name, DNS_TYPE_PTR, rclass, ttl, data, list);
|
||||
}
|
||||
|
||||
int DnsMessage::add_soa_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
const char *mname, const char *rname,
|
||||
uint32_t serial, int32_t refresh,
|
||||
int32_t retry, int32_t expire, uint32_t minimum)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_soa_record(name, rclass, ttl, mname, rname, serial,
|
||||
refresh, retry, expire, minimum, list);
|
||||
}
|
||||
|
||||
int DnsMessage::add_srv_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
uint16_t priority, uint16_t weight,
|
||||
uint16_t port, const char *target)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_srv_record(name, rclass, ttl, priority, weight,
|
||||
port, target, list);
|
||||
}
|
||||
|
||||
int DnsMessage::add_mx_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
int16_t preference, const char *exchange)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_mx_record(name, rclass, ttl, preference, exchange, list);
|
||||
}
|
||||
|
||||
int DnsMessage::add_raw_record(int section, const char *name, uint16_t type,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
const void *data, uint16_t dlen)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_raw_record(name, type, rclass, ttl, dlen, data, list);
|
||||
}
|
||||
|
||||
int DnsMessage::encode_reply()
|
||||
{
|
||||
dns_record_cursor_t cursor;
|
||||
|
||||
@@ -154,118 +154,42 @@ public:
|
||||
|
||||
int add_a_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
const void *data)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_raw_record(name, DNS_TYPE_A, rclass, ttl, 4, data, list);
|
||||
}
|
||||
const void *data);
|
||||
|
||||
int add_aaaa_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
const void *data)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_raw_record(name, DNS_TYPE_AAAA, rclass, ttl,
|
||||
16, data, list);
|
||||
}
|
||||
const void *data);
|
||||
|
||||
int add_ns_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
const char *data)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_str_record(name, DNS_TYPE_NS, rclass, ttl, data, list);
|
||||
}
|
||||
const char *data);
|
||||
|
||||
int add_cname_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
const char *data)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_str_record(name, DNS_TYPE_CNAME, rclass, ttl,
|
||||
data, list);
|
||||
}
|
||||
const char *data);
|
||||
|
||||
int add_ptr_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
const char *data)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_str_record(name, DNS_TYPE_PTR, rclass, ttl, data, list);
|
||||
}
|
||||
const char *data);
|
||||
|
||||
int add_soa_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
const char *mname, const char *rname,
|
||||
uint32_t serial, int32_t refresh,
|
||||
int32_t retry, int32_t expire, uint32_t minimum)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_soa_record(name, rclass, ttl, mname, rname, serial,
|
||||
refresh, retry, expire, minimum, list);
|
||||
}
|
||||
int32_t retry, int32_t expire, uint32_t minimum);
|
||||
|
||||
int add_srv_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
uint16_t priority, uint16_t weight, uint16_t port,
|
||||
const char *target)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_srv_record(name, rclass, ttl,
|
||||
priority, weight, port, target, list);
|
||||
}
|
||||
uint16_t priority, uint16_t weight,
|
||||
uint16_t port, const char *target);
|
||||
|
||||
int add_mx_record(int section, const char *name,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
int16_t preference, const char *exchange)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_mx_record(name, rclass, ttl, preference, exchange, list);
|
||||
}
|
||||
int16_t preference, const char *exchange);
|
||||
|
||||
int add_raw_record(int section, const char *name, uint16_t type,
|
||||
uint16_t rclass, uint32_t ttl,
|
||||
const void *data, uint16_t dlen)
|
||||
{
|
||||
struct list_head *list = get_section(section);
|
||||
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
return dns_add_raw_record(name, type, rclass, ttl, dlen, data, list);
|
||||
}
|
||||
const void *data, uint16_t dlen);
|
||||
|
||||
// Inner use only
|
||||
bool is_single_packet() const
|
||||
@@ -305,22 +229,7 @@ protected:
|
||||
private:
|
||||
int encode_reply();
|
||||
int encode_truncation_reply();
|
||||
|
||||
struct list_head *get_section(int section)
|
||||
{
|
||||
switch (section)
|
||||
{
|
||||
case DNS_ANSWER_SECTION:
|
||||
return &(parser->answer_list);
|
||||
case DNS_AUTHORITY_SECTION:
|
||||
return &(parser->authority_list);
|
||||
case DNS_ADDITIONAL_SECTION:
|
||||
return &(parser->additional_list);
|
||||
default:
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
struct list_head *get_section(int section);
|
||||
|
||||
// size of msgbuf, but in network byte order
|
||||
uint16_t msgsize;
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <string>
|
||||
#include "dns_types.h"
|
||||
#include "DnsUtil.h"
|
||||
|
||||
namespace protocol
|
||||
@@ -94,7 +95,7 @@ int DnsUtil::getaddrinfo(const DnsResponse *resp,
|
||||
{
|
||||
if (res)
|
||||
DnsUtil::freeaddrinfo(res);
|
||||
return EAI_SYSTEM;
|
||||
return EAI_MEMORY;
|
||||
}
|
||||
|
||||
ai->ai_family = family;
|
||||
|
||||
@@ -18,7 +18,9 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <arpa/inet.h>
|
||||
#include "dns_types.h"
|
||||
#include "dns_parser.h"
|
||||
|
||||
#define DNS_LABELS_MAX 63
|
||||
|
||||
@@ -23,68 +23,6 @@
|
||||
#include <stdint.h>
|
||||
#include "list.h"
|
||||
|
||||
enum
|
||||
{
|
||||
DNS_TYPE_A = 1,
|
||||
DNS_TYPE_NS,
|
||||
DNS_TYPE_MD,
|
||||
DNS_TYPE_MF,
|
||||
DNS_TYPE_CNAME,
|
||||
DNS_TYPE_SOA = 6,
|
||||
DNS_TYPE_MB,
|
||||
DNS_TYPE_MG,
|
||||
DNS_TYPE_MR,
|
||||
DNS_TYPE_NULL,
|
||||
DNS_TYPE_WKS = 11,
|
||||
DNS_TYPE_PTR,
|
||||
DNS_TYPE_HINFO,
|
||||
DNS_TYPE_MINFO,
|
||||
DNS_TYPE_MX,
|
||||
DNS_TYPE_TXT = 16,
|
||||
|
||||
DNS_TYPE_AAAA = 28,
|
||||
DNS_TYPE_SRV = 33,
|
||||
|
||||
DNS_TYPE_AXFR = 252,
|
||||
DNS_TYPE_MAILB = 253,
|
||||
DNS_TYPE_MAILA = 254,
|
||||
DNS_TYPE_ALL = 255
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
DNS_CLASS_IN = 1,
|
||||
DNS_CLASS_CS,
|
||||
DNS_CLASS_CH,
|
||||
DNS_CLASS_HS,
|
||||
|
||||
DNS_CLASS_ALL = 255
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
DNS_OPCODE_QUERY = 0,
|
||||
DNS_OPCODE_IQUERY,
|
||||
DNS_OPCODE_STATUS,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
DNS_RCODE_NO_ERROR = 0,
|
||||
DNS_RCODE_FORMAT_ERROR,
|
||||
DNS_RCODE_SERVER_FAILURE,
|
||||
DNS_RCODE_NAME_ERROR,
|
||||
DNS_RCODE_NOT_IMPLEMENTED,
|
||||
DNS_RCODE_REFUSED
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
DNS_ANSWER_SECTION = 1,
|
||||
DNS_AUTHORITY_SECTION = 2,
|
||||
DNS_ADDITIONAL_SECTION = 3,
|
||||
};
|
||||
|
||||
/**
|
||||
* dns_header_t is a struct to describe the header of a dns
|
||||
* request or response packet, but the byte order is not
|
||||
|
||||
85
src/protocol/dns_types.h
Normal file
85
src/protocol/dns_types.h
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
Copyright (c) 2021 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.
|
||||
|
||||
Author: Liu Kai (liukaidx@sogou-inc.com)
|
||||
*/
|
||||
|
||||
#ifndef _DNS_TYPES_H_
|
||||
#define _DNS_TYPES_H_
|
||||
|
||||
enum
|
||||
{
|
||||
DNS_TYPE_A = 1,
|
||||
DNS_TYPE_NS,
|
||||
DNS_TYPE_MD,
|
||||
DNS_TYPE_MF,
|
||||
DNS_TYPE_CNAME,
|
||||
DNS_TYPE_SOA = 6,
|
||||
DNS_TYPE_MB,
|
||||
DNS_TYPE_MG,
|
||||
DNS_TYPE_MR,
|
||||
DNS_TYPE_NULL,
|
||||
DNS_TYPE_WKS = 11,
|
||||
DNS_TYPE_PTR,
|
||||
DNS_TYPE_HINFO,
|
||||
DNS_TYPE_MINFO,
|
||||
DNS_TYPE_MX,
|
||||
DNS_TYPE_TXT = 16,
|
||||
|
||||
DNS_TYPE_AAAA = 28,
|
||||
DNS_TYPE_SRV = 33,
|
||||
|
||||
DNS_TYPE_AXFR = 252,
|
||||
DNS_TYPE_MAILB = 253,
|
||||
DNS_TYPE_MAILA = 254,
|
||||
DNS_TYPE_ALL = 255
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
DNS_CLASS_IN = 1,
|
||||
DNS_CLASS_CS,
|
||||
DNS_CLASS_CH,
|
||||
DNS_CLASS_HS,
|
||||
|
||||
DNS_CLASS_ALL = 255
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
DNS_OPCODE_QUERY = 0,
|
||||
DNS_OPCODE_IQUERY,
|
||||
DNS_OPCODE_STATUS,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
DNS_RCODE_NO_ERROR = 0,
|
||||
DNS_RCODE_FORMAT_ERROR,
|
||||
DNS_RCODE_SERVER_FAILURE,
|
||||
DNS_RCODE_NAME_ERROR,
|
||||
DNS_RCODE_NOT_IMPLEMENTED,
|
||||
DNS_RCODE_REFUSED
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
DNS_ANSWER_SECTION = 1,
|
||||
DNS_AUTHORITY_SECTION = 2,
|
||||
DNS_ADDITIONAL_SECTION = 3,
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#ifndef _WFDNSSERVER_H_
|
||||
#define _WFDNSSERVER_H_
|
||||
|
||||
#include "dns_types.h"
|
||||
#include "DnsMessage.h"
|
||||
#include "WFServer.h"
|
||||
#include "WFTaskFactory.h"
|
||||
|
||||
Reference in New Issue
Block a user