extracted common code from send_* in iodine.c

This commit is contained in:
Bjorn Andersson 2007-02-04 23:12:08 +00:00
parent da636b1f47
commit db62c7f82c
5 changed files with 47 additions and 57 deletions

View file

@ -197,7 +197,9 @@ dns_decode(char *buf, size_t buflen, struct query *q, qr_t qr, char *packet, siz
} }
int 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 avail;
int written; int written;

View file

@ -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_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_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); void dnsd_send(int, struct query*, char *, int);

View file

@ -34,7 +34,7 @@ static int reverse_init = 0;
/* Eat 5 bytes from src, write 8 bytes to dest */ /* Eat 5 bytes from src, write 8 bytes to dest */
static void static void
encode_chunk(char *dest, char *src) encode_chunk(char *dest, const char *src)
{ {
unsigned char c; unsigned char c;
@ -102,7 +102,7 @@ decode_chunk(char *dest, char *src)
} }
int 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 final;
int write; int write;
@ -112,8 +112,8 @@ encode_data(char *buf, int len, int space, char *dest)
int i; int i;
char encoded[255]; char encoded[255];
char padding[5]; char padding[5];
const char *dp;
char *pp; char *pp;
char *dp;
char *ep; char *ep;
space -= space / SPACING; space -= space / SPACING;

View file

@ -17,7 +17,7 @@
#ifndef _ENCODING_H_ #ifndef _ENCODING_H_
#define _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 *); int decode_data(char *, int, const char *, char *);
#endif /* _ENCODING_H_ */ #endif /* _ENCODING_H_ */

View file

@ -64,12 +64,31 @@ sighandler(int sig)
running = 0; 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 static void
dns_send_chunk(int fd) dns_send_chunk(int fd)
{ {
char packet[512]; char packet[4096];
struct query q; struct query q;
char buf[256]; char buf[4096];
int avail; int avail;
char *p; char *p;
int len; int len;
@ -188,8 +207,8 @@ tunnel(int tun_fd, int dns_fd)
{ {
struct timeval tv; struct timeval tv;
fd_set fds; fd_set fds;
int i;
int rv; int rv;
int i;
rv = 0; rv = 0;
@ -204,19 +223,20 @@ tunnel(int tun_fd, int dns_fd)
i = select(MAX(tun_fd, dns_fd) + 1, &fds, NULL, NULL, &tv); i = select(MAX(tun_fd, dns_fd) + 1, &fds, NULL, NULL, &tv);
if (running == 0 || i < 0) { if (running == 0)
rv = 1;
break; break;
}
if (i < 0)
err(1, "select");
if (i == 0) /* timeout */ if (i == 0) /* timeout */
send_ping(dns_fd); send_ping(dns_fd);
else { else {
if(FD_ISSET(tun_fd, &fds)) { if (FD_ISSET(tun_fd, &fds)) {
if (tunnel_tun(tun_fd, dns_fd) <= 0) if (tunnel_tun(tun_fd, dns_fd) <= 0)
continue; continue;
} }
if(FD_ISSET(dns_fd, &fds)) { if (FD_ISSET(dns_fd, &fds)) {
if (tunnel_dns(tun_fd, dns_fd) <= 0) if (tunnel_dns(tun_fd, dns_fd) <= 0)
continue; continue;
} }
@ -229,14 +249,8 @@ tunnel(int tun_fd, int dns_fd)
void void
send_login(int fd, char *login, int len) send_login(int fd, char *login, int len)
{ {
char packet[512];
struct query q;
char buf[256];
char data[18]; char data[18];
q.id = rand_seed;
q.type = T_NULL;
memset(data, 0, sizeof(data)); memset(data, 0, sizeof(data));
memcpy(data, login, MIN(len, 16)); memcpy(data, login, MIN(len, 16));
@ -245,22 +259,13 @@ send_login(int fd, char *login, int len)
rand_seed++; rand_seed++;
buf[0] = 'L'; send_packet(fd, 'L', data, sizeof(data));
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));
} }
static void static void
send_ping(int fd) send_ping(int fd)
{ {
char packet[512];
struct query q;
char buf[256];
char data[2]; char data[2];
int len;
if (dns_sending()) { if (dns_sending()) {
lastlen = 0; lastlen = 0;
@ -268,32 +273,18 @@ send_ping(int fd)
packetlen = 0; packetlen = 0;
} }
q.id = rand_seed;
q.type = T_NULL;
data[0] = (rand_seed >> 8) & 0xff; data[0] = (rand_seed >> 8) & 0xff;
data[1] = (rand_seed >> 0) & 0xff; data[1] = (rand_seed >> 0) & 0xff;
rand_seed++; rand_seed++;
buf[0] = 'P'; send_packet(fd, 'P', data, sizeof(data));
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));
} }
void void
send_version(int fd, uint32_t version) send_version(int fd, uint32_t version)
{ {
char packet[512];
struct query q;
char buf[256];
char data[6]; char data[6];
int len;
q.id = rand_seed;
q.type = T_NULL;
data[0] = (version >> 24) & 0xff; data[0] = (version >> 24) & 0xff;
data[1] = (version >> 16) & 0xff; data[1] = (version >> 16) & 0xff;
@ -305,11 +296,7 @@ send_version(int fd, uint32_t version)
rand_seed++; rand_seed++;
buf[0] = 'V'; send_packet(fd, 'V', data, sizeof(data));
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));
} }
static int static int
@ -352,16 +339,17 @@ handshake(int dns_fd)
if (read >= 8) { if (read >= 8) {
memcpy(&seed, in + 4, 4); memcpy(&seed, in + 4, 4);
seed = ntohl(seed); seed = ntohl(seed);
printf("Version ok, both running 0x%08x\n", VERSION); printf("version ok, both running 0x%08x\n", VERSION);
break; break;
} else { } else {
printf("Version ok but did not receive proper login challenge\n"); printf("version ok but did not receive proper login challenge\n");
} }
} else { } else {
memcpy(&version, in + 4, 4); memcpy(&version, in + 4, 4);
version = ntohl(version); version = ntohl(version);
printf("You run 0x%08x, server runs 0x%08x. Giving up\n", VERSION, version); errx(1, "you run 0x%08x, server runs 0x%08x. giving up\n",
return 1; VERSION, version);
/* NOTREACHED */
} }
} }
} }