Refactored to make it easier to add unit tests

This commit is contained in:
Erik Ekman 2009-09-20 08:43:48 +00:00
parent 5c64a44d5e
commit 94f1d670d4
6 changed files with 37 additions and 34 deletions

View File

@ -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)

View File

@ -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);

View File

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

View File

@ -15,8 +15,39 @@
*/
#include <string.h>
#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)

View File

@ -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);

View File

@ -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 */