Minor readability adjustments + more debug output

This commit is contained in:
frekky 2015-08-23 22:15:51 +08:00
parent 8e08c007c5
commit 4c8ce94be7

View File

@ -652,6 +652,9 @@ tunnel_dns(int tun_fd, int dns_fd, struct dnsfd *dns_fds, int bind_fd)
} }
} else { } else {
/* Forward query to other port ? */ /* Forward query to other port ? */
if (debug >= 3) {
fprintf(stderr, "Requested domain outside our topdomain.");
}
if (bind_fd) { if (bind_fd) {
forward_query(bind_fd, &q); forward_query(bind_fd, &q);
} }
@ -1158,6 +1161,7 @@ write_dns(int fd, struct query *q, char *data, int datalen, char downenc)
void void
handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query *q, int domain_len) handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query *q, int domain_len)
/* Handles a NULL DNS request. See doc/proto_XXXXXXXX.txt for details on iodine protocol. */
{ {
struct in_addr tempip; struct in_addr tempip;
char in[512]; char in[512];
@ -1176,13 +1180,18 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
memcpy(in, q->name, MIN(domain_len, sizeof(in))); memcpy(in, q->name, MIN(domain_len, sizeof(in)));
if(in[0] == 'V' || in[0] == 'v') { if (debug >= 3) {
fprintf(stderr, "NULL request length %d/%lu, command '%c'\n", domain_len, sizeof(in), in[0]);
}
if(in[0] == 'V' || in[0] == 'v') { /* Version request */
int version = 0; int version = 0;
read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), domain_len - 1, b32); read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), domain_len - 1, b32);
/* Version greeting, compare and send ack/nak */ /* Version greeting, compare and send ack/nak */
if (read > 4) { if (read > 4) {
/* Received V + 32bits version */ /* Received V + 32bits version */
// TODO htonl/ntohl for portability
version = (((unpacked[0] & 0xff) << 24) | version = (((unpacked[0] & 0xff) << 24) |
((unpacked[1] & 0xff) << 16) | ((unpacked[1] & 0xff) << 16) |
((unpacked[2] & 0xff) << 8) | ((unpacked[2] & 0xff) << 8) |
@ -1244,7 +1253,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
format_addr(&q->from, q->fromlen), version); format_addr(&q->from, q->fromlen), version);
} }
return; return;
} else if (in[0] == 'L' || in[0] == 'l') { } else if (in[0] == 'L' || in[0] == 'l') { /* Login request */
read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), domain_len - 1, b32); read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), domain_len - 1, b32);
if (read < 17) { if (read < 17) {
write_dns(dns_fd, q, "BADLEN", 6, 'T'); write_dns(dns_fd, q, "BADLEN", 6, 'T');
@ -1289,8 +1298,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
} }
} }
return; return;
} else if(in[0] == 'I' || in[0] == 'i') { } else if(in[0] == 'I' || in[0] == 'i') { /* IP address request */
/* Request for IP number */
char reply[17]; char reply[17];
int length; int length;
@ -1325,7 +1333,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
/* No userid here, reply with lowest-grade downenc */ /* No userid here, reply with lowest-grade downenc */
write_dns(dns_fd, q, in, domain_len, 'T'); write_dns(dns_fd, q, in, domain_len, 'T');
return; return;
} else if(in[0] == 'S' || in[0] == 's') { } else if(in[0] == 'S' || in[0] == 's') { /* Switch upstream codec */
int codec; int codec;
struct encoder *enc; struct encoder *enc;
if (domain_len < 3) { /* len at least 3, example: "S15" */ if (domain_len < 3) { /* len at least 3, example: "S15" */
@ -1368,7 +1376,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
break; break;
} }
return; return;
} else if(in[0] == 'O' || in[0] == 'o') { } else if(in[0] == 'O' || in[0] == 'o') { /* Protocol options */
if (domain_len < 3) { /* len at least 3, example: "O1T" */ if (domain_len < 3) { /* len at least 3, example: "O1T" */
write_dns(dns_fd, q, "BADLEN", 6, 'T'); write_dns(dns_fd, q, "BADLEN", 6, 'T');
return; return;
@ -1422,7 +1430,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
break; break;
} }
return; return;
} else if(in[0] == 'Y' || in[0] == 'y') { } else if(in[0] == 'Y' || in[0] == 'y') { /* Downstream codec check */
int i; int i;
char *datap; char *datap;
int datalen; int datalen;
@ -1494,7 +1502,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
write_dns(dns_fd, q, "BADCODEC", 8, 'T'); write_dns(dns_fd, q, "BADCODEC", 8, 'T');
return; return;
} else if(in[0] == 'R' || in[0] == 'r') { } else if(in[0] == 'R' || in[0] == 'r') { /* Downstream fragsize probe */
int req_frag_size; int req_frag_size;
if (domain_len < 16) { /* we'd better have some chars for data... */ if (domain_len < 16) { /* we'd better have some chars for data... */
@ -1510,6 +1518,10 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
} }
req_frag_size = ((b32_8to5(in[1]) & 1) << 10) | ((b32_8to5(in[2]) & 31) << 5) | (b32_8to5(in[3]) & 31); req_frag_size = ((b32_8to5(in[1]) & 1) << 10) | ((b32_8to5(in[2]) & 31) << 5) | (b32_8to5(in[3]) & 31);
if (debug >= 3) {
fprintf(stderr, "Got downstream fragsize probe from user %d, required fragsize %d\n", userid, req_frag_size);
}
if (req_frag_size < 2 || req_frag_size > 2047) { if (req_frag_size < 2 || req_frag_size > 2047) {
write_dns(dns_fd, q, "BADFRAG", 7, users[userid].downenc); write_dns(dns_fd, q, "BADFRAG", 7, users[userid].downenc);
} else { } else {
@ -1527,7 +1539,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
write_dns(dns_fd, q, buf, req_frag_size, users[userid].downenc); write_dns(dns_fd, q, buf, req_frag_size, users[userid].downenc);
} }
return; return;
} else if(in[0] == 'N' || in[0] == 'n') { } else if(in[0] == 'N' || in[0] == 'n') { /* Downstream fragsize (NS.topdomain A-type reply) */
int max_frag_size; int max_frag_size;
read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), domain_len - 1, b32); read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), domain_len - 1, b32);
@ -1552,7 +1564,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
write_dns(dns_fd, q, &unpacked[1], 2, users[userid].downenc); write_dns(dns_fd, q, &unpacked[1], 2, users[userid].downenc);
} }
return; return;
} else if(in[0] == 'P' || in[0] == 'p') { } else if(in[0] == 'P' || in[0] == 'p') { /* Ping request */
int dn_seq, up_seq, dn_wins, up_wins; int dn_seq, up_seq, dn_wins, up_wins;
int didsend = 0; int didsend = 0;
int respond; int respond;
@ -1566,7 +1578,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
if (q->id == 0) if (q->id == 0)
return; return;
read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), domain_len - 1, b32); read = unpack_data(unpacked, sizeof(unpacked), in + 1, domain_len - 1, b32);
if (read < 7) if (read < 7)
return; return;
@ -1638,7 +1650,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
} }
/* If there is a query that must be returned real soon, do it. /* If there is a query that must be returned real soon, do it.
May contain new downstream data if the ping had a new ack. May contain new downstream data if the ping had a new ack. TODO: ping with downstream data
Otherwise, may also be re-sending old data. */ Otherwise, may also be re-sending old data. */
if (users[userid].q_sendrealsoon.id != 0) { if (users[userid].q_sendrealsoon.id != 0) {
if (respond) send_ping_response(dns_fd, userid, &users[userid].q_sendrealsoon); if (respond) send_ping_response(dns_fd, userid, &users[userid].q_sendrealsoon);
@ -1670,7 +1682,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
if ((!didsend) || !users[userid].lazy) if ((!didsend) || !users[userid].lazy)
send_frag_or_dataless(dns_fd, userid, &users[userid].q); send_frag_or_dataless(dns_fd, userid, &users[userid].q);
} else if((in[0] >= '0' && in[0] <= '9') /* Data packet */ } else if((in[0] >= '0' && in[0] <= '9') /* Upstream data packet */
|| (in[0] >= 'a' && in[0] <= 'f') || (in[0] >= 'a' && in[0] <= 'f')
|| (in[0] >= 'A' && in[0] <= 'F')) { || (in[0] >= 'A' && in[0] <= 'F')) {
int didsend = 0; int didsend = 0;