mirror of
https://github.com/yarrick/iodine.git
synced 2024-11-22 08:09:19 +02:00
#3 move dnsd_read from dns.c to iodined.c, now named read_dns
This commit is contained in:
parent
064d060f79
commit
c5317fe388
36
src/dns.c
36
src/dns.c
|
@ -248,12 +248,17 @@ dns_decode(char *buf, size_t buflen, struct query *q, qr_t qr, char *packet, siz
|
|||
|
||||
rv = 0;
|
||||
header = (HEADER*)packet;
|
||||
|
||||
// Reject short packets
|
||||
if (packetlen < sizeof(HEADER)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
if (header->qr != qr) {
|
||||
warnx("header->qr does not match the requested qr");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
data = packet + sizeof(HEADER);
|
||||
qdcount = ntohs(header->qdcount);
|
||||
ancount = ntohs(header->ancount);
|
||||
|
@ -442,32 +447,3 @@ decodepacket(const char *name, char *buf, int buflen)
|
|||
return len;
|
||||
}
|
||||
|
||||
int
|
||||
dnsd_read(int fd, struct query *q, char *buf, int buflen)
|
||||
{
|
||||
struct sockaddr_in from;
|
||||
char packet[64*1024];
|
||||
socklen_t addrlen;
|
||||
int len;
|
||||
int rv;
|
||||
int r;
|
||||
|
||||
addrlen = sizeof(struct sockaddr);
|
||||
r = recvfrom(fd, packet, sizeof(packet), 0, (struct sockaddr*)&from, &addrlen);
|
||||
|
||||
if (r >= sizeof(HEADER)) {
|
||||
len = dns_decode(buf, buflen, q, QR_QUERY, packet, r);
|
||||
|
||||
q->fromlen = addrlen;
|
||||
memcpy((struct sockaddr*)&q->from, (struct sockaddr*)&from, addrlen);
|
||||
rv = len;
|
||||
} else if (r < 0) { // Error
|
||||
perror("recvfrom");
|
||||
rv = 0;
|
||||
} else { // Packet too small to be dns protocol
|
||||
rv = 0;
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
|
|
@ -37,7 +37,6 @@ int dns_encode_hostname(const char *, char *, int);
|
|||
int dns_encode(char *, size_t, struct query *, qr_t, char *, size_t);
|
||||
int dns_decode(char *, size_t, struct query *, qr_t, char *, size_t);
|
||||
|
||||
int dnsd_read(int, struct query*, char *, int);
|
||||
void dnsd_send(int, struct query*, char *, int);
|
||||
|
||||
int dnsd_haspacket();
|
||||
|
|
|
@ -56,6 +56,8 @@ char password[33];
|
|||
int my_mtu;
|
||||
in_addr_t my_ip;
|
||||
|
||||
static int read_dns(int, struct query *, char *, int);
|
||||
|
||||
static void
|
||||
sigint(int sig) {
|
||||
running = 0;
|
||||
|
@ -120,7 +122,7 @@ tunnel(int tun_fd, int dns_fd)
|
|||
outpacket.len = outlen;
|
||||
}
|
||||
if(FD_ISSET(dns_fd, &fds)) {
|
||||
read = dnsd_read(dns_fd, &q, in, sizeof(in));
|
||||
read = read_dns(dns_fd, &q, in, sizeof(in));
|
||||
if (read <= 0)
|
||||
continue;
|
||||
|
||||
|
@ -217,6 +219,33 @@ tunnel(int tun_fd, int dns_fd)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
read_dns(int fd, struct query *q, char *buf, int buflen)
|
||||
{
|
||||
struct sockaddr_in from;
|
||||
char packet[64*1024];
|
||||
socklen_t addrlen;
|
||||
int len;
|
||||
int rv;
|
||||
int r;
|
||||
|
||||
addrlen = sizeof(struct sockaddr);
|
||||
r = recvfrom(fd, packet, sizeof(packet), 0, (struct sockaddr*)&from, &addrlen);
|
||||
|
||||
if (r > 0) {
|
||||
len = dns_decode(buf, buflen, q, QR_QUERY, packet, r);
|
||||
|
||||
q->fromlen = addrlen;
|
||||
memcpy((struct sockaddr*)&q->from, (struct sockaddr*)&from, addrlen);
|
||||
rv = len;
|
||||
} else if (r < 0) { // Error
|
||||
perror("recvfrom");
|
||||
rv = 0;
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
static void
|
||||
usage() {
|
||||
extern char *__progname;
|
||||
|
|
Loading…
Reference in New Issue