mirror of
https://github.com/yarrick/iodine.git
synced 2024-11-26 10: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
34
src/dns.c
34
src/dns.c
|
@ -249,6 +249,11 @@ dns_decode(char *buf, size_t buflen, struct query *q, qr_t qr, char *packet, siz
|
||||||
rv = 0;
|
rv = 0;
|
||||||
header = (HEADER*)packet;
|
header = (HEADER*)packet;
|
||||||
|
|
||||||
|
// Reject short packets
|
||||||
|
if (packetlen < sizeof(HEADER)) {
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
if (header->qr != qr) {
|
if (header->qr != qr) {
|
||||||
warnx("header->qr does not match the requested qr");
|
warnx("header->qr does not match the requested qr");
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -442,32 +447,3 @@ decodepacket(const char *name, char *buf, int buflen)
|
||||||
return len;
|
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_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 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);
|
void dnsd_send(int, struct query*, char *, int);
|
||||||
|
|
||||||
int dnsd_haspacket();
|
int dnsd_haspacket();
|
||||||
|
|
|
@ -56,6 +56,8 @@ char password[33];
|
||||||
int my_mtu;
|
int my_mtu;
|
||||||
in_addr_t my_ip;
|
in_addr_t my_ip;
|
||||||
|
|
||||||
|
static int read_dns(int, struct query *, char *, int);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sigint(int sig) {
|
sigint(int sig) {
|
||||||
running = 0;
|
running = 0;
|
||||||
|
@ -120,7 +122,7 @@ tunnel(int tun_fd, int dns_fd)
|
||||||
outpacket.len = outlen;
|
outpacket.len = outlen;
|
||||||
}
|
}
|
||||||
if(FD_ISSET(dns_fd, &fds)) {
|
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)
|
if (read <= 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -217,6 +219,33 @@ tunnel(int tun_fd, int dns_fd)
|
||||||
return 0;
|
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
|
static void
|
||||||
usage() {
|
usage() {
|
||||||
extern char *__progname;
|
extern char *__progname;
|
||||||
|
|
Loading…
Reference in New Issue