add rfc1035 c++ module

This commit is contained in:
shuxin   zheng 2020-11-18 10:11:11 +08:00
parent c82d4ab07f
commit e98ddf4abb
19 changed files with 444 additions and 2 deletions

View File

@ -94,6 +94,7 @@ set(sources
${src}/memcache
${src}/mime
${src}/mime/internal
${src}/net
${src}/queue
${src}/redis
${src}/serialize

View File

@ -182,6 +182,7 @@ OBJS_DST = $(patsubst %.cpp, $(OBJ_PATH_DST)/%.o, $(notdir $(wildcard src/*.cpp)
$(patsubst %.cpp, $(OBJ_PATH_DST)/%.o, $(notdir $(wildcard src/smtp/*.cpp))) \
$(patsubst %.cpp, $(OBJ_PATH_DST)/%.o, $(notdir $(wildcard src/mime/*.cpp))) \
$(patsubst %.cpp, $(OBJ_PATH_DST)/%.o, $(notdir $(wildcard src/mime/internal/*.cpp))) \
$(patsubst %.cpp, $(OBJ_PATH_DST)/%.o, $(notdir $(wildcard src/net/*.cpp))) \
$(patsubst %.cpp, $(OBJ_PATH_DST)/%.o, $(notdir $(wildcard src/connpool/*.cpp))) \
$(patsubst %.cpp, $(OBJ_PATH_DST)/%.o, $(notdir $(wildcard src/redis/*.cpp))) \
$(patsubst %.cpp, $(OBJ_PATH_DST)/%.o, $(notdir $(wildcard src/disque/*.cpp))) \
@ -203,6 +204,7 @@ OBJS_DEF = $(patsubst %.cpp, $(DEF_PATH_DST)/%.inc, $(notdir $(wildcard src/*.cp
$(patsubst %.cpp, $(DEF_PATH_DST)/%.inc, $(notdir $(wildcard src/smtp/*.cpp))) \
$(patsubst %.cpp, $(DEF_PATH_DST)/%.inc, $(notdir $(wildcard src/mime/*.cpp))) \
$(patsubst %.cpp, $(DEF_PATH_DST)/%.inc, $(notdir $(wildcard src/mime/internal/*.cpp))) \
$(patsubst %.cpp, $(DEF_PATH_DST)/%.inc, $(notdir $(wildcard src/net/*.cpp))) \
$(patsubst %.cpp, $(DEF_PATH_DST)/%.inc, $(notdir $(wildcard src/connpool/*.cpp))) \
$(patsubst %.cpp, $(DEF_PATH_DST)/%.inc, $(notdir $(wildcard src/redis/*.cpp))) \
$(patsubst %.cpp, $(DEF_PATH_DST)/%.inc, $(notdir $(wildcard src/disque/*.cpp))) \
@ -324,6 +326,8 @@ $(OBJ_PATH_DST)/%.o: ./src/mime/%.cpp
$(COMPILE_OBJ)
$(OBJ_PATH_DST)/%.o: ./src/mime/internal/%.cpp
$(COMPILE_OBJ)
$(OBJ_PATH_DST)/%.o: ./src/net/%.cpp
$(COMPILE_OBJ)
$(OBJ_PATH_DST)/%.o: ./src/redis/%.cpp
$(COMPILE_OBJ)
$(OBJ_PATH_DST)/%.o: ./src/disque/%.cpp

View File

@ -216,6 +216,7 @@
7029CC2F23A9D075001B3EDB /* sslbase_io.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7029CC2E23A9D075001B3EDB /* sslbase_io.cpp */; };
702B9D9023A8E31200079240 /* mbedtls_conf.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 702B9D8E23A8E31200079240 /* mbedtls_conf.cpp */; };
702B9D9123A8E31200079240 /* mbedtls_io.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 702B9D8F23A8E31200079240 /* mbedtls_io.cpp */; };
7035839225626D94006ABBA5 /* rfc1035.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7035839125626D94006ABBA5 /* rfc1035.cpp */; };
70BD6B8C232B933D00E4C798 /* redis_stream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 07A4677E1E6FCCF1002C9B3C /* redis_stream.cpp */; };
70BD6B8D232B933D00E4C798 /* token_tree.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 07A467A01E6FCCF1002C9B3C /* token_tree.cpp */; };
70BD6B8E232B933D00E4C798 /* sqlite_manager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 07A4670B1E6FCCF1002C9B3C /* sqlite_manager.cpp */; };
@ -686,6 +687,8 @@
702B9D8D23A8E2FB00079240 /* mbedtls_io.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = mbedtls_io.hpp; sourceTree = "<group>"; };
702B9D8E23A8E31200079240 /* mbedtls_conf.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = mbedtls_conf.cpp; sourceTree = "<group>"; };
702B9D8F23A8E31200079240 /* mbedtls_io.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = mbedtls_io.cpp; sourceTree = "<group>"; };
7035839025626D84006ABBA5 /* rfc1035.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = rfc1035.hpp; path = net/rfc1035.hpp; sourceTree = "<group>"; };
7035839125626D94006ABBA5 /* rfc1035.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = rfc1035.cpp; path = net/rfc1035.cpp; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -729,6 +732,7 @@
07A466D91E6FCCF1002C9B3C /* src */ = {
isa = PBXGroup;
children = (
7035838E25626D66006ABBA5 /* net */,
07A466DA1E6FCCF1002C9B3C /* acl_cpp_init.cpp */,
07A466DB1E6FCCF1002C9B3C /* acl_cpp_test.cpp */,
07A466DC1E6FCCF1002C9B3C /* acl_stdafx.cpp */,
@ -1158,6 +1162,7 @@
07A468771E6FD404002C9B3C /* acl_cpp */ = {
isa = PBXGroup;
children = (
7035838F25626D74006ABBA5 /* net */,
07A468781E6FD404002C9B3C /* acl_cpp_define.hpp */,
07A468791E6FD404002C9B3C /* acl_cpp_init.hpp */,
07A4687A1E6FD404002C9B3C /* acl_cpp_test.hpp */,
@ -1557,6 +1562,22 @@
path = stream;
sourceTree = "<group>";
};
7035838E25626D66006ABBA5 /* net */ = {
isa = PBXGroup;
children = (
7035839125626D94006ABBA5 /* rfc1035.cpp */,
);
name = net;
sourceTree = "<group>";
};
7035838F25626D74006ABBA5 /* net */ = {
isa = PBXGroup;
children = (
7035839025626D84006ABBA5 /* rfc1035.hpp */,
);
name = net;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@ -1678,6 +1699,7 @@
07A4681B1E6FCCF1002C9B3C /* mime_attach.cpp in Sources */,
07A468191E6FCCF1002C9B3C /* trimblanks.cpp in Sources */,
70BD6B8E232B933D00E4C798 /* sqlite_manager.cpp in Sources */,
7035839225626D94006ABBA5 /* rfc1035.cpp in Sources */,
07A467D61E6FCCF1002C9B3C /* db_handle.cpp in Sources */,
07A467EF1E6FCCF1002C9B3C /* http_download.cpp in Sources */,
07A468391E6FCCF1002C9B3C /* redis_script.cpp in Sources */,

View File

@ -54,6 +54,8 @@
#include "session/redis_session.hpp"
#include "session/memcache_session.hpp"
#include "net/rfc1035.hpp"
#include "stream/stream.hpp"
#include "stream/istream.hpp"
#include "stream/ostream.hpp"

View File

@ -0,0 +1,96 @@
//
// Created by shuxin ¡¡¡¡zheng on 2020/11/14.
//
#pragma once
#include "../acl_cpp_define.hpp"
#include "../stdlib/string.hpp"
#include <vector>
struct ACL_RFC1035_MESSAGE;
namespace acl {
typedef enum {
rfc1035_type_null,
rfc1035_type_a,
rfc1035_type_aaaa,
rfc1035_type_mx,
rfc1035_type_txt,
rfc1035_type_ptr,
} rfc1035_type_t;
class ACL_CPP_API rfc1035_request {
public:
rfc1035_request(void);
~rfc1035_request(void);
rfc1035_request& set_name(const char* name);
rfc1035_request& set_qid(unsigned short id);
rfc1035_request& set_type(rfc1035_type_t type);
const char* get_name(void) const {
return name_;
}
unsigned short get_qid(void) const {
return qid_;
}
rfc1035_type_t get_type(void) const {
return type_;
}
public:
size_t build_query(char* buf, size_t size);
size_t build_query4ptr(const struct in_addr addr, char* buf, size_t size);
bool parse_request(const void* buf, size_t len);
private:
char name_[256];
unsigned short qid_;
rfc1035_type_t type_;
};
class ACL_CPP_API rfc1035_response {
public:
rfc1035_response(void);
~rfc1035_response(void);
rfc1035_response& set_name(const char* name);
rfc1035_response& set_qid(unsigned short id);
rfc1035_response& set_type(rfc1035_type_t type);
rfc1035_response& set_ttl(int ttl);
const char* get_name(void) const {
return name_;
}
unsigned short get_qid(void) const {
return qid_;
}
rfc1035_type_t get_type(void) const {
return type_;
}
int get_ttl(void) const {
return ttl_;
}
public:
size_t build_reply(const std::vector<string>& addrs,
char* buf, size_t size);
bool parse_reply(const void* buf, size_t len);
private:
char name_[256];
unsigned short qid_;
rfc1035_type_t type_;
int ttl_;
std::vector<string> addrs4a_;
std::vector<string> addrs4aaaa_;
std::vector<string> cnames_;
};
} // namespace acl

View File

@ -488,6 +488,13 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
</File>
</Filter>
</Filter>
<Filter
Name="net"
Filter="">
<File
RelativePath=".\src\net\rfc1035.cpp">
</File>
</Filter>
<Filter
Name="mime"
Filter="">
@ -1241,6 +1248,13 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
RelativePath=".\include\acl_cpp\stdlib\zlib_stream.hpp">
</File>
</Filter>
<Filter
Name="net"
Filter="">
<File
RelativePath=".\include\acl_cpp\net\rfc1035.hpp">
</File>
</Filter>
<Filter
Name="mime"
Filter="">

View File

@ -862,6 +862,15 @@
>
</File>
</Filter>
<Filter
Name="net"
>
<File
RelativePath=".\src\net\rfc1035.cpp"
>
</File>
</Filter>
<Filter
Name="redis"
>
@ -1724,6 +1733,14 @@
>
</File>
</Filter>
<Filter
Name="net"
>
<File
RelativePath=".\include\acl_cpp\net\rfc1035.hpp"
>
</File>
</Filter>
<Filter
Name="redis"
>

View File

@ -305,6 +305,7 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
<ClCompile Include="src\mime\mime_xxcode.cpp" />
<ClCompile Include="src\mime\rfc2047.cpp" />
<ClCompile Include="src\mime\rfc822.cpp" />
<ClCompile Include="src\net\rfc1035.cpp" />
<ClCompile Include="src\queue\queue_file.cpp" />
<ClCompile Include="src\queue\queue_manager.cpp" />
<ClCompile Include="src\redis\redis.cpp" />
@ -501,6 +502,7 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\mime\mime_xxcode.hpp" />
<ClInclude Include="include\acl_cpp\mime\rfc2047.hpp" />
<ClInclude Include="include\acl_cpp\mime\rfc822.hpp" />
<ClInclude Include="include\acl_cpp\mime\rfc1035.hpp" />
<ClInclude Include="include\acl_cpp\queue\queue_file.hpp" />
<ClInclude Include="include\acl_cpp\queue\queue_manager.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis.hpp" />

View File

@ -49,6 +49,9 @@
<ClCompile Include="src\mime\rfc822.cpp">
<Filter>src\mime</Filter>
</ClCompile>
<ClCompile Include="src\net\rfc1035.cpp">
<Filter>src\net</Filter>
</ClCompile>
<ClCompile Include="src\master\master_trigger.cpp">
<Filter>src\master</Filter>
</ClCompile>
@ -822,6 +825,9 @@
<ClInclude Include="include\acl_cpp\mime\rfc822.hpp">
<Filter>include\mime</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\net\rfc1035.hpp">
<Filter>include\net</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\session\session.hpp">
<Filter>include\session</Filter>
</ClInclude>

View File

@ -499,6 +499,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClCompile Include="src\mime\mime_xxcode.cpp" />
<ClCompile Include="src\mime\rfc2047.cpp" />
<ClCompile Include="src\mime\rfc822.cpp" />
<ClCompile Include="src\mime\rfc1035.cpp" />
<ClCompile Include="src\queue\queue_file.cpp" />
<ClCompile Include="src\queue\queue_manager.cpp" />
<ClCompile Include="src\redis\redis.cpp" />
@ -695,6 +696,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\mime\mime_xxcode.hpp" />
<ClInclude Include="include\acl_cpp\mime\rfc2047.hpp" />
<ClInclude Include="include\acl_cpp\mime\rfc822.hpp" />
<ClInclude Include="include\acl_cpp\mime\rfc1035.hpp" />
<ClInclude Include="include\acl_cpp\queue\queue_file.hpp" />
<ClInclude Include="include\acl_cpp\queue\queue_manager.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis.hpp" />

View File

@ -49,6 +49,9 @@
<ClCompile Include="src\mime\rfc822.cpp">
<Filter>Source Files\mime</Filter>
</ClCompile>
<ClCompile Include="src\net\rfc1035.cpp">
<Filter>Source Files\net</Filter>
</ClCompile>
<ClCompile Include="src\master\master_trigger.cpp">
<Filter>Source Files\master</Filter>
</ClCompile>
@ -816,6 +819,9 @@
<ClInclude Include="include\acl_cpp\mime\rfc822.hpp">
<Filter>Header Files\mime</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\net\rfc1035.hpp">
<Filter>Header Files\net</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\session\memcache_session.hpp">
<Filter>Header Files\session</Filter>
</ClInclude>

View File

@ -499,7 +499,8 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClCompile Include="src\mime\mime_uucode.cpp" />
<ClCompile Include="src\mime\mime_xxcode.cpp" />
<ClCompile Include="src\mime\rfc2047.cpp" />
<ClCompile Include="src\mime\rfc822.cpp" />
<ClCompile Include="src\net\rfc822.cpp" />
<ClCompile Include="src\mime\rfc1035.cpp" />
<ClCompile Include="src\queue\queue_file.cpp" />
<ClCompile Include="src\queue\queue_manager.cpp" />
<ClCompile Include="src\redis\redis.cpp" />
@ -696,6 +697,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\mime\mime_xxcode.hpp" />
<ClInclude Include="include\acl_cpp\mime\rfc2047.hpp" />
<ClInclude Include="include\acl_cpp\mime\rfc822.hpp" />
<ClInclude Include="include\acl_cpp\net\rfc1035.hpp" />
<ClInclude Include="include\acl_cpp\queue\queue_file.hpp" />
<ClInclude Include="include\acl_cpp\queue\queue_manager.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis.hpp" />

View File

@ -49,6 +49,9 @@
<ClCompile Include="src\mime\rfc822.cpp">
<Filter>Source Files\mime</Filter>
</ClCompile>
<ClCompile Include="src\net\rfc1035.cpp">
<Filter>Source Files\net</Filter>
</ClCompile>
<ClCompile Include="src\master\master_trigger.cpp">
<Filter>Source Files\master</Filter>
</ClCompile>
@ -816,6 +819,9 @@
<ClInclude Include="include\acl_cpp\mime\rfc822.hpp">
<Filter>Header Files\mime</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\net\rfc1035.hpp">
<Filter>Header Files\net</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\session\memcache_session.hpp">
<Filter>Header Files\session</Filter>
</ClInclude>

View File

@ -500,6 +500,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClCompile Include="src\mime\mime_xxcode.cpp" />
<ClCompile Include="src\mime\rfc2047.cpp" />
<ClCompile Include="src\mime\rfc822.cpp" />
<ClCompile Include="src\net\rfc1035.cpp" />
<ClCompile Include="src\queue\queue_file.cpp" />
<ClCompile Include="src\queue\queue_manager.cpp" />
<ClCompile Include="src\redis\redis.cpp" />
@ -696,6 +697,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\mime\mime_xxcode.hpp" />
<ClInclude Include="include\acl_cpp\mime\rfc2047.hpp" />
<ClInclude Include="include\acl_cpp\mime\rfc822.hpp" />
<ClInclude Include="include\acl_cpp\net\rfc1035.hpp" />
<ClInclude Include="include\acl_cpp\queue\queue_file.hpp" />
<ClInclude Include="include\acl_cpp\queue\queue_manager.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis.hpp" />

View File

@ -49,6 +49,9 @@
<ClCompile Include="src\mime\rfc822.cpp">
<Filter>Source Files\mime</Filter>
</ClCompile>
<ClCompile Include="src\net\rfc1035.cpp">
<Filter>Source Files\net</Filter>
</ClCompile>
<ClCompile Include="src\master\master_trigger.cpp">
<Filter>Source Files\master</Filter>
</ClCompile>
@ -816,6 +819,9 @@
<ClInclude Include="include\acl_cpp\mime\rfc822.hpp">
<Filter>Header Files\mime</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\net\rfc1035.hpp">
<Filter>Header Files\net</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\session\memcache_session.hpp">
<Filter>Header Files\session</Filter>
</ClInclude>

View File

@ -500,6 +500,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClCompile Include="src\mime\mime_xxcode.cpp" />
<ClCompile Include="src\mime\rfc2047.cpp" />
<ClCompile Include="src\mime\rfc822.cpp" />
<ClCompile Include="src\net\rfc1035.cpp" />
<ClCompile Include="src\queue\queue_file.cpp" />
<ClCompile Include="src\queue\queue_manager.cpp" />
<ClCompile Include="src\redis\redis.cpp" />
@ -696,6 +697,7 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\mime\mime_xxcode.hpp" />
<ClInclude Include="include\acl_cpp\mime\rfc2047.hpp" />
<ClInclude Include="include\acl_cpp\mime\rfc822.hpp" />
<ClInclude Include="include\acl_cpp\net\rfc1035.hpp" />
<ClInclude Include="include\acl_cpp\queue\queue_file.hpp" />
<ClInclude Include="include\acl_cpp\queue\queue_manager.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis.hpp" />
@ -830,4 +832,4 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -49,6 +49,9 @@
<ClCompile Include="src\mime\rfc822.cpp">
<Filter>Source Files\mime</Filter>
</ClCompile>
<ClCompile Include="src\net\rfc1035.cpp">
<Filter>Source Files\net</Filter>
</ClCompile>
<ClCompile Include="src\master\master_trigger.cpp">
<Filter>Source Files\master</Filter>
</ClCompile>
@ -816,6 +819,9 @@
<ClInclude Include="include\acl_cpp\mime\rfc822.hpp">
<Filter>Header Files\mime</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\net\rfc1035.hpp">
<Filter>Header Files\net</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\session\memcache_session.hpp">
<Filter>Header Files\session</Filter>
</ClInclude>

View File

@ -21,6 +21,7 @@ LOCAL_SRC_FILES := $(wildcard ./*.cpp) \
$(wildcard $(SRC_PATH)/smtp/*.cpp) \
$(wildcard $(SRC_PATH)/mime/*.cpp) \
$(wildcard $(SRC_PATH)/mime/internal/*.cpp) \
$(wildcard $(SRC_PATH)/net/*.cpp) \
$(wildcard $(SRC_PATH)/connpool/*.cpp) \
$(wildcard $(SRC_PATH)/redis/*.cpp) \
$(wildcard $(SRC_PATH)/disque/*.cpp) \

View File

@ -0,0 +1,245 @@
//
// Created by shuxin ¡¡¡¡zheng on 2020/11/14.
//
#include "acl_stdafx.hpp"
#ifndef ACL_PREPARE_COMPILE
#include "acl_cpp/net/rfc1035.hpp"
#endif
#define STRNCPY ACL_SAFE_STRNCPY
namespace acl {
rfc1035_request::rfc1035_request(void) {
name_[0] = 0;
qid_ = 0;
type_ = rfc1035_type_null;
}
rfc1035_request::~rfc1035_request(void) {}
rfc1035_request& rfc1035_request::set_name(const char *name) {
STRNCPY(name_, name, sizeof(name_));
return *this;
}
rfc1035_request& rfc1035_request::set_qid(unsigned short id) {
qid_ = id;
return *this;
}
rfc1035_request& rfc1035_request::set_type(rfc1035_type_t type) {
type_ = type;
return *this;
}
size_t rfc1035_request::build_query(char* buf, size_t size) {
if (name_[0] == 0) {
logger_error("name not set, call set_name first");
return 0;
}
if (size < 512) {
logger_error("buf' size(%ld) < 512 too small", (long) size);
return 0;
}
switch (type_) {
case rfc1035_type_a:
return acl_rfc1035_build_query4a(name_, buf, size, qid_, NULL);
case rfc1035_type_aaaa:
return acl_rfc1035_build_query4aaaa(name_, buf, size, qid_, NULL);
case rfc1035_type_mx:
return acl_rfc1035_build_query4mx(name_, buf, size, qid_, NULL);
default:
logger_error("type(%d) not supported", (int) type_);
return 0;
}
}
size_t rfc1035_request::build_query4ptr(const struct in_addr addr,
char* buf, size_t size) {
if (size < 512) {
logger_error("buf' size(%ld) < 512 too small", (long) size);
return 0;
}
return acl_rfc1035_build_query4ptr(addr, buf, size, qid_, NULL);
}
bool rfc1035_request::parse_request(const void *buf, size_t len) {
ACL_RFC1035_MESSAGE* message =
acl_rfc1035_request_unpack((const char*) buf, len);
if (message == NULL) {
return false;
}
ACL_RFC1035_QUERY *query = message->query;
if (query == NULL || query->name[0] == 0) {
acl_rfc1035_message_destroy(message);
return false;
}
qid_ = message->id;
STRNCPY(name_, query->name, sizeof(name_));
switch (query->qtype) {
case ACL_RFC1035_TYPE_A:
type_ = rfc1035_type_a;
break;
case ACL_RFC1035_TYPE_AAAA:
type_ = rfc1035_type_aaaa;
break;
default:
logger_error("type(%d) not supported", (int) type_);
acl_rfc1035_message_destroy(message);
return false;
}
acl_rfc1035_message_destroy(message);
return true;
}
//////////////////////////////////////////////////////////////////////////////
rfc1035_response::rfc1035_response(void) {
name_[0] = 0;
qid_ = 0;
type_ = rfc1035_type_null;
ttl_ = 0;
}
rfc1035_response::~rfc1035_response(void) {}
rfc1035_response& rfc1035_response::set_name(const char *name) {
STRNCPY(name_, name, sizeof(name_));
return *this;
}
rfc1035_response& rfc1035_response::set_qid(unsigned short id) {
qid_ = id;
return *this;
}
rfc1035_response& rfc1035_response::set_type(rfc1035_type_t type) {
type_ = type;
return *this;
}
rfc1035_response& rfc1035_response::set_ttl(int ttl) {
ttl_ = ttl;
return *this;
}
size_t rfc1035_response::build_reply(const std::vector<string>& addrs,
char *buf, size_t size) {
if (size < 512) {
logger_error("buf' size(%ld) < 512 too small", (long) size);
return 0;
}
int type;
switch (type_) {
case rfc1035_type_a:
type = ACL_RFC1035_TYPE_A;
break;
case rfc1035_type_aaaa:
type = ACL_RFC1035_TYPE_A;
break;
default:
logger_error("type(%d) not supported", (int) type_);
return 0;
}
ACL_ARGV* ips = acl_argv_alloc(5);
for (std::vector<string>::const_iterator cit = addrs.begin();
cit != addrs.end(); ++cit) {
acl_argv_add(ips, (*cit).c_str(), NULL);
}
ACL_RFC1035_REPLY reply;
reply.ip_type = type;
reply.hostname = name_;
#if 0
reply.domain_root = ".dns.com";
reply.dns_name = "ipv6-static.dns.com";
reply.domain_root = reply.dns_name;
reply.dns_ip = "127.0.0.1";
#else
reply.domain_root = NULL;
reply.dns_name = NULL;
reply.dns_ip = NULL;
#endif
reply.ips = ips;
reply.ttl = ttl_;
reply.qid = qid_;
size_t n = acl_rfc1035_build_reply(&reply, buf, size);
acl_argv_free(ips);
return n;
}
bool rfc1035_response::parse_reply(const void *buf, size_t len) {
ACL_RFC1035_MESSAGE* message =
acl_rfc1035_response_unpack((const char* ) buf, len);
if (message == NULL) {
return false;
}
qid_ = message->id;
for (unsigned short i = 0; i < message->ancount; i++) {
if (message->answer[i].type == ACL_RFC1035_TYPE_A) {
struct sockaddr_in in;
size_t n = message->answer[i].rdlength > 4
? 4 : message->answer[i].rdlength;
memcpy(&in.sin_addr, message->answer[i].rdata, n);
in.sin_family = AF_INET;
char ip[64];
if (acl_inet_ntop((const struct sockaddr*) &in,
ip, sizeof(ip))) {
addrs4a_.push_back(ip);
}
#ifdef AF_INET6
} else if (message->answer[i].type == ACL_RFC1035_TYPE_AAAA) {
struct sockaddr_in6 in;
size_t n = message->answer[i].rdlength > 16
? 16 : message->answer[i].rdlength;
memcpy(&in.sin6_addr, message->answer[i].rdata, n);
in.sin6_family = AF_INET6;
char ip[64];
if (acl_inet_ntop((const struct sockaddr*) &in,
ip, sizeof(ip))) {
addrs4aaaa_.push_back(ip);
}
#endif
} else if (message->answer[i].type == ACL_RFC1035_TYPE_CNAME) {
char cname[256];
len = sizeof(cname) - 1;
if (len > message->answer[i].rdlength) {
len = message->answer[i].rdlength;
}
memcpy(cname, message->answer[i].rdata, len);
cname[len] = 0;
cnames_.push_back(cname);
continue;
} else {
continue;
}
if (name_[0] == 0 && message->answer[i].name[0] != 0) {
STRNCPY(name_, message->answer[i].name, sizeof(name_));
}
}
acl_rfc1035_message_destroy(message);
return true;
}
} // namespace acl