2009-08-16 12:30:26 +00:00
|
|
|
/*
|
2014-06-01 06:46:42 +00:00
|
|
|
* Copyright (c) 2006-2014 Erik Ekman <yarrick@kryo.se>,
|
2015-08-21 08:57:54 +00:00
|
|
|
* 2006-2009 Bjorn Andersson <flex@kryo.se>,
|
|
|
|
* 2015 Frekk van Blagh <frekk@frekkworks.com>
|
2009-08-16 12:30:26 +00:00
|
|
|
*
|
2014-08-07 19:14:10 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
2009-08-16 12:30:26 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2009-09-20 15:11:14 +00:00
|
|
|
#include <ctype.h>
|
2009-08-16 12:30:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-05-29 16:37:40 +00:00
|
|
|
#include <strings.h>
|
2009-08-16 12:30:26 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <zlib.h>
|
2015-09-28 05:07:00 +00:00
|
|
|
#include <sys/time.h>
|
2009-08-16 12:30:26 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#ifdef WINDOWS32
|
|
|
|
#include "windows.h"
|
|
|
|
#else
|
2015-08-05 17:33:50 +00:00
|
|
|
#include <arpa/nameser.h>
|
2011-04-23 18:55:59 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include "android_dns.h"
|
|
|
|
#endif
|
2009-08-16 12:30:26 +00:00
|
|
|
#ifdef DARWIN
|
2009-09-19 08:32:57 +00:00
|
|
|
#define BIND_8_COMPAT
|
|
|
|
#include <arpa/nameser_compat.h>
|
2009-08-16 12:30:26 +00:00
|
|
|
#endif
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "encoding.h"
|
|
|
|
#include "base32.h"
|
|
|
|
#include "base64.h"
|
2009-12-29 20:00:57 +00:00
|
|
|
#include "base64u.h"
|
|
|
|
#include "base128.h"
|
2009-08-16 12:30:26 +00:00
|
|
|
#include "dns.h"
|
|
|
|
#include "login.h"
|
|
|
|
#include "tun.h"
|
|
|
|
#include "version.h"
|
2015-08-21 03:08:47 +00:00
|
|
|
#include "window.h"
|
2015-10-03 14:09:34 +00:00
|
|
|
#include "util.h"
|
2009-08-16 12:30:26 +00:00
|
|
|
#include "client.h"
|
|
|
|
|
2014-06-09 16:55:27 +00:00
|
|
|
int
|
2014-06-09 16:11:16 +00:00
|
|
|
client_set_qtype(char *qtype)
|
2009-09-20 15:11:14 +00:00
|
|
|
{
|
|
|
|
if (!strcasecmp(qtype, "NULL"))
|
2016-01-09 07:04:39 +00:00
|
|
|
this.do_qtype = T_NULL;
|
2014-06-09 18:05:29 +00:00
|
|
|
else if (!strcasecmp(qtype, "PRIVATE"))
|
2016-01-09 07:04:39 +00:00
|
|
|
this.do_qtype = T_PRIVATE;
|
2009-09-20 15:11:14 +00:00
|
|
|
else if (!strcasecmp(qtype, "CNAME"))
|
2016-01-09 07:04:39 +00:00
|
|
|
this.do_qtype = T_CNAME;
|
2009-09-20 15:11:14 +00:00
|
|
|
else if (!strcasecmp(qtype, "A"))
|
2016-01-09 07:04:39 +00:00
|
|
|
this.do_qtype = T_A;
|
2009-09-20 15:11:14 +00:00
|
|
|
else if (!strcasecmp(qtype, "MX"))
|
2016-01-09 07:04:39 +00:00
|
|
|
this.do_qtype = T_MX;
|
2009-12-29 20:00:57 +00:00
|
|
|
else if (!strcasecmp(qtype, "SRV"))
|
2016-01-09 07:04:39 +00:00
|
|
|
this.do_qtype = T_SRV;
|
2009-09-20 15:11:14 +00:00
|
|
|
else if (!strcasecmp(qtype, "TXT"))
|
2016-01-09 07:04:39 +00:00
|
|
|
this.do_qtype = T_TXT;
|
|
|
|
return (this.do_qtype == T_UNSET);
|
2009-09-20 15:11:14 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
char *
|
2014-06-09 16:11:16 +00:00
|
|
|
client_get_qtype()
|
2009-12-29 20:00:57 +00:00
|
|
|
{
|
|
|
|
char *c = "UNDEFINED";
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.do_qtype == T_NULL) c = "NULL";
|
|
|
|
else if (this.do_qtype == T_PRIVATE) c = "PRIVATE";
|
|
|
|
else if (this.do_qtype == T_CNAME) c = "CNAME";
|
|
|
|
else if (this.do_qtype == T_A) c = "A";
|
|
|
|
else if (this.do_qtype == T_MX) c = "MX";
|
|
|
|
else if (this.do_qtype == T_SRV) c = "SRV";
|
|
|
|
else if (this.do_qtype == T_TXT) c = "TXT";
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
char
|
|
|
|
parse_encoding(char *encoding)
|
2009-09-20 15:11:14 +00:00
|
|
|
{
|
2016-01-09 07:04:39 +00:00
|
|
|
char enc_char = 0;
|
2009-09-20 15:11:14 +00:00
|
|
|
if (!strcasecmp(encoding, "base32"))
|
2016-01-09 07:04:39 +00:00
|
|
|
enc_char = 'T';
|
2009-09-20 15:11:14 +00:00
|
|
|
else if (!strcasecmp(encoding, "base64"))
|
2016-01-09 07:04:39 +00:00
|
|
|
enc_char = 'S';
|
2009-12-29 20:00:57 +00:00
|
|
|
else if (!strcasecmp(encoding, "base64u"))
|
2016-01-09 07:04:39 +00:00
|
|
|
enc_char = 'U';
|
2009-12-29 20:00:57 +00:00
|
|
|
else if (!strcasecmp(encoding, "base128"))
|
2016-01-09 07:04:39 +00:00
|
|
|
enc_char = 'V';
|
2009-09-20 15:11:14 +00:00
|
|
|
else if (!strcasecmp(encoding, "raw"))
|
2016-01-09 07:04:39 +00:00
|
|
|
enc_char = 'R';
|
|
|
|
return enc_char;
|
2015-09-28 05:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
void
|
2015-08-23 14:14:27 +00:00
|
|
|
client_set_hostname_maxlen(size_t i)
|
2009-12-29 20:00:57 +00:00
|
|
|
{
|
2016-01-09 07:04:39 +00:00
|
|
|
if (i <= 0xFF && i != this.hostname_maxlen) {
|
|
|
|
this.hostname_maxlen = i;
|
|
|
|
this.maxfragsize_up = get_raw_length_from_dns(this.hostname_maxlen - UPSTREAM_HDR, this.dataenc, this.topdomain);
|
|
|
|
if (this.outbuf)
|
|
|
|
this.outbuf->maxfraglen = this.maxfragsize_up;
|
2015-08-23 14:14:27 +00:00
|
|
|
}
|
2009-12-29 20:00:57 +00:00
|
|
|
}
|
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
const char *
|
|
|
|
client_get_raw_addr()
|
|
|
|
{
|
2016-01-09 07:04:39 +00:00
|
|
|
return format_addr(&this.raw_serv, this.raw_serv_len);
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 14:07:48 +00:00
|
|
|
void
|
|
|
|
client_rotate_nameserver()
|
|
|
|
{
|
2016-01-09 07:04:39 +00:00
|
|
|
this.current_nameserver ++;
|
|
|
|
if (this.current_nameserver >= this.nameserv_addrs_len)
|
|
|
|
this.current_nameserver = 0;
|
2015-08-14 14:07:48 +00:00
|
|
|
}
|
|
|
|
|
2015-10-17 14:28:10 +00:00
|
|
|
void
|
|
|
|
immediate_mode_defaults()
|
|
|
|
{
|
2016-01-09 07:04:39 +00:00
|
|
|
this.send_interval_ms = MIN(this.rtt_total_ms / this.num_immediate, 1000);
|
|
|
|
this.max_timeout_ms = MAX(4 * this.rtt_total_ms / this.num_immediate, 5000);
|
|
|
|
this.server_timeout_ms = 0;
|
2015-10-17 14:28:10 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 05:07:00 +00:00
|
|
|
/* Client-side query tracking for lazy mode */
|
2015-10-03 14:09:34 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
/* Handy macro for printing this.stats with messages */
|
2015-11-10 12:54:01 +00:00
|
|
|
#ifdef DEBUG_BUILD
|
2015-10-03 14:09:34 +00:00
|
|
|
#define QTRACK_DEBUG(l, ...) \
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.debug >= l) {\
|
|
|
|
TIMEPRINT("[QTRACK (%lu/%lu), ? %lu, TO %lu, S %lu/%lu] ", this.num_pending, PENDING_QUERIES_LENGTH, \
|
|
|
|
this.num_untracked, this.num_timeouts, window_sending(this.outbuf, NULL), this.outbuf->numitems); \
|
2015-10-03 14:09:34 +00:00
|
|
|
fprintf(stderr, __VA_ARGS__);\
|
|
|
|
fprintf(stderr, "\n");\
|
|
|
|
}
|
2015-11-10 12:54:01 +00:00
|
|
|
#else
|
|
|
|
#define QTRACK_DEBUG(...)
|
|
|
|
#endif
|
2015-10-03 14:09:34 +00:00
|
|
|
|
2015-10-17 14:28:10 +00:00
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
update_server_timeout(int handshake)
|
2015-10-17 14:28:10 +00:00
|
|
|
/* Calculate server timeout based on average RTT, send ping "handshake" to set
|
|
|
|
* if handshake sent, return query ID */
|
2015-10-04 14:13:47 +00:00
|
|
|
{
|
|
|
|
time_t rtt_ms;
|
|
|
|
static size_t num_rtt_timeouts = 0;
|
|
|
|
|
|
|
|
/* Get average RTT in ms */
|
2016-01-09 14:13:13 +00:00
|
|
|
rtt_ms = (this.num_immediate == 0) ? 1 : this.rtt_total_ms / this.num_immediate;
|
2016-01-09 07:04:39 +00:00
|
|
|
if (rtt_ms >= this.max_timeout_ms && this.num_immediate > 5) {
|
2015-10-04 14:13:47 +00:00
|
|
|
num_rtt_timeouts++;
|
|
|
|
if (num_rtt_timeouts < 3) {
|
|
|
|
fprintf(stderr, "Target interval of %ld ms less than average round-trip of "
|
2016-01-09 07:04:39 +00:00
|
|
|
"%ld ms! Try increasing interval with -I.\n", this.max_timeout_ms, rtt_ms);
|
2015-10-04 14:13:47 +00:00
|
|
|
} else {
|
2015-10-17 14:28:10 +00:00
|
|
|
/* bump up target timeout */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.max_timeout_ms = rtt_ms + 1000;
|
|
|
|
this.server_timeout_ms = 1000;
|
|
|
|
if (this.lazymode)
|
2015-10-17 14:28:10 +00:00
|
|
|
fprintf(stderr, "Adjusting server timeout to %ld ms, target interval %ld ms. Try -I%.1f next time with this network.\n",
|
2016-01-09 07:04:39 +00:00
|
|
|
this.server_timeout_ms, this.max_timeout_ms, this.max_timeout_ms / 1000.0);
|
2015-10-04 14:13:47 +00:00
|
|
|
|
|
|
|
num_rtt_timeouts = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Set server timeout based on target interval and RTT */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.server_timeout_ms = this.max_timeout_ms - rtt_ms;
|
|
|
|
if (this.server_timeout_ms <= 0) {
|
|
|
|
this.server_timeout_ms = 0;
|
2015-10-27 09:25:56 +00:00
|
|
|
fprintf(stderr, "Setting server timeout to 0 ms: if this continues try disabling lazy mode. (-L0)\n");
|
2015-10-17 14:28:10 +00:00
|
|
|
}
|
2015-10-04 14:13:47 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 09:25:56 +00:00
|
|
|
/* update up/down window timeouts to something reasonable */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.downstream_timeout_ms = rtt_ms * 2;
|
|
|
|
this.outbuf->timeout = ms_to_timeval(this.downstream_timeout_ms);
|
2015-10-04 14:13:47 +00:00
|
|
|
|
2015-10-17 14:28:10 +00:00
|
|
|
if (handshake) {
|
|
|
|
/* Send ping handshake to set server timeout */
|
2016-01-09 11:29:17 +00:00
|
|
|
return send_ping(1, -1, 1);
|
2015-10-17 14:28:10 +00:00
|
|
|
}
|
|
|
|
return -1;
|
2015-10-04 14:13:47 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 14:09:34 +00:00
|
|
|
static void
|
|
|
|
check_pending_queries()
|
|
|
|
/* Updates pending queries list */
|
2015-09-28 05:07:00 +00:00
|
|
|
{
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_pending = 0;
|
2015-09-28 05:07:00 +00:00
|
|
|
struct timeval now, qtimeout, max_timeout;
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
/* Max timeout for queries is max interval + 1 second extra */
|
2016-01-09 07:04:39 +00:00
|
|
|
max_timeout = ms_to_timeval(this.max_timeout_ms + 1000);
|
2015-09-28 05:07:00 +00:00
|
|
|
for (int i = 0; i < PENDING_QUERIES_LENGTH; i++) {
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.pending_queries[i].time.tv_sec > 0 && this.pending_queries[i].id >= 0) {
|
|
|
|
timeradd(&this.pending_queries[i].time, &max_timeout, &qtimeout);
|
2015-10-03 14:09:34 +00:00
|
|
|
if (!timercmp(&qtimeout, &now, >)) {
|
2015-10-17 14:28:10 +00:00
|
|
|
/* Query has timed out, clear timestamp but leave ID */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.pending_queries[i].time.tv_sec = 0;
|
|
|
|
this.num_timeouts++;
|
2015-09-28 05:07:00 +00:00
|
|
|
}
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_pending++;
|
2015-09-28 05:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
query_sent_now(int id)
|
|
|
|
{
|
2015-10-03 14:09:34 +00:00
|
|
|
int i = 0, found = 0;
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.pending_queries)
|
2015-09-28 05:07:00 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (id < 0 || id > 65535)
|
|
|
|
return;
|
|
|
|
|
2015-10-03 14:09:34 +00:00
|
|
|
/* Replace any empty queries first, then timed out ones if necessary */
|
|
|
|
for (i = 0; i < PENDING_QUERIES_LENGTH; i++) {
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.pending_queries[i].id < 0) {
|
2015-10-03 14:09:34 +00:00
|
|
|
found = 1;
|
2015-09-28 05:07:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-10-03 14:09:34 +00:00
|
|
|
if (!found) {
|
|
|
|
for (i = 0; i < PENDING_QUERIES_LENGTH; i++) {
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.pending_queries[i].time.tv_sec == 0) {
|
2015-10-03 14:09:34 +00:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* if no slots found after both checks */
|
|
|
|
if (!found) {
|
|
|
|
QTRACK_DEBUG(1, "Buffer full! Failed to add id %d.", id);
|
|
|
|
} else {
|
|
|
|
/* Add query into found location */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.pending_queries[i].id = id;
|
|
|
|
gettimeofday(&this.pending_queries[i].time, NULL);
|
|
|
|
this.num_pending ++;
|
|
|
|
QTRACK_DEBUG(4, "Adding query id %d into this.pending_queries[%d]", id, i);
|
2015-10-03 14:09:34 +00:00
|
|
|
id = -1;
|
|
|
|
}
|
2015-09-28 05:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
static void
|
2015-10-17 14:28:10 +00:00
|
|
|
got_response(int id, int immediate, int fail)
|
2015-09-28 05:07:00 +00:00
|
|
|
{
|
|
|
|
struct timeval now, rtt;
|
2015-10-17 14:28:10 +00:00
|
|
|
time_t rtt_ms;
|
2015-09-28 05:07:00 +00:00
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
|
2015-10-17 14:28:10 +00:00
|
|
|
QTRACK_DEBUG(4, "Got answer id %d (%s)%s", id, immediate ? "immediate" : "lazy",
|
|
|
|
fail ? ", FAIL" : "");
|
2015-09-28 05:07:00 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < PENDING_QUERIES_LENGTH; i++) {
|
2016-01-09 07:04:39 +00:00
|
|
|
if (id >= 0 && this.pending_queries[i].id == id) {
|
|
|
|
if (this.num_pending > 0)
|
|
|
|
this.num_pending--;
|
2015-10-17 14:28:10 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.pending_queries[i].time.tv_sec == 0) {
|
|
|
|
if (this.num_timeouts > 0) {
|
2015-10-17 14:28:10 +00:00
|
|
|
/* If query has timed out but is still stored - just in case
|
|
|
|
* ID is kept on timeout in check_pending_queries() */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_timeouts --;
|
2015-10-17 14:28:10 +00:00
|
|
|
immediate = 0;
|
|
|
|
} else {
|
|
|
|
/* query is empty */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (immediate || this.debug >= 4) {
|
|
|
|
timersub(&now, &this.pending_queries[i].time, &rtt);
|
2015-10-17 14:28:10 +00:00
|
|
|
rtt_ms = timeval_to_ms(&rtt);
|
2015-09-28 05:07:00 +00:00
|
|
|
}
|
2015-10-17 14:28:10 +00:00
|
|
|
|
|
|
|
QTRACK_DEBUG(5, " found answer id %d in pending queries[%d], %ld ms old", id, i, rtt_ms);
|
|
|
|
|
2015-09-28 05:07:00 +00:00
|
|
|
if (immediate) {
|
|
|
|
/* If this was an immediate response we can use it to get
|
|
|
|
more detailed connection statistics like RTT.
|
|
|
|
This lets us determine and adjust server lazy response time
|
|
|
|
during the session much more accurately. */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.rtt_total_ms += rtt_ms;
|
|
|
|
this.num_immediate++;
|
2015-09-28 05:07:00 +00:00
|
|
|
|
2016-01-09 14:13:13 +00:00
|
|
|
if (this.autodetect_server_timeout && this.lazymode)
|
2016-01-09 11:29:17 +00:00
|
|
|
update_server_timeout(0);
|
2015-09-28 05:07:00 +00:00
|
|
|
}
|
2015-10-03 14:09:34 +00:00
|
|
|
|
|
|
|
/* Remove query info from buffer to mark it as answered */
|
2015-10-17 14:28:10 +00:00
|
|
|
id = -1;
|
2016-01-09 07:04:39 +00:00
|
|
|
this.pending_queries[i].id = -1;
|
|
|
|
this.pending_queries[i].time.tv_sec = 0;
|
2015-09-28 05:07:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-10-03 14:09:34 +00:00
|
|
|
if (id > 0) {
|
|
|
|
QTRACK_DEBUG(4, " got untracked response to id %d.", id);
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_untracked++;
|
2015-10-03 14:09:34 +00:00
|
|
|
}
|
2015-09-28 05:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
send_query(uint8_t *hostname)
|
2015-09-28 05:07:00 +00:00
|
|
|
/* Returns DNS ID of sent query */
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2015-08-29 12:11:21 +00:00
|
|
|
uint8_t packet[4096];
|
2009-08-16 12:30:26 +00:00
|
|
|
struct query q;
|
|
|
|
size_t len;
|
|
|
|
|
2015-10-04 14:39:40 +00:00
|
|
|
DEBUG(3, "TX: pkt len %lu: hostname '%s'", strlen((char *)hostname), hostname);
|
2015-08-29 12:11:21 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
this.chunkid += 7727;
|
|
|
|
if (this.chunkid == 0)
|
2009-09-20 15:11:14 +00:00
|
|
|
/* 0 is used as "no-query" in iodined.c */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.chunkid = rand() & 0xFF;
|
2009-09-20 15:11:14 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
q.id = this.chunkid;
|
|
|
|
q.type = this.do_qtype;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
len = dns_encode((char *)packet, sizeof(packet), &q, QR_QUERY, (char *)hostname, strlen((char *)hostname));
|
2009-09-20 15:11:14 +00:00
|
|
|
if (len < 1) {
|
|
|
|
warnx("dns_encode doesn't fit");
|
2015-09-28 05:07:00 +00:00
|
|
|
return -1;
|
2009-09-20 15:11:14 +00:00
|
|
|
}
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2015-10-04 14:39:40 +00:00
|
|
|
DEBUG(4, " Sendquery: id %5d name[0] '%c'", q.id, hostname[0]);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
sendto(this.dns_fd, packet, len, 0, (struct sockaddr*) &this.nameserv_addrs[this.current_nameserver],
|
2015-08-14 14:07:48 +00:00
|
|
|
sizeof(struct sockaddr_storage));
|
|
|
|
|
|
|
|
client_rotate_nameserver();
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
/* There are DNS relays that time out quickly but don't send anything
|
|
|
|
back on timeout.
|
|
|
|
And there are relays where, in lazy mode, our new query apparently
|
|
|
|
_replaces_ our previous query, and we get no answers at all in
|
|
|
|
lazy mode while legacy immediate-ping-pong works just fine.
|
2015-11-10 12:54:01 +00:00
|
|
|
In this case, the up/down windowsizes may need to be set to 1 for there
|
|
|
|
to only ever be one query pending.
|
2009-12-29 20:00:57 +00:00
|
|
|
Here we detect and fix these situations.
|
|
|
|
(Can't very well do this anywhere else; this is the only place
|
2015-10-17 14:28:10 +00:00
|
|
|
we'll reliably get to in such situations.)
|
2016-01-09 07:04:39 +00:00
|
|
|
Note: only start fixing up connection AFTER we have this.connected
|
2015-11-10 12:54:01 +00:00
|
|
|
and if user hasn't specified server timeout/window timeout etc. */
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_sent++;
|
|
|
|
if (this.send_query_sendcnt > 0 && this.send_query_sendcnt < 100 &&
|
|
|
|
this.lazymode && this.connected && this.autodetect_server_timeout) {
|
|
|
|
this.send_query_sendcnt++;
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if ((this.send_query_sendcnt > this.windowsize_down && this.send_query_recvcnt <= 0) ||
|
|
|
|
(this.send_query_sendcnt > 2 * this.windowsize_down && 4 * this.send_query_recvcnt < this.send_query_sendcnt)) {
|
|
|
|
if (this.max_timeout_ms > 500) {
|
|
|
|
this.max_timeout_ms -= 200;
|
|
|
|
double secs = (double) this.max_timeout_ms / 1000.0;
|
2015-10-04 14:13:47 +00:00
|
|
|
fprintf(stderr, "Receiving too few answers. Setting target timeout to %.1fs (-I%.1f)\n", secs, secs);
|
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
/* restart counting */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.send_query_sendcnt = 0;
|
|
|
|
this.send_query_recvcnt = 0;
|
2015-10-04 14:13:47 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
} else if (this.lazymode) {
|
2015-09-28 05:07:00 +00:00
|
|
|
fprintf(stderr, "Receiving too few answers. Will try to switch lazy mode off, but that may not"
|
2015-10-04 14:39:40 +00:00
|
|
|
" always work any more. Start with -L0 next time on this network.\n");
|
2016-01-09 07:04:39 +00:00
|
|
|
this.lazymode = 0;
|
|
|
|
this.server_timeout_ms = 0;
|
2009-12-29 20:00:57 +00:00
|
|
|
}
|
2016-01-09 11:29:17 +00:00
|
|
|
update_server_timeout(1);
|
2009-12-29 20:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-28 05:07:00 +00:00
|
|
|
return q.id;
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
send_raw(uint8_t *buf, size_t buflen, int cmd)
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
|
|
|
char packet[4096];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = MIN(sizeof(packet) - RAW_HDR_LEN, buflen);
|
|
|
|
|
|
|
|
memcpy(packet, raw_header, RAW_HDR_LEN);
|
|
|
|
if (len) {
|
|
|
|
memcpy(&packet[RAW_HDR_LEN], buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
len += RAW_HDR_LEN;
|
2016-01-09 11:29:17 +00:00
|
|
|
packet[RAW_HDR_CMD] = (cmd & 0xF0) | (this.userid & 0x0F);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
sendto(this.dns_fd, packet, len, 0, (struct sockaddr*)&this.raw_serv, sizeof(this.raw_serv));
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
send_raw_data(uint8_t *data, size_t datalen)
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2016-01-09 11:29:17 +00:00
|
|
|
send_raw(data, datalen, RAW_HDR_CMD_DATA);
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-28 05:07:00 +00:00
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
send_packet(char cmd, const uint8_t *data, const size_t datalen)
|
2015-09-28 05:07:00 +00:00
|
|
|
/* Base32 encodes data and sends as single DNS query
|
|
|
|
* Returns ID of sent query */
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2015-08-29 12:11:21 +00:00
|
|
|
uint8_t buf[4096];
|
2009-08-16 12:30:26 +00:00
|
|
|
|
|
|
|
buf[0] = cmd;
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
build_hostname(buf, sizeof(buf), data, datalen, this.topdomain, b32, this.hostname_maxlen, 1);
|
2015-08-29 12:11:21 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
return send_query(buf);
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
2015-10-17 14:28:10 +00:00
|
|
|
int
|
2016-01-09 11:29:17 +00:00
|
|
|
send_ping(int ping_response, int ack, int set_timeout)
|
2015-08-21 08:57:54 +00:00
|
|
|
{
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_pings++;
|
|
|
|
if (this.conn == CONN_DNS_NULL) {
|
2015-11-10 12:54:01 +00:00
|
|
|
uint8_t data[13];
|
2015-09-28 05:07:00 +00:00
|
|
|
int id;
|
2015-08-21 08:57:54 +00:00
|
|
|
|
2015-09-28 05:07:00 +00:00
|
|
|
/* Build ping header (see doc/proto_xxxxxxxx.txt) */
|
2016-01-09 07:04:39 +00:00
|
|
|
data[0] = this.userid;
|
2015-08-29 12:11:21 +00:00
|
|
|
data[1] = ack & 0xFF;
|
2015-09-28 05:07:00 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.outbuf && this.inbuf) {
|
|
|
|
data[2] = this.outbuf->windowsize & 0xff; /* Upstream window size */
|
|
|
|
data[3] = this.inbuf->windowsize & 0xff; /* Downstream window size */
|
|
|
|
data[4] = this.outbuf->start_seq_id & 0xff; /* Upstream window start */
|
|
|
|
data[5] = this.inbuf->start_seq_id & 0xff; /* Downstream window start */
|
2015-09-28 05:07:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
*(uint16_t *) (data + 6) = htons(this.server_timeout_ms);
|
|
|
|
*(uint16_t *) (data + 8) = htons(this.downstream_timeout_ms);
|
2015-09-28 05:07:00 +00:00
|
|
|
|
2015-10-27 09:25:56 +00:00
|
|
|
/* update server frag/lazy timeout, ack flag, respond with ping flag */
|
|
|
|
data[10] = ((set_timeout & 1) << 4) | ((set_timeout & 1) << 3) | ((ack < 0 ? 0 : 1) << 2) | (ping_response & 1);
|
2016-01-09 07:04:39 +00:00
|
|
|
data[11] = (this.rand_seed >> 8) & 0xff;
|
|
|
|
data[12] = (this.rand_seed >> 0) & 0xff;
|
|
|
|
this.rand_seed += 1;
|
2015-08-21 08:57:54 +00:00
|
|
|
|
2015-10-27 09:25:56 +00:00
|
|
|
DEBUG(3, " SEND PING: respond %d, ack %d, %s(server %ld ms, downfrag %ld ms), flags %02X",
|
2016-01-09 07:04:39 +00:00
|
|
|
ping_response, ack, set_timeout ? "SET " : "", this.server_timeout_ms,
|
|
|
|
this.downstream_timeout_ms, data[8]);
|
2015-09-28 05:07:00 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
id = send_packet('p', data, sizeof(data));
|
2015-09-28 05:07:00 +00:00
|
|
|
|
|
|
|
/* Log query ID as being sent now */
|
|
|
|
query_sent_now(id);
|
2015-10-17 14:28:10 +00:00
|
|
|
return id;
|
2015-08-21 08:57:54 +00:00
|
|
|
} else {
|
2016-01-09 11:29:17 +00:00
|
|
|
send_raw(NULL, 0, RAW_HDR_CMD_PING);
|
2015-10-17 14:28:10 +00:00
|
|
|
return -1;
|
2015-08-21 08:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-21 03:08:47 +00:00
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
send_next_frag()
|
2015-08-21 03:08:47 +00:00
|
|
|
/* Sends next available fragment of data from the outgoing window buffer */
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2015-08-29 12:11:21 +00:00
|
|
|
static uint8_t buf[MAX_FRAGSIZE], hdr[5];
|
2015-09-28 05:07:00 +00:00
|
|
|
int code, id;
|
2009-12-29 20:00:57 +00:00
|
|
|
static int datacmc = 0;
|
2015-08-21 03:08:47 +00:00
|
|
|
static char *datacmcchars = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
|
fragment *f;
|
2015-11-10 12:54:01 +00:00
|
|
|
size_t buflen;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2015-08-21 03:08:47 +00:00
|
|
|
/* Get next fragment to send */
|
2016-01-09 07:04:39 +00:00
|
|
|
f = window_get_next_sending_fragment(this.outbuf, &this.next_downstream_ack);
|
2015-08-29 12:11:21 +00:00
|
|
|
if (!f) {
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.outbuf->numitems > 0) {
|
2015-08-21 03:08:47 +00:00
|
|
|
/* There is stuff to send but we're out of sync, so send a ping
|
|
|
|
* to get things back in order and keep the packets flowing */
|
2016-01-09 11:29:17 +00:00
|
|
|
send_ping(1, this.next_downstream_ack, 1);
|
2016-01-09 07:04:39 +00:00
|
|
|
this.next_downstream_ack = -1;
|
|
|
|
window_tick(this.outbuf);
|
2015-08-21 03:08:47 +00:00
|
|
|
}
|
2015-09-28 05:07:00 +00:00
|
|
|
return; /* nothing to send */
|
2015-08-21 03:08:47 +00:00
|
|
|
}
|
2009-08-16 12:30:26 +00:00
|
|
|
|
|
|
|
/* Build upstream data header (see doc/proto_xxxxxxxx.txt) */
|
2016-01-09 07:04:39 +00:00
|
|
|
buf[0] = this.userid_char; /* First byte is hex this.userid */
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2015-08-21 03:08:47 +00:00
|
|
|
buf[1] = datacmcchars[datacmc]; /* Second byte is data-CMC */
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
/* Next 3 bytes is seq ID, downstream ACK and flags */
|
2015-09-28 05:07:00 +00:00
|
|
|
code = ((f->ack_other < 0 ? 0 : 1) << 3) | (f->compressed << 2)
|
2015-08-29 12:11:21 +00:00
|
|
|
| (f->start << 1) | f->end;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
hdr[0] = f->seqID & 0xFF;
|
|
|
|
hdr[1] = f->ack_other & 0xFF;
|
|
|
|
hdr[2] = code << 4; /* Flags are in upper 4 bits - lower 4 unused */
|
2015-08-21 03:08:47 +00:00
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
buflen = sizeof(buf) - 1;
|
|
|
|
/* Encode 3 bytes data into 2 bytes after buf */
|
2015-11-10 12:54:01 +00:00
|
|
|
b32->encode(buf + 2, &buflen, hdr, 3);
|
2015-08-21 03:08:47 +00:00
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
/* Encode data into buf after header (6 = user + CMC + 4 bytes header) */
|
2016-01-09 07:04:39 +00:00
|
|
|
build_hostname(buf, sizeof(buf), f->data, f->len, this.topdomain,
|
|
|
|
this.dataenc, this.hostname_maxlen, 6);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
datacmc++;
|
|
|
|
if (datacmc >= 36)
|
|
|
|
datacmc = 0;
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2015-11-10 12:54:01 +00:00
|
|
|
DEBUG(3, " SEND DATA: seq %d, ack %d, len %lu, s%d e%d c%d flags %1X",
|
|
|
|
f->seqID, f->ack_other, f->len, f->start, f->end, f->compressed, hdr[2] >> 4);
|
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
id = send_query(buf);
|
2015-09-28 05:07:00 +00:00
|
|
|
/* Log query ID as being sent now */
|
|
|
|
query_sent_now(id);
|
2015-08-21 15:24:53 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
window_tick(this.outbuf);
|
|
|
|
this.num_frags_sent++;
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
2009-09-21 20:06:30 +00:00
|
|
|
static void
|
2009-12-29 20:00:57 +00:00
|
|
|
write_dns_error(struct query *q, int ignore_some_errors)
|
|
|
|
/* This is called from:
|
|
|
|
1. handshake_waitdns() when already checked that reply fits to our
|
|
|
|
latest query.
|
2015-10-17 14:28:10 +00:00
|
|
|
2. tunnel_dns() when already checked that reply is for a ping or data
|
|
|
|
packet, but possibly timed out.
|
|
|
|
Errors should not be ignored, but too many can be annoying.
|
2009-12-29 20:00:57 +00:00
|
|
|
*/
|
2009-09-21 20:06:30 +00:00
|
|
|
{
|
2015-10-17 14:28:10 +00:00
|
|
|
static size_t errorcounts[24] = {0};
|
2009-09-21 20:06:30 +00:00
|
|
|
if (!q) return;
|
|
|
|
|
2015-10-17 14:28:10 +00:00
|
|
|
if (q->rcode < 24) {
|
|
|
|
errorcounts[q->rcode]++;
|
|
|
|
if (errorcounts[q->rcode] == 20) {
|
|
|
|
warnx("Too many error replies, not logging any more.");
|
|
|
|
return;
|
|
|
|
} else if (errorcounts[q->rcode] > 20) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-21 20:06:30 +00:00
|
|
|
switch (q->rcode) {
|
|
|
|
case NOERROR: /* 0 */
|
2009-12-29 20:00:57 +00:00
|
|
|
if (!ignore_some_errors)
|
|
|
|
warnx("Got reply without error, but also without question and/or answer");
|
2009-09-21 20:06:30 +00:00
|
|
|
break;
|
|
|
|
case FORMERR: /* 1 */
|
|
|
|
warnx("Got FORMERR as reply: server does not understand our request");
|
|
|
|
break;
|
|
|
|
case SERVFAIL: /* 2 */
|
2009-12-29 20:00:57 +00:00
|
|
|
if (!ignore_some_errors)
|
|
|
|
warnx("Got SERVFAIL as reply: server failed or recursion timeout");
|
2009-09-21 20:06:30 +00:00
|
|
|
break;
|
|
|
|
case NXDOMAIN: /* 3 */
|
|
|
|
warnx("Got NXDOMAIN as reply: domain does not exist");
|
|
|
|
break;
|
|
|
|
case NOTIMP: /* 4 */
|
|
|
|
warnx("Got NOTIMP as reply: server does not support our request");
|
|
|
|
break;
|
|
|
|
case REFUSED: /* 5 */
|
|
|
|
warnx("Got REFUSED as reply");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warnx("Got RCODE %u as reply", q->rcode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
static size_t
|
|
|
|
dns_namedec(uint8_t *outdata, size_t outdatalen, uint8_t *buf, size_t buflen)
|
2009-12-29 20:00:57 +00:00
|
|
|
/* Decodes *buf to *outdata.
|
|
|
|
* *buf WILL be changed by undotify.
|
|
|
|
* Note: buflen must be _exactly_ strlen(buf) before undotifying.
|
|
|
|
* (undotify of reduced-len won't copy \0, base-X decode will decode too much.)
|
|
|
|
* Returns #bytes usefully filled in outdata.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
size_t outdatalenu = outdatalen;
|
|
|
|
|
|
|
|
switch (buf[0]) {
|
|
|
|
case 'h': /* Hostname with base32 */
|
|
|
|
case 'H':
|
|
|
|
/* Need 1 byte H, 3 bytes ".xy", >=1 byte data */
|
|
|
|
if (buflen < 5)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* this also does undotify */
|
2015-08-29 12:11:21 +00:00
|
|
|
return unpack_data(outdata, outdatalen, buf + 1, buflen - 4, b32);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
case 'i': /* Hostname++ with base64 */
|
|
|
|
case 'I':
|
|
|
|
/* Need 1 byte I, 3 bytes ".xy", >=1 byte data */
|
|
|
|
if (buflen < 5)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* this also does undotify */
|
2015-08-29 12:11:21 +00:00
|
|
|
return unpack_data(outdata, outdatalen, buf + 1, buflen - 4, b64);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
case 'j': /* Hostname++ with base64u */
|
|
|
|
case 'J':
|
|
|
|
/* Need 1 byte J, 3 bytes ".xy", >=1 byte data */
|
|
|
|
if (buflen < 5)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* this also does undotify */
|
2015-08-29 12:11:21 +00:00
|
|
|
return unpack_data(outdata, outdatalen, buf + 1, buflen - 4, b64u);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
case 'k': /* Hostname++ with base128 */
|
|
|
|
case 'K':
|
|
|
|
/* Need 1 byte J, 3 bytes ".xy", >=1 byte data */
|
|
|
|
if (buflen < 5)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* this also does undotify */
|
2015-08-29 12:11:21 +00:00
|
|
|
return unpack_data(outdata, outdatalen, buf + 1, buflen - 4, b128);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
case 't': /* plain base32(Thirty-two) from TXT */
|
|
|
|
case 'T':
|
|
|
|
if (buflen < 2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return b32->decode(outdata, &outdatalenu, buf + 1, buflen - 1);
|
|
|
|
|
|
|
|
case 's': /* plain base64(Sixty-four) from TXT */
|
|
|
|
case 'S':
|
|
|
|
if (buflen < 2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return b64->decode(outdata, &outdatalenu, buf + 1, buflen - 1);
|
|
|
|
|
|
|
|
case 'u': /* plain base64u (Underscore) from TXT */
|
|
|
|
case 'U':
|
|
|
|
if (buflen < 2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return b64u->decode(outdata, &outdatalenu, buf + 1, buflen - 1);
|
|
|
|
|
|
|
|
case 'v': /* plain base128 from TXT */
|
|
|
|
case 'V':
|
|
|
|
if (buflen < 2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return b128->decode(outdata, &outdatalenu, buf + 1, buflen - 1);
|
|
|
|
|
|
|
|
case 'r': /* Raw binary from TXT */
|
|
|
|
case 'R':
|
|
|
|
/* buflen>=1 already checked */
|
|
|
|
buflen--;
|
|
|
|
buflen = MIN(buflen, outdatalen);
|
|
|
|
memcpy(outdata, buf + 1, buflen);
|
|
|
|
return buflen;
|
|
|
|
|
|
|
|
default:
|
|
|
|
warnx("Received unsupported encoding");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* notreached */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
read_dns_withq(uint8_t *buf, size_t buflen, struct query *q)
|
2009-12-29 20:00:57 +00:00
|
|
|
/* Returns -1 on receive error or decode error, including DNS error replies.
|
|
|
|
Returns 0 on replies that could be correct but are useless, and are not
|
|
|
|
DNS error replies.
|
|
|
|
Returns >0 on correct replies; value is #valid bytes in *buf.
|
|
|
|
*/
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2014-02-05 21:36:53 +00:00
|
|
|
struct sockaddr_storage from;
|
2015-08-29 12:11:21 +00:00
|
|
|
uint8_t data[64*1024];
|
2009-08-16 12:30:26 +00:00
|
|
|
socklen_t addrlen;
|
|
|
|
int r;
|
|
|
|
|
2014-02-05 21:36:53 +00:00
|
|
|
addrlen = sizeof(from);
|
2016-01-09 11:29:17 +00:00
|
|
|
if ((r = recvfrom(this.dns_fd, data, sizeof(data), 0,
|
2009-09-20 21:10:44 +00:00
|
|
|
(struct sockaddr*)&from, &addrlen)) < 0) {
|
2009-08-16 12:30:26 +00:00
|
|
|
warn("recvfrom");
|
2009-09-20 21:10:44 +00:00
|
|
|
return -1;
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.conn == CONN_DNS_NULL) {
|
2009-08-16 12:30:26 +00:00
|
|
|
int rv;
|
2009-09-20 21:10:44 +00:00
|
|
|
if (r <= 0)
|
|
|
|
/* useless packet */
|
|
|
|
return 0;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
rv = dns_decode((char *)buf, buflen, q, QR_ANSWER, (char *)data, r);
|
2009-09-20 21:10:44 +00:00
|
|
|
if (rv <= 0)
|
|
|
|
return rv;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
if (q->type == T_CNAME || q->type == T_TXT)
|
|
|
|
/* CNAME can also be returned from an A question */
|
2009-09-20 15:11:14 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* buf is a hostname or txt stream that we still need to
|
|
|
|
* decode to binary
|
2014-06-01 06:34:18 +00:00
|
|
|
*
|
2009-09-20 15:11:14 +00:00
|
|
|
* also update rv with the number of valid bytes
|
2014-06-01 06:34:18 +00:00
|
|
|
*
|
2009-09-20 15:11:14 +00:00
|
|
|
* data is unused here, and will certainly hold the smaller binary
|
|
|
|
*/
|
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
rv = dns_namedec(data, sizeof(data), buf, rv);
|
2009-09-20 15:11:14 +00:00
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
rv = MIN(rv, buflen);
|
|
|
|
if (rv > 0)
|
2009-09-20 15:11:14 +00:00
|
|
|
memcpy(buf, data, rv);
|
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
} else if (q->type == T_MX || q->type == T_SRV) {
|
|
|
|
/* buf is like "Hname.com\0Hanother.com\0\0" */
|
|
|
|
int buftotal = rv; /* idx of last \0 */
|
|
|
|
int bufoffset = 0;
|
|
|
|
int dataoffset = 0;
|
|
|
|
int thispartlen, dataspace, datanew;
|
|
|
|
|
|
|
|
while (1) {
|
2015-08-29 12:11:21 +00:00
|
|
|
thispartlen = strlen((char *)buf);
|
2009-12-29 20:00:57 +00:00
|
|
|
thispartlen = MIN(thispartlen, buftotal-bufoffset);
|
|
|
|
dataspace = sizeof(data) - dataoffset;
|
|
|
|
if (thispartlen <= 0 || dataspace <= 0)
|
2009-09-20 15:11:14 +00:00
|
|
|
break;
|
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
datanew = dns_namedec(data + dataoffset, dataspace,
|
|
|
|
buf + bufoffset, thispartlen);
|
|
|
|
if (datanew <= 0)
|
2009-09-20 15:11:14 +00:00
|
|
|
break;
|
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
bufoffset += thispartlen + 1;
|
|
|
|
dataoffset += datanew;
|
2009-09-20 15:11:14 +00:00
|
|
|
}
|
2009-12-29 20:00:57 +00:00
|
|
|
rv = dataoffset;
|
|
|
|
rv = MIN(rv, buflen);
|
|
|
|
if (rv > 0)
|
|
|
|
memcpy(buf, data, rv);
|
2009-09-20 15:11:14 +00:00
|
|
|
}
|
|
|
|
|
2015-10-04 14:39:40 +00:00
|
|
|
DEBUG(2, "RX: id %5d name[0]='%c'", q->id, q->name[0]);
|
2015-09-28 05:07:00 +00:00
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
return rv;
|
|
|
|
} else { /* CONN_RAW_UDP */
|
2015-09-28 05:07:00 +00:00
|
|
|
size_t datalen;
|
|
|
|
uint8_t buf[64*1024];
|
2009-08-16 12:30:26 +00:00
|
|
|
|
|
|
|
/* minimum length */
|
2015-09-28 05:07:00 +00:00
|
|
|
if (r < RAW_HDR_LEN)
|
|
|
|
return 0;
|
2009-08-16 12:30:26 +00:00
|
|
|
/* should start with header */
|
2015-09-28 05:07:00 +00:00
|
|
|
if (memcmp(data, raw_header, RAW_HDR_IDENT_LEN))
|
|
|
|
return 0;
|
2009-08-16 12:30:26 +00:00
|
|
|
/* should be my user id */
|
2016-01-09 07:04:39 +00:00
|
|
|
if (RAW_HDR_GET_USR(data) != this.userid)
|
2015-09-28 05:07:00 +00:00
|
|
|
return 0;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
if (RAW_HDR_GET_CMD(data) == RAW_HDR_CMD_DATA ||
|
|
|
|
RAW_HDR_GET_CMD(data) == RAW_HDR_CMD_PING)
|
2016-01-09 07:04:39 +00:00
|
|
|
this.lastdownstreamtime = time(NULL);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
/* should be data packet */
|
2015-09-28 05:07:00 +00:00
|
|
|
if (RAW_HDR_GET_CMD(data) != RAW_HDR_CMD_DATA)
|
|
|
|
return 0;
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
r -= RAW_HDR_LEN;
|
|
|
|
datalen = sizeof(buf);
|
2015-09-28 05:07:00 +00:00
|
|
|
if (uncompress(buf, &datalen, data + RAW_HDR_LEN, r) == Z_OK) {
|
2016-01-09 11:29:17 +00:00
|
|
|
write_tun(this.tun_fd, buf, datalen);
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2015-09-28 05:07:00 +00:00
|
|
|
/* all done */
|
2009-08-16 12:30:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 14:13:47 +00:00
|
|
|
int
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_waitdns(char *buf, size_t buflen, char cmd, int timeout)
|
2009-12-29 20:00:57 +00:00
|
|
|
/* Wait for DNS reply fitting to our latest query and returns it.
|
|
|
|
Returns length of reply = #bytes used in buf.
|
|
|
|
Returns 0 if fitting reply happens to be useless.
|
|
|
|
Returns -2 on (at least) DNS error that fits to our latest query,
|
|
|
|
error message already printed.
|
|
|
|
Returns -3 on timeout (given in seconds).
|
|
|
|
Returns -1 on other errors.
|
|
|
|
|
|
|
|
Timeout is restarted when "wrong" (previous/delayed) replies are received,
|
|
|
|
so effective timeout may be longer than specified.
|
2009-09-20 21:10:44 +00:00
|
|
|
*/
|
|
|
|
{
|
|
|
|
struct query q;
|
2009-12-29 20:00:57 +00:00
|
|
|
int r, rv;
|
|
|
|
fd_set fds;
|
|
|
|
struct timeval tv;
|
2015-09-28 05:07:00 +00:00
|
|
|
char qcmd;
|
|
|
|
|
|
|
|
cmd = toupper(cmd);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
tv.tv_sec = timeout;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
FD_ZERO(&fds);
|
2016-01-09 11:29:17 +00:00
|
|
|
FD_SET(this.dns_fd, &fds);
|
|
|
|
r = select(this.dns_fd + 1, &fds, NULL, NULL, &tv);
|
2009-09-20 21:10:44 +00:00
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
if (r < 0)
|
|
|
|
return -1; /* select error */
|
|
|
|
if (r == 0)
|
|
|
|
return -3; /* select timeout */
|
2009-09-20 21:10:44 +00:00
|
|
|
|
2015-10-27 09:25:56 +00:00
|
|
|
q.id = -1;
|
2009-12-29 20:00:57 +00:00
|
|
|
q.name[0] = '\0';
|
2016-01-09 11:29:17 +00:00
|
|
|
rv = read_dns_withq((uint8_t *)buf, buflen, &q);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2015-09-28 05:07:00 +00:00
|
|
|
qcmd = toupper(q.name[0]);
|
2016-01-09 07:04:39 +00:00
|
|
|
if (q.id != this.chunkid || qcmd != cmd) {
|
2015-10-04 14:39:40 +00:00
|
|
|
DEBUG(1, "Ignoring unfitting reply id %d starting with '%c'", q.id, q.name[0]);
|
2009-12-29 20:00:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if still here: reply matches our latest query */
|
|
|
|
|
|
|
|
/* Non-recursive DNS servers (such as [a-m].root-servers.net)
|
|
|
|
return no answer, but only additional and authority records.
|
|
|
|
Can't explicitly test for that here, just assume that
|
|
|
|
NOERROR is such situation. Only trigger on the very first
|
|
|
|
requests (Y or V, depending if -T given).
|
|
|
|
*/
|
|
|
|
if (rv < 0 && q.rcode == NOERROR &&
|
|
|
|
(q.name[0] == 'Y' || q.name[0] == 'y' ||
|
|
|
|
q.name[0] == 'V' || q.name[0] == 'v')) {
|
|
|
|
fprintf(stderr, "Got empty reply. This nameserver may not be resolving recursively, use another.\n");
|
|
|
|
fprintf(stderr, "Try \"iodine [options] ns.%s %s\" first, it might just work.\n",
|
2016-01-09 07:04:39 +00:00
|
|
|
this.topdomain, this.topdomain);
|
2009-12-29 20:00:57 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we get an immediate SERVFAIL on the handshake query
|
|
|
|
we're waiting for, wait a while before sending the next.
|
|
|
|
SERVFAIL reliably happens during fragsize autoprobe, but
|
|
|
|
mostly long after we've moved along to some other queries.
|
|
|
|
However, some DNS relays, once they throw a SERVFAIL, will
|
|
|
|
for several seconds apply it immediately to _any_ new query
|
2016-01-09 07:04:39 +00:00
|
|
|
for the same this.topdomain. When this happens, waiting a while
|
2009-12-29 20:00:57 +00:00
|
|
|
is the only option that works.
|
|
|
|
*/
|
|
|
|
if (rv < 0 && q.rcode == SERVFAIL)
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
if (rv < 0) {
|
|
|
|
write_dns_error(&q, 1);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
/* rv either 0 or >0, return it as is. */
|
|
|
|
return rv;
|
2009-09-21 20:06:30 +00:00
|
|
|
}
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
/* not reached */
|
|
|
|
return -1;
|
2009-09-20 21:10:44 +00:00
|
|
|
}
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2015-10-04 14:13:47 +00:00
|
|
|
int
|
2015-09-28 05:07:00 +00:00
|
|
|
parse_data(uint8_t *data, size_t len, fragment *f, int *immediate)
|
2015-08-21 03:08:47 +00:00
|
|
|
{
|
2015-08-29 12:11:21 +00:00
|
|
|
size_t headerlen = DOWNSTREAM_HDR;
|
2015-09-28 05:07:00 +00:00
|
|
|
int ping;
|
2015-08-21 03:08:47 +00:00
|
|
|
memset(f, 0, sizeof(fragment));
|
2015-09-28 05:07:00 +00:00
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
f->seqID = data[0];
|
2015-09-28 05:07:00 +00:00
|
|
|
|
|
|
|
/* Flags */
|
|
|
|
f->end = data[2] & 1;
|
|
|
|
f->start = (data[2] >> 1) & 1;
|
|
|
|
f->compressed = (data[2] >> 2) & 1;
|
|
|
|
f->ack_other = (data[2] >> 3) & 1 ? data[1] : -1;
|
|
|
|
ping = (data[2] >> 4) & 1;
|
|
|
|
|
|
|
|
if (immediate)
|
|
|
|
*immediate = (data[2] >> 5) & 1;
|
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
if (ping) { /* Handle ping stuff */
|
2015-09-28 05:07:00 +00:00
|
|
|
static unsigned dn_start_seq, up_start_seq, dn_wsize, up_wsize;
|
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
headerlen = DOWNSTREAM_PING_HDR;
|
|
|
|
if (len < headerlen) return -1; /* invalid packet - continue */
|
2015-09-28 05:07:00 +00:00
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
/* Parse data/ping header */
|
2015-09-28 05:07:00 +00:00
|
|
|
dn_wsize = data[3];
|
2015-08-29 12:11:21 +00:00
|
|
|
up_wsize = data[4];
|
|
|
|
dn_start_seq = data[5];
|
|
|
|
up_start_seq = data[6];
|
2015-10-04 14:39:40 +00:00
|
|
|
DEBUG(3, "PING pkt data=%lu WS: up=%u, dn=%u; Start: up=%u, dn=%u",
|
2015-08-29 12:11:21 +00:00
|
|
|
len - headerlen, up_wsize, dn_wsize, up_start_seq, dn_start_seq);
|
2015-08-21 08:57:54 +00:00
|
|
|
}
|
2015-08-29 12:11:21 +00:00
|
|
|
f->len = len - headerlen;
|
2015-09-28 05:07:00 +00:00
|
|
|
if (f->len > 0)
|
|
|
|
memcpy(f->data, data + headerlen, MIN(f->len, sizeof(f->data)));
|
2015-08-21 08:57:54 +00:00
|
|
|
return ping; /* return ping flag (if corresponding query was a ping) */
|
2015-08-21 03:08:47 +00:00
|
|
|
}
|
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
tunnel_tun()
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2015-09-28 05:07:00 +00:00
|
|
|
size_t datalen;
|
2015-08-21 03:08:47 +00:00
|
|
|
uint8_t out[64*1024];
|
|
|
|
uint8_t in[64*1024];
|
2015-09-28 05:07:00 +00:00
|
|
|
uint8_t *data;
|
2009-08-16 12:30:26 +00:00
|
|
|
ssize_t read;
|
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
if ((read = read_tun(this.tun_fd, in, sizeof(in))) <= 0)
|
2009-08-16 12:30:26 +00:00
|
|
|
return -1;
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
DEBUG(2, " IN: %lu bytes on tunnel, to be compressed: %d", read, this.compression_up);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.conn != CONN_DNS_NULL || this.compression_up) {
|
2015-09-28 05:07:00 +00:00
|
|
|
datalen = sizeof(out);
|
|
|
|
compress2(out, &datalen, in, read, 9);
|
|
|
|
data = out;
|
|
|
|
} else {
|
|
|
|
datalen = read;
|
|
|
|
data = in;
|
|
|
|
}
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.conn == CONN_DNS_NULL) {
|
2015-10-27 10:33:44 +00:00
|
|
|
/* Check if outgoing buffer can hold data */
|
2016-01-09 07:04:39 +00:00
|
|
|
if (window_buffer_available(this.outbuf) < (read / MAX_FRAGSIZE) + 1) {
|
2015-10-27 10:33:44 +00:00
|
|
|
DEBUG(1, " Outgoing buffer full (%lu/%lu), not adding data!",
|
2016-01-09 07:04:39 +00:00
|
|
|
this.outbuf->numitems, this.outbuf->length);
|
2015-10-27 10:33:44 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
window_add_outgoing_data(this.outbuf, data, datalen, this.compression_up);
|
2015-10-17 14:28:10 +00:00
|
|
|
/* Don't send anything here to respect min. send interval */
|
2009-08-16 12:30:26 +00:00
|
|
|
} else {
|
2016-01-09 11:29:17 +00:00
|
|
|
send_raw_data(data, datalen);
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
tunnel_dns()
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2015-10-17 14:28:10 +00:00
|
|
|
struct query q;
|
2015-08-29 12:11:21 +00:00
|
|
|
size_t datalen, buflen;
|
2015-10-17 14:28:10 +00:00
|
|
|
uint8_t buf[64*1024], cbuf[64*1024], *data;
|
|
|
|
fragment f;
|
2015-09-28 05:07:00 +00:00
|
|
|
int read, compressed, res, immediate;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2015-08-21 03:08:47 +00:00
|
|
|
memset(&q, 0, sizeof(q));
|
2015-08-14 14:07:48 +00:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2015-08-21 03:08:47 +00:00
|
|
|
memset(cbuf, 0, sizeof(cbuf));
|
2016-01-09 11:29:17 +00:00
|
|
|
read = read_dns_withq(cbuf, sizeof(cbuf), &q);
|
2009-09-21 20:06:30 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.conn != CONN_DNS_NULL)
|
2009-12-29 20:00:57 +00:00
|
|
|
return 1; /* everything already done */
|
|
|
|
|
|
|
|
/* Don't process anything that isn't data for us; usually error
|
|
|
|
replies from fragsize probes etc. However a sequence of those,
|
|
|
|
mostly 1 sec apart, will continuously break the >=2-second select
|
|
|
|
timeout, which means we won't send a proper ping for a while.
|
|
|
|
So make select a bit faster, <1sec. */
|
2009-09-21 20:06:30 +00:00
|
|
|
if (q.name[0] != 'P' && q.name[0] != 'p' &&
|
2016-01-09 07:04:39 +00:00
|
|
|
q.name[0] != this.userid_char && q.name[0] != this.userid_char2) {
|
|
|
|
this.send_ping_soon = 700;
|
2015-10-17 14:28:10 +00:00
|
|
|
got_response(q.id, 0, 0);
|
2009-09-21 20:06:30 +00:00
|
|
|
return -1; /* nothing done */
|
2009-12-29 20:00:57 +00:00
|
|
|
}
|
2009-09-21 20:06:30 +00:00
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
if (read < DOWNSTREAM_HDR) {
|
2009-09-20 21:10:44 +00:00
|
|
|
/* Maybe SERVFAIL etc. Send ping to get things back in order,
|
2015-10-17 14:28:10 +00:00
|
|
|
but wait a bit to prevent fast ping-pong loops.
|
|
|
|
Only change options if user hasn't specified server timeout */
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
if (read < 0)
|
|
|
|
write_dns_error(&q, 0);
|
|
|
|
|
2015-10-17 14:28:10 +00:00
|
|
|
if (q.rcode == SERVFAIL && read < 0) {
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_servfail++;
|
2015-10-04 14:13:47 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.lazymode) {
|
2015-10-04 14:13:47 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.send_query_recvcnt < 500 && this.num_servfail < 4) {
|
|
|
|
fprintf(stderr, "Hmm, that's %ld SERVFAILs. Your data should still go through...\n", this.num_servfail);
|
2015-10-17 14:28:10 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
} else if (this.send_query_recvcnt < 500 && this.num_servfail >= 10 &&
|
|
|
|
this.autodetect_server_timeout && this.max_timeout_ms >= 500 && this.num_servfail % 5 == 0) {
|
2015-10-17 14:28:10 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
this.max_timeout_ms -= 200;
|
|
|
|
double target_timeout = (float) this.max_timeout_ms / 1000.0;
|
2015-10-17 14:28:10 +00:00
|
|
|
fprintf(stderr, "Too many SERVFAILs (%ld), reducing timeout to"
|
|
|
|
" %.1f secs. (use -I%.1f next time on this network)\n",
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_servfail, target_timeout, target_timeout);
|
2015-10-17 14:28:10 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
/* Reset query counts this.stats */
|
|
|
|
this.send_query_sendcnt = 0;
|
|
|
|
this.send_query_recvcnt = 0;
|
2016-01-09 11:29:17 +00:00
|
|
|
update_server_timeout(1);
|
2015-10-04 14:13:47 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
} else if (this.send_query_recvcnt < 500 && this.num_servfail >= 40 &&
|
|
|
|
this.autodetect_server_timeout && this.max_timeout_ms < 500) {
|
2015-10-17 14:28:10 +00:00
|
|
|
|
|
|
|
/* last-ditch attempt to fix SERVFAILs - disable lazy mode */
|
|
|
|
immediate_mode_defaults();
|
|
|
|
fprintf(stderr, "Attempting to disable lazy mode due to excessive SERVFAILs\n");
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_switch_options(0, this.compression_down, this.downenc);
|
2015-10-17 14:28:10 +00:00
|
|
|
}
|
2009-12-29 20:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
this.send_ping_soon = 900;
|
2015-10-17 14:28:10 +00:00
|
|
|
|
|
|
|
/* Mark query as received */
|
|
|
|
got_response(q.id, 0, 1);
|
2009-09-20 21:10:44 +00:00
|
|
|
return -1; /* nothing done */
|
|
|
|
}
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
this.send_query_recvcnt++; /* unlikely we will ever overflow (2^64 queries is a LOT) */
|
2015-10-17 14:28:10 +00:00
|
|
|
|
2015-08-21 03:08:47 +00:00
|
|
|
if (read == 5 && !strncmp("BADIP", (char *)cbuf, 5)) {
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_badip++;
|
|
|
|
if (this.num_badip % 5 == 1) {
|
2015-10-04 14:13:47 +00:00
|
|
|
fprintf(stderr, "BADIP (%ld): Server rejected sender IP address (maybe iodined -c will help), or server "
|
2016-01-09 07:04:39 +00:00
|
|
|
"kicked us due to timeout. Will exit if no downstream data is received in 60 seconds.\n", this.num_badip);
|
2015-10-04 14:13:47 +00:00
|
|
|
}
|
2009-09-20 21:10:44 +00:00
|
|
|
return -1; /* nothing done */
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
/* Okay, we have a recent downstream packet */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.lastdownstreamtime = time(NULL);
|
2015-09-28 05:07:00 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_recv++;
|
2015-08-29 12:11:21 +00:00
|
|
|
|
2015-08-21 03:08:47 +00:00
|
|
|
/* Decode the downstream data header and fragment-ify ready for processing */
|
2015-09-28 05:07:00 +00:00
|
|
|
res = parse_data(cbuf, read, &f, &immediate);
|
2015-10-17 14:28:10 +00:00
|
|
|
|
|
|
|
/* Mark query as received */
|
|
|
|
got_response(q.id, immediate, 0);
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if ((this.debug >= 3 && res) || (this.debug >= 2 && !res))
|
2015-09-28 05:07:00 +00:00
|
|
|
fprintf(stderr, " RX %s; frag ID %3u, ACK %3d, compression %d, datalen %lu, s%d e%d\n",
|
|
|
|
res ? "PING" : "DATA", f.seqID, f.ack_other, f.compressed, f.len, f.start, f.end);
|
|
|
|
|
2009-09-20 21:10:44 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
window_ack(this.outbuf, f.ack_other);
|
|
|
|
window_tick(this.outbuf);
|
2009-09-20 21:10:44 +00:00
|
|
|
|
2015-08-21 03:08:47 +00:00
|
|
|
/* In lazy mode, we shouldn't get immediate replies to our most-recent
|
|
|
|
query, only during heavy data transfer. Since this means the server
|
|
|
|
doesn't have any packets to send, send one relatively fast (but not
|
|
|
|
too fast, to avoid runaway ping-pong loops..) */
|
2015-08-29 12:11:21 +00:00
|
|
|
/* Don't send anything too soon; no data waiting from server */
|
2015-08-21 03:08:47 +00:00
|
|
|
if (f.len == 0) {
|
2015-10-04 14:39:40 +00:00
|
|
|
if (!res)
|
|
|
|
DEBUG(1, "[WARNING] Received downstream data fragment with 0 length and NOT a ping!");
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.lazymode)
|
|
|
|
this.send_ping_soon = 100;
|
2015-09-28 05:07:00 +00:00
|
|
|
else
|
2016-01-09 07:04:39 +00:00
|
|
|
this.send_ping_soon = 700;
|
2015-08-21 03:08:47 +00:00
|
|
|
return -1;
|
2009-09-20 21:10:44 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 05:07:00 +00:00
|
|
|
/* Get next ACK if nothing already pending: if we get a new ack
|
|
|
|
* then we must send it immediately. */
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.next_downstream_ack >= 0) {
|
2015-09-28 05:07:00 +00:00
|
|
|
/* If this happens something is wrong (or last frag was a re-send)
|
|
|
|
* May result in ACKs being delayed. */
|
2016-01-09 07:04:39 +00:00
|
|
|
DEBUG(1, "this.next_downstream_ack NOT -1! (%d), %u resends, %u oos", this.next_downstream_ack, this.outbuf->resends, this.outbuf->oos);
|
2015-08-29 12:11:21 +00:00
|
|
|
}
|
2009-09-20 21:10:44 +00:00
|
|
|
|
2015-10-17 14:28:10 +00:00
|
|
|
/* Downstream data traffic + ack data fragment */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.next_downstream_ack = f.seqID;
|
|
|
|
window_process_incoming_fragment(this.inbuf, &f);
|
2009-09-20 21:10:44 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_frags_recv++;
|
2015-10-04 14:13:47 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
datalen = window_reassemble_data(this.inbuf, cbuf, sizeof(cbuf), &compressed);
|
2015-09-28 05:07:00 +00:00
|
|
|
if (datalen > 0) {
|
2015-08-21 03:08:47 +00:00
|
|
|
if (compressed) {
|
2015-08-29 12:11:21 +00:00
|
|
|
buflen = sizeof(buf);
|
|
|
|
if ((res = uncompress(buf, &buflen, cbuf, datalen)) != Z_OK) {
|
2015-10-04 14:39:40 +00:00
|
|
|
DEBUG(1, "Uncompress failed (%d) for data len %lu: reassembled data corrupted or incomplete!", res, datalen);
|
2015-09-28 05:07:00 +00:00
|
|
|
datalen = 0;
|
|
|
|
} else {
|
|
|
|
datalen = buflen;
|
2009-09-20 21:10:44 +00:00
|
|
|
}
|
2015-10-04 14:13:47 +00:00
|
|
|
data = buf;
|
|
|
|
} else {
|
|
|
|
data = cbuf;
|
2009-09-20 21:10:44 +00:00
|
|
|
}
|
2015-09-28 05:07:00 +00:00
|
|
|
|
|
|
|
if (datalen)
|
2016-01-09 11:29:17 +00:00
|
|
|
write_tun(this.tun_fd, data, datalen);
|
2009-09-20 21:10:44 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 03:08:47 +00:00
|
|
|
/* Move window along after doing all data processing */
|
2016-01-09 07:04:39 +00:00
|
|
|
window_tick(this.inbuf);
|
2009-09-20 21:10:44 +00:00
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-01-09 11:29:17 +00:00
|
|
|
client_tunnel()
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2015-11-10 12:54:01 +00:00
|
|
|
struct timeval tv, nextresend, tmp, now, now2;
|
2009-08-16 12:30:26 +00:00
|
|
|
fd_set fds;
|
|
|
|
int rv;
|
2015-11-10 12:54:01 +00:00
|
|
|
int i, use_min_send;
|
2015-10-17 14:28:10 +00:00
|
|
|
int sending, total;
|
2015-10-04 14:13:47 +00:00
|
|
|
time_t last_stats;
|
2015-10-17 14:28:10 +00:00
|
|
|
size_t sent_since_report, recv_since_report;
|
2015-09-28 05:07:00 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
this.connected = 1;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2015-09-28 05:07:00 +00:00
|
|
|
/* start counting now */
|
2009-08-16 12:30:26 +00:00
|
|
|
rv = 0;
|
2016-01-09 07:04:39 +00:00
|
|
|
this.lastdownstreamtime = time(NULL);
|
2015-10-04 14:13:47 +00:00
|
|
|
last_stats = time(NULL);
|
|
|
|
|
|
|
|
/* reset connection statistics */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_badip = 0;
|
|
|
|
this.num_servfail = 0;
|
|
|
|
this.num_timeouts = 0;
|
|
|
|
this.send_query_recvcnt = 0;
|
|
|
|
this.send_query_sendcnt = 0;
|
|
|
|
this.num_sent = 0;
|
|
|
|
this.num_recv = 0;
|
|
|
|
this.num_frags_sent = 0;
|
|
|
|
this.num_frags_recv = 0;
|
|
|
|
this.num_pings = 0;
|
2015-09-28 05:07:00 +00:00
|
|
|
|
2015-10-17 14:28:10 +00:00
|
|
|
sent_since_report = 0;
|
|
|
|
recv_since_report = 0;
|
|
|
|
|
2015-11-10 12:54:01 +00:00
|
|
|
use_min_send = 0;
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.debug >= 5)
|
|
|
|
window_debug = this.debug - 3;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
while (this.running) {
|
2015-11-10 12:54:01 +00:00
|
|
|
if (!use_min_send)
|
2016-01-09 07:04:39 +00:00
|
|
|
tv = ms_to_timeval(this.max_timeout_ms);
|
2015-09-28 05:07:00 +00:00
|
|
|
|
2015-10-04 14:13:47 +00:00
|
|
|
/* TODO: detect DNS servers which drop frequent requests
|
2015-09-28 05:07:00 +00:00
|
|
|
* TODO: adjust number of pending queries based on current data rate */
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.conn == CONN_DNS_NULL && !use_min_send) {
|
2015-11-10 12:54:01 +00:00
|
|
|
|
|
|
|
/* Send a single query per loop */
|
2016-01-09 07:04:39 +00:00
|
|
|
sending = window_sending(this.outbuf, &nextresend);
|
2015-10-27 10:33:44 +00:00
|
|
|
total = sending;
|
|
|
|
check_pending_queries();
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.num_pending < this.windowsize_down && this.lazymode)
|
|
|
|
total = MAX(total, this.windowsize_down - this.num_pending);
|
|
|
|
else if (this.num_pending < 1 && !this.lazymode)
|
2015-10-27 10:33:44 +00:00
|
|
|
total = MAX(total, 1);
|
|
|
|
|
2015-11-10 12:54:01 +00:00
|
|
|
/* Upstream traffic - this is where all ping/data queries are sent */
|
2016-01-09 07:04:39 +00:00
|
|
|
if (sending > 0 || total > 0 || this.next_downstream_ack >= 0) {
|
2015-10-27 10:33:44 +00:00
|
|
|
|
|
|
|
if (sending > 0) {
|
|
|
|
/* More to send - next fragment */
|
2016-01-09 11:29:17 +00:00
|
|
|
send_next_frag();
|
2015-10-27 10:33:44 +00:00
|
|
|
} else {
|
|
|
|
/* Send ping if we didn't send anything yet */
|
2016-01-09 11:29:17 +00:00
|
|
|
send_ping(0, this.next_downstream_ack, (this.num_pings > 20 && this.num_pings % 50 == 0));
|
2016-01-09 07:04:39 +00:00
|
|
|
this.next_downstream_ack = -1;
|
2015-10-27 10:33:44 +00:00
|
|
|
}
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2015-10-27 10:33:44 +00:00
|
|
|
sending--;
|
|
|
|
total--;
|
|
|
|
QTRACK_DEBUG(3, "Sent a query to fill server lazy buffer to %lu, will send another %d",
|
2016-01-09 07:04:39 +00:00
|
|
|
this.lazymode ? this.windowsize_down : 1, total);
|
2015-10-27 10:33:44 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (sending > 0 || (total > 0 && this.lazymode)) {
|
2015-11-10 12:54:01 +00:00
|
|
|
/* If sending any data fragments, or server has too few
|
|
|
|
* pending queries, send another one after min. interval */
|
2015-10-27 10:33:44 +00:00
|
|
|
/* TODO: enforce min send interval even if we get new data */
|
2016-01-09 07:04:39 +00:00
|
|
|
tv = ms_to_timeval(this.min_send_interval_ms);
|
|
|
|
if (this.min_send_interval_ms)
|
2015-11-10 12:54:01 +00:00
|
|
|
use_min_send = 1;
|
2015-10-27 10:33:44 +00:00
|
|
|
tv.tv_usec += 1;
|
2016-01-09 07:04:39 +00:00
|
|
|
} else if (total > 0 && !this.lazymode) {
|
2015-11-10 12:54:01 +00:00
|
|
|
/* In immediate mode, use normal interval when needing
|
|
|
|
* to send non-data queries to probe server. */
|
2016-01-09 07:04:39 +00:00
|
|
|
tv = ms_to_timeval(this.send_interval_ms);
|
2015-10-27 10:33:44 +00:00
|
|
|
}
|
2015-10-17 14:28:10 +00:00
|
|
|
|
2015-11-10 12:54:01 +00:00
|
|
|
if (sending == 0 && !use_min_send) {
|
|
|
|
/* check next resend time when not sending any data */
|
|
|
|
if (timercmp(&nextresend, &tv, <))
|
|
|
|
tv = nextresend;
|
|
|
|
}
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
this.send_ping_soon = 0;
|
2015-10-27 10:33:44 +00:00
|
|
|
}
|
2015-08-29 12:11:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.stats) {
|
|
|
|
if (difftime(time(NULL), last_stats) >= this.stats) {
|
2015-10-04 14:13:47 +00:00
|
|
|
/* print useful statistics report */
|
2016-01-09 07:04:39 +00:00
|
|
|
fprintf(stderr, "\n============ iodine connection statistics (user %1d) ============\n", this.userid);
|
2015-10-17 14:28:10 +00:00
|
|
|
fprintf(stderr, " Queries sent: %8lu" ", answered: %8lu" ", SERVFAILs: %4lu\n",
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_sent, this.num_recv, this.num_servfail);
|
2015-10-17 14:28:10 +00:00
|
|
|
fprintf(stderr, " last %3d secs: %7lu" " (%4lu/s), replies: %7lu" " (%4lu/s)\n",
|
2016-01-09 07:04:39 +00:00
|
|
|
this.stats, this.num_sent - sent_since_report, (this.num_sent - sent_since_report) / this.stats,
|
|
|
|
this.num_recv - recv_since_report, (this.num_recv - recv_since_report) / this.stats);
|
2015-10-17 14:28:10 +00:00
|
|
|
fprintf(stderr, " num IP rejected: %4lu, untracked: %4lu, lazy mode: %1d\n",
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_badip, this.num_untracked, this.lazymode);
|
2015-10-27 09:25:56 +00:00
|
|
|
fprintf(stderr, " Min send: %5ld ms, Avg RTT: %5ld ms Timeout server: %4ld ms\n",
|
2016-01-09 07:04:39 +00:00
|
|
|
this.min_send_interval_ms, this.rtt_total_ms / this.num_immediate, this.server_timeout_ms);
|
2015-10-27 09:25:56 +00:00
|
|
|
fprintf(stderr, " Queries immediate: %5lu, timed out: %4lu target: %4ld ms\n",
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_immediate, this.num_timeouts, this.max_timeout_ms);
|
|
|
|
if (this.conn == CONN_DNS_NULL) {
|
2015-10-27 10:33:44 +00:00
|
|
|
fprintf(stderr, " Frags resent: %4u, OOS: %4u down frag: %4ld ms\n",
|
2016-01-09 07:04:39 +00:00
|
|
|
this.outbuf->resends, this.inbuf->oos, this.downstream_timeout_ms);
|
2015-10-27 10:33:44 +00:00
|
|
|
fprintf(stderr, " TX fragments: %8lu" ", RX: %8lu" ", pings: %8lu" "\n\n",
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_frags_sent, this.num_frags_recv, this.num_pings);
|
2015-10-27 10:33:44 +00:00
|
|
|
}
|
2016-01-09 07:04:39 +00:00
|
|
|
/* update since-last-report this.stats */
|
|
|
|
sent_since_report = this.num_sent;
|
|
|
|
recv_since_report = this.num_recv;
|
2015-10-04 14:13:47 +00:00
|
|
|
last_stats = time(NULL);
|
2015-10-17 14:28:10 +00:00
|
|
|
|
2015-10-04 14:13:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.send_ping_soon && !use_min_send) {
|
2009-09-20 21:10:44 +00:00
|
|
|
tv.tv_sec = 0;
|
2016-01-09 07:04:39 +00:00
|
|
|
tv.tv_usec = this.send_ping_soon * 1000;
|
|
|
|
this.send_ping_soon = 0;
|
2009-09-20 21:10:44 +00:00
|
|
|
}
|
2009-08-16 12:30:26 +00:00
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.conn != CONN_DNS_NULL || window_buffer_available(this.outbuf) > 16) {
|
2015-10-27 10:33:44 +00:00
|
|
|
/* Fill up outgoing buffer with available data if it has enough space
|
2015-08-29 12:11:21 +00:00
|
|
|
* The windowing protocol manages data retransmits, timeouts etc. */
|
2016-01-09 11:29:17 +00:00
|
|
|
FD_SET(this.tun_fd, &fds);
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
2016-01-09 11:29:17 +00:00
|
|
|
FD_SET(this.dns_fd, &fds);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2015-11-10 12:54:01 +00:00
|
|
|
DEBUG(4, "Waiting %ld ms before sending more... (min_send %d)", timeval_to_ms(&tv), use_min_send);
|
|
|
|
|
|
|
|
if (use_min_send) {
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
}
|
2015-10-17 14:28:10 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
i = select(MAX(this.tun_fd, this.dns_fd) + 1, &fds, NULL, NULL, &tv);
|
2009-09-20 21:10:44 +00:00
|
|
|
|
2015-11-10 12:54:01 +00:00
|
|
|
if (use_min_send && i > 0) {
|
|
|
|
/* enforce min_send_interval if we get interrupted by new tun data */
|
|
|
|
gettimeofday(&now2, NULL);
|
|
|
|
timersub(&now2, &now, &tmp);
|
|
|
|
timersub(&tv, &tmp, &now);
|
|
|
|
tv = now;
|
|
|
|
} else {
|
|
|
|
use_min_send = 0;
|
|
|
|
}
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (difftime(time(NULL), this.lastdownstreamtime) > 60) {
|
2015-10-04 14:13:47 +00:00
|
|
|
fprintf(stderr, "No downstream data received in 60 seconds, shutting down.\n");
|
2016-01-09 07:04:39 +00:00
|
|
|
this.running = 0;
|
2009-09-20 21:10:44 +00:00
|
|
|
}
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.running == 0)
|
2009-08-16 12:30:26 +00:00
|
|
|
break;
|
|
|
|
|
2014-06-01 06:34:18 +00:00
|
|
|
if (i < 0)
|
2015-08-21 03:08:47 +00:00
|
|
|
err(1, "select < 0");
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2009-09-20 21:10:44 +00:00
|
|
|
if (i == 0) {
|
2015-11-10 12:54:01 +00:00
|
|
|
/* timed out - no new packets recv'd */
|
2009-08-16 12:30:26 +00:00
|
|
|
} else {
|
2016-01-09 11:29:17 +00:00
|
|
|
if (FD_ISSET(this.tun_fd, &fds)) {
|
|
|
|
if (tunnel_tun() <= 0)
|
2009-08-16 12:30:26 +00:00
|
|
|
continue;
|
2009-09-20 21:10:44 +00:00
|
|
|
/* Returns -1 on error OR when quickly
|
|
|
|
dropping data in case of DNS congestion;
|
|
|
|
we need to _not_ do tunnel_dns() then.
|
2016-01-09 07:04:39 +00:00
|
|
|
If chunk sent, sets this.send_ping_soon=0. */
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
2015-11-10 12:54:01 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
if (FD_ISSET(this.dns_fd, &fds)) {
|
|
|
|
tunnel_dns();
|
2014-06-01 06:34:18 +00:00
|
|
|
}
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
send_login(char *login, int len)
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2015-08-29 12:11:21 +00:00
|
|
|
uint8_t data[19];
|
2009-08-16 12:30:26 +00:00
|
|
|
|
|
|
|
memset(data, 0, sizeof(data));
|
2016-01-09 07:04:39 +00:00
|
|
|
data[0] = this.userid;
|
2009-08-16 12:30:26 +00:00
|
|
|
memcpy(&data[1], login, MIN(len, 16));
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
data[17] = (this.rand_seed >> 8) & 0xff;
|
|
|
|
data[18] = (this.rand_seed >> 0) & 0xff;
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
this.rand_seed++;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_packet('l', data, sizeof(data));
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
send_fragsize_probe(uint16_t fragsize)
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2015-08-29 12:11:21 +00:00
|
|
|
uint8_t probedata[256];
|
|
|
|
uint8_t buf[MAX_FRAGSIZE];
|
2015-09-28 05:07:00 +00:00
|
|
|
uint8_t hdr[3];
|
|
|
|
size_t hdr_len_enc = 6;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2015-09-28 05:07:00 +00:00
|
|
|
buf[0] = 'r'; /* Probe downstream fragsize packet */
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
hdr[0] = this.userid;
|
2015-09-28 05:07:00 +00:00
|
|
|
*(uint16_t *) (hdr + 1) = htons(fragsize);
|
|
|
|
|
|
|
|
b32->encode(buf + 1, &hdr_len_enc, hdr, 3);
|
|
|
|
/* build a large query domain which is random and maximum size,
|
|
|
|
* will also take up maximum space in the return packet */
|
2016-01-09 07:04:39 +00:00
|
|
|
memset(probedata, MAX(1, this.rand_seed & 0xff), sizeof(probedata));
|
|
|
|
probedata[1] = MAX(1, (this.rand_seed >> 8) & 0xff);
|
|
|
|
this.rand_seed++;
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
/* Note: must either be same, or larger, than send_chunk() */
|
2016-01-09 07:04:39 +00:00
|
|
|
build_hostname(buf, sizeof(buf), probedata, sizeof(probedata), this.topdomain,
|
|
|
|
this.dataenc, this.hostname_maxlen, 6);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_query(buf);
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
send_set_downstream_fragsize(uint16_t fragsize)
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2015-08-29 12:11:21 +00:00
|
|
|
uint8_t data[5];
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
data[0] = this.userid;
|
2015-09-28 05:07:00 +00:00
|
|
|
*(uint16_t *) (data + 1) = htons(fragsize);
|
2016-01-09 07:04:39 +00:00
|
|
|
data[3] = (this.rand_seed >> 8) & 0xff;
|
|
|
|
data[4] = (this.rand_seed >> 0) & 0xff;
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
this.rand_seed++;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_packet('n', data, sizeof(data));
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 06:34:18 +00:00
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
send_version(uint32_t version)
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2015-08-29 12:11:21 +00:00
|
|
|
uint8_t data[6];
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2015-09-28 05:07:00 +00:00
|
|
|
version = htonl(version);
|
|
|
|
*(uint32_t *) data = version;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
data[4] = (this.rand_seed >> 8) & 0xff;
|
|
|
|
data[5] = (this.rand_seed >> 0) & 0xff;
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
this.rand_seed++;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_packet('v', data, sizeof(data));
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
send_ip_request()
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2015-08-29 12:11:21 +00:00
|
|
|
uint8_t buf[512] = "i____.";
|
2016-01-09 07:04:39 +00:00
|
|
|
buf[1] = b32_5to8(this.userid);
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
buf[2] = b32_5to8((this.rand_seed >> 10) & 0x1f);
|
|
|
|
buf[3] = b32_5to8((this.rand_seed >> 5) & 0x1f);
|
|
|
|
buf[4] = b32_5to8((this.rand_seed ) & 0x1f);
|
|
|
|
this.rand_seed++;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
strncat((char *)buf, this.topdomain, 512 - strlen((char *)buf));
|
2016-01-09 11:29:17 +00:00
|
|
|
send_query(buf);
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
send_raw_udp_login(int seed)
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
|
|
|
char buf[16];
|
2016-01-09 07:04:39 +00:00
|
|
|
login_calculate(buf, 16, this.password, seed + 1);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_raw((uint8_t *) buf, sizeof(buf), RAW_HDR_CMD_LOGIN);
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
send_upenctest(char *s)
|
2009-12-29 20:00:57 +00:00
|
|
|
/* NOTE: String may be at most 63-4=59 chars to fit in 1 dns chunk. */
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2009-12-29 20:00:57 +00:00
|
|
|
char buf[512] = "z___";
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
buf[1] = b32_5to8((this.rand_seed >> 10) & 0x1f);
|
|
|
|
buf[2] = b32_5to8((this.rand_seed >> 5) & 0x1f);
|
|
|
|
buf[3] = b32_5to8((this.rand_seed ) & 0x1f);
|
|
|
|
this.rand_seed++;
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2015-10-27 09:25:56 +00:00
|
|
|
strncat(buf, s, 512 - strlen(buf));
|
|
|
|
strncat(buf, ".", 512 - strlen(buf));
|
2016-01-09 07:04:39 +00:00
|
|
|
strncat(buf, this.topdomain, 512 - strlen(buf));
|
2016-01-09 11:29:17 +00:00
|
|
|
send_query((uint8_t *)buf);
|
2009-12-29 20:00:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
send_downenctest(char downenc, int variant, char *s, int slen)
|
2009-12-29 20:00:57 +00:00
|
|
|
/* Note: content/handling of s is not defined yet. */
|
|
|
|
{
|
|
|
|
char buf[512] = "y_____.";
|
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
buf[1] = tolower(downenc);
|
2009-12-29 20:00:57 +00:00
|
|
|
buf[2] = b32_5to8(variant);
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
buf[3] = b32_5to8((this.rand_seed >> 10) & 0x1f);
|
|
|
|
buf[4] = b32_5to8((this.rand_seed >> 5) & 0x1f);
|
|
|
|
buf[5] = b32_5to8((this.rand_seed ) & 0x1f);
|
|
|
|
this.rand_seed++;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
strncat(buf, this.topdomain, 512 - strlen(buf));
|
2016-01-09 11:29:17 +00:00
|
|
|
send_query((uint8_t *)buf);
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
send_codec_switch(int bits)
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2009-09-20 15:11:14 +00:00
|
|
|
char buf[512] = "s_____.";
|
2016-01-09 07:04:39 +00:00
|
|
|
buf[1] = b32_5to8(this.userid);
|
2009-08-16 12:30:26 +00:00
|
|
|
buf[2] = b32_5to8(bits);
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
buf[3] = b32_5to8((this.rand_seed >> 10) & 0x1f);
|
|
|
|
buf[4] = b32_5to8((this.rand_seed >> 5) & 0x1f);
|
|
|
|
buf[5] = b32_5to8((this.rand_seed ) & 0x1f);
|
|
|
|
this.rand_seed++;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
strncat(buf, this.topdomain, 512 - strlen(buf));
|
2016-01-09 11:29:17 +00:00
|
|
|
send_query((uint8_t *)buf);
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
2009-09-20 15:11:14 +00:00
|
|
|
|
2015-09-28 05:07:00 +00:00
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
send_server_options(int lazy, int compression, char denc, char *options)
|
2015-09-28 12:06:23 +00:00
|
|
|
/* Options must be length >=4 */
|
2015-09-28 05:07:00 +00:00
|
|
|
{
|
2015-09-28 12:06:23 +00:00
|
|
|
char buf[512] = "oU3___CMC.";
|
2016-01-09 07:04:39 +00:00
|
|
|
buf[1] = b32_5to8(this.userid);
|
2015-09-28 05:07:00 +00:00
|
|
|
|
2015-09-28 12:06:23 +00:00
|
|
|
options[0] = tolower(denc);
|
|
|
|
options[1] = lazy ? 'l' : 'i';
|
|
|
|
options[2] = compression ? 'c' : 'd';
|
|
|
|
options[3] = 0;
|
|
|
|
strncpy(buf + 3, options, 3);
|
2009-09-20 15:11:14 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
buf[6] = b32_5to8((this.rand_seed >> 10) & 0x1f);
|
|
|
|
buf[7] = b32_5to8((this.rand_seed >> 5) & 0x1f);
|
|
|
|
buf[8] = b32_5to8((this.rand_seed) & 0x1f);
|
|
|
|
this.rand_seed++;
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
strncat(buf, this.topdomain, 512 - strlen(buf));
|
2016-01-09 11:29:17 +00:00
|
|
|
send_query((uint8_t *)buf);
|
2009-09-20 21:10:44 +00:00
|
|
|
}
|
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_version(int *seed)
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2009-09-20 21:10:44 +00:00
|
|
|
char hex[] = "0123456789abcdef";
|
|
|
|
char hex2[] = "0123456789ABCDEF";
|
2009-08-16 12:30:26 +00:00
|
|
|
char in[4096];
|
|
|
|
uint32_t payload;
|
|
|
|
int i;
|
|
|
|
int read;
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
for (i = 0; this.running && i < 5; i++) {
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_version(PROTOCOL_VERSION);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
read = handshake_waitdns(in, sizeof(in), 'V', i+1);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2012-02-06 19:28:42 +00:00
|
|
|
if (read >= 9) {
|
|
|
|
payload = (((in[4] & 0xff) << 24) |
|
|
|
|
((in[5] & 0xff) << 16) |
|
|
|
|
((in[6] & 0xff) << 8) |
|
|
|
|
((in[7] & 0xff)));
|
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
if (strncmp("VACK", (char *)in, 4) == 0) {
|
2012-02-06 19:28:42 +00:00
|
|
|
*seed = payload;
|
2016-01-09 07:04:39 +00:00
|
|
|
this.userid = in[8];
|
|
|
|
this.userid_char = hex[this.userid & 15];
|
|
|
|
this.userid_char2 = hex2[this.userid & 15];
|
2012-02-06 19:28:42 +00:00
|
|
|
|
2014-04-24 14:03:47 +00:00
|
|
|
fprintf(stderr, "Version ok, both using protocol v 0x%08x. You are user #%d\n",
|
2016-01-09 07:04:39 +00:00
|
|
|
PROTOCOL_VERSION, this.userid);
|
2012-02-06 19:28:42 +00:00
|
|
|
return 0;
|
2015-08-29 12:11:21 +00:00
|
|
|
} else if (strncmp("VNAK", (char *)in, 4) == 0) {
|
2014-06-01 06:34:18 +00:00
|
|
|
warnx("You use protocol v 0x%08x, server uses v 0x%08x. Giving up",
|
2014-04-24 14:03:47 +00:00
|
|
|
PROTOCOL_VERSION, payload);
|
2012-02-06 19:28:42 +00:00
|
|
|
return 1;
|
2015-08-29 12:11:21 +00:00
|
|
|
} else if (strncmp("VFUL", (char *)in, 4) == 0) {
|
2012-02-06 19:28:42 +00:00
|
|
|
warnx("Server full, all %d slots are taken. Try again later", payload);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else if (read > 0)
|
|
|
|
warnx("did not receive proper login challenge");
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
fprintf(stderr, "Retrying version check...\n");
|
|
|
|
}
|
2009-09-20 15:11:14 +00:00
|
|
|
warnx("couldn't connect to server (maybe other -T options will work)");
|
2009-08-16 12:30:26 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_login(int seed)
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
|
|
|
char in[4096];
|
|
|
|
char login[16];
|
|
|
|
char server[65];
|
|
|
|
char client[65];
|
|
|
|
int mtu;
|
|
|
|
int i;
|
|
|
|
int read;
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
login_calculate(login, 16, this.password, seed);
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
for (i=0; this.running && i<5 ;i++) {
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_login(login, 16);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
read = handshake_waitdns(in, sizeof(in), 'L', i+1);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2012-02-06 19:28:42 +00:00
|
|
|
if (read > 0) {
|
|
|
|
int netmask;
|
|
|
|
if (strncmp("LNAK", in, 4) == 0) {
|
2016-01-09 07:04:39 +00:00
|
|
|
fprintf(stderr, "Bad this.password\n");
|
2012-02-06 19:28:42 +00:00
|
|
|
return 1;
|
|
|
|
} else if (sscanf(in, "%64[^-]-%64[^-]-%d-%d",
|
|
|
|
server, client, &mtu, &netmask) == 4) {
|
|
|
|
|
|
|
|
server[64] = 0;
|
|
|
|
client[64] = 0;
|
|
|
|
if (tun_setip(client, server, netmask) == 0 &&
|
|
|
|
tun_setmtu(mtu) == 0) {
|
|
|
|
|
|
|
|
fprintf(stderr, "Server tunnel IP is %s\n", server);
|
|
|
|
return 0;
|
2009-08-16 12:30:26 +00:00
|
|
|
} else {
|
2012-02-06 19:28:42 +00:00
|
|
|
errx(4, "Failed to set IP and MTU");
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
2012-02-06 19:28:42 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Received bad handshake\n");
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
2012-02-06 19:28:42 +00:00
|
|
|
}
|
2009-08-16 12:30:26 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "Retrying login...\n");
|
|
|
|
}
|
|
|
|
warnx("couldn't login to server");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_raw_udp(int seed)
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
char in[4096];
|
|
|
|
fd_set fds;
|
|
|
|
int i;
|
|
|
|
int r;
|
|
|
|
int len;
|
2015-06-28 20:41:54 +00:00
|
|
|
int got_addr;
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
memset(&this.raw_serv, 0, sizeof(this.raw_serv));
|
2015-06-28 20:41:54 +00:00
|
|
|
got_addr = 0;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
fprintf(stderr, "Testing raw UDP data to the server (skip with -r)");
|
2016-01-09 07:04:39 +00:00
|
|
|
for (i=0; this.running && i<3 ;i++) {
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_ip_request();
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
len = handshake_waitdns(in, sizeof(in), 'I', i+1);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2012-02-06 19:28:42 +00:00
|
|
|
if (len == 5 && in[0] == 'I') {
|
2015-06-28 20:41:54 +00:00
|
|
|
/* Received IPv4 address */
|
2016-01-09 07:04:39 +00:00
|
|
|
struct sockaddr_in *raw4_serv = (struct sockaddr_in *) &this.raw_serv;
|
2015-06-28 20:41:54 +00:00
|
|
|
raw4_serv->sin_family = AF_INET;
|
|
|
|
memcpy(&raw4_serv->sin_addr, &in[1], sizeof(struct in_addr));
|
|
|
|
raw4_serv->sin_port = htons(53);
|
2016-01-09 07:04:39 +00:00
|
|
|
this.raw_serv_len = sizeof(struct sockaddr_in);
|
2015-06-28 20:41:54 +00:00
|
|
|
got_addr = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (len == 17 && in[0] == 'I') {
|
|
|
|
/* Received IPv6 address */
|
2016-01-09 07:04:39 +00:00
|
|
|
struct sockaddr_in6 *raw6_serv = (struct sockaddr_in6 *) &this.raw_serv;
|
2015-06-28 20:41:54 +00:00
|
|
|
raw6_serv->sin6_family = AF_INET6;
|
|
|
|
memcpy(&raw6_serv->sin6_addr, &in[1], sizeof(struct in6_addr));
|
|
|
|
raw6_serv->sin6_port = htons(53);
|
2016-01-09 07:04:39 +00:00
|
|
|
this.raw_serv_len = sizeof(struct sockaddr_in6);
|
2015-06-28 20:41:54 +00:00
|
|
|
got_addr = 1;
|
2012-02-06 19:28:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
fprintf(stderr, ".");
|
|
|
|
fflush(stderr);
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
2009-12-29 20:00:57 +00:00
|
|
|
fprintf(stderr, "\n");
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.running)
|
2009-12-29 20:00:57 +00:00
|
|
|
return 0;
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2015-06-28 20:41:54 +00:00
|
|
|
if (!got_addr) {
|
2009-08-16 12:30:26 +00:00
|
|
|
fprintf(stderr, "Failed to get raw server IP, will use DNS mode.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-09 07:04:39 +00:00
|
|
|
fprintf(stderr, "Server is at %s, trying raw login: ", format_addr(&this.raw_serv, this.raw_serv_len));
|
2009-08-16 12:30:26 +00:00
|
|
|
fflush(stderr);
|
|
|
|
|
2014-06-01 06:34:18 +00:00
|
|
|
/* do login against port 53 on remote server
|
2009-08-16 12:30:26 +00:00
|
|
|
* based on the old seed. If reply received,
|
|
|
|
* switch to raw udp mode */
|
2016-01-09 07:04:39 +00:00
|
|
|
for (i=0; this.running && i<4 ;i++) {
|
2009-08-16 12:30:26 +00:00
|
|
|
tv.tv_sec = i + 1;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_raw_udp_login(seed);
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
FD_ZERO(&fds);
|
2016-01-09 11:29:17 +00:00
|
|
|
FD_SET(this.dns_fd, &fds);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
r = select(this.dns_fd + 1, &fds, NULL, NULL, &tv);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
|
|
|
if(r > 0) {
|
|
|
|
/* recv() needed for windows, dont change to read() */
|
2016-01-09 11:29:17 +00:00
|
|
|
len = recv(this.dns_fd, in, sizeof(in), 0);
|
2009-08-16 12:30:26 +00:00
|
|
|
if (len >= (16 + RAW_HDR_LEN)) {
|
|
|
|
char hash[16];
|
2016-01-09 07:04:39 +00:00
|
|
|
login_calculate(hash, 16, this.password, seed - 1);
|
2009-08-16 12:30:26 +00:00
|
|
|
if (memcmp(in, raw_header, RAW_HDR_IDENT_LEN) == 0
|
2014-06-01 06:34:18 +00:00
|
|
|
&& RAW_HDR_GET_CMD(in) == RAW_HDR_CMD_LOGIN
|
2009-08-16 12:30:26 +00:00
|
|
|
&& memcmp(&in[RAW_HDR_LEN], hash, sizeof(hash)) == 0) {
|
|
|
|
|
|
|
|
fprintf(stderr, "OK\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, ".");
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
fprintf(stderr, "failed\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_upenctest(char *s)
|
2009-12-29 20:00:57 +00:00
|
|
|
/* NOTE: *s may be max 59 chars; must start with "aA" for case-swap check
|
|
|
|
Returns:
|
|
|
|
-1: case swap, no need for any further test: error printed; or Ctrl-C
|
|
|
|
0: not identical or error or timeout
|
|
|
|
1: identical string returned
|
|
|
|
*/
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
|
|
|
char in[4096];
|
2009-12-29 20:00:57 +00:00
|
|
|
unsigned char *uin = (unsigned char *) in;
|
|
|
|
unsigned char *us = (unsigned char *) s;
|
2009-08-16 12:30:26 +00:00
|
|
|
int i;
|
|
|
|
int read;
|
2009-12-29 20:00:57 +00:00
|
|
|
int slen;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
slen = strlen(s);
|
2016-01-09 07:04:39 +00:00
|
|
|
for (i=0; this.running && i<3 ;i++) {
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_upenctest(s);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
read = handshake_waitdns(in, sizeof(in), 'Z', i+1);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
if (read == -2)
|
|
|
|
return 0; /* hard error */
|
|
|
|
|
|
|
|
if (read > 0 && read < slen + 4)
|
|
|
|
return 0; /* reply too short (chars dropped) */
|
|
|
|
|
|
|
|
if (read > 0) {
|
|
|
|
int k;
|
|
|
|
/* quick check if case swapped, to give informative error msg */
|
|
|
|
if (in[4] == 'A') {
|
|
|
|
fprintf(stderr, "DNS queries get changed to uppercase, keeping upstream codec Base32\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (in[5] == 'a') {
|
|
|
|
fprintf(stderr, "DNS queries get changed to lowercase, keeping upstream codec Base32\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (k = 0; k < slen; k++) {
|
|
|
|
if (in[k+4] != s[k]) {
|
|
|
|
/* Definitely not reliable */
|
|
|
|
if (in[k+4] >= ' ' && in[k+4] <= '~' &&
|
|
|
|
s[k] >= ' ' && s[k] <= '~') {
|
|
|
|
fprintf(stderr, "DNS query char '%c' gets changed into '%c'\n",
|
|
|
|
s[k], in[k+4]);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "DNS query char 0x%02X gets changed into 0x%02X\n",
|
|
|
|
(unsigned int) us[k],
|
|
|
|
(unsigned int) uin[k+4]);
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
2009-12-29 20:00:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* if still here, then all okay */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Retrying upstream codec test...\n");
|
|
|
|
}
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.running)
|
2009-12-29 20:00:57 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* timeout */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_upenc_autodetect()
|
2009-12-29 20:00:57 +00:00
|
|
|
/* Returns:
|
|
|
|
0: keep Base32
|
|
|
|
1: Base64 is okay
|
|
|
|
2: Base64u is okay
|
|
|
|
3: Base128 is okay
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
/* Note: max 59 chars, must start with "aA".
|
|
|
|
pat64: If 0129 work, assume 3-8 are okay too.
|
|
|
|
|
|
|
|
RFC1035 par 2.3.1 states that [A-Z0-9-] allowed, but only
|
|
|
|
[A-Z] as first, and [A-Z0-9] as last char _per label_.
|
|
|
|
Test by having '-' as last char.
|
|
|
|
*/
|
|
|
|
char *pat64="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ+0129-";
|
|
|
|
char *pat64u="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ_0129-";
|
|
|
|
char *pat128a="aA-Aaahhh-Drink-mal-ein-J\344germeister-";
|
|
|
|
char *pat128b="aA-La-fl\373te-na\357ve-fran\347aise-est-retir\351-\340-Cr\350te";
|
|
|
|
char *pat128c="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
|
|
|
|
char *pat128d="aA0123456789\274\275\276\277"
|
|
|
|
"\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317";
|
|
|
|
char *pat128e="aA"
|
|
|
|
"\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337"
|
|
|
|
"\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357"
|
|
|
|
"\360\361\362\363\364\365\366\367\370\371\372\373\374\375";
|
|
|
|
int res;
|
|
|
|
|
|
|
|
/* Try Base128, starting very gently to not draw attention */
|
|
|
|
while (1) {
|
2016-01-09 11:29:17 +00:00
|
|
|
res = handshake_upenctest(pat128a);
|
2009-12-29 20:00:57 +00:00
|
|
|
if (res < 0) {
|
|
|
|
/* DNS swaps case, msg already printed; or Ctrl-C */
|
|
|
|
return 0;
|
|
|
|
} else if (res == 0) {
|
|
|
|
/* Probably not okay, skip Base128 entirely */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
res = handshake_upenctest(pat128b);
|
2009-12-29 20:00:57 +00:00
|
|
|
if (res < 0)
|
|
|
|
return 0;
|
|
|
|
else if (res == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* if this works, we can test the real stuff */
|
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
res = handshake_upenctest(pat128c);
|
2009-12-29 20:00:57 +00:00
|
|
|
if (res < 0)
|
|
|
|
return 0;
|
|
|
|
else if (res == 0)
|
|
|
|
break;
|
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
res = handshake_upenctest(pat128d);
|
2009-12-29 20:00:57 +00:00
|
|
|
if (res < 0)
|
|
|
|
return 0;
|
|
|
|
else if (res == 0)
|
|
|
|
break;
|
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
res = handshake_upenctest(pat128e);
|
2009-12-29 20:00:57 +00:00
|
|
|
if (res < 0)
|
|
|
|
return 0;
|
|
|
|
else if (res == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* if still here, then base128 works completely */
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try Base64 (with plus sign) */
|
2016-01-09 11:29:17 +00:00
|
|
|
res = handshake_upenctest(pat64);
|
2009-12-29 20:00:57 +00:00
|
|
|
if (res < 0) {
|
|
|
|
/* DNS swaps case, msg already printed; or Ctrl-C */
|
|
|
|
return 0;
|
|
|
|
} else if (res > 0) {
|
|
|
|
/* All okay, Base64 msg will be printed later */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try Base64u (with _u_nderscore) */
|
2016-01-09 11:29:17 +00:00
|
|
|
res = handshake_upenctest(pat64u);
|
2009-12-29 20:00:57 +00:00
|
|
|
if (res < 0) {
|
|
|
|
/* DNS swaps case, msg already printed; or Ctrl-C */
|
|
|
|
return 0;
|
|
|
|
} else if (res > 0) {
|
|
|
|
/* All okay, Base64u msg will be printed later */
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if here, then nonthing worked */
|
|
|
|
fprintf(stderr, "Keeping upstream codec Base32\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_downenctest(char trycodec)
|
2009-12-29 20:00:57 +00:00
|
|
|
/* Returns:
|
|
|
|
0: not identical or error or timeout
|
|
|
|
1: identical string returned
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
char in[4096];
|
|
|
|
int i;
|
|
|
|
int read;
|
|
|
|
char *s = DOWNCODECCHECK1;
|
|
|
|
int slen = DOWNCODECCHECK1_LEN;
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
for (i=0; this.running && i<3 ;i++) {
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_downenctest(trycodec, 1, NULL, 0);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
read = handshake_waitdns(in, sizeof(in), 'Y', i+1);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
if (read == -2)
|
|
|
|
return 0; /* hard error */
|
|
|
|
|
|
|
|
if (read > 0 && read != slen)
|
|
|
|
return 0; /* reply incorrect = unreliable */
|
|
|
|
|
|
|
|
if (read > 0) {
|
|
|
|
int k;
|
|
|
|
for (k = 0; k < slen; k++) {
|
|
|
|
if (in[k] != s[k]) {
|
|
|
|
/* Definitely not reliable */
|
|
|
|
return 0;
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-29 20:00:57 +00:00
|
|
|
/* if still here, then all okay */
|
|
|
|
return 1;
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
fprintf(stderr, "Retrying downstream codec test...\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* timeout */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_downenc_autodetect()
|
2009-12-29 20:00:57 +00:00
|
|
|
/* Returns codec char (or ' ' if no advanced codec works) */
|
|
|
|
{
|
|
|
|
int base64ok = 0;
|
|
|
|
int base64uok = 0;
|
|
|
|
int base128ok = 0;
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.do_qtype == T_NULL || this.do_qtype == T_PRIVATE) {
|
2009-12-29 20:00:57 +00:00
|
|
|
/* no other choice than raw */
|
|
|
|
fprintf(stderr, "No alternative downstream codec available, using default (Raw)\n");
|
2015-09-28 05:07:00 +00:00
|
|
|
return 'R';
|
2009-12-29 20:00:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Autodetecting downstream codec (use -O to override)\n");
|
|
|
|
|
|
|
|
/* Try Base64 */
|
2016-01-09 11:29:17 +00:00
|
|
|
if (handshake_downenctest('S'))
|
2009-12-29 20:00:57 +00:00
|
|
|
base64ok = 1;
|
2016-01-09 11:29:17 +00:00
|
|
|
else if (this.running && handshake_downenctest('U'))
|
2009-12-29 20:00:57 +00:00
|
|
|
base64uok = 1;
|
|
|
|
|
|
|
|
/* Try Base128 only if 64 gives us some perspective */
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.running && (base64ok || base64uok)) {
|
2016-01-09 11:29:17 +00:00
|
|
|
if (handshake_downenctest('V'))
|
2009-12-29 20:00:57 +00:00
|
|
|
base128ok = 1;
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
/* If 128 works, then TXT may give us Raw as well */
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.running && (base128ok && this.do_qtype == T_TXT)) {
|
2016-01-09 11:29:17 +00:00
|
|
|
if (handshake_downenctest('R'))
|
2009-12-29 20:00:57 +00:00
|
|
|
return 'R';
|
|
|
|
}
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.running)
|
2009-12-29 20:00:57 +00:00
|
|
|
return ' ';
|
|
|
|
|
|
|
|
if (base128ok)
|
|
|
|
return 'V';
|
|
|
|
if (base64ok)
|
|
|
|
return 'S';
|
|
|
|
if (base64uok)
|
|
|
|
return 'U';
|
|
|
|
|
|
|
|
fprintf(stderr, "No advanced downstream codecs seem to work, using default (Base32)\n");
|
|
|
|
return ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_qtypetest(int timeout)
|
2009-12-29 20:00:57 +00:00
|
|
|
/* Returns:
|
|
|
|
0: doesn't work with this timeout
|
|
|
|
1: works properly
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
char in[4096];
|
|
|
|
int read;
|
|
|
|
char *s = DOWNCODECCHECK1;
|
|
|
|
int slen = DOWNCODECCHECK1_LEN;
|
|
|
|
int trycodec;
|
|
|
|
int k;
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.do_qtype == T_NULL || this.do_qtype == T_PRIVATE)
|
2009-12-29 20:00:57 +00:00
|
|
|
trycodec = 'R';
|
|
|
|
else
|
|
|
|
trycodec = 'T';
|
|
|
|
|
|
|
|
/* We could use 'Z' bouncing here, but 'Y' also tests that 0-255
|
2014-06-09 18:05:29 +00:00
|
|
|
byte values can be returned, which is needed for NULL/PRIVATE
|
|
|
|
to work. */
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_downenctest(trycodec, 1, NULL, 0);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
read = handshake_waitdns(in, sizeof(in), 'Y', timeout);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
if (read != slen)
|
|
|
|
return 0; /* incorrect */
|
|
|
|
|
|
|
|
for (k = 0; k < slen; k++) {
|
|
|
|
if (in[k] != s[k]) {
|
|
|
|
/* corrupted */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if still here, then all okay */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
handshake_qtype_numcvt(int num)
|
|
|
|
{
|
|
|
|
switch (num) {
|
|
|
|
case 0: return T_NULL;
|
2014-06-09 18:05:29 +00:00
|
|
|
case 1: return T_PRIVATE;
|
|
|
|
case 2: return T_TXT;
|
|
|
|
case 3: return T_SRV;
|
|
|
|
case 4: return T_MX;
|
|
|
|
case 5: return T_CNAME;
|
|
|
|
case 6: return T_A;
|
2009-12-29 20:00:57 +00:00
|
|
|
}
|
|
|
|
return T_UNSET;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_qtype_autodetect()
|
2009-12-29 20:00:57 +00:00
|
|
|
/* Returns:
|
2016-01-09 07:04:39 +00:00
|
|
|
0: okay, this.do_qtype set
|
2009-12-29 20:00:57 +00:00
|
|
|
1: problem, program exit
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
int highestworking = 100;
|
|
|
|
int timeout;
|
|
|
|
int qtypenum;
|
|
|
|
|
|
|
|
fprintf(stderr, "Autodetecting DNS query type (use -T to override)");
|
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
/* Method: try all "interesting" qtypes with a 1-sec timeout, then try
|
|
|
|
all "still-interesting" qtypes with a 2-sec timeout, etc.
|
|
|
|
"Interesting" means: qtypes that (are expected to) have higher
|
|
|
|
bandwidth than what we know is working already (highestworking).
|
|
|
|
|
|
|
|
Note that DNS relays may not immediately resolve the first (NULL)
|
|
|
|
query in 1 sec, due to long recursive lookups, so we keep trying
|
|
|
|
to see if things will start working after a while.
|
|
|
|
*/
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
for (timeout = 1; this.running && timeout <= 3; timeout++) {
|
|
|
|
for (qtypenum = 0; this.running && qtypenum < highestworking; qtypenum++) {
|
|
|
|
this.do_qtype = handshake_qtype_numcvt(qtypenum);
|
|
|
|
if (this.do_qtype == T_UNSET)
|
2009-12-29 20:00:57 +00:00
|
|
|
break; /* this round finished */
|
|
|
|
|
|
|
|
fprintf(stderr, ".");
|
|
|
|
fflush(stderr);
|
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
if (handshake_qtypetest(timeout)) {
|
2009-12-29 20:00:57 +00:00
|
|
|
/* okay */
|
|
|
|
highestworking = qtypenum;
|
2015-10-04 14:39:40 +00:00
|
|
|
DEBUG(1, " Type %s timeout %d works", client_get_qtype(), timeout);
|
2009-12-29 20:00:57 +00:00
|
|
|
break;
|
|
|
|
/* try others with longer timeout */
|
|
|
|
}
|
|
|
|
/* else: try next qtype with same timeout */
|
|
|
|
}
|
|
|
|
if (highestworking == 0)
|
|
|
|
/* good, we have NULL; abort immediately */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.running) {
|
2009-12-29 20:00:57 +00:00
|
|
|
warnx("Stopped while autodetecting DNS query type (try setting manually with -T)");
|
|
|
|
return 1; /* problem */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* finished */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.do_qtype = handshake_qtype_numcvt(highestworking);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.do_qtype == T_UNSET) {
|
2009-12-29 20:00:57 +00:00
|
|
|
/* also catches highestworking still 100 */
|
2016-01-09 07:04:39 +00:00
|
|
|
warnx("No suitable DNS query type found. Are you this.connected to a network?");
|
2009-12-29 20:00:57 +00:00
|
|
|
warnx("If you expect very long roundtrip delays, use -T explicitly.");
|
|
|
|
warnx("(Also, connecting to an \"ancient\" version of iodined won't work.)");
|
|
|
|
return 1; /* problem */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "using qtype" message printed in handshake function */
|
|
|
|
return 0; /* okay */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_edns0_check()
|
2009-12-29 20:00:57 +00:00
|
|
|
/* Returns:
|
|
|
|
0: EDNS0 not supported; or Ctrl-C
|
|
|
|
1: EDNS0 works
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
char in[4096];
|
|
|
|
int i;
|
|
|
|
int read;
|
|
|
|
char *s = DOWNCODECCHECK1;
|
|
|
|
int slen = DOWNCODECCHECK1_LEN;
|
|
|
|
char trycodec;
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.do_qtype == T_NULL)
|
2009-12-29 20:00:57 +00:00
|
|
|
trycodec = 'R';
|
|
|
|
else
|
|
|
|
trycodec = 'T';
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
for (i=0; this.running && i<3 ;i++) {
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_downenctest(trycodec, 1, NULL, 0);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
read = handshake_waitdns(in, sizeof(in), 'Y', i+1);
|
2009-12-29 20:00:57 +00:00
|
|
|
|
|
|
|
if (read == -2)
|
|
|
|
return 0; /* hard error */
|
|
|
|
|
|
|
|
if (read > 0 && read != slen)
|
|
|
|
return 0; /* reply incorrect = unreliable */
|
|
|
|
|
|
|
|
if (read > 0) {
|
|
|
|
int k;
|
|
|
|
for (k = 0; k < slen; k++) {
|
|
|
|
if (in[k] != s[k]) {
|
|
|
|
/* Definitely not reliable */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* if still here, then all okay */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Retrying EDNS0 support test...\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* timeout or Ctrl-C */
|
|
|
|
return 0;
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_switch_codec(int bits)
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
|
|
|
char in[4096];
|
|
|
|
int i;
|
|
|
|
int read;
|
2009-12-29 20:00:57 +00:00
|
|
|
struct encoder *tempenc;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
if (bits == 5)
|
|
|
|
tempenc = get_base32_encoder();
|
|
|
|
else if (bits == 6)
|
|
|
|
tempenc = get_base64_encoder();
|
|
|
|
else if (bits == 26) /* "2nd" 6 bits per byte, with underscore */
|
|
|
|
tempenc = get_base64u_encoder();
|
|
|
|
else if (bits == 7)
|
|
|
|
tempenc = get_base128_encoder();
|
|
|
|
else return;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
fprintf(stderr, "Switching upstream to codec %s\n", tempenc->name);
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
for (i=0; this.running && i<5 ;i++) {
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_codec_switch(bits);
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
read = handshake_waitdns(in, sizeof(in), 'S', i+1);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2012-02-06 19:28:42 +00:00
|
|
|
if (read > 0) {
|
|
|
|
if (strncmp("BADLEN", in, 6) == 0) {
|
2015-08-29 12:11:21 +00:00
|
|
|
fprintf(stderr, "Server got bad message length.\n");
|
2012-02-06 19:28:42 +00:00
|
|
|
goto codec_revert;
|
|
|
|
} else if (strncmp("BADIP", in, 5) == 0) {
|
2015-08-29 12:11:21 +00:00
|
|
|
fprintf(stderr, "Server rejected sender IP address.\n");
|
2012-02-06 19:28:42 +00:00
|
|
|
goto codec_revert;
|
|
|
|
} else if (strncmp("BADCODEC", in, 8) == 0) {
|
2015-08-29 12:11:21 +00:00
|
|
|
fprintf(stderr, "Server rejected the selected codec.\n");
|
2012-02-06 19:28:42 +00:00
|
|
|
goto codec_revert;
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
2012-02-06 19:28:42 +00:00
|
|
|
in[read] = 0; /* zero terminate */
|
|
|
|
fprintf(stderr, "Server switched upstream to codec %s\n", in);
|
2016-01-09 07:04:39 +00:00
|
|
|
this.dataenc = tempenc;
|
2015-08-23 14:14:27 +00:00
|
|
|
|
|
|
|
/* Update outgoing buffer max (decoded) fragsize */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.maxfragsize_up = get_raw_length_from_dns(this.hostname_maxlen - UPSTREAM_HDR, this.dataenc, this.topdomain);
|
2012-02-06 19:28:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
fprintf(stderr, "Retrying codec switch...\n");
|
|
|
|
}
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.running)
|
2009-12-29 20:00:57 +00:00
|
|
|
return;
|
|
|
|
|
2015-08-29 12:11:21 +00:00
|
|
|
fprintf(stderr, "No reply from server on codec switch.\n");
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2014-06-01 06:34:18 +00:00
|
|
|
codec_revert:
|
2016-01-09 07:04:39 +00:00
|
|
|
fprintf(stderr, "Falling back to upstream codec %s\n", this.dataenc->name);
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 12:06:23 +00:00
|
|
|
void
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_switch_options(int lazy, int compression, char denc)
|
2015-09-28 05:07:00 +00:00
|
|
|
{
|
|
|
|
char in[4096];
|
|
|
|
int read;
|
2015-09-28 12:06:23 +00:00
|
|
|
char *dname, *comp_status, *lazy_status;
|
|
|
|
char opts[4];
|
2015-09-28 05:07:00 +00:00
|
|
|
|
2015-09-28 12:06:23 +00:00
|
|
|
comp_status = compression ? "enabled" : "disabled";
|
2009-09-20 15:11:14 +00:00
|
|
|
|
|
|
|
dname = "Base32";
|
2015-09-28 12:06:23 +00:00
|
|
|
if (denc == 'S')
|
2009-09-20 15:11:14 +00:00
|
|
|
dname = "Base64";
|
2015-09-28 12:06:23 +00:00
|
|
|
else if (denc == 'U')
|
2009-12-29 20:00:57 +00:00
|
|
|
dname = "Base64u";
|
2015-09-28 12:06:23 +00:00
|
|
|
else if (denc == 'V')
|
2009-12-29 20:00:57 +00:00
|
|
|
dname = "Base128";
|
2015-09-28 12:06:23 +00:00
|
|
|
else if (denc == 'R')
|
2009-09-20 15:11:14 +00:00
|
|
|
dname = "Raw";
|
|
|
|
|
2015-09-28 12:06:23 +00:00
|
|
|
lazy_status = lazy ? "lazy" : "immediate";
|
2009-09-20 15:11:14 +00:00
|
|
|
|
2015-09-28 12:06:23 +00:00
|
|
|
fprintf(stderr, "Switching server options: %s mode, downstream codec %s, compression %s...\n",
|
|
|
|
lazy_status, dname, comp_status);
|
2016-01-09 07:04:39 +00:00
|
|
|
for (int i = 0; this.running && i < 5; i++) {
|
2009-09-20 15:11:14 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_server_options(lazy, compression, denc, opts);
|
2015-09-28 12:06:23 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
read = handshake_waitdns(in, sizeof(in) - 1, 'O', i + 1);
|
2009-09-20 15:11:14 +00:00
|
|
|
|
2012-02-06 19:28:42 +00:00
|
|
|
if (read > 0) {
|
|
|
|
if (strncmp("BADLEN", in, 6) == 0) {
|
2015-08-29 12:11:21 +00:00
|
|
|
fprintf(stderr, "Server got bad message length.\n");
|
2015-09-28 12:06:23 +00:00
|
|
|
goto opt_revert;
|
2012-02-06 19:28:42 +00:00
|
|
|
} else if (strncmp("BADIP", in, 5) == 0) {
|
2015-08-29 12:11:21 +00:00
|
|
|
fprintf(stderr, "Server rejected sender IP address.\n");
|
2015-09-28 12:06:23 +00:00
|
|
|
goto opt_revert;
|
2012-02-06 19:28:42 +00:00
|
|
|
} else if (strncmp("BADCODEC", in, 8) == 0) {
|
2015-09-28 12:06:23 +00:00
|
|
|
fprintf(stderr, "Server rejected the selected options.\n");
|
|
|
|
goto opt_revert;
|
2009-09-20 15:11:14 +00:00
|
|
|
}
|
2015-09-28 12:06:23 +00:00
|
|
|
fprintf(stderr, "Switched server options successfully. (%s)\n", opts);
|
2016-01-09 07:04:39 +00:00
|
|
|
this.lazymode = lazy;
|
|
|
|
this.compression_down = compression;
|
|
|
|
this.downenc = denc;
|
2012-02-06 19:28:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2015-10-17 14:28:10 +00:00
|
|
|
fprintf(stderr, "Retrying options switch...\n");
|
2009-09-20 15:11:14 +00:00
|
|
|
}
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.running)
|
2009-12-29 20:00:57 +00:00
|
|
|
return;
|
|
|
|
|
2015-10-17 14:28:10 +00:00
|
|
|
fprintf(stderr, "No reply from server on options switch.\n");
|
2009-09-20 15:11:14 +00:00
|
|
|
|
2015-09-28 12:06:23 +00:00
|
|
|
opt_revert:
|
2016-01-09 07:04:39 +00:00
|
|
|
comp_status = this.compression_down ? "enabled" : "disabled";
|
|
|
|
lazy_status = this.lazymode ? "lazy" : "immediate";
|
2015-10-03 14:09:34 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "Falling back to previous configuration: downstream codec %s, %s mode, compression %s.\n",
|
2016-01-09 07:04:39 +00:00
|
|
|
this.dataenc->name, lazy_status, comp_status);
|
2009-09-20 21:10:44 +00:00
|
|
|
}
|
2009-09-20 15:11:14 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
fragsize_check(char *in, int read, int proposed_fragsize, int *max_fragsize)
|
|
|
|
/* Returns: 0: keep checking, 1: break loop (either okay or definitely wrong) */
|
|
|
|
{
|
|
|
|
int acked_fragsize = ((in[0] & 0xff) << 8) | (in[1] & 0xff);
|
2009-12-29 20:00:57 +00:00
|
|
|
int okay;
|
|
|
|
int i;
|
|
|
|
unsigned int v;
|
2009-09-20 15:11:14 +00:00
|
|
|
|
|
|
|
if (read >= 5 && strncmp("BADIP", in, 5) == 0) {
|
|
|
|
fprintf(stderr, "got BADIP (Try iodined -c)..\n");
|
|
|
|
fflush(stderr);
|
|
|
|
return 0; /* maybe temporary error */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acked_fragsize != proposed_fragsize) {
|
|
|
|
/*
|
|
|
|
* got ack for wrong fragsize, maybe late response for
|
|
|
|
* earlier query, or ack corrupted
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read != proposed_fragsize) {
|
|
|
|
/*
|
|
|
|
* correctly acked fragsize but read too little (or too
|
|
|
|
* much): this fragsize is definitely not reliable
|
|
|
|
*/
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* here: read == proposed_fragsize == acked_fragsize */
|
|
|
|
|
|
|
|
/* test: */
|
|
|
|
/* in[123] = 123; */
|
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
if ((in[2] & 0xff) != 107) {
|
2015-08-29 12:11:21 +00:00
|
|
|
warnx("\ncorruption at byte 2, this won't work. Try -O Base32, or other -T options.");
|
2009-12-29 20:00:57 +00:00
|
|
|
*max_fragsize = -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-09-20 15:11:14 +00:00
|
|
|
/* Check for corruption */
|
2009-12-29 20:00:57 +00:00
|
|
|
okay = 1;
|
|
|
|
v = in[3] & 0xff;
|
2009-09-20 15:11:14 +00:00
|
|
|
|
2012-02-06 19:28:42 +00:00
|
|
|
for (i = 3; i < read; i++, v = (v + 107) & 0xff)
|
|
|
|
if ((in[i] & 0xff) != v) {
|
|
|
|
okay = 0;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-20 15:11:14 +00:00
|
|
|
|
2012-02-06 19:28:42 +00:00
|
|
|
if (okay) {
|
|
|
|
fprintf(stderr, "%d ok.. ", acked_fragsize);
|
|
|
|
fflush(stderr);
|
|
|
|
*max_fragsize = acked_fragsize;
|
|
|
|
return 1;
|
|
|
|
} else {
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.downenc != ' ' && this.downenc != 'T') {
|
2012-02-06 19:28:42 +00:00
|
|
|
fprintf(stderr, "%d corrupted at %d.. (Try -O Base32)\n", acked_fragsize, i);
|
2009-09-20 15:11:14 +00:00
|
|
|
} else {
|
2012-02-06 19:28:42 +00:00
|
|
|
fprintf(stderr, "%d corrupted at %d.. ", acked_fragsize, i);
|
2009-09-20 15:11:14 +00:00
|
|
|
}
|
2012-02-06 19:28:42 +00:00
|
|
|
fflush(stderr);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-09-20 15:11:14 +00:00
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
/* notreached */
|
2009-09-20 15:11:14 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
static int
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_autoprobe_fragsize()
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
2015-08-29 12:11:21 +00:00
|
|
|
char in[MAX_FRAGSIZE];
|
2009-08-16 12:30:26 +00:00
|
|
|
int i;
|
|
|
|
int read;
|
|
|
|
int proposed_fragsize = 768;
|
|
|
|
int range = 768;
|
2009-09-20 15:11:14 +00:00
|
|
|
int max_fragsize;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
|
|
|
max_fragsize = 0;
|
2015-08-29 12:11:21 +00:00
|
|
|
fprintf(stderr, "Autoprobing max downstream fragment size... (skip with -m fragsize)");
|
2016-01-09 07:04:39 +00:00
|
|
|
while (this.running && range > 0 && (range >= 8 || max_fragsize < 300)) {
|
2009-09-20 15:11:14 +00:00
|
|
|
/* stop the slow probing early when we have enough bytes anyway */
|
2016-01-09 07:04:39 +00:00
|
|
|
for (i=0; this.running && i<3 ;i++) {
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_fragsize_probe(proposed_fragsize);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
read = handshake_waitdns(in, sizeof(in), 'R', 1);
|
2014-06-01 06:34:18 +00:00
|
|
|
|
2012-02-06 19:28:42 +00:00
|
|
|
if (read > 0) {
|
|
|
|
/* We got a reply */
|
|
|
|
if (fragsize_check(in, read, proposed_fragsize, &max_fragsize) == 1)
|
|
|
|
break;
|
|
|
|
}
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
fprintf(stderr, ".");
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
2009-12-29 20:00:57 +00:00
|
|
|
if (max_fragsize < 0)
|
|
|
|
break;
|
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
range >>= 1;
|
|
|
|
if (max_fragsize == proposed_fragsize) {
|
|
|
|
/* Try bigger */
|
|
|
|
proposed_fragsize += range;
|
|
|
|
} else {
|
|
|
|
/* Try smaller */
|
|
|
|
fprintf(stderr, "%d not ok.. ", proposed_fragsize);
|
|
|
|
fflush(stderr);
|
|
|
|
proposed_fragsize -= range;
|
|
|
|
}
|
|
|
|
}
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.running) {
|
2015-08-29 12:11:21 +00:00
|
|
|
warnx("\nstopped while autodetecting fragment size (Try setting manually with -m)");
|
2009-08-16 12:30:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-08-21 03:08:47 +00:00
|
|
|
if (max_fragsize <= 6) {
|
2009-12-29 20:00:57 +00:00
|
|
|
/* Tried all the way down to 2 and found no good size.
|
|
|
|
But we _did_ do all handshake before this, so there must
|
|
|
|
be some workable connection. */
|
2015-08-29 12:11:21 +00:00
|
|
|
warnx("\nfound no accepted fragment size.");
|
2009-12-29 20:00:57 +00:00
|
|
|
warnx("try setting -M to 200 or lower, or try other -T or -O options.");
|
2009-08-16 12:30:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-08-21 03:08:47 +00:00
|
|
|
/* data header adds 6 bytes */
|
|
|
|
fprintf(stderr, "will use %d-6=%d\n", max_fragsize, max_fragsize - 6);
|
2009-09-20 15:11:14 +00:00
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
/* need 1200 / 16frags = 75 bytes fragsize */
|
|
|
|
if (max_fragsize < 82) {
|
|
|
|
fprintf(stderr, "Note: this probably won't work well.\n");
|
|
|
|
fprintf(stderr, "Try setting -M to 200 or lower, or try other DNS types (-T option).\n");
|
|
|
|
} else if (max_fragsize < 202 &&
|
2016-01-09 07:04:39 +00:00
|
|
|
(this.do_qtype == T_NULL || this.do_qtype == T_PRIVATE || this.do_qtype == T_TXT ||
|
|
|
|
this.do_qtype == T_SRV || this.do_qtype == T_MX)) {
|
2009-12-29 20:00:57 +00:00
|
|
|
fprintf(stderr, "Note: this isn't very much.\n");
|
|
|
|
fprintf(stderr, "Try setting -M to 200 or lower, or try other DNS types (-T option).\n");
|
|
|
|
}
|
2009-09-20 15:11:14 +00:00
|
|
|
|
|
|
|
return max_fragsize - 2;
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_set_fragsize(int fragsize)
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
|
|
|
char in[4096];
|
|
|
|
int i;
|
|
|
|
int read;
|
|
|
|
|
|
|
|
fprintf(stderr, "Setting downstream fragment size to max %d...\n", fragsize);
|
2016-01-09 07:04:39 +00:00
|
|
|
for (i=0; this.running && i<5 ;i++) {
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
send_set_downstream_fragsize(fragsize);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
read = handshake_waitdns(in, sizeof(in), 'N', i+1);
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2012-02-06 19:28:42 +00:00
|
|
|
if (read > 0) {
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2012-02-06 19:28:42 +00:00
|
|
|
if (strncmp("BADFRAG", in, 7) == 0) {
|
2015-08-29 12:11:21 +00:00
|
|
|
fprintf(stderr, "Server rejected fragsize. Keeping default.\n");
|
2012-02-06 19:28:42 +00:00
|
|
|
return;
|
|
|
|
} else if (strncmp("BADIP", in, 5) == 0) {
|
|
|
|
fprintf(stderr, "Server rejected sender IP address.\n");
|
2009-08-16 12:30:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-02-06 19:28:42 +00:00
|
|
|
|
2014-01-29 18:33:54 +00:00
|
|
|
/* The server returns the accepted fragsize:
|
|
|
|
accepted_fragsize = ((in[0] & 0xff) << 8) | (in[1] & 0xff) */
|
2012-02-06 19:28:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
fprintf(stderr, "Retrying set fragsize...\n");
|
|
|
|
}
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.running)
|
2009-12-29 20:00:57 +00:00
|
|
|
return;
|
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
fprintf(stderr, "No reply from server when setting fragsize. Keeping default.\n");
|
|
|
|
}
|
|
|
|
|
2015-10-17 14:28:10 +00:00
|
|
|
static void
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_set_timeout()
|
2015-10-17 14:28:10 +00:00
|
|
|
{
|
|
|
|
char in[4096];
|
|
|
|
int read, id;
|
|
|
|
|
2016-01-09 14:13:13 +00:00
|
|
|
fprintf(stderr, "Setting window sizes to %lu frags upstream, %lu frags downstream...\n",
|
|
|
|
this.windowsize_up, this.windowsize_down);
|
|
|
|
|
|
|
|
fprintf(stderr, "Calculating round-trip time...");
|
|
|
|
|
|
|
|
/* Reset RTT stats */
|
|
|
|
this.num_immediate = 0;
|
|
|
|
this.rtt_total_ms = 0;
|
2015-10-17 14:28:10 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
for (int i = 0; this.running && i < 5; i++) {
|
2015-10-17 14:28:10 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
id = this.autodetect_server_timeout ?
|
2016-01-09 11:29:17 +00:00
|
|
|
update_server_timeout(1) : send_ping(1, -1, 1);
|
2015-10-17 14:28:10 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
read = handshake_waitdns(in, sizeof(in), 'P', i + 1);
|
2015-10-17 14:28:10 +00:00
|
|
|
got_response(id, 1, 0);
|
|
|
|
|
2015-10-27 09:25:56 +00:00
|
|
|
fprintf(stderr, ".");
|
2015-10-17 14:28:10 +00:00
|
|
|
if (read > 0) {
|
|
|
|
if (strncmp("BADIP", in, 5) == 0) {
|
|
|
|
fprintf(stderr, "Server rejected sender IP address.\n");
|
|
|
|
}
|
2016-01-09 14:13:13 +00:00
|
|
|
continue;
|
2015-10-17 14:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.running)
|
2015-10-17 14:28:10 +00:00
|
|
|
return;
|
|
|
|
|
2016-01-09 14:13:13 +00:00
|
|
|
fprintf(stderr, "\nDetermined round-trip time of %ld ms, using server timeout of %ld ms.\n",
|
|
|
|
this.rtt_total_ms / this.num_immediate, this.server_timeout_ms);
|
2015-10-17 14:28:10 +00:00
|
|
|
}
|
|
|
|
|
2009-08-16 12:30:26 +00:00
|
|
|
int
|
2016-01-09 11:29:17 +00:00
|
|
|
client_handshake()
|
2009-08-16 12:30:26 +00:00
|
|
|
{
|
|
|
|
int seed;
|
2009-12-29 20:00:57 +00:00
|
|
|
int upcodec;
|
2009-08-16 12:30:26 +00:00
|
|
|
int r;
|
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
dnsc_use_edns0 = 0;
|
|
|
|
|
|
|
|
/* qtype message printed in handshake function */
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.do_qtype == T_UNSET) {
|
2016-01-09 11:29:17 +00:00
|
|
|
r = handshake_qtype_autodetect();
|
2009-12-29 20:00:57 +00:00
|
|
|
if (r) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 16:11:16 +00:00
|
|
|
fprintf(stderr, "Using DNS type %s queries\n", client_get_qtype());
|
2009-12-29 20:00:57 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
r = handshake_version(&seed);
|
2009-08-16 12:30:26 +00:00
|
|
|
if (r) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
r = handshake_login(seed);
|
2009-08-16 12:30:26 +00:00
|
|
|
if (r) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
if (this.raw_mode && handshake_raw_udp(seed)) {
|
2016-01-09 07:04:39 +00:00
|
|
|
this.conn = CONN_RAW_UDP;
|
|
|
|
this.max_timeout_ms = 10000;
|
|
|
|
this.compression_down = 1;
|
|
|
|
this.compression_up = 1;
|
2009-08-16 12:30:26 +00:00
|
|
|
} else {
|
2016-01-09 11:29:17 +00:00
|
|
|
if (this.raw_mode == 0) {
|
2009-08-16 12:30:26 +00:00
|
|
|
fprintf(stderr, "Skipping raw mode\n");
|
|
|
|
}
|
|
|
|
|
2009-12-29 20:00:57 +00:00
|
|
|
dnsc_use_edns0 = 1;
|
2016-01-09 11:29:17 +00:00
|
|
|
if (handshake_edns0_check() && this.running) {
|
2009-12-29 20:00:57 +00:00
|
|
|
fprintf(stderr, "Using EDNS0 extension\n");
|
2016-01-09 07:04:39 +00:00
|
|
|
} else if (!this.running) {
|
2009-12-29 20:00:57 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "DNS relay does not support EDNS0 extension\n");
|
|
|
|
dnsc_use_edns0 = 0;
|
|
|
|
}
|
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
upcodec = handshake_upenc_autodetect();
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.running)
|
2009-12-29 20:00:57 +00:00
|
|
|
return -1;
|
|
|
|
|
2015-08-23 14:14:27 +00:00
|
|
|
if (upcodec == 1) { /* Base64 */
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_switch_codec(6);
|
2015-08-23 14:14:27 +00:00
|
|
|
} else if (upcodec == 2) { /* Base64u */
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_switch_codec(26);
|
2015-08-23 14:14:27 +00:00
|
|
|
} else if (upcodec == 3) { /* Base128 */
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_switch_codec(7);
|
2009-12-29 20:00:57 +00:00
|
|
|
}
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.running)
|
2009-12-29 20:00:57 +00:00
|
|
|
return -1;
|
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
if (this.downenc == ' ') {
|
2016-01-09 11:29:17 +00:00
|
|
|
this.downenc = handshake_downenc_autodetect();
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.running)
|
2009-12-29 20:00:57 +00:00
|
|
|
return -1;
|
2009-08-16 12:30:26 +00:00
|
|
|
|
2016-01-09 07:04:39 +00:00
|
|
|
/* Set options for compression, this.lazymode and downstream codec */
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_switch_options(this.lazymode, this.compression_down, this.downenc);
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.running)
|
2009-12-29 20:00:57 +00:00
|
|
|
return -1;
|
2009-09-20 21:10:44 +00:00
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
if (this.autodetect_frag_size) {
|
|
|
|
this.max_downstream_frag_size = handshake_autoprobe_fragsize();
|
|
|
|
if (this.max_downstream_frag_size > MAX_FRAGSIZE) {
|
2015-08-21 03:08:47 +00:00
|
|
|
/* This is very unlikely except perhaps over LAN */
|
2015-10-17 14:28:10 +00:00
|
|
|
fprintf(stderr, "Can transfer fragsize of %d, however iodine has been compiled with MAX_FRAGSIZE = %d."
|
2016-01-09 11:29:17 +00:00
|
|
|
" To fully utilize this connection, please recompile iodine/iodined.\n", this.max_downstream_frag_size, MAX_FRAGSIZE);
|
|
|
|
this.max_downstream_frag_size = MAX_FRAGSIZE;
|
2015-08-21 03:08:47 +00:00
|
|
|
}
|
2016-01-09 11:29:17 +00:00
|
|
|
if (!this.max_downstream_frag_size) {
|
2009-08-16 12:30:26 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_set_fragsize(this.max_downstream_frag_size);
|
2016-01-09 07:04:39 +00:00
|
|
|
if (!this.running)
|
2009-12-29 20:00:57 +00:00
|
|
|
return -1;
|
2015-10-17 14:28:10 +00:00
|
|
|
|
|
|
|
/* init windowing protocol */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.outbuf = window_buffer_init(64, this.windowsize_up, this.maxfragsize_up, WINDOW_SENDING);
|
|
|
|
this.outbuf->timeout = ms_to_timeval(this.downstream_timeout_ms);
|
2015-10-17 14:28:10 +00:00
|
|
|
/* Incoming buffer max fragsize doesn't matter */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.inbuf = window_buffer_init(64, this.windowsize_down, MAX_FRAGSIZE, WINDOW_RECVING);
|
2015-10-17 14:28:10 +00:00
|
|
|
|
|
|
|
/* init query tracking */
|
2016-01-09 07:04:39 +00:00
|
|
|
this.num_untracked = 0;
|
|
|
|
this.num_pending = 0;
|
|
|
|
this.pending_queries = calloc(PENDING_QUERIES_LENGTH, sizeof(struct query_tuple));
|
2015-10-17 14:28:10 +00:00
|
|
|
for (int i = 0; i < PENDING_QUERIES_LENGTH; i++)
|
2016-01-09 07:04:39 +00:00
|
|
|
this.pending_queries[i].id = -1;
|
2015-10-17 14:28:10 +00:00
|
|
|
|
|
|
|
/* set server window/timeout parameters and calculate RTT */
|
2016-01-09 11:29:17 +00:00
|
|
|
handshake_set_timeout();
|
2009-08-16 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|