Update code

This commit is contained in:
Nick Peng
2018-05-16 00:56:43 +08:00
parent 66f804a109
commit 924745da04
6 changed files with 1110 additions and 818 deletions

1702
dns.c

File diff suppressed because it is too large Load Diff

61
dns.h
View File

@@ -6,23 +6,17 @@
#include <netdb.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_AAAA_LEN 16
#define DNS_MAX_CNAME_LEN 128
#define DNS_RRS_QD 0
#define DNS_RRS_AN 1
#define DNS_RRS_NS 2
#define DNS_RRS_NR 3
#define DNS_RR_END (0XFFFF)
typedef enum dns_rr_type {
DNS_RRS_QD = 0,
DNS_RRS_AN = 1,
DNS_RRS_NS = 2,
DNS_RRS_NR = 3,
DNS_RRS_END = 4,
} dns_rr_type;
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];
};
struct dns_data_context
{
struct dns_data_context {
unsigned char *data;
unsigned char *ptr;
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_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);
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, 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]);
/*
* Question
*/
int dns_add_domain(struct dns_packet *packet, char *domain, 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);

View File

@@ -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 "atomic.h"
#include "fast_ping.h"

View File

@@ -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.h"
#include "hashtable.h"
@@ -46,7 +64,6 @@ static void tv_sub(struct timeval *out, struct timeval *in)
void _dns_server_period_run()
{
unsigned char packet_data[DNS_INPACKET_SIZE];
unsigned char data[DNS_INPACKET_SIZE];
@@ -56,6 +73,7 @@ void _dns_server_period_run()
memset(&head, 0, sizeof(head));
head.rcode = 0;
head.qr = 0;
head.rd = 1;
head.ra = 0;
head.id = 1;
@@ -82,7 +100,12 @@ static int _dns_server_process(struct timeval *now)
int len;
unsigned char inpacket[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 *anspacket = (struct dns_packet *)aswpacket;
struct sockaddr_storage from;
socklen_t from_len = sizeof(from);
int data_len;
@@ -105,41 +128,81 @@ static int _dns_server_process(struct timeval *now)
struct dns_rrs *rrs;
char name[128];
int i = 0;
int j = 0;
int ttl;
int qtype;
int qclass;
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);
struct dns_head head;
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);
rrs = dns_get_rrs_start(packet, DNS_RRS_QD, &count);
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);
printf("domain: %s qtype: %d qclass: %d\n", name, qtype, qclass);
dns_get_domain(rrs, name, 128, &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;
default:
break;
}
}
rrs = dns_get_rrs_start(packet, DNS_RRS_AN, &count);
for (i = 0; i < count && rrs; i++, rrs = dns_get_rrs_next(packet, rrs)) {
switch (rrs->type) {
case DNS_T_A: {
unsigned char addr[4];
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]);
} break;
case DNS_T_CNAME: {
char cname[128];
dns_get_CNAME(rrs, name, 128, &ttl, cname, 128);
printf("%s %d : %s\n", name, ttl, cname);
} break;
default:
break;
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)) {
switch (rrs->type) {
case DNS_T_A: {
unsigned char addr[4];
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]);
} break;
case DNS_T_NS:
case DNS_T_CNAME: {
char cname[128];
dns_get_CNAME(rrs, name, 128, &ttl, cname, 128);
printf("%s %d : %s\n", name, ttl, cname);
} break;
default:
break;
}
}
}

View File

@@ -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 "atomic.h"
#include "hashtable.h"

View File

@@ -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 "dns_client.h"
#include "dns_server.h"