Refactored client/server to use debug macro

This commit is contained in:
frekky 2015-10-04 22:39:40 +08:00
parent 2e0b7f8eae
commit ceaac64814
4 changed files with 54 additions and 113 deletions

View File

@ -490,8 +490,7 @@ send_query(int fd, uint8_t *hostname)
struct query q; struct query q;
size_t len; size_t len;
if (debug >= 2) DEBUG(3, "TX: pkt len %lu: hostname '%s'", strlen((char *)hostname), hostname);
fprintf(stderr, "TX: pkt len %lu: hostname '%s'\n", strlen((char *)hostname), hostname);
chunkid += 7727; chunkid += 7727;
if (chunkid == 0) if (chunkid == 0)
@ -507,8 +506,7 @@ send_query(int fd, uint8_t *hostname)
return -1; return -1;
} }
if (debug >= 3) DEBUG(4, " Sendquery: id %5d name[0] '%c'", q.id, hostname[0]);
fprintf(stderr, " Sendquery: id %5d name[0] '%c'\n", q.id, hostname[0]);
sendto(fd, packet, len, 0, (struct sockaddr*) &nameserv_addrs[current_nameserver], sendto(fd, packet, len, 0, (struct sockaddr*) &nameserv_addrs[current_nameserver],
sizeof(struct sockaddr_storage)); sizeof(struct sockaddr_storage));
@ -545,7 +543,7 @@ send_query(int fd, uint8_t *hostname)
} else if (lazymode && autodetect_server_timeout) { } else if (lazymode && autodetect_server_timeout) {
fprintf(stderr, "Receiving too few answers. Will try to switch lazy mode off, but that may not" fprintf(stderr, "Receiving too few answers. Will try to switch lazy mode off, but that may not"
" always work any more. Start with -L0 next time on this network."); " always work any more. Start with -L0 next time on this network.\n");
lazymode = 0; lazymode = 0;
server_timeout_ms = 0; server_timeout_ms = 0;
} }
@ -622,10 +620,8 @@ send_ping(int fd, int ping_response, int ack)
data[10] = (rand_seed >> 0) & 0xff; data[10] = (rand_seed >> 0) & 0xff;
rand_seed += 263; rand_seed += 263;
if (debug >= 3) { DEBUG(3, " SEND PING: respond %d, ack %d, servertimeout %ld, flags %02X",
fprintf(stderr, " SEND PING: respond %d, ack %d, servertimeout %ld, flags %02X\n",
ping_response, ack, server_timeout_ms, data[8]); ping_response, ack, server_timeout_ms, data[8]);
}
id = send_packet(fd, 'p', data, sizeof(data)); id = send_packet(fd, 'p', data, sizeof(data));
@ -904,8 +900,7 @@ read_dns_withq(int dns_fd, int tun_fd, uint8_t *buf, size_t buflen, struct query
memcpy(buf, data, rv); memcpy(buf, data, rv);
} }
if (debug >= 2) DEBUG(2, "RX: id %5d name[0]='%c'", q->id, q->name[0]);
fprintf(stderr, "RX: id %5d name[0]='%c'\n", q->id, q->name[0]);
return rv; return rv;
} else { /* CONN_RAW_UDP */ } else { /* CONN_RAW_UDP */
@ -981,8 +976,7 @@ handshake_waitdns(int dns_fd, char *buf, size_t buflen, char cmd, int timeout)
qcmd = toupper(q.name[0]); qcmd = toupper(q.name[0]);
if (q.id != chunkid || qcmd != cmd) { if (q.id != chunkid || qcmd != cmd) {
if (debug >= 1) DEBUG(1, "Ignoring unfitting reply id %d starting with '%c'", q.id, q.name[0]);
fprintf(stderr, "Ignoring unfitting reply id %d starting with '%c'\n", q.id, q.name[0]);
continue; continue;
} }
@ -1057,11 +1051,9 @@ parse_data(uint8_t *data, size_t len, fragment *f, int *immediate)
up_wsize = data[4]; up_wsize = data[4];
dn_start_seq = data[5]; dn_start_seq = data[5];
up_start_seq = data[6]; up_start_seq = data[6];
if (debug >= 3) { DEBUG(3, "PING pkt data=%lu WS: up=%u, dn=%u; Start: up=%u, dn=%u",
fprintf(stderr, "PING pkt data=%lu WS: up=%u, dn=%u; Start: up=%u, dn=%u\n",
len - headerlen, up_wsize, dn_wsize, up_start_seq, dn_start_seq); len - headerlen, up_wsize, dn_wsize, up_start_seq, dn_start_seq);
} }
}
f->len = len - headerlen; f->len = len - headerlen;
if (f->len > 0) if (f->len > 0)
memcpy(f->data, data + headerlen, MIN(f->len, sizeof(f->data))); memcpy(f->data, data + headerlen, MIN(f->len, sizeof(f->data)));
@ -1082,14 +1074,12 @@ tunnel_tun(int tun_fd, int dns_fd)
/* Check if outgoing buffer can hold data */ /* Check if outgoing buffer can hold data */
if (window_buffer_available(outbuf) < (read / MAX_FRAGSIZE) + 1) { if (window_buffer_available(outbuf) < (read / MAX_FRAGSIZE) + 1) {
if (debug >= 2) DEBUG(1, " Outgoing buffer full (%lu/%lu), not adding data!",
fprintf(stderr, " Outgoing buffer full (%lu/%lu), not adding data!\n",
outbuf->numitems, outbuf->length); outbuf->numitems, outbuf->length);
return -1; return -1;
} }
if (debug >= 2) DEBUG(2, " IN: %lu bytes on tunnel, compression %d", read, compression_up);
fprintf(stderr, " IN: %lu bytes on tunnel, compression %d\n", read, compression_up);
if (conn != CONN_DNS_NULL || compression_up) { if (conn != CONN_DNS_NULL || compression_up) {
datalen = sizeof(out); datalen = sizeof(out);
@ -1207,8 +1197,8 @@ tunnel_dns(int tun_fd, int dns_fd)
too fast, to avoid runaway ping-pong loops..) */ too fast, to avoid runaway ping-pong loops..) */
/* Don't send anything too soon; no data waiting from server */ /* Don't send anything too soon; no data waiting from server */
if (f.len == 0) { if (f.len == 0) {
if (!res && debug >= 1) if (!res)
fprintf(stderr, "[WARNING] Received downstream data fragment with 0 length and NOT a ping!"); DEBUG(1, "[WARNING] Received downstream data fragment with 0 length and NOT a ping!");
if (!lazymode) if (!lazymode)
send_ping_soon = 100; send_ping_soon = 100;
else else
@ -1221,8 +1211,7 @@ tunnel_dns(int tun_fd, int dns_fd)
if (next_downstream_ack >= 0) { if (next_downstream_ack >= 0) {
/* If this happens something is wrong (or last frag was a re-send) /* If this happens something is wrong (or last frag was a re-send)
* May result in ACKs being delayed. */ * May result in ACKs being delayed. */
if (debug >= 1) DEBUG(1, "next_downstream_ack NOT -1! (%d), %u resends, %u oos", next_downstream_ack, outbuf->resends, outbuf->oos);
warnx("next_downstream_ack NOT -1! (%d), %u resends, %u oos", next_downstream_ack, outbuf->resends, outbuf->oos);
} }
/* Downstream data traffic + get ack for that data */ /* Downstream data traffic + get ack for that data */
@ -1235,8 +1224,7 @@ tunnel_dns(int tun_fd, int dns_fd)
if (compressed) { if (compressed) {
buflen = sizeof(buf); buflen = sizeof(buf);
if ((res = uncompress(buf, &buflen, cbuf, datalen)) != Z_OK) { if ((res = uncompress(buf, &buflen, cbuf, datalen)) != Z_OK) {
if (debug >= 1) DEBUG(1, "Uncompress failed (%d) for data len %lu: reassembled data corrupted or incomplete!", res, datalen);
warnx("Uncompress failed (%d) for data len %lu: reassembled data corrupted or incomplete!", res, datalen);
datalen = 0; datalen = 0;
} else { } else {
datalen = buflen; datalen = buflen;
@ -2105,8 +2093,7 @@ handshake_qtype_autodetect(int dns_fd)
if (handshake_qtypetest(dns_fd, timeout)) { if (handshake_qtypetest(dns_fd, timeout)) {
/* okay */ /* okay */
highestworking = qtypenum; highestworking = qtypenum;
if (debug >= 1) DEBUG(1, " Type %s timeout %d works", client_get_qtype(), timeout);
fprintf(stderr, " Type %s timeout %d works\n", client_get_qtype(), timeout);
break; break;
/* try others with longer timeout */ /* try others with longer timeout */
} }

View File

@ -87,7 +87,7 @@ extern const unsigned char raw_header[RAW_HDR_LEN];
if (level >= 3) {\ if (level >= 3) {\
fprintf(stderr, "[D%d %s:%d] ", level, __FILE__, __LINE__); \ fprintf(stderr, "[D%d %s:%d] ", level, __FILE__, __LINE__); \
} else { \ } else { \
fprintf(stderr, "[D%d] ", level)\ fprintf(stderr, "[D%d] ", level);\
}\ }\
fprintf(stderr, __VA_ARGS__);\ fprintf(stderr, __VA_ARGS__);\
fprintf(stderr, "\n");\ fprintf(stderr, "\n");\

View File

@ -112,10 +112,8 @@ send_raw(int fd, uint8_t *buf, size_t buflen, int user, int cmd, struct sockaddr
len += RAW_HDR_LEN; len += RAW_HDR_LEN;
packet[RAW_HDR_CMD] = cmd | (user & 0x0F); packet[RAW_HDR_CMD] = cmd | (user & 0x0F);
if (debug >= 2) { DEBUG(3, "TX-raw: client %s (user %d), cmd %d, %d bytes\n",
fprintf(stderr, "TX-raw: client %s (user %d), cmd %d, %d bytes\n",
format_addr(from, fromlen), user, cmd, len); format_addr(from, fromlen), user, cmd, len);
}
sendto(fd, packet, len, 0, (struct sockaddr *) from, fromlen); sendto(fd, packet, len, 0, (struct sockaddr *) from, fromlen);
} }
@ -172,7 +170,7 @@ qmem_append(int dns_fd, int userid, struct query *q)
if (memcmp(pq->name, q->name, sizeof(q->name))) if (memcmp(pq->name, q->name, sizeof(q->name)))
continue; continue;
QMEM_DEBUG(1, userid, "OUT for '%s' == duplicate, sending illegal reply\n", q->name); QMEM_DEBUG(1, userid, "OUT for '%s' == duplicate, sending illegal reply", q->name);
// TODO cache answers/respond using cache? (merge with dnscache) // TODO cache answers/respond using cache? (merge with dnscache)
write_dns(dns_fd, q, "x", 1, 'T'); write_dns(dns_fd, q, "x", 1, 'T');
@ -211,7 +209,7 @@ qmem_answered(int userid)
if (buf->num_pending == 0) { if (buf->num_pending == 0) {
/* Most likely caused by bugs somewhere else. */ /* Most likely caused by bugs somewhere else. */
QMEM_DEBUG(3, userid, "can't answer query that has already been answered! Fix bugs."); QMEM_DEBUG(1, userid, "can't answer query that has already been answered! Fix bugs.");
return; return;
} }
answered = buf->start_pending; answered = buf->start_pending;
@ -409,8 +407,7 @@ answer_from_dnscache(int dns_fd, int userid, struct query *q)
continue; continue;
/* okay, match */ /* okay, match */
if (debug >= 1) DEBUG(1, "OUT user %d %s from dnscache", userid, q->name);
fprintf(stderr, "OUT user %d %s from dnscache\n", userid, q->name);
write_dns(dns_fd, q, users[userid].dnscache_answer[use], write_dns(dns_fd, q, users[userid].dnscache_answer[use],
users[userid].dnscache_answerlen[use], users[userid].dnscache_answerlen[use],
@ -462,9 +459,7 @@ forward_query(int bind_fd, struct query *q)
memcpy(&(myaddr->sin_addr), &newaddr, sizeof(in_addr_t)); memcpy(&(myaddr->sin_addr), &newaddr, sizeof(in_addr_t));
myaddr->sin_port = htons(bind_port); myaddr->sin_port = htons(bind_port);
if (debug >= 2) { DEBUG(2, "TX: NS reply");
fprintf(stderr, "TX: NS reply\n");
}
if (sendto(bind_fd, buf, len, 0, (struct sockaddr*)&q->from, q->fromlen) <= 0) { if (sendto(bind_fd, buf, len, 0, (struct sockaddr*)&q->from, q->fromlen) <= 0) {
warn("forward query error"); warn("forward query error");
@ -615,9 +610,7 @@ user_send_data(int userid, struct dnsfd *dns_fds, uint8_t *indata,
datalen = sizeof(out); datalen = sizeof(out);
ret = uncompress(out, &datalen, indata, len); ret = uncompress(out, &datalen, indata, len);
if (ret != Z_OK) { if (ret != Z_OK) {
if (debug >= 1) { DEBUG(1, "FAIL: Uncompress == %d: %lu bytes to user %d!", ret, len, userid);
warnx("Uncompress == %d: %lu bytes to user %d!", ret, len, userid);
}
return 0; return 0;
} }
} }
@ -629,9 +622,8 @@ user_send_data(int userid, struct dnsfd *dns_fds, uint8_t *indata,
ret = window_add_outgoing_data(users[userid].outgoing, data, datalen, compressed); ret = window_add_outgoing_data(users[userid].outgoing, data, datalen, compressed);
} else if (data && datalen) { /* CONN_RAW_UDP */ } else if (data && datalen) { /* CONN_RAW_UDP */
if (!compressed && debug >= 1) { if (!compressed)
warnx("Sending in RAW mode uncompressed to user %d!", userid); DEBUG(1, "Sending in RAW mode uncompressed to user %d!", userid);
}
int dns_fd = get_dns_fd(dns_fds, &users[userid].host); int dns_fd = get_dns_fd(dns_fds, &users[userid].host);
send_raw(dns_fd, data, datalen, userid, RAW_HDR_CMD_DATA, send_raw(dns_fd, data, datalen, userid, RAW_HDR_CMD_DATA,
&users[userid].host, users[userid].hostlen); &users[userid].host, users[userid].hostlen);
@ -661,23 +653,17 @@ tunnel_bind(int bind_fd, struct dnsfd *dns_fds)
id = dns_get_id(packet, r); id = dns_get_id(packet, r);
if (debug >= 2) { DEBUG(3, "RX: Got response on query %u from DNS", (id & 0xFFFF));
fprintf(stderr, "RX: Got response on query %u from DNS\n", (id & 0xFFFF));
}
/* Get sockaddr from id */ /* Get sockaddr from id */
fw_query_get(id, &query); fw_query_get(id, &query);
if (!query) { if (!query) {
if (debug >= 2) { DEBUG(2, "Lost sender of id %u, dropping reply", (id & 0xFFFF));
fprintf(stderr, "Lost sender of id %u, dropping reply\n", (id & 0xFFFF));
}
return 0; return 0;
} }
if (debug >= 2) { DEBUG(3, "TX: client %s id %u, %d bytes",
fprintf(stderr, "TX: client %s id %u, %d bytes\n",
format_addr(&query->addr, query->addrlen), (id & 0xffff), r); format_addr(&query->addr, query->addrlen), (id & 0xffff), r);
}
dns_fd = get_dns_fd(dns_fds, &query->addr); dns_fd = get_dns_fd(dns_fds, &query->addr);
if (sendto(dns_fd, packet, r, 0, (const struct sockaddr *) &(query->addr), if (sendto(dns_fd, packet, r, 0, (const struct sockaddr *) &(query->addr),
@ -705,8 +691,7 @@ tunnel_tun(int tun_fd, struct dnsfd *dns_fds)
if (userid < 0) if (userid < 0)
return 0; return 0;
if (debug >= 2) DEBUG(3, "IN: %d byte pkt from tun to user %d; compression %d",
fprintf(stderr, "IN: %d byte pkt from tun to user %d; compression %d\n",
read, userid, users[userid].down_compression); read, userid, users[userid].down_compression);
return user_send_data(userid, dns_fds, in, read, 0); return user_send_data(userid, dns_fds, in, read, 0);
@ -723,10 +708,8 @@ tunnel_dns(int tun_fd, int dns_fd, struct dnsfd *dns_fds, int bind_fd)
if ((read = read_dns(dns_fd, dns_fds, tun_fd, &q)) <= 0) if ((read = read_dns(dns_fd, dns_fds, tun_fd, &q)) <= 0)
return 0; return 0;
if (debug >= 2) { DEBUG(3, "RX: client %s ID %5d, type %d, name %s\n",
fprintf(stderr, "RX: client %s ID %5d, type %d, name %s\n",
format_addr(&q.from, q.fromlen), q.id, q.type, q.name); format_addr(&q.from, q.fromlen), q.id, q.type, q.name);
}
domain_len = strlen(q.name) - strlen(topdomain); domain_len = strlen(q.name) - strlen(topdomain);
if (domain_len >= 0 && !strcasecmp(q.name + domain_len, topdomain)) if (domain_len >= 0 && !strcasecmp(q.name + domain_len, topdomain))
@ -778,9 +761,7 @@ tunnel_dns(int tun_fd, int dns_fd, struct dnsfd *dns_fds, int bind_fd)
} }
} else { } else {
/* Forward query to other port ? */ /* Forward query to other port ? */
if (debug >= 3) { DEBUG(2, "Requested domain outside our topdomain.");
fprintf(stderr, "Requested domain outside our topdomain.\n");
}
if (bind_fd) { if (bind_fd) {
forward_query(bind_fd, &q); forward_query(bind_fd, &q);
} }
@ -798,7 +779,7 @@ server_tunnel(int tun_fd, struct dnsfd *dns_fds, int bind_fd, int max_idle_time)
struct query *answer_now = NULL; struct query *answer_now = NULL;
time_t last_action = time(NULL); time_t last_action = time(NULL);
if (debug >= 4) if (debug >= 5)
window_debug = debug - 3; window_debug = debug - 3;
while (running) { while (running) {
@ -894,8 +875,7 @@ handle_full_packet(int tun_fd, struct dnsfd *dns_fds, int userid, uint8_t *data,
if (ret == Z_OK) { if (ret == Z_OK) {
hdr = (struct ip*) (out + 4); hdr = (struct ip*) (out + 4);
touser = find_user_by_ip(hdr->ip_dst.s_addr); touser = find_user_by_ip(hdr->ip_dst.s_addr);
if (debug >= 3) DEBUG(2, "FULL PKT: %lu bytes from user %d (touser %d)", len, userid, touser);
fprintf(stderr, "FULL PKT: %lu bytes from user %d (touser %d)\n", len, userid, touser);
if (touser == -1) { if (touser == -1) {
/* send the uncompressed packet to tun device */ /* send the uncompressed packet to tun device */
write_tun(tun_fd, rawdata, rawlen); write_tun(tun_fd, rawdata, rawlen);
@ -908,8 +888,7 @@ handle_full_packet(int tun_fd, struct dnsfd *dns_fds, int userid, uint8_t *data,
} }
} }
} else { } else {
if (debug >= 2) DEBUG(2, "Discarded upstream data from user %d, uncompress() result: %d", userid, ret);
fprintf(stderr, "Discarded upstream data from user %d, uncompress() result: %d\n", userid, ret);
} }
} }
@ -925,9 +904,7 @@ handle_raw_login(uint8_t *packet, size_t len, struct query *q, int fd, int useri
if (userid < 0 || userid >= created_users) return; if (userid < 0 || userid >= created_users) return;
if (!check_authenticated_user_and_ip(userid, q)) return; if (!check_authenticated_user_and_ip(userid, q)) return;
if (debug >= 1) { DEBUG(1, "IN login raw, len %lu, from user %d", len, userid);
fprintf(stderr, "IN login raw, len %lu, from user %d\n", len, userid);
}
/* User sends hash of seed + 1 */ /* User sends hash of seed + 1 */
login_calculate(myhash, 16, password, users[userid].seed + 1); login_calculate(myhash, 16, password, users[userid].seed + 1);
@ -961,9 +938,7 @@ handle_raw_data(uint8_t *packet, size_t len, struct query *q, struct dnsfd *dns_
/* copy to packet buffer, update length TODO fix the raw UDP protocol */ /* copy to packet buffer, update length TODO fix the raw UDP protocol */
if (debug >= 1) { DEBUG(3, "IN pkt raw, total %lu, from user %d", len, userid);
fprintf(stderr, "IN pkt raw, total %lu, from user %d\n", len, userid);
}
handle_full_packet(tun_fd, dns_fds, userid, packet, len, 1); handle_full_packet(tun_fd, dns_fds, userid, packet, len, 1);
} }
@ -979,9 +954,7 @@ handle_raw_ping(struct query *q, int dns_fd, int userid)
/* Update time info for user */ /* Update time info for user */
users[userid].last_pkt = time(NULL); users[userid].last_pkt = time(NULL);
if (debug >= 1) { DEBUG(3, "IN ping raw, from user %d", userid);
fprintf(stderr, "IN ping raw, from user %d\n", userid);
}
/* Send ping reply */ /* Send ping reply */
send_raw(dns_fd, NULL, 0, userid, RAW_HDR_CMD_PING, &q->from, q->fromlen); send_raw(dns_fd, NULL, 0, userid, RAW_HDR_CMD_PING, &q->from, q->fromlen);
@ -998,8 +971,7 @@ raw_decode(uint8_t *packet, size_t len, struct query *q, int dns_fd, struct dnsf
if (memcmp(packet, raw_header, RAW_HDR_IDENT_LEN)) return 0; if (memcmp(packet, raw_header, RAW_HDR_IDENT_LEN)) return 0;
raw_user = RAW_HDR_GET_USR(packet); raw_user = RAW_HDR_GET_USR(packet);
if (debug >= 3) DEBUG(3, "TX-raw: client %s, user %d, raw command '%c' length %lu",
warnx("TX-raw: client %s, user %d, raw command '%c' length %lu",
format_addr(&q->from, q->fromlen), raw_user, RAW_HDR_GET_CMD(packet), len); format_addr(&q->from, q->fromlen), raw_user, RAW_HDR_GET_CMD(packet), len);
switch (RAW_HDR_GET_CMD(packet)) { switch (RAW_HDR_GET_CMD(packet)) {
case RAW_HDR_CMD_LOGIN: case RAW_HDR_CMD_LOGIN:
@ -1015,7 +987,7 @@ raw_decode(uint8_t *packet, size_t len, struct query *q, int dns_fd, struct dnsf
handle_raw_ping(q, dns_fd, raw_user); handle_raw_ping(q, dns_fd, raw_user);
break; break;
default: default:
warnx("Unhandled raw command %02X from user %d", RAW_HDR_GET_CMD(packet), raw_user); DEBUG(1, "Unhandled raw command %02X from user %d", RAW_HDR_GET_CMD(packet), raw_user);
break; break;
} }
return 1; return 1;
@ -1217,10 +1189,8 @@ write_dns(int fd, struct query *q, char *data, size_t datalen, char downenc)
return; return;
} }
if (debug >= 2) { DEBUG(3, "TX: client %s ID %5d, %lu bytes data, type %d, name '%10s'\n",
fprintf(stderr, "TX: client %s ID %5d, %lu bytes data, type %d, name '%10s'\n",
format_addr(&q->from, q->fromlen), q->id, datalen, q->type, q->name); format_addr(&q->from, q->fromlen), q->id, datalen, q->type, q->name);
}
sendto(fd, buf, len, 0, (struct sockaddr*)&q->from, q->fromlen); sendto(fd, buf, len, 0, (struct sockaddr*)&q->from, q->fromlen);
} }
@ -1246,9 +1216,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
memcpy(in, q->name, MIN(domain_len, sizeof(in))); memcpy(in, q->name, MIN(domain_len, sizeof(in)));
if (debug >= 3) { DEBUG(3, "NULL request length %d/%lu, command '%c'\n", domain_len, sizeof(in), in[0]);
fprintf(stderr, "NULL request length %d/%lu, command '%c'\n", domain_len, sizeof(in), in[0]);
}
if(in[0] == 'V' || in[0] == 'v') { /* Version request */ if(in[0] == 'V' || in[0] == 'v') { /* Version request */
uint32_t version = !PROTOCOL_VERSION; uint32_t version = !PROTOCOL_VERSION;
@ -1303,8 +1271,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
u->dnscache_lastfilled = 0; u->dnscache_lastfilled = 0;
#endif #endif
if (debug >= 1) DEBUG(1, "User %d connected with correct version from %s.",
fprintf(stderr, "User %d connected with correct version from %s.\n",
userid, format_addr(&q->from, q->fromlen)); userid, format_addr(&q->from, q->fromlen));
} else { } else {
/* No space for another user */ /* No space for another user */
@ -1327,8 +1294,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
/* Login phase, handle auth */ /* Login phase, handle auth */
userid = unpacked[0]; userid = unpacked[0];
if (debug >= 3) DEBUG(2, "Received login request for user %d from %s.",
fprintf(stderr, "Received login request for user %d from %s.\n",
userid, format_addr(&q->from, q->fromlen)); userid, format_addr(&q->from, q->fromlen));
if (check_user_and_ip(userid, q) != 0) { if (check_user_and_ip(userid, q) != 0) {
write_dns(dns_fd, q, "BADIP", 5, 'T'); write_dns(dns_fd, q, "BADIP", 5, 'T');
@ -1519,14 +1485,12 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
if ((q->type == T_NULL || q->type == T_PRIVATE) && !bits) { if ((q->type == T_NULL || q->type == T_PRIVATE) && !bits) {
users[userid].downenc = 'R'; users[userid].downenc = 'R';
bits = 8; bits = 8;
if (debug >= 3) DEBUG(2, "Assuming raw data encoding with NULL/PRIVATE requests for user %d.", userid);
warnx("Assuming raw data encoding with NULL/PRIVATE requests for user %d.", userid);
} }
if (bits) { if (bits) {
int f = users[userid].fragsize; int f = users[userid].fragsize;
users[userid].outgoing->maxfraglen = (bits * f) / 8 - DOWNSTREAM_PING_HDR; users[userid].outgoing->maxfraglen = (bits * f) / 8 - DOWNSTREAM_PING_HDR;
if (debug >= 1) DEBUG(1, "Setting max downstream data length to %u bytes for user %d; bits %d (%c)",
warnx("Setting max downstream data length to %u bytes for user %d; bits %d (%c)",
users[userid].outgoing->maxfraglen, userid, bits, users[userid].downenc); users[userid].outgoing->maxfraglen, userid, bits, users[userid].downenc);
users[userid].downenc_bits = bits; users[userid].downenc_bits = bits;
} }
@ -1602,9 +1566,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
} }
req_frag_size = ntohs(*(uint16_t *) (unpacked + 1)); req_frag_size = ntohs(*(uint16_t *) (unpacked + 1));
if (debug >= 3) { DEBUG(3, "Got downstream fragsize probe from user %d, required fragsize %d", userid, req_frag_size);
fprintf(stderr, "Got downstream fragsize probe from user %d, required fragsize %d\n", userid, req_frag_size);
}
if (req_frag_size < 2 || req_frag_size > 2047) { if (req_frag_size < 2 || req_frag_size > 2047) {
write_dns(dns_fd, q, "BADFRAG", 7, users[userid].downenc); write_dns(dns_fd, q, "BADFRAG", 7, users[userid].downenc);
@ -1649,8 +1611,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
8 - DOWNSTREAM_PING_HDR; 8 - DOWNSTREAM_PING_HDR;
write_dns(dns_fd, q, (char *) unpacked + 1, 2, users[userid].downenc); write_dns(dns_fd, q, (char *) unpacked + 1, 2, users[userid].downenc);
if (debug >= 1) DEBUG(1, "Setting max downstream data length to %u bytes for user %d; %d bits (%c)",
warnx("Setting max downstream data length to %u bytes for user %d; %d bits (%c)",
users[userid].outgoing->maxfraglen, userid, users[userid].downenc_bits, users[userid].downenc); users[userid].outgoing->maxfraglen, userid, users[userid].downenc_bits, users[userid].downenc);
} }
return; return;
@ -1670,7 +1631,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
read = unpack_data(unpacked, sizeof(unpacked), in + 1, domain_len - 1, b32); read = unpack_data(unpacked, sizeof(unpacked), in + 1, domain_len - 1, b32);
if (read < UPSTREAM_PING) { if (read < UPSTREAM_PING) {
if (debug >= 1) warnx("Invalid ping! Length %lu", read); DEBUG(1, "Invalid ping! Length %lu", read);
return; return;
} }
@ -1712,10 +1673,8 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
} }
} }
if (debug >= 2) { DEBUG(3, "PING pkt from user %d, down %d/%d, up %d/%d, ACK %d, set timeout %u ms (flags %02X)",
fprintf(stderr, "PING pkt from user %d, down %d/%d, up %d/%d, ACK %d, set timeout %u ms (flags %02X)\n",
userid, dn_seq, dn_winsize, up_seq, up_winsize, dn_ack, timeout_ms, unpacked[8]); userid, dn_seq, dn_winsize, up_seq, up_winsize, dn_ack, timeout_ms, unpacked[8]);
}
user_process_incoming_data(tun_fd, dns_fds, userid, dn_ack); user_process_incoming_data(tun_fd, dns_fds, userid, dn_ack);
@ -1738,7 +1697,7 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
different id, then all okay. different id, then all okay.
Else client doesn't get our ack, and will retransmit in 1 second. */ Else client doesn't get our ack, and will retransmit in 1 second. */
if (q->id == 0) { if (q->id == 0) {
warnx("Query with ID 0!"); DEBUG(1, "Query with ID 0!");
return; return;
} }
@ -1782,14 +1741,13 @@ handle_null_request(int tun_fd, int dns_fd, struct dnsfd *dns_fds, struct query
f.len = unpack_data(f.data, MAX_FRAGSIZE, in + UPSTREAM_HDR, f.len = unpack_data(f.data, MAX_FRAGSIZE, in + UPSTREAM_HDR,
domain_len - UPSTREAM_HDR, users[userid].encoder); domain_len - UPSTREAM_HDR, users[userid].encoder);
if (debug >= 3) DEBUG(3, "frag seq %3u, datalen %5lu, ACK %3d, compression %1d, s%1d e%1d",
warnx("frag seq %3u, datalen %5lu, ACK %3d, compression %1d, s%1d e%1d",
f.seqID, f.len, f.ack_other, f.compressed, f.start, f.end); f.seqID, f.len, f.ack_other, f.compressed, f.start, f.end);
/* if already waiting for an ACK to be sent back upstream (on incoming buffer) */ /* if already waiting for an ACK to be sent back upstream (on incoming buffer) */
if (users[userid].next_upstream_ack >= 0) { if (users[userid].next_upstream_ack >= 0) {
/* Shouldn't normally happen; will always be reset after sending a packet. */ /* Shouldn't normally happen; will always be reset after sending a packet. */
warnx("[WARNING] next_upstream_ack == %d for user %d.", users[userid].next_upstream_ack, userid); DEBUG(1, "[WARNING] next_upstream_ack == %d for user %d.", users[userid].next_upstream_ack, userid);
} }
users[userid].next_upstream_ack = window_process_incoming_fragment(users[userid].incoming, &f); users[userid].next_upstream_ack = window_process_incoming_fragment(users[userid].incoming, &f);
@ -1821,10 +1779,8 @@ handle_ns_request(int dns_fd, struct query *q)
return; return;
} }
if (debug >= 2) { DEBUG(2, "TX: NS reply client %s ID %5d, type %d, name %s, %d bytes",
fprintf(stderr, "TX: NS reply client %s ID %5d, type %d, name %s, %d bytes\n",
format_addr(&q->from, q->fromlen), q->id, q->type, q->name, len); format_addr(&q->from, q->fromlen), q->id, q->type, q->name, len);
}
if (sendto(dns_fd, buf, len, 0, (struct sockaddr*)&q->from, q->fromlen) <= 0) { if (sendto(dns_fd, buf, len, 0, (struct sockaddr*)&q->from, q->fromlen) <= 0) {
warn("ns reply send error"); warn("ns reply send error");
} }
@ -1855,10 +1811,8 @@ handle_a_request(int dns_fd, struct query *q, int fakeip)
return; return;
} }
if (debug >= 2) { DEBUG(2, "TX: A reply client %s ID %5d, type %d, name %s, %d bytes",
fprintf(stderr, "TX: A reply client %s ID %5d, type %d, name %s, %d bytes\n",
format_addr(&q->from, q->fromlen), q->id, q->type, q->name, len); format_addr(&q->from, q->fromlen), q->id, q->type, q->name, len);
}
if (sendto(dns_fd, buf, len, 0, (struct sockaddr*)&q->from, q->fromlen) <= 0) { if (sendto(dns_fd, buf, len, 0, (struct sockaddr*)&q->from, q->fromlen) <= 0) {
warn("a reply send error"); warn("a reply send error");
} }

View File

@ -81,7 +81,7 @@ init_users(in_addr_t my_ip, int netbits)
if (debug >= 2) { if (debug >= 2) {
struct in_addr IP; struct in_addr IP;
IP.s_addr = ip; IP.s_addr = ip;
fprintf(stderr, "User %d: IP %s\n", i, inet_ntoa(IP)); DEBUG(2, "User %d: IP %s\n", i, inet_ntoa(IP));
} }
users[i].tun_ip = ip; users[i].tun_ip = ip;
net.s_addr = ip; net.s_addr = ip;