From a1a2e3cefe013e9323abfabed432225630baffc6 Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Sun, 20 Sep 2009 08:43:48 +0000 Subject: [PATCH] Refactored to make it easier to add unit tests --- src/client.c | 34 ++-------------------------------- src/client.h | 2 +- src/common.h | 1 + src/encoding.c | 31 +++++++++++++++++++++++++++++++ src/encoding.h | 1 + src/iodine.c | 2 +- 6 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/client.c b/src/client.c index 0dc7464..046b2cd 100644 --- a/src/client.c +++ b/src/client.c @@ -120,7 +120,7 @@ client_get_conn() } void -client_set_nameserver(const char *cp) +client_set_nameserver(const char *cp, int port) { struct in_addr addr; @@ -129,7 +129,7 @@ client_set_nameserver(const char *cp) memset(&nameserv, 0, sizeof(nameserv)); nameserv.sin_family = AF_INET; - nameserv.sin_port = htons(53); + nameserv.sin_port = htons(port); nameserv.sin_addr = addr; } @@ -191,36 +191,6 @@ send_raw_data(int dns_fd) send_raw(dns_fd, outpkt.data, outpkt.len, userid, RAW_HDR_CMD_DATA); } -static int -build_hostname(char *buf, size_t buflen, - const char *data, const size_t datalen, - const char *topdomain, struct encoder *encoder) -{ - int encsize; - size_t space; - char *b; - - space = MIN(0xFF, buflen) - strlen(topdomain) - 7; - if (!encoder->places_dots()) - space -= (space / 57); /* space for dots */ - - memset(buf, 0, buflen); - - encsize = encoder->encode(buf, &space, data, datalen); - - if (!encoder->places_dots()) - inline_dotify(buf, buflen); - - b = buf; - b += strlen(buf); - - if (*b != '.') - *b++ = '.'; - - strncpy(b, topdomain, strlen(topdomain)+1); - - return space; -} static void send_packet(int fd, char cmd, const char *data, const size_t datalen) diff --git a/src/client.h b/src/client.h index 0df4d3d..865ae4f 100644 --- a/src/client.h +++ b/src/client.h @@ -23,7 +23,7 @@ void client_stop(); enum connection client_get_conn(); const char *client_get_raw_addr(); -void client_set_nameserver(const char *cp); +void client_set_nameserver(const char *cp, int port); void client_set_topdomain(const char *cp); void client_set_password(const char *cp); diff --git a/src/common.h b/src/common.h index 3570058..d5ecc4e 100644 --- a/src/common.h +++ b/src/common.h @@ -40,6 +40,7 @@ extern const unsigned char raw_header[RAW_HDR_LEN]; #include #endif +#define DNS_PORT 53 #ifndef MIN #define MIN(a,b) ((a)<(b)?(a):(b)) diff --git a/src/encoding.c b/src/encoding.c index 6878e5e..e92cfe5 100644 --- a/src/encoding.c +++ b/src/encoding.c @@ -15,8 +15,39 @@ */ #include +#include "common.h" #include "encoding.h" +int +build_hostname(char *buf, size_t buflen, + const char *data, const size_t datalen, + const char *topdomain, struct encoder *encoder) +{ + int encsize; + size_t space; + char *b; + + space = MIN(0xFF, buflen) - strlen(topdomain) - 7; + if (!encoder->places_dots()) + space -= (space / 57); /* space for dots */ + + memset(buf, 0, buflen); + + encsize = encoder->encode(buf, &space, data, datalen); + + if (!encoder->places_dots()) + inline_dotify(buf, buflen); + + b = buf; + b += strlen(buf); + + if (*b != '.') + *b++ = '.'; + + strncpy(b, topdomain, strlen(topdomain)+1); + + return space; +} int unpack_data(char *buf, size_t buflen, char *data, size_t datalen, struct encoder *enc) diff --git a/src/encoding.h b/src/encoding.h index dda2c13..d2ac4f2 100644 --- a/src/encoding.h +++ b/src/encoding.h @@ -27,6 +27,7 @@ struct encoder { int (*blocksize_encoded)(void); }; +int build_hostname(char *, size_t, const char *, const size_t, const char *, struct encoder *); int unpack_data(char *, size_t, char *, size_t, struct encoder *); int inline_dotify(char *, size_t); int inline_undotify(char *, size_t); diff --git a/src/iodine.c b/src/iodine.c index 27aaf46..8d12c04 100644 --- a/src/iodine.c +++ b/src/iodine.c @@ -232,7 +232,7 @@ main(int argc, char **argv) } if (nameserv_addr) { - client_set_nameserver(nameserv_addr); + client_set_nameserver(nameserv_addr, DNS_PORT); } else { usage(); /* NOTREACHED */