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; 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,53 +505,36 @@ 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;
if(*np == '.') { np = name + 1;
np++;
continue;
}
sscanf(np, "%02X", &r); while(len < buflen && np < domain) {
*dp++ = (char)r; if(*np == '.') {
np+=2; np++;
len++; continue;
} }
packet->len += len; sscanf(np, "%02X", &r);
packet->offset += len; *dp++ = (char)r;
np+=2;
len++;
} }
if(last) { if (len == buflen)
len = packet->len; return -1;
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

@ -34,11 +34,14 @@
#ifndef MAX #ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b))
#endif #endif
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,16 +133,24 @@ 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;
} }
p = strchr(in, '-'); if (read == 0)
*p++ = '\0'; continue;
if (tun_setip(in) == 0 && tun_setmtu(atoi(p)) == 0) p = strchr(in, '-');
return 0; 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"); 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) { continue;
read = snprintf(in, sizeof(in), "%s-%d", "172.30.5.2", 1023);
dnsd_queuepacket(in, read); if(in[0] == 'H' || in[0] == 'h') {
} read = snprintf(out, sizeof(out), "%s-%d", "172.30.5.2", 1023);
continue; 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; 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;