Skip to content
/ dns.cc Public

A lightweight DNS client that can resolve queries for A and AAAA records.

License

Notifications You must be signed in to change notification settings

CandyMi/dns.cc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

介绍

一个轻量级的DNS客户端, 实现对AAAAA记录的查询解析.

优势

  • 使用现代C++11标准编写, 可同时支持C/C++使用.

  • 跨平台兼容性好, 方便自行交叉编译. 使用简单、接口稳定.

  • 同时支持IPv4IPv6, 无需开发者编写多余判断代码.

  • 可使用CMAKE构建二进制文件, 也可自行拷贝源文件自行编译.

  • 代码干净并无任何系统调用, 让你轻松集成到任何现有框架内.

  • 同时在阻塞和非阻塞环境中使用, 让查询变得更加简单.

平台

  • Windows

  • Linux

  • MacOS

  • FreeBSD

编译器

  • gcc

  • clang

  • msvc

示例

查询: 通过dns_query生成查询内容, 开发者自行发送至对端socket.

响应: 通过dns_parse解析响应内容, answers数量与回调次数一致.

/* main.c */
#include "stdio.h"
#include "string.h"

#include "dns.h"

void cb(void *udata, const dns_ans_t *ans)
{
  dns_type_t  t =  dns_get_type(ans);
  const char *ip = dns_get_ip(ans);
  printf("回调: type = %d, ip = '%s', 字符串长度: %d\n", t, ip, strlen(ip));
}

int main(int argc, char const *argv[])
{
  const char resp[] = ".o\201\200\000\001\000\002\000\004\000\b\004ipv6\006google\003com\000\000\034\000\001\300\f\000\005\000\001\000\000\000i\000\t\004ipv6\001l\300\021\300-\000\034\000\001\000\000\001,\000\020$\004h\000@\005\b\021\000\000\000\000\000\000 \016\300\021\000\002\000\001\000\003\372\334\000\006\003ns2\300\021\300\021\000\002\000\001\000\003\372\334\000\006\003ns3\300\021\300\021\000\002\000\001\000\003\372\334\000\006\003ns1\300\021\300\021\000\002\000\001\000\003\372\334\000\006\003ns4\300\021\300\202\000\001\000\001\000\000\365A\000\004\330\357 \n\300\202\000\034\000\001\000\000\365A\000\020 \001H`H\002\0002\000\000\000\000\000\000\000\n\300^\000\001\000\001\000\000\365A\000\004\330\357\"\n\300^\000\034\000\001\000\000\365A\000\020 \001H`H\002\0004\000\000\000\000\000\000\000\n\300p\000\001\000\001\000\000\365A\000\004\330\357$\n\300p\000\034\000\001\000\000\365A\000\020 \001H`H\002\0006\000\000\000\000\000\000\000\n\300\224\000\001\000\001\000\000\365A\000\004\330\357&\n\300\224\000\034\000\001\000\000\365A\000\020 \001H`H\002\0008\000\000\000\000\000\000\000\n";

  dns_req_t *req = dns_new();
  dns_set_unsafe();

  char data[10086]; int r = 0;

  r = dns_query(req, A, "ipv6.google.com", data);
  printf("q r = %d\n", r);

  r = dns_parse(req, resp, NULL, cb);
  printf("r r = %d\n", r);

  dns_destory(req);
  return 0;
}

测试

编译运行

c++ -o adns.so dns.cc -fPIC -shared -O0 -g -std=c++11
cc -o main main.c -O0 -g ./adns.so -fsanitize=address && ./main

输出结果

q r = 33
回调: type = 5, ip = 'ipv6.l.google.com', 字符串长度: 17
回调: type = 28, ip = '2404:6800:4005:0811:0000:0000:0000:200e', 字符串长度: 39
r r = 0

构建系统

请参阅CMakeLists.txt

About

A lightweight DNS client that can resolve queries for A and AAAA records.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published