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

51
dns.c
View file

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

View file

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

View file

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

4
tun.c
View file

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