mirror of
https://github.com/yarrick/iodine.git
synced 2024-11-22 16:19:20 +02:00
Split handshake() function into smaller functions
This commit is contained in:
parent
bf46666fe8
commit
9e6ae4ff1d
140
src/iodine.c
140
src/iodine.c
|
@ -500,20 +500,15 @@ send_codec_switch(int fd, int userid, int bits)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
handshake(int dns_fd)
|
handshake_version(int dns_fd, int *seed)
|
||||||
{
|
{
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
uint32_t payload;
|
|
||||||
char server[65];
|
|
||||||
char client[65];
|
|
||||||
char login[16];
|
|
||||||
char in[4096];
|
char in[4096];
|
||||||
fd_set fds;
|
fd_set fds;
|
||||||
int read;
|
uint32_t payload;
|
||||||
int mtu;
|
|
||||||
int seed;
|
|
||||||
int i;
|
int i;
|
||||||
int r;
|
int r;
|
||||||
|
int read;
|
||||||
|
|
||||||
for (i = 0; running && i < 5; i++) {
|
for (i = 0; running && i < 5; i++) {
|
||||||
tv.tv_sec = i + 1;
|
tv.tv_sec = i + 1;
|
||||||
|
@ -544,11 +539,11 @@ handshake(int dns_fd)
|
||||||
((in[7] & 0xff)));
|
((in[7] & 0xff)));
|
||||||
|
|
||||||
if (strncmp("VACK", in, 4) == 0) {
|
if (strncmp("VACK", in, 4) == 0) {
|
||||||
seed = payload;
|
*seed = payload;
|
||||||
userid = in[8];
|
userid = in[8];
|
||||||
|
|
||||||
printf("Version ok, both using protocol v 0x%08x. You are user #%d\n", VERSION, userid);
|
printf("Version ok, both using protocol v 0x%08x. You are user #%d\n", VERSION, userid);
|
||||||
goto perform_login;
|
return 0;
|
||||||
} else if (strncmp("VNAK", in, 4) == 0) {
|
} else if (strncmp("VNAK", in, 4) == 0) {
|
||||||
warnx("You use protocol v 0x%08x, server uses v 0x%08x. Giving up",
|
warnx("You use protocol v 0x%08x, server uses v 0x%08x. Giving up",
|
||||||
VERSION, payload);
|
VERSION, payload);
|
||||||
|
@ -563,10 +558,24 @@ handshake(int dns_fd)
|
||||||
|
|
||||||
printf("Retrying version check...\n");
|
printf("Retrying version check...\n");
|
||||||
}
|
}
|
||||||
errx(1, "couldn't connect to server");
|
warnx("couldn't connect to server");
|
||||||
/* NOTREACHED */
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
handshake_login(int dns_fd, int seed)
|
||||||
|
{
|
||||||
|
struct timeval tv;
|
||||||
|
char in[4096];
|
||||||
|
char login[16];
|
||||||
|
char server[65];
|
||||||
|
char client[65];
|
||||||
|
int mtu;
|
||||||
|
fd_set fds;
|
||||||
|
int i;
|
||||||
|
int r;
|
||||||
|
int read;
|
||||||
|
|
||||||
perform_login:
|
|
||||||
login_calculate(login, 16, password, seed);
|
login_calculate(login, 16, password, seed);
|
||||||
|
|
||||||
for (i=0; running && i<5 ;i++) {
|
for (i=0; running && i<5 ;i++) {
|
||||||
|
@ -600,7 +609,7 @@ perform_login:
|
||||||
client[64] = 0;
|
client[64] = 0;
|
||||||
if (tun_setip(client, netmask) == 0 &&
|
if (tun_setip(client, netmask) == 0 &&
|
||||||
tun_setmtu(mtu) == 0) {
|
tun_setmtu(mtu) == 0) {
|
||||||
goto perform_case_check;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
warnx("Received handshake with bad data");
|
warnx("Received handshake with bad data");
|
||||||
}
|
}
|
||||||
|
@ -614,8 +623,18 @@ perform_login:
|
||||||
}
|
}
|
||||||
warnx("couldn't login to server");
|
warnx("couldn't login to server");
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
handshake_case_check(int dns_fd)
|
||||||
|
{
|
||||||
|
struct timeval tv;
|
||||||
|
char in[4096];
|
||||||
|
fd_set fds;
|
||||||
|
int i;
|
||||||
|
int r;
|
||||||
|
int read;
|
||||||
|
|
||||||
perform_case_check:
|
|
||||||
case_preserved = 0;
|
case_preserved = 0;
|
||||||
for (i=0; running && i<5 ;i++) {
|
for (i=0; running && i<5 ;i++) {
|
||||||
tv.tv_sec = i + 1;
|
tv.tv_sec = i + 1;
|
||||||
|
@ -635,7 +654,7 @@ perform_case_check:
|
||||||
if (in[0] == 'z' || in[0] == 'Z') {
|
if (in[0] == 'z' || in[0] == 'Z') {
|
||||||
if (read < (27 * 2)) {
|
if (read < (27 * 2)) {
|
||||||
printf("Received short case check reply. Will use base32 encoder\n");
|
printf("Received short case check reply. Will use base32 encoder\n");
|
||||||
goto switch_codec;
|
return;
|
||||||
} else {
|
} else {
|
||||||
int k;
|
int k;
|
||||||
|
|
||||||
|
@ -647,14 +666,14 @@ perform_case_check:
|
||||||
case_preserved = 0;
|
case_preserved = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
goto switch_codec;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
printf("Received bad case check reply\n");
|
printf("Received bad case check reply\n");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
printf("Got error on case check, will use base32\n");
|
printf("Got error on case check, will use base32\n");
|
||||||
goto switch_codec;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -662,9 +681,17 @@ perform_case_check:
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("No reply on case check, continuing\n");
|
printf("No reply on case check, continuing\n");
|
||||||
switch_codec:
|
}
|
||||||
if (!case_preserved)
|
|
||||||
goto autodetect_max_fragsize;
|
static void
|
||||||
|
handshake_switch_codec(int dns_fd)
|
||||||
|
{
|
||||||
|
struct timeval tv;
|
||||||
|
char in[4096];
|
||||||
|
fd_set fds;
|
||||||
|
int i;
|
||||||
|
int r;
|
||||||
|
int read;
|
||||||
|
|
||||||
dataenc = get_base64_encoder();
|
dataenc = get_base64_encoder();
|
||||||
printf("Switching to %s codec\n", dataenc->name);
|
printf("Switching to %s codec\n", dataenc->name);
|
||||||
|
@ -699,19 +726,30 @@ switch_codec:
|
||||||
}
|
}
|
||||||
in[read] = 0; /* zero terminate */
|
in[read] = 0; /* zero terminate */
|
||||||
printf("Server switched to codec %s\n", in);
|
printf("Server switched to codec %s\n", in);
|
||||||
goto autodetect_max_fragsize;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("Retrying codec switch...\n");
|
printf("Retrying codec switch...\n");
|
||||||
}
|
}
|
||||||
printf("No reply from server on codec switch. ");
|
printf("No reply from server on codec switch. ");
|
||||||
|
|
||||||
codec_revert:
|
codec_revert:
|
||||||
printf("Falling back to base32\n");
|
printf("Falling back to base32\n");
|
||||||
dataenc = get_base32_encoder();
|
dataenc = get_base32_encoder();
|
||||||
autodetect_max_fragsize:
|
}
|
||||||
if (autodetect_frag_size) {
|
|
||||||
|
static int
|
||||||
|
handshake_autoprobe_fragsize(int dns_fd)
|
||||||
|
{
|
||||||
|
struct timeval tv;
|
||||||
|
char in[4096];
|
||||||
|
fd_set fds;
|
||||||
|
int i;
|
||||||
|
int r;
|
||||||
|
int read;
|
||||||
int proposed_fragsize = 768;
|
int proposed_fragsize = 768;
|
||||||
int range = 768;
|
int range = 768;
|
||||||
|
|
||||||
max_downstream_frag_size = 0;
|
max_downstream_frag_size = 0;
|
||||||
printf("Autoprobing max downstream fragment size... (skip with -m fragsize)\n");
|
printf("Autoprobing max downstream fragment size... (skip with -m fragsize)\n");
|
||||||
while (running && range > 0 && (range >= 8 || !max_downstream_frag_size)) {
|
while (running && range > 0 && (range >= 8 || !max_downstream_frag_size)) {
|
||||||
|
@ -768,7 +806,19 @@ badlen:
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
printf("will use %d\n", max_downstream_frag_size);
|
printf("will use %d\n", max_downstream_frag_size);
|
||||||
}
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
handshake_set_fragsize(int dns_fd)
|
||||||
|
{
|
||||||
|
struct timeval tv;
|
||||||
|
char in[4096];
|
||||||
|
fd_set fds;
|
||||||
|
int i;
|
||||||
|
int r;
|
||||||
|
int read;
|
||||||
|
|
||||||
printf("Setting downstream fragment size to max %d...\n", max_downstream_frag_size);
|
printf("Setting downstream fragment size to max %d...\n", max_downstream_frag_size);
|
||||||
for (i=0; running && i<5 ;i++) {
|
for (i=0; running && i<5 ;i++) {
|
||||||
tv.tv_sec = i + 1;
|
tv.tv_sec = i + 1;
|
||||||
|
@ -789,20 +839,52 @@ badlen:
|
||||||
|
|
||||||
if (strncmp("BADFRAG", in, 7) == 0) {
|
if (strncmp("BADFRAG", in, 7) == 0) {
|
||||||
printf("Server rejected fragsize. Keeping default.");
|
printf("Server rejected fragsize. Keeping default.");
|
||||||
goto done;
|
return;
|
||||||
} else if (strncmp("BADIP", in, 5) == 0) {
|
} else if (strncmp("BADIP", in, 5) == 0) {
|
||||||
printf("Server rejected sender IP address.\n");
|
printf("Server rejected sender IP address.\n");
|
||||||
goto done;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
accepted_fragsize = ((in[0] & 0xff) << 8) | (in[1] & 0xff);
|
accepted_fragsize = ((in[0] & 0xff) << 8) | (in[1] & 0xff);
|
||||||
goto done;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("Retrying set fragsize...\n");
|
printf("Retrying set fragsize...\n");
|
||||||
}
|
}
|
||||||
printf("No reply from server when setting fragsize. Keeping default.\n");
|
printf("No reply from server when setting fragsize. Keeping default.\n");
|
||||||
done:
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
handshake(int dns_fd)
|
||||||
|
{
|
||||||
|
int seed;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
r = handshake_version(dns_fd, &seed);
|
||||||
|
if (r) {
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
r = handshake_login(dns_fd, seed);
|
||||||
|
if (r) {
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
handshake_case_check(dns_fd);
|
||||||
|
|
||||||
|
if (case_preserved) {
|
||||||
|
handshake_switch_codec(dns_fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (autodetect_frag_size) {
|
||||||
|
r = handshake_autoprobe_fragsize(dns_fd);
|
||||||
|
if (r) {
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handshake_set_fragsize(dns_fd);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue