moved packetpacking out from dns.c

This commit is contained in:
Bjorn Andersson 2006-06-11 18:07:26 +00:00
parent 2134d77dba
commit 20e2ffe5e2
4 changed files with 70 additions and 69 deletions

63
dns.c
View file

@ -55,14 +55,6 @@ short delayed_q_id;
struct sockaddr_in delayed_q_from;
int delayed_q_fromlen;
struct packet
{
int len;
int offset;
char data[64*1024];
};
struct packet packetbuf;
int
open_dns(const char *host, const char *domain)
@ -513,53 +505,36 @@ dnsd_forceack(int fd)
}
static int
decodepacket(const char *name, struct packet *packet)
decodepacket(const char *name, char *buf, int buflen)
{
int r;
int len;
int last;
int ping;
int hello;
char *dp;
char *domain;
const char *np;
len = 0;
last = (name[0] == '1');
ping = (name[0] == 'p' || name[0] == 'P');
hello = (name[0] == 'h' || name[0] == 'H');
len = 1;
domain = strstr(name, topdomain);
if (!ping && !hello && domain) {
np = name + 1;
dp = packet->data + packet->offset;
buf[0] = name[0];
while(np < domain) {
if(*np == '.') {
np++;
continue;
}
dp = buf;
np = name + 1;
sscanf(np, "%02X", &r);
*dp++ = (char)r;
np+=2;
len++;
while(len < buflen && np < domain) {
if(*np == '.') {
np++;
continue;
}
packet->len += len;
packet->offset += len;
}
if(last) {
len = packet->len;
packet->len = packet->offset = 0;
} else if (hello) {
len = GOTHELLO;
} else {
len = 0;
sscanf(np, "%02X", &r);
*dp++ = (char)r;
np+=2;
len++;
}
if (len == buflen)
return -1;
return len;
}
@ -616,13 +591,7 @@ dnsd_read(int fd, char *buf, int buflen)
memcpy((struct sockaddr*)&delayed_q_from, (struct sockaddr*)&from, addrlen);
}
r = decodepacket(name, &packetbuf);
if (r > 0) {
memcpy(buf, packetbuf.data, r);
}
return r;
return decodepacket(name, buf, buflen);
}
}
}

View file

@ -37,8 +37,11 @@
int running = 1;
int tun_fd;
int dns_fd;
static void
sigint(int sig) {
sighandler(int sig) {
running = 0;
}
@ -107,6 +110,7 @@ handshake(int dns_fd)
int i;
int r;
char *p;
int mtu;
int read;
fd_set fds;
int timeout;
@ -129,16 +133,24 @@ handshake(int dns_fd)
if(r > 0) {
read = dns_read(dns_fd, in, sizeof(in));
if(read <= 0) {
if(read < 0) {
perror("read");
continue;
}
p = strchr(in, '-');
*p++ = '\0';
if (read == 0)
continue;
if (tun_setip(in) == 0 && tun_setmtu(atoi(p)) == 0)
return 0;
p = strchr(in, '-');
if (p) {
*p++ = '\0';
mtu = atoi(p);
printf("%s %d\n", in, mtu);
if (tun_setip(in) == 0 && tun_setmtu(atoi(p)) == 0)
return 0;
}
}
printf("Retrying...\n");
@ -181,8 +193,6 @@ int
main(int argc, char **argv)
{
int choice;
int tun_fd;
int dns_fd;
char *newroot;
char *username;
int foreground;
@ -239,7 +249,8 @@ main(int argc, char **argv)
if ((dns_fd = open_dns(argv[0], argv[1])) == -1)
goto cleanup2;
signal(SIGINT, sigint);
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);
if(handshake(dns_fd))
goto cleanup2;

View file

@ -37,6 +37,15 @@
int running = 1;
struct packet
{
int len;
int offset;
char data[64*1024];
};
struct packet packetbuf;
static void
sigint(int sig) {
running = 0;
@ -90,18 +99,28 @@ tunnel(int tun_fd, int dns_fd)
}
if(FD_ISSET(dns_fd, &fds)) {
read = dnsd_read(dns_fd, in, sizeof(in));
if (read <= 0) {
if (read == GOTHELLO) {
read = snprintf(in, sizeof(in), "%s-%d", "172.30.5.2", 1023);
dnsd_queuepacket(in, read);
}
continue;
if (read < 0)
continue;
if(in[0] == 'H' || in[0] == 'h') {
read = snprintf(out, sizeof(out), "%s-%d", "172.30.5.2", 1023);
dnsd_queuepacket(out, read);
} else if(in[0] == '0') {
memcpy(packetbuf.data + packetbuf.offset, in, read);
packetbuf.len += read;
packetbuf.offset += read;
} else if(in[0] == '1') {
memcpy(packetbuf.data + packetbuf.offset, in, read);
packetbuf.len += read;
packetbuf.offset += read;
outlen = sizeof(out);
uncompress(out, &outlen, packetbuf.data, packetbuf.len);
write_tun(tun_fd, out, outlen);
packetbuf.len = packetbuf.offset = 0;
}
outlen = sizeof(out);
uncompress(out, &outlen, in, read);
write_tun(tun_fd, out, outlen);
}
}
}

4
tun.c
View file

@ -112,7 +112,7 @@ open_tun()
return -1;
}
#endif /* LINUX */
#endif /* !LINUX */
void
close_tun(int tun_fd)
@ -194,6 +194,8 @@ tun_setmtu(const int mtu)
printf("Setting MTU of %s to %d\n", if_name, mtu);
return system(cmdline);
} else {
warn("MTU out of range: %d\n", mtu);
}
return 1;