Refactored to make it easier to add unit tests

This commit is contained in:
Erik Ekman 2009-09-20 08:43:48 +00:00 committed by Erik Ekman
parent 7e9ce2716b
commit a1a2e3cefe
6 changed files with 37 additions and 34 deletions

View File

@ -120,7 +120,7 @@ client_get_conn()
} }
void void
client_set_nameserver(const char *cp) client_set_nameserver(const char *cp, int port)
{ {
struct in_addr addr; struct in_addr addr;
@ -129,7 +129,7 @@ client_set_nameserver(const char *cp)
memset(&nameserv, 0, sizeof(nameserv)); memset(&nameserv, 0, sizeof(nameserv));
nameserv.sin_family = AF_INET; nameserv.sin_family = AF_INET;
nameserv.sin_port = htons(53); nameserv.sin_port = htons(port);
nameserv.sin_addr = addr; 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); 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 static void
send_packet(int fd, char cmd, const char *data, const size_t datalen) send_packet(int fd, char cmd, const char *data, const size_t datalen)

View File

@ -23,7 +23,7 @@ void client_stop();
enum connection client_get_conn(); enum connection client_get_conn();
const char *client_get_raw_addr(); 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_topdomain(const char *cp);
void client_set_password(const char *cp); void client_set_password(const char *cp);

View File

@ -40,6 +40,7 @@ extern const unsigned char raw_header[RAW_HDR_LEN];
#include <netinet/in.h> #include <netinet/in.h>
#endif #endif
#define DNS_PORT 53
#ifndef MIN #ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b)) #define MIN(a,b) ((a)<(b)?(a):(b))

View File

@ -15,8 +15,39 @@
*/ */
#include <string.h> #include <string.h>
#include "common.h"
#include "encoding.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 int
unpack_data(char *buf, size_t buflen, char *data, size_t datalen, struct encoder *enc) unpack_data(char *buf, size_t buflen, char *data, size_t datalen, struct encoder *enc)

View File

@ -27,6 +27,7 @@ struct encoder {
int (*blocksize_encoded)(void); 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 unpack_data(char *, size_t, char *, size_t, struct encoder *);
int inline_dotify(char *, size_t); int inline_dotify(char *, size_t);
int inline_undotify(char *, size_t); int inline_undotify(char *, size_t);

View File

@ -232,7 +232,7 @@ main(int argc, char **argv)
} }
if (nameserv_addr) { if (nameserv_addr) {
client_set_nameserver(nameserv_addr); client_set_nameserver(nameserv_addr, DNS_PORT);
} else { } else {
usage(); usage();
/* NOTREACHED */ /* NOTREACHED */