Prepare for encoding switching

This commit is contained in:
Erik Ekman 2007-06-24 10:54:50 +00:00 committed by Erik Ekman
parent f8e9b0449e
commit 1e895fa713

View File

@ -47,7 +47,7 @@ static void send_ping(int fd);
static void send_chunk(int fd); static void send_chunk(int fd);
static int build_hostname(char *buf, size_t buflen, static int build_hostname(char *buf, size_t buflen,
const char *data, const size_t datalen, const char *data, const size_t datalen,
const char *topdomain); const char *topdomain, struct encoder *encoder);
int running = 1; int running = 1;
char password[33]; char password[33];
@ -65,7 +65,11 @@ static int packetpos;
static int packetlen; static int packetlen;
static uint16_t chunkid; static uint16_t chunkid;
static struct encoder *enc; /* Base32 encoder used for non-data packets */
static struct encoder *b32;
/* The encoder used for data packets
* Defaults to Base32, can be changed after handshake */
static struct encoder *dataenc;
static void static void
sighandler(int sig) sighandler(int sig)
@ -86,7 +90,7 @@ send_packet(int fd, char cmd, const char *data, const size_t datalen)
buf[0] = cmd; buf[0] = cmd;
len = build_hostname(buf + 1, sizeof(buf) - 1, data, datalen, topdomain); len = build_hostname(buf + 1, sizeof(buf) - 1, data, datalen, topdomain, b32);
len = dns_encode(packet, sizeof(packet), &q, QR_QUERY, buf, strlen(buf)); len = dns_encode(packet, sizeof(packet), &q, QR_QUERY, buf, strlen(buf));
sendto(fd, packet, len, 0, (struct sockaddr*)&peer, sizeof(peer)); sendto(fd, packet, len, 0, (struct sockaddr*)&peer, sizeof(peer));
@ -95,7 +99,7 @@ send_packet(int fd, char cmd, const char *data, const size_t datalen)
static int static int
build_hostname(char *buf, size_t buflen, build_hostname(char *buf, size_t buflen,
const char *data, const size_t datalen, const char *data, const size_t datalen,
const char *topdomain) const char *topdomain, struct encoder *encoder)
{ {
int encsize; int encsize;
size_t space; size_t space;
@ -103,14 +107,14 @@ build_hostname(char *buf, size_t buflen,
space = MIN(0xFF, buflen) - strlen(topdomain) - 2; space = MIN(0xFF, buflen) - strlen(topdomain) - 2;
if (!enc->places_dots()) if (!encoder->places_dots())
space -= (space / 62); /* space for dots */ space -= (space / 62); /* space for dots */
memset(buf, 0, buflen); memset(buf, 0, buflen);
encsize = enc->encode(buf, &space, data, datalen); encsize = encoder->encode(buf, &space, data, datalen);
if (!enc->places_dots()) if (!encoder->places_dots())
inline_dotify(buf, buflen); inline_dotify(buf, buflen);
b = buf; b = buf;
@ -279,7 +283,7 @@ send_chunk(int fd)
p += packetpos; p += packetpos;
avail = packetlen - packetpos; avail = packetlen - packetpos;
lastlen = build_hostname(buf + 1, sizeof(buf) - 1, p, avail, topdomain); lastlen = build_hostname(buf + 1, sizeof(buf) - 1, p, avail, topdomain, dataenc);
if (lastlen == avail) if (lastlen == avail)
code = 1; code = 1;
@ -536,7 +540,8 @@ main(int argc, char **argv)
device = NULL; device = NULL;
chunkid = 0; chunkid = 0;
enc = get_base32_encoder(); b32 = get_base32_encoder();
dataenc = get_base32_encoder();
while ((choice = getopt(argc, argv, "vfhu:t:d:P:")) != -1) { while ((choice = getopt(argc, argv, "vfhu:t:d:P:")) != -1) {
switch(choice) { switch(choice) {