Update code
This commit is contained in:
228
dns.c
228
dns.c
@@ -1,8 +1,34 @@
|
|||||||
|
/*************************************************************************
|
||||||
|
*
|
||||||
|
* Copyright (C) 2018 Ruilin Peng (Nick) <pymumu@gmail.com>.
|
||||||
|
*
|
||||||
|
* smartdns is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* smartdns is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "dns.h"
|
#include "dns.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define DNS_MAX_CNAME_LEN 128
|
#define QR_MASK 0x8000
|
||||||
|
#define OPCODE_MASK 0x7800
|
||||||
|
#define AA_MASK 0x0400
|
||||||
|
#define TC_MASK 0x0200
|
||||||
|
#define RD_MASK 0x0100
|
||||||
|
#define RA_MASK 0x8000
|
||||||
|
#define RCODE_MASK 0x000F
|
||||||
|
|
||||||
|
#define DNS_RR_END (0XFFFF)
|
||||||
|
|
||||||
short dns_read_short(unsigned char **buffer)
|
short dns_read_short(unsigned char **buffer)
|
||||||
{
|
{
|
||||||
@@ -51,7 +77,7 @@ unsigned int dns_read_int(unsigned char **buffer)
|
|||||||
return ntohs(value);
|
return ntohs(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct dns_rrs *dns_get_rrs_start(struct dns_packet *packet, int type, int *count)
|
struct dns_rrs *dns_get_rrs_start(struct dns_packet *packet, dns_rr_type type, int *count)
|
||||||
{
|
{
|
||||||
unsigned short start;
|
unsigned short start;
|
||||||
struct dns_head *head = &packet->head;
|
struct dns_head *head = &packet->head;
|
||||||
@@ -156,7 +182,6 @@ int dns_rr_add_end(struct dns_packet *packet, int type, dns_type_t rrtype, int l
|
|||||||
*count += 1;
|
*count += 1;
|
||||||
rrs->len = len;
|
rrs->len = len;
|
||||||
rrs->type = rrtype;
|
rrs->type = rrtype;
|
||||||
//*start = packet->len;
|
|
||||||
packet->len += len + sizeof(*rrs);
|
packet->len += len + sizeof(*rrs);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -268,7 +293,7 @@ int _dns_get_rr_head(struct dns_data_context *data_context, char *domain, int ma
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
int dns_add_CNAME(struct dns_packet *packet, char *domain, int ttl, char *cname)
|
int dns_add_CNAME(struct dns_packet *packet, dns_rr_type type, char *domain, int ttl, char *cname)
|
||||||
{
|
{
|
||||||
int maxlen = 0;
|
int maxlen = 0;
|
||||||
int len = 0;
|
int len = 0;
|
||||||
@@ -298,7 +323,7 @@ int dns_add_CNAME(struct dns_packet *packet, char *domain, int ttl, char *cname)
|
|||||||
data_context.ptr += rr_len;
|
data_context.ptr += rr_len;
|
||||||
len = data_context.ptr - data_context.data;
|
len = data_context.ptr - data_context.data;
|
||||||
|
|
||||||
return dns_rr_add_end(packet, DNS_RRS_AN, DNS_T_CNAME, len);
|
return dns_rr_add_end(packet, type, DNS_T_CNAME, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int dns_get_CNAME(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, char *cname, int cname_size)
|
int dns_get_CNAME(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, char *cname, int cname_size)
|
||||||
@@ -333,7 +358,7 @@ int dns_get_CNAME(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, char
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int dns_add_A(struct dns_packet *packet, char *domain, int ttl, unsigned char addr[4])
|
int dns_add_A(struct dns_packet *packet, dns_rr_type type, char *domain, int ttl, unsigned char addr[4])
|
||||||
{
|
{
|
||||||
int maxlen = 0;
|
int maxlen = 0;
|
||||||
int len = 0;
|
int len = 0;
|
||||||
@@ -357,7 +382,17 @@ int dns_add_A(struct dns_packet *packet, char *domain, int ttl, unsigned char ad
|
|||||||
data_context.ptr += DNS_RR_A_LEN;
|
data_context.ptr += DNS_RR_A_LEN;
|
||||||
len = data_context.ptr - data_context.data;
|
len = data_context.ptr - data_context.data;
|
||||||
|
|
||||||
return dns_rr_add_end(packet, DNS_RRS_AN, DNS_T_A, len);
|
return dns_rr_add_end(packet, type, DNS_T_A, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
int dns_add_PTR(struct dns_packet *packet, dns_rr_type type, char *domain, int ttl, char *cname)
|
||||||
|
{
|
||||||
|
return dns_add_CNAME(packet, type, domain, ttl, cname);
|
||||||
|
}
|
||||||
|
|
||||||
|
int dns_get_PTR(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, char *cname, int cname_size)
|
||||||
|
{
|
||||||
|
return dns_get_CNAME(rrs, domain, maxsize, ttl, cname, cname_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
int dns_get_A(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, unsigned char addr[4])
|
int dns_get_A(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, unsigned char addr[4])
|
||||||
@@ -392,7 +427,7 @@ int dns_get_A(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, unsigned
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int dns_add_AAAA(struct dns_packet *packet, char *domain, int ttl, unsigned char addr[16])
|
int dns_add_AAAA(struct dns_packet *packet, dns_rr_type type, char *domain, int ttl, unsigned char addr[16])
|
||||||
{
|
{
|
||||||
int maxlen = 0;
|
int maxlen = 0;
|
||||||
int len = 0;
|
int len = 0;
|
||||||
@@ -416,7 +451,7 @@ int dns_add_AAAA(struct dns_packet *packet, char *domain, int ttl, unsigned char
|
|||||||
data_context.ptr += DNS_RR_AAAA_LEN;
|
data_context.ptr += DNS_RR_AAAA_LEN;
|
||||||
len = data_context.ptr - data_context.data;
|
len = data_context.ptr - data_context.data;
|
||||||
|
|
||||||
return dns_rr_add_end(packet, DNS_RRS_AN, DNS_T_AAAA, len);
|
return dns_rr_add_end(packet, type, DNS_T_AAAA, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int dns_get_AAAA(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, unsigned char addr[16])
|
int dns_get_AAAA(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, unsigned char addr[16])
|
||||||
@@ -566,7 +601,7 @@ int _dns_decode_domain(struct dns_context *context, char *output, int size)
|
|||||||
while (1) {
|
while (1) {
|
||||||
len = *ptr;
|
len = *ptr;
|
||||||
if (len == 0) {
|
if (len == 0) {
|
||||||
*output = 0;
|
*(output - 1) = 0;
|
||||||
ptr++;
|
ptr++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -801,12 +836,60 @@ int _dns_encode_A(struct dns_context *context, struct dns_rrs *rrs)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(context->ptr, rrs->data, DNS_RR_A_LEN);
|
memcpy(context->ptr, data_context.ptr, DNS_RR_A_LEN);
|
||||||
context->ptr += DNS_RR_A_LEN;
|
context->ptr += DNS_RR_A_LEN;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int _dns_decode_PTR(struct dns_context *context, char *name, int name_size)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = _dns_decode_domain(context, name, name_size);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _dns_encode_PTR(struct dns_context *context, struct dns_rrs *rrs)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
int qtype = 0;
|
||||||
|
int qclass = 0;
|
||||||
|
int ttl = 0;
|
||||||
|
char domain[DNS_MAX_CNAME_LEN];
|
||||||
|
int rr_len;
|
||||||
|
struct dns_data_context data_context;
|
||||||
|
|
||||||
|
data_context.data = rrs->data;
|
||||||
|
data_context.ptr = rrs->data;
|
||||||
|
data_context.maxsize = rrs->len;
|
||||||
|
|
||||||
|
ret = _dns_get_rr_head(&data_context, domain, DNS_MAX_CNAME_LEN, &qtype, &qclass, &ttl, &rr_len);
|
||||||
|
if (ret < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rr_len >= data_context.maxsize) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = _dns_encode_rr_head(context, domain, qtype, qclass, ttl, rr_len);
|
||||||
|
if (ret < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_dns_left_len(context) < rr_len) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = _dns_encode_domain(context, (char *)data_context.ptr);
|
||||||
|
if (ret < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int _dns_decode_AAAA(struct dns_context *context, unsigned char addr[DNS_RR_AAAA_LEN])
|
int _dns_decode_AAAA(struct dns_context *context, unsigned char addr[DNS_RR_AAAA_LEN])
|
||||||
{
|
{
|
||||||
if (_dns_left_len(context) < DNS_RR_AAAA_LEN) {
|
if (_dns_left_len(context) < DNS_RR_AAAA_LEN) {
|
||||||
@@ -850,12 +933,61 @@ int _dns_encode_AAAA(struct dns_context *context, struct dns_rrs *rrs)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(context->ptr, rrs->data, DNS_RR_AAAA_LEN);
|
memcpy(context->ptr, context->ptr, DNS_RR_AAAA_LEN);
|
||||||
context->ptr += DNS_RR_AAAA_LEN;
|
context->ptr += DNS_RR_AAAA_LEN;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int _dns_decode_NS(struct dns_context *context, unsigned char addr[4])
|
||||||
|
{
|
||||||
|
if (_dns_left_len(context) < DNS_RR_A_LEN) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(addr, context->ptr, DNS_RR_A_LEN);
|
||||||
|
context->ptr += DNS_RR_A_LEN;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _dns_encode_NS(struct dns_context *context, struct dns_rrs *rrs)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
int qtype = 0;
|
||||||
|
int qclass = 0;
|
||||||
|
int ttl = 0;
|
||||||
|
char domain[DNS_MAX_CNAME_LEN];
|
||||||
|
int rr_len;
|
||||||
|
struct dns_data_context data_context;
|
||||||
|
|
||||||
|
data_context.data = rrs->data;
|
||||||
|
data_context.ptr = rrs->data;
|
||||||
|
data_context.maxsize = rrs->len;
|
||||||
|
|
||||||
|
ret = _dns_get_rr_head(&data_context, domain, DNS_MAX_CNAME_LEN, &qtype, &qclass, &ttl, &rr_len);
|
||||||
|
if (ret < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rr_len != DNS_RR_A_LEN) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = _dns_encode_rr_head(context, domain, qtype, qclass, ttl, DNS_RR_A_LEN);
|
||||||
|
if (ret < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_dns_left_len(context) < DNS_RR_A_LEN) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(context->ptr, rrs->data, DNS_RR_A_LEN);
|
||||||
|
context->ptr += DNS_RR_A_LEN;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int _dns_decode_qd(struct dns_context *context)
|
int _dns_decode_qd(struct dns_context *context)
|
||||||
{
|
{
|
||||||
struct dns_packet *packet = context->packet;
|
struct dns_packet *packet = context->packet;
|
||||||
@@ -877,7 +1009,7 @@ int _dns_decode_qd(struct dns_context *context)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int _dns_decode_an(struct dns_context *context)
|
int _dns_decode_an(struct dns_context *context, dns_rr_type type)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
int qtype = 0;
|
int qtype = 0;
|
||||||
@@ -886,25 +1018,15 @@ int _dns_decode_an(struct dns_context *context)
|
|||||||
int rr_len = 0;
|
int rr_len = 0;
|
||||||
char domain[DNS_MAX_CNAME_LEN];
|
char domain[DNS_MAX_CNAME_LEN];
|
||||||
struct dns_packet *packet = context->packet;
|
struct dns_packet *packet = context->packet;
|
||||||
|
unsigned char *start;
|
||||||
|
|
||||||
ret = _dns_decode_rr_head(context, domain, DNS_MAX_CNAME_LEN, &qtype, &qclass, &ttl, &rr_len);
|
ret = _dns_decode_rr_head(context, domain, DNS_MAX_CNAME_LEN, &qtype, &qclass, &ttl, &rr_len);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
start = context->ptr;
|
||||||
|
|
||||||
switch (qtype) {
|
switch (qtype) {
|
||||||
case DNS_T_CNAME: {
|
|
||||||
char cname[DNS_MAX_CNAME_LEN];
|
|
||||||
ret = _dns_decode_CNAME(context, cname, DNS_MAX_CNAME_LEN);
|
|
||||||
if (ret < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = dns_add_CNAME(packet, domain, ttl, cname);
|
|
||||||
if (ret < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
} break;
|
|
||||||
case DNS_T_A: {
|
case DNS_T_A: {
|
||||||
unsigned char addr[DNS_RR_A_LEN];
|
unsigned char addr[DNS_RR_A_LEN];
|
||||||
ret = _dns_decode_A(context, addr);
|
ret = _dns_decode_A(context, addr);
|
||||||
@@ -912,7 +1034,31 @@ int _dns_decode_an(struct dns_context *context)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = dns_add_A(packet, domain, ttl, addr);
|
ret = dns_add_A(packet, type, domain, ttl, addr);
|
||||||
|
if (ret < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case DNS_T_CNAME: {
|
||||||
|
char cname[DNS_MAX_CNAME_LEN];
|
||||||
|
ret = _dns_decode_CNAME(context, cname, DNS_MAX_CNAME_LEN);
|
||||||
|
if (ret < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = dns_add_CNAME(packet, type, domain, ttl, cname);
|
||||||
|
if (ret < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case DNS_T_PTR: {
|
||||||
|
char name[DNS_MAX_CNAME_LEN];
|
||||||
|
ret = _dns_decode_PTR(context, name, DNS_MAX_CNAME_LEN);
|
||||||
|
if (ret < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = dns_add_PTR(packet, type, domain, ttl, name);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -924,7 +1070,7 @@ int _dns_decode_an(struct dns_context *context)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = dns_add_AAAA(packet, domain, ttl, addr);
|
ret = dns_add_AAAA(packet, type, domain, ttl, addr);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -934,6 +1080,10 @@ int _dns_decode_an(struct dns_context *context)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (context->ptr - start != rr_len) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -972,6 +1122,12 @@ int _dns_encode_an(struct dns_context *context, struct dns_rrs *rrs)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
case DNS_T_PTR:
|
||||||
|
ret = _dns_encode_PTR(context, rrs);
|
||||||
|
if (ret < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case DNS_T_AAAA:
|
case DNS_T_AAAA:
|
||||||
ret = _dns_encode_AAAA(context, rrs);
|
ret = _dns_encode_AAAA(context, rrs);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
@@ -1001,13 +1157,29 @@ int _dns_decode_body(struct dns_context *context)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < head->ancount; i++) {
|
for (i = 0; i < head->ancount; i++) {
|
||||||
ret = _dns_decode_an(context);
|
ret = _dns_decode_an(context, DNS_RRS_AN);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
head->ancount--;
|
head->ancount--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < head->nscount; i++) {
|
||||||
|
ret = _dns_decode_an(context, DNS_RRS_NS);
|
||||||
|
if (ret < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
head->nscount--;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < head->nrcount; i++) {
|
||||||
|
ret = _dns_decode_an(context, DNS_RRS_NR);
|
||||||
|
if (ret < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
head->nrcount--;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
61
dns.h
61
dns.h
@@ -6,23 +6,17 @@
|
|||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#define QR_MASK 0x8000
|
|
||||||
#define OPCODE_MASK 0x7800
|
|
||||||
#define AA_MASK 0x0400
|
|
||||||
#define TC_MASK 0x0200
|
|
||||||
#define RD_MASK 0x0100
|
|
||||||
#define RA_MASK 0x8000
|
|
||||||
#define RCODE_MASK 0x000F
|
|
||||||
|
|
||||||
#define DNS_RR_A_LEN 4
|
#define DNS_RR_A_LEN 4
|
||||||
#define DNS_RR_AAAA_LEN 16
|
#define DNS_RR_AAAA_LEN 16
|
||||||
|
#define DNS_MAX_CNAME_LEN 128
|
||||||
|
|
||||||
#define DNS_RRS_QD 0
|
typedef enum dns_rr_type {
|
||||||
#define DNS_RRS_AN 1
|
DNS_RRS_QD = 0,
|
||||||
#define DNS_RRS_NS 2
|
DNS_RRS_AN = 1,
|
||||||
#define DNS_RRS_NR 3
|
DNS_RRS_NS = 2,
|
||||||
|
DNS_RRS_NR = 3,
|
||||||
#define DNS_RR_END (0XFFFF)
|
DNS_RRS_END = 4,
|
||||||
|
} dns_rr_type;
|
||||||
|
|
||||||
typedef enum dns_class { DNS_C_IN = 1, DNS_C_ANY = 255 } dns_class_t;
|
typedef enum dns_class { DNS_C_IN = 1, DNS_C_ANY = 255 } dns_class_t;
|
||||||
|
|
||||||
@@ -100,8 +94,7 @@ struct dns_packet {
|
|||||||
unsigned char data[0];
|
unsigned char data[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct dns_data_context
|
struct dns_data_context {
|
||||||
{
|
|
||||||
unsigned char *data;
|
unsigned char *data;
|
||||||
unsigned char *ptr;
|
unsigned char *ptr;
|
||||||
unsigned int maxsize;
|
unsigned int maxsize;
|
||||||
@@ -116,23 +109,33 @@ struct dns_context {
|
|||||||
|
|
||||||
struct dns_rrs *dns_get_rrs_next(struct dns_packet *packet, struct dns_rrs *rrs);
|
struct dns_rrs *dns_get_rrs_next(struct dns_packet *packet, struct dns_rrs *rrs);
|
||||||
|
|
||||||
struct dns_rrs *dns_get_rrs_start(struct dns_packet *packet, int type, int *count);
|
struct dns_rrs *dns_get_rrs_start(struct dns_packet *packet, dns_rr_type type, int *count);
|
||||||
|
|
||||||
int dns_add_CNAME(struct dns_packet *packet, char *domain, int ttl, char *cname);
|
/*
|
||||||
|
* Question
|
||||||
int dns_get_CNAME(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, char *cname, int cname_size);
|
*/
|
||||||
|
int dns_add_domain(struct dns_packet *packet, char *domain, int qtype, int qclass);
|
||||||
int dns_add_A(struct dns_packet *packet, char *domain, int ttl, unsigned char addr[4]);
|
|
||||||
|
|
||||||
int dns_get_A(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, unsigned char addr[4]);
|
|
||||||
|
|
||||||
int dns_add_AAAA(struct dns_packet *packet, char *domain, int ttl, unsigned char addr[16]);
|
|
||||||
|
|
||||||
int dns_get_AAAA(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, unsigned char addr[16]);
|
|
||||||
|
|
||||||
int dns_get_domain(struct dns_rrs *rrs, char *domain, int maxsize, int *qtype, int *qclass);
|
int dns_get_domain(struct dns_rrs *rrs, char *domain, int maxsize, int *qtype, int *qclass);
|
||||||
|
|
||||||
int dns_add_domain(struct dns_packet *packet, char *domain, int qtype, int qclass);
|
/*
|
||||||
|
* Answers
|
||||||
|
*/
|
||||||
|
int dns_add_CNAME(struct dns_packet *packet, dns_rr_type type, char *domain, int ttl, char *cname);
|
||||||
|
|
||||||
|
int dns_get_CNAME(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, char *cname, int cname_size);
|
||||||
|
|
||||||
|
int dns_add_A(struct dns_packet *packet, dns_rr_type type, char *domain, int ttl, unsigned char addr[4]);
|
||||||
|
|
||||||
|
int dns_get_A(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, unsigned char addr[4]);
|
||||||
|
|
||||||
|
int dns_add_PTR(struct dns_packet *packet, dns_rr_type type, char *domain, int ttl, char *cname);
|
||||||
|
|
||||||
|
int dns_get_PTR(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, char *cname, int cname_size);
|
||||||
|
|
||||||
|
int dns_add_AAAA(struct dns_packet *packet, dns_rr_type type, char *domain, int ttl, unsigned char addr[16]);
|
||||||
|
|
||||||
|
int dns_get_AAAA(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, unsigned char addr[16]);
|
||||||
|
|
||||||
int dns_decode(struct dns_packet *packet, int maxsize, unsigned char *data, int size);
|
int dns_decode(struct dns_packet *packet, int maxsize, unsigned char *data, int size);
|
||||||
|
|
||||||
|
|||||||
18
dns_client.c
18
dns_client.c
@@ -1,3 +1,21 @@
|
|||||||
|
/*************************************************************************
|
||||||
|
*
|
||||||
|
* Copyright (C) 2018 Ruilin Peng (Nick) <pymumu@gmail.com>.
|
||||||
|
*
|
||||||
|
* smartdns is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* smartdns is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "dns_client.h"
|
#include "dns_client.h"
|
||||||
#include "atomic.h"
|
#include "atomic.h"
|
||||||
#include "fast_ping.h"
|
#include "fast_ping.h"
|
||||||
|
|||||||
75
dns_server.c
75
dns_server.c
@@ -1,3 +1,21 @@
|
|||||||
|
/*************************************************************************
|
||||||
|
*
|
||||||
|
* Copyright (C) 2018 Ruilin Peng (Nick) <pymumu@gmail.com>.
|
||||||
|
*
|
||||||
|
* smartdns is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* smartdns is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "dns_server.h"
|
#include "dns_server.h"
|
||||||
#include "dns.h"
|
#include "dns.h"
|
||||||
#include "hashtable.h"
|
#include "hashtable.h"
|
||||||
@@ -46,7 +64,6 @@ static void tv_sub(struct timeval *out, struct timeval *in)
|
|||||||
|
|
||||||
void _dns_server_period_run()
|
void _dns_server_period_run()
|
||||||
{
|
{
|
||||||
|
|
||||||
unsigned char packet_data[DNS_INPACKET_SIZE];
|
unsigned char packet_data[DNS_INPACKET_SIZE];
|
||||||
unsigned char data[DNS_INPACKET_SIZE];
|
unsigned char data[DNS_INPACKET_SIZE];
|
||||||
|
|
||||||
@@ -56,6 +73,7 @@ void _dns_server_period_run()
|
|||||||
memset(&head, 0, sizeof(head));
|
memset(&head, 0, sizeof(head));
|
||||||
head.rcode = 0;
|
head.rcode = 0;
|
||||||
head.qr = 0;
|
head.qr = 0;
|
||||||
|
head.rd = 1;
|
||||||
head.ra = 0;
|
head.ra = 0;
|
||||||
head.id = 1;
|
head.id = 1;
|
||||||
|
|
||||||
@@ -82,7 +100,12 @@ static int _dns_server_process(struct timeval *now)
|
|||||||
int len;
|
int len;
|
||||||
unsigned char inpacket[DNS_INPACKET_SIZE];
|
unsigned char inpacket[DNS_INPACKET_SIZE];
|
||||||
unsigned char rsppacket[DNS_INPACKET_SIZE];
|
unsigned char rsppacket[DNS_INPACKET_SIZE];
|
||||||
|
unsigned char aswpacket[DNS_INPACKET_SIZE];
|
||||||
|
unsigned char outpacket[DNS_INPACKET_SIZE];
|
||||||
|
|
||||||
struct dns_packet *packet = (struct dns_packet *)rsppacket;
|
struct dns_packet *packet = (struct dns_packet *)rsppacket;
|
||||||
|
struct dns_packet *anspacket = (struct dns_packet *)aswpacket;
|
||||||
|
|
||||||
struct sockaddr_storage from;
|
struct sockaddr_storage from;
|
||||||
socklen_t from_len = sizeof(from);
|
socklen_t from_len = sizeof(from);
|
||||||
int data_len;
|
int data_len;
|
||||||
@@ -105,27 +128,65 @@ static int _dns_server_process(struct timeval *now)
|
|||||||
struct dns_rrs *rrs;
|
struct dns_rrs *rrs;
|
||||||
char name[128];
|
char name[128];
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
int j = 0;
|
||||||
int ttl;
|
int ttl;
|
||||||
int qtype;
|
int qtype;
|
||||||
int qclass;
|
int qclass;
|
||||||
|
|
||||||
printf("qdcount = %d, ancount = %d, nscount = %d, nrcount = %d, len = %d\n",
|
struct dns_head head;
|
||||||
packet->head.qdcount, packet->head.ancount, packet->head.nscount,
|
memset(&head, 0, sizeof(head));
|
||||||
|
head.rcode = 0;
|
||||||
|
head.qr = 1;
|
||||||
|
head.rd = 1;
|
||||||
|
head.ra = 0;
|
||||||
|
head.id = packet->head.id;
|
||||||
|
int n = 0;
|
||||||
|
|
||||||
|
dns_packet_init(anspacket, DNS_INPACKET_SIZE, &head);
|
||||||
|
|
||||||
|
printf("qdcount = %d, ancount = %d, nscount = %d, nrcount = %d, len = %d\n", packet->head.qdcount, packet->head.ancount, packet->head.nscount,
|
||||||
packet->head.nrcount, data_len);
|
packet->head.nrcount, data_len);
|
||||||
|
|
||||||
rrs = dns_get_rrs_start(packet, DNS_RRS_QD, &count);
|
rrs = dns_get_rrs_start(packet, DNS_RRS_QD, &count);
|
||||||
for (i = 0; i < count && rrs; i++, rrs = dns_get_rrs_next(packet, rrs)) {
|
for (i = 0; i < count && rrs; i++, rrs = dns_get_rrs_next(packet, rrs)) {
|
||||||
switch (rrs->type) {
|
|
||||||
case DNS_T_CNAME: {
|
|
||||||
dns_get_domain(rrs, name, 128, &qtype, &qclass);
|
dns_get_domain(rrs, name, 128, &qtype, &qclass);
|
||||||
printf("domain: %s qtype: %d qclass: %d\n", name, qtype, qclass);
|
printf("domain: %s qtype: %d qclass: %d\n", name, qtype, qclass);
|
||||||
|
switch (qtype) {
|
||||||
|
case DNS_T_A: {
|
||||||
|
unsigned char addr[4];
|
||||||
|
addr[0] = 192;
|
||||||
|
addr[1] = 188;
|
||||||
|
addr[2] = 9;
|
||||||
|
addr[3] = 8;
|
||||||
|
dns_add_A(anspacket, DNS_RRS_AN, name, 60 * 60, addr);
|
||||||
|
n++;
|
||||||
|
} break;
|
||||||
|
case DNS_T_AAAA: {
|
||||||
|
unsigned char addr[16];
|
||||||
|
addr[0] = 192;
|
||||||
|
addr[1] = 188;
|
||||||
|
addr[2] = 9;
|
||||||
|
addr[3] = 8;
|
||||||
|
dns_add_AAAA(anspacket, DNS_RRS_AN, name, 60 * 60, addr);
|
||||||
|
n++;
|
||||||
|
} break;
|
||||||
|
case DNS_T_PTR:{
|
||||||
|
dns_add_PTR(anspacket, DNS_RRS_AN, name, 60 * 60, "raspberrypi.larva-family.com");
|
||||||
|
n++;
|
||||||
} break;
|
} break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rrs = dns_get_rrs_start(packet, DNS_RRS_AN, &count);
|
if (n > 0 && packet->head.qr == 0) {
|
||||||
|
dns_add_CNAME(anspacket, DNS_RRS_AN, name, 60 * 60, name);
|
||||||
|
len = dns_encode(outpacket, DNS_INPACKET_SIZE, anspacket);
|
||||||
|
sendto(server.fd, outpacket, len, 0, (struct sockaddr *)&from, from_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (j = 1; j < DNS_RRS_END; j++) {
|
||||||
|
rrs = dns_get_rrs_start(packet, j, &count);
|
||||||
for (i = 0; i < count && rrs; i++, rrs = dns_get_rrs_next(packet, rrs)) {
|
for (i = 0; i < count && rrs; i++, rrs = dns_get_rrs_next(packet, rrs)) {
|
||||||
switch (rrs->type) {
|
switch (rrs->type) {
|
||||||
case DNS_T_A: {
|
case DNS_T_A: {
|
||||||
@@ -133,6 +194,7 @@ static int _dns_server_process(struct timeval *now)
|
|||||||
dns_get_A(rrs, name, 128, &ttl, addr);
|
dns_get_A(rrs, name, 128, &ttl, addr);
|
||||||
printf("%s %d : %d.%d.%d.%d\n", name, ttl, addr[0], addr[1], addr[2], addr[3]);
|
printf("%s %d : %d.%d.%d.%d\n", name, ttl, addr[0], addr[1], addr[2], addr[3]);
|
||||||
} break;
|
} break;
|
||||||
|
case DNS_T_NS:
|
||||||
case DNS_T_CNAME: {
|
case DNS_T_CNAME: {
|
||||||
char cname[128];
|
char cname[128];
|
||||||
dns_get_CNAME(rrs, name, 128, &ttl, cname, 128);
|
dns_get_CNAME(rrs, name, 128, &ttl, cname, 128);
|
||||||
@@ -142,6 +204,7 @@ static int _dns_server_process(struct timeval *now)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
18
fast_ping.c
18
fast_ping.c
@@ -1,3 +1,21 @@
|
|||||||
|
/*************************************************************************
|
||||||
|
*
|
||||||
|
* Copyright (C) 2018 Ruilin Peng (Nick) <pymumu@gmail.com>.
|
||||||
|
*
|
||||||
|
* smartdns is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* smartdns is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "fast_ping.h"
|
#include "fast_ping.h"
|
||||||
#include "atomic.h"
|
#include "atomic.h"
|
||||||
#include "hashtable.h"
|
#include "hashtable.h"
|
||||||
|
|||||||
18
smartdns.c
18
smartdns.c
@@ -1,3 +1,21 @@
|
|||||||
|
/*************************************************************************
|
||||||
|
*
|
||||||
|
* Copyright (C) 2018 Ruilin Peng (Nick) <pymumu@gmail.com>.
|
||||||
|
*
|
||||||
|
* smartdns is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* smartdns is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "fast_ping.h"
|
#include "fast_ping.h"
|
||||||
#include "dns_client.h"
|
#include "dns_client.h"
|
||||||
#include "dns_server.h"
|
#include "dns_server.h"
|
||||||
|
|||||||
Reference in New Issue
Block a user