From db62c7f82cc00e2da76be197e822d84da1bccde3 Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Sun, 4 Feb 2007 23:12:08 +0000 Subject: [PATCH] extracted common code from send_* in iodine.c --- src/dns.c | 4 ++- src/dns.h | 2 +- src/encoding.c | 6 ++-- src/encoding.h | 2 +- src/iodine.c | 90 ++++++++++++++++++++++---------------------------- 5 files changed, 47 insertions(+), 57 deletions(-) diff --git a/src/dns.c b/src/dns.c index 363e155..850270d 100644 --- a/src/dns.c +++ b/src/dns.c @@ -197,7 +197,9 @@ dns_decode(char *buf, size_t buflen, struct query *q, qr_t qr, char *packet, siz } int -dns_build_hostname(char *buf, size_t buflen, char *data, size_t datalen, char *topdomain) +dns_build_hostname(char *buf, size_t buflen, + const char *data, const size_t datalen, + const char *topdomain) { int avail; int written; diff --git a/src/dns.h b/src/dns.h index dad6fd2..8946993 100644 --- a/src/dns.h +++ b/src/dns.h @@ -35,7 +35,7 @@ int dns_encode_hostname(const char *, char *, int); int dns_encode(char *, size_t, struct query *, qr_t, char *, size_t); int dns_decode(char *, size_t, struct query *, qr_t, char *, size_t); -int dns_build_hostname(char *, size_t, char *, size_t, char *); +int dns_build_hostname(char *, size_t, const char *, const size_t, const char *); void dnsd_send(int, struct query*, char *, int); diff --git a/src/encoding.c b/src/encoding.c index e54e900..7337a0e 100644 --- a/src/encoding.c +++ b/src/encoding.c @@ -34,7 +34,7 @@ static int reverse_init = 0; /* Eat 5 bytes from src, write 8 bytes to dest */ static void -encode_chunk(char *dest, char *src) +encode_chunk(char *dest, const char *src) { unsigned char c; @@ -102,7 +102,7 @@ decode_chunk(char *dest, char *src) } int -encode_data(char *buf, int len, int space, char *dest) +encode_data(const char *buf, const size_t len, int space, char *dest) { int final; int write; @@ -112,8 +112,8 @@ encode_data(char *buf, int len, int space, char *dest) int i; char encoded[255]; char padding[5]; + const char *dp; char *pp; - char *dp; char *ep; space -= space / SPACING; diff --git a/src/encoding.h b/src/encoding.h index c16b1b9..94b43ca 100644 --- a/src/encoding.h +++ b/src/encoding.h @@ -17,7 +17,7 @@ #ifndef _ENCODING_H_ #define _ENCODING_H_ -int encode_data(char *, int, int, char *); +int encode_data(const char *, const size_t, int, char *); int decode_data(char *, int, const char *, char *); #endif /* _ENCODING_H_ */ diff --git a/src/iodine.c b/src/iodine.c index 9d6ff09..bef1428 100644 --- a/src/iodine.c +++ b/src/iodine.c @@ -64,12 +64,31 @@ sighandler(int sig) running = 0; } +static void +send_packet(int fd, char cmd, const char *data, const size_t datalen) +{ + char packet[4096]; + struct query q; + char buf[4096]; + size_t len; + + q.id = rand_seed; + q.type = T_NULL; + + buf[0] = cmd; + + len = dns_build_hostname(buf + 1, sizeof(buf) - 1, data, datalen, topdomain); + len = dns_encode(packet, sizeof(packet), &q, QR_QUERY, buf, strlen(buf)); + + sendto(fd, packet, len, 0, (struct sockaddr*)&peer, sizeof(peer)); +} + static void dns_send_chunk(int fd) { - char packet[512]; + char packet[4096]; struct query q; - char buf[256]; + char buf[4096]; int avail; char *p; int len; @@ -188,8 +207,8 @@ tunnel(int tun_fd, int dns_fd) { struct timeval tv; fd_set fds; - int i; int rv; + int i; rv = 0; @@ -203,20 +222,21 @@ tunnel(int tun_fd, int dns_fd) FD_SET(dns_fd, &fds); i = select(MAX(tun_fd, dns_fd) + 1, &fds, NULL, NULL, &tv); - - if (running == 0 || i < 0) { - rv = 1; - break; - } + if (running == 0) + break; + + if (i < 0) + err(1, "select"); + if (i == 0) /* timeout */ send_ping(dns_fd); - else { - if(FD_ISSET(tun_fd, &fds)) { + else { + if (FD_ISSET(tun_fd, &fds)) { if (tunnel_tun(tun_fd, dns_fd) <= 0) continue; } - if(FD_ISSET(dns_fd, &fds)) { + if (FD_ISSET(dns_fd, &fds)) { if (tunnel_dns(tun_fd, dns_fd) <= 0) continue; } @@ -229,14 +249,8 @@ tunnel(int tun_fd, int dns_fd) void send_login(int fd, char *login, int len) { - char packet[512]; - struct query q; - char buf[256]; char data[18]; - q.id = rand_seed; - q.type = T_NULL; - memset(data, 0, sizeof(data)); memcpy(data, login, MIN(len, 16)); @@ -245,22 +259,13 @@ send_login(int fd, char *login, int len) rand_seed++; - buf[0] = 'L'; - len = dns_build_hostname(buf + 1, sizeof(buf) - 1, data, sizeof(data), topdomain); - len = dns_encode(packet, sizeof(packet), &q, QR_QUERY, buf, strlen(buf)); - - sendto(fd, packet, len, 0, (struct sockaddr*)&peer, sizeof(peer)); + send_packet(fd, 'L', data, sizeof(data)); } static void send_ping(int fd) { - - char packet[512]; - struct query q; - char buf[256]; char data[2]; - int len; if (dns_sending()) { lastlen = 0; @@ -268,32 +273,18 @@ send_ping(int fd) packetlen = 0; } - q.id = rand_seed; - q.type = T_NULL; - data[0] = (rand_seed >> 8) & 0xff; data[1] = (rand_seed >> 0) & 0xff; rand_seed++; - buf[0] = 'P'; - len = dns_build_hostname(buf + 1, sizeof(buf) - 1, data, sizeof(data), topdomain); - len = dns_encode(packet, sizeof(packet), &q, QR_QUERY, buf, strlen(buf)); - - sendto(fd, packet, len, 0, (struct sockaddr*)&peer, sizeof(peer)); + send_packet(fd, 'P', data, sizeof(data)); } void send_version(int fd, uint32_t version) { - char packet[512]; - struct query q; - char buf[256]; char data[6]; - int len; - - q.id = rand_seed; - q.type = T_NULL; data[0] = (version >> 24) & 0xff; data[1] = (version >> 16) & 0xff; @@ -305,11 +296,7 @@ send_version(int fd, uint32_t version) rand_seed++; - buf[0] = 'V'; - len = dns_build_hostname(buf + 1, sizeof(buf) - 1, data, sizeof(data), topdomain); - len = dns_encode(packet, sizeof(packet), &q, QR_QUERY, buf, strlen(buf)); - - sendto(fd, packet, len, 0, (struct sockaddr*)&peer, sizeof(peer)); + send_packet(fd, 'V', data, sizeof(data)); } static int @@ -352,16 +339,17 @@ handshake(int dns_fd) if (read >= 8) { memcpy(&seed, in + 4, 4); seed = ntohl(seed); - printf("Version ok, both running 0x%08x\n", VERSION); + printf("version ok, both running 0x%08x\n", VERSION); break; } else { - printf("Version ok but did not receive proper login challenge\n"); + printf("version ok but did not receive proper login challenge\n"); } } else { memcpy(&version, in + 4, 4); version = ntohl(version); - printf("You run 0x%08x, server runs 0x%08x. Giving up\n", VERSION, version); - return 1; + errx(1, "you run 0x%08x, server runs 0x%08x. giving up\n", + VERSION, version); + /* NOTREACHED */ } } }