some code cleanup in handshake, fixes possibly strange errors if short packet received

This commit is contained in:
Bjorn Andersson 2007-02-10 17:10:17 +00:00
parent 3a3b06b4b4
commit 3dad84f5b9

View file

@ -304,6 +304,7 @@ static int
handshake(int dns_fd) handshake(int dns_fd)
{ {
struct timeval tv; struct timeval tv;
uint32_t payload;
char server[65]; char server[65];
char client[65]; char client[65];
char login[16]; char login[16];
@ -312,7 +313,6 @@ handshake(int dns_fd)
int read; int read;
int mtu; int mtu;
int seed; int seed;
int version;
int i; int i;
int r; int r;
@ -335,32 +335,32 @@ handshake(int dns_fd)
continue; continue;
} }
if (read > 0) {
if (strncmp("VACK", in, 4) == 0) {
if (read >= 8) { if (read >= 8) {
memcpy(&seed, in + 4, 4); payload = (((in[4] & 0xff) << 24) |
seed = ntohl(seed); ((in[5] & 0xff) << 16) |
((in[6] & 0xff) << 8) |
((in[7] & 0xff)));
if (strncmp("VACK", in, 4) == 0) {
seed = payload;
printf("Version ok, both running 0x%08x\n", VERSION); printf("Version ok, both running 0x%08x\n", VERSION);
break; goto perform_login;
} else { } else {
printf("Version ok but did not receive proper login challenge\n");
}
} else {
memcpy(&version, in + 4, 4);
version = ntohl(version);
errx(1, "you run 0x%08x, server runs 0x%08x. giving up\n", errx(1, "you run 0x%08x, server runs 0x%08x. giving up\n",
VERSION, version); VERSION, payload);
/* NOTREACHED */ /* NOTREACHED */
} }
} else
warnx("did not receive proper login challenge\n");
} }
}
if (i == 4)
errx(1, "couldn't connect to server");
printf("Retrying version check...\n"); printf("Retrying version check...\n");
} }
errx(1, "couldn't connect to server");
/* NOTREACHED */
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++) {
tv.tv_sec = i + 1; tv.tv_sec = i + 1;
@ -377,7 +377,7 @@ handshake(int dns_fd)
read = read_dns(dns_fd, in, sizeof(in)); read = read_dns(dns_fd, in, sizeof(in));
if(read <= 0) { if(read <= 0) {
perror("read"); warn("read");
continue; continue;
} }
@ -394,7 +394,7 @@ handshake(int dns_fd)
tun_setmtu(mtu) == 0) { tun_setmtu(mtu) == 0) {
return 0; return 0;
} else { } else {
warn("Received handshake with bad data"); warnx("Received handshake with bad data");
} }
} else { } else {
printf("Received bad handshake\n"); printf("Received bad handshake\n");